comparison Framework/Viewport/SdlViewport.cpp @ 1205:6009c59d8676 broker

fix to sdl
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 02 Dec 2019 14:32:05 +0100
parents f3bb9a6dd949
children d10d2acb8a02
comparison
equal deleted inserted replaced
1204:b519c1c878f1 1205:6009c59d8676
22 22
23 #include <Core/OrthancException.h> 23 #include <Core/OrthancException.h>
24 24
25 namespace OrthancStone 25 namespace OrthancStone
26 { 26 {
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
27 SdlOpenGLViewport::SdlOpenGLViewport(const char* title, 47 SdlOpenGLViewport::SdlOpenGLViewport(const char* title,
28 unsigned int width, 48 unsigned int width,
29 unsigned int height, 49 unsigned int height,
30 bool allowDpiScaling) : 50 bool allowDpiScaling) :
31 context_(title, width, height, allowDpiScaling) 51 context_(title, width, height, allowDpiScaling)
32 { 52 {
33 compositor_.reset(new OpenGLCompositor(context_, GetScene())); 53 compositor_.reset(new OpenGLCompositor(context_, GetScene()));
34 } 54 }
35 55
36 void SdlOpenGLViewport::Refresh() 56 void SdlOpenGLViewport::Invalidate()
57 {
58 SendRefreshEvent();
59 }
60
61 void SdlOpenGLViewport::Paint()
37 { 62 {
38 boost::mutex::scoped_lock lock(mutex_); 63 boost::mutex::scoped_lock lock(mutex_);
39 compositor_->Refresh(); 64 compositor_->Refresh();
40 } 65 }
41 66
43 SdlCairoViewport::SdlCairoViewport(const char* title, 68 SdlCairoViewport::SdlCairoViewport(const char* title,
44 unsigned int width, 69 unsigned int width,
45 unsigned int height, 70 unsigned int height,
46 bool allowDpiScaling) : 71 bool allowDpiScaling) :
47 window_(title, width, height, false /* enable OpenGL */, allowDpiScaling), 72 window_(title, width, height, false /* enable OpenGL */, allowDpiScaling),
48 compositor_(GetScene(), width, height) 73 compositor_(GetScene(), width, height),
74 sdlSurface_(NULL)
49 { 75 {
50 UpdateSdlSurfaceSize(width, height);
51 } 76 }
52 77
53 SdlCairoViewport::~SdlCairoViewport() 78 SdlCairoViewport::~SdlCairoViewport()
54 { 79 {
55 if (sdlSurface_) 80 if (sdlSurface_)
56 { 81 {
57 SDL_FreeSurface(sdlSurface_); 82 SDL_FreeSurface(sdlSurface_);
58 } 83 }
59 } 84 }
60 85
61 void SdlCairoViewport::RefreshInternal() // Assumes that the mutex is locked 86 void SdlCairoViewport::InvalidateInternal() // Assumes that the mutex is locked
62 { 87 {
63 compositor_.Refresh(); 88 compositor_.Refresh();
64 window_.Render(sdlSurface_); 89 CreateSdlSurfaceFromCompositor();
65 } 90 }
66 91
67 void SdlCairoViewport::Refresh() 92 void SdlCairoViewport::Paint()
68 { 93 {
69 boost::mutex::scoped_lock lock(mutex_); 94 boost::mutex::scoped_lock lock(mutex_);
70 RefreshInternal(); 95
96 if (sdlSurface_ != NULL)
97 {
98 window_.Render(sdlSurface_);
99 }
100 }
101
102 void SdlCairoViewport::Invalidate()
103 {
104 {
105 boost::mutex::scoped_lock lock(mutex_);
106 InvalidateInternal();
107 }
108
109 SendRefreshEvent();
71 } 110 }
72 111
73 void SdlCairoViewport::UpdateSize(unsigned int width, 112 void SdlCairoViewport::UpdateSize(unsigned int width,
74 unsigned int height) 113 unsigned int height)
75 { 114 {
76 boost::mutex::scoped_lock lock(mutex_); 115 boost::mutex::scoped_lock lock(mutex_);
77 compositor_.UpdateSize(width, height); 116 compositor_.UpdateSize(width, height);
78 UpdateSdlSurfaceSize(width, height); 117 InvalidateInternal();
79 RefreshInternal();
80 } 118 }
81 119
82 void SdlCairoViewport::UpdateSdlSurfaceSize(unsigned int width, 120 void SdlCairoViewport::CreateSdlSurfaceFromCompositor() // Assumes that the mutex is locked
83 unsigned int height) // Assumes that the mutex is locked
84 { 121 {
85 static const uint32_t rmask = 0x00ff0000; 122 static const uint32_t rmask = 0x00ff0000;
86 static const uint32_t gmask = 0x0000ff00; 123 static const uint32_t gmask = 0x0000ff00;
87 static const uint32_t bmask = 0x000000ff; 124 static const uint32_t bmask = 0x000000ff;
125
126 const unsigned int width = compositor_.GetCanvas().GetWidth();
127 const unsigned int height = compositor_.GetCanvas().GetHeight();
128
129 if (sdlSurface_ != NULL)
130 {
131 if (sdlSurface_->pixels == compositor_.GetCanvas().GetBuffer() &&
132 sdlSurface_->w == static_cast<int>(width) &&
133 sdlSurface_->h == static_cast<int>(height) &&
134 sdlSurface_->pitch == static_cast<int>(compositor_.GetCanvas().GetPitch()))
135 {
136 // The image from the compositor has not changed, no need to update the surface
137 return;
138 }
139 else
140 {
141 SDL_FreeSurface(sdlSurface_);
142 }
143 }
88 144
89 sdlSurface_ = SDL_CreateRGBSurfaceFrom((void*)(compositor_.GetCanvas().GetBuffer()), width, height, 32, 145 sdlSurface_ = SDL_CreateRGBSurfaceFrom((void*)(compositor_.GetCanvas().GetBuffer()), width, height, 32,
90 compositor_.GetCanvas().GetPitch(), rmask, gmask, bmask, 0); 146 compositor_.GetCanvas().GetPitch(), rmask, gmask, bmask, 0);
91 if (!sdlSurface_) 147 if (!sdlSurface_)
92 { 148 {