Flutter Engine
The Flutter Engine
GrNativeRect.h
Go to the documentation of this file.
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 */
20 int fX;
21 int fY;
22 int fWidth;
24
25 static GrNativeRect MakeRelativeTo(GrSurfaceOrigin origin, int rtHeight, SkIRect devRect) {
26 GrNativeRect nativeRect;
27 nativeRect.setRelativeTo(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
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(org, rtHeight, devRect.x(), devRect.y(), devRect.width(),
59 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 }
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(this, &that, sizeof(GrNativeRect));
90 }
91
92 bool operator !=(const GrNativeRect& that) const {return !(*this == that);}
93};
94
95#endif
GrSurfaceOrigin
Definition: GrTypes.h:147
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
#define SkASSERT(cond)
Definition: SkAssert.h:116
int32_t height
int32_t width
void invalidate()
Definition: GrNativeRect.h:84
SkIRect asSkIRect() const
Definition: GrNativeRect.h:49
bool operator==(const GrNativeRect &that) const
Definition: GrNativeRect.h:88
const int * asInts() const
Definition: GrNativeRect.h:38
bool operator!=(const GrNativeRect &that) const
Definition: GrNativeRect.h:92
bool isInvalid() const
Definition: GrNativeRect.h:85
int * asInts()
Definition: GrNativeRect.h:47
static SkIRect MakeIRectRelativeTo(GrSurfaceOrigin origin, int rtHeight, SkIRect devRect)
Definition: GrNativeRect.h:31
void setRelativeTo(GrSurfaceOrigin org, int rtHeight, const SkIRect &devRect)
Definition: GrNativeRect.h:57
bool contains(int width, int height) const
Definition: GrNativeRect.h:77
void setRelativeTo(GrSurfaceOrigin origin, int surfaceHeight, int leftOffset, int topOffset, int width, int height)
Definition: GrNativeRect.h:62
static GrNativeRect MakeRelativeTo(GrSurfaceOrigin origin, int rtHeight, SkIRect devRect)
Definition: GrNativeRect.h:25
Definition: SkRect.h:32
constexpr int32_t x() const
Definition: SkRect.h:141
constexpr int32_t y() const
Definition: SkRect.h:148
constexpr int32_t height() const
Definition: SkRect.h:165
constexpr int32_t width() const
Definition: SkRect.h:158
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104