blob: 44f5280fd69fe370729b7720a382b605dc802356 [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
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
commit-bot@chromium.org63150af2013-04-11 22:00:22 +000039 // sometimes we have a SkIRect from the client that we
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000040 // want to simultaneously make relative to GL's viewport
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000041 // and (optionally) convert from top-down to bottom-up.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000042 void setRelativeTo(const GrGLIRect& glRect,
43 int leftOffset,
44 int topOffset,
45 int width,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000046 int height,
47 GrSurfaceOrigin origin) {
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000048 fLeft = glRect.fLeft + leftOffset;
49 fWidth = width;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +000050 if (kBottomLeft_GrSurfaceOrigin == origin) {
51 fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height);
52 } else {
53 fBottom = glRect.fBottom + topOffset;
54 }
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000055 fHeight = height;
56
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000057 SkASSERT(fLeft >= 0);
58 SkASSERT(fWidth >= 0);
59 SkASSERT(fBottom >= 0);
60 SkASSERT(fHeight >= 0);
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000061 }
62
63 bool contains(const GrGLIRect& glRect) const {
64 return fLeft <= glRect.fLeft &&
65 fBottom <= glRect.fBottom &&
66 fLeft + fWidth >= glRect.fLeft + glRect.fWidth &&
67 fBottom + fHeight >= glRect.fBottom + glRect.fHeight;
68 }
69
70 void invalidate() {fLeft = fWidth = fBottom = fHeight = -1;}
71
72 bool operator ==(const GrGLIRect& glRect) const {
73 return 0 == memcmp(this, &glRect, sizeof(GrGLIRect));
74 }
75
76 bool operator !=(const GrGLIRect& glRect) const {return !(*this == glRect);}
77};
78
reed@google.comaff86f32011-02-18 21:07:35 +000079#endif