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