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;
48 class RE_Window;
49 class RE_WindowDrawable;
52 struct re_GLState;
53 
54 /// Specialized style painter class for RE_WindowDrawable.
55 /// The class manages transitioning between native Houdini (OpenGL) painting
56 /// and Qt styled painting in the window drawable.
57 class RE_API RE_WindowDrawablePainter : public QStylePainter
58 {
59 public:
62 
63  /// Pass key class to help restrict access to the methods that
64  /// control the paint session.
66  {
67  private:
68  friend re_AutoPaintBlockBase;
69  friend RE_WindowDrawable;
70  };
71 
72  ///
73  /// Begin a new paint session.
74  /// This should only be called when not currently in a paint session.
75  ///
76  void beginPaintSession(
77  const PaintSessionPassKey &k,
78  RE_WindowDrawable *drawable, RE_QPainterDrawable *qpainter_drawable,
79  RE_Render *render);
80 
81  ///
82  /// End the current paint session.
83  /// This should only be called when currently in a paint session.
84  ///
85  void endPaintSession(const PaintSessionPassKey &k);
86 
87  ///
88  /// Switch to OpenGL painting mode.
89  /// This should only be called when currently in a paint session.
90  ///
91  void beginOpenGLPainting(const PaintSessionPassKey &k);
92 
93  ///
94  /// Switch to Qt styled painting mode.
95  /// This should only be called when currently in a paint session.
96  ///
97  void endOpenGLPainting(const PaintSessionPassKey &k);
98 
99  ///
100  /// Query whether in OpenGL painting mode.
101  /// This should only be called when currently in a paint session.
102  ///
103  bool isOpenGLPainting() const;
104 
105  RE_Render *currentRender() const { return myCurRender; }
106  RE_WindowDrawable *currentDrawable() const { return myCurDrawable; }
107 
108  /// For debugging. Output the painter's state.
109  void dumpState() const;
110 
111  /// Return the paint state of the current paint session.
112  /// Return nullptr if there is currently no paint session.
113  static RE_WindowDrawablePaintState *currentPaintState();
114 
115 private:
116 
117 #if UT_ASSERT_LEVEL > 0
118  /// Assert that the OpenGL state matches what's cached in RE_Render.
119  void assertGLStateForRender() const;
120 #endif
121 
122  /// Restore the OpenGL state to what's cached in RE_Render.
123  void restoreGLStateForRender() const;
124 
125  /// Save parts of the OpenGL state that Qt/QPainter care about.
126  void saveGLStateForQPainter();
127 
128  /// Restore parts of the OpenGL state that Qt/QPainter care about.
129  void restoreGLStateForQPainter();
130 
131  // The RE_WindwoDrawable that the current painting session applies to.
132  RE_WindowDrawable *myCurDrawable = nullptr;
133 
134  // The RE_QPainterDrawable that the current non-OpenGL painting session
135  // applies to.
136  RE_QPainterDrawable *myCurQPainterDrawable = nullptr;
137 
138  // The RE_Render used in the current painting session.
139  RE_Render *myCurRender = nullptr;
140 
141  // This counter keeps track of nested calls to switch into OpenGL
142  // paint mode. We default to 0 because the painter starts in QPainter
143  // paint mode.
144  int myOpenGLModeLevel = 0;
145 
146  // The OpenGL state that Qt/QPainter care about.
147  struct re_GLState;
148  UT_UniquePtr<re_GLState>myGLState;
149 };
150 
151 /// Class to keep track of the painter and the current paint
152 /// area in a paint session. The paint area y-origin and height
153 /// are specified in Houdini's UI coordinate space
154 /// (i.e. bottom-left origin).
156 {
157 public:
159  RE_WindowDrawablePainter *painter,
160  int paint_area_x, int paint_area_y, int paint_area_w, int paint_area_h);
161  ~RE_WindowDrawablePaintState() = default;
162 
165  const RE_WindowDrawablePaintState &src);
166 
167  RE_WindowDrawablePainter *painter() const;
168 
169  /// Push onto the stack a new paint area.
170  /// This is needed by UI_Viewport, for example, which paints
171  /// into an FBO with a different viewport.
172  void pushPaintArea(
173  int paint_area_x, int paint_area_y,
174  int paint_area_w, int paint_area_h);
175 
176  /// Pop the current paint area from the stack.
177  void popPaintArea();
178 
179  /// Return the current paint area.
180  const UT_DimRect &paintArea() const;
181 
182 #if defined(MBSD)
183  /// Return true if the paint state is currently for a UI_Viewport.
184  bool isForUIViewport() const;
185 #endif
186 
187  /// Convert the given x,y position from the Houdini UI coordinate space
188  /// to Qt's QPainter coordinate space. If x,y is for the (bottom-left)
189  /// corner of a rectangle, then specify the width and height of the
190  /// rectangle to convert the x,y position to the Qt (top-left)
191  /// rectangle.
192  void toPainterPosition(int &x, int &y, int w=0, int h=0) const;
193 
194  /// Convert the given x,y position from the Qt QPainter coordinate space
195  /// to the Houdini UI coordinate space. If x,y is for the (top-left)
196  /// corner of a rectangle, then specify the width and height of the
197  /// rectangle to convert the x,y position to the Houdini UI (bottom-left)
198  /// rectangle.
199  void toUIPosition(int &x, int &y, int w=0, int h=0) const;
200 
201 private:
202  RE_WindowDrawablePainter *myPainter;
203  UT_Array<UT_DimRect> myPaintAreaStack;
204 };
205 
207 {
208 public:
210  = delete;
211 
212  /// Return the underlying paint state.
214  { return myPaintState; }
215 
216  /// Convenience method to access the paint state's painter.
218  { return myPaintState->painter(); }
219 
220  /// Convenience method to access the paint state's paint area.
221  const UT_DimRect &paintArea() const
222  { return myPaintState->paintArea(); }
223 
224  /// Convenience method to call the paint state's toPainterPosition()
225  /// method.
226  void toPainterPosition(int &x, int &y, int w=0, int h=0) const
227  { myPaintState->toPainterPosition(x, y, w, h); }
228 
229  /// Convenience method to call the paint state's toUIPosition()
230  /// method.
231  void toUIPosition(int &x, int &y, int w=0, int h=0) const
232  { myPaintState->toUIPosition(x, y, w, h); }
233 
234 protected:
237 
238  RE_WindowDrawablePaintState *getPaintState() { return myPaintState; }
239 
240 private:
241  RE_WindowDrawablePaintState *myPaintState = nullptr;
242  RE_WindowDrawable *myDrawable = nullptr;
243  RE_Render *myRender = nullptr;
244 };
245 
246 /// Construct at the beginning of a painting code block to switch to
247 /// QPainter painting mode. If there is no active paint state, then
248 /// begin a new paint session to create an active paint state.
250 {
251 public:
254 
257  = delete;
258 
259 private:
260  bool mySwitchedToQPainterMode = false;
261 };
262 
263 /// Construct at the beginning of a painting code block to switch to
264 /// OpenGL painting mode. If there is no active paint state, then
265 /// begin a new paint session to create an active paint state.
267 {
268 public:
271 
273  = delete;
274 
275 private:
276  bool mySwitchedToOpenGLMode = false;
277 };
278 
279 
280 /// Window drawable.
281 /// It currently uses an OpenGL backend via QOpenGLWidget.
282 class RE_API RE_WindowDrawable : public QOpenGLWidget
283 {
284 public:
285  RE_WindowDrawable(const QSurfaceFormat &format);
287  const QSurfaceFormat &format, QWidget *parent,
288  QOpenGLContext *sharedContext);
289  ~RE_WindowDrawable() override;
290 
291  /// Assign the drawable to the given window.
292  /// The given drawable will be used to render the contents of the window.
293  void setREWindow(RE_Window *window);
294 
295  /// Return the window that the drawable is currently assigned to.
296  const RE_Window *getREWindow() const { return myREWindow; }
297 
298  /// Multisample sample count this drawable was created with (-1 if none).
299  int samples() const { return mySamples; }
300 
301  static const char *widgetTag() { return RE_WINDOW_DRAWABLE_OBJ_NAME; }
302 
303  /// Return the string tag used to identify whether a QWindow
304  /// is used a drawable surface for Houdini.
305  static const char * drawableSurfaceTag()
307 
308  /// Return the string tag used to identify whether a QWidget
309  /// is the wrapper widget of a drawable in Houdini.
310  static const char *widgetWrapperTag()
312 
313  /// Return the Painter used to paint the contents of the drawable.
314  RE_WindowDrawablePainter &painter();
315 
316 protected:
317  void initializeGL() override;
318  void paintGL() override;
319  void resizeGL(int w, int h) override;
320  void paintEvent(QPaintEvent *e) override;
321  void showEvent(QShowEvent *event) override;
322 
323 private:
324  void init_(
325  const QSurfaceFormat *format, QOpenGLContext *shared_context=nullptr);
326 
327  QOpenGLContext *myInitializedContext = nullptr;
328  RE_Window *myREWindow = nullptr;
329 
330  bool myIsPainting = false;
331  bool myNeedsOwnerUpdate = false;
332 
333  int mySamples = -1;
334 
335  RE_WindowDrawablePainter myPainter;
336 };
337 
338 
339 #endif // __RE_WINDOWDRAWABLE_H_INCLUDED__
int samples() const
Multisample sample count this drawable was created with (-1 if none).
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:56
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