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" |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 9 | #include "SkImageFilterCacheKey.h" |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkBitmap.h" |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 12 | #include "SkBitmapDevice.h" |
mtklein | 19fcc74 | 2014-07-11 08:42:17 -0700 | [diff] [blame] | 13 | #include "SkChecksum.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 14 | #include "SkDevice.h" |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 15 | #include "SkLocalMatrixImageFilter.h" |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 16 | #include "SkMatrixImageFilter.h" |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 17 | #include "SkOncePtr.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 18 | #include "SkReadBuffer.h" |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 19 | #include "SkRect.h" |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 20 | #include "SkSpecialImage.h" |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 21 | #include "SkSpecialSurface.h" |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 22 | #include "SkTDynamicHash.h" |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 23 | #include "SkTInternalLList.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 24 | #include "SkValidationUtils.h" |
mtklein | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 25 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 26 | #if SK_SUPPORT_GPU |
| 27 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 28 | #include "GrDrawContext.h" |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 29 | #include "SkGrPixelRef.h" |
| 30 | #include "SkGr.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 31 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 32 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 33 | #ifdef SK_BUILD_FOR_IOS |
| 34 | enum { kDefaultCacheSize = 2 * 1024 * 1024 }; |
| 35 | #else |
| 36 | enum { kDefaultCacheSize = 128 * 1024 * 1024 }; |
| 37 | #endif |
| 38 | |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 39 | #ifndef SK_IGNORE_TO_STRING |
| 40 | void SkImageFilter::CropRect::toString(SkString* str) const { |
| 41 | if (!fFlags) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | str->appendf("cropRect ("); |
| 46 | if (fFlags & CropRect::kHasLeft_CropEdge) { |
| 47 | str->appendf("%.2f, ", fRect.fLeft); |
| 48 | } else { |
| 49 | str->appendf("X, "); |
| 50 | } |
| 51 | if (fFlags & CropRect::kHasTop_CropEdge) { |
| 52 | str->appendf("%.2f, ", fRect.fTop); |
| 53 | } else { |
| 54 | str->appendf("X, "); |
| 55 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 56 | if (fFlags & CropRect::kHasWidth_CropEdge) { |
| 57 | str->appendf("%.2f, ", fRect.width()); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 58 | } else { |
| 59 | str->appendf("X, "); |
| 60 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 61 | if (fFlags & CropRect::kHasHeight_CropEdge) { |
| 62 | str->appendf("%.2f", fRect.height()); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 63 | } else { |
| 64 | str->appendf("X"); |
| 65 | } |
| 66 | str->appendf(") "); |
| 67 | } |
| 68 | #endif |
| 69 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 70 | void SkImageFilter::CropRect::applyTo(const SkIRect& imageBounds, |
| 71 | const SkMatrix& ctm, |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 72 | SkIRect* cropped) const { |
| 73 | *cropped = imageBounds; |
| 74 | if (fFlags) { |
| 75 | SkRect devCropR; |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 76 | ctm.mapRect(&devCropR, fRect); |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 77 | const SkIRect devICropR = devCropR.roundOut(); |
| 78 | |
| 79 | // Compute the left/top first, in case we have to read them to compute right/bottom |
| 80 | if (fFlags & kHasLeft_CropEdge) { |
| 81 | cropped->fLeft = devICropR.fLeft; |
| 82 | } |
| 83 | if (fFlags & kHasTop_CropEdge) { |
| 84 | cropped->fTop = devICropR.fTop; |
| 85 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 86 | if (fFlags & kHasWidth_CropEdge) { |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 87 | cropped->fRight = cropped->fLeft + devICropR.width(); |
| 88 | } |
senorblanco | ed7cf27 | 2015-07-16 15:19:11 -0700 | [diff] [blame] | 89 | if (fFlags & kHasHeight_CropEdge) { |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 90 | cropped->fBottom = cropped->fTop + devICropR.height(); |
| 91 | } |
| 92 | } |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 96 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 97 | static int32_t next_image_filter_unique_id() { |
| 98 | static int32_t gImageFilterUniqueID; |
| 99 | |
| 100 | // Never return 0. |
| 101 | int32_t id; |
| 102 | do { |
| 103 | id = sk_atomic_inc(&gImageFilterUniqueID) + 1; |
| 104 | } while (0 == id); |
| 105 | return id; |
| 106 | } |
| 107 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 108 | void SkImageFilter::Common::allocInputs(int count) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 109 | fInputs.reset(count); |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void SkImageFilter::Common::detachInputs(SkImageFilter** inputs) { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 113 | for (int i = 0; i < fInputs.count(); ++i) { |
| 114 | inputs[i] = fInputs[i].release(); |
| 115 | } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool SkImageFilter::Common::unflatten(SkReadBuffer& buffer, int expectedCount) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 119 | const int count = buffer.readInt(); |
| 120 | if (!buffer.validate(count >= 0)) { |
| 121 | return false; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 122 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 123 | if (!buffer.validate(expectedCount < 0 || count == expectedCount)) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | this->allocInputs(count); |
| 128 | for (int i = 0; i < count; i++) { |
| 129 | if (buffer.readBool()) { |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 130 | fInputs[i] = sk_sp<SkImageFilter>(buffer.readImageFilter()); |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 131 | } |
| 132 | if (!buffer.isValid()) { |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | SkRect rect; |
| 137 | buffer.readRect(&rect); |
| 138 | if (!buffer.isValid() || !buffer.validate(SkIsValidRect(rect))) { |
| 139 | return false; |
| 140 | } |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 141 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 142 | uint32_t flags = buffer.readUInt(); |
| 143 | fCropRect = CropRect(rect, flags); |
senorblanco | 4a22a43 | 2015-03-18 13:14:54 -0700 | [diff] [blame] | 144 | if (buffer.isVersionLT(SkReadBuffer::kImageFilterNoUniqueID_Version)) { |
| 145 | |
| 146 | (void) buffer.readUInt(); |
| 147 | } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 148 | return buffer.isValid(); |
| 149 | } |
| 150 | |
| 151 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 152 | |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 153 | SkImageFilter::SkImageFilter(sk_sp<SkImageFilter>* inputs, |
| 154 | int inputCount, |
| 155 | const CropRect* cropRect) |
| 156 | : fInputCount(inputCount), |
| 157 | fInputs(new SkImageFilter*[inputCount]), |
| 158 | fUsesSrcInput(false), |
| 159 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)), |
| 160 | fUniqueID(next_image_filter_unique_id()) { |
| 161 | for (int i = 0; i < inputCount; ++i) { |
| 162 | if (nullptr == inputs[i] || inputs[i]->usesSrcInput()) { |
| 163 | fUsesSrcInput = true; |
| 164 | } |
| 165 | fInputs[i] = SkSafeRef(inputs[i].get()); |
| 166 | } |
| 167 | } |
| 168 | |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 169 | SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 170 | : fInputCount(inputCount), |
| 171 | fInputs(new SkImageFilter*[inputCount]), |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 172 | fUsesSrcInput(false), |
| 173 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)), |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 174 | fUniqueID(next_image_filter_unique_id()) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 175 | for (int i = 0; i < inputCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 176 | if (nullptr == inputs[i] || inputs[i]->usesSrcInput()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 177 | fUsesSrcInput = true; |
| 178 | } |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 179 | fInputs[i] = SkSafeRef(inputs[i]); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 183 | SkImageFilter::~SkImageFilter() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 184 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 185 | SkSafeUnref(fInputs[i]); |
| 186 | } |
| 187 | delete[] fInputs; |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 188 | Cache::Get()->purgeByKeys(fCacheKeys.begin(), fCacheKeys.count()); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 189 | } |
| 190 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 191 | SkImageFilter::SkImageFilter(int inputCount, SkReadBuffer& buffer) |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 192 | : fUsesSrcInput(false) |
| 193 | , fUniqueID(next_image_filter_unique_id()) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 194 | Common common; |
| 195 | if (common.unflatten(buffer, inputCount)) { |
| 196 | fCropRect = common.cropRect(); |
| 197 | fInputCount = common.inputCount(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 198 | fInputs = new SkImageFilter* [fInputCount]; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 199 | common.detachInputs(fInputs); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 200 | for (int i = 0; i < fInputCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | if (nullptr == fInputs[i] || fInputs[i]->usesSrcInput()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 202 | fUsesSrcInput = true; |
| 203 | } |
| 204 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 205 | } else { |
| 206 | fInputCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | fInputs = nullptr; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 211 | void SkImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 212 | buffer.writeInt(fInputCount); |
| 213 | for (int i = 0; i < fInputCount; i++) { |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 214 | SkImageFilter* input = this->getInput(i); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | buffer.writeBool(input != nullptr); |
| 216 | if (input != nullptr) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 217 | buffer.writeFlattenable(input); |
| 218 | } |
| 219 | } |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 220 | buffer.writeRect(fCropRect.rect()); |
| 221 | buffer.writeUInt(fCropRect.flags()); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 222 | } |
| 223 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 224 | sk_sp<SkSpecialImage> SkImageFilter::filterImage(SkSpecialImage* src, const Context& context, |
| 225 | SkIPoint* offset) const { |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 226 | SkASSERT(src && offset); |
| 227 | |
| 228 | uint32_t srcGenID = fUsesSrcInput ? src->uniqueID() : 0; |
| 229 | const SkIRect srcSubset = fUsesSrcInput ? src->subset() : SkIRect::MakeWH(0, 0); |
| 230 | Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID, srcSubset); |
| 231 | if (context.cache()) { |
| 232 | SkSpecialImage* result = context.cache()->get(key, offset); |
| 233 | if (result) { |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 234 | return sk_sp<SkSpecialImage>(SkRef(result)); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 238 | sk_sp<SkSpecialImage> result(this->onFilterImage(src, context, offset)); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 239 | if (result && context.cache()) { |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 240 | context.cache()->set(key, result.get(), *offset); |
robertphillips | bd590aa | 2016-03-08 08:37:18 -0800 | [diff] [blame] | 241 | SkAutoMutexAcquire mutex(fMutex); |
| 242 | fCacheKeys.push_back(key); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return result; |
| 246 | } |
| 247 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 248 | bool SkImageFilter::filterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 249 | const Context& context, |
| 250 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 251 | SkASSERT(result); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 252 | SkASSERT(offset); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 253 | uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 254 | Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), |
| 255 | srcGenID, SkIRect::MakeWH(0, 0)); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 256 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 257 | if (context.cache()->get(key, result, offset)) { |
| 258 | return true; |
| 259 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 260 | } |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 261 | /* |
| 262 | * Give the proxy first shot at the filter. If it returns false, ask |
| 263 | * the filter to do it. |
| 264 | */ |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 265 | if ((proxy && proxy->filterImage(this, src, context, result, offset)) || |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 266 | this->onFilterImageDeprecated(proxy, src, context, result, offset)) { |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 267 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 268 | context.cache()->set(key, *result, *offset); |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 269 | SkAutoMutexAcquire mutex(fMutex); |
| 270 | fCacheKeys.push_back(key); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 271 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | return false; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 275 | } |
| 276 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 277 | bool SkImageFilter::filterInputDeprecated(int index, Proxy* proxy, const SkBitmap& src, |
| 278 | const Context& ctx, |
| 279 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 280 | SkImageFilter* input = this->getInput(index); |
reed | c9b5f8b | 2015-10-22 13:20:20 -0700 | [diff] [blame] | 281 | if (!input) { |
| 282 | return true; |
| 283 | } |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 284 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 285 | sk_sp<SkSpecialImage> specialSrc(SkSpecialImage::internal_fromBM(proxy, src)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 286 | if (!specialSrc) { |
| 287 | return false; |
| 288 | } |
| 289 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 290 | sk_sp<SkSpecialImage> tmp(input->onFilterImage(specialSrc.get(), |
| 291 | this->mapContext(ctx), |
| 292 | offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 293 | if (!tmp) { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return tmp->internal_getBM(result); |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 298 | } |
| 299 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 300 | SkIRect SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 301 | MapDirection direction) const { |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 302 | if (kReverse_MapDirection == direction) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 303 | SkIRect bounds = this->onFilterNodeBounds(src, ctm, direction); |
| 304 | return this->onFilterBounds(bounds, ctm, direction); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 305 | } else { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 306 | SkIRect bounds = this->onFilterBounds(src, ctm, direction); |
| 307 | bounds = this->onFilterNodeBounds(bounds, ctm, direction); |
| 308 | SkIRect dst; |
| 309 | this->getCropRect().applyTo(bounds, ctm, &dst); |
| 310 | return dst; |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 311 | } |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 312 | } |
| 313 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 314 | SkRect SkImageFilter::computeFastBounds(const SkRect& src) const { |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 315 | if (0 == fInputCount) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 316 | return src; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 317 | } |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 318 | SkRect combinedBounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 319 | for (int i = 1; i < fInputCount; i++) { |
| 320 | SkImageFilter* input = this->getInput(i); |
| 321 | if (input) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 322 | combinedBounds.join(input->computeFastBounds(src)); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 323 | } else { |
jbroman | 0e3129d | 2016-03-17 12:24:23 -0700 | [diff] [blame] | 324 | combinedBounds.join(src); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 327 | return combinedBounds; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 328 | } |
| 329 | |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 330 | bool SkImageFilter::canComputeFastBounds() const { |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 331 | for (int i = 0; i < fInputCount; i++) { |
| 332 | SkImageFilter* input = this->getInput(i); |
| 333 | if (input && !input->canComputeFastBounds()) { |
| 334 | return false; |
| 335 | } |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 340 | bool SkImageFilter::onFilterImageDeprecated(Proxy*, const SkBitmap&, const Context&, |
| 341 | SkBitmap*, SkIPoint*) const { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 342 | // Only classes that now use the new SkSpecialImage-based path will not have |
| 343 | // onFilterImageDeprecated methods. For those classes we should never be |
| 344 | // calling this method. |
| 345 | SkASSERT(0); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 349 | // SkImageFilter-derived classes that do not yet have their own onFilterImage |
| 350 | // implementation convert back to calling the deprecated filterImage method |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 351 | sk_sp<SkSpecialImage> SkImageFilter::onFilterImage(SkSpecialImage* src, const Context& ctx, |
| 352 | SkIPoint* offset) const { |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 353 | SkBitmap srcBM, resultBM; |
| 354 | |
| 355 | if (!src->internal_getBM(&srcBM)) { |
| 356 | return nullptr; |
| 357 | } |
| 358 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 359 | // This is the only valid call to the old filterImage path |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 360 | if (!this->filterImageDeprecated(src->internal_getProxy(), srcBM, ctx, &resultBM, offset)) { |
| 361 | return nullptr; |
| 362 | } |
| 363 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 364 | return SkSpecialImage::internal_fromBM(src->internal_getProxy(), resultBM); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 365 | } |
| 366 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 367 | bool SkImageFilter::canFilterImageGPU() const { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 368 | return this->asFragmentProcessor(nullptr, nullptr, SkMatrix::I(), SkIRect()); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 369 | } |
| 370 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 371 | bool SkImageFilter::filterImageGPUDeprecated(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
| 372 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 373 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 374 | SkBitmap input = src; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 375 | SkASSERT(fInputCount == 1); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 376 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 377 | if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 380 | GrTexture* srcTexture = input.getTexture(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 381 | SkIRect bounds; |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 382 | if (!this->applyCropRectDeprecated(ctx, proxy, input, &srcOffset, &bounds, &input)) { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 383 | return false; |
| 384 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 385 | GrContext* context = srcTexture->getContext(); |
| 386 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 387 | GrSurfaceDesc desc; |
| 388 | desc.fFlags = kRenderTarget_GrSurfaceFlag, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 389 | desc.fWidth = bounds.width(); |
| 390 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 391 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 392 | |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 393 | SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 394 | if (!dst) { |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 397 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 398 | GrFragmentProcessor* fp; |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 399 | offset->fX = bounds.left(); |
| 400 | offset->fY = bounds.top(); |
| 401 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 402 | SkMatrix matrix(ctx.ctm()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 403 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
joshualitt | 85ff25e | 2015-07-08 09:10:03 -0700 | [diff] [blame] | 404 | GrPaint paint; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 405 | if (this->asFragmentProcessor(&fp, srcTexture, matrix, bounds)) { |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 406 | SkASSERT(fp); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 407 | paint.addColorFragmentProcessor(fp)->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 408 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 409 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 410 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 411 | if (drawContext) { |
senorblanco | eae84c2 | 2016-01-26 08:41:02 -0800 | [diff] [blame] | 412 | SkRect srcRect = SkRect::Make(bounds); |
| 413 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
| 414 | GrClip clip(dstRect); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 415 | drawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 416 | |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 417 | GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 418 | return true; |
| 419 | } |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 420 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 421 | #endif |
bsalomon | b501ecd | 2014-10-14 08:40:57 -0700 | [diff] [blame] | 422 | return false; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 423 | } |
| 424 | |
senorblanco | a544eda | 2015-12-07 07:48:34 -0800 | [diff] [blame] | 425 | bool SkImageFilter::asAColorFilter(SkColorFilter** filterPtr) const { |
| 426 | SkASSERT(nullptr != filterPtr); |
| 427 | if (!this->isColorFilterNode(filterPtr)) { |
| 428 | return false; |
| 429 | } |
| 430 | if (nullptr != this->getInput(0) || (*filterPtr)->affectsTransparentBlack()) { |
| 431 | (*filterPtr)->unref(); |
| 432 | return false; |
| 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
senorblanco | afec27f | 2016-02-16 09:11:18 -0800 | [diff] [blame] | 437 | bool SkImageFilter::applyCropRect(const Context& ctx, const SkIRect& srcBounds, |
| 438 | SkIRect* dstBounds) const { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 439 | SkIRect temp = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection); |
| 440 | fCropRect.applyTo(temp, ctx.ctm(), dstBounds); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 441 | // Intersect against the clip bounds, in case the crop rect has |
| 442 | // grown the bounds beyond the original clip. This can happen for |
| 443 | // example in tiling, where the clip is much smaller than the filtered |
| 444 | // primitive. If we didn't do this, we would be processing the filter |
| 445 | // at the full crop rect size in every tile. |
| 446 | return dstBounds->intersect(ctx.clipBounds()); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 447 | } |
| 448 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 449 | bool SkImageFilter::applyCropRectDeprecated(const Context& ctx, Proxy* proxy, const SkBitmap& src, |
| 450 | SkIPoint* srcOffset, SkIRect* bounds, |
| 451 | SkBitmap* dst) const { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 452 | SkIRect srcBounds; |
| 453 | src.getBounds(&srcBounds); |
| 454 | srcBounds.offset(*srcOffset); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 455 | SkIRect dstBounds = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection); |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 456 | fCropRect.applyTo(dstBounds, ctx.ctm(), bounds); |
| 457 | if (!bounds->intersect(ctx.clipBounds())) { |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
reed | 1891863 | 2015-07-16 13:17:13 -0700 | [diff] [blame] | 460 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 461 | if (srcBounds.contains(*bounds)) { |
| 462 | *dst = src; |
| 463 | return true; |
| 464 | } else { |
| 465 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), bounds->height())); |
| 466 | if (!device) { |
| 467 | return false; |
| 468 | } |
| 469 | SkCanvas canvas(device); |
| 470 | canvas.clear(0x00000000); |
| 471 | canvas.drawBitmap(src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y()); |
| 472 | *srcOffset = SkIPoint::Make(bounds->x(), bounds->y()); |
| 473 | *dst = device->accessBitmap(false); |
| 474 | return true; |
| 475 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 478 | // Return a larger (newWidth x newHeight) copy of 'src' with black padding |
| 479 | // around it. |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 480 | static sk_sp<SkSpecialImage> pad_image(SkSpecialImage* src, |
| 481 | int newWidth, int newHeight, int offX, int offY) { |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 482 | |
| 483 | SkImageInfo info = SkImageInfo::MakeN32Premul(newWidth, newHeight); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 484 | sk_sp<SkSpecialSurface> surf(src->makeSurface(info)); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 485 | if (!surf) { |
| 486 | return nullptr; |
| 487 | } |
| 488 | |
| 489 | SkCanvas* canvas = surf->getCanvas(); |
| 490 | SkASSERT(canvas); |
| 491 | |
| 492 | canvas->clear(0x0); |
| 493 | |
| 494 | src->draw(canvas, offX, offY, nullptr); |
| 495 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 496 | return surf->makeImageSnapshot(); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 497 | } |
| 498 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 499 | sk_sp<SkSpecialImage> SkImageFilter::applyCropRect(const Context& ctx, |
| 500 | SkSpecialImage* src, |
| 501 | SkIPoint* srcOffset, |
| 502 | SkIRect* bounds) const { |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 503 | SkIRect srcBounds; |
| 504 | srcBounds = SkIRect::MakeXYWH(srcOffset->fX, srcOffset->fY, src->width(), src->height()); |
| 505 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 506 | SkIRect dstBounds = this->onFilterNodeBounds(srcBounds, ctx.ctm(), kForward_MapDirection); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 507 | fCropRect.applyTo(dstBounds, ctx.ctm(), bounds); |
| 508 | if (!bounds->intersect(ctx.clipBounds())) { |
| 509 | return nullptr; |
| 510 | } |
| 511 | |
| 512 | if (srcBounds.contains(*bounds)) { |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 513 | return sk_sp<SkSpecialImage>(SkRef(src)); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 514 | } else { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 515 | sk_sp<SkSpecialImage> img(pad_image(src, |
| 516 | bounds->width(), bounds->height(), |
| 517 | srcOffset->x() - bounds->x(), |
| 518 | srcOffset->y() - bounds->y())); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 519 | *srcOffset = SkIPoint::Make(bounds->x(), bounds->y()); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 520 | return img; |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 524 | SkIRect SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 525 | MapDirection direction) const { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 526 | if (fInputCount < 1) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 527 | return src; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 528 | } |
| 529 | |
senorblanco | d8ff5b3 | 2016-01-28 08:23:02 -0800 | [diff] [blame] | 530 | SkIRect totalBounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 531 | for (int i = 0; i < fInputCount; ++i) { |
| 532 | SkImageFilter* filter = this->getInput(i); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 533 | SkIRect rect = filter ? filter->filterBounds(src, ctm, direction) : src; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 534 | if (0 == i) { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 535 | totalBounds = rect; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 536 | } else { |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 537 | totalBounds.join(rect); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 541 | return totalBounds; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 542 | } |
| 543 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 544 | SkIRect SkImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix&, MapDirection) const { |
| 545 | return src; |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | |
| 549 | SkImageFilter::Context SkImageFilter::mapContext(const Context& ctx) const { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 550 | SkIRect clipBounds = this->onFilterNodeBounds(ctx.clipBounds(), ctx.ctm(), |
| 551 | MapDirection::kReverse_MapDirection); |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 552 | return Context(ctx.ctm(), clipBounds, ctx.cache()); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 553 | } |
| 554 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 555 | bool SkImageFilter::asFragmentProcessor(GrFragmentProcessor**, GrTexture*, |
joshualitt | 85ff25e | 2015-07-08 09:10:03 -0700 | [diff] [blame] | 556 | const SkMatrix&, const SkIRect&) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 557 | return false; |
| 558 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 559 | |
senorblanco | 8c874ee | 2015-03-20 06:38:17 -0700 | [diff] [blame] | 560 | SkImageFilter* SkImageFilter::CreateMatrixFilter(const SkMatrix& matrix, |
| 561 | SkFilterQuality filterQuality, |
| 562 | SkImageFilter* input) { |
| 563 | return SkMatrixImageFilter::Create(matrix, filterQuality, input); |
| 564 | } |
| 565 | |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 566 | sk_sp<SkImageFilter> SkImageFilter::makeWithLocalMatrix(const SkMatrix& matrix) const { |
reed | 94dd7a5 | 2015-10-14 07:49:35 -0700 | [diff] [blame] | 567 | // SkLocalMatrixImageFilter takes SkImage* in its factory, but logically that parameter |
| 568 | // is *always* treated as a const ptr. Hence the const-cast here. |
| 569 | // |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 570 | SkImageFilter* nonConstThis = const_cast<SkImageFilter*>(this); |
| 571 | return SkLocalMatrixImageFilter::Make(matrix, sk_ref_sp<SkImageFilter>(nonConstThis)); |
senorblanco | 20311d4 | 2015-10-14 04:53:31 -0700 | [diff] [blame] | 572 | } |
| 573 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 574 | sk_sp<SkSpecialImage> SkImageFilter::filterInput(int index, |
| 575 | SkSpecialImage* src, |
| 576 | const Context& ctx, |
| 577 | SkIPoint* offset) const { |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 578 | SkImageFilter* input = this->getInput(index); |
| 579 | if (!input) { |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 580 | return sk_sp<SkSpecialImage>(SkRef(src)); |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 581 | } |
| 582 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 583 | sk_sp<SkSpecialImage> result(input->filterImage(src, this->mapContext(ctx), offset)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 584 | |
| 585 | #if SK_SUPPORT_GPU |
| 586 | if (src->peekTexture() && result && !result->peekTexture()) { |
| 587 | // Keep the result on the GPU - this is still required for some |
| 588 | // image filters that don't support GPU in all cases |
| 589 | GrContext* context = src->peekTexture()->getContext(); |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 590 | return result->makeTextureImage(src->internal_getProxy(), context); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 591 | } |
| 592 | #endif |
| 593 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 594 | return result; |
robertphillips | eaf086e | 2016-03-07 04:51:10 -0800 | [diff] [blame] | 595 | } |
| 596 | |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 597 | #if SK_SUPPORT_GPU |
| 598 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 599 | bool SkImageFilter::filterInputGPUDeprecated(int index, SkImageFilter::Proxy* proxy, |
| 600 | const SkBitmap& src, const Context& ctx, |
| 601 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco | 9a70b6e | 2015-10-16 11:35:14 -0700 | [diff] [blame] | 602 | SkImageFilter* input = this->getInput(index); |
| 603 | if (!input) { |
| 604 | return true; |
| 605 | } |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 606 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 607 | sk_sp<SkSpecialImage> specialSrc(SkSpecialImage::internal_fromBM(proxy, src)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 608 | if (!specialSrc) { |
senorblanco | 7b87ee7 | 2015-10-26 06:55:47 -0700 | [diff] [blame] | 609 | return false; |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 610 | } |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 611 | |
robertphillips | 2302de9 | 2016-03-24 07:26:32 -0700 | [diff] [blame] | 612 | sk_sp<SkSpecialImage> tmp(input->onFilterImage(specialSrc.get(), |
| 613 | this->mapContext(ctx), |
| 614 | offset)); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 615 | if (!tmp) { |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | if (!tmp->internal_getBM(result)) { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | if (!result->getTexture()) { |
| 624 | GrContext* context = src.getTexture()->getContext(); |
| 625 | |
| 626 | const SkImageInfo info = result->info(); |
| 627 | if (kUnknown_SkColorType == info.colorType()) { |
| 628 | return false; |
| 629 | } |
| 630 | SkAutoTUnref<GrTexture> resultTex( |
| 631 | GrRefCachedBitmapTexture(context, *result, GrTextureParams::ClampNoFilter())); |
| 632 | if (!resultTex) { |
| 633 | return false; |
| 634 | } |
| 635 | result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref(); |
| 636 | } |
| 637 | |
| 638 | return true; |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 639 | } |
| 640 | #endif |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 641 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 642 | namespace { |
| 643 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 644 | class CacheImpl : public SkImageFilter::Cache { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 645 | public: |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 646 | CacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) { } |
| 647 | ~CacheImpl() override { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 648 | SkTDynamicHash<Value, Key>::Iter iter(&fLookup); |
| 649 | |
| 650 | while (!iter.done()) { |
| 651 | Value* v = &*iter; |
| 652 | ++iter; |
| 653 | delete v; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 654 | } |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 655 | } |
| 656 | struct Value { |
| 657 | Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset) |
| 658 | : fKey(key), fBitmap(bitmap), fOffset(offset) {} |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 659 | Value(const Key& key, SkSpecialImage* image, const SkIPoint& offset) |
| 660 | : fKey(key), fImage(SkRef(image)), fOffset(offset) {} |
| 661 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 662 | Key fKey; |
| 663 | SkBitmap fBitmap; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 664 | SkAutoTUnref<SkSpecialImage> fImage; |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 665 | SkIPoint fOffset; |
| 666 | static const Key& GetKey(const Value& v) { |
| 667 | return v.fKey; |
| 668 | } |
| 669 | static uint32_t Hash(const Key& key) { |
| 670 | return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); |
| 671 | } |
| 672 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 673 | }; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 674 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 675 | bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const override { |
| 676 | SkAutoMutexAcquire mutex(fMutex); |
| 677 | if (Value* v = fLookup.find(key)) { |
| 678 | *result = v->fBitmap; |
| 679 | *offset = v->fOffset; |
| 680 | if (v != fLRU.head()) { |
| 681 | fLRU.remove(v); |
| 682 | fLRU.addToHead(v); |
| 683 | } |
| 684 | return true; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 685 | } |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 686 | return false; |
| 687 | } |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 688 | |
| 689 | SkSpecialImage* get(const Key& key, SkIPoint* offset) const override { |
| 690 | SkAutoMutexAcquire mutex(fMutex); |
| 691 | if (Value* v = fLookup.find(key)) { |
| 692 | *offset = v->fOffset; |
| 693 | if (v != fLRU.head()) { |
| 694 | fLRU.remove(v); |
| 695 | fLRU.addToHead(v); |
| 696 | } |
| 697 | return v->fImage; |
| 698 | } |
| 699 | return nullptr; |
| 700 | } |
| 701 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 702 | void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) override { |
| 703 | SkAutoMutexAcquire mutex(fMutex); |
| 704 | if (Value* v = fLookup.find(key)) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 705 | this->removeInternal(v); |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 706 | } |
| 707 | Value* v = new Value(key, result, offset); |
| 708 | fLookup.add(v); |
| 709 | fLRU.addToHead(v); |
| 710 | fCurrentBytes += result.getSize(); |
| 711 | while (fCurrentBytes > fMaxBytes) { |
| 712 | Value* tail = fLRU.tail(); |
| 713 | SkASSERT(tail); |
| 714 | if (tail == v) { |
| 715 | break; |
| 716 | } |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 717 | this->removeInternal(tail); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | void set(const Key& key, SkSpecialImage* image, const SkIPoint& offset) override { |
| 722 | SkAutoMutexAcquire mutex(fMutex); |
| 723 | if (Value* v = fLookup.find(key)) { |
| 724 | this->removeInternal(v); |
| 725 | } |
| 726 | Value* v = new Value(key, image, offset); |
| 727 | fLookup.add(v); |
| 728 | fLRU.addToHead(v); |
| 729 | fCurrentBytes += image->getSize(); |
| 730 | while (fCurrentBytes > fMaxBytes) { |
| 731 | Value* tail = fLRU.tail(); |
| 732 | SkASSERT(tail); |
| 733 | if (tail == v) { |
| 734 | break; |
| 735 | } |
| 736 | this->removeInternal(tail); |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 737 | } |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 738 | } |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 739 | |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 740 | void purge() override { |
| 741 | SkAutoMutexAcquire mutex(fMutex); |
| 742 | while (fCurrentBytes > 0) { |
| 743 | Value* tail = fLRU.tail(); |
| 744 | SkASSERT(tail); |
| 745 | this->removeInternal(tail); |
| 746 | } |
| 747 | } |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 748 | |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 749 | void purgeByKeys(const Key keys[], int count) override { |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 750 | SkAutoMutexAcquire mutex(fMutex); |
xidachen | 2352696 | 2016-02-01 05:27:16 -0800 | [diff] [blame] | 751 | for (int i = 0; i < count; i++) { |
| 752 | if (Value* v = fLookup.find(keys[i])) { |
| 753 | this->removeInternal(v); |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 754 | } |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
mtklein | 75135d8 | 2015-09-29 12:15:52 -0700 | [diff] [blame] | 758 | private: |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 759 | void removeInternal(Value* v) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 760 | if (v->fImage) { |
| 761 | fCurrentBytes -= v->fImage->getSize(); |
| 762 | } else { |
| 763 | fCurrentBytes -= v->fBitmap.getSize(); |
| 764 | } |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 765 | fLRU.remove(v); |
| 766 | fLookup.remove(v->fKey); |
| 767 | delete v; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 768 | } |
| 769 | private: |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 770 | SkTDynamicHash<Value, Key> fLookup; |
xidachen | 6277691 | 2015-12-21 07:29:03 -0800 | [diff] [blame] | 771 | mutable SkTInternalLList<Value> fLRU; |
| 772 | size_t fMaxBytes; |
| 773 | size_t fCurrentBytes; |
| 774 | mutable SkMutex fMutex; |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 775 | }; |
| 776 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 777 | } // namespace |
| 778 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 779 | SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 780 | return new CacheImpl(maxBytes); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 781 | } |
| 782 | |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 783 | SK_DECLARE_STATIC_ONCE_PTR(SkImageFilter::Cache, cache); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 784 | SkImageFilter::Cache* SkImageFilter::Cache::Get() { |
mtklein | 242397a | 2015-09-29 12:17:08 -0700 | [diff] [blame] | 785 | return cache.get([]{ return SkImageFilter::Cache::Create(kDefaultCacheSize); }); |
| 786 | } |
| 787 | |
| 788 | void SkImageFilter::PurgeCache() { |
| 789 | Cache::Get()->purge(); |
reed | 67ca2a9 | 2015-05-20 13:22:58 -0700 | [diff] [blame] | 790 | } |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 791 | |
| 792 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 793 | |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 794 | SkBaseDevice* SkImageFilter::DeviceProxy::createDevice(int w, int h, TileUsage usage) { |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 795 | SkBaseDevice::CreateInfo cinfo(SkImageInfo::MakeN32Premul(w, h), |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 796 | kPossible_TileUsage == usage ? SkBaseDevice::kPossible_TileUsage |
| 797 | : SkBaseDevice::kNever_TileUsage, |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 798 | kUnknown_SkPixelGeometry, |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 799 | false, /* preserveLCDText */ |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 800 | true /*forImageFilter*/); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 801 | SkBaseDevice* dev = fDevice->onCreateDevice(cinfo, nullptr); |
| 802 | if (nullptr == dev) { |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 803 | const SkSurfaceProps surfaceProps(fDevice->fSurfaceProps.flags(), |
| 804 | kUnknown_SkPixelGeometry); |
| 805 | dev = SkBitmapDevice::Create(cinfo.fInfo, surfaceProps); |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 806 | } |
| 807 | return dev; |
| 808 | } |
| 809 | |
reed | 88d064d | 2015-10-12 11:30:02 -0700 | [diff] [blame] | 810 | bool SkImageFilter::DeviceProxy::filterImage(const SkImageFilter* filter, const SkBitmap& src, |
reed | 2c55d7b | 2015-06-09 08:18:39 -0700 | [diff] [blame] | 811 | const SkImageFilter::Context& ctx, |
| 812 | SkBitmap* result, SkIPoint* offset) { |
| 813 | return fDevice->filterImage(filter, src, ctx, result, offset); |
| 814 | } |