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 "SkTileImageFilter.h" | 8 #include "SkTileImageFilter.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 "SkMatrix.h" | 13 #include "SkMatrix.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkShader.h" | 15 #include "SkShader.h" |
16 #include "SkValidationUtils.h" | 16 #include "SkValidationUtils.h" |
17 | 17 |
18 bool SkTileImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, const S
kMatrix& ctm, | 18 bool SkTileImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, const S
kMatrix& ctm, |
19 SkBitmap* dst, SkIPoint* offset) { | 19 SkBitmap* dst, SkIPoint* offset) { |
20 SkBitmap source = src; | 20 SkBitmap source = src; |
21 SkImageFilter* input = getInput(0); | 21 SkImageFilter* input = getInput(0); |
22 SkIPoint localOffset = SkIPoint::Make(0, 0); | 22 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
23 if (input && !input->filterImage(proxy, src, ctm, &source, &localOffset)) { | 23 if (input && !input->filterImage(proxy, src, ctm, &source, &srcOffset)) { |
24 return false; | 24 return false; |
25 } | 25 } |
26 | 26 |
27 SkRect dstRect; | 27 SkRect dstRect; |
28 ctm.mapRect(&dstRect, fDstRect); | 28 ctm.mapRect(&dstRect, fDstRect); |
29 int w = SkScalarCeilToInt(dstRect.width()); | 29 SkIRect dstIRect; |
30 int h = SkScalarCeilToInt(dstRect.height()); | 30 dstRect.roundOut(&dstIRect); |
| 31 int w = dstIRect.width(); |
| 32 int h = dstIRect.height(); |
31 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { | 33 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { |
32 return false; | 34 return false; |
33 } | 35 } |
34 | 36 |
35 SkRect srcRect; | 37 SkRect srcRect; |
36 ctm.mapRect(&srcRect, fSrcRect); | 38 ctm.mapRect(&srcRect, fSrcRect); |
37 SkIRect srcIRect; | 39 SkIRect srcIRect; |
38 srcRect.roundOut(&srcIRect); | 40 srcRect.roundOut(&srcIRect); |
| 41 srcIRect.offset(-srcOffset); |
39 SkBitmap subset; | 42 SkBitmap subset; |
40 SkIRect bounds; | 43 SkIRect bounds; |
41 source.getBounds(&bounds); | 44 source.getBounds(&bounds); |
| 45 |
42 if (!srcIRect.intersect(bounds)) { | 46 if (!srcIRect.intersect(bounds)) { |
| 47 offset->fX = offset->fY = 0; |
43 return true; | 48 return true; |
44 } else if (!source.extractSubset(&subset, srcIRect)) { | 49 } else if (!source.extractSubset(&subset, srcIRect)) { |
45 return false; | 50 return false; |
46 } | 51 } |
47 | 52 |
48 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); | 53 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); |
49 if (NULL == device.get()) { | 54 if (NULL == device.get()) { |
50 return false; | 55 return false; |
51 } | 56 } |
52 SkCanvas canvas(device); | 57 SkCanvas canvas(device); |
53 SkPaint paint; | 58 SkPaint paint; |
54 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 59 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
55 | 60 |
56 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(subset, | 61 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(subset, |
57 SkShader::kRepeat_TileMode, SkShader::kRepeat_
TileMode)); | 62 SkShader::kRepeat_TileMode, SkShader::kRepeat_
TileMode)); |
| 63 SkMatrix shaderMatrix; |
| 64 shaderMatrix.setTranslate(SkIntToScalar(srcOffset.fX), |
| 65 SkIntToScalar(srcOffset.fY)); |
| 66 shader->setLocalMatrix(shaderMatrix); |
58 paint.setShader(shader); | 67 paint.setShader(shader); |
59 dstRect.offset(SkIntToScalar(localOffset.fX), SkIntToScalar(localOffset.fY))
; | 68 canvas.translate(-dstRect.fLeft, -dstRect.fTop); |
60 canvas.drawRect(dstRect, paint); | 69 canvas.drawRect(dstRect, paint); |
61 *dst = device->accessBitmap(false); | 70 *dst = device->accessBitmap(false); |
| 71 offset->fX = dstIRect.fLeft; |
| 72 offset->fY = dstIRect.fTop; |
62 return true; | 73 return true; |
63 } | 74 } |
64 | 75 |
65 SkTileImageFilter::SkTileImageFilter(SkFlattenableReadBuffer& buffer) | 76 SkTileImageFilter::SkTileImageFilter(SkFlattenableReadBuffer& buffer) |
66 : INHERITED(1, buffer) { | 77 : INHERITED(1, buffer) { |
67 buffer.readRect(&fSrcRect); | 78 buffer.readRect(&fSrcRect); |
68 buffer.readRect(&fDstRect); | 79 buffer.readRect(&fDstRect); |
69 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect
(fDstRect)); | 80 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect
(fDstRect)); |
70 } | 81 } |
71 | 82 |
72 void SkTileImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | 83 void SkTileImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
73 this->INHERITED::flatten(buffer); | 84 this->INHERITED::flatten(buffer); |
74 buffer.writeRect(fSrcRect); | 85 buffer.writeRect(fSrcRect); |
75 buffer.writeRect(fDstRect); | 86 buffer.writeRect(fDstRect); |
76 } | 87 } |
OLD | NEW |