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

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

Issue 68973005: Expand pixelref to return SkImageInfo and rowbytes (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: new convention: require SkImageInfo in constructor Created 7 years 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
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "SkCachingPixelRef.h" 8 #include "SkCachingPixelRef.h"
9 #include "SkScaledImageCache.h" 9 #include "SkScaledImageCache.h"
10 10
11 SkCachingPixelRef::SkCachingPixelRef() 11 SkCachingPixelRef::SkCachingPixelRef(const SkImageInfo& info)
12 : fErrorInDecoding(false) 12 : INHERITED(info)
13 , fScaledCacheId(NULL) { 13 , fScaledCacheId(NULL)
14 memset(&fInfo, 0xFF, sizeof(fInfo)); 14 , fErrorInDecoding(false)
15 {
15 } 16 }
17
16 SkCachingPixelRef::~SkCachingPixelRef() { 18 SkCachingPixelRef::~SkCachingPixelRef() {
17 SkASSERT(NULL == fScaledCacheId); 19 SkASSERT(NULL == fScaledCacheId);
18 // Assert always unlock before unref. 20 // Assert always unlock before unref.
19 } 21 }
20 22
21 bool SkCachingPixelRef::getInfo(SkImageInfo* info) { 23 bool SkCachingPixelRef::configure(SkBitmap* bitmap) {
22 SkASSERT(info != NULL); 24 SkASSERT(bitmap != NULL);
23 if (fErrorInDecoding) { 25 return bitmap->setConfig(this->info(), 0);
24 return false; // Don't try again.
25 }
26 if (fInfo.fWidth < 0) {
27 SkImageInfo tmp;
28 if (!this->onDecodeInfo(&tmp)) {
29 fErrorInDecoding = true;
30 return false;
31 }
32 SkASSERT(tmp.fWidth >= 0);
33 fInfo = tmp;
34 }
35 *info = fInfo;
36 return true;
37 } 26 }
38 27
39 bool SkCachingPixelRef::configure(SkBitmap* bitmap) { 28 bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) {
40 SkASSERT(bitmap != NULL); 29 const SkImageInfo& info = this->info();
41 SkImageInfo info;
42 if (!this->getInfo(&info)) {
43 return false;
44 }
45 return bitmap->setConfig(info, 0);
46 }
47
48 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
49 (void)colorTable;
50 SkImageInfo info;
51 if (!this->getInfo(&info)) {
52 return NULL;
53 }
54 SkBitmap bitmap; 30 SkBitmap bitmap;
55 31
56 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(), 32 fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
57 info.fWidth, 33 info.fWidth,
58 info.fHeight, 34 info.fHeight,
59 &bitmap); 35 &bitmap);
60 if (NULL == fScaledCacheId) { 36 if (NULL == fScaledCacheId) {
61 // Cache has been purged, must re-decode. 37 // Cache has been purged, must re-decode.
62 if (!this->onDecodeInto(0, &bitmap)) { 38 if (!this->onDecodeInto(0, &bitmap)) {
63 return NULL; 39 return false;
64 } 40 }
65 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(), 41 fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
66 info.fWidth, 42 info.fWidth,
67 info.fHeight, 43 info.fHeight,
68 bitmap); 44 bitmap);
69 SkASSERT(fScaledCacheId != NULL); 45 SkASSERT(fScaledCacheId != NULL);
70 } 46 }
71 // Now bitmap should contain a concrete PixelRef of the decoded 47 // Now bitmap should contain a concrete PixelRef of the decoded
72 // image. 48 // image.
73 SkAutoLockPixels autoLockPixels(bitmap); 49 SkAutoLockPixels autoLockPixels(bitmap);
74 void* pixels = bitmap.getPixels(); 50 void* pixels = bitmap.getPixels();
75 SkASSERT(pixels != NULL); 51 SkASSERT(pixels != NULL);
52
76 // At this point, the autoLockPixels will unlockPixels() 53 // At this point, the autoLockPixels will unlockPixels()
77 // to remove bitmap's lock on the pixels. We will then 54 // to remove bitmap's lock on the pixels. We will then
78 // destroy bitmap. The *only* guarantee that this pointer 55 // destroy bitmap. The *only* guarantee that this pointer
79 // remains valid is the guarantee made by 56 // remains valid is the guarantee made by
80 // SkScaledImageCache that it will not destroy the *other* 57 // SkScaledImageCache that it will not destroy the *other*
81 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a 58 // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
82 // reference to the concrete PixelRef while this record is 59 // reference to the concrete PixelRef while this record is
83 // locked. 60 // locked.
84 return pixels; 61 rec->fPixels = pixels;
62 rec->fColorTable = NULL;
63 rec->fRowBytes = bitmap.rowBytes();
64 return true;
85 } 65 }
86 66
87 void SkCachingPixelRef::onUnlockPixels() { 67 void SkCachingPixelRef::onUnlockPixels() {
88 if (fScaledCacheId != NULL) { 68 if (fScaledCacheId != NULL) {
89 SkScaledImageCache::Unlock( 69 SkScaledImageCache::Unlock(
90 static_cast<SkScaledImageCache::ID*>(fScaledCacheId)); 70 static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
91 fScaledCacheId = NULL; 71 fScaledCacheId = NULL;
92 } 72 }
93 } 73 }
94 74
95 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) { 75 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
96 SkASSERT(bitmap != NULL); 76 SkASSERT(bitmap != NULL);
97 SkBitmap tmp; 77 SkBitmap tmp;
98 SkImageInfo info; 78
99 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom 79 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom
100 // allocator. `tmp.allocPixels(fAllocator, NULL)` 80 // allocator. `tmp.allocPixels(fAllocator, NULL)`
101 if (!(this->configure(&tmp) && tmp.allocPixels())) { 81 if (!(this->configure(&tmp) && tmp.allocPixels())) {
102 return false; 82 return false;
103 } 83 }
104 SkAssertResult(this->getInfo(&info)); // since configure() succeeded. 84 if (!this->onDecodePixels(tmp.getPixels(), tmp.rowBytes())) {
105 if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) {
106 fErrorInDecoding = true; 85 fErrorInDecoding = true;
107 return false; 86 return false;
108 } 87 }
109 *bitmap = tmp; 88 *bitmap = tmp;
110 return true; 89 return true;
111 } 90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698