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

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

Issue 12433020: Improvements/additions to SkImageCache/SkLazyPixelRef. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Respond to comments. Created 7 years, 9 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
« no previous file with comments | « src/lazy/SkLazyPixelRef.h ('k') | src/lazy/SkLruImageCache.cpp » ('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 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 14
15 #if LAZY_CACHE_STATS 15 #if LAZY_CACHE_STATS
16 #include "SkThread.h" 16 #include "SkThread.h"
17 17
18 int32_t SkLazyPixelRef::gCacheHits; 18 int32_t SkLazyPixelRef::gCacheHits;
19 int32_t SkLazyPixelRef::gCacheMisses; 19 int32_t SkLazyPixelRef::gCacheMisses;
20 #endif 20 #endif
21 21
22 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S kImageCache* cache) 22 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. 23 // Pass NULL for the Mutex so that the default (ring buffer) will be used.
24 : INHERITED(NULL) 24 : INHERITED(NULL)
25 , fDecodeProc(proc) 25 , fDecodeProc(proc)
26 , fImageCache(cache) 26 , fImageCache(cache)
27 , fCacheId(SkImageCache::UNINITIALIZED_ID) { 27 , fCacheId(SkImageCache::UNINITIALIZED_ID)
28 , fRowBytes(0) {
28 SkASSERT(fDecodeProc != NULL); 29 SkASSERT(fDecodeProc != NULL);
29 if (NULL == data) { 30 if (NULL == data) {
30 fData = SkData::NewEmpty(); 31 fData = SkData::NewEmpty();
31 fErrorInDecoding = true; 32 fErrorInDecoding = true;
32 } else { 33 } else {
33 fData = data; 34 fData = data;
34 fData->ref(); 35 fData->ref();
35 fErrorInDecoding = data->size() == 0; 36 fErrorInDecoding = data->size() == 0;
36 } 37 }
37 SkASSERT(cache != NULL); 38 SkASSERT(cache != NULL);
(...skipping 26 matching lines...) Expand all
64 65
65 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { 66 void* SkLazyPixelRef::onLockPixels(SkColorTable**) {
66 if (fErrorInDecoding) { 67 if (fErrorInDecoding) {
67 return NULL; 68 return NULL;
68 } 69 }
69 SkBitmapFactory::Target target; 70 SkBitmapFactory::Target target;
70 // Check to see if the pixels still exist in the cache. 71 // Check to see if the pixels still exist in the cache.
71 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { 72 if (SkImageCache::UNINITIALIZED_ID == fCacheId) {
72 target.fAddr = NULL; 73 target.fAddr = NULL;
73 } else { 74 } else {
74 target.fAddr = fImageCache->pinCache(fCacheId); 75 SkImageCache::DataStatus status;
75 if (NULL != target.fAddr) { 76 target.fAddr = fImageCache->pinCache(fCacheId, &status);
77 if (target.fAddr == NULL) {
78 fCacheId = SkImageCache::UNINITIALIZED_ID;
79 } else {
80 if (SkImageCache::kRetained_DataStatus == status) {
76 #if LAZY_CACHE_STATS 81 #if LAZY_CACHE_STATS
77 sk_atomic_inc(&gCacheHits); 82 sk_atomic_inc(&gCacheHits);
78 #endif 83 #endif
79 return target.fAddr; 84 return target.fAddr;
85 }
86 SkASSERT(SkImageCache::kUninitialized_DataStatus == status);
80 } 87 }
88 // Cache miss. Either pinCache returned NULL or it returned a memory add ress without the old
89 // data
81 #if LAZY_CACHE_STATS 90 #if LAZY_CACHE_STATS
82 sk_atomic_inc(&gCacheMisses); 91 sk_atomic_inc(&gCacheMisses);
83 #endif 92 #endif
84 } 93 }
85 SkASSERT(NULL == target.fAddr);
86 SkImage::Info info; 94 SkImage::Info info;
87 SkASSERT(fData != NULL && fData->size() > 0); 95 SkASSERT(fData != NULL && fData->size() > 0);
88 // FIXME: As an optimization, only do this part once. 96 if (NULL == target.fAddr) {
89 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL); 97 // Determine the size of the image in order to determine how much memory to allocate.
90 if (fErrorInDecoding) { 98 // FIXME: As an optimization, only do this part once.
91 // In case a previous call to allocAndPinCache succeeded. 99 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL L);
92 fImageCache->throwAwayCache(fCacheId); 100 if (fErrorInDecoding) {
93 fCacheId = SkImageCache::UNINITIALIZED_ID; 101 // We can only reach here if fCacheId was already set to UNINITIALIZ ED_ID, or if
94 return NULL; 102 // pinCache returned NULL, in which case it was reset to UNINITIALIZ ED_ID.
103 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId);
104 return NULL;
105 }
106
107 size_t bytes = ComputeMinRowBytesAndSize(info, &target.fRowBytes);
108 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId);
109 if (NULL == target.fAddr) {
110 // Space could not be allocated.
111 // Just like the last assert, fCacheId must be UNINITIALIZED_ID.
112 SkASSERT(SkImageCache::UNINITIALIZED_ID == fCacheId);
113 return NULL;
114 }
115 } else {
116 // pinCache returned purged memory to which target.fAddr already points. Set
117 // target.fRowBytes properly.
118 target.fRowBytes = fRowBytes;
119 // Assume that the size is correct, since it was determined by this same function
120 // previously.
95 } 121 }
96 // Allocate the memory. 122 SkASSERT(target.fAddr != NULL);
97 size_t bytes = ComputeMinRowBytesAndSize(info, &target.fRowBytes);
98
99 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId);
100 if (NULL == target.fAddr) {
101 // Space could not be allocated.
102 fCacheId = SkImageCache::UNINITIALIZED_ID;
103 return NULL;
104 }
105 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); 123 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId);
106 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target ); 124 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target );
107 if (fErrorInDecoding) { 125 if (fErrorInDecoding) {
108 fImageCache->throwAwayCache(fCacheId); 126 fImageCache->throwAwayCache(fCacheId);
109 fCacheId = SkImageCache::UNINITIALIZED_ID; 127 fCacheId = SkImageCache::UNINITIALIZED_ID;
110 return NULL; 128 return NULL;
111 } 129 }
130 // Upon success, store fRowBytes so it can be used in case pinCache later re turns purged memory.
131 fRowBytes = target.fRowBytes;
112 return target.fAddr; 132 return target.fAddr;
113 } 133 }
114 134
115 void SkLazyPixelRef::onUnlockPixels() { 135 void SkLazyPixelRef::onUnlockPixels() {
116 if (fErrorInDecoding) { 136 if (fErrorInDecoding) {
117 return; 137 return;
118 } 138 }
119 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { 139 if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
120 fImageCache->releaseCache(fCacheId); 140 fImageCache->releaseCache(fCacheId);
121 } 141 }
122 } 142 }
123 143
124 SkData* SkLazyPixelRef::onRefEncodedData() { 144 SkData* SkLazyPixelRef::onRefEncodedData() {
125 fData->ref(); 145 fData->ref();
126 return fData; 146 return fData;
127 } 147 }
OLDNEW
« no previous file with comments | « src/lazy/SkLazyPixelRef.h ('k') | src/lazy/SkLruImageCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698