Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/gpu/GrWindowRectangles.h

Issue 2289363005: Improve usage of window rectangles (Closed) Base URL: https://skia.googlesource.com/skia.git@upload_drawsinreducedclip
Patch Set: Improve usage of window rectangles Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef GrWindowRectangles_DEFINED 8 #ifndef GrWindowRectangles_DEFINED
9 #define GrWindowRectangles_DEFINED 9 #define GrWindowRectangles_DEFINED
10 10
11 #include "GrNonAtomicRef.h" 11 #include "GrNonAtomicRef.h"
12 #include "SkRect.h" 12 #include "SkRect.h"
13 13
14 class GrWindowRectangles { 14 class GrWindowRectangles {
15 public: 15 public:
16 constexpr static int kMaxWindows = 8; 16 constexpr static int kMaxWindows = 8;
17 17
18 enum class Mode : bool { 18 GrWindowRectangles() : fCount(0) {}
19 kExclusive,
20 kInclusive
21 };
22
23 GrWindowRectangles(Mode mode = Mode::kExclusive) : fMode(mode), fCount(0) {}
24 GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = tha t; } 19 GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = tha t; }
25 ~GrWindowRectangles() { SkSafeUnref(this->rec()); } 20 ~GrWindowRectangles() { SkSafeUnref(this->rec()); }
26 21
27 Mode mode() const { return fMode; } 22 bool empty() const { return !fCount; }
28 int count() const { return fCount; } 23 int count() const { return fCount; }
29 bool disabled() const { return Mode::kExclusive == fMode && !fCount; }
30 const SkIRect* data() const; 24 const SkIRect* data() const;
31 25
32 void reset(Mode = Mode::kExclusive); 26 void reset();
33 GrWindowRectangles& operator=(const GrWindowRectangles&); 27 GrWindowRectangles& operator=(const GrWindowRectangles&);
34 28
35 SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = windo w; } 29 SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = windo w; }
36 SkIRect& addWindow(); 30 SkIRect& addWindow();
37 31
38 bool operator!=(const GrWindowRectangles& that) const { return !(*this == th at); } 32 bool operator!=(const GrWindowRectangles& that) const { return !(*this == th at); }
39 bool operator==(const GrWindowRectangles&) const; 33 bool operator==(const GrWindowRectangles&) const;
40 34
41 private: 35 private:
42 constexpr static int kNumLocalWindows = 1; 36 constexpr static int kNumLocalWindows = 1;
43 struct Rec; 37 struct Rec;
44 38
45 const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; } 39 const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; }
46 40
47 Mode fMode; 41 int fCount;
48 uint8_t fCount;
49 union { 42 union {
50 SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWind ows. 43 SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWind ows.
51 Rec* fRec; // If fCount > kNumLocalWindo ws. 44 Rec* fRec; // If fCount > kNumLocalWindo ws.
52 }; 45 };
53 }; 46 };
54 47
55 struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> { 48 struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
56 Rec(const SkIRect* windows, int numWindows) { 49 Rec(const SkIRect* windows, int numWindows) {
57 SkASSERT(numWindows < kMaxWindows); 50 SkASSERT(numWindows < kMaxWindows);
58 memcpy(fData, windows, sizeof(SkIRect) * numWindows); 51 memcpy(fData, windows, sizeof(SkIRect) * numWindows);
59 } 52 }
60 53
61 SkIRect fData[kMaxWindows]; 54 SkIRect fData[kMaxWindows];
62 }; 55 };
63 56
64 inline const SkIRect* GrWindowRectangles::data() const { 57 inline const SkIRect* GrWindowRectangles::data() const {
65 return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData; 58 return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData;
66 } 59 }
67 60
68 inline void GrWindowRectangles::reset(Mode mode) { 61 inline void GrWindowRectangles::reset() {
69 SkSafeUnref(this->rec()); 62 SkSafeUnref(this->rec());
70 fMode = mode;
71 fCount = 0; 63 fCount = 0;
72 } 64 }
73 65
74 inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangle s& that) { 66 inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangle s& that) {
75 SkSafeUnref(this->rec()); 67 SkSafeUnref(this->rec());
76 fMode = that.fMode;
77 fCount = that.fCount; 68 fCount = that.fCount;
78 if (fCount <= kNumLocalWindows) { 69 if (fCount <= kNumLocalWindows) {
79 memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect)); 70 memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect));
80 } else { 71 } else {
81 fRec = SkRef(that.fRec); 72 fRec = SkRef(that.fRec);
82 } 73 }
83 return *this; 74 return *this;
84 } 75 }
85 76
86 inline SkIRect& GrWindowRectangles::addWindow() { 77 inline SkIRect& GrWindowRectangles::addWindow() {
87 SkASSERT(fCount < kMaxWindows); 78 SkASSERT(fCount < kMaxWindows);
88 if (fCount < kNumLocalWindows) { 79 if (fCount < kNumLocalWindows) {
89 return fLocalWindows[fCount++]; 80 return fLocalWindows[fCount++];
90 } 81 }
91 if (fCount == kNumLocalWindows) { 82 if (fCount == kNumLocalWindows) {
92 fRec = new Rec(fLocalWindows, kNumLocalWindows); 83 fRec = new Rec(fLocalWindows, kNumLocalWindows);
93 } else if (!fRec->unique()) { // Simple copy-on-write. 84 } else if (!fRec->unique()) { // Simple copy-on-write.
94 fRec->unref(); 85 fRec->unref();
95 fRec = new Rec(fRec->fData, fCount); 86 fRec = new Rec(fRec->fData, fCount);
96 } 87 }
97 return fRec->fData[fCount++]; 88 return fRec->fData[fCount++];
98 } 89 }
99 90
100 inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const { 91 inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
101 if (fMode != that.fMode || fCount != that.fCount) { 92 if (fCount != that.fCount) {
102 return false; 93 return false;
103 } 94 }
104 if (fCount > kNumLocalWindows && fRec == that.fRec) { 95 if (fCount > kNumLocalWindows && fRec == that.fRec) {
105 return true; 96 return true;
106 } 97 }
107 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCoun t); 98 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCoun t);
108 } 99 }
109 100
110 #endif 101 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698