OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCachingPixelRef.h" | 8 #include "SkCachingPixelRef.h" |
9 #include "SkScaledImageCache.h" | 9 #include "SkBitmapCache.h" |
10 | 10 |
11 bool SkCachingPixelRef::Install(SkImageGenerator* generator, | 11 bool SkCachingPixelRef::Install(SkImageGenerator* generator, |
12 SkBitmap* dst) { | 12 SkBitmap* dst) { |
13 SkImageInfo info; | 13 SkImageInfo info; |
14 SkASSERT(dst != NULL); | 14 SkASSERT(dst != NULL); |
15 if ((NULL == generator) | 15 if ((NULL == generator) |
16 || !(generator->getInfo(&info)) | 16 || !(generator->getInfo(&info)) |
17 || !dst->setInfo(info)) { | 17 || !dst->setInfo(info)) { |
18 SkDELETE(generator); | 18 SkDELETE(generator); |
19 return false; | 19 return false; |
(...skipping 21 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { | 43 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { |
44 if (fErrorInDecoding) { | 44 if (fErrorInDecoding) { |
45 return false; // don't try again. | 45 return false; // don't try again. |
46 } | 46 } |
47 | 47 |
48 const SkImageInfo& info = this->info(); | 48 const SkImageInfo& info = this->info(); |
49 SkBitmap bitmap; | 49 SkBitmap bitmap; |
50 SkASSERT(NULL == fScaledCacheId); | 50 SkASSERT(NULL == fScaledCacheId); |
51 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | 51 fScaledCacheId = SkBitmapCache::FindAndLock(this->getGenerationID(), info.fW
idth, info.fHeight, |
52 info.fWidth, | 52 &bitmap); |
53 info.fHeight, | |
54 &bitmap); | |
55 if (NULL == fScaledCacheId) { | 53 if (NULL == fScaledCacheId) { |
56 // Cache has been purged, must re-decode. | 54 // Cache has been purged, must re-decode. |
57 if (!bitmap.allocPixels(info, fRowBytes)) { | 55 if (!bitmap.allocPixels(info, fRowBytes)) { |
58 fErrorInDecoding = true; | 56 fErrorInDecoding = true; |
59 return false; | 57 return false; |
60 } | 58 } |
61 SkAutoLockPixels autoLockPixels(bitmap); | 59 SkAutoLockPixels autoLockPixels(bitmap); |
62 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) { | 60 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) { |
63 fErrorInDecoding = true; | 61 fErrorInDecoding = true; |
64 return false; | 62 return false; |
65 } | 63 } |
66 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | 64 fScaledCacheId = SkBitmapCache::AddAndLock(this->getGenerationID(), |
67 info.fWidth, | 65 info.fWidth, info.fHeight, bi
tmap); |
68 info.fHeight, | |
69 bitmap); | |
70 SkASSERT(fScaledCacheId != NULL); | 66 SkASSERT(fScaledCacheId != NULL); |
71 } | 67 } |
72 | 68 |
73 // Now bitmap should contain a concrete PixelRef of the decoded | 69 // Now bitmap should contain a concrete PixelRef of the decoded |
74 // image. | 70 // image. |
75 SkAutoLockPixels autoLockPixels(bitmap); | 71 SkAutoLockPixels autoLockPixels(bitmap); |
76 void* pixels = bitmap.getPixels(); | 72 void* pixels = bitmap.getPixels(); |
77 SkASSERT(pixels != NULL); | 73 SkASSERT(pixels != NULL); |
78 | 74 |
79 // At this point, the autoLockPixels will unlockPixels() | 75 // At this point, the autoLockPixels will unlockPixels() |
80 // to remove bitmap's lock on the pixels. We will then | 76 // to remove bitmap's lock on the pixels. We will then |
81 // destroy bitmap. The *only* guarantee that this pointer | 77 // destroy bitmap. The *only* guarantee that this pointer |
82 // remains valid is the guarantee made by | 78 // remains valid is the guarantee made by |
83 // SkScaledImageCache that it will not destroy the *other* | 79 // SkScaledImageCache that it will not destroy the *other* |
84 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | 80 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a |
85 // reference to the concrete PixelRef while this record is | 81 // reference to the concrete PixelRef while this record is |
86 // locked. | 82 // locked. |
87 rec->fPixels = pixels; | 83 rec->fPixels = pixels; |
88 rec->fColorTable = NULL; | 84 rec->fColorTable = NULL; |
89 rec->fRowBytes = bitmap.rowBytes(); | 85 rec->fRowBytes = bitmap.rowBytes(); |
90 return true; | 86 return true; |
91 } | 87 } |
92 | 88 |
93 void SkCachingPixelRef::onUnlockPixels() { | 89 void SkCachingPixelRef::onUnlockPixels() { |
94 SkASSERT(fScaledCacheId != NULL); | 90 SkASSERT(fScaledCacheId != NULL); |
95 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCach
eId)); | 91 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCach
eId)); |
96 fScaledCacheId = NULL; | 92 fScaledCacheId = NULL; |
97 } | 93 } |
OLD | NEW |