blob: 40f7582fb5864cab4c3b0e0f33d521dba5caf756 [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:
csmartdalton28341fa2016-08-17 10:00:21 -070038 struct Rec;
39
Mike Kleinedc3bde2019-12-04 10:03:42 -060040 const Rec* rec() const { return fCount <= 1 ? nullptr : fRec; }
csmartdalton28341fa2016-08-17 10:00:21 -070041
csmartdaltonbf4a8f92016-09-06 10:01:06 -070042 int fCount;
csmartdalton28341fa2016-08-17 10:00:21 -070043 union {
Mike Kleinedc3bde2019-12-04 10:03:42 -060044 SkIRect fLocalWindow; // If fCount <= 1
45 Rec* fRec; // If fCount > 1.
csmartdalton28341fa2016-08-17 10:00:21 -070046 };
47};
48
49struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
50 Rec(const SkIRect* windows, int numWindows) {
51 SkASSERT(numWindows < kMaxWindows);
52 memcpy(fData, windows, sizeof(SkIRect) * numWindows);
53 }
Brian Salomon9a767722017-03-13 17:57:28 -040054 Rec() = default;
csmartdalton28341fa2016-08-17 10:00:21 -070055
56 SkIRect fData[kMaxWindows];
57};
58
59inline const SkIRect* GrWindowRectangles::data() const {
Mike Kleinedc3bde2019-12-04 10:03:42 -060060 return fCount <= 1 ? &fLocalWindow : fRec->fData;
csmartdalton28341fa2016-08-17 10:00:21 -070061}
62
csmartdaltonbf4a8f92016-09-06 10:01:06 -070063inline void GrWindowRectangles::reset() {
csmartdalton28341fa2016-08-17 10:00:21 -070064 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070065 fCount = 0;
66}
67
68inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) {
69 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070070 fCount = that.fCount;
Mike Kleinedc3bde2019-12-04 10:03:42 -060071 if (fCount <= 1) {
72 fLocalWindow = that.fLocalWindow;
csmartdalton28341fa2016-08-17 10:00:21 -070073 } else {
74 fRec = SkRef(that.fRec);
75 }
76 return *this;
77}
78
Brian Salomon9a767722017-03-13 17:57:28 -040079inline GrWindowRectangles GrWindowRectangles::makeOffset(int dx, int dy) const {
80 if (!dx && !dy) {
81 return *this;
82 }
83 GrWindowRectangles result;
84 result.fCount = fCount;
85 SkIRect* windows;
Mike Kleinedc3bde2019-12-04 10:03:42 -060086 if (result.fCount > 1) {
Brian Salomon9a767722017-03-13 17:57:28 -040087 result.fRec = new Rec();
88 windows = result.fRec->fData;
89 } else {
Mike Kleinedc3bde2019-12-04 10:03:42 -060090 windows = &result.fLocalWindow;
Brian Salomon9a767722017-03-13 17:57:28 -040091 }
92 for (int i = 0; i < fCount; ++i) {
93 windows[i] = this->data()[i].makeOffset(dx, dy);
94 }
95 return result;
96}
97
csmartdalton28341fa2016-08-17 10:00:21 -070098inline SkIRect& GrWindowRectangles::addWindow() {
99 SkASSERT(fCount < kMaxWindows);
Mike Kleinedc3bde2019-12-04 10:03:42 -0600100 if (fCount == 0) {
101 fCount = 1;
102 return fLocalWindow;
csmartdalton28341fa2016-08-17 10:00:21 -0700103 }
Mike Kleinedc3bde2019-12-04 10:03:42 -0600104 if (fCount == 1) {
105 fRec = new Rec(&fLocalWindow, 1);
csmartdalton28341fa2016-08-17 10:00:21 -0700106 } 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 }
Mike Kleinedc3bde2019-12-04 10:03:42 -0600117 if (fCount > 1 && fRec == that.fRec) {
csmartdalton28341fa2016-08-17 10:00:21 -0700118 return true;
119 }
120 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);
121}
122
123#endif