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" |
11 | 11 |
12 SkDiscardablePixelRef::SkDiscardablePixelRef(SkImageGenerator* generator, | 12 SkDiscardablePixelRef::SkDiscardablePixelRef(SkImageGenerator* generator, |
13 const SkImageInfo& info, | 13 const SkImageInfo& info, |
14 size_t size, | |
15 size_t rowBytes, | 14 size_t rowBytes, |
16 SkDiscardableMemory::Factory* fact) | 15 SkDiscardableMemory::Factory* fact) |
17 : fGenerator(generator) | 16 : INHERITED(info) |
| 17 , fGenerator(generator) |
18 , fDMFactory(fact) | 18 , fDMFactory(fact) |
19 , fInfo(info) | |
20 , fSize(size) | |
21 , fRowBytes(rowBytes) | 19 , fRowBytes(rowBytes) |
22 , fDiscardableMemory(NULL) { | 20 , fDiscardableMemory(NULL) |
| 21 { |
23 SkASSERT(fGenerator != NULL); | 22 SkASSERT(fGenerator != NULL); |
24 SkASSERT(fSize > 0); | |
25 SkASSERT(fRowBytes > 0); | 23 SkASSERT(fRowBytes > 0); |
26 // The SkImageGenerator contract requires fGenerator to always | 24 // The SkImageGenerator contract requires fGenerator to always |
27 // decode the same image on each call to getPixels(). | 25 // decode the same image on each call to getPixels(). |
28 this->setImmutable(); | 26 this->setImmutable(); |
29 SkSafeRef(fDMFactory); | 27 SkSafeRef(fDMFactory); |
30 } | 28 } |
31 | 29 |
32 SkDiscardablePixelRef::~SkDiscardablePixelRef() { | 30 SkDiscardablePixelRef::~SkDiscardablePixelRef() { |
33 if (this->isLocked()) { | 31 if (this->isLocked()) { |
34 fDiscardableMemory->unlock(); | 32 fDiscardableMemory->unlock(); |
35 } | 33 } |
36 SkDELETE(fDiscardableMemory); | 34 SkDELETE(fDiscardableMemory); |
37 SkSafeUnref(fDMFactory); | 35 SkSafeUnref(fDMFactory); |
38 SkDELETE(fGenerator); | 36 SkDELETE(fGenerator); |
39 } | 37 } |
40 | 38 |
41 void* SkDiscardablePixelRef::onLockPixels(SkColorTable**) { | 39 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
42 if (fDiscardableMemory != NULL) { | 40 if (fDiscardableMemory != NULL) { |
43 if (fDiscardableMemory->lock()) { | 41 if (fDiscardableMemory->lock()) { |
44 return fDiscardableMemory->data(); | 42 rec->fPixels = fDiscardableMemory->data(); |
| 43 rec->fColorTable = NULL; |
| 44 rec->fRowBytes = fRowBytes; |
| 45 return true; |
45 } | 46 } |
46 SkDELETE(fDiscardableMemory); | 47 SkDELETE(fDiscardableMemory); |
47 fDiscardableMemory = NULL; | 48 fDiscardableMemory = NULL; |
48 } | 49 } |
| 50 |
| 51 const size_t size = this->info().getSafeSize(fRowBytes); |
49 if (fDMFactory != NULL) { | 52 if (fDMFactory != NULL) { |
50 fDiscardableMemory = fDMFactory->create(fSize); | 53 fDiscardableMemory = fDMFactory->create(size); |
51 } else { | 54 } else { |
52 fDiscardableMemory = SkDiscardableMemory::Create(fSize); | 55 fDiscardableMemory = SkDiscardableMemory::Create(size); |
53 } | 56 } |
54 if (NULL == fDiscardableMemory) { | 57 if (NULL == fDiscardableMemory) { |
55 return NULL; // Memory allocation failed. | 58 return false; // Memory allocation failed. |
56 } | 59 } |
| 60 |
57 void* pixels = fDiscardableMemory->data(); | 61 void* pixels = fDiscardableMemory->data(); |
58 if (!fGenerator->getPixels(fInfo, pixels, fRowBytes)) { | 62 if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) { |
59 fDiscardableMemory->unlock(); | 63 fDiscardableMemory->unlock(); |
60 SkDELETE(fDiscardableMemory); | 64 SkDELETE(fDiscardableMemory); |
61 fDiscardableMemory = NULL; | 65 fDiscardableMemory = NULL; |
62 return NULL; | 66 return NULL; |
63 } | 67 } |
64 return pixels; | 68 |
| 69 rec->fPixels = pixels; |
| 70 rec->fColorTable = NULL; |
| 71 rec->fRowBytes = fRowBytes; |
| 72 return true; |
65 } | 73 } |
| 74 |
66 void SkDiscardablePixelRef::onUnlockPixels() { | 75 void SkDiscardablePixelRef::onUnlockPixels() { |
67 fDiscardableMemory->unlock(); | 76 fDiscardableMemory->unlock(); |
68 } | 77 } |
69 | 78 |
70 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, | 79 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, |
71 SkBitmap* dst, | 80 SkBitmap* dst, |
72 SkDiscardableMemory::Factory* factory) { | 81 SkDiscardableMemory::Factory* factory) { |
73 SkImageInfo info; | 82 SkImageInfo info; |
74 SkASSERT(generator != NULL); | 83 SkASSERT(generator != NULL); |
75 if ((NULL == generator) | 84 if ((NULL == generator) |
76 || (!generator->getInfo(&info)) | 85 || (!generator->getInfo(&info)) |
77 || (!dst->setConfig(info, 0))) { | 86 || (!dst->setConfig(info, 0))) { |
78 SkDELETE(generator); | 87 SkDELETE(generator); |
79 return false; | 88 return false; |
80 } | 89 } |
81 SkASSERT(dst->config() != SkBitmap::kNo_Config); | 90 SkASSERT(dst->config() != SkBitmap::kNo_Config); |
82 if (dst->empty()) { // Use a normal pixelref. | 91 if (dst->empty()) { // Use a normal pixelref. |
83 SkDELETE(generator); // Do not need this anymore. | 92 SkDELETE(generator); // Do not need this anymore. |
84 return dst->allocPixels(NULL, NULL); | 93 return dst->allocPixels(NULL, NULL); |
85 } | 94 } |
86 SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef, | 95 SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef, |
87 (generator, info, | 96 (generator, info, |
88 dst->getSize(), | |
89 dst->rowBytes(), | 97 dst->rowBytes(), |
90 factory))); | 98 factory))); |
91 dst->setPixelRef(ref); | 99 dst->setPixelRef(ref); |
92 return true; | 100 return true; |
93 } | 101 } |
OLD | NEW |