Line data Source code
1 : /*
2 : * Copyright 2011 Google Inc.
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 :
9 :
10 : #ifndef GrGLIRect_DEFINED
11 : #define GrGLIRect_DEFINED
12 :
13 : #include "gl/GrGLInterface.h"
14 : #include "GrGLUtil.h"
15 :
16 : /**
17 : * Helper struct for dealing with the fact that Ganesh and GL use different
18 : * window coordinate systems (top-down vs bottom-up)
19 : */
20 : struct GrGLIRect {
21 : GrGLint fLeft;
22 : GrGLint fBottom;
23 : GrGLsizei fWidth;
24 : GrGLsizei fHeight;
25 :
26 : /**
27 : * cast-safe way to treat the rect as an array of (4) ints.
28 : */
29 : const int* asInts() const {
30 : return &fLeft;
31 :
32 : GR_STATIC_ASSERT(0 == offsetof(GrGLIRect, fLeft));
33 : GR_STATIC_ASSERT(4 == offsetof(GrGLIRect, fBottom));
34 : GR_STATIC_ASSERT(8 == offsetof(GrGLIRect, fWidth));
35 : GR_STATIC_ASSERT(12 == offsetof(GrGLIRect, fHeight));
36 : GR_STATIC_ASSERT(16 == sizeof(GrGLIRect)); // For an array of GrGLIRect.
37 : }
38 0 : int* asInts() { return &fLeft; }
39 :
40 0 : void pushToGLViewport(const GrGLInterface* gl) const {
41 0 : GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
42 0 : }
43 :
44 0 : void pushToGLScissor(const GrGLInterface* gl) const {
45 0 : GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
46 0 : }
47 :
48 : void setFromGLViewport(const GrGLInterface* gl) {
49 : GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
50 : GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
51 : }
52 :
53 : // sometimes we have a SkIRect from the client that we
54 : // want to simultaneously make relative to GL's viewport
55 : // and (optionally) convert from top-down to bottom-up.
56 0 : void setRelativeTo(const GrGLIRect& glViewport, const SkIRect& devRect, GrSurfaceOrigin org) {
57 0 : this->setRelativeTo(glViewport, devRect.x(), devRect.y(), devRect.width(), devRect.height(),
58 0 : org);
59 0 : }
60 :
61 0 : void setRelativeTo(const GrGLIRect& glRect,
62 : int leftOffset,
63 : int topOffset,
64 : int width,
65 : int height,
66 : GrSurfaceOrigin origin) {
67 0 : fLeft = glRect.fLeft + leftOffset;
68 0 : fWidth = width;
69 0 : if (kBottomLeft_GrSurfaceOrigin == origin) {
70 0 : fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
71 : } else {
72 0 : fBottom = glRect.fBottom + topOffset;
73 : }
74 0 : fHeight = height;
75 :
76 0 : SkASSERT(fWidth >= 0);
77 0 : SkASSERT(fHeight >= 0);
78 0 : }
79 :
80 0 : bool contains(const GrGLIRect& glRect) const {
81 0 : return fLeft <= glRect.fLeft &&
82 0 : fBottom <= glRect.fBottom &&
83 0 : fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
84 0 : fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
85 : }
86 :
87 0 : void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
88 :
89 0 : bool operator ==(const GrGLIRect& glRect) const {
90 0 : return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
91 : }
92 :
93 0 : bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
94 : };
95 :
96 : #endif
|