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 "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 dst->setPixelRef(ref); | 23 dst->setPixelRef(ref); |
24 return true; | 24 return true; |
25 } | 25 } |
26 | 26 |
27 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, | 27 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info, |
28 SkImageGenerator* generator, | 28 SkImageGenerator* generator, |
29 size_t rowBytes) | 29 size_t rowBytes) |
30 : INHERITED(info) | 30 : INHERITED(info) |
31 , fImageGenerator(generator) | 31 , fImageGenerator(generator) |
32 , fErrorInDecoding(false) | 32 , fErrorInDecoding(false) |
33 , fScaledCacheId(NULL) | |
34 , fRowBytes(rowBytes) { | 33 , fRowBytes(rowBytes) { |
35 SkASSERT(fImageGenerator != NULL); | 34 SkASSERT(fImageGenerator != NULL); |
36 } | 35 } |
37 SkCachingPixelRef::~SkCachingPixelRef() { | 36 SkCachingPixelRef::~SkCachingPixelRef() { |
38 SkDELETE(fImageGenerator); | 37 SkDELETE(fImageGenerator); |
39 SkASSERT(NULL == fScaledCacheId); | |
40 // Assert always unlock before unref. | 38 // Assert always unlock before unref. |
41 } | 39 } |
42 | 40 |
43 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { | 41 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { |
44 if (fErrorInDecoding) { | 42 if (fErrorInDecoding) { |
45 return false; // don't try again. | 43 return false; // don't try again. |
46 } | 44 } |
47 | 45 |
48 const SkImageInfo& info = this->info(); | 46 const SkImageInfo& info = this->info(); |
49 SkBitmap bitmap; | 47 if (!SkBitmapCache::Find(this->getGenerationID(), info.fWidth, info.fHeight,
&fLockedBitmap)) { |
50 SkASSERT(NULL == fScaledCacheId); | |
51 fScaledCacheId = SkBitmapCache::FindAndLock(this->getGenerationID(), info.fW
idth, info.fHeight, | |
52 &bitmap); | |
53 if (NULL == fScaledCacheId) { | |
54 // Cache has been purged, must re-decode. | 48 // Cache has been purged, must re-decode. |
55 if (!bitmap.allocPixels(info, fRowBytes)) { | 49 if (!fLockedBitmap.allocPixels(info, fRowBytes)) { |
56 fErrorInDecoding = true; | 50 fErrorInDecoding = true; |
57 return false; | 51 return false; |
58 } | 52 } |
59 SkAutoLockPixels autoLockPixels(bitmap); | 53 if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowByt
es)) { |
60 if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) { | |
61 fErrorInDecoding = true; | 54 fErrorInDecoding = true; |
62 return false; | 55 return false; |
63 } | 56 } |
64 fScaledCacheId = SkBitmapCache::AddAndLock(this->getGenerationID(), | 57 SkBitmapCache::Add(this->getGenerationID(), info.fWidth, info.fHeight, f
LockedBitmap); |
65 info.fWidth, info.fHeight, bi
tmap); | |
66 SkASSERT(fScaledCacheId != NULL); | |
67 } | 58 } |
68 | 59 |
69 // Now bitmap should contain a concrete PixelRef of the decoded | 60 // Now bitmap should contain a concrete PixelRef of the decoded image. |
70 // image. | 61 void* pixels = fLockedBitmap.getPixels(); |
71 SkAutoLockPixels autoLockPixels(bitmap); | |
72 void* pixels = bitmap.getPixels(); | |
73 SkASSERT(pixels != NULL); | 62 SkASSERT(pixels != NULL); |
74 | |
75 // At this point, the autoLockPixels will unlockPixels() | |
76 // to remove bitmap's lock on the pixels. We will then | |
77 // destroy bitmap. The *only* guarantee that this pointer | |
78 // remains valid is the guarantee made by | |
79 // SkScaledImageCache that it will not destroy the *other* | |
80 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
81 // reference to the concrete PixelRef while this record is | |
82 // locked. | |
83 rec->fPixels = pixels; | 63 rec->fPixels = pixels; |
84 rec->fColorTable = NULL; | 64 rec->fColorTable = NULL; |
85 rec->fRowBytes = bitmap.rowBytes(); | 65 rec->fRowBytes = fLockedBitmap.rowBytes(); |
86 return true; | 66 return true; |
87 } | 67 } |
88 | 68 |
89 void SkCachingPixelRef::onUnlockPixels() { | 69 void SkCachingPixelRef::onUnlockPixels() { |
90 SkASSERT(fScaledCacheId != NULL); | 70 fLockedBitmap.reset(); |
91 SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCach
eId)); | |
92 fScaledCacheId = NULL; | |
93 } | 71 } |
OLD | NEW |