2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkMallocPixelRef.h"
10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h"
13 // assumes ptr was allocated via sk_malloc
14 static void sk_free_releaseproc(void* ptr, void*) {
18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) {
19 if (info.width() < 0 || info.height() < 0 ||
20 (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType ||
21 (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType)
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
28 // with colortables. fix?
30 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) {
33 if (kIndex8_SkColorType != info.fColorType && ctable) {
40 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info,
43 SkColorTable* ctable) {
44 if (!is_valid(info, ctable)) {
47 return SkNEW_ARGS(SkMallocPixelRef,
48 (info, addr, rowBytes, ctable, NULL, NULL));
52 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info,
53 size_t requestedRowBytes,
54 SkColorTable* ctable) {
55 if (!is_valid(info, ctable)) {
59 int32_t minRB = SkToS32(info.minRowBytes());
61 return NULL; // allocation will be too large
63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) {
64 return NULL; // cannot meet requested rowbytes
68 if (requestedRowBytes) {
69 rowBytes = SkToS32(requestedRowBytes);
74 int64_t bigSize = (int64_t)info.height() * rowBytes;
75 if (!sk_64_isS32(bigSize)) {
79 size_t size = sk_64_asS32(bigSize);
80 SkASSERT(size >= info.getSafeSize(rowBytes));
81 void* addr = sk_malloc_flags(size, 0);
86 return SkNEW_ARGS(SkMallocPixelRef,
87 (info, addr, rowBytes, ctable,
88 sk_free_releaseproc, NULL));
91 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info,
95 SkMallocPixelRef::ReleaseProc proc,
97 if (!is_valid(info, ctable)) {
100 return SkNEW_ARGS(SkMallocPixelRef,
101 (info, addr, rowBytes, ctable, proc, context));
104 static void sk_data_releaseproc(void*, void* dataPtr) {
105 (static_cast<SkData*>(dataPtr))->unref();
108 SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info,
110 SkColorTable* ctable,
112 SkASSERT(data != NULL);
113 if (!is_valid(info, ctable)) {
116 if ((rowBytes < info.minRowBytes())
117 || (data->size() < info.getSafeSize(rowBytes))) {
122 = SkNEW_ARGS(SkMallocPixelRef,
123 (info, const_cast<void*>(data->data()), rowBytes, ctable,
124 sk_data_releaseproc, static_cast<void*>(data)));
125 SkASSERT(pr != NULL);
126 // We rely on the immutability of the pixels to make the
132 ///////////////////////////////////////////////////////////////////////////////
134 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
135 size_t rowBytes, SkColorTable* ctable,
138 , fReleaseProc(ownsPixels ? sk_free_releaseproc : NULL)
139 , fReleaseProcContext(NULL) {
140 // This constructor is now DEPRICATED.
141 SkASSERT(is_valid(info, ctable));
142 SkASSERT(rowBytes >= info.minRowBytes());
144 if (kIndex_8_SkColorType != info.colorType()) {
153 this->setPreLocked(fStorage, rowBytes, fCTable);
156 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
157 size_t rowBytes, SkColorTable* ctable,
158 SkMallocPixelRef::ReleaseProc proc,
162 , fReleaseProcContext(context)
164 SkASSERT(is_valid(info, ctable));
165 SkASSERT(rowBytes >= info.minRowBytes());
167 if (kIndex_8_SkColorType != info.colorType()) {
176 this->setPreLocked(fStorage, rowBytes, fCTable);
180 SkMallocPixelRef::~SkMallocPixelRef() {
181 SkSafeUnref(fCTable);
182 if (fReleaseProc != NULL) {
183 fReleaseProc(fStorage, fReleaseProcContext);
187 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
188 rec->fPixels = fStorage;
189 rec->fRowBytes = fRB;
190 rec->fColorTable = fCTable;
194 void SkMallocPixelRef::onUnlockPixels() {
198 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const {
199 return this->info().getSafeSize(fRB);
202 ///////////////////////////////////////////////////////////////////////////////
204 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info, size_t rowBytes,
205 SkColorTable* ctable) {
206 return SkMallocPixelRef::NewAllocate(info, rowBytes, ctable);