Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: src/effects/SkTileImageFilter.cpp

Issue 112803004: Make SkImageFilter crop rects relative to the primitive origin, instead of relative to their parent (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Remove a useless zero. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 int w = SkScalarCeilToInt(dstRect.width());
30 int h = SkScalarCeilToInt(dstRect.height()); 30 int h = SkScalarCeilToInt(dstRect.height());
31 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { 31 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) {
32 return false; 32 return false;
33 } 33 }
34 34
35 SkRect srcRect; 35 SkRect srcRect;
36 ctm.mapRect(&srcRect, fSrcRect); 36 ctm.mapRect(&srcRect, fSrcRect);
37 SkIRect srcIRect; 37 SkIRect srcIRect;
38 srcRect.roundOut(&srcIRect); 38 srcRect.roundOut(&srcIRect);
39 srcIRect.offset(-srcOffset);
39 SkBitmap subset; 40 SkBitmap subset;
40 SkIRect bounds; 41 SkIRect bounds;
41 source.getBounds(&bounds); 42 source.getBounds(&bounds);
43
42 if (!srcIRect.intersect(bounds)) { 44 if (!srcIRect.intersect(bounds)) {
43 return true; 45 return true;
44 } else if (!source.extractSubset(&subset, srcIRect)) { 46 } else if (!source.extractSubset(&subset, srcIRect)) {
45 return false; 47 return false;
46 } 48 }
47 49
48 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); 50 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h));
49 if (NULL == device.get()) { 51 if (NULL == device.get()) {
50 return false; 52 return false;
51 } 53 }
52 SkCanvas canvas(device); 54 SkCanvas canvas(device);
53 SkPaint paint; 55 SkPaint paint;
54 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 56 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
55 57
56 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(subset, 58 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(subset,
57 SkShader::kRepeat_TileMode, SkShader::kRepeat_ TileMode)); 59 SkShader::kRepeat_TileMode, SkShader::kRepeat_ TileMode));
60 SkMatrix shaderMatrix;
61 shaderMatrix.setTranslate(SkIntToScalar(srcOffset.fX),
62 SkIntToScalar(srcOffset.fY));
63 shader->setLocalMatrix(shaderMatrix);
58 paint.setShader(shader); 64 paint.setShader(shader);
59 dstRect.offset(SkIntToScalar(localOffset.fX), SkIntToScalar(localOffset.fY)) ; 65 canvas.translate(-dstRect.fLeft, -dstRect.fTop);
60 canvas.drawRect(dstRect, paint); 66 canvas.drawRect(dstRect, paint);
61 *dst = device->accessBitmap(false); 67 *dst = device->accessBitmap(false);
68 offset->fX = dstRect.fLeft;
69 offset->fY = dstRect.fTop;
62 return true; 70 return true;
63 } 71 }
64 72
65 SkTileImageFilter::SkTileImageFilter(SkFlattenableReadBuffer& buffer) 73 SkTileImageFilter::SkTileImageFilter(SkFlattenableReadBuffer& buffer)
66 : INHERITED(1, buffer) { 74 : INHERITED(1, buffer) {
67 buffer.readRect(&fSrcRect); 75 buffer.readRect(&fSrcRect);
68 buffer.readRect(&fDstRect); 76 buffer.readRect(&fDstRect);
69 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect (fDstRect)); 77 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect (fDstRect));
70 } 78 }
71 79
72 void SkTileImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { 80 void SkTileImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
73 this->INHERITED::flatten(buffer); 81 this->INHERITED::flatten(buffer);
74 buffer.writeRect(fSrcRect); 82 buffer.writeRect(fSrcRect);
75 buffer.writeRect(fDstRect); 83 buffer.writeRect(fDstRect);
76 } 84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698