blob: 732c99372cecaa0f5b4cc292ff74579f4d4b88fd [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRect.h"
12#include "src/gpu/GrNonAtomicRef.h"
csmartdalton28341fa2016-08-17 10:00:21 -070013
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) {
Mike Klein73ac3932019-12-02 14:36:22 -060073 for (int i = 0; i < fCount; i++) {
74 fLocalWindows[i] = that.fLocalWindows[i];
75 }
csmartdalton28341fa2016-08-17 10:00:21 -070076 } else {
77 fRec = SkRef(that.fRec);
78 }
79 return *this;
80}
81
Brian Salomon9a767722017-03-13 17:57:28 -040082inline GrWindowRectangles GrWindowRectangles::makeOffset(int dx, int dy) const {
83 if (!dx && !dy) {
84 return *this;
85 }
86 GrWindowRectangles result;
87 result.fCount = fCount;
88 SkIRect* windows;
89 if (result.fCount > kNumLocalWindows) {
90 result.fRec = new Rec();
91 windows = result.fRec->fData;
92 } else {
93 windows = result.fLocalWindows;
94 }
95 for (int i = 0; i < fCount; ++i) {
96 windows[i] = this->data()[i].makeOffset(dx, dy);
97 }
98 return result;
99}
100
csmartdalton28341fa2016-08-17 10:00:21 -0700101inline SkIRect& GrWindowRectangles::addWindow() {
102 SkASSERT(fCount < kMaxWindows);
103 if (fCount < kNumLocalWindows) {
104 return fLocalWindows[fCount++];
105 }
106 if (fCount == kNumLocalWindows) {
107 fRec = new Rec(fLocalWindows, kNumLocalWindows);
108 } else if (!fRec->unique()) { // Simple copy-on-write.
109 fRec->unref();
110 fRec = new Rec(fRec->fData, fCount);
111 }
112 return fRec->fData[fCount++];
113}
114
115inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700116 if (fCount != that.fCount) {
csmartdalton28341fa2016-08-17 10:00:21 -0700117 return false;
118 }
119 if (fCount > kNumLocalWindows && fRec == that.fRec) {
120 return true;
121 }
122 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);
123}
124
125#endif