OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkCachingPixelRef.h" | |
9 #include "SkScaledImageCache.h" | |
10 | |
11 SkCachingPixelRef::SkCachingPixelRef() | |
12 : fErrorInDecoding(false) | |
13 , fScaledCacheId(NULL) { | |
14 // Please note: a non-NULL cache is not thread-safe. | |
scroggo
2013/11/04 17:18:03
Obsolete comment.
hal.canary
2013/11/04 18:24:19
Done.
| |
15 memset(&fInfo, 0xFF, sizeof(fInfo)); | |
16 } | |
17 SkCachingPixelRef::~SkCachingPixelRef() { | |
18 SkASSERT(NULL == fScaledCacheId); | |
19 // Assert always unlock before unref. | |
20 } | |
21 | |
22 bool SkCachingPixelRef::getInfo(SkImageInfo* info) { | |
23 if (fErrorInDecoding) { | |
24 SkASSERT(fInfo.fWidth < 0); | |
25 return false; // Don't try again. | |
26 } | |
27 if (fInfo.fWidth >= 0) { | |
28 *info = fInfo; | |
scroggo
2013/11/04 17:18:03
Can you assert that info is not NULL?
hal.canary
2013/11/04 18:24:19
Done.
| |
29 return true; // already successful | |
30 } | |
31 SkImageInfo tmp; | |
32 if (!this->onDecodeInfo(&tmp)) { | |
33 fErrorInDecoding = true; | |
34 return false; | |
35 } | |
36 SkASSERT(tmp.fWidth >= 0); | |
37 *info = fInfo = tmp; | |
38 return true; | |
39 } | |
40 | |
41 bool SkCachingPixelRef::configure(SkBitmap* bitmap) { | |
42 SkImageInfo info; | |
43 if (!this->getInfo(&info)) { | |
44 return false; | |
45 } | |
46 return bitmap->setConfig(info, 0); | |
47 } | |
48 | |
49 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { | |
50 (void)colorTable; | |
51 SkImageInfo info; | |
52 if (!this->getInfo(&info)) { | |
53 return NULL; | |
54 } | |
55 SkBitmap bitmap; | |
56 | |
57 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | |
58 info.fWidth, | |
scroggo
2013/11/04 17:18:03
It looks like these are tabs? If so, please conver
hal.canary
2013/11/04 18:24:19
Done.
| |
59 info.fHeight, | |
60 &bitmap); | |
61 if (NULL == fScaledCacheId) { | |
62 // Cache has been purged, must re-decode. | |
63 if (!this->onDecodeInto(0, &bitmap)) { | |
64 return NULL; | |
65 } | |
66 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | |
67 info.fWidth, | |
68 info.fHeight, | |
69 bitmap); | |
70 SkASSERT(fScaledCacheId != NULL); | |
71 } | |
72 // Now bitmap should contain a concrete PixelRef of the decoded | |
73 // image. | |
74 SkAutoLockPixels autoLockPixels(bitmap); | |
75 void* pixels = bitmap.getPixels(); | |
76 SkASSERT(NULL != pixels); | |
77 // At this point, the autoLockPixels will unlockPixels() | |
78 // to remove bitmap's lock on the pixels. We will then | |
79 // destroy bitmap. The *only* guarantee that this pointer | |
80 // remains valid is the guarantee made by | |
81 // SkScaledImageCache that it will not destroy the *other* | |
82 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
83 // reference to the concrete PixelRef while this record is | |
84 // locked. | |
85 return pixels; | |
86 } | |
87 | |
88 void SkCachingPixelRef::onUnlockPixels() { | |
89 if (NULL != fScaledCacheId) { | |
90 SkScaledImageCache::Unlock( | |
91 static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); | |
92 fScaledCacheId = NULL; | |
93 } | |
94 } | |
95 | |
96 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { | |
97 SkBitmap tmp; | |
98 SkImageInfo info; | |
99 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom | |
100 // allocator. `tmp.allocPixels(fAllocator, NULL)` | |
101 if (!(this->getInfo(&info) | |
102 && this->configure(&tmp) | |
103 && tmp.allocPixels(NULL, NULL))) { | |
104 fErrorInDecoding = true; | |
105 return false; | |
106 } | |
107 if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) { | |
108 fErrorInDecoding = true; | |
109 return false; | |
110 } | |
111 *bitmap = tmp; | |
112 return true; | |
113 } | |
114 | |
OLD | NEW |