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" |
| 11 | #include "SkFlattenableBuffers.h" |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 13 | #include "SkValidationUtils.h" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
| 15 | #include "GrContext.h" |
| 16 | #include "GrTexture.h" |
| 17 | #include "SkImageFilterUtils.h" |
| 18 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 19 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 20 | SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 21 | : fInputCount(inputCount), |
| 22 | fInputs(new SkImageFilter*[inputCount]), |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 23 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 24 | for (int i = 0; i < inputCount; ++i) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 25 | fInputs[i] = inputs[i]; |
| 26 | SkSafeRef(fInputs[i]); |
| 27 | } |
| 28 | } |
| 29 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 30 | SkImageFilter::SkImageFilter(SkImageFilter* input, const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 31 | : fInputCount(1), |
| 32 | fInputs(new SkImageFilter*[1]), |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 33 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 34 | fInputs[0] = input; |
| 35 | SkSafeRef(fInputs[0]); |
| 36 | } |
| 37 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 38 | SkImageFilter::SkImageFilter(SkImageFilter* input1, SkImageFilter* input2, const CropRect* cropRect) |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 39 | : fInputCount(2), fInputs(new SkImageFilter*[2]), |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 40 | fCropRect(cropRect ? *cropRect : CropRect(SkRect(), 0x0)) { |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 41 | fInputs[0] = input1; |
| 42 | fInputs[1] = input2; |
| 43 | SkSafeRef(fInputs[0]); |
| 44 | SkSafeRef(fInputs[1]); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | SkImageFilter::~SkImageFilter() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 48 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 49 | SkSafeUnref(fInputs[i]); |
| 50 | } |
| 51 | delete[] fInputs; |
| 52 | } |
| 53 | |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 54 | SkImageFilter::SkImageFilter(int inputCount, SkFlattenableReadBuffer& buffer) { |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 55 | fInputCount = buffer.readInt(); |
commit-bot@chromium.org | c84728d | 2013-12-04 20:07:47 +0000 | [diff] [blame] | 56 | if (buffer.validate((fInputCount >= 0) && ((inputCount < 0) || (fInputCount == inputCount)))) { |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 57 | fInputs = new SkImageFilter*[fInputCount]; |
| 58 | for (int i = 0; i < fInputCount; i++) { |
| 59 | if (buffer.readBool()) { |
| 60 | fInputs[i] = buffer.readImageFilter(); |
| 61 | } else { |
| 62 | fInputs[i] = NULL; |
| 63 | } |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 64 | } |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 65 | SkRect rect; |
| 66 | buffer.readRect(&rect); |
| 67 | if (buffer.validate(SkIsValidRect(rect))) { |
| 68 | uint32_t flags = buffer.readUInt(); |
| 69 | fCropRect = CropRect(rect, flags); |
| 70 | } |
| 71 | } else { |
| 72 | fInputCount = 0; |
| 73 | fInputs = NULL; |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | void SkImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 78 | buffer.writeInt(fInputCount); |
| 79 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 80 | SkImageFilter* input = getInput(i); |
| 81 | buffer.writeBool(input != NULL); |
| 82 | if (input != NULL) { |
| 83 | buffer.writeFlattenable(input); |
| 84 | } |
| 85 | } |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 86 | buffer.writeRect(fCropRect.rect()); |
| 87 | buffer.writeUInt(fCropRect.flags()); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 90 | bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src, |
| 91 | const SkMatrix& ctm, |
| 92 | SkBitmap* result, SkIPoint* loc) { |
| 93 | SkASSERT(result); |
| 94 | SkASSERT(loc); |
| 95 | /* |
| 96 | * Give the proxy first shot at the filter. If it returns false, ask |
| 97 | * the filter to do it. |
| 98 | */ |
| 99 | return (proxy && proxy->filterImage(this, src, ctm, result, loc)) || |
| 100 | this->onFilterImage(proxy, src, ctm, result, loc); |
| 101 | } |
| 102 | |
| 103 | bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 104 | SkIRect* dst) { |
| 105 | SkASSERT(&src); |
| 106 | SkASSERT(dst); |
| 107 | return this->onFilterBounds(src, ctm, dst); |
| 108 | } |
| 109 | |
| 110 | bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&, |
| 111 | SkBitmap*, SkIPoint*) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool SkImageFilter::canFilterImageGPU() const { |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 116 | return this->asNewEffect(NULL, NULL, SkMatrix::I(), SkIRect()); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 119 | bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm, |
| 120 | SkBitmap* result, SkIPoint* offset) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 121 | #if SK_SUPPORT_GPU |
| 122 | SkBitmap input; |
| 123 | SkASSERT(fInputCount == 1); |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 124 | if (!SkImageFilterUtils::GetInputResultGPU(this->getInput(0), proxy, src, ctm, &input, offset)) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 125 | return false; |
| 126 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 127 | GrTexture* srcTexture = input.getTexture(); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 128 | SkIRect bounds; |
| 129 | src.getBounds(&bounds); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 130 | if (!this->applyCropRect(&bounds, ctm)) { |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | SkRect srcRect = SkRect::Make(bounds); |
| 134 | SkRect dstRect = SkRect::MakeWH(srcRect.width(), srcRect.height()); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 135 | GrContext* context = srcTexture->getContext(); |
| 136 | |
| 137 | GrTextureDesc desc; |
| 138 | desc.fFlags = kRenderTarget_GrTextureFlagBit, |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 139 | desc.fWidth = bounds.width(); |
| 140 | desc.fHeight = bounds.height(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 141 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 142 | |
| 143 | GrAutoScratchTexture dst(context, desc); |
| 144 | GrContext::AutoMatrix am; |
| 145 | am.setIdentity(context); |
| 146 | GrContext::AutoRenderTarget art(context, dst.texture()->asRenderTarget()); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 147 | GrContext::AutoClip acs(context, dstRect); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 148 | GrEffectRef* effect; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 149 | SkMatrix matrix(ctm); |
| 150 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 151 | this->asNewEffect(&effect, srcTexture, matrix, bounds); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 152 | SkASSERT(effect); |
| 153 | SkAutoUnref effectRef(effect); |
| 154 | GrPaint paint; |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 155 | paint.addColorEffect(effect); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 156 | context->drawRectToRect(paint, dstRect, srcRect); |
| 157 | |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 158 | SkAutoTUnref<GrTexture> resultTex(dst.detach()); |
senorblanco@chromium.org | 4e16bb2 | 2013-07-26 00:10:07 +0000 | [diff] [blame] | 159 | SkImageFilterUtils::WrapTexture(resultTex, bounds.width(), bounds.height(), result); |
| 160 | offset->fX += bounds.left(); |
| 161 | offset->fY += bounds.top(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 162 | return true; |
| 163 | #else |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 164 | return false; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 165 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 166 | } |
| 167 | |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 168 | bool SkImageFilter::applyCropRect(SkIRect* rect, const SkMatrix& matrix) const { |
| 169 | SkRect cropRect; |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 170 | matrix.mapRect(&cropRect, fCropRect.rect()); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 171 | SkIRect cropRectI; |
| 172 | cropRect.roundOut(&cropRectI); |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 173 | uint32_t flags = fCropRect.flags(); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 174 | // If the original crop rect edges were unset, max out the new crop edges |
senorblanco@chromium.org | 3f1f2a3 | 2013-10-16 18:07:48 +0000 | [diff] [blame] | 175 | if (!(flags & CropRect::kHasLeft_CropEdge)) cropRectI.fLeft = SK_MinS32; |
| 176 | if (!(flags & CropRect::kHasTop_CropEdge)) cropRectI.fTop = SK_MinS32; |
| 177 | if (!(flags & CropRect::kHasRight_CropEdge)) cropRectI.fRight = SK_MaxS32; |
| 178 | if (!(flags & CropRect::kHasBottom_CropEdge)) cropRectI.fBottom = SK_MaxS32; |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 179 | return rect->intersect(cropRectI); |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 182 | bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 183 | SkIRect* dst) { |
| 184 | *dst = src; |
| 185 | return true; |
| 186 | } |
| 187 | |
senorblanco@chromium.org | 7938bae | 2013-10-18 20:08:14 +0000 | [diff] [blame] | 188 | bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*, const SkMatrix&, const SkIRect&) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 191 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 192 | bool SkImageFilter::asColorFilter(SkColorFilter**) const { |
| 193 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 194 | } |