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