Sat, 03 Jan 2015 20:18:00 +0100
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 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | #include "SkImageRef.h" |
michael@0 | 9 | #include "SkBitmap.h" |
michael@0 | 10 | #include "SkReadBuffer.h" |
michael@0 | 11 | #include "SkWriteBuffer.h" |
michael@0 | 12 | #include "SkImageDecoder.h" |
michael@0 | 13 | #include "SkStream.h" |
michael@0 | 14 | #include "SkTemplates.h" |
michael@0 | 15 | #include "SkThread.h" |
michael@0 | 16 | |
michael@0 | 17 | //#define DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 18 | |
michael@0 | 19 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 20 | |
michael@0 | 21 | SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream, |
michael@0 | 22 | int sampleSize, SkBaseMutex* mutex) |
michael@0 | 23 | : INHERITED(info, mutex), fErrorInDecoding(false) |
michael@0 | 24 | { |
michael@0 | 25 | SkASSERT(stream); |
michael@0 | 26 | stream->ref(); |
michael@0 | 27 | fStream = stream; |
michael@0 | 28 | fSampleSize = sampleSize; |
michael@0 | 29 | fDoDither = true; |
michael@0 | 30 | fPrev = fNext = NULL; |
michael@0 | 31 | fFactory = NULL; |
michael@0 | 32 | |
michael@0 | 33 | // This sets the colortype/alphatype to exactly match our info, so that this |
michael@0 | 34 | // can get communicated down to the codec. |
michael@0 | 35 | fBitmap.setConfig(info); |
michael@0 | 36 | |
michael@0 | 37 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 38 | SkDebugf("add ImageRef %p [%d] data=%d\n", |
michael@0 | 39 | this, this->info().fColorType, (int)stream->getLength()); |
michael@0 | 40 | #endif |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | SkImageRef::~SkImageRef() { |
michael@0 | 44 | |
michael@0 | 45 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 46 | SkDebugf("delete ImageRef %p [%d] data=%d\n", |
michael@0 | 47 | this, this->info().fColorType, (int)fStream->getLength()); |
michael@0 | 48 | #endif |
michael@0 | 49 | |
michael@0 | 50 | fStream->unref(); |
michael@0 | 51 | SkSafeUnref(fFactory); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | bool SkImageRef::getInfo(SkBitmap* bitmap) { |
michael@0 | 55 | SkAutoMutexAcquire ac(this->mutex()); |
michael@0 | 56 | |
michael@0 | 57 | if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) { |
michael@0 | 58 | return false; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | SkASSERT(SkBitmap::kNo_Config != fBitmap.config()); |
michael@0 | 62 | if (bitmap) { |
michael@0 | 63 | bitmap->setConfig(fBitmap.config(), fBitmap.width(), fBitmap.height()); |
michael@0 | 64 | } |
michael@0 | 65 | return true; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | bool SkImageRef::isOpaque(SkBitmap* bitmap) { |
michael@0 | 69 | if (bitmap && bitmap->pixelRef() == this) { |
michael@0 | 70 | bitmap->lockPixels(); |
michael@0 | 71 | // what about colortables?????? |
michael@0 | 72 | bitmap->setAlphaType(fBitmap.alphaType()); |
michael@0 | 73 | bitmap->unlockPixels(); |
michael@0 | 74 | return true; |
michael@0 | 75 | } |
michael@0 | 76 | return false; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | SkImageDecoderFactory* SkImageRef::setDecoderFactory( |
michael@0 | 80 | SkImageDecoderFactory* fact) { |
michael@0 | 81 | SkRefCnt_SafeAssign(fFactory, fact); |
michael@0 | 82 | return fact; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 86 | |
michael@0 | 87 | bool SkImageRef::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, |
michael@0 | 88 | SkBitmap* bitmap, SkBitmap::Config config, |
michael@0 | 89 | SkImageDecoder::Mode mode) { |
michael@0 | 90 | return codec->decode(stream, bitmap, config, mode); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { |
michael@0 | 94 | |
michael@0 | 95 | if (fErrorInDecoding) { |
michael@0 | 96 | return false; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | if (NULL != fBitmap.getPixels() || |
michael@0 | 100 | (SkBitmap::kNo_Config != fBitmap.config() && |
michael@0 | 101 | SkImageDecoder::kDecodeBounds_Mode == mode)) { |
michael@0 | 102 | return true; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | SkASSERT(fBitmap.getPixels() == NULL); |
michael@0 | 106 | |
michael@0 | 107 | if (!fStream->rewind()) { |
michael@0 | 108 | SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | SkImageDecoder* codec; |
michael@0 | 113 | if (fFactory) { |
michael@0 | 114 | codec = fFactory->newDecoder(fStream); |
michael@0 | 115 | } else { |
michael@0 | 116 | codec = SkImageDecoder::Factory(fStream); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | if (codec) { |
michael@0 | 120 | SkAutoTDelete<SkImageDecoder> ad(codec); |
michael@0 | 121 | |
michael@0 | 122 | codec->setSampleSize(fSampleSize); |
michael@0 | 123 | codec->setDitherImage(fDoDither); |
michael@0 | 124 | codec->setRequireUnpremultipliedColors(this->info().fAlphaType == kUnpremul_SkAlphaType); |
michael@0 | 125 | if (this->onDecode(codec, fStream, &fBitmap, fBitmap.config(), mode)) { |
michael@0 | 126 | if (kOpaque_SkAlphaType == fBitmap.alphaType()) { |
michael@0 | 127 | this->changeAlphaType(kOpaque_SkAlphaType); |
michael@0 | 128 | } |
michael@0 | 129 | SkASSERT(this->info() == fBitmap.info()); |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 135 | if (NULL == codec) { |
michael@0 | 136 | SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); |
michael@0 | 137 | } else { |
michael@0 | 138 | SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", |
michael@0 | 139 | this->getURI(), mode); |
michael@0 | 140 | } |
michael@0 | 141 | #endif |
michael@0 | 142 | fErrorInDecoding = true; |
michael@0 | 143 | fBitmap.reset(); |
michael@0 | 144 | return false; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | bool SkImageRef::onNewLockPixels(LockRec* rec) { |
michael@0 | 148 | if (NULL == fBitmap.getPixels()) { |
michael@0 | 149 | (void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | if (NULL == fBitmap.getPixels()) { |
michael@0 | 153 | return false; |
michael@0 | 154 | } |
michael@0 | 155 | rec->fPixels = fBitmap.getPixels(); |
michael@0 | 156 | rec->fColorTable = NULL; |
michael@0 | 157 | rec->fRowBytes = fBitmap.rowBytes(); |
michael@0 | 158 | return true; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | size_t SkImageRef::ramUsed() const { |
michael@0 | 162 | size_t size = 0; |
michael@0 | 163 | |
michael@0 | 164 | if (fBitmap.getPixels()) { |
michael@0 | 165 | size = fBitmap.getSize(); |
michael@0 | 166 | if (fBitmap.getColorTable()) { |
michael@0 | 167 | size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | return size; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 174 | |
michael@0 | 175 | SkImageRef::SkImageRef(SkReadBuffer& buffer, SkBaseMutex* mutex) |
michael@0 | 176 | : INHERITED(buffer, mutex), fErrorInDecoding(false) { |
michael@0 | 177 | fSampleSize = buffer.readInt(); |
michael@0 | 178 | fDoDither = buffer.readBool(); |
michael@0 | 179 | |
michael@0 | 180 | size_t length = buffer.getArrayCount(); |
michael@0 | 181 | if (buffer.validateAvailable(length)) { |
michael@0 | 182 | fStream = SkNEW_ARGS(SkMemoryStream, (length)); |
michael@0 | 183 | buffer.readByteArray((void*)fStream->getMemoryBase(), length); |
michael@0 | 184 | } else { |
michael@0 | 185 | fStream = NULL; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | fPrev = fNext = NULL; |
michael@0 | 189 | fFactory = NULL; |
michael@0 | 190 | |
michael@0 | 191 | // This sets the colortype/alphatype to exactly match our info, so that this |
michael@0 | 192 | // can get communicated down to the codec. |
michael@0 | 193 | fBitmap.setConfig(this->info()); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | void SkImageRef::flatten(SkWriteBuffer& buffer) const { |
michael@0 | 197 | this->INHERITED::flatten(buffer); |
michael@0 | 198 | |
michael@0 | 199 | buffer.writeInt(fSampleSize); |
michael@0 | 200 | buffer.writeBool(fDoDither); |
michael@0 | 201 | // FIXME: Consider moving this logic should go into writeStream itself. |
michael@0 | 202 | // writeStream currently has no other callers, so this may be fine for |
michael@0 | 203 | // now. |
michael@0 | 204 | if (!fStream->rewind()) { |
michael@0 | 205 | SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
michael@0 | 206 | buffer.write32(0); |
michael@0 | 207 | } else { |
michael@0 | 208 | // FIXME: Handle getLength properly here. Perhaps this class should |
michael@0 | 209 | // take an SkStreamAsset. |
michael@0 | 210 | buffer.writeStream(fStream, fStream->getLength()); |
michael@0 | 211 | } |
michael@0 | 212 | } |