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

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

Issue 54203006: Break up SkLazyPixelRef functionally into class hierarchy. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Use SkImageInfo Created 7 years, 1 month 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
(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 if (NULL == fCache) {
15 fCache = SkScaledImageCache::GetGlobalInstance();
16 }
17 memset(&fInfo, 0xFF, sizeof(fInfo));
18 }
19 SkCachingPixelRef::~SkCachingPixelRef() {
20 SkASSERT(NULL == fScaledCacheId);
21 // Assert always unlock before unref.
22 }
23
24 const SkImageInfo& SkCachingPixelRef::getInfo() {
25 if (fErrorInDecoding) {
26 SkASSERT(fInfo.fWidth < 0);
27 return fInfo; // Don't try again.
28 }
29 if (fInfo.fWidth >= 0) {
30 return fInfo; // already successful
31 }
32 SkImageInfo tmp;
33 if (!this->onDecodeInfo(&tmp)) {
34 fErrorInDecoding = true;
35 return fInfo;
36 }
37 fInfo = tmp;
38 SkASSERT(fInfo.fWidth >= 0);
39 return fInfo;
40 }
41
42 bool SkCachingPixelRef::configure(SkBitmap* bitmap) {
43 const SkImageInfo& info = this->getInfo();
44 if (info.fWidth < 0) {
45 return false;
46 }
47 return bitmap->setConfig(info, 0);
48 }
49
50 void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
51 (void)colorTable;
52 const SkImageInfo& info = this->getInfo();
53 if (info.fWidth < 0) {
54 return NULL;
55 }
56 SkBitmap bitmap;
57
58 fScaledCacheId = fCache->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 = fCache->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(NULL != pixels);
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 (NULL != fScaledCacheId) {
91 fCache->unlock(fScaledCacheId);
92 fScaledCacheId = NULL;
93 }
94 }
95
96 bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
97 SkBitmap tmp;
98 // TODO(halcanary) - Enable SkCachingPixelRef to use a custom
99 // allocator. `tmp.allocPixels(fAllocator, NULL)`
100 if (!(this->configure(&tmp) && tmp.allocPixels(NULL, NULL))) {
101 fErrorInDecoding = true;
102 return false;
103 }
104 if (!this->onDecode(tmp.getPixels(), tmp.rowBytes())) {
105 fErrorInDecoding = true;
106 return false;
107 }
108 *bitmap = tmp;
109 return true;
110 }
111
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698