Line data Source code
1 : /*
2 : * Copyright 2016 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 : #ifndef GrWindowRectangles_DEFINED
9 : #define GrWindowRectangles_DEFINED
10 :
11 : #include "GrNonAtomicRef.h"
12 : #include "SkRect.h"
13 :
14 : class GrWindowRectangles {
15 : public:
16 : constexpr static int kMaxWindows = 8;
17 :
18 0 : GrWindowRectangles() : fCount(0) {}
19 0 : GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = that; }
20 0 : ~GrWindowRectangles() { SkSafeUnref(this->rec()); }
21 :
22 : GrWindowRectangles makeOffset(int dx, int dy) const;
23 :
24 0 : bool empty() const { return !fCount; }
25 0 : int count() const { return fCount; }
26 : const SkIRect* data() const;
27 :
28 : void reset();
29 : GrWindowRectangles& operator=(const GrWindowRectangles&);
30 :
31 0 : SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = window; }
32 : SkIRect& addWindow();
33 :
34 : bool operator!=(const GrWindowRectangles& that) const { return !(*this == that); }
35 : bool operator==(const GrWindowRectangles&) const;
36 :
37 : private:
38 : constexpr static int kNumLocalWindows = 1;
39 : struct Rec;
40 :
41 0 : const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; }
42 :
43 : int fCount;
44 : union {
45 : SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWindows.
46 : Rec* fRec; // If fCount > kNumLocalWindows.
47 : };
48 : };
49 :
50 0 : struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
51 0 : Rec(const SkIRect* windows, int numWindows) {
52 0 : SkASSERT(numWindows < kMaxWindows);
53 0 : memcpy(fData, windows, sizeof(SkIRect) * numWindows);
54 0 : }
55 0 : Rec() = default;
56 :
57 : SkIRect fData[kMaxWindows];
58 : };
59 :
60 0 : inline const SkIRect* GrWindowRectangles::data() const {
61 0 : return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData;
62 : }
63 :
64 0 : inline void GrWindowRectangles::reset() {
65 0 : SkSafeUnref(this->rec());
66 0 : fCount = 0;
67 0 : }
68 :
69 0 : inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) {
70 0 : SkSafeUnref(this->rec());
71 0 : fCount = that.fCount;
72 0 : if (fCount <= kNumLocalWindows) {
73 0 : memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect));
74 : } else {
75 0 : fRec = SkRef(that.fRec);
76 : }
77 0 : return *this;
78 : }
79 :
80 0 : inline GrWindowRectangles GrWindowRectangles::makeOffset(int dx, int dy) const {
81 0 : if (!dx && !dy) {
82 0 : return *this;
83 : }
84 0 : GrWindowRectangles result;
85 0 : result.fCount = fCount;
86 : SkIRect* windows;
87 0 : if (result.fCount > kNumLocalWindows) {
88 0 : result.fRec = new Rec();
89 0 : windows = result.fRec->fData;
90 : } else {
91 0 : windows = result.fLocalWindows;
92 : }
93 0 : for (int i = 0; i < fCount; ++i) {
94 0 : windows[i] = this->data()[i].makeOffset(dx, dy);
95 : }
96 0 : return result;
97 : }
98 :
99 0 : inline SkIRect& GrWindowRectangles::addWindow() {
100 0 : SkASSERT(fCount < kMaxWindows);
101 0 : if (fCount < kNumLocalWindows) {
102 0 : return fLocalWindows[fCount++];
103 : }
104 0 : if (fCount == kNumLocalWindows) {
105 0 : fRec = new Rec(fLocalWindows, kNumLocalWindows);
106 0 : } else if (!fRec->unique()) { // Simple copy-on-write.
107 0 : fRec->unref();
108 0 : fRec = new Rec(fRec->fData, fCount);
109 : }
110 0 : return fRec->fData[fCount++];
111 : }
112 :
113 0 : inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
114 0 : if (fCount != that.fCount) {
115 0 : return false;
116 : }
117 0 : if (fCount > kNumLocalWindows && fRec == that.fRec) {
118 0 : return true;
119 : }
120 0 : return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);
121 : }
122 :
123 : #endif
|