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 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 10 | #ifndef GrNativeRect_DEFINED |
| 11 | #define GrNativeRect_DEFINED |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 12 | |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/gpu/GrTypes.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 | /** |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 17 | * Helper struct for dealing with bottom-up surface origins (bottom-up instead of top-down). |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 18 | */ |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 19 | struct GrNativeRect { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 20 | int fX; |
| 21 | int fY; |
| 22 | int fWidth; |
| 23 | int fHeight; |
| 24 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 25 | static GrNativeRect MakeRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) { |
| 26 | GrNativeRect nativeRect; |
| 27 | nativeRect.setRelativeTo(org, rtHeight, devRect); |
| 28 | return nativeRect; |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 29 | } |
| 30 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 31 | static GrNativeRect MakeRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, |
| 32 | int topOffset, int width, int height) { |
| 33 | GrNativeRect nativeRect; |
| 34 | nativeRect.setRelativeTo(origin, surfaceHeight, leftOffset, topOffset, width, height); |
| 35 | return nativeRect; |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 36 | } |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 37 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 38 | /** |
| 39 | * cast-safe way to treat the rect as an array of (4) ints. |
| 40 | */ |
| 41 | const int* asInts() const { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 42 | return &fX; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 43 | |
Brian Salomon | b0047b5 | 2019-12-05 19:52:25 +0000 | [diff] [blame] | 44 | GR_STATIC_ASSERT(0 == offsetof(GrNativeRect, fX)); |
| 45 | GR_STATIC_ASSERT(4 == offsetof(GrNativeRect, fY)); |
| 46 | GR_STATIC_ASSERT(8 == offsetof(GrNativeRect, fWidth)); |
| 47 | GR_STATIC_ASSERT(12 == offsetof(GrNativeRect, fHeight)); |
| 48 | GR_STATIC_ASSERT(16 == sizeof(GrNativeRect)); // For an array of GrNativeRect. |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 49 | } |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 50 | int* asInts() { return &fX; } |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 51 | |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 52 | SkIRect asSkIRect() const { return SkIRect::MakeXYWH(fX, fY, fWidth, fHeight); } |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 54 | // sometimes we have a SkIRect from the client that we |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 55 | // want to simultaneously make relative to GL's viewport |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 56 | // and (optionally) convert from top-down to bottom-up. |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 57 | // The GL's viewport will always be the full size of the |
| 58 | // current render target so we just pass in the rtHeight |
| 59 | // here. |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 60 | void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) { |
| 61 | this->setRelativeTo(org, rtHeight, devRect.x(), devRect.y(), devRect.width(), |
| 62 | devRect.height()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 65 | void setRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, int topOffset, |
| 66 | int width, int height) { |
| 67 | fX = leftOffset; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 68 | fWidth = width; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 69 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 70 | fY = surfaceHeight - topOffset - height; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 71 | } else { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 72 | fY = topOffset; |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 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 | |
Greg Daniel | acd66b4 | 2019-05-22 16:29:12 -0400 | [diff] [blame] | 80 | bool contains(int width, int height) const { |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 81 | return fX <= 0 && |
| 82 | fY <= 0 && |
| 83 | fX + fWidth >= width && |
| 84 | fY + fHeight >= height; |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Dalton | 76500e5 | 2019-09-05 02:13:05 -0600 | [diff] [blame] | 87 | void invalidate() {fX = fWidth = fY = fHeight = -1;} |
| 88 | bool isInvalid() const { return fX == -1 && fWidth == -1 && fY == -1 |
Adrienne Walker | 3a69c74 | 2018-05-21 11:05:48 -0700 | [diff] [blame] | 89 | && fHeight == -1; } |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 90 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 91 | bool operator ==(const GrNativeRect& that) const { |
| 92 | return 0 == memcmp(this, &that, sizeof(GrNativeRect)); |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Dalton | d6cda8d | 2019-09-05 02:30:04 -0600 | [diff] [blame] | 95 | bool operator !=(const GrNativeRect& that) const {return !(*this == that);} |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
reed@google.com | aff86f3 | 2011-02-18 21:07:35 +0000 | [diff] [blame] | 98 | #endif |