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 "SkImageRefPool.h" |
michael@0 | 9 | #include "SkImageRef.h" |
michael@0 | 10 | #include "SkThread.h" |
michael@0 | 11 | |
michael@0 | 12 | SkImageRefPool::SkImageRefPool() { |
michael@0 | 13 | fRAMBudget = 0; // means no explicit limit |
michael@0 | 14 | fRAMUsed = 0; |
michael@0 | 15 | fCount = 0; |
michael@0 | 16 | fHead = fTail = NULL; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | SkImageRefPool::~SkImageRefPool() { |
michael@0 | 20 | // SkASSERT(NULL == fHead); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | void SkImageRefPool::setRAMBudget(size_t size) { |
michael@0 | 24 | if (fRAMBudget != size) { |
michael@0 | 25 | fRAMBudget = size; |
michael@0 | 26 | this->purgeIfNeeded(); |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | void SkImageRefPool::justAddedPixels(SkImageRef* ref) { |
michael@0 | 31 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 32 | SkDebugf("=== ImagePool: add pixels %s [%d %d %d] bytes=%d heap=%d\n", |
michael@0 | 33 | ref->getURI(), |
michael@0 | 34 | ref->fBitmap.width(), ref->fBitmap.height(), |
michael@0 | 35 | ref->fBitmap.bytesPerPixel(), |
michael@0 | 36 | ref->fBitmap.getSize(), (int)fRAMUsed); |
michael@0 | 37 | #endif |
michael@0 | 38 | fRAMUsed += ref->ramUsed(); |
michael@0 | 39 | this->purgeIfNeeded(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void SkImageRefPool::canLosePixels(SkImageRef* ref) { |
michael@0 | 43 | // the refs near fHead have recently been released (used) |
michael@0 | 44 | // if we purge, we purge from the tail |
michael@0 | 45 | this->detach(ref); |
michael@0 | 46 | this->addToHead(ref); |
michael@0 | 47 | this->purgeIfNeeded(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void SkImageRefPool::purgeIfNeeded() { |
michael@0 | 51 | // do nothing if we have a zero-budget (i.e. unlimited) |
michael@0 | 52 | if (fRAMBudget != 0) { |
michael@0 | 53 | this->setRAMUsed(fRAMBudget); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | void SkImageRefPool::setRAMUsed(size_t limit) { |
michael@0 | 58 | SkImageRef* ref = fTail; |
michael@0 | 59 | |
michael@0 | 60 | while (NULL != ref && fRAMUsed > limit) { |
michael@0 | 61 | // only purge it if its pixels are unlocked |
michael@0 | 62 | if (!ref->isLocked() && ref->fBitmap.getPixels()) { |
michael@0 | 63 | size_t size = ref->ramUsed(); |
michael@0 | 64 | SkASSERT(size <= fRAMUsed); |
michael@0 | 65 | fRAMUsed -= size; |
michael@0 | 66 | |
michael@0 | 67 | #ifdef DUMP_IMAGEREF_LIFECYCLE |
michael@0 | 68 | SkDebugf("=== ImagePool: purge %s [%d %d %d] bytes=%d heap=%d\n", |
michael@0 | 69 | ref->getURI(), |
michael@0 | 70 | ref->fBitmap.width(), ref->fBitmap.height(), |
michael@0 | 71 | ref->fBitmap.bytesPerPixel(), |
michael@0 | 72 | (int)size, (int)fRAMUsed); |
michael@0 | 73 | #endif |
michael@0 | 74 | |
michael@0 | 75 | // remember the bitmap config (don't call reset), |
michael@0 | 76 | // just clear the pixel memory |
michael@0 | 77 | ref->fBitmap.setPixels(NULL); |
michael@0 | 78 | SkASSERT(NULL == ref->fBitmap.getPixels()); |
michael@0 | 79 | } |
michael@0 | 80 | ref = ref->fPrev; |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 85 | |
michael@0 | 86 | void SkImageRefPool::addToHead(SkImageRef* ref) { |
michael@0 | 87 | ref->fNext = fHead; |
michael@0 | 88 | ref->fPrev = NULL; |
michael@0 | 89 | |
michael@0 | 90 | if (fHead) { |
michael@0 | 91 | SkASSERT(NULL == fHead->fPrev); |
michael@0 | 92 | fHead->fPrev = ref; |
michael@0 | 93 | } |
michael@0 | 94 | fHead = ref; |
michael@0 | 95 | |
michael@0 | 96 | if (NULL == fTail) { |
michael@0 | 97 | fTail = ref; |
michael@0 | 98 | } |
michael@0 | 99 | fCount += 1; |
michael@0 | 100 | SkASSERT(computeCount() == fCount); |
michael@0 | 101 | |
michael@0 | 102 | fRAMUsed += ref->ramUsed(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void SkImageRefPool::addToTail(SkImageRef* ref) { |
michael@0 | 106 | ref->fNext = NULL; |
michael@0 | 107 | ref->fPrev = fTail; |
michael@0 | 108 | |
michael@0 | 109 | if (fTail) { |
michael@0 | 110 | SkASSERT(NULL == fTail->fNext); |
michael@0 | 111 | fTail->fNext = ref; |
michael@0 | 112 | } |
michael@0 | 113 | fTail = ref; |
michael@0 | 114 | |
michael@0 | 115 | if (NULL == fHead) { |
michael@0 | 116 | fHead = ref; |
michael@0 | 117 | } |
michael@0 | 118 | fCount += 1; |
michael@0 | 119 | SkASSERT(computeCount() == fCount); |
michael@0 | 120 | |
michael@0 | 121 | fRAMUsed += ref->ramUsed(); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void SkImageRefPool::detach(SkImageRef* ref) { |
michael@0 | 125 | SkASSERT(fCount > 0); |
michael@0 | 126 | |
michael@0 | 127 | if (fHead == ref) { |
michael@0 | 128 | fHead = ref->fNext; |
michael@0 | 129 | } |
michael@0 | 130 | if (fTail == ref) { |
michael@0 | 131 | fTail = ref->fPrev; |
michael@0 | 132 | } |
michael@0 | 133 | if (ref->fPrev) { |
michael@0 | 134 | ref->fPrev->fNext = ref->fNext; |
michael@0 | 135 | } |
michael@0 | 136 | if (ref->fNext) { |
michael@0 | 137 | ref->fNext->fPrev = ref->fPrev; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | ref->fNext = ref->fPrev = NULL; |
michael@0 | 141 | |
michael@0 | 142 | fCount -= 1; |
michael@0 | 143 | SkASSERT(computeCount() == fCount); |
michael@0 | 144 | |
michael@0 | 145 | SkASSERT(fRAMUsed >= ref->ramUsed()); |
michael@0 | 146 | fRAMUsed -= ref->ramUsed(); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | int SkImageRefPool::computeCount() const { |
michael@0 | 150 | SkImageRef* ref = fHead; |
michael@0 | 151 | int count = 0; |
michael@0 | 152 | |
michael@0 | 153 | while (ref != NULL) { |
michael@0 | 154 | count += 1; |
michael@0 | 155 | ref = ref->fNext; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | #ifdef SK_DEBUG |
michael@0 | 159 | ref = fTail; |
michael@0 | 160 | int count2 = 0; |
michael@0 | 161 | |
michael@0 | 162 | while (ref != NULL) { |
michael@0 | 163 | count2 += 1; |
michael@0 | 164 | ref = ref->fPrev; |
michael@0 | 165 | } |
michael@0 | 166 | SkASSERT(count2 == count); |
michael@0 | 167 | #endif |
michael@0 | 168 | |
michael@0 | 169 | return count; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 173 | |
michael@0 | 174 | #include "SkStream.h" |
michael@0 | 175 | |
michael@0 | 176 | void SkImageRefPool::dump() const { |
michael@0 | 177 | #if defined(SK_DEBUG) || defined(DUMP_IMAGEREF_LIFECYCLE) |
michael@0 | 178 | SkDebugf("ImagePool dump: bugdet: %d used: %d count: %d\n", |
michael@0 | 179 | (int)fRAMBudget, (int)fRAMUsed, fCount); |
michael@0 | 180 | |
michael@0 | 181 | SkImageRef* ref = fHead; |
michael@0 | 182 | |
michael@0 | 183 | while (ref != NULL) { |
michael@0 | 184 | SkDebugf(" [%3d %3d %d] ram=%d data=%d locked=%d %s\n", ref->fBitmap.width(), |
michael@0 | 185 | ref->fBitmap.height(), ref->fBitmap.config(), |
michael@0 | 186 | ref->ramUsed(), (int)ref->fStream->getLength(), |
michael@0 | 187 | ref->isLocked(), ref->getURI()); |
michael@0 | 188 | |
michael@0 | 189 | ref = ref->fNext; |
michael@0 | 190 | } |
michael@0 | 191 | #endif |
michael@0 | 192 | } |