Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:01:30

0001 // -*- C++ -*-
0002 //
0003 // Package:     Subsystem/Package
0004 // Class  :     FWTGLViewer
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:
0010 //         Created:  Tue, 03 Feb 2015 21:45:22 GMT
0011 //
0012 
0013 // system include files
0014 
0015 #include <stdexcept>
0016 
0017 // user include files
0018 
0019 #include "TMath.h"
0020 
0021 #include "TGLIncludes.h"
0022 #include "TGLFBO.h"
0023 #include "TGLWidget.h"
0024 
0025 #include "Fireworks/Core/interface/FWTGLViewer.h"
0026 
0027 //
0028 // constants, enums and typedefs
0029 //
0030 
0031 //
0032 // static data member definitions
0033 //
0034 
0035 //
0036 // constructors and destructor
0037 //
0038 FWTGLViewer::FWTGLViewer(const TGWindow* parent)
0039     : TGLEmbeddedViewer(parent, nullptr, nullptr, 0), m_fbo(nullptr), m_fbo_w(-1), m_fbo_h(-1) {}
0040 
0041 // FWTGLViewer::FWTGLViewer(const FWTGLViewer& rhs)
0042 // {
0043 //    // do actual copying here;
0044 // }
0045 
0046 FWTGLViewer::~FWTGLViewer() { delete m_fbo; }
0047 
0048 //
0049 // assignment operators
0050 //
0051 // const FWTGLViewer& FWTGLViewer::operator=(const FWTGLViewer& rhs)
0052 // {
0053 //   //An exception safe implementation is
0054 //   FWTGLViewer temp(rhs);
0055 //   swap(rhs);
0056 //
0057 //   return *this;
0058 // }
0059 
0060 //
0061 // member functions
0062 //
0063 
0064 //------------------------------------------------------------------------------
0065 // Draw functions
0066 //------------------------------------------------------------------------------
0067 
0068 void FWTGLViewer::DrawHiLod(Bool_t swap_buffers) {
0069   fRedrawTimer->Stop();
0070 
0071   // Ignore request if GL window or context not yet availible or shown.
0072   if ((!fGLWidget && fGLDevice == -1) || (fGLWidget && !fGLWidget->IsMapped())) {
0073     return;
0074   }
0075 
0076   // Take scene draw lock - to be revisited
0077   if (!TakeLock(kDrawLock)) {
0078     // If taking drawlock fails the previous draw is still in progress
0079     // set timer to do this one later
0080     Error("FWTGLViewer::DrawHiLodNoSwap", "viewer locked - skipping this draw.");
0081     fRedrawTimer->RequestDraw(100, TGLRnrCtx::kLODHigh);
0082     return;
0083   }
0084 
0085   fLOD = TGLRnrCtx::kLODHigh;
0086 
0087   DoDraw(swap_buffers);
0088 }
0089 
0090 void FWTGLViewer::JustSwap() { fGLWidget->SwapBuffers(); }
0091 
0092 //------------------------------------------------------------------------------
0093 // FBO functions
0094 //------------------------------------------------------------------------------
0095 
0096 //______________________________________________________________________________
0097 TGLFBO* FWTGLViewer::MakeFbo() {
0098   // Generate FBO with same dimensions as the viewport.
0099 
0100   return GenerateFbo(fViewport.Width(), fViewport.Height(), kFALSE);
0101 }
0102 
0103 //______________________________________________________________________________
0104 TGLFBO* FWTGLViewer::MakeFboWidth(Int_t width, Bool_t pixel_object_scale) {
0105   // Generate FBO with given width (height scaled proportinally).
0106   // If pixel_object_scale is true (default), the corresponding
0107   // scaling gets calculated from the current window size.
0108 
0109   Float_t scale = Float_t(width) / fViewport.Width();
0110   Int_t height = TMath::Nint(scale * fViewport.Height());
0111 
0112   return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
0113 }
0114 
0115 //______________________________________________________________________________
0116 TGLFBO* FWTGLViewer::MakeFboHeight(Int_t height, Bool_t pixel_object_scale) {
0117   // Generate FBO with given height (width scaled proportinally).
0118   // If pixel_object_scale is true (default), the corresponding
0119   // scaling gets calculated from the current window size.
0120 
0121   Float_t scale = Float_t(height) / fViewport.Height();
0122   Int_t width = TMath::Nint(scale * fViewport.Width());
0123 
0124   return GenerateFbo(width, height, pixel_object_scale ? scale : 0);
0125 }
0126 
0127 //______________________________________________________________________________
0128 TGLFBO* FWTGLViewer::MakeFboScale(Float_t scale, Bool_t pixel_object_scale) {
0129   // Generate FBO with given scale to current window size.
0130   // If pixel_object_scale is true (default), the same scaling is
0131   // used.
0132 
0133   Int_t w = TMath::Nint(scale * fViewport.Width());
0134   Int_t h = TMath::Nint(scale * fViewport.Height());
0135 
0136   return GenerateFbo(w, h, pixel_object_scale ? scale : 0);
0137 }
0138 
0139 //______________________________________________________________________________
0140 TGLFBO* FWTGLViewer::GenerateFbo(Int_t w, Int_t h, Float_t pixel_object_scale) {
0141   // Generate FBO -- function that does the actual work.
0142 
0143   static const TString eh("FWTGLViewer::SavePictureUsingFBO");
0144 
0145   if (!GLEW_EXT_framebuffer_object) {
0146     ::Warning(eh, "Missing FBO support.");
0147   }
0148 
0149   if (!TakeLock(kDrawLock)) {
0150     ::Error(eh, "viewer locked - try later.");
0151     return nullptr;
0152   }
0153 
0154   TUnlocker ulck(this);
0155 
0156   MakeCurrent();
0157 
0158   if (m_fbo == nullptr) {
0159     m_fbo = new TGLFBO();
0160   }
0161   if (m_fbo_w != w || m_fbo_h != h) {
0162     try {
0163       m_fbo->Init(w, h, fGLWidget->GetPixelFormat()->GetSamples());
0164     } catch (std::runtime_error& exc) {
0165       m_fbo_w = m_fbo_h = -1;
0166 
0167       ::Error(eh, "%s", exc.what());
0168       return nullptr;
0169     }
0170 
0171     m_fbo_w = w;
0172     m_fbo_h = h;
0173   }
0174 
0175   TGLRect old_vp(fViewport);
0176   SetViewport(0, 0, w, h);
0177 
0178   Float_t old_scale = 1;
0179   if (pixel_object_scale != 0) {
0180     old_scale = fRnrCtx->GetRenderScale();
0181     fRnrCtx->SetRenderScale(old_scale * pixel_object_scale);
0182   }
0183 
0184   m_fbo->Bind();
0185 
0186   fLOD = TGLRnrCtx::kLODHigh;
0187   fRnrCtx->SetGrabImage(kTRUE);
0188 
0189   DoDraw(kFALSE);
0190 
0191   fRnrCtx->SetGrabImage(kFALSE);
0192 
0193   m_fbo->Unbind();
0194 
0195   if (pixel_object_scale != 0) {
0196     fRnrCtx->SetRenderScale(old_scale);
0197   }
0198 
0199   SetViewport(old_vp);
0200 
0201   return m_fbo;
0202 }
0203 
0204 //
0205 // const member functions
0206 //
0207 
0208 //
0209 // static member functions
0210 //