gfx/skia/trunk/src/core/SkMallocPixelRef.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2011 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkMallocPixelRef.h"
michael@0 9 #include "SkBitmap.h"
michael@0 10 #include "SkReadBuffer.h"
michael@0 11 #include "SkWriteBuffer.h"
michael@0 12
michael@0 13 // assumes ptr was allocated via sk_malloc
michael@0 14 static void sk_free_releaseproc(void* ptr, void*) {
michael@0 15 sk_free(ptr);
michael@0 16 }
michael@0 17
michael@0 18 static bool is_valid(const SkImageInfo& info, SkColorTable* ctable) {
michael@0 19 if (info.fWidth < 0 ||
michael@0 20 info.fHeight < 0 ||
michael@0 21 (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType ||
michael@0 22 (unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType)
michael@0 23 {
michael@0 24 return false;
michael@0 25 }
michael@0 26
michael@0 27 // these seem like good checks, but currently we have (at least) tests
michael@0 28 // that expect the pixelref to succeed even when there is a mismatch
michael@0 29 // with colortables. fix?
michael@0 30 #if 0
michael@0 31 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) {
michael@0 32 return false;
michael@0 33 }
michael@0 34 if (kIndex8_SkColorType != info.fColorType && NULL != ctable) {
michael@0 35 return false;
michael@0 36 }
michael@0 37 #endif
michael@0 38 return true;
michael@0 39 }
michael@0 40
michael@0 41 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info,
michael@0 42 void* addr,
michael@0 43 size_t rowBytes,
michael@0 44 SkColorTable* ctable) {
michael@0 45 if (!is_valid(info, ctable)) {
michael@0 46 return NULL;
michael@0 47 }
michael@0 48 return SkNEW_ARGS(SkMallocPixelRef,
michael@0 49 (info, addr, rowBytes, ctable, NULL, NULL));
michael@0 50 }
michael@0 51
michael@0 52 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info,
michael@0 53 size_t requestedRowBytes,
michael@0 54 SkColorTable* ctable) {
michael@0 55 if (!is_valid(info, ctable)) {
michael@0 56 return NULL;
michael@0 57 }
michael@0 58
michael@0 59 int32_t minRB = SkToS32(info.minRowBytes());
michael@0 60 if (minRB < 0) {
michael@0 61 return NULL; // allocation will be too large
michael@0 62 }
michael@0 63 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) {
michael@0 64 return NULL; // cannot meet requested rowbytes
michael@0 65 }
michael@0 66
michael@0 67 int32_t rowBytes;
michael@0 68 if (requestedRowBytes) {
michael@0 69 rowBytes = SkToS32(requestedRowBytes);
michael@0 70 } else {
michael@0 71 rowBytes = minRB;
michael@0 72 }
michael@0 73
michael@0 74 int64_t bigSize = (int64_t)info.fHeight * rowBytes;
michael@0 75 if (!sk_64_isS32(bigSize)) {
michael@0 76 return NULL;
michael@0 77 }
michael@0 78
michael@0 79 size_t size = sk_64_asS32(bigSize);
michael@0 80 SkASSERT(size >= info.getSafeSize(rowBytes));
michael@0 81 void* addr = sk_malloc_flags(size, 0);
michael@0 82 if (NULL == addr) {
michael@0 83 return NULL;
michael@0 84 }
michael@0 85
michael@0 86 return SkNEW_ARGS(SkMallocPixelRef,
michael@0 87 (info, addr, rowBytes, ctable,
michael@0 88 sk_free_releaseproc, NULL));
michael@0 89 }
michael@0 90
michael@0 91 SkMallocPixelRef* SkMallocPixelRef::NewWithProc(const SkImageInfo& info,
michael@0 92 size_t rowBytes,
michael@0 93 SkColorTable* ctable,
michael@0 94 void* addr,
michael@0 95 SkMallocPixelRef::ReleaseProc proc,
michael@0 96 void* context) {
michael@0 97 if (!is_valid(info, ctable)) {
michael@0 98 return NULL;
michael@0 99 }
michael@0 100 return SkNEW_ARGS(SkMallocPixelRef,
michael@0 101 (info, addr, rowBytes, ctable, proc, context));
michael@0 102 }
michael@0 103
michael@0 104 static void sk_data_releaseproc(void*, void* dataPtr) {
michael@0 105 (static_cast<SkData*>(dataPtr))->unref();
michael@0 106 }
michael@0 107
michael@0 108 SkMallocPixelRef* SkMallocPixelRef::NewWithData(const SkImageInfo& info,
michael@0 109 size_t rowBytes,
michael@0 110 SkColorTable* ctable,
michael@0 111 SkData* data,
michael@0 112 size_t offset) {
michael@0 113 SkASSERT(data != NULL);
michael@0 114 SkASSERT(offset <= data->size());
michael@0 115 if (!is_valid(info, ctable)) {
michael@0 116 return NULL;
michael@0 117 }
michael@0 118 if ((rowBytes < info.minRowBytes())
michael@0 119 || ((data->size() - offset) < info.getSafeSize(rowBytes))) {
michael@0 120 return NULL;
michael@0 121 }
michael@0 122 data->ref();
michael@0 123 const void* ptr = static_cast<const void*>(data->bytes() + offset);
michael@0 124 SkMallocPixelRef* pr
michael@0 125 = SkNEW_ARGS(SkMallocPixelRef,
michael@0 126 (info, const_cast<void*>(ptr), rowBytes, ctable,
michael@0 127 sk_data_releaseproc, static_cast<void*>(data)));
michael@0 128 SkASSERT(pr != NULL);
michael@0 129 // We rely on the immutability of the pixels to make the
michael@0 130 // const_cast okay.
michael@0 131 pr->setImmutable();
michael@0 132 return pr;
michael@0 133 }
michael@0 134
michael@0 135 ///////////////////////////////////////////////////////////////////////////////
michael@0 136
michael@0 137 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
michael@0 138 size_t rowBytes, SkColorTable* ctable,
michael@0 139 bool ownsPixels)
michael@0 140 : INHERITED(info)
michael@0 141 , fReleaseProc(ownsPixels ? sk_free_releaseproc : NULL)
michael@0 142 , fReleaseProcContext(NULL) {
michael@0 143 // This constructor is now DEPRICATED.
michael@0 144 SkASSERT(is_valid(info, ctable));
michael@0 145 SkASSERT(rowBytes >= info.minRowBytes());
michael@0 146
michael@0 147 if (kIndex_8_SkColorType != info.fColorType) {
michael@0 148 ctable = NULL;
michael@0 149 }
michael@0 150
michael@0 151 fStorage = storage;
michael@0 152 fCTable = ctable;
michael@0 153 fRB = rowBytes;
michael@0 154 SkSafeRef(ctable);
michael@0 155
michael@0 156 this->setPreLocked(fStorage, rowBytes, fCTable);
michael@0 157 }
michael@0 158
michael@0 159 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
michael@0 160 size_t rowBytes, SkColorTable* ctable,
michael@0 161 SkMallocPixelRef::ReleaseProc proc,
michael@0 162 void* context)
michael@0 163 : INHERITED(info)
michael@0 164 , fReleaseProc(proc)
michael@0 165 , fReleaseProcContext(context)
michael@0 166 {
michael@0 167 SkASSERT(is_valid(info, ctable));
michael@0 168 SkASSERT(rowBytes >= info.minRowBytes());
michael@0 169
michael@0 170 if (kIndex_8_SkColorType != info.fColorType) {
michael@0 171 ctable = NULL;
michael@0 172 }
michael@0 173
michael@0 174 fStorage = storage;
michael@0 175 fCTable = ctable;
michael@0 176 fRB = rowBytes;
michael@0 177 SkSafeRef(ctable);
michael@0 178
michael@0 179 this->setPreLocked(fStorage, rowBytes, fCTable);
michael@0 180 }
michael@0 181
michael@0 182
michael@0 183 SkMallocPixelRef::~SkMallocPixelRef() {
michael@0 184 SkSafeUnref(fCTable);
michael@0 185 if (fReleaseProc != NULL) {
michael@0 186 fReleaseProc(fStorage, fReleaseProcContext);
michael@0 187 }
michael@0 188 }
michael@0 189
michael@0 190 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
michael@0 191 rec->fPixels = fStorage;
michael@0 192 rec->fRowBytes = fRB;
michael@0 193 rec->fColorTable = fCTable;
michael@0 194 return true;
michael@0 195 }
michael@0 196
michael@0 197 void SkMallocPixelRef::onUnlockPixels() {
michael@0 198 // nothing to do
michael@0 199 }
michael@0 200
michael@0 201 size_t SkMallocPixelRef::getAllocatedSizeInBytes() const {
michael@0 202 return this->info().getSafeSize(fRB);
michael@0 203 }
michael@0 204
michael@0 205 void SkMallocPixelRef::flatten(SkWriteBuffer& buffer) const {
michael@0 206 this->INHERITED::flatten(buffer);
michael@0 207
michael@0 208 buffer.write32(SkToU32(fRB));
michael@0 209
michael@0 210 // TODO: replace this bulk write with a chunky one that can trim off any
michael@0 211 // trailing bytes on each scanline (in case rowbytes > width*size)
michael@0 212 size_t size = this->info().getSafeSize(fRB);
michael@0 213 buffer.writeByteArray(fStorage, size);
michael@0 214 buffer.writeBool(fCTable != NULL);
michael@0 215 if (fCTable) {
michael@0 216 fCTable->writeToBuffer(buffer);
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 SkMallocPixelRef::SkMallocPixelRef(SkReadBuffer& buffer)
michael@0 221 : INHERITED(buffer, NULL)
michael@0 222 , fReleaseProc(sk_free_releaseproc)
michael@0 223 , fReleaseProcContext(NULL)
michael@0 224 {
michael@0 225 fRB = buffer.read32();
michael@0 226 size_t size = buffer.isValid() ? this->info().getSafeSize(fRB) : 0;
michael@0 227 if (buffer.validateAvailable(size)) {
michael@0 228 fStorage = sk_malloc_throw(size);
michael@0 229 buffer.readByteArray(fStorage, size);
michael@0 230 } else {
michael@0 231 fStorage = NULL;
michael@0 232 }
michael@0 233
michael@0 234 if (buffer.readBool()) {
michael@0 235 fCTable = SkNEW_ARGS(SkColorTable, (buffer));
michael@0 236 } else {
michael@0 237 fCTable = NULL;
michael@0 238 }
michael@0 239
michael@0 240 this->setPreLocked(fStorage, fRB, fCTable);
michael@0 241 }
michael@0 242
michael@0 243 ///////////////////////////////////////////////////////////////////////////////
michael@0 244
michael@0 245 SkPixelRef* SkMallocPixelRef::PRFactory::create(const SkImageInfo& info,
michael@0 246 SkColorTable* ctable) {
michael@0 247 return SkMallocPixelRef::NewAllocate(info, info.minRowBytes(), ctable);
michael@0 248 }

mercurial