LibreOffice Module vcl (master)  1
RenderState.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  */
10 
11 #ifndef INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H
12 #define INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H
13 
14 #include <opengl/TextureState.hxx>
15 
16 template<GLenum ENUM_TYPE, typename TYPE>
18 {
19 protected:
20  bool mbTest;
21 
23  : mbTest(false)
24  {
25  }
26 
27  static bool readState()
28  {
29  return glIsEnabled(ENUM_TYPE);
30  }
31 
32 public:
33  void sync()
34  {
35  mbTest = readState();
36  }
37 
38  void enable()
39  {
40  if (!mbTest)
41  {
42  glEnable(ENUM_TYPE);
44  mbTest = true;
45  }
46  else
47  {
48  VCL_GL_INFO(TYPE::className() << ": enable called but already enabled");
49  }
50 #ifdef DBG_UTIL
51  checkState();
52 #endif
53  }
54 
55  void disable()
56  {
57  if (mbTest)
58  {
59  glDisable(ENUM_TYPE);
61  mbTest = false;
62  }
63  else
64  {
65  VCL_GL_INFO(TYPE::className() << ": disable called but already disabled");
66  }
67 #ifdef DBG_UTIL
68  checkState();
69 #endif
70  }
71 
72 #ifdef DBG_UTIL
73  void checkState()
74  {
75  bool bRealState = readState();
76  if (mbTest != bRealState)
77  {
78  VCL_GL_INFO(TYPE::className() << " mismatch! "
79  << "Expected: " << (mbTest ? "enabled" : "disabled")
80  << " but is: " << (bRealState ? "enabled" : "disabled"));
81  }
82  }
83 #endif
84 };
85 
86 class ScissorState : public GenericCapabilityState<GL_SCISSOR_TEST, ScissorState>
87 {
88 private:
89  int mX;
90  int mY;
91  int mWidth;
92  int mHeight;
93 
94 public:
95  static std::string className() { return std::string("ScissorState"); }
96 
98  : mX(0)
99  , mY(0)
100  , mWidth(0)
101  , mHeight(0)
102  {}
103 
104  void set(int x, int y, int width, int height)
105  {
106  if (x != mX || y != mY || width != mWidth || height != mHeight)
107  {
108  glScissor(x, y, width, height);
109  CHECK_GL_ERROR();
110 
111  mX = x;
112  mY = y;
113  mWidth = width;
114  mHeight = height;
115  }
116  }
117 };
118 
119 class StencilState : public GenericCapabilityState<GL_STENCIL_TEST, StencilState>
120 {
121 public:
122  static std::string className() { return std::string("StencilState"); }
123 };
124 
125 class BlendState : public GenericCapabilityState<GL_BLEND, BlendState>
126 {
127  GLenum mnSourceMode;
129 public:
131  : mnSourceMode(GL_ZERO)
132  , mnDestinationMode(GL_ZERO)
133  {}
134 
135  static std::string className() { return std::string("BlendState"); }
136 
137  void func(GLenum nSource, GLenum nDestination)
138  {
139  if (mnSourceMode != nSource || mnDestinationMode != nDestination)
140  {
141  glBlendFunc(nSource, nDestination);
142  CHECK_GL_ERROR();
143  mnSourceMode = nSource;
144  mnDestinationMode = nDestination;
145  }
146  }
147 };
148 
150 {
155 
157 
158 public:
160  {}
161 
162  void viewport(tools::Rectangle aViewPort)
163  {
164  if (aViewPort != maCurrentViewport)
165  {
166  glViewport(aViewPort.Left(), aViewPort.Top(), aViewPort.GetWidth(), aViewPort.GetHeight());
167  CHECK_GL_ERROR();
168  maCurrentViewport = aViewPort;
169  }
170  }
171 
175  BlendState& blend() { return maBlend; }
176 
177  void sync()
178  {
179  VCL_GL_INFO("RenderState::sync");
180  maScissor.sync();
181  maStencil.sync();
182  maBlend.sync();
183  }
184 };
185 
186 #endif // INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H
187 
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
StencilState & stencil()
long GetWidth() const
long GetHeight() const
void set(int x, int y, int width, int height)
static bool readState()
Definition: RenderState.hxx:27
static std::string className()
Definition: RenderState.hxx:95
tools::Rectangle maCurrentViewport
TextureState maTexture
void func(GLenum nSource, GLenum nDestination)
long Top() const
static std::string className()
GLenum mnDestinationMode
#define VCL_GL_INFO(stream)
Helper to do a SAL_INFO as well as a GL log.
ScissorState & scissor()
void viewport(tools::Rectangle aViewPort)
ScissorState maScissor
BlendState & blend()
static std::string className()
TextureState & texture()
long Left() const
StencilState maStencil
#define CHECK_GL_ERROR()
BlendState maBlend
GLenum mnSourceMode