OLD | NEW |
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 "SkDiscardablePixelRef.h" | 8 #include "SkDiscardablePixelRef.h" |
9 #include "SkDiscardableMemory.h" | 9 #include "SkDiscardableMemory.h" |
10 #include "SkImageGenerator.h" | 10 #include "SkImageGenerator.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 // decode the same image on each call to getPixels(). | 26 // decode the same image on each call to getPixels(). |
27 this->setImmutable(); | 27 this->setImmutable(); |
28 SkSafeRef(fDMFactory); | 28 SkSafeRef(fDMFactory); |
29 } | 29 } |
30 | 30 |
31 SkDiscardablePixelRef::~SkDiscardablePixelRef() { | 31 SkDiscardablePixelRef::~SkDiscardablePixelRef() { |
32 if (fDiscardableMemoryIsLocked) { | 32 if (fDiscardableMemoryIsLocked) { |
33 fDiscardableMemory->unlock(); | 33 fDiscardableMemory->unlock(); |
34 fDiscardableMemoryIsLocked = false; | 34 fDiscardableMemoryIsLocked = false; |
35 } | 35 } |
36 SkDELETE(fDiscardableMemory); | 36 delete fDiscardableMemory; |
37 SkSafeUnref(fDMFactory); | 37 SkSafeUnref(fDMFactory); |
38 SkDELETE(fGenerator); | 38 delete fGenerator; |
39 } | 39 } |
40 | 40 |
41 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 41 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
42 if (fDiscardableMemory != NULL) { | 42 if (fDiscardableMemory != NULL) { |
43 if (fDiscardableMemory->lock()) { | 43 if (fDiscardableMemory->lock()) { |
44 fDiscardableMemoryIsLocked = true; | 44 fDiscardableMemoryIsLocked = true; |
45 rec->fPixels = fDiscardableMemory->data(); | 45 rec->fPixels = fDiscardableMemory->data(); |
46 rec->fColorTable = fCTable.get(); | 46 rec->fColorTable = fCTable.get(); |
47 rec->fRowBytes = fRowBytes; | 47 rec->fRowBytes = fRowBytes; |
48 return true; | 48 return true; |
49 } | 49 } |
50 SkDELETE(fDiscardableMemory); | 50 delete fDiscardableMemory; |
51 fDiscardableMemory = NULL; | 51 fDiscardableMemory = NULL; |
52 fDiscardableMemoryIsLocked = false; | 52 fDiscardableMemoryIsLocked = false; |
53 } | 53 } |
54 | 54 |
55 const size_t size = this->info().getSafeSize(fRowBytes); | 55 const size_t size = this->info().getSafeSize(fRowBytes); |
56 | 56 |
57 if (fDMFactory != NULL) { | 57 if (fDMFactory != NULL) { |
58 fDiscardableMemory = fDMFactory->create(size); | 58 fDiscardableMemory = fDMFactory->create(size); |
59 fDiscardableMemoryIsLocked = true; | 59 fDiscardableMemoryIsLocked = true; |
60 } else { | 60 } else { |
61 fDiscardableMemory = SkDiscardableMemory::Create(size); | 61 fDiscardableMemory = SkDiscardableMemory::Create(size); |
62 fDiscardableMemoryIsLocked = true; | 62 fDiscardableMemoryIsLocked = true; |
63 } | 63 } |
64 if (NULL == fDiscardableMemory) { | 64 if (NULL == fDiscardableMemory) { |
65 fDiscardableMemoryIsLocked = false; | 65 fDiscardableMemoryIsLocked = false; |
66 return false; // Memory allocation failed. | 66 return false; // Memory allocation failed. |
67 } | 67 } |
68 | 68 |
69 void* pixels = fDiscardableMemory->data(); | 69 void* pixels = fDiscardableMemory->data(); |
70 const SkImageInfo& info = this->info(); | 70 const SkImageInfo& info = this->info(); |
71 SkPMColor colors[256]; | 71 SkPMColor colors[256]; |
72 int colorCount = 0; | 72 int colorCount = 0; |
73 | 73 |
74 if (!fGenerator->getPixels(info, pixels, fRowBytes, colors, &colorCount)) { | 74 if (!fGenerator->getPixels(info, pixels, fRowBytes, colors, &colorCount)) { |
75 fDiscardableMemory->unlock(); | 75 fDiscardableMemory->unlock(); |
76 fDiscardableMemoryIsLocked = false; | 76 fDiscardableMemoryIsLocked = false; |
77 SkDELETE(fDiscardableMemory); | 77 delete fDiscardableMemory; |
78 fDiscardableMemory = NULL; | 78 fDiscardableMemory = NULL; |
79 return false; | 79 return false; |
80 } | 80 } |
81 | 81 |
82 // Note: our ctable is not purgeable, as it is not stored in the discardable
memory block. | 82 // Note: our ctable is not purgeable, as it is not stored in the discardable
memory block. |
83 // This is because SkColorTable is refcntable, and therefore our caller coul
d hold onto it | 83 // This is because SkColorTable is refcntable, and therefore our caller coul
d hold onto it |
84 // beyond the scope of a lock/unlock. If we change the API/lifecycle for SkC
olorTable, we | 84 // beyond the scope of a lock/unlock. If we change the API/lifecycle for SkC
olorTable, we |
85 // could move it into the block, but then again perhaps it is small enough t
hat this doesn't | 85 // could move it into the block, but then again perhaps it is small enough t
hat this doesn't |
86 // really matter. | 86 // really matter. |
87 if (colorCount > 0) { | 87 if (colorCount > 0) { |
88 fCTable.reset(SkNEW_ARGS(SkColorTable, (colors, colorCount))); | 88 fCTable.reset(new SkColorTable(colors, colorCount)); |
89 } else { | 89 } else { |
90 fCTable.reset(NULL); | 90 fCTable.reset(NULL); |
91 } | 91 } |
92 | 92 |
93 rec->fPixels = pixels; | 93 rec->fPixels = pixels; |
94 rec->fColorTable = fCTable.get(); | 94 rec->fColorTable = fCTable.get(); |
95 rec->fRowBytes = fRowBytes; | 95 rec->fRowBytes = fRowBytes; |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return false; | 130 return false; |
131 } | 131 } |
132 | 132 |
133 // Since dst->setInfo() may have changed/fixed-up info, we check from the bi
tmap | 133 // Since dst->setInfo() may have changed/fixed-up info, we check from the bi
tmap |
134 SkASSERT(dst->info().colorType() != kUnknown_SkColorType); | 134 SkASSERT(dst->info().colorType() != kUnknown_SkColorType); |
135 | 135 |
136 if (dst->empty()) { // Use a normal pixelref. | 136 if (dst->empty()) { // Use a normal pixelref. |
137 return dst->tryAllocPixels(); | 137 return dst->tryAllocPixels(); |
138 } | 138 } |
139 SkAutoTUnref<SkDiscardablePixelRef> ref( | 139 SkAutoTUnref<SkDiscardablePixelRef> ref( |
140 SkNEW_ARGS(SkDiscardablePixelRef, | 140 new SkDiscardablePixelRef(prInfo, autoGenerator.detach(), dst->rowBy
tes(), factory)); |
141 (prInfo, autoGenerator.detach(), dst->rowBytes(), factory))); | |
142 dst->setPixelRef(ref, origin.x(), origin.y()); | 141 dst->setPixelRef(ref, origin.x(), origin.y()); |
143 return true; | 142 return true; |
144 } | 143 } |
145 | 144 |
146 // These are the public API | 145 // These are the public API |
147 | 146 |
148 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { | 147 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { |
149 return SkInstallDiscardablePixelRef(generator, NULL, dst, NULL); | 148 return SkInstallDiscardablePixelRef(generator, NULL, dst, NULL); |
150 } | 149 } |
151 | 150 |
152 bool SkInstallDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { | 151 bool SkInstallDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
153 SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); | 152 SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); |
154 return generator ? SkInstallDiscardablePixelRef(generator, NULL, dst, NULL)
: false; | 153 return generator ? SkInstallDiscardablePixelRef(generator, NULL, dst, NULL)
: false; |
155 } | 154 } |
OLD | NEW |