diff Framework/Viewport/SdlViewport.cpp @ 905:88bf49aebc13

introduced ICompositor and allow SdlViewport to work with a CairoCompositor (to run on machines without OpenGL drivers)
author Alain Mazy <alain@mazy.be>
date Wed, 17 Jul 2019 16:56:53 +0200
parents 0c5201499af8
children 722ee73e6ba2
line wrap: on
line diff
--- a/Framework/Viewport/SdlViewport.cpp	Wed Jul 17 15:33:34 2019 +0200
+++ b/Framework/Viewport/SdlViewport.cpp	Wed Jul 17 16:56:53 2019 +0200
@@ -26,24 +26,105 @@
 
 namespace OrthancStone
 {
-  SdlViewport::SdlViewport(const char* title,
-                           unsigned int width,
-                           unsigned int height,
-                           bool allowDpiScaling) :
-    ViewportBase(title),
+  SdlOpenGLViewport::SdlOpenGLViewport(const char* title,
+                                       unsigned int width,
+                                       unsigned int height,
+                                       bool allowDpiScaling) :
+    SdlViewport(title),
+    context_(title, width, height, allowDpiScaling),
+    compositor_(context_, GetScene())
+  {
+  }
+
+  SdlOpenGLViewport::SdlOpenGLViewport(const char* title,
+                                       unsigned int width,
+                                       unsigned int height,
+                                       boost::shared_ptr<Scene2D>& scene,
+                                       bool allowDpiScaling) :
+    SdlViewport(title, scene),
     context_(title, width, height, allowDpiScaling),
     compositor_(context_, GetScene())
   {
   }
 
-  SdlViewport::SdlViewport(const char* title,
-                           unsigned int width,
-                           unsigned int height,
-                           boost::shared_ptr<Scene2D>& scene,
-                           bool allowDpiScaling) :
-    ViewportBase(title, scene),
-    context_(title, width, height, allowDpiScaling),
-    compositor_(context_, GetScene())
+
+  SdlCairoViewport::SdlCairoViewport(const char* title,
+                                     unsigned int width,
+                                     unsigned int height,
+                                     bool allowDpiScaling) :
+    SdlViewport(title),
+    window_(title, width, height, false /* enable OpenGL */, allowDpiScaling),
+    compositor_(GetScene(), width, height)
+  {
+    static const uint32_t rmask = 0x00ff0000;
+    static const uint32_t gmask = 0x0000ff00;
+    static const uint32_t bmask = 0x000000ff;
+
+    sdlSurface_ = SDL_CreateRGBSurfaceFrom((void*)(compositor_.GetCanvas().GetBuffer()), width, height, 32,
+                                           compositor_.GetCanvas().GetPitch(), rmask, gmask, bmask, 0);
+    if (!sdlSurface_)
+    {
+      LOG(ERROR) << "Cannot create a SDL surface from a Cairo surface";
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+    }
+  }
+
+
+  SdlCairoViewport::~SdlCairoViewport()
   {
+    if (sdlSurface_)
+    {
+      SDL_FreeSurface(sdlSurface_);
+    }
   }
+
+//  void SdlCairoViewport::SetSize(unsigned int width,
+//                                 unsigned int height)
+//  {
+    //    if (cairoSurface_.get() == NULL ||
+    //        cairoSurface_->GetWidth() != width ||
+    //        cairoSurface_->GetHeight() != height)
+    //    {
+    //      cairoSurface_.reset(new CairoSurface(width, height, false /* no alpha */));
+
+    //      // TODO Big endian?
+    //      static const uint32_t rmask = 0x00ff0000;
+    //      static const uint32_t gmask = 0x0000ff00;
+    //      static const uint32_t bmask = 0x000000ff;
+
+    //      if (sdlSurface_)
+    //      {
+    //        SDL_FreeSurface(sdlSurface_);
+    //      }
+
+    //      sdlSurface_ = SDL_CreateRGBSurfaceFrom(cairoSurface_->GetBuffer(), width, height, 32,
+    //                                             cairoSurface_->GetPitch(), rmask, gmask, bmask, 0);
+    //      if (!sdlSurface_)
+    //      {
+    //        LOG(ERROR) << "Cannot create a SDL surface from a Cairo surface";
+    //        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+    //      }
+    //    }
+//  }
+
+
+  void SdlCairoViewport::Refresh()
+  {
+    GetCompositor().Refresh();
+    window_.Render(sdlSurface_);
+  }
+
+
+  //  void SdlCairoViewport::Render()
+  //  {
+  //    Orthanc::ImageAccessor target;
+  //    compositor_.GetCanvas() cairoSurface_->GetWriteableAccessor(target);
+
+  //    if (viewport.Render(target))
+  //    {
+  //      window_.Render(sdlSurface_);
+  //    }
+  //  }
+
+
 }