OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRectShaderImageFilter.h" | 8 #include "SkRectShaderImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkFlattenableBuffers.h" | 12 #include "SkFlattenableBuffers.h" |
13 #include "SkShader.h" | 13 #include "SkShader.h" |
14 | 14 |
15 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRe
ct& rect) { | 15 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRe
ct& rect) { |
16 SkASSERT(s); | 16 SkASSERT(s); |
17 return SkNEW_ARGS(SkRectShaderImageFilter, (s, rect)); | 17 #ifdef SK_CROP_RECT_IS_INT |
| 18 SkIRect cropRect; |
| 19 rect.roundOut(&cropRect); |
| 20 #else |
| 21 CropRect cropRect(rect); |
| 22 #endif |
| 23 return SkNEW_ARGS(SkRectShaderImageFilter, (s, &cropRect)); |
18 } | 24 } |
19 | 25 |
20 SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const SkRect& rect
) | 26 SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const CropRect* cr
opRect) |
21 : INHERITED(NULL) | 27 : INHERITED(NULL, cropRect) |
22 , fShader(s) | 28 , fShader(s) { |
23 , fRect(rect) { | |
24 SkASSERT(s); | 29 SkASSERT(s); |
25 s->ref(); | 30 s->ref(); |
26 } | 31 } |
27 | 32 |
28 SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer
) | 33 SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer
) |
29 : INHERITED(buffer) { | 34 : INHERITED(buffer) { |
30 fShader = buffer.readFlattenableT<SkShader>(); | 35 fShader = buffer.readFlattenableT<SkShader>(); |
31 buffer.readRect(&fRect); | |
32 } | 36 } |
33 | 37 |
34 void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | 38 void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
35 this->INHERITED::flatten(buffer); | 39 this->INHERITED::flatten(buffer); |
36 | 40 |
37 buffer.writeFlattenable(fShader); | 41 buffer.writeFlattenable(fShader); |
38 buffer.writeRect(fRect); | |
39 } | 42 } |
40 | 43 |
41 SkRectShaderImageFilter::~SkRectShaderImageFilter() { | 44 SkRectShaderImageFilter::~SkRectShaderImageFilter() { |
42 SkSafeUnref(fShader); | 45 SkSafeUnref(fShader); |
43 } | 46 } |
44 | 47 |
45 bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, | 48 bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, |
46 const SkBitmap& source, | 49 const SkBitmap& source, |
47 const SkMatrix&, | 50 const SkMatrix& ctm, |
48 SkBitmap* result, | 51 SkBitmap* result, |
49 SkIPoint*) { | 52 SkIPoint* offset) { |
50 SkRect rect(fRect); | 53 SkIRect bounds; |
51 if (rect.isEmpty()) { | 54 source.getBounds(&bounds); |
52 rect = SkRect::MakeWH(SkIntToScalar(source.width()), SkIntToScalar(sourc
e.height())); | 55 if (!this->applyCropRect(&bounds, ctm)) { |
53 } | |
54 | |
55 if (rect.isEmpty()) { | |
56 return false; | 56 return false; |
57 } | 57 } |
58 | 58 |
59 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(SkScalarCeilToInt(rect
.width()), | 59 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), |
60 SkScalarCeilToInt(rect
.height()))); | 60 bounds.height())); |
61 SkCanvas canvas(device.get()); | 61 SkCanvas canvas(device.get()); |
62 SkPaint paint; | 62 SkPaint paint; |
63 paint.setShader(fShader); | 63 paint.setShader(fShader); |
| 64 SkMatrix matrix; |
| 65 matrix.setTranslate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop
)); |
| 66 fShader->setLocalMatrix(matrix); |
| 67 SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bo
unds.height())); |
64 canvas.drawRect(rect, paint); | 68 canvas.drawRect(rect, paint); |
65 *result = device.get()->accessBitmap(false); | 69 *result = device.get()->accessBitmap(false); |
| 70 offset->fX += bounds.fLeft; |
| 71 offset->fY += bounds.fTop; |
66 return true; | 72 return true; |
67 } | 73 } |
OLD | NEW |