comparison Framework/Viewport/SdlViewport.h @ 1216:5147277850cf broker

better abstraction for IViewport
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Dec 2019 16:36:40 +0100
parents 9efa66d8d3f8
children 0ca50d275b9a
comparison
equal deleted inserted replaced
1215:9efa66d8d3f8 1216:5147277850cf
46 namespace OrthancStone 46 namespace OrthancStone
47 { 47 {
48 class SdlViewport : public IViewport 48 class SdlViewport : public IViewport
49 { 49 {
50 private: 50 private:
51 uint32_t refreshEvent_; 51 boost::mutex mutex_;
52 uint32_t refreshEvent_;
53 boost::shared_ptr<ViewportController> controller_;
54 std::auto_ptr<ICompositor> compositor_;
55
56 void SendRefreshEvent();
52 57
53 protected: 58 protected:
54 void SendRefreshEvent(); 59 class SdlLock : public ILock
55 60 {
61 private:
62 SdlViewport& that_;
63 boost::mutex::scoped_lock lock_;
64
65 public:
66 SdlLock(SdlViewport& that) :
67 that_(that),
68 lock_(that.mutex_)
69 {
70 }
71
72 virtual bool HasCompositor() const ORTHANC_OVERRIDE
73 {
74 return true;
75 }
76
77 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE;
78
79 virtual ViewportController& GetController() ORTHANC_OVERRIDE
80 {
81 return *that_.controller_;
82 }
83
84 virtual void Invalidate() ORTHANC_OVERRIDE
85 {
86 that_.SendRefreshEvent();
87 }
88 };
89
90 void ClearCompositor()
91 {
92 compositor_.reset();
93 }
94
95 void AcquireCompositor(ICompositor* compositor /* takes ownership */);
96
56 public: 97 public:
57 SdlViewport(); 98 SdlViewport();
58 99
59 bool IsRefreshEvent(const SDL_Event& event) const 100 bool IsRefreshEvent(const SDL_Event& event) const
60 { 101 {
61 return (event.type == refreshEvent_); 102 return (event.type == refreshEvent_);
62 } 103 }
63 104
105 virtual ILock* Lock() ORTHANC_OVERRIDE
106 {
107 return new SdlLock(*this);
108 }
109
64 virtual void UpdateSize(unsigned int width, 110 virtual void UpdateSize(unsigned int width,
65 unsigned int height) = 0; 111 unsigned int height) = 0;
66 112
67 virtual void ToggleMaximize() = 0; 113 virtual void ToggleMaximize() = 0;
114
115 // Must be invoked from the main SDL thread
116 virtual void Paint() = 0;
68 }; 117 };
69 118
70 119
71 class SdlOpenGLViewport : public SdlViewport 120 class SdlOpenGLViewport : public SdlViewport
72 { 121 {
73 private: 122 private:
74 boost::mutex mutex_; 123 SdlOpenGLContext context_;
75 SdlOpenGLContext context_;
76 std::auto_ptr<OpenGLCompositor> compositor_;
77 124
78 class SdlLock : public ILock
79 {
80 private:
81 SdlOpenGLViewport& that_;
82 boost::mutex::scoped_lock lock_;
83
84 public:
85 SdlLock(SdlOpenGLViewport& viewport) :
86 that_(viewport),
87 lock_(viewport.mutex_)
88 {
89 }
90
91 virtual bool HasCompositor() const ORTHANC_OVERRIDE
92 {
93 return true;
94 }
95
96 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
97 {
98 return *that_.compositor_;
99 }
100 };
101
102 public: 125 public:
103 SdlOpenGLViewport(const char* title, 126 SdlOpenGLViewport(const char* title,
104 unsigned int width, 127 unsigned int width,
105 unsigned int height, 128 unsigned int height,
106 bool allowDpiScaling = true); 129 bool allowDpiScaling = true);
107 130
108 virtual void Invalidate() ORTHANC_OVERRIDE; 131 virtual ~SdlOpenGLViewport();
109 132
110 virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE; 133 virtual void Paint() ORTHANC_OVERRIDE;
111 134
112 virtual ILock* Lock() ORTHANC_OVERRIDE 135 virtual void UpdateSize(unsigned int width,
113 { 136 unsigned int height) ORTHANC_OVERRIDE;
114 return new SdlLock(*this);
115 }
116 137
117 virtual void UpdateSize(unsigned int width, unsigned int height) ORTHANC_OVERRIDE 138 virtual void ToggleMaximize() ORTHANC_OVERRIDE;
118 {
119 // nothing to do in OpenGL, the OpenGLCompositor::UpdateSize will be called automatically
120 }
121
122 virtual void ToggleMaximize() ORTHANC_OVERRIDE
123 {
124 boost::mutex::scoped_lock lock(mutex_);
125 context_.ToggleMaximize();
126 }
127 }; 139 };
128 140
129 141
130 class SdlCairoViewport : public SdlViewport 142 class SdlCairoViewport : public SdlViewport
131 { 143 {
132 private: 144 private:
133 class SdlLock : public ILock 145 SdlWindow window_;
134 { 146 SDL_Surface* sdlSurface_;
135 private:
136 SdlCairoViewport& that_;
137 boost::mutex::scoped_lock lock_;
138
139 public:
140 SdlLock(SdlCairoViewport& viewport) :
141 that_(viewport),
142 lock_(viewport.mutex_)
143 {
144 }
145 147
146 virtual bool HasCompositor() const ORTHANC_OVERRIDE 148 void CreateSdlSurfaceFromCompositor(CairoCompositor& compositor);
147 {
148 return true;
149 }
150
151 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
152 {
153 return that_.compositor_;
154 }
155 };
156
157 boost::mutex mutex_;
158 SdlWindow window_;
159 CairoCompositor compositor_;
160 SDL_Surface* sdlSurface_;
161
162 void CreateSdlSurfaceFromCompositor();
163 149
164 public: 150 public:
165 SdlCairoViewport(const char* title, 151 SdlCairoViewport(const char* title,
166 unsigned int width, 152 unsigned int width,
167 unsigned int height, 153 unsigned int height,
168 bool allowDpiScaling = true); 154 bool allowDpiScaling = true);
169 155
170 ~SdlCairoViewport(); 156 virtual ~SdlCairoViewport();
171 157
172 virtual void Invalidate() ORTHANC_OVERRIDE; 158 virtual void Paint() ORTHANC_OVERRIDE;
173
174 virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE;
175
176 virtual ILock* Lock() ORTHANC_OVERRIDE
177 {
178 return new SdlLock(*this);
179 }
180 159
181 virtual void UpdateSize(unsigned int width, 160 virtual void UpdateSize(unsigned int width,
182 unsigned int height) ORTHANC_OVERRIDE; 161 unsigned int height) ORTHANC_OVERRIDE;
183 162
184 virtual void ToggleMaximize() ORTHANC_OVERRIDE 163 virtual void ToggleMaximize() ORTHANC_OVERRIDE;
185 {
186 boost::mutex::scoped_lock lock(mutex_);
187 window_.ToggleMaximize();
188 }
189 }; 164 };
190 } 165 }