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 | |
10 SkCachingPixelRef::SkCachingPixelRef(SkScaledImageCache* cache) | |
11 : fErrorInDecoding(false) | |
12 , fCache(cache) | |
13 , fScaledCacheId(NULL) | |
14 , fBitmapSize(SkISize::Make(0, 0)) { | |
15 if (fCache == NULL) { | |
scroggo
2013/10/31 21:53:06
NULL == fCache.
hal.canary
2013/11/01 03:29:24
Done.
| |
16 fCache = SkScaledImageCache::Instance(); | |
17 } | |
18 } | |
19 SkCachingPixelRef::~SkCachingPixelRef() { | |
scroggo
2013/10/31 21:53:06
Maybe assert that fScaledCacheId is NULL?
hal.canary
2013/11/01 03:29:24
Done.
| |
20 } | |
21 | |
22 bool SkCachingPixelRef::updateInfo() { | |
23 if (fErrorInDecoding || !fBitmapSize.isZero()) { | |
scroggo
2013/10/31 21:53:06
This block is painful for me to look at. I think i
hal.canary
2013/11/01 03:29:24
Done.
| |
24 return !fErrorInDecoding; | |
25 } | |
26 if (!this->onDecodeInfo(&fBitmapSize, &fBitmapConfig, &fBitmapAlphaType)) { | |
27 fErrorInDecoding = true; | |
28 return false; | |
29 } | |
30 SkASSERT(!fBitmapSize.isZero()); | |
31 return true; | |
32 } | |
33 | |
34 bool SkCachingPixelRef::configure(SkBitmap* bitmap) { | |
35 if (!this->updateInfo()) { | |
36 return false; | |
37 } | |
38 return bitmap->setConfig(fBitmapConfig, fBitmapSize.width(), | |
39 fBitmapSize.height(), 0, fBitmapAlphaType); | |
40 } | |
41 | |
42 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { | |
43 (void)colorTable; | |
44 if (!this->updateInfo()) { | |
45 return NULL; | |
46 } | |
47 SkBitmap bitmap; | |
48 | |
49 fScaledCacheId = fCache->findAndLock(this->getGenerationID(), | |
50 fBitmapSize.fWidth, | |
51 fBitmapSize.fHeight, | |
52 &bitmap); | |
53 if (fScaledCacheId != NULL) { | |
54 SkAutoLockPixels autoLockPixels(bitmap); | |
55 void* pixels = bitmap.getPixels(); | |
56 SkASSERT(NULL != pixels); | |
57 // At this point, the autoLockPixels will unlockPixels() | |
58 // to remove bitmap's lock on the pixels. We will then | |
59 // destroy bitmap. The *only* guarantee that this pointer | |
60 // remains valid is the guarantee made by | |
61 // SkScaledImageCache that it will not destroy the *other* | |
62 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
63 // reference to the concrete PixelRef while this record is | |
64 // locked. | |
65 return pixels; | |
66 } else { | |
67 // Cache has been purged, must re-decode. | |
68 if (!this->onDecodeInto(0, &bitmap)) { | |
69 return NULL; | |
70 } | |
71 SkAutoLockPixels autoLockPixels(bitmap); | |
72 void* pixels = bitmap.getPixels(); | |
73 SkASSERT(pixels != NULL); | |
74 fScaledCacheId = fCache->addAndLock(this->getGenerationID(), | |
75 fBitmapSize.fWidth, | |
76 fBitmapSize.fHeight, | |
77 bitmap); | |
78 SkASSERT(fScaledCacheId != NULL); | |
79 return pixels; | |
scroggo
2013/10/31 21:53:06
Similar comment why pixels is still valid?
hal.canary
2013/11/01 03:29:24
I cleaned up the code to make this clear.
| |
80 } | |
81 } | |
82 | |
83 void SkCachingPixelRef::onUnlockPixels() { | |
84 if (!fErrorInDecoding && (NULL != fScaledCacheId)) { | |
scroggo
2013/10/31 21:53:06
If fErrorInDecoding is false, is it ever correct t
hal.canary
2013/11/01 03:29:24
I don't think so. Let me make this clear.
| |
85 fCache->unlock(fScaledCacheId); | |
86 fScaledCacheId = NULL; | |
87 } | |
88 } | |
89 | |
90 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { | |
91 SkBitmap tmp; | |
92 if (!(this->configure(&tmp) && tmp.allocPixels(NULL, NULL))) { | |
93 fErrorInDecoding = true; | |
94 return false; | |
95 } | |
96 if (!this->onDecode(tmp.getPixels(), tmp.rowBytes())) { | |
97 fErrorInDecoding = true; | |
98 return false; | |
99 } | |
100 *bitmap = tmp; | |
101 return true; | |
102 } | |
OLD | NEW |