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" |
mtklein | 19fcc74 | 2014-07-11 08:42:17 -0700 | [diff] [blame] | 11 | #include "SkChecksum.h" |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 13 | #include "SkLazyPtr.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| 15 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 16 | #include "SkRect.h" |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 17 | #include "SkTDynamicHash.h" |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 18 | #include "SkTInternalLList.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 19 | #include "SkValidationUtils.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 22 | #include "SkGrPixelRef.h" |
| 23 | #include "SkGr.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 24 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 25 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 26 | enum { kDefaultCacheSize = 128 * 1024 * 1024 }; |
| 27 | |
| 28 | static int32_t next_image_filter_unique_id() { |
| 29 | static int32_t gImageFilterUniqueID; |
| 30 | |
| 31 | // Never return 0. |
| 32 | int32_t id; |
| 33 | do { |
| 34 | id = sk_atomic_inc(&gImageFilterUniqueID) + 1; |
| 35 | } while (0 == id); |
| 36 | return id; |
| 37 | } |
| 38 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 39 | struct SkImageFilter::Cache::Key { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 40 | Key(const uint32_t uniqueID, const SkMatrix& matrix, const SkIRect& clipBounds, uint32_t srcGenID) |
| 41 | : fUniqueID(uniqueID), fMatrix(matrix), fClipBounds(clipBounds), fSrcGenID(srcGenID) { |
| 42 | // Assert that Key is tightly-packed, since it is hashed. |
| 43 | SK_COMPILE_ASSERT(sizeof(Key) == sizeof(uint32_t) + sizeof(SkMatrix) + sizeof(SkIRect) + |
| 44 | sizeof(uint32_t), image_filter_key_tight_packing); |
| 45 | fMatrix.getType(); // force initialization of type, so hashes match |
| 46 | } |
| 47 | uint32_t fUniqueID; |
| 48 | SkMatrix fMatrix; |
| 49 | SkIRect fClipBounds; |
| 50 | uint32_t fSrcGenID; |
| 51 | bool operator==(const Key& other) const { |
| 52 | return fUniqueID == other.fUniqueID |
| 53 | && fMatrix == other.fMatrix |
| 54 | && fClipBounds == other.fClipBounds |
| 55 | && fSrcGenID == other.fSrcGenID; |
| 56 | } |
| 57 | }; |
| 58 | |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 59 | SkImageFilter::Common::~Common() { |
| 60 | for (int i = 0; i < fInputs.count(); ++i) { |
| 61 | SkSafeUnref(fInputs[i]); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void SkImageFilter::Common::allocInputs(int count) { |
| 66 | const size_t size = count * sizeof(SkImageFilter*); |
| 67 | fInputs.reset(count); |
| 68 | sk_bzero(fInputs.get(), size); |
| 69 | } |
| 70 | |
| 71 | void SkImageFilter::Common::detachInputs(SkImageFilter** inputs) { |
| 72 | const size_t size = fInputs.count() * sizeof(SkImageFilter*); |
| 73 | memcpy(inputs, fInputs.get(), size); |
| 74 | sk_bzero(fInputs.get(), size); |
| 75 | } |
| 76 | |
| 77 | bool SkImageFilter::Common::unflatten(SkReadBuffer& buffer, int expectedCount) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 78 | const int count = buffer.readInt(); |
| 79 | if (!buffer.validate(count >= 0)) { |
| 80 | return false; |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 81 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 82 | if (!buffer.validate(expectedCount < 0 || count == expectedCount)) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
| 86 | this->allocInputs(count); |
| 87 | for (int i = 0; i < count; i++) { |
| 88 | if (buffer.readBool()) { |
| 89 | fInputs[i] = buffer.readImageFilter(); |
| 90 | } |
| 91 | if (!buffer.isValid()) { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | SkRect rect; |
| 96 | buffer.readRect(&rect); |
| 97 | if (!buffer.isValid() || !buffer.validate(SkIsValidRect(rect))) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | uint32_t flags = buffer.readUInt(); |
| 102 | fCropRect = CropRect(rect, flags); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 103 | if (buffer.isVersionLT(SkReadBuffer::kImageFilterUniqueID_Version)) { |
| 104 | fUniqueID = next_image_filter_unique_id(); |
| 105 | } else { |
| 106 | fUniqueID = buffer.readUInt(); |
| 107 | } |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 108 | return buffer.isValid(); |
| 109 | } |
| 110 | |
| 111 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 112 | |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 113 | SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect, uint32_t uniqueID) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 114 | : fInputCount(inputCount), |
| 115 | fInputs(new SkImageFilter*[inputCount]), |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 116 | fUsesSrcInput(false), |
| 117 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)), |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 118 | fUniqueID(uniqueID ? uniqueID : next_image_filter_unique_id()) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 119 | for (int i = 0; i < inputCount; ++i) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 120 | if (NULL == inputs[i] || inputs[i]->usesSrcInput()) { |
| 121 | fUsesSrcInput = true; |
| 122 | } |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 123 | fInputs[i] = inputs[i]; |
| 124 | SkSafeRef(fInputs[i]); |
| 125 | } |
| 126 | } |
| 127 | |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 128 | SkImageFilter::~SkImageFilter() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 129 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 130 | SkSafeUnref(fInputs[i]); |
| 131 | } |
| 132 | delete[] fInputs; |
| 133 | } |
| 134 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 135 | SkImageFilter::SkImageFilter(int inputCount, SkReadBuffer& buffer) |
| 136 | : fUsesSrcInput(false) { |
reed | b959ec7 | 2014-07-17 07:03:09 -0700 | [diff] [blame] | 137 | Common common; |
| 138 | if (common.unflatten(buffer, inputCount)) { |
| 139 | fCropRect = common.cropRect(); |
| 140 | fInputCount = common.inputCount(); |
| 141 | fInputs = SkNEW_ARRAY(SkImageFilter*, fInputCount); |
| 142 | common.detachInputs(fInputs); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 143 | for (int i = 0; i < fInputCount; ++i) { |
| 144 | if (NULL == fInputs[i] || fInputs[i]->usesSrcInput()) { |
| 145 | fUsesSrcInput = true; |
| 146 | } |
| 147 | } |
| 148 | fUniqueID = buffer.isCrossProcess() ? next_image_filter_unique_id() : common.uniqueID(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 149 | } else { |
| 150 | fInputCount = 0; |
| 151 | fInputs = NULL; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 155 | void SkImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 156 | buffer.writeInt(fInputCount); |
| 157 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 158 | SkImageFilter* input = getInput(i); |
| 159 | buffer.writeBool(input != NULL); |
| 160 | if (input != NULL) { |
| 161 | buffer.writeFlattenable(input); |
| 162 | } |
| 163 | } |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 164 | buffer.writeRect(fCropRect.rect()); |
| 165 | buffer.writeUInt(fCropRect.flags()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 166 | buffer.writeUInt(fUniqueID); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 169 | bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 170 | const Context& context, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 171 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 172 | SkASSERT(result); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 173 | SkASSERT(offset); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 174 | uint32_t srcGenID = fUsesSrcInput ? src.getGenerationID() : 0; |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 175 | Cache::Key key(fUniqueID, context.ctm(), context.clipBounds(), srcGenID); |
| 176 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 177 | if (context.cache()->get(key, result, offset)) { |
| 178 | return true; |
| 179 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 180 | } |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 181 | /* |
| 182 | * Give the proxy first shot at the filter. If it returns false, ask |
| 183 | * the filter to do it. |
| 184 | */ |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 185 | if ((proxy && proxy->filterImage(this, src, context, result, offset)) || |
| 186 | this->onFilterImage(proxy, src, context, result, offset)) { |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 187 | if (context.cache()) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 188 | context.cache()->set(key, *result, *offset); |
| 189 | } |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | return false; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 196 | SkIRect* dst) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 197 | SkASSERT(&src); |
| 198 | SkASSERT(dst); |
| 199 | return this->onFilterBounds(src, ctm, dst); |
| 200 | } |
| 201 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 202 | void SkImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
| 203 | if (0 == fInputCount) { |
| 204 | *dst = src; |
| 205 | return; |
| 206 | } |
| 207 | if (this->getInput(0)) { |
| 208 | this->getInput(0)->computeFastBounds(src, dst); |
| 209 | } else { |
| 210 | *dst = src; |
| 211 | } |
| 212 | for (int i = 1; i < fInputCount; i++) { |
| 213 | SkImageFilter* input = this->getInput(i); |
| 214 | if (input) { |
| 215 | SkRect bounds; |
| 216 | input->computeFastBounds(src, &bounds); |
| 217 | dst->join(bounds); |
| 218 | } else { |
| 219 | dst->join(src); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 224 | bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 225 | SkBitmap*, SkIPoint*) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | bool SkImageFilter::canFilterImageGPU() const { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 230 | return this->asNewEffect(NULL, NULL, SkMatrix::I(), SkIRect()); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 231 | } |
| 232 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 233 | bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 234 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 235 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 236 | SkBitmap input = src; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 237 | SkASSERT(fInputCount == 1); |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 238 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 239 | if (this->getInput(0) && |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 240 | !this->getInput(0)->getInputResultGPU(proxy, src, ctx, &input, &srcOffset)) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 243 | GrTexture* srcTexture = input.getTexture(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 244 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 245 | if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | SkRect srcRect = SkRect::Make(bounds); |
| 249 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 250 | GrContext* context = srcTexture->getContext(); |
| 251 | |
| 252 | GrTextureDesc desc; |
| 253 | desc.fFlags = kRenderTarget_GrTextureFlagBit, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 254 | desc.fWidth = bounds.width(); |
| 255 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 256 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 257 | |
| 258 | GrAutoScratchTexture dst(context, desc); |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 259 | if (NULL == dst.texture()) { |
| 260 | return false; |
| 261 | } |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 262 | GrContext::AutoMatrix am; |
| 263 | am.setIdentity(context); |
| 264 | GrContext::AutoRenderTarget art(context, dst.texture()->asRenderTarget()); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 265 | GrContext::AutoClip acs(context, dstRect); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 266 | GrEffect* effect; |
senorblanco@chromium.org | aba651c | 2014-02-03 22:22:16 +0000 | [diff] [blame] | 267 | offset->fX = bounds.left(); |
| 268 | offset->fY = bounds.top(); |
| 269 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 270 | SkMatrix matrix(ctx.ctm()); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 271 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 272 | this->asNewEffect(&effect, srcTexture, matrix, bounds); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 273 | SkASSERT(effect); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 274 | GrPaint paint; |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 275 | paint.addColorEffect(effect)->unref(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 276 | context->drawRectToRect(paint, dstRect, srcRect); |
| 277 | |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 278 | SkAutoTUnref<GrTexture> resultTex(dst.detach()); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 279 | WrapTexture(resultTex, bounds.width(), bounds.height(), result); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 280 | return true; |
| 281 | #else |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 282 | return false; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 283 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 284 | } |
| 285 | |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 286 | bool SkImageFilter::applyCropRect(const Context& ctx, const SkBitmap& src, |
| 287 | const SkIPoint& srcOffset, SkIRect* bounds) const { |
| 288 | SkIRect srcBounds; |
| 289 | src.getBounds(&srcBounds); |
| 290 | srcBounds.offset(srcOffset); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 291 | SkRect cropRect; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 292 | ctx.ctm().mapRect(&cropRect, fCropRect.rect()); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 293 | SkIRect cropRectI; |
| 294 | cropRect.roundOut(&cropRectI); |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 295 | uint32_t flags = fCropRect.flags(); |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 296 | if (flags & CropRect::kHasLeft_CropEdge) srcBounds.fLeft = cropRectI.fLeft; |
| 297 | if (flags & CropRect::kHasTop_CropEdge) srcBounds.fTop = cropRectI.fTop; |
| 298 | if (flags & CropRect::kHasRight_CropEdge) srcBounds.fRight = cropRectI.fRight; |
| 299 | if (flags & CropRect::kHasBottom_CropEdge) srcBounds.fBottom = cropRectI.fBottom; |
| 300 | if (!srcBounds.intersect(ctx.clipBounds())) { |
| 301 | return false; |
| 302 | } |
| 303 | *bounds = srcBounds; |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | bool SkImageFilter::applyCropRect(const Context& ctx, Proxy* proxy, const SkBitmap& src, |
| 308 | SkIPoint* srcOffset, SkIRect* bounds, SkBitmap* dst) const { |
| 309 | SkIRect srcBounds; |
| 310 | src.getBounds(&srcBounds); |
| 311 | srcBounds.offset(*srcOffset); |
| 312 | SkRect cropRect; |
| 313 | ctx.ctm().mapRect(&cropRect, fCropRect.rect()); |
| 314 | SkIRect cropRectI; |
| 315 | cropRect.roundOut(&cropRectI); |
| 316 | uint32_t flags = fCropRect.flags(); |
| 317 | *bounds = srcBounds; |
| 318 | if (flags & CropRect::kHasLeft_CropEdge) bounds->fLeft = cropRectI.fLeft; |
| 319 | if (flags & CropRect::kHasTop_CropEdge) bounds->fTop = cropRectI.fTop; |
| 320 | if (flags & CropRect::kHasRight_CropEdge) bounds->fRight = cropRectI.fRight; |
| 321 | if (flags & CropRect::kHasBottom_CropEdge) bounds->fBottom = cropRectI.fBottom; |
| 322 | if (!bounds->intersect(ctx.clipBounds())) { |
| 323 | return false; |
| 324 | } |
| 325 | if (srcBounds.contains(*bounds)) { |
| 326 | *dst = src; |
| 327 | return true; |
| 328 | } else { |
| 329 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds->width(), bounds->height())); |
| 330 | if (!device) { |
| 331 | return false; |
| 332 | } |
| 333 | SkCanvas canvas(device); |
| 334 | canvas.clear(0x00000000); |
| 335 | canvas.drawBitmap(src, srcOffset->x() - bounds->x(), srcOffset->y() - bounds->y()); |
| 336 | *srcOffset = SkIPoint::Make(bounds->x(), bounds->y()); |
| 337 | *dst = device->accessBitmap(false); |
| 338 | return true; |
| 339 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 340 | } |
| 341 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 342 | bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 343 | SkIRect* dst) const { |
| 344 | if (fInputCount < 1) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | SkIRect bounds; |
| 349 | for (int i = 0; i < fInputCount; ++i) { |
| 350 | SkImageFilter* filter = this->getInput(i); |
| 351 | SkIRect rect = src; |
| 352 | if (filter && !filter->filterBounds(src, ctm, &rect)) { |
| 353 | return false; |
| 354 | } |
| 355 | if (0 == i) { |
| 356 | bounds = rect; |
| 357 | } else { |
| 358 | bounds.join(rect); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // don't modify dst until now, so we don't accidentally change it in the |
| 363 | // loop, but then return false on the next filter. |
| 364 | *dst = bounds; |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 365 | return true; |
| 366 | } |
| 367 | |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 368 | bool SkImageFilter::asNewEffect(GrEffect**, GrTexture*, const SkMatrix&, const SkIRect&) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 369 | return false; |
| 370 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 371 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 372 | bool SkImageFilter::asColorFilter(SkColorFilter**) const { |
| 373 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 374 | } |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 375 | |
| 376 | #if SK_SUPPORT_GPU |
| 377 | |
| 378 | void SkImageFilter::WrapTexture(GrTexture* texture, int width, int height, SkBitmap* result) { |
| 379 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 380 | result->setInfo(info); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 381 | result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref(); |
| 382 | } |
| 383 | |
| 384 | bool SkImageFilter::getInputResultGPU(SkImageFilter::Proxy* proxy, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 385 | const SkBitmap& src, const Context& ctx, |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 386 | SkBitmap* result, SkIPoint* offset) const { |
| 387 | // Ensure that GrContext calls under filterImage and filterImageGPU below will see an identity |
| 388 | // matrix with no clip and that the matrix, clip, and render target set before this function was |
| 389 | // called are restored before we return to the caller. |
| 390 | GrContext* context = src.getTexture()->getContext(); |
| 391 | GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 392 | if (this->canFilterImageGPU()) { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 393 | return this->filterImageGPU(proxy, src, ctx, result, offset); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 394 | } else { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 395 | if (this->filterImage(proxy, src, ctx, result, offset)) { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 396 | if (!result->getTexture()) { |
commit-bot@chromium.org | 466f5f3 | 2014-05-27 21:30:37 +0000 | [diff] [blame] | 397 | const SkImageInfo info = result->info(); |
| 398 | if (kUnknown_SkColorType == info.colorType()) { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | GrTexture* resultTex = GrLockAndRefCachedBitmapTexture(context, *result, NULL); |
| 402 | result->setPixelRef(new SkGrPixelRef(info, resultTex))->unref(); |
| 403 | GrUnlockAndUnrefCachedBitmapTexture(resultTex); |
| 404 | } |
| 405 | return true; |
| 406 | } else { |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | #endif |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 412 | |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 413 | namespace { |
| 414 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 415 | class CacheImpl : public SkImageFilter::Cache { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 416 | public: |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 417 | CacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 418 | } |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 419 | virtual ~CacheImpl() { |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 420 | SkTDynamicHash<Value, Key>::Iter iter(&fLookup); |
| 421 | |
| 422 | while (!iter.done()) { |
| 423 | Value* v = &*iter; |
| 424 | ++iter; |
| 425 | delete v; |
| 426 | } |
| 427 | } |
| 428 | struct Value { |
| 429 | Value(const Key& key, const SkBitmap& bitmap, const SkIPoint& offset) |
| 430 | : fKey(key), fBitmap(bitmap), fOffset(offset) {} |
| 431 | Key fKey; |
| 432 | SkBitmap fBitmap; |
| 433 | SkIPoint fOffset; |
| 434 | static const Key& GetKey(const Value& v) { |
| 435 | return v.fKey; |
| 436 | } |
| 437 | static uint32_t Hash(const Key& key) { |
| 438 | return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key), sizeof(Key)); |
| 439 | } |
| 440 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); |
| 441 | }; |
| 442 | virtual bool get(const Key& key, SkBitmap* result, SkIPoint* offset) const { |
| 443 | SkAutoMutexAcquire mutex(fMutex); |
| 444 | if (Value* v = fLookup.find(key)) { |
| 445 | *result = v->fBitmap; |
| 446 | *offset = v->fOffset; |
| 447 | if (v != fLRU.head()) { |
| 448 | fLRU.remove(v); |
| 449 | fLRU.addToHead(v); |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | virtual void set(const Key& key, const SkBitmap& result, const SkIPoint& offset) { |
| 456 | SkAutoMutexAcquire mutex(fMutex); |
| 457 | if (Value* v = fLookup.find(key)) { |
| 458 | removeInternal(v); |
| 459 | } |
| 460 | Value* v = new Value(key, result, offset); |
| 461 | fLookup.add(v); |
| 462 | fLRU.addToHead(v); |
| 463 | fCurrentBytes += result.getSize(); |
| 464 | while (fCurrentBytes > fMaxBytes) { |
| 465 | Value* tail = fLRU.tail(); |
| 466 | SkASSERT(tail); |
| 467 | if (tail == v) { |
| 468 | break; |
| 469 | } |
| 470 | removeInternal(tail); |
| 471 | } |
| 472 | } |
| 473 | private: |
| 474 | void removeInternal(Value* v) { |
| 475 | fCurrentBytes -= v->fBitmap.getSize(); |
| 476 | fLRU.remove(v); |
| 477 | fLookup.remove(v->fKey); |
| 478 | delete v; |
| 479 | } |
| 480 | private: |
| 481 | SkTDynamicHash<Value, Key> fLookup; |
| 482 | mutable SkTInternalLList<Value> fLRU; |
| 483 | size_t fMaxBytes; |
| 484 | size_t fCurrentBytes; |
| 485 | mutable SkMutex fMutex; |
| 486 | }; |
| 487 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 488 | SkImageFilter::Cache* CreateCache() { |
| 489 | return SkImageFilter::Cache::Create(kDefaultCacheSize); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | } // namespace |
| 493 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 494 | SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) { |
| 495 | return SkNEW_ARGS(CacheImpl, (maxBytes)); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 496 | } |
| 497 | |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 498 | SkImageFilter::Cache* SkImageFilter::Cache::Get() { |
| 499 | SK_DECLARE_STATIC_LAZY_PTR(SkImageFilter::Cache, cache, CreateCache); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 500 | return cache.get(); |
| 501 | } |