blob: 076c40d7a4557692e517d2393a23bc1280123019 [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
csmartdaltonbf4a8f92016-09-06 10:01:06 -070022 bool empty() const { return !fCount; }
csmartdalton7535f412016-08-23 06:51:00 -070023 int count() const { return fCount; }
csmartdalton28341fa2016-08-17 10:00:21 -070024 const SkIRect* data() const;
25
csmartdaltonbf4a8f92016-09-06 10:01:06 -070026 void reset();
csmartdalton28341fa2016-08-17 10:00:21 -070027 GrWindowRectangles& operator=(const GrWindowRectangles&);
28
29 SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = window; }
30 SkIRect& addWindow();
31
32 bool operator!=(const GrWindowRectangles& that) const { return !(*this == that); }
33 bool operator==(const GrWindowRectangles&) const;
34
35private:
36 constexpr static int kNumLocalWindows = 1;
37 struct Rec;
38
39 const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; }
40
csmartdaltonbf4a8f92016-09-06 10:01:06 -070041 int fCount;
csmartdalton28341fa2016-08-17 10:00:21 -070042 union {
43 SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWindows.
44 Rec* fRec; // If fCount > kNumLocalWindows.
45 };
46};
47
48struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
49 Rec(const SkIRect* windows, int numWindows) {
50 SkASSERT(numWindows < kMaxWindows);
51 memcpy(fData, windows, sizeof(SkIRect) * numWindows);
52 }
53
54 SkIRect fData[kMaxWindows];
55};
56
57inline const SkIRect* GrWindowRectangles::data() const {
58 return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData;
59}
60
csmartdaltonbf4a8f92016-09-06 10:01:06 -070061inline void GrWindowRectangles::reset() {
csmartdalton28341fa2016-08-17 10:00:21 -070062 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070063 fCount = 0;
64}
65
66inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) {
67 SkSafeUnref(this->rec());
csmartdalton28341fa2016-08-17 10:00:21 -070068 fCount = that.fCount;
69 if (fCount <= kNumLocalWindows) {
70 memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect));
71 } else {
72 fRec = SkRef(that.fRec);
73 }
74 return *this;
75}
76
77inline SkIRect& GrWindowRectangles::addWindow() {
78 SkASSERT(fCount < kMaxWindows);
79 if (fCount < kNumLocalWindows) {
80 return fLocalWindows[fCount++];
81 }
82 if (fCount == kNumLocalWindows) {
83 fRec = new Rec(fLocalWindows, kNumLocalWindows);
84 } else if (!fRec->unique()) { // Simple copy-on-write.
85 fRec->unref();
86 fRec = new Rec(fRec->fData, fCount);
87 }
88 return fRec->fData[fCount++];
89}
90
91inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
csmartdaltonbf4a8f92016-09-06 10:01:06 -070092 if (fCount != that.fCount) {
csmartdalton28341fa2016-08-17 10:00:21 -070093 return false;
94 }
95 if (fCount > kNumLocalWindows && fRec == that.fRec) {
96 return true;
97 }
98 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);
99}
100
101#endif