• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 origin, int rtHeight, SkIRect devRect) {
26         GrNativeRect nativeRect;
27         nativeRect.setRelativeTo(origin, rtHeight, devRect);
28         return nativeRect;
29     }
30 
MakeIRectRelativeToGrNativeRect31     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      */
asIntsGrNativeRect38     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     }
asIntsGrNativeRect47     int* asInts() { return &fX; }
48 
asSkIRectGrNativeRect49     SkIRect asSkIRect() const { return SkIRect::MakeXYWH(fX, fY, fWidth, 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.
setRelativeToGrNativeRect57     void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect& devRect) {
58         this->setRelativeTo(org, rtHeight, devRect.x(), devRect.y(), devRect.width(),
59                             devRect.height());
60     }
61 
setRelativeToGrNativeRect62     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 
containsGrNativeRect77     bool contains(int width, int height) const {
78         return fX <= 0 &&
79                fY <= 0 &&
80                fX + fWidth >= width &&
81                fY + fHeight >= height;
82     }
83 
invalidateGrNativeRect84     void invalidate() {fX = fWidth = fY = fHeight = -1;}
isInvalidGrNativeRect85     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(this, &that, sizeof(GrNativeRect));
90     }
91 
92     bool operator !=(const GrNativeRect& that) const {return !(*this == that);}
93 };
94 
95 #endif
96