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

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: again Created 7 years, 2 months 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
23
scroggo 2013/10/24 20:41:39 unnecessary newline.
hal.canary 2013/10/25 16:37:10 Done.
22 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) 24 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. 25 // Pass NULL for the Mutex so that the default (ring buffer) will be used.
24 : INHERITED(NULL) 26 : INHERITED(NULL)
27 , fErrorInDecoding(false)
25 , fDecodeProc(proc) 28 , fDecodeProc(proc)
26 , fImageCache(cache) 29 , fImageCache(cache)
27 , fCacheId(SkImageCache::UNINITIALIZED_ID) 30 , fRowBytes(0)
28 , fRowBytes(0) { 31 , fAllocator(NULL) {
29 SkASSERT(fDecodeProc != NULL); 32 SkASSERT(fDecodeProc != NULL);
30 if (NULL == data) { 33 if (NULL == data) {
31 fData = SkData::NewEmpty(); 34 fData = SkData::NewEmpty();
32 fErrorInDecoding = true; 35 fErrorInDecoding = true;
33 } else { 36 } else {
34 fData = data; 37 fData = data;
35 fData->ref(); 38 fData->ref();
36 fErrorInDecoding = data->size() == 0; 39 fErrorInDecoding = data->size() == 0;
37 } 40 }
38 SkASSERT(cache != NULL); 41 if (fImageCache != NULL) {
39 cache->ref(); 42 fImageCache->ref();
43 fCacheId = SkImageCache::UNINITIALIZED_ID;
44 } else {
45 fScaledCacheId = NULL;
46 }
40 47
41 // mark as uninitialized -- all fields are -1 48 // mark as uninitialized -- all fields are -1
42 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo)); 49 memset(&fLazilyCachedInfo, 0xFF, sizeof(fLazilyCachedInfo));
43 50
44 // Since this pixel ref bases its data on encoded data, it should never chan ge. 51 // Since this pixel ref bases its data on encoded data, it should never chan ge.
45 this->setImmutable(); 52 this->setImmutable();
46 } 53 }
47 54
48 SkLazyPixelRef::~SkLazyPixelRef() { 55 SkLazyPixelRef::~SkLazyPixelRef() {
49 SkASSERT(fData != NULL); 56 SkASSERT(fData != NULL);
50 fData->unref(); 57 fData->unref();
58 SkSafeUnref(fAllocator);
59 if (NULL == fImageCache) {
60 if (fScaledCacheId != NULL) {
61 SkScaledImageCache::Unlock(fScaledCacheId);
62 // SkScaledImageCache needs a throwAwayCache(id) method.
63 }
64 return;
65 }
51 SkASSERT(fImageCache); 66 SkASSERT(fImageCache);
52 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { 67 if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
53 fImageCache->throwAwayCache(fCacheId); 68 fImageCache->throwAwayCache(fCacheId);
54 } 69 }
55 fImageCache->unref(); 70 fImageCache->unref();
56 } 71 }
57 72
58 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) { 73 static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBy tes) {
59 *rowBytes = SkImageMinRowBytes(info); 74 *rowBytes = SkImageMinRowBytes(info);
60 75
(...skipping 11 matching lines...) Expand all
72 SkImage::Info info; 87 SkImage::Info info;
73 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L); 88 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L);
74 if (fErrorInDecoding) { 89 if (fErrorInDecoding) {
75 return NULL; 90 return NULL;
76 } 91 }
77 fLazilyCachedInfo = info; 92 fLazilyCachedInfo = info;
78 } 93 }
79 return &fLazilyCachedInfo; 94 return &fLazilyCachedInfo;
80 } 95 }
81 96
97 /**
98 Returns bitmap->getPixels() on success; NULL on failure */
99 static void* decode_into_bitmap(SkImage::Info* info,
100 SkBitmapFactory::DecodeProc decodeProc,
101 size_t* rowBytes,
102 SkData* data,
103 SkBitmap::Allocator* allocator,
104 SkBitmap* bm) {
105 SkASSERT(info && decodeProc && rowBytes && data && bm);
106 if (!(bm->setConfig(SkImageInfoToBitmapConfig(*info), info->fWidth,
107 info->fHeight, *rowBytes, info->fAlphaType)
108 && bm->allocPixels(allocator, NULL))) {
109 return NULL;
110 }
111 SkBitmapFactory::Target target;
112 target.fAddr = bm->getPixels();
113 target.fRowBytes = bm->rowBytes();
114 *rowBytes = target.fRowBytes;
115 if (!decodeProc(data->data(), data->size(), info, &target)) {
116 return NULL;
117 }
118 return target.fAddr;
119 }
120
121 void* SkLazyPixelRef::lockScaledImageCachePixels() {
122 SkASSERT(!fErrorInDecoding);
123 SkASSERT(NULL == fImageCache);
124 SkBitmap bitmap;
125 if (fLazilyCachedInfo.fWidth > 0) { // fInfo already populated
scroggo 2013/10/24 20:41:39 inline helper function?
hal.canary 2013/10/25 16:37:10 changed
126 fScaledCacheId = SkScaledImageCache::FindAndLock(
127 this->getGenerationID(),
128 fLazilyCachedInfo.fWidth,
129 fLazilyCachedInfo.fHeight,
130 &bitmap);
131 if (fScaledCacheId != NULL) {
132 SkAutoLockPixels autoLockPixels(bitmap);
133 void* pixels = bitmap.getPixels();
134 SkASSERT(NULL != pixels);
135 // At this point, the autoLockPixels will unlockPixels()
136 // to remove bitmap's lock on the pixels. We will then
137 // destroy bitmap. The *only* guarantee that this pointer
138 // remains valid is the guarantee made by
139 // SkScaledImageCache that it will not destroy the *other*
140 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
141 // reference to the concrete PixelRef while this record is
142 // locked.
143 return pixels;
144 } // else cache has been purged, must re-decode.
145 } else { // first time through this code, must populate info
146 (void)this->getCachedInfo();
scroggo 2013/10/24 20:41:39 The idea behind getCachedInfo returning an info po
hal.canary 2013/10/25 16:37:10 Done.
147 if (fErrorInDecoding) {
148 return NULL;
149 }
150 }
151 SkASSERT(fLazilyCachedInfo.fWidth > 0);
152 void* pixels = decode_into_bitmap(&fLazilyCachedInfo, fDecodeProc,
153 &fRowBytes, fData, fAllocator, &bitmap);
154 if (NULL == pixels) {
155 fErrorInDecoding = true;
156 return NULL;
157 }
158 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
159 fLazilyCachedInfo.fWidth,
160 fLazilyCachedInfo.fHeight,
161 bitmap);
162 return pixels;
163 }
164
82 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { 165 void* SkLazyPixelRef::onLockPixels(SkColorTable**) {
83 if (fErrorInDecoding) { 166 if (fErrorInDecoding) {
84 return NULL; 167 return NULL;
85 } 168 }
169 if (NULL == fImageCache) {
170 return this->lockScaledImageCachePixels();
171 } else {
172 return this->lockImageCachePixels();
173 }
174 }
175
176 void* SkLazyPixelRef::lockImageCachePixels() {
177 SkASSERT(fImageCache != NULL);
178 SkASSERT(!fErrorInDecoding);
86 SkBitmapFactory::Target target; 179 SkBitmapFactory::Target target;
87 // Check to see if the pixels still exist in the cache. 180 // Check to see if the pixels still exist in the cache.
88 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { 181 if (SkImageCache::UNINITIALIZED_ID == fCacheId) {
89 target.fAddr = NULL; 182 target.fAddr = NULL;
90 } else { 183 } else {
91 SkImageCache::DataStatus status; 184 SkImageCache::DataStatus status;
92 target.fAddr = fImageCache->pinCache(fCacheId, &status); 185 target.fAddr = fImageCache->pinCache(fCacheId, &status);
93 if (target.fAddr == NULL) { 186 if (target.fAddr == NULL) {
94 fCacheId = SkImageCache::UNINITIALIZED_ID; 187 fCacheId = SkImageCache::UNINITIALIZED_ID;
95 } else { 188 } else {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 fImageCache->throwAwayCache(fCacheId); 230 fImageCache->throwAwayCache(fCacheId);
138 fCacheId = SkImageCache::UNINITIALIZED_ID; 231 fCacheId = SkImageCache::UNINITIALIZED_ID;
139 return NULL; 232 return NULL;
140 } 233 }
141 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory. 234 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory.
142 fRowBytes = target.fRowBytes; 235 fRowBytes = target.fRowBytes;
143 return target.fAddr; 236 return target.fAddr;
144 } 237 }
145 238
146 void SkLazyPixelRef::onUnlockPixels() { 239 void SkLazyPixelRef::onUnlockPixels() {
147 if (fErrorInDecoding) { 240 if (fErrorInDecoding || (0 == fCacheId)) {
scroggo 2013/10/24 20:41:39 Maybe a comment here that 0 == fCacheId is the sam
hal.canary 2013/10/25 16:37:10 Cleaned up the code to make this clear.
148 return; 241 return;
149 } 242 }
150 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { 243 if (NULL == fImageCache) {
244 SkScaledImageCache::Unlock(fScaledCacheId);
245 fScaledCacheId = NULL;
246 return;
247 } else { // use fImageCache
151 fImageCache->releaseCache(fCacheId); 248 fImageCache->releaseCache(fCacheId);
152 } 249 }
153 } 250 }
154 251
155 SkData* SkLazyPixelRef::onRefEncodedData() { 252 SkData* SkLazyPixelRef::onRefEncodedData() {
156 fData->ref(); 253 fData->ref();
157 return fData; 254 return fData;
158 } 255 }
159 256
160 #include "SkImagePriv.h"
161
162 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info, 257 static bool init_from_info(SkBitmap* bm, const SkImage::Info& info,
163 size_t rowBytes) { 258 size_t rowBytes) {
164 SkBitmap::Config config = SkImageInfoToBitmapConfig(info); 259 SkBitmap::Config config = SkImageInfoToBitmapConfig(info);
165 if (SkBitmap::kNo_Config == config) { 260 if (SkBitmap::kNo_Config == config) {
166 return false; 261 return false;
167 } 262 }
168 263
169 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType) 264 return bm->setConfig(config, info.fWidth, info.fHeight, rowBytes, info.fAlph aType)
170 && 265 &&
171 bm->allocPixels(); 266 bm->allocPixels();
(...skipping 27 matching lines...) Expand all
199 294
200 target.fAddr = tmp.getPixels(); 295 target.fAddr = tmp.getPixels();
201 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); 296 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target );
202 if (fErrorInDecoding) { 297 if (fErrorInDecoding) {
203 return false; 298 return false;
204 } 299 }
205 300
206 *bitmap = tmp; 301 *bitmap = tmp;
207 return true; 302 return true;
208 } 303 }
304
305 void SkLazyPixelRef::setAllocator(SkBitmap::Allocator* allocator) {
306 SkRefCnt_SafeAssign(fAllocator, allocator);
307 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698