Mercurial > hg > orthanc-stone
annotate Framework/Viewport/SdlViewport.cpp @ 1214:741201eab130 broker
removing pointer to viewport from ViewportController
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Dec 2019 19:12:08 +0100 |
parents | 86a8266b8888 |
children | 5147277850cf |
rev | line source |
---|---|
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Stone of Orthanc |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 **/ |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 #include "SdlViewport.h" |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 #include <Core/OrthancException.h> |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 namespace OrthancStone |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 { |
1205 | 27 SdlViewport::SdlViewport() |
28 { | |
29 refreshEvent_ = SDL_RegisterEvents(1); | |
30 | |
31 if (refreshEvent_ == static_cast<uint32_t>(-1)) | |
32 { | |
33 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
34 } | |
35 } | |
36 | |
37 | |
38 void SdlViewport::SendRefreshEvent() | |
39 { | |
40 SDL_Event event; | |
41 SDL_memset(&event, 0, sizeof(event)); | |
42 event.type = refreshEvent_; | |
43 SDL_PushEvent(&event); // This function is thread-safe, and can be called from other threads safely. | |
44 } | |
45 | |
46 | |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
47 SdlOpenGLViewport::SdlOpenGLViewport(const char* title, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
48 unsigned int width, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
49 unsigned int height, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
50 bool allowDpiScaling) : |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
909
diff
changeset
|
51 context_(title, width, height, allowDpiScaling) |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
52 { |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1205
diff
changeset
|
53 compositor_.reset(new OpenGLCompositor(context_)); |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
54 } |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
55 |
1205 | 56 void SdlOpenGLViewport::Invalidate() |
57 { | |
58 SendRefreshEvent(); | |
59 } | |
60 | |
1213
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
61 void SdlOpenGLViewport::Paint(const Scene2D& scene) |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 { |
1203
f3bb9a6dd949
locking abstraction in IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
63 boost::mutex::scoped_lock lock(mutex_); |
1213
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
64 compositor_->Refresh(scene); |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 } |
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
67 |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
68 SdlCairoViewport::SdlCairoViewport(const char* title, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
69 unsigned int width, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
70 unsigned int height, |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
71 bool allowDpiScaling) : |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
72 window_(title, width, height, false /* enable OpenGL */, allowDpiScaling), |
1211
d10d2acb8a02
compositors do not keep a reference to the scene anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1205
diff
changeset
|
73 compositor_(width, height), |
1205 | 74 sdlSurface_(NULL) |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
75 { |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
76 } |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
77 |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
78 SdlCairoViewport::~SdlCairoViewport() |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 { |
905
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
80 if (sdlSurface_) |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
81 { |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
82 SDL_FreeSurface(sdlSurface_); |
88bf49aebc13
introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
Alain Mazy <alain@mazy.be>
parents:
893
diff
changeset
|
83 } |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 } |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
909
diff
changeset
|
85 |
1213
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
86 void SdlCairoViewport::Paint(const Scene2D& scene) |
1203
f3bb9a6dd949
locking abstraction in IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
87 { |
f3bb9a6dd949
locking abstraction in IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
88 boost::mutex::scoped_lock lock(mutex_); |
1213
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
89 |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
90 compositor_.Refresh(scene); |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
91 CreateSdlSurfaceFromCompositor(); |
1205 | 92 |
93 if (sdlSurface_ != NULL) | |
94 { | |
95 window_.Render(sdlSurface_); | |
96 } | |
97 } | |
98 | |
99 void SdlCairoViewport::Invalidate() | |
100 { | |
101 SendRefreshEvent(); | |
1203
f3bb9a6dd949
locking abstraction in IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
102 } |
f3bb9a6dd949
locking abstraction in IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
103 |
909 | 104 void SdlCairoViewport::UpdateSize(unsigned int width, |
105 unsigned int height) | |
106 { | |
1213
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
107 { |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
108 boost::mutex::scoped_lock lock(mutex_); |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
109 compositor_.UpdateSize(width, height); |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
110 } |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
111 |
86a8266b8888
moving the scene from IViewport to ViewportController
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1211
diff
changeset
|
112 Invalidate(); |
909 | 113 } |
947
1091b2adeb5a
Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents:
909
diff
changeset
|
114 |
1205 | 115 void SdlCairoViewport::CreateSdlSurfaceFromCompositor() // Assumes that the mutex is locked |
909 | 116 { |
117 static const uint32_t rmask = 0x00ff0000; | |
118 static const uint32_t gmask = 0x0000ff00; | |
119 static const uint32_t bmask = 0x000000ff; | |
120 | |
1205 | 121 const unsigned int width = compositor_.GetCanvas().GetWidth(); |
122 const unsigned int height = compositor_.GetCanvas().GetHeight(); | |
123 | |
124 if (sdlSurface_ != NULL) | |
125 { | |
126 if (sdlSurface_->pixels == compositor_.GetCanvas().GetBuffer() && | |
127 sdlSurface_->w == static_cast<int>(width) && | |
128 sdlSurface_->h == static_cast<int>(height) && | |
129 sdlSurface_->pitch == static_cast<int>(compositor_.GetCanvas().GetPitch())) | |
130 { | |
131 // The image from the compositor has not changed, no need to update the surface | |
132 return; | |
133 } | |
134 else | |
135 { | |
136 SDL_FreeSurface(sdlSurface_); | |
137 } | |
138 } | |
139 | |
909 | 140 sdlSurface_ = SDL_CreateRGBSurfaceFrom((void*)(compositor_.GetCanvas().GetBuffer()), width, height, 32, |
141 compositor_.GetCanvas().GetPitch(), rmask, gmask, bmask, 0); | |
142 if (!sdlSurface_) | |
143 { | |
144 LOG(ERROR) << "Cannot create a SDL surface from a Cairo surface"; | |
145 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
146 } | |
147 } | |
891
0aff28f15ea2
new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 } |