HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_WindowDrawable.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: RE_WindowDrawable.h (RE Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __RE_WINDOWDRAWABLE_H_INCLUDED__
12 #define __RE_WINDOWDRAWABLE_H_INCLUDED__
13 
14 #include "RE_API.h"
15 
16 // We must include this before the Qt OpenGL headers in order to ensure the
17 // correct linkage on Windows.
18 #include "RE_OGL.h"
19 
20 #include "RE_Render.h"
21 #include <UT/UT_Rect.h>
22 #include <UT/UT_UniquePtr.h>
23 
24 #if defined(USE_QT6)
25 #include <QtOpenGLWidgets/QOpenGLWidget>
26 #else
27 #include <QtWidgets/QOpenGLWidget>
28 #endif
29 #include <QtWidgets/QStylePainter>
30 
31 #undef emit
32 
33 
34 // Object names for QWidget and QWindow (drawable surface) objects used
35 // for OpenGL rendering. Note that the drawable surface name must be exactly
36 // equal to the drawable name plus "Window" as the suffix. This is a
37 // restriction of the QWidgetWindow class.
38 #define RE_WINDOW_DRAWABLE_OBJ_NAME "RE_WindowDrawable"
39 #define RE_WINDOW_DRAWABLE_SURFACE_OBJ_NAME \
40  RE_WINDOW_DRAWABLE_OBJ_NAME "Window"
41 #define RE_WINDOW_DRAWABLE_WRAPPER_OBJ_NAME \
42  RE_WINDOW_DRAWABLE_OBJ_NAME "Wrapper"
43 
44 
45 class QOpenGLContext;
46 class QSurfaceFormat;
47 class RE_OpenGLDrawable;
49 class RE_Window;
50 class RE_WindowDrawable;
53 struct re_GLState;
54 
55 /// Specialized style painter class for RE_WindowDrawable.
56 /// The class manages transitioning between native Houdini (OpenGL) painting
57 /// and Qt styled painting in the window drawable.
58 class RE_API RE_WindowDrawablePainter : public QStylePainter
59 {
60 public:
63 
64  /// Pass key class to help restrict access to the methods that
65  /// control the paint session.
67  {
68  private:
69  friend re_AutoPaintBlockBase;
70  friend RE_OpenGLDrawable;
71  friend RE_WindowDrawable;
72  };
73 
74  ///
75  /// Begin a new paint session.
76  /// This should only be called when not currently in a paint session.
77  ///
78  void beginPaintSession(
79  const PaintSessionPassKey &k,
80  RE_WindowDrawable *drawable, RE_OpenGLDrawable *gl_drawable,
81  RE_QPainterDrawable *qpainter_drawable, RE_Render *render);
82 
83  ///
84  /// End the current paint session.
85  /// This should only be called when currently in a paint session.
86  ///
87  void endPaintSession(const PaintSessionPassKey &k);
88 
89  ///
90  /// Switch to OpenGL painting mode.
91  /// This should only be called when currently in a paint session.
92  ///
93  void beginOpenGLPainting(const PaintSessionPassKey &k);
94 
95  ///
96  /// Switch to Qt styled painting mode.
97  /// This should only be called when currently in a paint session.
98  ///
99  void endOpenGLPainting(const PaintSessionPassKey &k);
100 
101  ///
102  /// Query whether in OpenGL painting mode.
103  /// This should only be called when currently in a paint session.
104  ///
105  bool isOpenGLPainting() const;
106 
107  RE_Render *currentRender() const { return myCurRender; }
108  RE_WindowDrawable *currentDrawable() const { return myCurDrawable; }
110  { return myCurOpenGLDrawable; }
111 
112  /// For debugging. Output the painter's state.
113  void dumpState() const;
114 
115  /// Return the paint state of the current paint session.
116  /// Return nullptr if there is currently no paint session.
117  static RE_WindowDrawablePaintState *currentPaintState();
118 
119 private:
120 
121 #if UT_ASSERT_LEVEL > 0
122  /// Assert that the OpenGL state matches what's cached in RE_Render.
123  void assertGLStateForRender() const;
124 #endif
125 
126  /// Restore the OpenGL state to what's cached in RE_Render.
127  void restoreGLStateForRender() const;
128 
129  /// Save parts of the OpenGL state that Qt/QPainter care about.
130  void saveGLStateForQPainter();
131 
132  /// Restore parts of the OpenGL state that Qt/QPainter care about.
133  void restoreGLStateForQPainter();
134 
135  // The RE_WindwoDrawable that the current painting session applies to.
136  RE_WindowDrawable *myCurDrawable = nullptr;
137 
138  // The RE_OpenGLDrawable that the current non-OpenGL painting session
139  // applies to.
140  RE_OpenGLDrawable *myCurOpenGLDrawable = nullptr;
141 
142  // The RE_QPainterDrawable that the current non-OpenGL painting session
143  // applies to.
144  RE_QPainterDrawable *myCurQPainterDrawable = nullptr;
145 
146  // The RE_Render used in the current painting session.
147  RE_Render *myCurRender = nullptr;
148 
149  // This counter keeps track of nested calls to switch into OpenGL
150  // paint mode. We default to 0 because the painter starts in QPainter
151  // paint mode.
152  int myOpenGLModeLevel = 0;
153 
154  // The OpenGL state that Qt/QPainter care about.
155  struct re_GLState;
156  UT_UniquePtr<re_GLState>myGLState;
157 };
158 
159 /// Class to keep track of the painter and the current paint
160 /// area in a paint session. The paint area y-origin and height
161 /// are specified in Houdini's UI coordinate space
162 /// (i.e. bottom-left origin).
164 {
165 public:
167  RE_WindowDrawablePainter *painter,
168  int paint_area_x, int paint_area_y, int paint_area_w, int paint_area_h);
169  ~RE_WindowDrawablePaintState() = default;
170 
173  const RE_WindowDrawablePaintState &src);
174 
175  RE_WindowDrawablePainter *painter() const;
176 
177  /// Push onto the stack a new paint area.
178  /// This is needed by UI_Viewport, for example, which paints
179  /// into an FBO with a different viewport.
180  void pushPaintArea(
181  int paint_area_x, int paint_area_y,
182  int paint_area_w, int paint_area_h);
183 
184  /// Pop the current paint area from the stack.
185  void popPaintArea();
186 
187  /// Return the current paint area.
188  const UT_DimRect &paintArea() const;
189 
190 #if defined(MBSD)
191  /// Return true if the paint state is currently for a UI_Viewport.
192  bool isForUIViewport() const;
193 #endif
194 
195  /// Convert the given x,y position from the Houdini UI coordinate space
196  /// to Qt's QPainter coordinate space. If x,y is for the (bottom-left)
197  /// corner of a rectangle, then specify the width and height of the
198  /// rectangle to convert the x,y position to the Qt (top-left)
199  /// rectangle.
200  void toPainterPosition(int &x, int &y, int w=0, int h=0) const;
201 
202  /// Convert the given x,y position from the Qt QPainter coordinate space
203  /// to the Houdini UI coordinate space. If x,y is for the (top-left)
204  /// corner of a rectangle, then specify the width and height of the
205  /// rectangle to convert the x,y position to the Houdini UI (bottom-left)
206  /// rectangle.
207  void toUIPosition(int &x, int &y, int w=0, int h=0) const;
208 
209 private:
210  RE_WindowDrawablePainter *myPainter;
211  UT_Array<UT_DimRect> myPaintAreaStack;
212 };
213 
215 {
216 public:
218  = delete;
219 
220  /// Return the underlying paint state.
222  { return myPaintState; }
223 
224  /// Convenience method to access the paint state's painter.
226  { return myPaintState->painter(); }
227 
228  /// Convenience method to access the paint state's paint area.
229  const UT_DimRect &paintArea() const
230  { return myPaintState->paintArea(); }
231 
232  /// Convenience method to call the paint state's toPainterPosition()
233  /// method.
234  void toPainterPosition(int &x, int &y, int w=0, int h=0) const
235  { myPaintState->toPainterPosition(x, y, w, h); }
236 
237  /// Convenience method to call the paint state's toUIPosition()
238  /// method.
239  void toUIPosition(int &x, int &y, int w=0, int h=0) const
240  { myPaintState->toUIPosition(x, y, w, h); }
241 
242 protected:
245 
246  RE_WindowDrawablePaintState *getPaintState() { return myPaintState; }
247 
248 private:
249  RE_WindowDrawablePaintState *myPaintState = nullptr;
250  RE_WindowDrawable *myDrawable = nullptr;
251  RE_OpenGLDrawable *myOpenGLDrawable = nullptr;
252  RE_Render *myRender = nullptr;
253 };
254 
255 /// Construct at the beginning of a painting code block to switch to
256 /// QPainter painting mode. If there is no active paint state, then
257 /// begin a new paint session to create an active paint state.
259 {
260 public:
263 
266  = delete;
267 
268 private:
269  bool mySwitchedToQPainterMode = false;
270 };
271 
272 /// Construct at the beginning of a painting code block to switch to
273 /// OpenGL painting mode. If there is no active paint state, then
274 /// begin a new paint session to create an active paint state.
276 {
277 public:
280 
282  = delete;
283 
284 private:
285  bool mySwitchedToOpenGLMode = false;
286 };
287 
288 
289 /// Window drawable.
290 /// It currently uses an OpenGL backend via QOpenGLWidget.
291 class RE_API RE_WindowDrawable : public QOpenGLWidget
292 {
293 public:
294  RE_WindowDrawable(const QSurfaceFormat &format);
296  const QSurfaceFormat &format, QWidget *parent,
297  QOpenGLContext *sharedContext);
298  ~RE_WindowDrawable() override;
299 
300  /// Assign the drawable to the given window.
301  /// The given drawable will be used to render the contents of the window.
302  void setREWindow(RE_Window *window);
303 
304  /// Return the window that the drawable is currently assigned to.
305  const RE_Window *getREWindow() const { return myREWindow; }
306 
307  /// Multisample sample count this drawable was created with (-1 if none).
308  int samples() const { return mySamples; }
309 
310  static const char *widgetTag() { return RE_WINDOW_DRAWABLE_OBJ_NAME; }
311 
312  /// Return the string tag used to identify whether a QWindow
313  /// is used a drawable surface for Houdini.
314  static const char * drawableSurfaceTag()
316 
317  /// Return the string tag used to identify whether a QWidget
318  /// is the wrapper widget of a drawable in Houdini.
319  static const char *widgetWrapperTag()
321 
322  /// Return the Painter used to paint the contents of the drawable.
323  RE_WindowDrawablePainter &painter();
324 
325 protected:
326  void initializeGL() override;
327  void paintGL() override;
328  void resizeGL(int w, int h) override;
329  void paintEvent(QPaintEvent *e) override;
330  void showEvent(QShowEvent *event) override;
331 
332 private:
333  void init_(
334  const QSurfaceFormat *format, QOpenGLContext *shared_context=nullptr);
335 
336  QOpenGLContext *myInitializedContext = nullptr;
337  RE_Window *myREWindow = nullptr;
338 
339  bool myIsPainting = false;
340  bool myNeedsOwnerUpdate = false;
341 
342  int mySamples = -1;
343 
344  RE_WindowDrawablePainter myPainter;
345 };
346 
347 
348 #endif // __RE_WINDOWDRAWABLE_H_INCLUDED__
int samples() const
Multisample sample count this drawable was created with (-1 if none).
RE_OpenGLDrawable * currentOpenGLDrawable() const
void toPainterPosition(int &x, int &y, int w=0, int h=0) const
RE_WindowDrawable * currentDrawable() const
#define RE_API
Definition: RE_API.h:10
==========================================================================
Definition: RE_Window.h:57
RE_WindowDrawablePaintState * getPaintState()
GLint y
Definition: glcorearb.h:103
IFDmantra you can see code vm_image_mplay_direction endcode When SOHO starts a render
Definition: HDK_Image.dox:266
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
struct _cl_event * event
Definition: glcorearb.h:2961
void toUIPosition(int &x, int &y, int w=0, int h=0) const
RE_Render * currentRender() const
static const char * widgetTag()
static const char * widgetWrapperTag()
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
RE_WindowDrawablePaintState * paintState()
Return the underlying paint state.
GLint GLenum GLint x
Definition: glcorearb.h:409
const UT_DimRect & paintArea() const
Convenience method to access the paint state's paint area.
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
#define RE_WINDOW_DRAWABLE_WRAPPER_OBJ_NAME
LeafData & operator=(const LeafData &)=delete
const RE_Window * getREWindow() const
Return the window that the drawable is currently assigned to.
RE_WindowDrawablePainter * painter() const
Convenience method to access the paint state's painter.
Window drawable that uses only QPainter for the new UI path.
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GLboolean r
Definition: glcorearb.h:1222
#define RE_WINDOW_DRAWABLE_OBJ_NAME
static const char * drawableSurfaceTag()
re_AutoPaintBlockBase & operator=(const re_AutoPaintBlockBase &src)=delete
GLenum src
Definition: glcorearb.h:1793
#define RE_WINDOW_DRAWABLE_SURFACE_OBJ_NAME