csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | class GrWindowRectangles { |
| 15 | public: |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 16 | constexpr static int kMaxWindows = 8; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 17 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 18 | GrWindowRectangles() : fCount(0) {} |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 19 | GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = that; } |
| 20 | ~GrWindowRectangles() { SkSafeUnref(this->rec()); } |
| 21 | |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 22 | GrWindowRectangles makeOffset(int dx, int dy) const; |
| 23 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 24 | bool empty() const { return !fCount; } |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 25 | int count() const { return fCount; } |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 26 | const SkIRect* data() const; |
| 27 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 28 | void reset(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 29 | 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 | |
| 37 | private: |
| 38 | constexpr static int kNumLocalWindows = 1; |
| 39 | struct Rec; |
| 40 | |
| 41 | const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; } |
| 42 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 43 | int fCount; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 44 | union { |
| 45 | SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWindows. |
| 46 | Rec* fRec; // If fCount > kNumLocalWindows. |
| 47 | }; |
| 48 | }; |
| 49 | |
| 50 | struct 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 Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 55 | Rec() = default; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 56 | |
| 57 | SkIRect fData[kMaxWindows]; |
| 58 | }; |
| 59 | |
| 60 | inline const SkIRect* GrWindowRectangles::data() const { |
| 61 | return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData; |
| 62 | } |
| 63 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 64 | inline void GrWindowRectangles::reset() { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 65 | SkSafeUnref(this->rec()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 66 | fCount = 0; |
| 67 | } |
| 68 | |
| 69 | inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) { |
| 70 | SkSafeUnref(this->rec()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 71 | 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 Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 80 | inline 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 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 99 | inline 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 | |
| 113 | inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 114 | if (fCount != that.fCount) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 115 | 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 |