sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 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 "SkRectShaderImageFilter.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 16 | SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRect& rect) { |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 17 | SkASSERT(s); |
senorblanco@chromium.org | 5e85300 | 2013-10-11 17:57:47 +0000 | [diff] [blame] | 18 | uint32_t flags = CropRect::kHasAll_CropEdge; |
| 19 | if (rect.width() == 0 || rect.height() == 0) { |
| 20 | flags = 0x0; |
| 21 | } |
| 22 | CropRect cropRect(rect, flags); |
senorblanco@chromium.org | e258eb3 | 2013-10-10 21:36:48 +0000 | [diff] [blame] | 23 | return SkNEW_ARGS(SkRectShaderImageFilter, (s, &cropRect)); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 24 | } |
| 25 | |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 26 | SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const CropRect* cropRect, uint32_t uniqueID) { |
senorblanco@chromium.org | 33e88b7 | 2013-10-15 15:25:18 +0000 | [diff] [blame] | 27 | SkASSERT(s); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 28 | return SkNEW_ARGS(SkRectShaderImageFilter, (s, cropRect, uniqueID)); |
senorblanco@chromium.org | 33e88b7 | 2013-10-15 15:25:18 +0000 | [diff] [blame] | 29 | } |
| 30 | |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 31 | SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const CropRect* cropRect, |
| 32 | uint32_t uniqueID) |
| 33 | : INHERITED(0, NULL, cropRect, uniqueID) |
senorblanco@chromium.org | e258eb3 | 2013-10-10 21:36:48 +0000 | [diff] [blame] | 34 | , fShader(s) { |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 35 | SkASSERT(s); |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 36 | s->ref(); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 37 | } |
| 38 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 39 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 40 | SkRectShaderImageFilter::SkRectShaderImageFilter(SkReadBuffer& buffer) |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 41 | : INHERITED(0, buffer) { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 42 | fShader = buffer.readShader(); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 43 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | SkFlattenable* SkRectShaderImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 47 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
mtklein | b00f54d | 2014-08-22 13:42:56 -0700 | [diff] [blame] | 48 | SkAutoTUnref<SkShader> shader(buffer.readShader()); |
senorblanco | 5e5f948 | 2014-08-26 12:27:12 -0700 | [diff] [blame^] | 49 | return Create(shader.get(), &common.cropRect(), common.uniqueID()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 50 | } |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 52 | void SkRectShaderImageFilter::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 53 | this->INHERITED::flatten(buffer); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 54 | buffer.writeFlattenable(fShader); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | SkRectShaderImageFilter::~SkRectShaderImageFilter() { |
| 58 | SkSafeUnref(fShader); |
| 59 | } |
| 60 | |
| 61 | bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 62 | const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 63 | const Context& ctx, |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 64 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 65 | SkIPoint* offset) const { |
senorblanco@chromium.org | e258eb3 | 2013-10-10 21:36:48 +0000 | [diff] [blame] | 66 | SkIRect bounds; |
senorblanco@chromium.org | 1182529 | 2014-03-14 17:44:41 +0000 | [diff] [blame] | 67 | if (!this->applyCropRect(ctx, source, SkIPoint::Make(0, 0), &bounds)) { |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
senorblanco@chromium.org | e258eb3 | 2013-10-10 21:36:48 +0000 | [diff] [blame] | 71 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), |
| 72 | bounds.height())); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 73 | if (NULL == device.get()) { |
| 74 | return false; |
| 75 | } |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 76 | SkCanvas canvas(device.get()); |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 77 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 78 | SkPaint paint; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 79 | SkMatrix matrix(ctx.ctm()); |
senorblanco@chromium.org | 47d98c8 | 2014-03-03 14:58:09 +0000 | [diff] [blame] | 80 | matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 81 | paint.setShader(SkShader::CreateLocalMatrixShader(fShader, matrix))->unref(); |
| 82 | |
senorblanco@chromium.org | e258eb3 | 2013-10-10 21:36:48 +0000 | [diff] [blame] | 83 | SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bounds.height())); |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 84 | canvas.drawRect(rect, paint); |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 85 | |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 86 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 87 | offset->fX = bounds.fLeft; |
| 88 | offset->fY = bounds.fTop; |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |