blob: e94fa21a5a78b10f3d055a38a2cddf6e9369a02c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
twiz@google.com0f31ca72011-03-18 17:38:11 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
twiz@google.com0f31ca72011-03-18 17:38:11 +00007 */
8
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00009
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000011#ifndef GrGLIRect_DEFINED
12#define GrGLIRect_DEFINED
13
bsalomon@google.comf987d1b2011-04-04 17:13:52 +000014#include "GrGLInterface.h"
twiz@google.com0f31ca72011-03-18 17:38:11 +000015
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000016/**
17 * Helper struct for dealing with the fact that Ganesh and GL use different
18 * window coordinate systems (top-down vs bottom-up)
19 */
20struct GrGLIRect {
twiz@google.com0f31ca72011-03-18 17:38:11 +000021 GrGLint fLeft;
22 GrGLint fBottom;
23 GrGLsizei fWidth;
24 GrGLsizei fHeight;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000025
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026 void pushToGLViewport(const GrGLInterface* gl) const {
27 GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000028 }
29
bsalomon@google.com0b77d682011-08-19 13:28:54 +000030 void pushToGLScissor(const GrGLInterface* gl) const {
31 GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000032 }
33
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034 void setFromGLViewport(const GrGLInterface* gl) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000035 GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
bsalomon@google.com0b77d682011-08-19 13:28:54 +000036 GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000037 }
38
39 // sometimes we have a GrIRect from the client that we
40 // want to simultaneously make relative to GL's viewport
41 // and convert from top-down to bottom-up.
42 void setRelativeTo(const GrGLIRect& glRect,
43 int leftOffset,
44 int topOffset,
45 int width,
46 int height) {
47 fLeft = glRect.fLeft + leftOffset;
48 fWidth = width;
49 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
50 fHeight = height;
51
52 GrAssert(fLeft >= 0);
53 GrAssert(fWidth >= 0);
54 GrAssert(fBottom >= 0);
55 GrAssert(fHeight >= 0);
56 }
57
58 bool contains(const GrGLIRect& glRect) const {
59 return fLeft <= glRect.fLeft &&
60 fBottom <= glRect.fBottom &&
61 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
62 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
63 }
64
65 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
66
67 bool operator ==(const GrGLIRect& glRect) const {
68 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
69 }
70
71 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
72};
73
reed@google.comaff86f32011-02-18 21:07:35 +000074#endif