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