senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImageFilter.h" |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkBitmap.h" |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
mtklein | 19fcc74 | 2014-07-11 08:42:17 -0700 | [diff] [blame] | 12 | #include "SkChecksum.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 13 | #include "SkDevice.h" |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 14 | #include "SkLocalMatrixImageFilter.h" |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 15 | #include "SkMatrixImageFilter.h" |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 16 | #include "SkOncePtr.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 17 | #include "SkReadBuffer.h" |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 18 | #include "SkRect.h" |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 19 | #include "SkTDynamicHash.h" |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 20 | #include "SkTInternalLList.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 21 | #include "SkValidationUtils.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 22 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 23 | #if SK_SUPPORT_GPU |
| 24 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 25 | #include "GrDrawContext.h" |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 26 | #include "SkGrPixelRef.h" |
| 27 | #include "SkGr.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 28 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 29 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 30 | #ifdef SK_BUILD_FOR_IOS |
| 31 | enum { kDefaultCacheSize = 2 * 1024 * 1024 }; |
| 32 | #else |
| 33 | enum { kDefaultCacheSize = 128 * 1024 * 1024 }; |
| 34 | #endif |
| 35 | |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 36 | #ifndef SK_IGNORE_TO_STRING |
| 37 | void SkImageFilter::CropRect::toString(SkString* str) const { |
| 38 | if (!fFlags) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | str->appendf("cropRect ("); |
| 43 | if (fFlags & CropRect::kHasLeft_CropEdge) { |
| 44 | str->appendf("%.2f, ", fRect.fLeft); |
| 45 | } else { |
| 46 | str->appendf("X, "); |
| 47 | } |
| 48 | if (fFlags & CropRect::kHasTop_CropEdge) { |
| 49 | str->appendf("%.2f, ", fRect.fTop); |
| 50 | } else { |
| 51 | str->appendf("X, "); |
| 52 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 53 | if (fFlags & CropRect::kHasWidth_CropEdge) { |
| 54 | str->appendf("%.2f, ", fRect.width()); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 55 | } else { |
| 56 | str->appendf("X, "); |
| 57 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 58 | if (fFlags & CropRect::kHasHeight_CropEdge) { |
| 59 | str->appendf("%.2f", fRect.height()); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 60 | } else { |
| 61 | str->appendf("X"); |
| 62 | } |
| 63 | str->appendf(") "); |
| 64 | } |
| 65 | #endif |
| 66 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 67 | void SkImageFilter::CropRect::applyTo(const SkIRect& imageBounds, |
| 68 | const SkMatrix& ctm, |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 69 | SkIRect* cropped) const { |
| 70 | *cropped = imageBounds; |
| 71 | if (fFlags) { |
| 72 | SkRect devCropR; |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 73 | ctm.mapRect(&devCropR, fRect); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 74 | const SkIRect devICropR = devCropR.roundOut(); |
| 75 | |
| 76 | // Compute the left/top first, in case we have to read them to compute right/bottom |
| 77 | if (fFlags & kHasLeft_CropEdge) { |
| 78 | cropped->fLeft = devICropR.fLeft; |
| 79 | } |
| 80 | if (fFlags & kHasTop_CropEdge) { |
| 81 | cropped->fTop = devICropR.fTop; |
| 82 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 83 | if (fFlags & kHasWidth_CropEdge) { |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 84 | cropped->fRight = cropped->fLeft + devICropR.width(); |
| 85 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 86 | if (fFlags & kHasHeight_CropEdge) { |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 87 | cropped->fBottom = cropped->fTop + devICropR.height(); |
| 88 | } |
| 89 | } |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 93 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 94 | static int32_t next_image_filter_unique_id() { |
| 95 | static int32_t gImageFilterUniqueID; |
| 96 | |
| 97 | // Never return 0. |
| 98 | int32_t id; |
| 99 | do { |
| 100 | id = sk_atomic_inc(&gImageFilterUniqueID) + 1; |
| 101 | } while (0 == id); |
| 102 | return id; |
| 103 | } |
| 104 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 105 | struct SkImageFilter::Cache::Key { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 106 | Key(const uint32_t uniqueID, const SkMatrix& matrix, const SkIRect& clipBounds, uint32_t srcGenID) |
| 107 | : fUniqueID(uniqueID), fMatrix(matrix), fClipBounds(clipBounds), fSrcGenID(srcGenID) { |
| 108 | // Assert that Key is tightly-packed, since it is hashed. |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 109 | static_assert(sizeof(Key) == sizeof(uint32_t) + sizeof(SkMatrix) + sizeof(SkIRect) + |
| 110 | sizeof(uint32_t), "image_filter_key_tight_packing"); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 111 | fMatrix.getType(); // force initialization of type, so hashes match |
| 112 | } |
| 113 | uint32_t fUniqueID; |
| 114 | SkMatrix fMatrix; |
| 115 | SkIRect fClipBounds; |
| 116 | uint32_t fSrcGenID; |
| 117 | bool operator==(const Key& other) const { |
| 118 | return fUniqueID == other.fUniqueID |
| 119 | && fMatrix == other.fMatrix |
| 120 | && fClipBounds == other.fClipBounds |
| 121 | && fSrcGenID == other.fSrcGenID; |
| 122 | } |
| 123 | }; |
| 124 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 125 | SkImageFilter::Common::~Common() { |
| 126 | for (int i = 0; i < fInputs.count(); ++i) { |
| 127 | SkSafeUnref(fInputs[i]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void SkImageFilter::Common::allocInputs(int count) { |
| 132 | const size_t size = count * sizeof(SkImageFilter*); |
| 133 | fInputs.reset(count); |
| 134 | sk_bzero(fInputs.get(), size); |
| 135 | } |
| 136 | |
| 137 | void SkImageFilter::Common::detachInputs(SkImageFilter** inputs) { |
| 138 | const size_t size = fInputs.count() * sizeof(SkImageFilter*); |
| 139 | memcpy(inputs, fInputs.get(), size); |
| 140 | sk_bzero(fInputs.get(), size); |
| 141 | } |
| 142 | |
| 143 | bool SkImageFilter::Common::unflatten(SkReadBuffer& buffer, int expectedCount) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 144 | const int count = buffer.readInt(); |
| 145 | if (!buffer.validate(count >= 0)) { |
| 146 | return false; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 147 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 148 | if (!buffer.validate(expectedCount < 0 || count == expectedCount)) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | |
| 152 | this->allocInputs(count); |
| 153 | for (int i = 0; i < count; i++) { |
| 154 | if (buffer.readBool()) { |
| 155 | fInputs[i] = buffer.readImageFilter(); |
| 156 | } |
| 157 | if (!buffer.isValid()) { |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | SkRect rect; |
| 162 | buffer.readRect(&rect); |
| 163 | if (!buffer.isValid() || !buffer.validate(SkIsValidRect(rect))) { |
| 164 | return false; |
| 165 | } |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 166 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 167 | uint32_t flags = buffer.readUInt(); |
| 168 | fCropRect = CropRect(rect, flags); |
senorblanco | 4a22a43 | 2015-03-18 13:14:54 -0700 | [diff] [blame] | 169 | if (buffer.isVersionLT(SkReadBuffer::kImageFilterNoUniqueID_Version)) { |
| 170 | |
| 171 | (void) buffer.readUInt(); |
| 172 | } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 173 | return buffer.isValid(); |
| 174 | } |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 177 | |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 178 | SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 179 | : fInputCount(inputCount), |
| 180 | fInputs(new SkImageFilter*[inputCount]), |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 181 | fUsesSrcInput(false), |
| 182 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)), |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 183 | fUniqueID(next_image_filter_unique_id()) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 184 | for (int i = 0; i < inputCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 185 | if (nullptr == inputs[i] || inputs[i]->usesSrcInput()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 186 | fUsesSrcInput = true; |
| 187 | } |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 188 | fInputs[i] = inputs[i]; |
| 189 | SkSafeRef(fInputs[i]); |
| 190 | } |
| 191 | } |
| 192 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 193 | SkImageFilter::~SkImageFilter() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 194 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 195 | SkSafeUnref(fInputs[i]); |
| 196 | } |
| 197 | delete[] fInputs; |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 198 | Cache::Get()->purgeByKeys(fCacheKeys.begin(), fCacheKeys.count()); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 199 | } |
| 200 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 201 | SkImageFilter::SkImageFilter(int inputCount, SkReadBuffer& buffer) |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 202 | : fUsesSrcInput(false) |
| 203 | , fUniqueID(next_image_filter_unique_id()) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 204 | Common common; |
| 205 | if (common.unflatten(buffer, inputCount)) { |
| 206 | fCropRect = common.cropRect(); |
| 207 | fInputCount = common.inputCount(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 208 | fInputs = new SkImageFilter* [fInputCount]; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 209 | common.detachInputs(fInputs); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 210 | for (int i = 0; i < fInputCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 211 | if (nullptr == fInputs[i] || fInputs[i]->usesSrcInput()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 212 | fUsesSrcInput = true; |
| 213 | } |
| 214 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 215 | } else { |
| 216 | fInputCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 217 | fInputs = nullptr; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 221 | void SkImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 222 | buffer.writeInt(fInputCount); |
| 223 | for (int i = 0; i < fInputCount; i++) { |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 224 | SkImageFilter* input = this->getInput(i); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 225 | buffer.writeBool(input != nullptr); |
| 226 | if (input != nullptr) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 227 | buffer.writeFlattenable(input); |
| 228 | } |
| 229 | } |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 230 | buffer.writeRect(fCropRect.rect()); |
| 231 | buffer.writeUInt(fCropRect.flags()); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 232 | } |
| 233 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 234 | bool SkImageFilter::filterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 235 | const Context& context, |
| 236 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 237 | SkASSERT(result); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 238 | SkASSERT(offset); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 239 | uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0; |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 240 | Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID); |
| 241 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 242 | if (context.cache()->get(key, result, offset)) { |
| 243 | return true; |
| 244 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 245 | } |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 246 | /* |
| 247 | * Give the proxy first shot at the filter. If it returns false, ask |
| 248 | * the filter to do it. |
| 249 | */ |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 250 | if ((proxy && proxy->filterImage(this, src, context, result, offset)) || |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 251 | this->onFilterImageDeprecated(proxy, src, context, result, offset)) { |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 252 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 253 | context.cache()->set(key, *result, *offset); |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 254 | SkAutoMutexAcquire mutex(fMutex); |
| 255 | fCacheKeys.push_back(key); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 256 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 257 | return true; |
| 258 | } |
| 259 | return false; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 260 | } |
| 261 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 262 | bool SkImageFilter::filterInputDeprecated(int index, Proxy* proxy, const SkBitmap& src, |
| 263 | const Context& ctx, |
| 264 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 265 | SkImageFilter* input = this->getInput(index); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 266 | if (!input) { |
| 267 | return true; |
| 268 | } |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 269 | return input->filterImageDeprecated(proxy, src, this->mapContext(ctx), result, offset); |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 270 | } |
| 271 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 272 | bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm, SkIRect* dst, |
| 273 | MapDirection direction) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 274 | SkASSERT(dst); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 275 | SkIRect bounds; |
| 276 | if (kReverse_MapDirection == direction) { |
| 277 | this->onFilterNodeBounds(src, ctm, &bounds, direction); |
| 278 | return this->onFilterBounds(bounds, ctm, dst, direction); |
| 279 | } else { |
| 280 | SkIRect temp; |
| 281 | if (!this->onFilterBounds(src, ctm, &bounds, direction)) { |
| 282 | return false; |
| 283 | } |
| 284 | this->onFilterNodeBounds(bounds, ctm, &temp, direction); |
| 285 | this->getCropRect().applyTo(temp, ctm, dst); |
| 286 | return true; |
| 287 | } |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 288 | } |
| 289 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 290 | void SkImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 291 | if (0 == fInputCount) { |
| 292 | *dst = src; |
| 293 | return; |
| 294 | } |
| 295 | if (this->getInput(0)) { |
| 296 | this->getInput(0)->computeFastBounds(src, dst); |
| 297 | } else { |
| 298 | *dst = src; |
| 299 | } |
| 300 | for (int i = 1; i < fInputCount; i++) { |
| 301 | SkImageFilter* input = this->getInput(i); |
| 302 | if (input) { |
| 303 | SkRect bounds; |
| 304 | input->computeFastBounds(src, &bounds); |
| 305 | dst->join(bounds); |
| 306 | } else { |
| 307 | dst->join(src); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 312 | bool SkImageFilter::canComputeFastBounds() const { |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 313 | for (int i = 0; i < fInputCount; i++) { |
| 314 | SkImageFilter* input = this->getInput(i); |
| 315 | if (input && !input->canComputeFastBounds()) { |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | return true; |
| 320 | } |
| 321 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 322 | bool SkImageFilter::onFilterImageDeprecated(Proxy*, const SkBitmap&, const Context&, |
| 323 | SkBitmap*, SkIPoint*) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool SkImageFilter::canFilterImageGPU() const { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 328 | return this->asFragmentProcessor(nullptr, nullptr, SkMatrix::I(), SkIRect()); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 329 | } |
| 330 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 331 | bool SkImageFilter::filterImageGPUDeprecated(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
| 332 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 333 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 334 | SkBitmap input = src; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 335 | SkASSERT(fInputCount == 1); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 336 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 337 | if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 338 | return false; |
| 339 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 340 | GrTexture* srcTexture = input.getTexture(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 341 | SkIRect bounds; |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 342 | if (!this->applyCropRectDeprecated(ctx, proxy, input, &srcOffset, &bounds, &input)) { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 343 | return false; |
| 344 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 345 | GrContext* context = srcTexture->getContext(); |
| 346 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 347 | GrSurfaceDesc desc; |
| 348 | desc.fFlags = kRenderTarget_GrSurfaceFlag, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 349 | desc.fWidth = bounds.width(); |
| 350 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 351 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 352 | |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 353 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 354 | if (!dst) { |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 355 | return false; |
| 356 | } |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 357 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 358 | GrFragmentProcessor* fp; |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 359 | offset->fX = bounds.left(); |
| 360 | offset->fY = bounds.top(); |
| 361 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 362 | SkMatrix matrix(ctx.ctm()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 363 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
joshualitt | 85ff25e | 2015-07-08 09:10:03 -0700 | [diff] [blame] | 364 | GrPaint paint; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 365 | if (this->asFragmentProcessor(&fp, srcTexture, matrix, bounds)) { |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 366 | SkASSERT(fp); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 367 | paint.addColorFragmentProcessor(fp)->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 368 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 369 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 370 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 371 | if (drawContext) { |
senorblanco | eae84c2 | 2016-01-26 08:41:02 -0800 | [diff] [blame] | 372 | SkRect srcRect = SkRect::Make(bounds); |
| 373 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 374 | GrClip clip(dstRect); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 375 | drawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 376 | |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 377 | GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 378 | return true; |
| 379 | } |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 380 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 381 | #endif |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 382 | return false; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 383 | } |
| 384 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 385 | bool SkImageFilter::asAColorFilter(SkColorFilter** filterPtr) const { |
| 386 | SkASSERT(nullptr != filterPtr); |
| 387 | if (!this->isColorFilterNode(filterPtr)) { |
| 388 | return false; |
| 389 | } |
| 390 | if (nullptr != this->getInput(0) || (*filterPtr)->affectsTransparentBlack()) { |
| 391 | (*filterPtr)->unref(); |
| 392 | return false; |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 397 | bool SkImageFilter::applyCropRect(const Context& ctx, const SkIRect& srcBounds, |
| 398 | SkIRect* dstBounds) const { |
| 399 | this->onFilterNodeBounds(srcBounds, ctx.ctm(), dstBounds, kForward_MapDirection); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 400 | fCropRect.applyTo(*dstBounds, ctx.ctm(), dstBounds); |
| 401 | // Intersect against the clip bounds, in case the crop rect has |
| 402 | // grown the bounds beyond the original clip. This can happen for |
| 403 | // example in tiling, where the clip is much smaller than the filtered |
| 404 | // primitive. If we didn't do this, we would be processing the filter |
| 405 | // at the full crop rect size in every tile. |
| 406 | return dstBounds->intersect(ctx.clipBounds()); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 407 | } |
| 408 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 409 | bool SkImageFilter::applyCropRectDeprecated(const Context& ctx, Proxy* proxy, const SkBitmap& src, |
| 410 | SkIPoint* srcOffset, SkIRect* bounds, |
| 411 | SkBitmap* dst) const { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 412 | SkIRect srcBounds; |
| 413 | src.getBounds(&srcBounds); |
| 414 | srcBounds.offset(*srcOffset); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 415 | SkIRect dstBounds; |
| 416 | this->onFilterNodeBounds(srcBounds, ctx.ctm(), &dstBounds, kForward_MapDirection); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 417 | fCropRect.applyTo(dstBounds, ctx.ctm(), bounds); |
| 418 | if (!bounds->intersect(ctx.clipBounds())) { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 421 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 422 | if (srcBounds.contains(*bounds)) { |
| 423 | *dst = src; |
| 424 | return true; |
| 425 | } else { |
| 426 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), bounds->height())); |
| 427 | if (!device) { |
| 428 | return false; |
| 429 | } |
| 430 | SkCanvas canvas(device); |
| 431 | canvas.clear(0x00000000); |
| 432 | canvas.drawBitmap(src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y()); |
| 433 | *srcOffset = SkIPoint::Make(bounds->x(), bounds->y()); |
| 434 | *dst = device->accessBitmap(false); |
| 435 | return true; |
| 436 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 439 | bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 440 | SkIRect* dst, MapDirection direction) const { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 441 | if (fInputCount < 1) { |
senorblanco | 8f3937d | 2014-10-29 12:36:32 -0700 | [diff] [blame] | 442 | *dst = src; |
| 443 | return true; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 444 | } |
| 445 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 446 | SkIRect totalBounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 447 | for (int i = 0; i < fInputCount; ++i) { |
| 448 | SkImageFilter* filter = this->getInput(i); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 449 | SkIRect rect = src; |
| 450 | if (filter && !filter->filterBounds(src, ctm, &rect, direction)) { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | if (0 == i) { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 454 | totalBounds = rect; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 455 | } else { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 456 | totalBounds.join(rect); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | // don't modify dst until now, so we don't accidentally change it in the |
| 461 | // loop, but then return false on the next filter. |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 462 | *dst = totalBounds; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 466 | void SkImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix&, |
| 467 | SkIRect* dst, MapDirection) const { |
| 468 | *dst = src; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | SkImageFilter::Context SkImageFilter::mapContext(const Context& ctx) const { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 473 | SkIRect clipBounds; |
| 474 | this->onFilterNodeBounds(ctx.clipBounds(), ctx.ctm(), &clipBounds, |
| 475 | MapDirection::kReverse_MapDirection); |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 476 | return Context(ctx.ctm(), clipBounds, ctx.cache()); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 477 | } |
| 478 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 479 | bool SkImageFilter::asFragmentProcessor(GrFragmentProcessor**, GrTexture*, |
joshualitt | 85ff25e | 2015-07-08 09:10:03 -0700 | [diff] [blame] | 480 | const SkMatrix&, const SkIRect&) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 481 | return false; |
| 482 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 483 | |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 484 | SkImageFilter* SkImageFilter::CreateMatrixFilter(const SkMatrix& matrix, |
| 485 | SkFilterQuality filterQuality, |
| 486 | SkImageFilter* input) { |
| 487 | return SkMatrixImageFilter::Create(matrix, filterQuality, input); |
| 488 | } |
| 489 | |
reed | 94dd7a5 | 2015-10-14 07:49:35 -0700 | [diff] [blame] | 490 | SkImageFilter* SkImageFilter::newWithLocalMatrix(const SkMatrix& matrix) const { |
| 491 | // SkLocalMatrixImageFilter takes SkImage* in its factory, but logically that parameter |
| 492 | // is *always* treated as a const ptr. Hence the const-cast here. |
| 493 | // |
| 494 | return SkLocalMatrixImageFilter::Create(matrix, const_cast<SkImageFilter*>(this)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 495 | } |
| 496 | |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 497 | #if SK_SUPPORT_GPU |
| 498 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 499 | bool SkImageFilter::filterInputGPUDeprecated(int index, SkImageFilter::Proxy* proxy, |
| 500 | const SkBitmap& src, const Context& ctx, |
| 501 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco | 9a70b6e | 2015-10-16 11:35:14 -0700 | [diff] [blame] | 502 | SkImageFilter* input = this->getInput(index); |
| 503 | if (!input) { |
| 504 | return true; |
| 505 | } |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 506 | // Ensure that GrContext calls under filterImage and filterImageGPU below will see an identity |
| 507 | // matrix with no clip and that the matrix, clip, and render target set before this function was |
| 508 | // called are restored before we return to the caller. |
| 509 | GrContext* context = src.getTexture()->getContext(); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame^] | 510 | if (input->filterImageDeprecated(proxy, src, this->mapContext(ctx), result, offset)) { |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 511 | if (!result->getTexture()) { |
| 512 | const SkImageInfo info = result->info(); |
| 513 | if (kUnknown_SkColorType == info.colorType()) { |
| 514 | return false; |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 515 | } |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 516 | SkAutoTUnref<GrTexture> resultTex( |
bsalomon | 45eefcf | 2016-01-05 08:39:28 -0800 | [diff] [blame] | 517 | GrRefCachedBitmapTexture(context, *result, GrTextureParams::ClampNoFilter())); |
| 518 | if (!resultTex) { |
| 519 | return false; |
| 520 | } |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 521 | result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref(); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 522 | } |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 523 | return true; |
| 524 | } else { |
| 525 | return false; |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | #endif |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 529 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 530 | namespace { |
| 531 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 532 | class CacheImpl : public SkImageFilter::Cache { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 533 | public: |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 534 | CacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) { |
| 535 | } |
| 536 | virtual ~CacheImpl() { |
| 537 | SkTDynamicHash<Value, Key>::Iter iter(&fLookup); |
| 538 | |
| 539 | while (!iter.done()) { |
| 540 | Value* v = &*iter; |
| 541 | ++iter; |
| 542 | delete v; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 543 | } |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 544 | } |
| 545 | struct Value { |
| 546 | Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset) |
| 547 | : fKey(key), fBitmap(bitmap), fOffset(offset) {} |
| 548 | Key fKey; |
| 549 | SkBitmap fBitmap; |
| 550 | SkIPoint fOffset; |
| 551 | static const Key& GetKey(const Value& v) { |
| 552 | return v.fKey; |
| 553 | } |
| 554 | static uint32_t Hash(const Key& key) { |
| 555 | return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); |
| 556 | } |
| 557 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 558 | }; |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 559 | bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const override { |
| 560 | SkAutoMutexAcquire mutex(fMutex); |
| 561 | if (Value* v = fLookup.find(key)) { |
| 562 | *result = v->fBitmap; |
| 563 | *offset = v->fOffset; |
| 564 | if (v != fLRU.head()) { |
| 565 | fLRU.remove(v); |
| 566 | fLRU.addToHead(v); |
| 567 | } |
| 568 | return true; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 569 | } |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 570 | return false; |
| 571 | } |
| 572 | void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) override { |
| 573 | SkAutoMutexAcquire mutex(fMutex); |
| 574 | if (Value* v = fLookup.find(key)) { |
| 575 | removeInternal(v); |
| 576 | } |
| 577 | Value* v = new Value(key, result, offset); |
| 578 | fLookup.add(v); |
| 579 | fLRU.addToHead(v); |
| 580 | fCurrentBytes += result.getSize(); |
| 581 | while (fCurrentBytes > fMaxBytes) { |
| 582 | Value* tail = fLRU.tail(); |
| 583 | SkASSERT(tail); |
| 584 | if (tail == v) { |
| 585 | break; |
| 586 | } |
| 587 | removeInternal(tail); |
| 588 | } |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 589 | } |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 590 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 591 | void purge() override { |
| 592 | SkAutoMutexAcquire mutex(fMutex); |
| 593 | while (fCurrentBytes > 0) { |
| 594 | Value* tail = fLRU.tail(); |
| 595 | SkASSERT(tail); |
| 596 | this->removeInternal(tail); |
| 597 | } |
| 598 | } |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 599 | |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 600 | void purgeByKeys(const Key keys[], int count) override { |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 601 | SkAutoMutexAcquire mutex(fMutex); |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 602 | for (int i = 0; i < count; i++) { |
| 603 | if (Value* v = fLookup.find(keys[i])) { |
| 604 | this->removeInternal(v); |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 605 | } |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 609 | private: |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 610 | void removeInternal(Value* v) { |
| 611 | fCurrentBytes -= v->fBitmap.getSize(); |
| 612 | fLRU.remove(v); |
| 613 | fLookup.remove(v->fKey); |
| 614 | delete v; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 615 | } |
| 616 | private: |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 617 | SkTDynamicHash<Value, Key> fLookup; |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 618 | mutable SkTInternalLList<Value> fLRU; |
| 619 | size_t fMaxBytes; |
| 620 | size_t fCurrentBytes; |
| 621 | mutable SkMutex fMutex; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 622 | }; |
| 623 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 624 | } // namespace |
| 625 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 626 | SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 627 | return new CacheImpl(maxBytes); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 628 | } |
| 629 | |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 630 | SK_DECLARE_STATIC_ONCE_PTR(SkImageFilter::Cache, cache); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 631 | SkImageFilter::Cache* SkImageFilter::Cache::Get() { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 632 | return cache.get([]{ return SkImageFilter::Cache::Create(kDefaultCacheSize); }); |
| 633 | } |
| 634 | |
| 635 | void SkImageFilter::PurgeCache() { |
| 636 | Cache::Get()->purge(); |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 637 | } |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 638 | |
| 639 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 640 | |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 641 | SkBaseDevice* SkImageFilter::DeviceProxy::createDevice(int w, int h, TileUsage usage) { |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 642 | SkBaseDevice::CreateInfo cinfo(SkImageInfo::MakeN32Premul(w, h), |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 643 | kPossible_TileUsage == usage ? SkBaseDevice::kPossible_TileUsage |
| 644 | : SkBaseDevice::kNever_TileUsage, |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 645 | kUnknown_SkPixelGeometry, |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 646 | false, /* preserveLCDText */ |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 647 | true /*forImageFilter*/); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 648 | SkBaseDevice* dev = fDevice->onCreateDevice(cinfo, nullptr); |
| 649 | if (nullptr == dev) { |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 650 | const SkSurfaceProps surfaceProps(fDevice->fSurfaceProps.flags(), |
| 651 | kUnknown_SkPixelGeometry); |
| 652 | dev = SkBitmapDevice::Create(cinfo.fInfo, surfaceProps); |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 653 | } |
| 654 | return dev; |
| 655 | } |
| 656 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 657 | bool SkImageFilter::DeviceProxy::filterImage(const SkImageFilter* filter, const SkBitmap& src, |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 658 | const SkImageFilter::Context& ctx, |
| 659 | SkBitmap* result, SkIPoint* offset) { |
| 660 | return fDevice->filterImage(filter, src, ctx, result, offset); |
| 661 | } |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 662 | |