blob: 05bba117fce91033f86b8af7ceee26a8e2a5a2b2 [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
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000014#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000015#include "GrGLUtil.h"
twiz@google.com0f31ca72011-03-18 17:38:11 +000016
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000017/**
18 * Helper struct for dealing with the fact that Ganesh and GL use different
19 * window coordinate systems (top-down vs bottom-up)
20 */
21struct GrGLIRect {
twiz@google.com0f31ca72011-03-18 17:38:11 +000022 GrGLint fLeft;
23 GrGLint fBottom;
24 GrGLsizei fWidth;
25 GrGLsizei fHeight;
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000026
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027 void pushToGLViewport(const GrGLInterface* gl) const {
28 GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000029 }
30
bsalomon@google.com0b77d682011-08-19 13:28:54 +000031 void pushToGLScissor(const GrGLInterface* gl) const {
32 GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000033 }
34
bsalomon@google.com0b77d682011-08-19 13:28:54 +000035 void setFromGLViewport(const GrGLInterface* gl) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000036 GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
bsalomon@google.com0b77d682011-08-19 13:28:54 +000037 GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000038 }
39
40 // sometimes we have a GrIRect from the client that we
41 // want to simultaneously make relative to GL's viewport
42 // and convert from top-down to bottom-up.
43 void setRelativeTo(const GrGLIRect& glRect,
44 int leftOffset,
45 int topOffset,
46 int width,
47 int height) {
48 fLeft = glRect.fLeft + leftOffset;
49 fWidth = width;
50 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
51 fHeight = height;
52
53 GrAssert(fLeft >= 0);
54 GrAssert(fWidth >= 0);
55 GrAssert(fBottom >= 0);
56 GrAssert(fHeight >= 0);
57 }
58
59 bool contains(const GrGLIRect& glRect) const {
60 return fLeft <= glRect.fLeft &&
61 fBottom <= glRect.fBottom &&
62 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
63 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
64 }
65
66 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
67
68 bool operator ==(const GrGLIRect& glRect) const {
69 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
70 }
71
72 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
73};
74
reed@google.comaff86f32011-02-18 21:07:35 +000075#endif