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