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

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

Powered by Google App Engine
This is Rietveld 408576698