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