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" |
| 12 | #include "SkFlattenableBuffers.h" |
| 13 | #include "SkShader.h" |
| 14 | |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 15 | SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRect& rect) { |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 16 | SkASSERT(s); |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 17 | return SkNEW_ARGS(SkRectShaderImageFilter, (s, rect)); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 18 | } |
| 19 | |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 20 | SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const SkRect& rect) |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 21 | : INHERITED(NULL) |
| 22 | , fShader(s) |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 23 | , fRect(rect) { |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 24 | SkASSERT(s); |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 25 | s->ref(); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer) |
| 29 | : INHERITED(buffer) { |
| 30 | fShader = buffer.readFlattenableT<SkShader>(); |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 31 | buffer.readRect(&fRect); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 35 | this->INHERITED::flatten(buffer); |
| 36 | |
| 37 | buffer.writeFlattenable(fShader); |
sugoi@google.com | 4b6d432 | 2013-02-21 20:26:50 +0000 | [diff] [blame] | 38 | buffer.writeRect(fRect); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | SkRectShaderImageFilter::~SkRectShaderImageFilter() { |
| 42 | SkSafeUnref(fShader); |
| 43 | } |
| 44 | |
| 45 | bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 46 | const SkBitmap& source, |
| 47 | const SkMatrix&, |
| 48 | SkBitmap* result, |
| 49 | SkIPoint*) { |
| 50 | SkRect rect(fRect); |
| 51 | if (rect.isEmpty()) { |
caryclark@google.com | 9584b9e | 2013-04-18 17:44:57 +0000 | [diff] [blame] | 52 | rect = SkRect::MakeWH(SkIntToScalar(source.width()), SkIntToScalar(source.height())); |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | if (rect.isEmpty()) { |
| 56 | return false; |
| 57 | } |
| 58 | |
robertphillips@google.com | 9b051a3 | 2013-08-20 20:06:40 +0000 | [diff] [blame^] | 59 | SkAutoTUnref<SkDevice> device(proxy->createDevice(SkScalarCeilToInt(rect.width()), |
| 60 | SkScalarCeilToInt(rect.height()))); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 61 | SkCanvas canvas(device.get()); |
| 62 | SkPaint paint; |
| 63 | paint.setShader(fShader); |
sugoi@google.com | f738cf0 | 2013-04-18 14:13:10 +0000 | [diff] [blame] | 64 | canvas.drawRect(rect, paint); |
sugoi@google.com | a1c511b | 2013-02-21 15:02:28 +0000 | [diff] [blame] | 65 | *result = device.get()->accessBitmap(false); |
| 66 | return true; |
| 67 | } |