2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrWindowRectangles_DEFINED
9 #define GrWindowRectangles_DEFINED
11 #include "include/core/SkRect.h"
12 #include "src/gpu/ganesh/GrNonAtomicRef.h"
14 class GrWindowRectangles {
16 constexpr static int kMaxWindows = 8;
18 GrWindowRectangles() : fCount(0) {}
19 GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = that; }
20 ~GrWindowRectangles() { SkSafeUnref(this->rec()); }
22 GrWindowRectangles makeOffset(int dx, int dy) const;
24 bool empty() const { return !fCount; }
25 int count() const { return fCount; }
26 const SkIRect* data() const;
29 GrWindowRectangles& operator=(const GrWindowRectangles&);
31 SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = window; }
34 bool operator!=(const GrWindowRectangles& that) const { return !(*this == that); }
35 bool operator==(const GrWindowRectangles&) const;
40 const Rec* rec() const { return fCount <= 1 ? nullptr : fRec; }
44 SkIRect fLocalWindow; // If fCount <= 1
45 Rec* fRec; // If fCount > 1.
49 struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> {
50 Rec(const SkIRect* windows, int numWindows) {
51 SkASSERT(numWindows <= kMaxWindows);
52 memcpy(fData, windows, sizeof(SkIRect) * numWindows);
56 SkIRect fData[kMaxWindows];
59 inline const SkIRect* GrWindowRectangles::data() const {
60 return fCount <= 1 ? &fLocalWindow : fRec->fData;
63 inline void GrWindowRectangles::reset() {
64 SkSafeUnref(this->rec());
68 inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) {
69 SkSafeUnref(this->rec());
72 fLocalWindow = that.fLocalWindow;
74 fRec = SkRef(that.fRec);
79 inline GrWindowRectangles GrWindowRectangles::makeOffset(int dx, int dy) const {
83 GrWindowRectangles result;
84 result.fCount = fCount;
86 if (result.fCount > 1) {
87 result.fRec = new Rec();
88 windows = result.fRec->fData;
90 windows = &result.fLocalWindow;
92 for (int i = 0; i < fCount; ++i) {
93 windows[i] = this->data()[i].makeOffset(dx, dy);
98 inline SkIRect& GrWindowRectangles::addWindow() {
99 SkASSERT(fCount < kMaxWindows);
105 fRec = new Rec(&fLocalWindow, 1);
106 } else if (!fRec->unique()) { // Simple copy-on-write.
108 fRec = new Rec(fRec->fData, fCount);
110 return fRec->fData[fCount++];
113 inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const {
114 if (fCount != that.fCount) {
117 if (fCount > 1 && fRec == that.fRec) {
120 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCount);