Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 4 * | 3 * |
| 5 * 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 |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 7 | |
| 8 #include "SkMallocPixelRef.h" | 8 #include "SkMallocPixelRef.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
| 11 | 11 |
| 12 SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size, | 12 static bool check_info(const SkImageInfo& info, SkColorTable* ctable) { |
| 13 SkColorTable* ctable, bool ownPixels) { | 13 if (!info.isValid()) { |
| 14 if (NULL == storage) { | 14 return false; |
| 15 SkASSERT(ownPixels); | |
| 16 storage = sk_malloc_throw(size); | |
| 17 } | 15 } |
| 16 | |
| 17 // these seem like good checks, but currently we have (at least) tests | |
| 18 // that expect the pixelref to succeed even when there is a mismatch | |
| 19 // with colortables. fix? | |
| 20 #if 0 | |
| 21 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) { | |
| 22 return false; | |
| 23 } | |
| 24 if (kIndex8_SkColorType != info.fColorType && NULL != ctable) { | |
| 25 return false; | |
| 26 } | |
| 27 #endif | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 SkMallocPixelRef* SkMallocPixelRef::Create(const SkImageInfo& info, void* addr, | |
| 32 size_t rowBytes, SkColorTable* ctable ) { | |
| 33 if (!check_info(info, ctable)) { | |
| 34 return NULL; | |
| 35 } | |
| 36 return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, false)); | |
| 37 } | |
| 38 | |
| 39 SkMallocPixelRef* SkMallocPixelRef::Allocate(const SkImageInfo& info, | |
| 40 size_t requestedRowBytes, | |
| 41 SkColorTable* ctable) { | |
| 42 if (!check_info(info, ctable)) { | |
| 43 return NULL; | |
| 44 } | |
| 45 | |
| 46 int32_t minRB = info.minRowBytes(); | |
| 47 if (minRB < 0) { | |
| 48 return NULL; // allocation will be too large | |
| 49 } | |
| 50 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) { | |
| 51 return NULL; // cannot meet requested rowbytes | |
| 52 } | |
| 53 | |
| 54 int32_t rowBytes; | |
| 55 if (requestedRowBytes) { | |
| 56 rowBytes = requestedRowBytes; | |
| 57 } else { | |
| 58 rowBytes = minRB; | |
| 59 } | |
| 60 | |
| 61 Sk64 bigSize; | |
| 62 bigSize.setMul(info.fHeight, rowBytes); | |
| 63 if (!bigSize.is32()) { | |
| 64 return NULL; | |
| 65 } | |
| 66 | |
| 67 size_t size = bigSize.get32(); | |
| 68 void* addr = sk_malloc_flags(size, 0); | |
| 69 if (NULL == addr) { | |
| 70 return NULL; | |
| 71 } | |
| 72 | |
| 73 return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, true)); | |
| 74 } | |
| 75 | |
| 76 /////////////////////////////////////////////////////////////////////////////// | |
| 77 | |
| 78 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage, | |
| 79 size_t rowBytes, SkColorTable* ctable, | |
| 80 bool ownsPixels) | |
| 81 : fOwnPixels(ownsPixels) | |
| 82 { | |
|
scroggo
2013/11/20 21:04:28
Brace should be on previous line.
reed1
2013/11/20 21:27:18
I am increasingly finding that much harder to read
scroggo
2013/11/20 21:35:19
Agreed! I think we should update the style guide i
| |
| 83 SkASSERT(check_info(info, ctable)); | |
| 84 SkASSERT(rowBytes >= info.minRowBytes()); | |
| 85 | |
| 86 if (kIndex8_SkColorType != info.fColorType) { | |
| 87 ctable = NULL; | |
| 88 } | |
| 89 | |
| 18 fStorage = storage; | 90 fStorage = storage; |
| 19 fSize = size; | |
| 20 fCTable = ctable; | 91 fCTable = ctable; |
| 92 fRB = rowBytes; | |
| 93 fInfo = info; | |
| 21 SkSafeRef(ctable); | 94 SkSafeRef(ctable); |
| 22 fOwnPixels = ownPixels; | 95 |
| 23 | 96 this->setPreLocked(fStorage, fRB, fCTable); |
| 24 this->setPreLocked(fStorage, fCTable); | |
| 25 } | 97 } |
| 26 | 98 |
| 27 SkMallocPixelRef::~SkMallocPixelRef() { | 99 SkMallocPixelRef::~SkMallocPixelRef() { |
| 28 SkSafeUnref(fCTable); | 100 SkSafeUnref(fCTable); |
| 29 if (fOwnPixels) { | 101 if (fOwnPixels) { |
| 30 sk_free(fStorage); | 102 sk_free(fStorage); |
| 31 } | 103 } |
| 32 } | 104 } |
| 33 | 105 |
| 34 void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) { | 106 void* SkMallocPixelRef::onLockPixels(SkImageInfo* info, size_t* rowBytes, |
| 107 SkColorTable** ct) { | |
| 108 *info = fInfo; | |
| 109 *rowBytes = fRB; | |
| 35 *ct = fCTable; | 110 *ct = fCTable; |
| 36 return fStorage; | 111 return fStorage; |
| 37 } | 112 } |
| 38 | 113 |
| 39 void SkMallocPixelRef::onUnlockPixels() { | 114 void SkMallocPixelRef::onUnlockPixels() { |
| 40 // nothing to do | 115 // nothing to do |
| 41 } | 116 } |
| 42 | 117 |
| 43 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 118 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 44 this->INHERITED::flatten(buffer); | 119 this->INHERITED::flatten(buffer); |
| 45 | 120 |
| 46 buffer.writeByteArray(fStorage, fSize); | 121 buffer.write32(fInfo.fWidth); |
| 122 buffer.write32(fInfo.fHeight); | |
| 123 buffer.write32(fInfo.fAlphaType); | |
| 124 buffer.write32(fInfo.fColorType); | |
| 125 buffer.write32(fRB); | |
| 126 | |
| 127 // TODO: replace this bulk write with a chunky one that can trim off any | |
| 128 // trailing bytes on each scanline (in case rowbytes > width*size) | |
| 129 size_t size = this->computeMinSize(); | |
| 130 buffer.writeByteArray(fStorage, size); | |
| 47 buffer.writeBool(fCTable != NULL); | 131 buffer.writeBool(fCTable != NULL); |
| 48 if (fCTable) { | 132 if (fCTable) { |
| 49 fCTable->writeToBuffer(buffer); | 133 fCTable->writeToBuffer(buffer); |
| 50 } | 134 } |
| 51 } | 135 } |
| 52 | 136 |
| 53 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) | 137 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) |
| 54 : INHERITED(buffer, NULL) { | 138 : INHERITED(buffer, NULL) |
| 55 fSize = buffer.getArrayCount(); | 139 , fOwnPixels(true) |
| 56 fStorage = sk_malloc_throw(fSize); | 140 { |
|
scroggo
2013/11/20 21:04:28
brace should be on previous line.
| |
| 57 buffer.readByteArray(fStorage, fSize); | 141 fInfo.fWidth = buffer.read32(); |
| 142 fInfo.fHeight = buffer.read32(); | |
| 143 fInfo.fAlphaType = (SkAlphaType)buffer.read32(); | |
| 144 fInfo.fColorType = (SkColorType)buffer.read32(); | |
| 145 | |
| 146 fRB = buffer.read32(); | |
| 147 size_t size = this->computeMinSize(); | |
| 148 fStorage = sk_malloc_throw(size); | |
| 149 buffer.readByteArray(fStorage, size); | |
| 58 if (buffer.readBool()) { | 150 if (buffer.readBool()) { |
| 59 fCTable = SkNEW_ARGS(SkColorTable, (buffer)); | 151 fCTable = SkNEW_ARGS(SkColorTable, (buffer)); |
| 60 } else { | 152 } else { |
| 61 fCTable = NULL; | 153 fCTable = NULL; |
| 62 } | 154 } |
| 63 fOwnPixels = true; | |
| 64 | 155 |
| 65 this->setPreLocked(fStorage, fCTable); | 156 this->setPreLocked(fStorage, fRB, fCTable); |
| 66 } | 157 } |
| OLD | NEW |