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