OLD | NEW |
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 |
| 16 #include "SkThread.h" |
| 17 |
| 18 int32_t SkLazyPixelRef::gCacheHits; |
| 19 int32_t SkLazyPixelRef::gCacheMisses; |
| 20 #endif |
| 21 |
15 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S
kImageCache* cache) | 22 SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, S
kImageCache* cache) |
16 // 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. |
17 : INHERITED(NULL) | 24 : INHERITED(NULL) |
18 , fDecodeProc(proc) | 25 , fDecodeProc(proc) |
19 , fImageCache(cache) | 26 , fImageCache(cache) |
20 , fCacheId(SkImageCache::UNINITIALIZED_ID) { | 27 , fCacheId(SkImageCache::UNINITIALIZED_ID) { |
21 SkASSERT(fDecodeProc != NULL); | 28 SkASSERT(fDecodeProc != NULL); |
22 if (NULL == data) { | 29 if (NULL == data) { |
23 fData = SkData::NewEmpty(); | 30 fData = SkData::NewEmpty(); |
24 fErrorInDecoding = true; | 31 fErrorInDecoding = true; |
(...skipping 29 matching lines...) Expand all Loading... |
54 SkASSERT(!safeSize.isNeg()); | 61 SkASSERT(!safeSize.isNeg()); |
55 return safeSize.is32() ? safeSize.get32() : 0; | 62 return safeSize.is32() ? safeSize.get32() : 0; |
56 } | 63 } |
57 | 64 |
58 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { | 65 void* SkLazyPixelRef::onLockPixels(SkColorTable**) { |
59 if (fErrorInDecoding) { | 66 if (fErrorInDecoding) { |
60 return NULL; | 67 return NULL; |
61 } | 68 } |
62 SkBitmapFactory::Target target; | 69 SkBitmapFactory::Target target; |
63 // Check to see if the pixels still exist in the cache. | 70 // Check to see if the pixels still exist in the cache. |
64 target.fAddr = SkImageCache::UNINITIALIZED_ID == fCacheId ? | 71 if (SkImageCache::UNINITIALIZED_ID == fCacheId) { |
65 NULL : fImageCache->pinCache(fCacheId); | 72 target.fAddr = NULL; |
| 73 } else { |
| 74 target.fAddr = fImageCache->pinCache(fCacheId); |
| 75 if (NULL != target.fAddr) { |
| 76 #if LAZY_CACHE_STATS |
| 77 sk_atomic_inc(&gCacheHits); |
| 78 #endif |
| 79 return target.fAddr; |
| 80 } |
| 81 #if LAZY_CACHE_STATS |
| 82 sk_atomic_inc(&gCacheMisses); |
| 83 #endif |
| 84 } |
| 85 SkASSERT(NULL == target.fAddr); |
| 86 SkImage::Info info; |
| 87 SkASSERT(fData != NULL && fData->size() > 0); |
| 88 // FIXME: As an optimization, only do this part once. |
| 89 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL); |
| 90 if (fErrorInDecoding) { |
| 91 fCacheId = SkImageCache::UNINITIALIZED_ID; |
| 92 return NULL; |
| 93 } |
| 94 // Allocate the memory. |
| 95 size_t bytes = ComputeMinRowBytesAndSize(info, &target.fRowBytes); |
| 96 |
| 97 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); |
66 if (NULL == target.fAddr) { | 98 if (NULL == target.fAddr) { |
67 SkImage::Info info; | 99 // Space could not be allocated. |
68 SkASSERT(fData != NULL && fData->size() > 0); | 100 fCacheId = SkImageCache::UNINITIALIZED_ID; |
69 // FIXME: As an optimization, only do this part once. | 101 return NULL; |
70 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NUL
L); | 102 } |
71 if (fErrorInDecoding) { | 103 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); |
72 fCacheId = SkImageCache::UNINITIALIZED_ID; | 104 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target
); |
73 return NULL; | 105 if (fErrorInDecoding) { |
74 } | 106 fImageCache->throwAwayCache(fCacheId); |
75 // Allocate the memory. | 107 fCacheId = SkImageCache::UNINITIALIZED_ID; |
76 size_t bytes = ComputeMinRowBytesAndSize(info, &target.fRowBytes); | 108 return NULL; |
77 | |
78 target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId); | |
79 if (NULL == target.fAddr) { | |
80 // Space could not be allocated. | |
81 fCacheId = SkImageCache::UNINITIALIZED_ID; | |
82 return NULL; | |
83 } | |
84 SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId); | |
85 fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &ta
rget); | |
86 if (fErrorInDecoding) { | |
87 fImageCache->throwAwayCache(fCacheId); | |
88 fCacheId = SkImageCache::UNINITIALIZED_ID; | |
89 return NULL; | |
90 } | |
91 } | 109 } |
92 return target.fAddr; | 110 return target.fAddr; |
93 } | 111 } |
94 | 112 |
95 void SkLazyPixelRef::onUnlockPixels() { | 113 void SkLazyPixelRef::onUnlockPixels() { |
96 if (fErrorInDecoding) { | 114 if (fErrorInDecoding) { |
97 return; | 115 return; |
98 } | 116 } |
99 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { | 117 if (fCacheId != SkImageCache::UNINITIALIZED_ID) { |
100 fImageCache->releaseCache(fCacheId); | 118 fImageCache->releaseCache(fCacheId); |
101 } | 119 } |
102 } | 120 } |
103 | 121 |
104 SkData* SkLazyPixelRef::onRefEncodedData() { | 122 SkData* SkLazyPixelRef::onRefEncodedData() { |
105 fData->ref(); | 123 fData->ref(); |
106 return fData; | 124 return fData; |
107 } | 125 } |
OLD | NEW |