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 "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
15 | 15 |
16 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRe
ct& rect) { | 16 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const SkRe
ct& rect) { |
17 SkASSERT(s); | 17 SkASSERT(s); |
18 uint32_t flags = CropRect::kHasAll_CropEdge; | 18 uint32_t flags = CropRect::kHasAll_CropEdge; |
19 if (rect.width() == 0 || rect.height() == 0) { | 19 if (rect.width() == 0 || rect.height() == 0) { |
20 flags = 0x0; | 20 flags = 0x0; |
21 } | 21 } |
22 CropRect cropRect(rect, flags); | 22 CropRect cropRect(rect, flags); |
23 return SkNEW_ARGS(SkRectShaderImageFilter, (s, &cropRect)); | 23 return s ? SkNEW_ARGS(SkRectShaderImageFilter, (s, &cropRect)) : NULL; |
24 } | 24 } |
25 | 25 |
26 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const Crop
Rect* cropRect, uint32_t uniqueID) { | 26 SkRectShaderImageFilter* SkRectShaderImageFilter::Create(SkShader* s, const Crop
Rect* cropRect, uint32_t uniqueID) { |
27 SkASSERT(s); | 27 SkASSERT(s); |
28 return SkNEW_ARGS(SkRectShaderImageFilter, (s, cropRect, uniqueID)); | 28 return s ? SkNEW_ARGS(SkRectShaderImageFilter, (s, cropRect, uniqueID)) : NU
LL; |
29 } | 29 } |
30 | 30 |
31 SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const CropRect* cr
opRect, | 31 SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const CropRect* cr
opRect, |
32 uint32_t uniqueID) | 32 uint32_t uniqueID) |
33 : INHERITED(0, NULL, cropRect, uniqueID) | 33 : INHERITED(0, NULL, cropRect, uniqueID) |
34 , fShader(s) { | 34 , fShader(SkRef(s)) { |
35 SkASSERT(s); | |
36 s->ref(); | |
37 } | 35 } |
38 | 36 |
39 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 37 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
40 SkRectShaderImageFilter::SkRectShaderImageFilter(SkReadBuffer& buffer) | 38 SkRectShaderImageFilter::SkRectShaderImageFilter(SkReadBuffer& buffer) |
41 : INHERITED(0, buffer) { | 39 : INHERITED(0, buffer) { |
42 fShader = buffer.readShader(); | 40 fShader = buffer.readShader(); |
43 } | 41 } |
44 #endif | 42 #endif |
45 | 43 |
46 SkFlattenable* SkRectShaderImageFilter::CreateProc(SkReadBuffer& buffer) { | 44 SkFlattenable* SkRectShaderImageFilter::CreateProc(SkReadBuffer& buffer) { |
47 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); | 45 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
48 SkAutoTUnref<SkShader> shader(buffer.readShader()); | 46 SkAutoTUnref<SkShader> shader(buffer.readShader()); |
49 return Create(shader.get(), &common.cropRect(), common.uniqueID()); | 47 return Create(shader.get(), &common.cropRect(), common.uniqueID()); |
50 } | 48 } |
51 | 49 |
52 void SkRectShaderImageFilter::flatten(SkWriteBuffer& buffer) const { | 50 void SkRectShaderImageFilter::flatten(SkWriteBuffer& buffer) const { |
53 this->INHERITED::flatten(buffer); | 51 this->INHERITED::flatten(buffer); |
54 buffer.writeFlattenable(fShader); | 52 buffer.writeFlattenable(fShader); |
55 } | 53 } |
56 | 54 |
57 SkRectShaderImageFilter::~SkRectShaderImageFilter() { | 55 SkRectShaderImageFilter::~SkRectShaderImageFilter() { |
58 SkSafeUnref(fShader); | 56 fShader->unref(); |
59 } | 57 } |
60 | 58 |
61 bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, | 59 bool SkRectShaderImageFilter::onFilterImage(Proxy* proxy, |
62 const SkBitmap& source, | 60 const SkBitmap& source, |
63 const Context& ctx, | 61 const Context& ctx, |
64 SkBitmap* result, | 62 SkBitmap* result, |
65 SkIPoint* offset) const { | 63 SkIPoint* offset) const { |
66 SkIRect bounds; | 64 SkIRect bounds; |
67 if (!this->applyCropRect(ctx, source, SkIPoint::Make(0, 0), &bounds)) { | 65 if (!this->applyCropRect(ctx, source, SkIPoint::Make(0, 0), &bounds)) { |
68 return false; | 66 return false; |
69 } | 67 } |
70 | 68 |
71 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), | 69 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), |
72 bounds.height())); | 70 bounds.height())); |
73 if (NULL == device.get()) { | 71 if (NULL == device.get()) { |
74 return false; | 72 return false; |
75 } | 73 } |
76 SkCanvas canvas(device.get()); | 74 SkCanvas canvas(device.get()); |
77 | 75 |
78 SkPaint paint; | 76 SkPaint paint; |
79 SkMatrix matrix(ctx.ctm()); | 77 SkMatrix matrix(ctx.ctm()); |
80 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); | 78 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to
p())); |
81 paint.setShader(SkShader::CreateLocalMatrixShader(fShader, matrix))->unref()
; | 79 SkSafeUnref(paint.setShader(SkShader::CreateLocalMatrixShader(fShader, matri
x))); |
82 | 80 |
83 SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bo
unds.height())); | 81 SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bo
unds.height())); |
84 canvas.drawRect(rect, paint); | 82 canvas.drawRect(rect, paint); |
85 | 83 |
86 *result = device.get()->accessBitmap(false); | 84 *result = device.get()->accessBitmap(false); |
87 offset->fX = bounds.fLeft; | 85 offset->fX = bounds.fLeft; |
88 offset->fY = bounds.fTop; | 86 offset->fY = bounds.fTop; |
89 return true; | 87 return true; |
90 } | 88 } |
OLD | NEW |