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 #ifndef SkBitmapKey_DEFINED |
| 8 #define SkBitmapKey_DEFINED |
| 9 |
| 10 #include "SkBitmap.h" |
| 11 #include "SkImage.h" |
| 12 #include "SkCanvas.h" |
| 13 |
| 14 class SkBitmapKey { |
| 15 public: |
| 16 SkBitmapKey() : fSubset(SkIRect::MakeEmpty()), fID(0) {} |
| 17 explicit SkBitmapKey(const SkBitmap& bm) |
| 18 : fSubset(bm.getSubset()), fID(bm.getGenerationID()) {} |
| 19 explicit SkBitmapKey(const SkImage* img) |
| 20 : fSubset(img ? img->bounds() : SkIRect::MakeEmpty()) |
| 21 , fID(img ? img->uniqueID() : 0) {} |
| 22 explicit SkBitmapKey(const sk_sp<SkImage> img) |
| 23 : fSubset(img->bounds()), fID(img->uniqueID()) {} |
| 24 bool operator==(const SkBitmapKey& rhs) const { |
| 25 return fID == rhs.fID && fSubset == rhs.fSubset; |
| 26 } |
| 27 bool operator!=(const SkBitmapKey& rhs) const { return !(*this == rhs); } |
| 28 |
| 29 private: |
| 30 SkIRect fSubset; |
| 31 uint32_t fID; |
| 32 }; |
| 33 |
| 34 /** |
| 35 This wraps a thing that could either be a bitmap or a image and |
| 36 abstracts out some common tasks. |
| 37 */ |
| 38 class SkImageBitmap { |
| 39 public: |
| 40 explicit SkImageBitmap(const SkBitmap& b) : fBitmap(b), fImage(nullptr) {} |
| 41 explicit SkImageBitmap(SkImage* i) : fImage(i) { SkASSERT(fImage); } |
| 42 SkIRect bounds() const { return fImage ? fImage->bounds() : fBitmap.bounds()
; } |
| 43 SkISize dimensions() const { |
| 44 return fImage ? fImage->dimensions() : fBitmap.dimensions(); |
| 45 } |
| 46 sk_sp<SkImage> makeImage() const { |
| 47 return fImage ? sk_ref_sp(fImage) : SkImage::MakeFromBitmap(fBitmap); |
| 48 } |
| 49 SkBitmapKey getKey() const { |
| 50 return fImage ? SkBitmapKey(fImage) : SkBitmapKey(fBitmap); |
| 51 } |
| 52 void draw(SkCanvas* canvas, SkPaint* paint) const { |
| 53 if (fImage) { |
| 54 canvas->drawImage(fImage, 0, 0, paint); |
| 55 } else { |
| 56 canvas->drawBitmap(fBitmap, 0, 0, paint); |
| 57 } |
| 58 } |
| 59 |
| 60 private: |
| 61 SkBitmap fBitmap; |
| 62 SkImage* fImage; // non-owning; when drawImage starts passing a sk_sp<>, |
| 63 // we can take a const ref to that sk_sp<>. |
| 64 }; |
| 65 |
| 66 #endif // SkBitmapKey_DEFINED |
OLD | NEW |