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 
MakeRelativeToGrNativeRect25     static GrNativeRect MakeRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) {
26         GrNativeRect nativeRect;
27         nativeRect.setRelativeTo(org, rtHeight, devRect);
28         return nativeRect;
29     }
30 
MakeRelativeToGrNativeRect31     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;
36     }
37 
38     /**
39      *  cast-safe way to treat the rect as an array of (4) ints.
40      */
asIntsGrNativeRect41     const int* asInts() const {
42         return &fX;
43 
44         static_assert(0 == offsetof(GrNativeRect, fX));
45         static_assert(4 == offsetof(GrNativeRect, fY));
46         static_assert(8 == offsetof(GrNativeRect, fWidth));
47         static_assert(12 == offsetof(GrNativeRect, fHeight));
48         static_assert(16 == sizeof(GrNativeRect));  // For an array of GrNativeRect.
49     }
asIntsGrNativeRect50     int* asInts() { return &fX; }
51 
asSkIRectGrNativeRect52     SkIRect asSkIRect() const { return SkIRect::MakeXYWH(fX, fY, fWidth, fHeight); }
53 
54     // sometimes we have a SkIRect from the client that we
55     // want to simultaneously make relative to GL's viewport
56     // and (optionally) convert from top-down to bottom-up.
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.
setRelativeToGrNativeRect60     void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) {
61         this->setRelativeTo(org, rtHeight, devRect.x(), devRect.y(), devRect.width(),
62                             devRect.height());
63     }
64 
setRelativeToGrNativeRect65     void setRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, int topOffset,
66                        int width, int height) {
67         fX = leftOffset;
68         fWidth = width;
69         if (kBottomLeft_GrSurfaceOrigin == origin) {
70             fY = surfaceHeight - topOffset - height;
71         } else {
72             fY = topOffset;
73         }
74         fHeight = height;
75 
76         SkASSERT(fWidth >= 0);
77         SkASSERT(fHeight >= 0);
78     }
79 
containsGrNativeRect80     bool contains(int width, int height) const {
81         return fX <= 0 &&
82                fY <= 0 &&
83                fX + fWidth >= width &&
84                fY + fHeight >= height;
85     }
86 
invalidateGrNativeRect87     void invalidate() {fX = fWidth = fY = fHeight = -1;}
isInvalidGrNativeRect88     bool isInvalid() const { return fX == -1 && fWidth == -1 && fY == -1
89         && fHeight == -1; }
90 
91     bool operator ==(const GrNativeRect& that) const {
92         return 0 == memcmp(this, &that, sizeof(GrNativeRect));
93     }
94 
95     bool operator !=(const GrNativeRect& that) const {return !(*this == that);}
96 };
97 
98 #endif
99