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" |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 13 | #if SK_SUPPORT_GPU |
| 14 | #include "GrContext.h" |
| 15 | #include "GrTexture.h" |
| 16 | #include "SkImageFilterUtils.h" |
| 17 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 18 | |
| 19 | SK_DEFINE_INST_COUNT(SkImageFilter) |
| 20 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 21 | SkImageFilter::SkImageFilter(int inputCount, SkImageFilter** inputs, const SkIRect* cropRect) |
| 22 | : fInputCount(inputCount), |
| 23 | fInputs(new SkImageFilter*[inputCount]), |
| 24 | fCropRect(cropRect ? *cropRect : SkIRect::MakeLargest()) { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 25 | for (int i = 0; i < inputCount; ++i) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 26 | fInputs[i] = inputs[i]; |
| 27 | SkSafeRef(fInputs[i]); |
| 28 | } |
| 29 | } |
| 30 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 31 | SkImageFilter::SkImageFilter(SkImageFilter* input, const SkIRect* cropRect) |
| 32 | : fInputCount(1), |
| 33 | fInputs(new SkImageFilter*[1]), |
| 34 | fCropRect(cropRect ? *cropRect : SkIRect::MakeLargest()) { |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 35 | fInputs[0] = input; |
| 36 | SkSafeRef(fInputs[0]); |
| 37 | } |
| 38 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 39 | SkImageFilter::SkImageFilter(SkImageFilter* input1, SkImageFilter* input2, const SkIRect* cropRect) |
| 40 | : fInputCount(2), fInputs(new SkImageFilter*[2]), |
| 41 | fCropRect(cropRect ? *cropRect : SkIRect::MakeLargest()) { |
senorblanco@chromium.org | c2e8cef | 2012-10-22 15:07:14 +0000 | [diff] [blame] | 42 | fInputs[0] = input1; |
| 43 | fInputs[1] = input2; |
| 44 | SkSafeRef(fInputs[0]); |
| 45 | SkSafeRef(fInputs[1]); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | SkImageFilter::~SkImageFilter() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 49 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 50 | SkSafeUnref(fInputs[i]); |
| 51 | } |
| 52 | delete[] fInputs; |
| 53 | } |
| 54 | |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 55 | SkImageFilter::SkImageFilter(SkFlattenableReadBuffer& buffer) |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 56 | : fInputCount(buffer.readInt()), fInputs(new SkImageFilter*[fInputCount]) { |
| 57 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 58 | if (buffer.readBool()) { |
| 59 | fInputs[i] = static_cast<SkImageFilter*>(buffer.readFlattenable()); |
| 60 | } else { |
| 61 | fInputs[i] = NULL; |
| 62 | } |
| 63 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 64 | buffer.readIRect(&fCropRect); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void SkImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 68 | buffer.writeInt(fInputCount); |
| 69 | for (int i = 0; i < fInputCount; i++) { |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 70 | SkImageFilter* input = getInput(i); |
| 71 | buffer.writeBool(input != NULL); |
| 72 | if (input != NULL) { |
| 73 | buffer.writeFlattenable(input); |
| 74 | } |
| 75 | } |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 76 | buffer.writeIRect(fCropRect); |
senorblanco@chromium.org | 9f25de7 | 2012-10-10 20:36:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 79 | bool SkImageFilter::filterImage(Proxy* proxy, const SkBitmap& src, |
| 80 | const SkMatrix& ctm, |
| 81 | SkBitmap* result, SkIPoint* loc) { |
| 82 | SkASSERT(result); |
| 83 | SkASSERT(loc); |
| 84 | /* |
| 85 | * Give the proxy first shot at the filter. If it returns false, ask |
| 86 | * the filter to do it. |
| 87 | */ |
| 88 | return (proxy && proxy->filterImage(this, src, ctm, result, loc)) || |
| 89 | this->onFilterImage(proxy, src, ctm, result, loc); |
| 90 | } |
| 91 | |
| 92 | bool SkImageFilter::filterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 93 | SkIRect* dst) { |
| 94 | SkASSERT(&src); |
| 95 | SkASSERT(dst); |
| 96 | return this->onFilterBounds(src, ctm, dst); |
| 97 | } |
| 98 | |
| 99 | bool SkImageFilter::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&, |
| 100 | SkBitmap*, SkIPoint*) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | bool SkImageFilter::canFilterImageGPU() const { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 105 | return this->asNewEffect(NULL, NULL); |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 108 | bool SkImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, SkBitmap* result, |
| 109 | SkIPoint* offset) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 110 | #if SK_SUPPORT_GPU |
| 111 | SkBitmap input; |
| 112 | SkASSERT(fInputCount == 1); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 113 | if (!SkImageFilterUtils::GetInputResultGPU(this->getInput(0), proxy, src, &input, offset)) { |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 114 | return false; |
| 115 | } |
commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 116 | GrTexture* srcTexture = input.getTexture(); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 117 | SkRect rect; |
| 118 | src.getBounds(&rect); |
| 119 | GrContext* context = srcTexture->getContext(); |
| 120 | |
| 121 | GrTextureDesc desc; |
| 122 | desc.fFlags = kRenderTarget_GrTextureFlagBit, |
| 123 | desc.fWidth = input.width(); |
| 124 | desc.fHeight = input.height(); |
| 125 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 126 | |
| 127 | GrAutoScratchTexture dst(context, desc); |
| 128 | GrContext::AutoMatrix am; |
| 129 | am.setIdentity(context); |
| 130 | GrContext::AutoRenderTarget art(context, dst.texture()->asRenderTarget()); |
| 131 | GrContext::AutoClip acs(context, rect); |
| 132 | GrEffectRef* effect; |
| 133 | this->asNewEffect(&effect, srcTexture); |
| 134 | SkASSERT(effect); |
| 135 | SkAutoUnref effectRef(effect); |
| 136 | GrPaint paint; |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 137 | paint.addColorEffect(effect); |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 138 | context->drawRect(paint, rect); |
| 139 | SkAutoTUnref<GrTexture> resultTex(dst.detach()); |
| 140 | SkImageFilterUtils::WrapTexture(resultTex, input.width(), input.height(), result); |
| 141 | return true; |
| 142 | #else |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 143 | return false; |
senorblanco@chromium.org | d043cce | 2013-04-08 19:43:22 +0000 | [diff] [blame] | 144 | #endif |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
senorblanco@chromium.org | 194d775 | 2013-07-24 22:19:24 +0000 | [diff] [blame^] | 147 | bool SkImageFilter::applyCropRect(SkIRect* rect) const { |
| 148 | return rect->intersect(fCropRect); |
| 149 | } |
| 150 | |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 151 | bool SkImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
| 152 | SkIRect* dst) { |
| 153 | *dst = src; |
| 154 | return true; |
| 155 | } |
| 156 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 157 | bool SkImageFilter::asNewEffect(GrEffectRef**, GrTexture*) const { |
senorblanco@chromium.org | bf2768b | 2012-08-20 15:43:14 +0000 | [diff] [blame] | 158 | return false; |
| 159 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 160 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 161 | bool SkImageFilter::asColorFilter(SkColorFilter**) const { |
| 162 | return false; |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 163 | } |