blob: a699ae36bd79bc54cd793d1d46c114ed9cebe59a [file] [log] [blame]
twiz@google.com0f31ca72011-03-18 17:38:11 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
twiz@google.com0f31ca72011-03-18 17:38:11 +00006 */
7
bsalomon@google.com8895a7a2011-02-18 16:09:55 +00008
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000010#ifndef GrGLIRect_DEFINED
11#define GrGLIRect_DEFINED
12
tomhudson@google.com6bf38b52012-02-14 15:11:59 +000013#include "gl/GrGLInterface.h"
bsalomon@google.com9c1f1ac2012-05-07 17:09:37 +000014#include "GrGLUtil.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
csmartdalton28341fa2016-08-17 10:00:21 -070026 /**
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 int* asInts() { return &fLeft; }
39
bsalomon@google.com0b77d682011-08-19 13:28:54 +000040 void pushToGLViewport(const GrGLInterface* gl) const {
41 GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000042 }
43
bsalomon@google.com0b77d682011-08-19 13:28:54 +000044 void pushToGLScissor(const GrGLInterface* gl) const {
45 GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight));
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000046 }
47
bsalomon@google.com0b77d682011-08-19 13:28:54 +000048 void setFromGLViewport(const GrGLInterface* gl) {
twiz@google.com0f31ca72011-03-18 17:38:11 +000049 GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint));
bsalomon@google.com0b77d682011-08-19 13:28:54 +000050 GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000051 }
52
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000053 // sometimes we have a SkIRect from the client that we
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000054 // want to simultaneously make relative to GL's viewport
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000055 // and (optionally) convert from top-down to bottom-up.
csmartdalton28341fa2016-08-17 10:00:21 -070056 void setRelativeTo(const GrGLIRect& glViewport, const SkIRect& devRect, GrSurfaceOrigin org) {
57 this->setRelativeTo(glViewport, devRect.x(), devRect.y(), devRect.width(), devRect.height(),
58 org);
59 }
60
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000061 void setRelativeTo(const GrGLIRect& glRect,
62 int leftOffset,
63 int topOffset,
64 int width,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000065 int height,
66 GrSurfaceOrigin origin) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000067 fLeft = glRect.fLeft + leftOffset;
68 fWidth = width;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000069 if (kBottomLeft_GrSurfaceOrigin == origin) {
70 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
71 } else {
72 fBottom = glRect.fBottom + topOffset;
73 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000074 fHeight = height;
75
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000076 SkASSERT(fWidth >= 0);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000077 SkASSERT(fHeight >= 0);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000078 }
79
80 bool contains(const GrGLIRect& glRect) const {
81 return fLeft <= glRect.fLeft &&
82 fBottom <= glRect.fBottom &&
83 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
84 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
85 }
86
87 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
88
89 bool operator ==(const GrGLIRect& glRect) const {
90 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
91 }
92
93 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
94};
95
reed@google.comaff86f32011-02-18 21:07:35 +000096#endif