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 SkDELETE(fDiscardableMemory); | 33 SkDELETE(fDiscardableMemory); |
32 SkSafeUnref(fDMFactory); | 34 SkSafeUnref(fDMFactory); |
33 SkDELETE(fGenerator); | 35 SkDELETE(fGenerator); |
34 } | 36 } |
35 | 37 |
36 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 38 void* SkDiscardablePixelRef::onLockPixels(SkColorTable**) { |
37 if (fDiscardableMemory != NULL) { | 39 if (fDiscardableMemory != NULL) { |
38 if (fDiscardableMemory->lock()) { | 40 if (fDiscardableMemory->lock()) { |
39 rec->fPixels = fDiscardableMemory->data(); | 41 return fDiscardableMemory->data(); |
40 rec->fColorTable = NULL; | |
41 rec->fRowBytes = fRowBytes; | |
42 return true; | |
43 } | 42 } |
44 SkDELETE(fDiscardableMemory); | 43 SkDELETE(fDiscardableMemory); |
45 fDiscardableMemory = NULL; | 44 fDiscardableMemory = NULL; |
46 } | 45 } |
47 | |
48 const size_t size = this->info().getSafeSize(fRowBytes); | |
49 if (fDMFactory != NULL) { | 46 if (fDMFactory != NULL) { |
50 fDiscardableMemory = fDMFactory->create(size); | 47 fDiscardableMemory = fDMFactory->create(fSize); |
51 } else { | 48 } else { |
52 fDiscardableMemory = SkDiscardableMemory::Create(size); | 49 fDiscardableMemory = SkDiscardableMemory::Create(fSize); |
53 } | 50 } |
54 if (NULL == fDiscardableMemory) { | 51 if (NULL == fDiscardableMemory) { |
55 return false; // Memory allocation failed. | 52 return NULL; // Memory allocation failed. |
56 } | 53 } |
57 | |
58 void* pixels = fDiscardableMemory->data(); | 54 void* pixels = fDiscardableMemory->data(); |
59 if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) { | 55 if (!fGenerator->getPixels(fInfo, pixels, fRowBytes)) { |
60 return false; // TODO(halcanary) Find out correct thing to do. | 56 return NULL; // TODO(halcanary) Find out correct thing to do. |
61 } | 57 } |
62 | 58 return pixels; |
63 rec->fPixels = pixels; | |
64 rec->fColorTable = NULL; | |
65 rec->fRowBytes = fRowBytes; | |
66 return true; | |
67 } | 59 } |
68 | |
69 void SkDiscardablePixelRef::onUnlockPixels() { | 60 void SkDiscardablePixelRef::onUnlockPixels() { |
70 if (fDiscardableMemory != NULL) { | 61 if (fDiscardableMemory != NULL) { |
71 fDiscardableMemory->unlock(); | 62 fDiscardableMemory->unlock(); |
72 } | 63 } |
73 } | 64 } |
74 | 65 |
75 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, | 66 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, |
76 SkBitmap* dst, | 67 SkBitmap* dst, |
77 SkDiscardableMemory::Factory* factory) { | 68 SkDiscardableMemory::Factory* factory) { |
78 SkImageInfo info; | 69 SkImageInfo info; |
79 SkASSERT(generator != NULL); | 70 SkASSERT(generator != NULL); |
80 if ((NULL == generator) | 71 if ((NULL == generator) |
81 || (!generator->getInfo(&info)) | 72 || (!generator->getInfo(&info)) |
82 || (!dst->setConfig(info, 0))) { | 73 || (!dst->setConfig(info, 0))) { |
83 SkDELETE(generator); | 74 SkDELETE(generator); |
84 return false; | 75 return false; |
85 } | 76 } |
86 SkASSERT(dst->config() != SkBitmap::kNo_Config); | 77 SkASSERT(dst->config() != SkBitmap::kNo_Config); |
87 if (dst->empty()) { // Use a normal pixelref. | 78 if (dst->empty()) { // Use a normal pixelref. |
88 SkDELETE(generator); // Do not need this anymore. | 79 SkDELETE(generator); // Do not need this anymore. |
89 return dst->allocPixels(NULL, NULL); | 80 return dst->allocPixels(NULL, NULL); |
90 } | 81 } |
91 SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef, | 82 SkAutoTUnref<SkDiscardablePixelRef> ref(SkNEW_ARGS(SkDiscardablePixelRef, |
92 (generator, info, | 83 (generator, info, |
| 84 dst->getSize(), |
93 dst->rowBytes(), | 85 dst->rowBytes(), |
94 factory))); | 86 factory))); |
95 dst->setPixelRef(ref); | 87 dst->setPixelRef(ref); |
96 return true; | 88 return true; |
97 } | 89 } |
OLD | NEW |