| 1 | /* |
| 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. |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | |
| 10 | #ifndef GrNativeRect_DEFINED |
| 11 | #define GrNativeRect_DEFINED |
| 12 | |
| 13 | #include "include/core/SkRect.h" |
| 14 | #include "include/gpu/GrTypes.h" |
| 15 | |
| 16 | /** |
| 17 | * Helper struct for dealing with bottom-up surface origins (bottom-up instead of top-down). |
| 18 | */ |
| 19 | struct GrNativeRect { |
| 20 | int fX; |
| 21 | int fY; |
| 22 | int fWidth; |
| 23 | int fHeight; |
| 24 | |
| 25 | static GrNativeRect MakeRelativeTo(GrSurfaceOrigin origin, int rtHeight, SkIRect devRect) { |
| 26 | GrNativeRect nativeRect; |
| 27 | nativeRect.setRelativeTo(org: origin, rtHeight, devRect); |
| 28 | return nativeRect; |
| 29 | } |
| 30 | |
| 31 | static SkIRect MakeIRectRelativeTo(GrSurfaceOrigin origin, int rtHeight, SkIRect devRect) { |
| 32 | return MakeRelativeTo(origin, rtHeight, devRect).asSkIRect(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * cast-safe way to treat the rect as an array of (4) ints. |
| 37 | */ |
| 38 | const int* asInts() const { |
| 39 | return &fX; |
| 40 | |
| 41 | static_assert(0 == offsetof(GrNativeRect, fX)); |
| 42 | static_assert(4 == offsetof(GrNativeRect, fY)); |
| 43 | static_assert(8 == offsetof(GrNativeRect, fWidth)); |
| 44 | static_assert(12 == offsetof(GrNativeRect, fHeight)); |
| 45 | static_assert(16 == sizeof(GrNativeRect)); // For an array of GrNativeRect. |
| 46 | } |
| 47 | int* asInts() { return &fX; } |
| 48 | |
| 49 | SkIRect asSkIRect() const { return SkIRect::MakeXYWH(x: fX, y: fY, w: fWidth, h: fHeight); } |
| 50 | |
| 51 | // sometimes we have a SkIRect from the client that we |
| 52 | // want to simultaneously make relative to GL's viewport |
| 53 | // and (optionally) convert from top-down to bottom-up. |
| 54 | // The GL's viewport will always be the full size of the |
| 55 | // current render target so we just pass in the rtHeight |
| 56 | // here. |
| 57 | void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) { |
| 58 | this->setRelativeTo(origin: org, surfaceHeight: rtHeight, leftOffset: devRect.x(), topOffset: devRect.y(), width: devRect.width(), |
| 59 | height: devRect.height()); |
| 60 | } |
| 61 | |
| 62 | void setRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, int topOffset, |
| 63 | int width, int height) { |
| 64 | fX = leftOffset; |
| 65 | fWidth = width; |
| 66 | if (kBottomLeft_GrSurfaceOrigin == origin) { |
| 67 | fY = surfaceHeight - topOffset - height; |
| 68 | } else { |
| 69 | fY = topOffset; |
| 70 | } |
| 71 | fHeight = height; |
| 72 | |
| 73 | SkASSERT(fWidth >= 0); |
| 74 | SkASSERT(fHeight >= 0); |
| 75 | } |
| 76 | |
| 77 | bool contains(int width, int height) const { |
| 78 | return fX <= 0 && |
| 79 | fY <= 0 && |
| 80 | fX + fWidth >= width && |
| 81 | fY + fHeight >= height; |
| 82 | } |
| 83 | |
| 84 | void invalidate() {fX = fWidth = fY = fHeight = -1;} |
| 85 | bool isInvalid() const { return fX == -1 && fWidth == -1 && fY == -1 |
| 86 | && fHeight == -1; } |
| 87 | |
| 88 | bool operator ==(const GrNativeRect& that) const { |
| 89 | return 0 == memcmp(s1: this, s2: &that, n: sizeof(GrNativeRect)); |
| 90 | } |
| 91 | |
| 92 | bool operator !=(const GrNativeRect& that) const {return !(*this == that);} |
| 93 | }; |
| 94 | |
| 95 | #endif |
| 96 | |