twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 10 | #ifndef GrGLIRect_DEFINED |
| 11 | #define GrGLIRect_DEFINED |
| 12 | |
tomhudson@google.com | 6bf38b5 | 2012-02-14 15:11:59 +0000 | [diff] [blame] | 13 | #include "gl/GrGLInterface.h" |
bsalomon@google.com | 9c1f1ac | 2012-05-07 17:09:37 +0000 | [diff] [blame] | 14 | #include "GrGLUtil.h" |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Helper struct for dealing with the fact that Ganesh and GL use different |
| 18 | * window coordinate systems (top-down vs bottom-up) |
| 19 | */ |
| 20 | struct GrGLIRect { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 21 | GrGLint fLeft; |
| 22 | GrGLint fBottom; |
| 23 | GrGLsizei fWidth; |
| 24 | GrGLsizei fHeight; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 25 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame^] | 26 | /** |
| 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.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 40 | void pushToGLViewport(const GrGLInterface* gl) const { |
| 41 | GR_GL_CALL(gl, Viewport(fLeft, fBottom, fWidth, fHeight)); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 44 | void pushToGLScissor(const GrGLInterface* gl) const { |
| 45 | GR_GL_CALL(gl, Scissor(fLeft, fBottom, fWidth, fHeight)); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 46 | } |
| 47 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 48 | void setFromGLViewport(const GrGLInterface* gl) { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 49 | GR_STATIC_ASSERT(sizeof(GrGLIRect) == 4*sizeof(GrGLint)); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 50 | GR_GL_GetIntegerv(gl, GR_GL_VIEWPORT, (GrGLint*) this); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 53 | // sometimes we have a SkIRect from the client that we |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 54 | // want to simultaneously make relative to GL's viewport |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 55 | // and (optionally) convert from top-down to bottom-up. |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame^] | 56 | 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.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 61 | void setRelativeTo(const GrGLIRect& glRect, |
| 62 | int leftOffset, |
| 63 | int topOffset, |
| 64 | int width, |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 65 | int height, |
| 66 | GrSurfaceOrigin origin) { |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 67 | fLeft = glRect.fLeft + leftOffset; |
| 68 | fWidth = width; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 69 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
| 70 | fBottom = glRect.fBottom + (glRect.fHeight - topOffset - height); |
| 71 | } else { |
| 72 | fBottom = glRect.fBottom + topOffset; |
| 73 | } |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 74 | fHeight = height; |
| 75 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 76 | SkASSERT(fWidth >= 0); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 77 | SkASSERT(fHeight >= 0); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 78 | } |
| 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.com | aff86f3 | 2011-02-18 21:07:35 +0000 | [diff] [blame] | 96 | #endif |