blob: b8638c0af922bef528a4d677b57bf49f83a87991 [file] [log] [blame]
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00001#include "GrGLConfig.h"
2
3#ifndef GrGLIRect_DEFINED
4#define GrGLIRect_DEFINED
5
6/**
7 * Helper struct for dealing with the fact that Ganesh and GL use different
8 * window coordinate systems (top-down vs bottom-up)
9 */
10struct GrGLIRect {
11 GLint fLeft;
12 GLint fBottom;
13 GLsizei fWidth;
14 GLsizei fHeight;
15
16 void pushToGLViewport() const {
17 GR_GL(Viewport(fLeft, fBottom, fWidth, fHeight));
18 }
19
20 void pushToGLScissor() const {
21 GR_GL(Scissor(fLeft, fBottom, fWidth, fHeight));
22 }
23
24 void setFromGLViewport() {
25 GR_STATIC_ASSERT(sizeof(*this) == 4*sizeof(GLint));
26 GR_GL_GetIntegerv(GL_VIEWPORT, (GLint*) this);
27 }
28
29 // sometimes we have a GrIRect from the client that we
30 // want to simultaneously make relative to GL's viewport
31 // and convert from top-down to bottom-up.
32 void setRelativeTo(const GrGLIRect& glRect,
33 int leftOffset,
34 int topOffset,
35 int width,
36 int height) {
37 fLeft = glRect.fLeft + leftOffset;
38 fWidth = width;
39 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
40 fHeight = height;
41
42 GrAssert(fLeft >= 0);
43 GrAssert(fWidth >= 0);
44 GrAssert(fBottom >= 0);
45 GrAssert(fHeight >= 0);
46 }
47
48 bool contains(const GrGLIRect& glRect) const {
49 return fLeft <= glRect.fLeft &&
50 fBottom <= glRect.fBottom &&
51 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
52 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
53 }
54
55 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
56
57 bool operator ==(const GrGLIRect& glRect) const {
58 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
59 }
60
61 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
62};
63
64#endif