| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkMallocPixelRef.h" | 8 #include "SkMallocPixelRef.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkWriteBuffer.h" | 11 #include "SkWriteBuffer.h" |
| 12 | 12 |
| 13 // assumes ptr was allocated via sk_malloc | 13 // assumes ptr was allocated via sk_malloc |
| 14 static void sk_free_releaseproc(void* ptr, void*) { | 14 static void sk_free_releaseproc(void* ptr, void*) { |
| 15 sk_free(ptr); | 15 sk_free(ptr); |
| 16 } | 16 } |
| 17 | 17 |
| 18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { | 18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) { |
| 19 if (info.width() < 0 || info.height() < 0 || | 19 if (info.width() < 0 || info.height() < 0 || |
| 20 (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType || | 20 (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType || |
| 21 (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) | 21 (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) |
| 22 { | 22 { |
| 23 return false; | 23 return false; |
| 24 } | 24 } |
| 25 | 25 |
| 26 // these seem like good checks, but currently we have (at least) tests | 26 // these seem like good checks, but currently we have (at least) tests |
| 27 // that expect the pixelref to succeed even when there is a mismatch | 27 // that expect the pixelref to succeed even when there is a mismatch |
| 28 // with colortables. fix? | 28 // with colortables. fix? |
| 29 #if 0 | 29 #if 0 |
| 30 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) { | 30 if (kIndex8_SkColorType == info.fColorType && nullptr == ctable) { |
| 31 return false; | 31 return false; |
| 32 } | 32 } |
| 33 if (kIndex8_SkColorType != info.fColorType && ctable) { | 33 if (kIndex8_SkColorType != info.fColorType && ctable) { |
| 34 return false; | 34 return false; |
| 35 } | 35 } |
| 36 #endif | 36 #endif |
| 37 return true; | 37 return true; |
| 38 } | 38 } |
| 39 | 39 |
| 40 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info, | 40 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info, |
| 41 void* addr, | 41 void* addr, |
| 42 size_t rowBytes, | 42 size_t rowBytes, |
| 43 SkColorTable* ctable) { | 43 SkColorTable* ctable) { |
| 44 if (!is_valid(info, ctable)) { | 44 if (!is_valid(info, ctable)) { |
| 45 return NULL; | 45 return nullptr; |
| 46 } | 46 } |
| 47 return new SkMallocPixelRef(info, addr, rowBytes, ctable, NULL, NULL); | 47 return new SkMallocPixelRef(info, addr, rowBytes, ctable, nullptr, nullptr); |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, | 51 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info, |
| 52 size_t requestedRowBytes, | 52 size_t requestedRowBytes, |
| 53 SkColorTable* ctable) { | 53 SkColorTable* ctable) { |
| 54 if (!is_valid(info, ctable)) { | 54 if (!is_valid(info, ctable)) { |
| 55 return NULL; | 55 return nullptr; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // only want to permit 31bits of rowBytes | 58 // only want to permit 31bits of rowBytes |
| 59 int64_t minRB = (int64_t)info.minRowBytes64(); | 59 int64_t minRB = (int64_t)info.minRowBytes64(); |
| 60 if (minRB < 0 || !sk_64_isS32(minRB)) { | 60 if (minRB < 0 || !sk_64_isS32(minRB)) { |
| 61 return NULL; // allocation will be too large | 61 return nullptr; // allocation will be too large |
| 62 } | 62 } |
| 63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { | 63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { |
| 64 return NULL; // cannot meet requested rowbytes | 64 return nullptr; // cannot meet requested rowbytes |
| 65 } | 65 } |
| 66 | 66 |
| 67 int32_t rowBytes; | 67 int32_t rowBytes; |
| 68 if (requestedRowBytes) { | 68 if (requestedRowBytes) { |
| 69 rowBytes = SkToS32(requestedRowBytes); | 69 rowBytes = SkToS32(requestedRowBytes); |
| 70 } else { | 70 } else { |
| 71 rowBytes = minRB; | 71 rowBytes = minRB; |
| 72 } | 72 } |
| 73 | 73 |
| 74 int64_t bigSize = (int64_t)info.height() * rowBytes; | 74 int64_t bigSize = (int64_t)info.height() * rowBytes; |
| 75 if (!sk_64_isS32(bigSize)) { | 75 if (!sk_64_isS32(bigSize)) { |
| 76 return NULL; | 76 return nullptr; |
| 77 } | 77 } |
| 78 | 78 |
| 79 size_t size = sk_64_asS32(bigSize); | 79 size_t size = sk_64_asS32(bigSize); |
| 80 SkASSERT(size >= info.getSafeSize(rowBytes)); | 80 SkASSERT(size >= info.getSafeSize(rowBytes)); |
| 81 void* addr = sk_malloc_flags(size, 0); | 81 void* addr = sk_malloc_flags(size, 0); |
| 82 if (NULL == addr) { | 82 if (nullptr == addr) { |
| 83 return NULL; | 83 return nullptr; |
| 84 } | 84 } |
| 85 | 85 |
| 86 return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releasepro
c, NULL); | 86 return new SkMallocPixelRef(info, addr, rowBytes, ctable, sk_free_releasepro
c, nullptr); |
| 87 } | 87 } |
| 88 | 88 |
| 89 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, | 89 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info, |
| 90 size_t rowBytes, | 90 size_t rowBytes, |
| 91 SkColorTable* ctable, | 91 SkColorTable* ctable, |
| 92 void* addr, | 92 void* addr, |
| 93 SkMallocPixelRef::ReleaseProc pr
oc, | 93 SkMallocPixelRef::ReleaseProc pr
oc, |
| 94 void* context) { | 94 void* context) { |
| 95 if (!is_valid(info, ctable)) { | 95 if (!is_valid(info, ctable)) { |
| 96 return NULL; | 96 return nullptr; |
| 97 } | 97 } |
| 98 return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); | 98 return new SkMallocPixelRef(info, addr, rowBytes, ctable, proc, context); |
| 99 } | 99 } |
| 100 | 100 |
| 101 static void sk_data_releaseproc(void*, void* dataPtr) { | 101 static void sk_data_releaseproc(void*, void* dataPtr) { |
| 102 (static_cast<SkData*>(dataPtr))->unref(); | 102 (static_cast<SkData*>(dataPtr))->unref(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info, | 105 SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info, |
| 106 size_t rowBytes, | 106 size_t rowBytes, |
| 107 SkColorTable* ctable, | 107 SkColorTable* ctable, |
| 108 SkData* data) { | 108 SkData* data) { |
| 109 SkASSERT(data != NULL); | 109 SkASSERT(data != nullptr); |
| 110 if (!is_valid(info, ctable)) { | 110 if (!is_valid(info, ctable)) { |
| 111 return NULL; | 111 return nullptr; |
| 112 } | 112 } |
| 113 if ((rowBytes < info.minRowBytes()) | 113 if ((rowBytes < info.minRowBytes()) |
| 114 || (data->size() < info.getSafeSize(rowBytes))) { | 114 || (data->size() < info.getSafeSize(rowBytes))) { |
| 115 return NULL; | 115 return nullptr; |
| 116 } | 116 } |
| 117 data->ref(); | 117 data->ref(); |
| 118 SkMallocPixelRef* pr = | 118 SkMallocPixelRef* pr = |
| 119 new SkMallocPixelRef(info, const_cast<void*>(data->data()), rowBytes
, ctable, | 119 new SkMallocPixelRef(info, const_cast<void*>(data->data()), rowBytes
, ctable, |
| 120 sk_data_releaseproc, static_cast<void*>(data)); | 120 sk_data_releaseproc, static_cast<void*>(data)); |
| 121 SkASSERT(pr != NULL); | 121 SkASSERT(pr != nullptr); |
| 122 // We rely on the immutability of the pixels to make the | 122 // We rely on the immutability of the pixels to make the |
| 123 // const_cast okay. | 123 // const_cast okay. |
| 124 pr->setImmutable(); | 124 pr->setImmutable(); |
| 125 return pr; | 125 return pr; |
| 126 } | 126 } |
| 127 | 127 |
| 128 /////////////////////////////////////////////////////////////////////////////// | 128 /////////////////////////////////////////////////////////////////////////////// |
| 129 | 129 |
| 130 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, | 130 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, |
| 131 size_t rowBytes, SkColorTable* ctable, | 131 size_t rowBytes, SkColorTable* ctable, |
| 132 bool ownsPixels) | 132 bool ownsPixels) |
| 133 : INHERITED(info) | 133 : INHERITED(info) |
| 134 , fReleaseProc(ownsPixels ? sk_free_releaseproc : NULL) | 134 , fReleaseProc(ownsPixels ? sk_free_releaseproc : nullptr) |
| 135 , fReleaseProcContext(NULL) { | 135 , fReleaseProcContext(nullptr) { |
| 136 // This constructor is now DEPRICATED. | 136 // This constructor is now DEPRICATED. |
| 137 SkASSERT(is_valid(info, ctable)); | 137 SkASSERT(is_valid(info, ctable)); |
| 138 SkASSERT(rowBytes >= info.minRowBytes()); | 138 SkASSERT(rowBytes >= info.minRowBytes()); |
| 139 | 139 |
| 140 if (kIndex_8_SkColorType != info.colorType()) { | 140 if (kIndex_8_SkColorType != info.colorType()) { |
| 141 ctable = NULL; | 141 ctable = nullptr; |
| 142 } | 142 } |
| 143 | 143 |
| 144 fStorage = storage; | 144 fStorage = storage; |
| 145 fCTable = ctable; | 145 fCTable = ctable; |
| 146 fRB = rowBytes; | 146 fRB = rowBytes; |
| 147 SkSafeRef(ctable); | 147 SkSafeRef(ctable); |
| 148 | 148 |
| 149 this->setPreLocked(fStorage, rowBytes, fCTable); | 149 this->setPreLocked(fStorage, rowBytes, fCTable); |
| 150 } | 150 } |
| 151 | 151 |
| 152 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, | 152 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, |
| 153 size_t rowBytes, SkColorTable* ctable, | 153 size_t rowBytes, SkColorTable* ctable, |
| 154 SkMallocPixelRef::ReleaseProc proc, | 154 SkMallocPixelRef::ReleaseProc proc, |
| 155 void* context) | 155 void* context) |
| 156 : INHERITED(info) | 156 : INHERITED(info) |
| 157 , fReleaseProc(proc) | 157 , fReleaseProc(proc) |
| 158 , fReleaseProcContext(context) | 158 , fReleaseProcContext(context) |
| 159 { | 159 { |
| 160 SkASSERT(is_valid(info, ctable)); | 160 SkASSERT(is_valid(info, ctable)); |
| 161 SkASSERT(rowBytes >= info.minRowBytes()); | 161 SkASSERT(rowBytes >= info.minRowBytes()); |
| 162 | 162 |
| 163 if (kIndex_8_SkColorType != info.colorType()) { | 163 if (kIndex_8_SkColorType != info.colorType()) { |
| 164 ctable = NULL; | 164 ctable = nullptr; |
| 165 } | 165 } |
| 166 | 166 |
| 167 fStorage = storage; | 167 fStorage = storage; |
| 168 fCTable = ctable; | 168 fCTable = ctable; |
| 169 fRB = rowBytes; | 169 fRB = rowBytes; |
| 170 SkSafeRef(ctable); | 170 SkSafeRef(ctable); |
| 171 | 171 |
| 172 this->setPreLocked(fStorage, rowBytes, fCTable); | 172 this->setPreLocked(fStorage, rowBytes, fCTable); |
| 173 } | 173 } |
| 174 | 174 |
| 175 | 175 |
| 176 SkMallocPixelRef::~SkMallocPixelRef() { | 176 SkMallocPixelRef::~SkMallocPixelRef() { |
| 177 SkSafeUnref(fCTable); | 177 SkSafeUnref(fCTable); |
| 178 if (fReleaseProc != NULL) { | 178 if (fReleaseProc != nullptr) { |
| 179 fReleaseProc(fStorage, fReleaseProcContext); | 179 fReleaseProc(fStorage, fReleaseProcContext); |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 | 182 |
| 183 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) { | 183 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) { |
| 184 rec->fPixels = fStorage; | 184 rec->fPixels = fStorage; |
| 185 rec->fRowBytes = fRB; | 185 rec->fRowBytes = fRB; |
| 186 rec->fColorTable = fCTable; | 186 rec->fColorTable = fCTable; |
| 187 return true; | 187 return true; |
| 188 } | 188 } |
| 189 | 189 |
| 190 void SkMallocPixelRef::onUnlockPixels() { | 190 void SkMallocPixelRef::onUnlockPixels() { |
| 191 // nothing to do | 191 // nothing to do |
| 192 } | 192 } |
| 193 | 193 |
| 194 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { | 194 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const { |
| 195 return this->info().getSafeSize(fRB); | 195 return this->info().getSafeSize(fRB); |
| 196 } | 196 } |
| 197 | 197 |
| 198 /////////////////////////////////////////////////////////////////////////////// | 198 /////////////////////////////////////////////////////////////////////////////// |
| 199 | 199 |
| 200 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t
rowBytes, | 200 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t
rowBytes, |
| 201 SkColorTable* ctable) { | 201 SkColorTable* ctable) { |
| 202 return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable); | 202 return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable); |
| 203 } | 203 } |
| OLD | NEW |