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 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 22 | bool empty() const { return !fCount; } |
csmartdalton | 7535f41 | 2016-08-23 06:51:00 -0700 | [diff] [blame] | 23 | int count() const { return fCount; } |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 24 | const SkIRect* data() const; |
| 25 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 26 | void reset(); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 27 | 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 | |
| 35 | private: |
| 36 | constexpr static int kNumLocalWindows = 1; |
| 37 | struct Rec; |
| 38 | |
| 39 | const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec; } |
| 40 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 41 | int fCount; |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 42 | union { |
| 43 | SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWindows. |
| 44 | Rec* fRec; // If fCount > kNumLocalWindows. |
| 45 | }; |
| 46 | }; |
| 47 | |
| 48 | struct 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 | |
| 57 | inline const SkIRect* GrWindowRectangles::data() const { |
| 58 | return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData; |
| 59 | } |
| 60 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 61 | inline void GrWindowRectangles::reset() { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 62 | SkSafeUnref(this->rec()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 63 | fCount = 0; |
| 64 | } |
| 65 | |
| 66 | inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangles& that) { |
| 67 | SkSafeUnref(this->rec()); |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 68 | 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 | |
| 77 | inline 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 | |
| 91 | inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const { |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 92 | if (fCount != that.fCount) { |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 93 | 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 |