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

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

Issue 37343002: Allow SkLazyPixelRef to use SkScaledImageCache (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: changes from all commenters Created 7 years, 1 month 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 2012 Google Inc. 2 * Copyright 2012 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 "Sk64.h" 8 #include "Sk64.h"
9 #include "SkLazyPixelRef.h" 9 #include "SkLazyPixelRef.h"
10 #include "SkColorTable.h" 10 #include "SkColorTable.h"
11 #include "SkData.h" 11 #include "SkData.h"
12 #include "SkImageCache.h" 12 #include "SkImageCache.h"
13 #include "SkImagePriv.h" 13 #include "SkImagePriv.h"
14 #include "SkScaledImageCache.h"
14 15
15 #if LAZY_CACHE_STATS 16 #if LAZY_CACHE_STATS
16 #include "SkThread.h" 17 #include "SkThread.h"
17 18
18 int32_t SkLazyPixelRef::gCacheHits; 19 int32_t SkLazyPixelRef::gCacheHits;
19 int32_t SkLazyPixelRef::gCacheMisses; 20 int32_t SkLazyPixelRef::gCacheMisses;
20 #endif 21 #endif
21 22
22 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) 23 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache)
23 // Pass NULL for the Mutex so that the default (ring buffer) will be used. 24 // Pass NULL for the Mutex so that the default (ring buffer) will be used.
24 : INHERITED(NULL) 25 : INHERITED(NULL)
26 , fErrorInDecoding(false)
25 , fDecodeProc(proc) 27 , fDecodeProc(proc)
26 , fImageCache(cache) 28 , fImageCache(cache)
27 , fCacheId(SkImageCache::UNINITIALIZED_ID) 29 , fCacheId(0)
28 , fRowBytes(0) { 30 , fRowBytes(0)
31 , fAllocator(NULL) {
29 SkASSERT(fDecodeProc != NULL); 32 SkASSERT(fDecodeProc != NULL);
33 SkASSERT(0 == SkImageCache::UNINITIALIZED_ID); // for fCacheId
30 if (NULL == data) { 34 if (NULL == data) {
31 fData = SkData::NewEmpty(); 35 fData = SkData::NewEmpty();
32 fErrorInDecoding = true; 36 fErrorInDecoding = true;
33 } else { 37 } else {
34 fData = data; 38 fData = data;
35 fData->ref(); 39 fData->ref();
36 fErrorInDecoding = data->size() == 0; 40 fErrorInDecoding = data->size() == 0;
37 } 41 }
38 SkASSERT(cache != NULL); 42 if (fImageCache != NULL) {
39 cache->ref(); 43 fImageCache->ref();
44 }
40 45
41 // mark as uninitialized -- all fields are -1 46 // mark as uninitialized -- all fields are -1
42 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); 47 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo));
43 48
44 // Since this pixel ref bases its data on encoded data, it should never chan ge. 49 // Since this pixel ref bases its data on encoded data, it should never chan ge.
45 this->setImmutable(); 50 this->setImmutable();
46 } 51 }
47 52
48 SkLazyPixelRef::~SkLazyPixelRef() { 53 SkLazyPixelRef::~SkLazyPixelRef() {
scroggo 2013/10/23 23:38:33 Now that we ref fAllocator, it needs to be unref'e
hal.canary 2013/10/24 14:21:55 Done.
49 SkASSERT(fData != NULL); 54 SkASSERT(fData != NULL);
50 fData->unref(); 55 fData->unref();
56 if (NULL == fImageCache) {
57 if (fCacheId != 0) {
58 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId));
59 }
60 return;
61 }
51 SkASSERT(fImageCache); 62 SkASSERT(fImageCache);
52 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { 63 if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
53 fImageCache->throwAwayCache(fCacheId); 64 fImageCache->throwAwayCache(fCacheId);
54 } 65 }
55 fImageCache->unref(); 66 fImageCache->unref();
56 } 67 }
57 68
58 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { 69 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) {
59 *rowBytes = SkImageMinRowBytes(info); 70 *rowBytes = SkImageMinRowBytes(info);
60 71
(...skipping 11 matching lines...) Expand all
72 SkImage::Info info; 83 SkImage::Info info;
73 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); 84 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L);
74 if (fErrorInDecoding) { 85 if (fErrorInDecoding) {
75 return NULL; 86 return NULL;
76 } 87 }
77 fLazilyCachedInfo = info; 88 fLazilyCachedInfo = info;
78 } 89 }
79 return &fLazilyCachedInfo; 90 return &fLazilyCachedInfo;
80 } 91 }
81 92
93 /**
94 Returns bitmap->getPixels() on success; NULL on failure */
95 static void* decode_into_bitmap(SkImage::Info* info,
96 SkBitmapFactory::DecodeProc decodeProc,
97 size_t* rowBytes,
98 SkData* data,
99 SkBitmap::Allocator* allocator,
100 SkBitmap* bm) {
101 SkASSERT(info && decodeProc && rowBytes && data && bm);
102 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth,
103 info->fHeight, *rowBytes, info->fAlphaType)
104 && bm->allocPixels(allocator, NULL))) {
105 return NULL;
106 }
107 SkBitmapFactory::Target target;
108 target.fAddr = bm->getPixels();
109 target.fRowBytes = bm->rowBytes();
110 *rowBytes = target.fRowBytes;
111 if (!decodeProc(data->data(), data->size(), info, &target)) {
112 return NULL;
113 }
114 return target.fAddr;
115 }
116
117 void* SkLazyPixelRef::lockScaledImageCachePixels() {
118 SkASSERT(!fErrorInDecoding);
scroggo 2013/10/23 23:38:33 Maybe assert NULL == fImageCache as well?
hal.canary 2013/10/24 14:21:55 Done.
119 SkBitmap bitmap;
120 if (fLazilyCachedInfo.fWidth > 0) { // fInfo already populated
121 fCacheId = (intptr_t)(SkScaledImageCache::FindAndLock(
122 this->getGenerationID(),
123 fLazilyCachedInfo.fWidth,
124 fLazilyCachedInfo.fHeight,
125 &bitmap));
126 if (fCacheId != 0) {
127 SkAutoLockPixels autoLockPixels(bitmap);
scroggo 2013/10/23 23:38:33 Maybe this is correct, but it seems odd to me: -
hal.canary 2013/10/24 14:21:55 This is confusing enough that I will add comments
128 void* pixels = bitmap.getPixels();
129 SkASSERT(NULL != pixels);
130 return pixels;
131 } // else cache has been purged, must re-decode.
132 } else { // first time through this code, must populate info
133 (void)this->getCachedInfo();
134 if (fErrorInDecoding) {
135 return NULL;
136 }
137 }
138 SkASSERT(fLazilyCachedInfo.fWidth > 0);
139 void * ptr = decode_into_bitmap(&fLazilyCachedInfo, fDecodeProc,
140 &fRowBytes, fData, fAllocator, &bitmap);
141 if (NULL == ptr) {
142 fErrorInDecoding = true;
143 return NULL;
144 }
145 fCacheId = (intptr_t)(SkScaledImageCache::AddAndLock(
146 this->getGenerationID(),
147 fLazilyCachedInfo.fWidth,
148 fLazilyCachedInfo.fHeight,
149 bitmap));
150 return ptr;
151 }
152
82 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { 153 void* SkLazyPixelRef::onLockPixels(SkColorTable**) {
83 if (fErrorInDecoding) { 154 if (fErrorInDecoding) {
84 return NULL; 155 return NULL;
85 } 156 }
157 if (NULL == fImageCache) {
158 return this->lockScaledImageCachePixels();
159 } else {
160 return this->lockImageCachePixels();
161 }
162 }
163
164 void* SkLazyPixelRef::lockImageCachePixels() {
165 SkASSERT(!fErrorInDecoding);
scroggo 2013/10/23 23:38:33 Assert that fImageCache != NULL?
hal.canary 2013/10/24 14:21:55 Done.
86 SkBitmapFactory::Target target; 166 SkBitmapFactory::Target target;
87 // Check to see if the pixels still exist in the cache. 167 // Check to see if the pixels still exist in the cache.
88 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { 168 if (SkImageCache::UNINITIALIZED_ID == fCacheId) {
89 target.fAddr = NULL; 169 target.fAddr = NULL;
90 } else { 170 } else {
91 SkImageCache::DataStatus status; 171 SkImageCache::DataStatus status;
92 target.fAddr = fImageCache->pinCache(fCacheId, &status); 172 target.fAddr = fImageCache->pinCache(fCacheId, &status);
93 if (target.fAddr == NULL) { 173 if (target.fAddr == NULL) {
94 fCacheId = SkImageCache::UNINITIALIZED_ID; 174 fCacheId = SkImageCache::UNINITIALIZED_ID;
95 } else { 175 } else {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 fImageCache->throwAwayCache(fCacheId); 217 fImageCache->throwAwayCache(fCacheId);
138 fCacheId = SkImageCache::UNINITIALIZED_ID; 218 fCacheId = SkImageCache::UNINITIALIZED_ID;
139 return NULL; 219 return NULL;
140 } 220 }
141 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. 221 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory.
142 fRowBytes = target.fRowBytes; 222 fRowBytes = target.fRowBytes;
143 return target.fAddr; 223 return target.fAddr;
144 } 224 }
145 225
146 void SkLazyPixelRef::onUnlockPixels() { 226 void SkLazyPixelRef::onUnlockPixels() {
147 if (fErrorInDecoding) { 227 if (fErrorInDecoding || (0 == fCacheId)) {
148 return; 228 return;
149 } 229 }
150 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { 230 if (NULL == fImageCache) {
231 SkScaledImageCache::Unlock((SkScaledImageCache::ID *)(fCacheId));
232 fCacheId = 0;
233 return;
234 } else { // use fImageCache
151 fImageCache->releaseCache(fCacheId); 235 fImageCache->releaseCache(fCacheId);
152 } 236 }
153 } 237 }
154 238
155 SkData* SkLazyPixelRef::onRefEncodedData() { 239 SkData* SkLazyPixelRef::onRefEncodedData() {
156 fData->ref(); 240 fData->ref();
157 return fData; 241 return fData;
158 } 242 }
159 243
160 #include "SkImagePriv.h"
161
162 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, 244 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info,
163 size_t rowBytes) { 245 size_t rowBytes) {
164 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); 246 SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
165 if (SkBitmap::kNo_Config == config) { 247 if (SkBitmap::kNo_Config == config) {
166 return false; 248 return false;
167 } 249 }
168 250
169 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType) 251 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType)
170 && 252 &&
171 bm->allocPixels(); 253 bm->allocPixels();
(...skipping 27 matching lines...) Expand all
199 281
200 target.fAddr = tmp.getPixels(); 282 target.fAddr = tmp.getPixels();
201 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); 283 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target );
202 if (fErrorInDecoding) { 284 if (fErrorInDecoding) {
203 return false; 285 return false;
204 } 286 }
205 287
206 *bitmap = tmp; 288 *bitmap = tmp;
207 return true; 289 return true;
208 } 290 }
291
292 void SkLazyPixelRef::setAllocator(SkBitmap::Allocator * allocator) {
293 SkRefCnt_SafeAssign(fAllocator, allocator);
294 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698