| 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" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 if (fDMFactory != NULL) { | 53 if (fDMFactory != NULL) { |
| 54 fDiscardableMemory = fDMFactory->create(size); | 54 fDiscardableMemory = fDMFactory->create(size); |
| 55 } else { | 55 } else { |
| 56 fDiscardableMemory = SkDiscardableMemory::Create(size); | 56 fDiscardableMemory = SkDiscardableMemory::Create(size); |
| 57 } | 57 } |
| 58 if (NULL == fDiscardableMemory) { | 58 if (NULL == fDiscardableMemory) { |
| 59 return false; // Memory allocation failed. | 59 return false; // Memory allocation failed. |
| 60 } | 60 } |
| 61 | 61 |
| 62 void* pixels = fDiscardableMemory->data(); | 62 void* pixels = fDiscardableMemory->data(); |
| 63 if (!fGenerator->getPixels(this->info(), pixels, fRowBytes)) { | 63 const SkImageInfo& info = this->info(); |
| 64 SkPMColor colors[256]; |
| 65 int colorCount = 0; |
| 66 |
| 67 if (!fGenerator->getPixels(info, pixels, fRowBytes, colors, &colorCount)) { |
| 64 fDiscardableMemory->unlock(); | 68 fDiscardableMemory->unlock(); |
| 65 SkDELETE(fDiscardableMemory); | 69 SkDELETE(fDiscardableMemory); |
| 66 fDiscardableMemory = NULL; | 70 fDiscardableMemory = NULL; |
| 67 return false; | 71 return false; |
| 68 } | 72 } |
| 69 | 73 |
| 74 // Note: our ctable is not purgable, as it is not stored in the discardablem
emory block. |
| 75 // This is because SkColorTable is refcntable, and therefore our caller coul
d hold onto it |
| 76 // beyond the scope of a lock/unlock. If we change the API/lifecycle for SkC
olorTable, we |
| 77 // could move it into the block, but then again perhaps it is small enough t
hat this doesn't |
| 78 // really matter. |
| 79 if (colorCount > 0) { |
| 80 fCTable.reset(SkNEW_ARGS(SkColorTable, (colors, colorCount))); |
| 81 } else { |
| 82 fCTable.reset(NULL); |
| 83 } |
| 84 |
| 70 rec->fPixels = pixels; | 85 rec->fPixels = pixels; |
| 71 rec->fColorTable = NULL; | 86 rec->fColorTable = fCTable.get(); |
| 72 rec->fRowBytes = fRowBytes; | 87 rec->fRowBytes = fRowBytes; |
| 73 return true; | 88 return true; |
| 74 } | 89 } |
| 75 | 90 |
| 76 void SkDiscardablePixelRef::onUnlockPixels() { | 91 void SkDiscardablePixelRef::onUnlockPixels() { |
| 77 fDiscardableMemory->unlock(); | 92 fDiscardableMemory->unlock(); |
| 78 } | 93 } |
| 79 | 94 |
| 80 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst, | 95 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst, |
| 81 SkDiscardableMemory::Factory* factory) { | 96 SkDiscardableMemory::Factory* factory) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 94 SkNEW_ARGS(SkDiscardablePixelRef, | 109 SkNEW_ARGS(SkDiscardablePixelRef, |
| 95 (info, autoGenerator.detach(), dst->rowBytes(), factory))); | 110 (info, autoGenerator.detach(), dst->rowBytes(), factory))); |
| 96 dst->setPixelRef(ref); | 111 dst->setPixelRef(ref); |
| 97 return true; | 112 return true; |
| 98 } | 113 } |
| 99 | 114 |
| 100 // This is the public API | 115 // This is the public API |
| 101 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { | 116 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { |
| 102 return SkInstallDiscardablePixelRef(generator, dst, NULL); | 117 return SkInstallDiscardablePixelRef(generator, dst, NULL); |
| 103 } | 118 } |
| OLD | NEW |