Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: src/lazy/SkCachingPixelRef.cpp

Issue 84083002: SkCachingPixelRef to use SkImageGenerator (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 SkCachingPixelRef::SkCachingPixelRef() 11
12 : fErrorInDecoding(false) 12 bool SkCachingPixelRef::Install(SkAutoTDelete<SkImageGenerator>* generator,
13 , fScaledCacheId(NULL) { 13 SkBitmap* dst) {
14 memset(&fInfo, 0xFF, sizeof(fInfo)); 14 SkImageInfo info;
15 SkASSERT(generator->get() != NULL);
16 SkASSERT(dst != NULL);
17 if ((NULL == generator->get())
18 || !((*generator)->getInfo(&info))
19 || !dst->setConfig(info, 0)) {
20 return false;
21 }
22 SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
23 ((*generator).detach(),
24 info,
25 dst->rowBytes())));
26 dst->setPixelRef(ref);
27 return true;
28 }
29
30 SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator,
31 const SkImageInfo& info,
32 size_t rowBytes)
33 : fImageGenerator(generator)
34 , fErrorInDecoding(false)
35 , fScaledCacheId(NULL)
36 , fInfo(info)
37 , fRowBytes(rowBytes) {
38 SkASSERT(fImageGenerator != NULL);
15 } 39 }
16 SkCachingPixelRef::~SkCachingPixelRef() { 40 SkCachingPixelRef::~SkCachingPixelRef() {
41 SkDELETE(fImageGenerator);
17 SkASSERT(NULL == fScaledCacheId); 42 SkASSERT(NULL == fScaledCacheId);
18 // Assert always unlock before unref. 43 // Assert always unlock before unref.
19 } 44 }
20 45
21 bool SkCachingPixelRef::getInfo(SkImageInfo* info) {
22 SkASSERT(info != NULL);
23 if (fErrorInDecoding) {
24 return false; // Don't try again.
25 }
26 if (fInfo.fWidth < 0) {
27 SkImageInfo tmp;
28 if (!this->onDecodeInfo(&tmp)) {
29 fErrorInDecoding = true;
30 return false;
31 }
32 SkASSERT(tmp.fWidth >= 0);
33 fInfo = tmp;
34 }
35 *info = fInfo;
36 return true;
37 }
38
39 bool SkCachingPixelRef::configure(SkBitmap* bitmap) {
40 SkASSERT(bitmap != NULL);
41 SkImageInfo info;
42 if (!this->getInfo(&info)) {
43 return false;
44 }
45 return bitmap->setConfig(info, 0);
46 }
47
48 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { 46 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
49 (void)colorTable; 47 (void)colorTable;
50 SkImageInfo info; 48 if (fErrorInDecoding) {
51 if (!this->getInfo(&info)) { 49 return NULL; // don't try again.
52 return NULL;
53 } 50 }
54 SkBitmap bitmap; 51 SkBitmap bitmap;
55 52 SkASSERT(NULL == fScaledCacheId);
56 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), 53 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
57 info.fWidth, 54 fInfo.fWidth,
58 info.fHeight, 55 fInfo.fHeight,
59 &bitmap); 56 &bitmap);
60 if (NULL == fScaledCacheId) { 57 if (NULL == fScaledCacheId) {
61 // Cache has been purged, must re-decode. 58 // Cache has been purged, must re-decode.
62 if (!this->onDecodeInto(0, &bitmap)) { 59 if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) {
60 fErrorInDecoding = true;
61 return NULL;
62 }
63 SkAutoLockPixels autoLockPixels(bitmap);
64 if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) {
65 fErrorInDecoding = true;
63 return NULL; 66 return NULL;
64 } 67 }
65 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), 68 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
66 info.fWidth, 69 fInfo.fWidth,
67 info.fHeight, 70 fInfo.fHeight,
68 bitmap); 71 bitmap);
69 SkASSERT(fScaledCacheId != NULL); 72 SkASSERT(fScaledCacheId != NULL);
70 } 73 }
74
71 // Now bitmap should contain a concrete PixelRef of the decoded 75 // Now bitmap should contain a concrete PixelRef of the decoded
72 // image. 76 // image.
73 SkAutoLockPixels autoLockPixels(bitmap); 77 SkAutoLockPixels autoLockPixels(bitmap);
74 void* pixels = bitmap.getPixels(); 78 void* pixels = bitmap.getPixels();
75 SkASSERT(pixels != NULL); 79 SkASSERT(pixels != NULL);
76 // At this point, the autoLockPixels will unlockPixels() 80 // At this point, the autoLockPixels will unlockPixels()
77 // to remove bitmap's lock on the pixels. We will then 81 // to remove bitmap's lock on the pixels. We will then
78 // destroy bitmap. The *only* guarantee that this pointer 82 // destroy bitmap. The *only* guarantee that this pointer
79 // remains valid is the guarantee made by 83 // remains valid is the guarantee made by
80 // SkScaledImageCache that it will not destroy the *other* 84 // SkScaledImageCache that it will not destroy the *other*
81 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a 85 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
82 // reference to the concrete PixelRef while this record is 86 // reference to the concrete PixelRef while this record is
83 // locked. 87 // locked.
84 return pixels; 88 return pixels;
85 } 89 }
86 90
87 void SkCachingPixelRef::onUnlockPixels() { 91 void SkCachingPixelRef::onUnlockPixels() {
88 if (fScaledCacheId != NULL) { 92 if (fScaledCacheId != NULL) {
89 SkScaledImageCache::Unlock( 93 SkScaledImageCache::Unlock(
90 static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); 94 static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
91 fScaledCacheId = NULL; 95 fScaledCacheId = NULL;
92 } 96 }
93 } 97 }
94 98
95 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
96 SkASSERT(bitmap != NULL);
97 SkBitmap tmp;
98 SkImageInfo info;
99 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom
100 // allocator. `tmp.allocPixels(fAllocator, NULL)`
101 if (!(this->configure(&tmp) && tmp.allocPixels())) {
102 return false;
103 }
104 SkAssertResult(this->getInfo(&info)); // since configure() succeeded.
105 if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) {
106 fErrorInDecoding = true;
107 return false;
108 }
109 *bitmap = tmp;
110 return true;
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698