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 | * 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 "SkImageRef_ashmem.h" |
michael@0 | 9 | #include "SkImageDecoder.h" |
michael@0 | 10 | #include "SkReadBuffer.h" |
michael@0 | 11 | #include "SkWriteBuffer.h" |
michael@0 | 12 | #include "SkThread.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "android/ashmem.h" |
michael@0 | 15 | |
michael@0 | 16 | #include <sys/mman.h> |
michael@0 | 17 | #include <unistd.h> |
michael@0 | 18 | |
michael@0 | 19 | //#define TRACE_ASH_PURGE // just trace purges |
michael@0 | 20 | |
michael@0 | 21 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 22 | #define DUMP_ASHMEM_LIFECYCLE |
michael@0 | 23 | #else |
michael@0 | 24 | // #define DUMP_ASHMEM_LIFECYCLE |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | // ashmem likes lengths on page boundaries |
michael@0 | 28 | static size_t roundToPageSize(size_t size) { |
michael@0 | 29 | const size_t mask = getpagesize() - 1; |
michael@0 | 30 | size_t newsize = (size + mask) & ~mask; |
michael@0 | 31 | // SkDebugf("---- oldsize %d newsize %d\n", size, newsize); |
michael@0 | 32 | return newsize; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | SkImageRef_ashmem::SkImageRef_ashmem(const SkImageInfo& info, |
michael@0 | 36 | SkStreamRewindable* stream, |
michael@0 | 37 | int sampleSize) |
michael@0 | 38 | : SkImageRef(info, stream, sampleSize) |
michael@0 | 39 | { |
michael@0 | 40 | fRec.fFD = -1; |
michael@0 | 41 | fRec.fAddr = NULL; |
michael@0 | 42 | fRec.fSize = 0; |
michael@0 | 43 | fRec.fPinned = false; |
michael@0 | 44 | |
michael@0 | 45 | fCT = NULL; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | SkImageRef_ashmem::~SkImageRef_ashmem() { |
michael@0 | 49 | SkSafeUnref(fCT); |
michael@0 | 50 | this->closeFD(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | void SkImageRef_ashmem::closeFD() { |
michael@0 | 54 | if (-1 != fRec.fFD) { |
michael@0 | 55 | #ifdef DUMP_ASHMEM_LIFECYCLE |
michael@0 | 56 | SkDebugf("=== ashmem close %d\n", fRec.fFD); |
michael@0 | 57 | #endif |
michael@0 | 58 | SkASSERT(fRec.fAddr); |
michael@0 | 59 | SkASSERT(fRec.fSize); |
michael@0 | 60 | munmap(fRec.fAddr, fRec.fSize); |
michael@0 | 61 | close(fRec.fFD); |
michael@0 | 62 | fRec.fFD = -1; |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 67 | |
michael@0 | 68 | class AshmemAllocator : public SkBitmap::Allocator { |
michael@0 | 69 | public: |
michael@0 | 70 | AshmemAllocator(SkAshmemRec* rec, const char name[]) |
michael@0 | 71 | : fRec(rec), fName(name) {} |
michael@0 | 72 | |
michael@0 | 73 | virtual bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) { |
michael@0 | 74 | const size_t size = roundToPageSize(bm->getSize()); |
michael@0 | 75 | int fd = fRec->fFD; |
michael@0 | 76 | void* addr = fRec->fAddr; |
michael@0 | 77 | |
michael@0 | 78 | SkASSERT(!fRec->fPinned); |
michael@0 | 79 | |
michael@0 | 80 | if (-1 == fd) { |
michael@0 | 81 | SkASSERT(NULL == addr); |
michael@0 | 82 | SkASSERT(0 == fRec->fSize); |
michael@0 | 83 | |
michael@0 | 84 | fd = ashmem_create_region(fName, size); |
michael@0 | 85 | #ifdef DUMP_ASHMEM_LIFECYCLE |
michael@0 | 86 | SkDebugf("=== ashmem_create_region %s size=%d fd=%d\n", fName, size, fd); |
michael@0 | 87 | #endif |
michael@0 | 88 | if (-1 == fd) { |
michael@0 | 89 | SkDebugf("------- imageref_ashmem create failed <%s> %d\n", |
michael@0 | 90 | fName, size); |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); |
michael@0 | 95 | if (err) { |
michael@0 | 96 | SkDebugf("------ ashmem_set_prot_region(%d) failed %d\n", |
michael@0 | 97 | fd, err); |
michael@0 | 98 | close(fd); |
michael@0 | 99 | return false; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
michael@0 | 103 | if (-1 == (long)addr) { |
michael@0 | 104 | SkDebugf("---------- mmap failed for imageref_ashmem size=%d\n", |
michael@0 | 105 | size); |
michael@0 | 106 | close(fd); |
michael@0 | 107 | return false; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | fRec->fFD = fd; |
michael@0 | 111 | fRec->fAddr = addr; |
michael@0 | 112 | fRec->fSize = size; |
michael@0 | 113 | } else { |
michael@0 | 114 | SkASSERT(addr); |
michael@0 | 115 | SkASSERT(size == fRec->fSize); |
michael@0 | 116 | (void)ashmem_pin_region(fd, 0, 0); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | bm->setPixels(addr, ct); |
michael@0 | 120 | fRec->fPinned = true; |
michael@0 | 121 | return true; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | private: |
michael@0 | 125 | // we just point to our caller's memory, these are not copies |
michael@0 | 126 | SkAshmemRec* fRec; |
michael@0 | 127 | const char* fName; |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | bool SkImageRef_ashmem::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, |
michael@0 | 131 | SkBitmap* bitmap, SkBitmap::Config config, |
michael@0 | 132 | SkImageDecoder::Mode mode) { |
michael@0 | 133 | |
michael@0 | 134 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
michael@0 | 135 | return this->INHERITED::onDecode(codec, stream, bitmap, config, mode); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // Ashmem memory is guaranteed to be initialized to 0. |
michael@0 | 139 | codec->setSkipWritingZeroes(true); |
michael@0 | 140 | |
michael@0 | 141 | AshmemAllocator alloc(&fRec, this->getURI()); |
michael@0 | 142 | |
michael@0 | 143 | codec->setAllocator(&alloc); |
michael@0 | 144 | bool success = this->INHERITED::onDecode(codec, stream, bitmap, config, |
michael@0 | 145 | mode); |
michael@0 | 146 | // remove the allocator, since its on the stack |
michael@0 | 147 | codec->setAllocator(NULL); |
michael@0 | 148 | |
michael@0 | 149 | if (success) { |
michael@0 | 150 | // remember the colortable (if any) |
michael@0 | 151 | SkRefCnt_SafeAssign(fCT, bitmap->getColorTable()); |
michael@0 | 152 | return true; |
michael@0 | 153 | } else { |
michael@0 | 154 | if (fRec.fPinned) { |
michael@0 | 155 | ashmem_unpin_region(fRec.fFD, 0, 0); |
michael@0 | 156 | fRec.fPinned = false; |
michael@0 | 157 | } |
michael@0 | 158 | this->closeFD(); |
michael@0 | 159 | return false; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | bool SkImageRef_ashmem::onNewLockPixels(LockRec* rec) { |
michael@0 | 164 | SkASSERT(fBitmap.getPixels() == NULL); |
michael@0 | 165 | SkASSERT(fBitmap.getColorTable() == NULL); |
michael@0 | 166 | |
michael@0 | 167 | // fast case: check if we can just pin and get the cached data |
michael@0 | 168 | if (-1 != fRec.fFD) { |
michael@0 | 169 | SkASSERT(fRec.fAddr); |
michael@0 | 170 | SkASSERT(!fRec.fPinned); |
michael@0 | 171 | int pin = ashmem_pin_region(fRec.fFD, 0, 0); |
michael@0 | 172 | |
michael@0 | 173 | if (ASHMEM_NOT_PURGED == pin) { // yea, fast case! |
michael@0 | 174 | fBitmap.setPixels(fRec.fAddr, fCT); |
michael@0 | 175 | fRec.fPinned = true; |
michael@0 | 176 | } else if (ASHMEM_WAS_PURGED == pin) { |
michael@0 | 177 | ashmem_unpin_region(fRec.fFD, 0, 0); |
michael@0 | 178 | // let go of our colortable if we lost the pixels. Well get it back |
michael@0 | 179 | // again when we re-decode |
michael@0 | 180 | if (fCT) { |
michael@0 | 181 | fCT->unref(); |
michael@0 | 182 | fCT = NULL; |
michael@0 | 183 | } |
michael@0 | 184 | #if defined(DUMP_ASHMEM_LIFECYCLE) || defined(TRACE_ASH_PURGE) |
michael@0 | 185 | SkDebugf("===== ashmem purged %d\n", fBitmap.getSize()); |
michael@0 | 186 | #endif |
michael@0 | 187 | } else { |
michael@0 | 188 | SkDebugf("===== ashmem pin_region(%d) returned %d\n", fRec.fFD, pin); |
michael@0 | 189 | return false; |
michael@0 | 190 | } |
michael@0 | 191 | } else { |
michael@0 | 192 | // no FD, will create an ashmem region in allocator |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | return this->INHERITED::onNewLockPixels(rec); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | void SkImageRef_ashmem::onUnlockPixels() { |
michael@0 | 199 | this->INHERITED::onUnlockPixels(); |
michael@0 | 200 | |
michael@0 | 201 | if (-1 != fRec.fFD) { |
michael@0 | 202 | SkASSERT(fRec.fAddr); |
michael@0 | 203 | SkASSERT(fRec.fPinned); |
michael@0 | 204 | |
michael@0 | 205 | ashmem_unpin_region(fRec.fFD, 0, 0); |
michael@0 | 206 | fRec.fPinned = false; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | // we clear this with or without an error, since we've either closed or |
michael@0 | 210 | // unpinned the region |
michael@0 | 211 | fBitmap.setPixels(NULL, NULL); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | void SkImageRef_ashmem::flatten(SkWriteBuffer& buffer) const { |
michael@0 | 215 | this->INHERITED::flatten(buffer); |
michael@0 | 216 | buffer.writeString(getURI()); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | SkImageRef_ashmem::SkImageRef_ashmem(SkReadBuffer& buffer) |
michael@0 | 220 | : INHERITED(buffer) { |
michael@0 | 221 | fRec.fFD = -1; |
michael@0 | 222 | fRec.fAddr = NULL; |
michael@0 | 223 | fRec.fSize = 0; |
michael@0 | 224 | fRec.fPinned = false; |
michael@0 | 225 | fCT = NULL; |
michael@0 | 226 | |
michael@0 | 227 | SkString uri; |
michael@0 | 228 | buffer.readString(&uri); |
michael@0 | 229 | this->setURI(uri); |
michael@0 | 230 | } |