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