OLD | NEW |
(Empty) | |
| 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: |
| 16 constexpr static int kMaxWindows = 16; |
| 17 |
| 18 enum class Mode : bool { |
| 19 kExclusive, |
| 20 kInclusive |
| 21 }; |
| 22 |
| 23 GrWindowRectangles(Mode mode = Mode::kExclusive) : fMode(mode), fCount(0) {} |
| 24 GrWindowRectangles(const GrWindowRectangles& that) : fCount(0) { *this = tha
t; } |
| 25 ~GrWindowRectangles() { SkSafeUnref(this->rec()); } |
| 26 |
| 27 Mode mode() const { return fMode; } |
| 28 uint16_t count() const { return fCount; } |
| 29 bool disabled() const { return Mode::kExclusive == fMode && !fCount; } |
| 30 const SkIRect* data() const; |
| 31 |
| 32 void reset(Mode = Mode::kExclusive); |
| 33 GrWindowRectangles& operator=(const GrWindowRectangles&); |
| 34 |
| 35 SkIRect& addWindow(const SkIRect& window) { return this->addWindow() = windo
w; } |
| 36 SkIRect& addWindow(); |
| 37 |
| 38 bool operator!=(const GrWindowRectangles& that) const { return !(*this == th
at); } |
| 39 bool operator==(const GrWindowRectangles&) const; |
| 40 |
| 41 private: |
| 42 constexpr static int kNumLocalWindows = 1; |
| 43 struct Rec; |
| 44 |
| 45 const Rec* rec() const { return fCount <= kNumLocalWindows ? nullptr : fRec;
} |
| 46 |
| 47 Mode fMode; |
| 48 uint8_t fCount; |
| 49 union { |
| 50 SkIRect fLocalWindows[kNumLocalWindows]; // If fCount <= kNumLocalWind
ows. |
| 51 Rec* fRec; // If fCount > kNumLocalWindo
ws. |
| 52 }; |
| 53 }; |
| 54 |
| 55 struct GrWindowRectangles::Rec : public GrNonAtomicRef<Rec> { |
| 56 Rec(const SkIRect* windows, int numWindows) { |
| 57 SkASSERT(numWindows < kMaxWindows); |
| 58 memcpy(fData, windows, sizeof(SkIRect) * numWindows); |
| 59 } |
| 60 |
| 61 SkIRect fData[kMaxWindows]; |
| 62 }; |
| 63 |
| 64 inline const SkIRect* GrWindowRectangles::data() const { |
| 65 return fCount <= kNumLocalWindows ? fLocalWindows : fRec->fData; |
| 66 } |
| 67 |
| 68 inline void GrWindowRectangles::reset(Mode mode) { |
| 69 SkSafeUnref(this->rec()); |
| 70 fMode = mode; |
| 71 fCount = 0; |
| 72 } |
| 73 |
| 74 inline GrWindowRectangles& GrWindowRectangles::operator=(const GrWindowRectangle
s& that) { |
| 75 SkSafeUnref(this->rec()); |
| 76 fMode = that.fMode; |
| 77 fCount = that.fCount; |
| 78 if (fCount <= kNumLocalWindows) { |
| 79 memcpy(fLocalWindows, that.fLocalWindows, fCount * sizeof(SkIRect)); |
| 80 } else { |
| 81 fRec = SkRef(that.fRec); |
| 82 } |
| 83 return *this; |
| 84 } |
| 85 |
| 86 inline SkIRect& GrWindowRectangles::addWindow() { |
| 87 SkASSERT(fCount < kMaxWindows); |
| 88 if (fCount < kNumLocalWindows) { |
| 89 return fLocalWindows[fCount++]; |
| 90 } |
| 91 if (fCount == kNumLocalWindows) { |
| 92 fRec = new Rec(fLocalWindows, kNumLocalWindows); |
| 93 } else if (!fRec->unique()) { // Simple copy-on-write. |
| 94 fRec->unref(); |
| 95 fRec = new Rec(fRec->fData, fCount); |
| 96 } |
| 97 return fRec->fData[fCount++]; |
| 98 } |
| 99 |
| 100 inline bool GrWindowRectangles::operator==(const GrWindowRectangles& that) const
{ |
| 101 if (fMode != that.fMode || fCount != that.fCount) { |
| 102 return false; |
| 103 } |
| 104 if (fCount > kNumLocalWindows && fRec == that.fRec) { |
| 105 return true; |
| 106 } |
| 107 return !fCount || !memcmp(this->data(), that.data(), sizeof(SkIRect) * fCoun
t); |
| 108 } |
| 109 |
| 110 #endif |
OLD | NEW |