reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "SkBitmapCache.h" |
| 9 | #include "SkRect.h" |
| 10 | |
| 11 | /** |
| 12 | This function finds the bounds of the bitmap *within its pixelRef*. |
| 13 | If the bitmap lacks a pixelRef, it will return an empty rect, since |
| 14 | that doesn't make sense. This may be a useful enough function that |
| 15 | it should be somewhere else (in SkBitmap?). |
| 16 | */ |
| 17 | static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { |
| 18 | if (!(bm.pixelRef())) { |
| 19 | return SkIRect::MakeEmpty(); |
| 20 | } |
| 21 | SkIPoint origin = bm.pixelRefOrigin(); |
| 22 | return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
| 23 | } |
| 24 | |
| 25 | struct BitmapKey : public SkScaledImageCache::Key { |
| 26 | public: |
| 27 | BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds) |
| 28 | : fGenID(genID) |
| 29 | , fScaleX(scaleX) |
| 30 | , fScaleY(scaleY) |
| 31 | , fBounds(bounds) |
| 32 | { |
| 33 | this->init(sizeof(fGenID) + sizeof(fScaleX) + sizeof(fScaleY) + sizeof(fBounds)); |
| 34 | } |
qiankun.miao | 045bb7f | 2014-08-25 06:08:25 -0700 | [diff] [blame^] | 35 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 36 | uint32_t fGenID; |
| 37 | SkScalar fScaleX; |
| 38 | SkScalar fScaleY; |
| 39 | SkIRect fBounds; |
| 40 | }; |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | SkScaledImageCache::ID* SkBitmapCache::FindAndLock(const SkBitmap& src, |
| 45 | SkScalar invScaleX, SkScalar invScaleY, |
| 46 | SkBitmap* result) { |
| 47 | if (0 == invScaleX || 0 == invScaleY) { |
| 48 | // degenerate, and the key we use for mipmaps |
| 49 | return NULL; |
| 50 | } |
| 51 | BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src)); |
| 52 | return SkScaledImageCache::FindAndLock(key, result); |
| 53 | } |
| 54 | |
| 55 | SkScaledImageCache::ID* SkBitmapCache::AddAndLock(const SkBitmap& src, |
| 56 | SkScalar invScaleX, SkScalar invScaleY, |
| 57 | const SkBitmap& result) { |
| 58 | if (0 == invScaleX || 0 == invScaleY) { |
| 59 | // degenerate, and the key we use for mipmaps |
| 60 | return NULL; |
| 61 | } |
| 62 | BitmapKey key(src.getGenerationID(), invScaleX, invScaleY, get_bounds_from_bitmap(src)); |
| 63 | return SkScaledImageCache::AddAndLock(key, result); |
| 64 | } |
| 65 | |
| 66 | //// |
| 67 | |
| 68 | SkScaledImageCache::ID* SkBitmapCache::FindAndLock(uint32_t genID, int width, int height, |
| 69 | SkBitmap* result) { |
| 70 | BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)); |
| 71 | return SkScaledImageCache::FindAndLock(key, result); |
| 72 | } |
| 73 | |
| 74 | SkScaledImageCache::ID* SkBitmapCache::AddAndLock(uint32_t genID, int width, int height, |
| 75 | const SkBitmap& result) { |
| 76 | BitmapKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)); |
| 77 | return SkScaledImageCache::AddAndLock(key, result); |
| 78 | } |
| 79 | |
| 80 | //// |
| 81 | |
| 82 | SkScaledImageCache::ID* SkMipMapCache::FindAndLock(const SkBitmap& src, const SkMipMap** result) { |
| 83 | BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from_bitmap(src)); |
| 84 | return SkScaledImageCache::FindAndLock(key, result); |
| 85 | } |
| 86 | |
| 87 | SkScaledImageCache::ID* SkMipMapCache::AddAndLock(const SkBitmap& src, const SkMipMap* result) { |
| 88 | BitmapKey key(src.getGenerationID(), SK_Scalar1, SK_Scalar1, get_bounds_from_bitmap(src)); |
| 89 | return SkScaledImageCache::AddAndLock(key, result); |
| 90 | } |
| 91 | |