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

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

Powered by Google App Engine
This is Rietveld 408576698