blob: 4a375860e0de87b40dd83a9c67e35b1beb50367c [file] [log] [blame]
csmartdalton28341fa2016-08-17 10:00:21 -07001/*
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
14class GrWindowRectangles {
15public:
csmartdalton7535f412016-08-23 06:51:00 -070016 constexpr static int kMaxWindows = 8;
csmartdalton28341fa2016-08-17 10:00:21 -070017
csmartdaltonbf4a8f92016-09-06 10:01:06 -070018 GrWindowRectangles() : fCount(0) {}
csmartdalton28341fa2016-08-17 10:00:21 -070019 GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = that; }
20 ~GrWindowRectangles() { SkSafeUnref(this->rec()); }
21
Brian Salomon9a767722017-03-13 17:57:28 -040022 GrWindowRectangles makeOffset(int dx, int dy) const;
23
csmartdaltonbf4a8f92016-09-06 10:01:06 -070024 bool empty() const { return !fCount; }
csmartdalton7535f412016-08-23 06:51:00 -070025 int count() const { return fCount; }
csmartdalton28341fa2016-08-17 10:00:21 -070026 const SkIRect* data() const;
27
csmartdaltonbf4a8f92016-09-06 10:01:06 -070028 void reset();
csmartdalton28341fa2016-08-17 10:00:21 -070029 GrWindowRectangles& operator=(const GrWindowRectangles&);
30
31 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
37private:
38 constexpr static int kNumLocalWindows = 1;
39 struct Rec;
40
41 const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; }
42
csmartdaltonbf4a8f92016-09-06 10:01:06 -070043 int fCount;
csmartdalton28341fa2016-08-17 10:00:21 -070044 union {
45 SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWindows.
46 Rec* fRec; // If fCount > kNumLocalWindows.
47 };
48};
49
50struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
51 Rec(const SkIRect* windows, int numWindows) {
52 SkASSERT(numWindows < kMaxWindows);
53 memcpy(fData, windows, sizeof(SkIRect) * numWindows);
54 }
Brian Salomon9a767722017-03-13 17:57:28 -040055 Rec() = default;
csmartdalton28341fa2016-08-17 10:00:21 -070056
57 SkIRect fData[kMaxWindows];
58};
59
60inline const SkIRect* GrWindowRectangles::data() const {
61 return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData;
62}
63
csmartdaltonbf4a8f92016-09-06 10:01:06 -070064inline void GrWindowRectangles::reset() {
csmartdalton28341fa2016-08-17 10:00:21 -070065 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070066 fCount = 0;
67}
68
69inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) {
70 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070071 fCount = that.fCount;
72 if (fCount <= kNumLocalWindows) {
73 memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect));
74 } else {
75 fRec = SkRef(that.fRec);
76 }
77 return *this;
78}
79
Brian Salomon9a767722017-03-13 17:57:28 -040080inline GrWindowRectangles GrWindowRectangles::makeOffset(int dx, int dy) const {
81 if (!dx && !dy) {
82 return *this;
83 }
84 GrWindowRectangles result;
85 result.fCount = fCount;
86 SkIRect* windows;
87 if (result.fCount > kNumLocalWindows) {
88 result.fRec = new Rec();
89 windows = result.fRec->fData;
90 } else {
91 windows = result.fLocalWindows;
92 }
93 for (int i = 0; i < fCount; ++i) {
94 windows[i] = this->data()[i].makeOffset(dx, dy);
95 }
96 return result;
97}
98
csmartdalton28341fa2016-08-17 10:00:21 -070099inline SkIRect& GrWindowRectangles::addWindow() {
100 SkASSERT(fCount < kMaxWindows);
101 if (fCount < kNumLocalWindows) {
102 return fLocalWindows[fCount++];
103 }
104 if (fCount == kNumLocalWindows) {
105 fRec = new Rec(fLocalWindows, kNumLocalWindows);
106 } else if (!fRec->unique()) { // Simple copy-on-write.
107 fRec->unref();
108 fRec = new Rec(fRec->fData, fCount);
109 }
110 return fRec->fData[fCount++];
111}
112
113inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700114 if (fCount != that.fCount) {
csmartdalton28341fa2016-08-17 10:00:21 -0700115 return false;
116 }
117 if (fCount > kNumLocalWindows && fRec == that.fRec) {
118 return true;
119 }
120 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);
121}
122
123#endif