blob: ec60029519e69387bc76ec204e0176136c405413 [file] [log] [blame]
twiz@google.com0f31ca72011-03-18 17:38:11 +00001/*
2 Copyright 2011 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000017
18#ifndef GrGLIRect_DEFINED
19#define GrGLIRect_DEFINED
20
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000021#include "GrGLInterface.h"
twiz@google.com0f31ca72011-03-18 17:38:11 +000022
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000023/**
24 * Helper struct for dealing with the fact that Ganesh and GL use different
25 * window coordinate systems (top-down vs bottom-up)
26 */
27struct GrGLIRect {
twiz@google.com0f31ca72011-03-18 17:38:11 +000028 GrGLint fLeft;
29 GrGLint fBottom;
30 GrGLsizei fWidth;
31 GrGLsizei fHeight;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000032
33 void pushToGLViewport() const {
reed@google.comaff86f32011-02-18 21:07:35 +000034 GR_GL(Viewport(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000035 }
36
37 void pushToGLScissor() const {
38 GR_GL(Scissor(fLeft, fBottom, fWidth, fHeight));
39 }
40
41 void setFromGLViewport() {
twiz@google.com0f31ca72011-03-18 17:38:11 +000042 GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
43 GR_GL_GetIntegerv(GR_GL_VIEWPORT, (GrGLint*) this);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000044 }
45
46 // sometimes we have a GrIRect from the client that we
47 // want to simultaneously make relative to GL's viewport
48 // and convert from top-down to bottom-up.
49 void setRelativeTo(const GrGLIRect& glRect,
50 int leftOffset,
51 int topOffset,
52 int width,
53 int height) {
54 fLeft = glRect.fLeft + leftOffset;
55 fWidth = width;
56 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
57 fHeight = height;
58
59 GrAssert(fLeft >= 0);
60 GrAssert(fWidth >= 0);
61 GrAssert(fBottom >= 0);
62 GrAssert(fHeight >= 0);
63 }
64
65 bool contains(const GrGLIRect& glRect) const {
66 return fLeft <= glRect.fLeft &&
67 fBottom <= glRect.fBottom &&
68 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
69 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
70 }
71
72 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
73
74 bool operator ==(const GrGLIRect& glRect) const {
75 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
76 }
77
78 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
79};
80
reed@google.comaff86f32011-02-18 21:07:35 +000081#endif