Chromium Code Reviews| 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 memset(&fInfo, 0xFF, sizeof(fInfo)); | |
| 15 } | |
| 16 SkCachingPixelRef::~SkCachingPixelRef() { | |
| 17 SkASSERT(NULL == fScaledCacheId); | |
| 18 // Assert always unlock before unref. | |
| 19 } | |
| 20 | |
| 21 bool SkCachingPixelRef::getInfo(SkImageInfo* info) { | |
| 22 SkASSERT(info != NULL); | |
| 23 if (fErrorInDecoding) { | |
| 24 SkASSERT(fInfo.fWidth < 0); | |
| 25 return false; // Don't try again. | |
| 26 } | |
| 27 if (fInfo.fWidth >= 0) { | |
| 28 *info = fInfo; | |
| 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 SkASSERT(bitmap != NULL); | |
| 43 SkImageInfo info; | |
| 44 if (!this->getInfo(&info)) { | |
| 45 return false; | |
| 46 } | |
| 47 return bitmap->setConfig(info, 0); | |
| 48 } | |
| 49 | |
| 50 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) { | |
| 51 (void)colorTable; | |
| 52 SkImageInfo info; | |
| 53 if (!this->getInfo(&info)) { | |
| 54 return NULL; | |
| 55 } | |
| 56 SkBitmap bitmap; | |
| 57 | |
| 58 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), | |
| 59 info.fWidth, | |
| 60 info.fHeight, | |
| 61 &bitmap); | |
| 62 if (NULL == fScaledCacheId) { | |
| 63 // Cache has been purged, must re-decode. | |
| 64 if (!this->onDecodeInto(0, &bitmap)) { | |
| 65 return NULL; | |
| 66 } | |
| 67 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), | |
| 68 info.fWidth, | |
| 69 info.fHeight, | |
| 70 bitmap); | |
| 71 SkASSERT(fScaledCacheId != NULL); | |
| 72 } | |
| 73 // Now bitmap should contain a concrete PixelRef of the decoded | |
| 74 // image. | |
| 75 SkAutoLockPixels autoLockPixels(bitmap); | |
| 76 void* pixels = bitmap.getPixels(); | |
| 77 SkASSERT(pixels != NULL); | |
| 78 // At this point, the autoLockPixels will unlockPixels() | |
| 79 // to remove bitmap's lock on the pixels. We will then | |
| 80 // destroy bitmap. The *only* guarantee that this pointer | |
| 81 // remains valid is the guarantee made by | |
| 82 // SkScaledImageCache that it will not destroy the *other* | |
| 83 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a | |
| 84 // reference to the concrete PixelRef while this record is | |
| 85 // locked. | |
| 86 return pixels; | |
| 87 } | |
| 88 | |
| 89 void SkCachingPixelRef::onUnlockPixels() { | |
| 90 if (fScaledCacheId != NULL) { | |
| 91 SkScaledImageCache::Unlock( | |
| 92 static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); | |
| 93 fScaledCacheId = NULL; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { | |
| 98 SkASSERT(bitmap != NULL); | |
| 99 SkBitmap tmp; | |
| 100 SkImageInfo info; | |
| 101 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom | |
| 102 // allocator. `tmp.allocPixels(fAllocator, NULL)` | |
| 103 if (!(this->getInfo(&info) | |
| 104 && this->configure(&tmp) | |
| 105 && tmp.allocPixels(NULL, NULL))) { | |
| 106 fErrorInDecoding = true; | |
|
reed1
2013/11/04 18:34:47
This could just be
if (!(this->configure(&tmp) &&
hal.canary
2013/11/04 19:41:07
Done.
| |
| 107 return false; | |
| 108 } | |
| 109 if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) { | |
| 110 fErrorInDecoding = true; | |
|
reed1
2013/11/04 18:34:47
should you set fInfo.fWidth = -1 here? If not, per
hal.canary
2013/11/04 19:41:07
Good catch.
| |
| 111 return false; | |
| 112 } | |
| 113 *bitmap = tmp; | |
| 114 return true; | |
| 115 } | |
| 116 | |
| OLD | NEW |