| 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 "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
| 14 #include "SkMatrix.h" | 14 #include "SkMatrix.h" |
| 15 #include "SkPaint.h" | 15 #include "SkPaint.h" |
| 16 #include "SkShader.h" | 16 #include "SkShader.h" |
| 17 #include "SkValidationUtils.h" | 17 #include "SkValidationUtils.h" |
| 18 | 18 |
| 19 SkTileImageFilter* SkTileImageFilter::Create(const SkRect& srcRect, const SkRect
& dstRect, | 19 SkTileImageFilter* SkTileImageFilter::Create(const SkRect& srcRect, const SkRect
& dstRect, |
| 20 SkImageFilter* input) { | 20 SkImageFilter* input) { |
| 21 if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { | 21 if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { |
| 22 return NULL; | 22 return NULL; |
| 23 } | 23 } |
| 24 return SkNEW_ARGS(SkTileImageFilter, (srcRect, dstRect, input)); | 24 return SkNEW_ARGS(SkTileImageFilter, (srcRect, dstRect, input)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 bool SkTileImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, | 27 bool SkTileImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& src, |
| 28 const Context& ctx, | 28 const Context& ctx, |
| 29 SkBitmap* dst, SkIPoint* offset) const { | 29 SkBitmap* dst, SkIPoint* offset) const { |
| 30 SkBitmap source = src; |
| 31 SkImageFilter* input = getInput(0); |
| 32 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 33 if (input && !input->filterImage(proxy, src, ctx, &source, &srcOffset)) { |
| 34 return false; |
| 35 } |
| 30 | 36 |
| 31 SkRect dstRect; | 37 SkRect dstRect; |
| 32 ctx.ctm().mapRect(&dstRect, fDstRect); | 38 ctx.ctm().mapRect(&dstRect, fDstRect); |
| 33 const SkIRect dstIRect = dstRect.roundOut(); | 39 const SkIRect dstIRect = dstRect.roundOut(); |
| 34 if (fSrcRect.isEmpty() || dstIRect.isEmpty()) { | 40 int w = dstIRect.width(); |
| 41 int h = dstIRect.height(); |
| 42 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { |
| 35 return false; | 43 return false; |
| 36 } | 44 } |
| 37 | 45 |
| 38 // TODO: the actual clip that needs to be applied to the src should be (roug
hly) determined by: | |
| 39 // intersect ctx.clip and dstIRect | |
| 40 // determine if that rect lies wholly inside fSrcRect | |
| 41 // if so pass it on as the clip | |
| 42 // if not pass the entire fSrcRect as the clip | |
| 43 // For now don't apply any clip to the source (since it is usually very smal
l and all of it | |
| 44 // will be required anyway). | |
| 45 Context srcCtx(ctx.ctm(), SkIRect::MakeLargest(), ctx.cache()); | |
| 46 | |
| 47 SkBitmap source = src; | |
| 48 SkImageFilter* input = this->getInput(0); | |
| 49 SkIPoint srcOffset = SkIPoint::Make(0, 0); | |
| 50 if (input && !input->filterImage(proxy, src, srcCtx, &source, &srcOffset)) { | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 SkRect srcRect; | 46 SkRect srcRect; |
| 55 ctx.ctm().mapRect(&srcRect, fSrcRect); | 47 ctx.ctm().mapRect(&srcRect, fSrcRect); |
| 56 SkIRect srcIRect; | 48 SkIRect srcIRect; |
| 57 srcRect.roundOut(&srcIRect); | 49 srcRect.roundOut(&srcIRect); |
| 58 srcIRect.offset(-srcOffset); | 50 srcIRect.offset(-srcOffset); |
| 59 SkBitmap subset; | 51 SkBitmap subset; |
| 60 SkIRect bounds; | 52 SkIRect bounds; |
| 61 source.getBounds(&bounds); | 53 source.getBounds(&bounds); |
| 62 | 54 |
| 63 if (!srcIRect.intersect(bounds)) { | 55 if (!srcIRect.intersect(bounds)) { |
| 64 offset->fX = offset->fY = 0; | 56 offset->fX = offset->fY = 0; |
| 65 return true; | 57 return true; |
| 66 } else if (!source.extractSubset(&subset, srcIRect)) { | 58 } else if (!source.extractSubset(&subset, srcIRect)) { |
| 67 return false; | 59 return false; |
| 68 } | 60 } |
| 69 | 61 |
| 70 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstI
Rect.height())); | 62 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); |
| 71 if (NULL == device.get()) { | 63 if (NULL == device.get()) { |
| 72 return false; | 64 return false; |
| 73 } | 65 } |
| 74 SkCanvas canvas(device); | 66 SkCanvas canvas(device); |
| 75 SkPaint paint; | 67 SkPaint paint; |
| 76 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 68 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 77 | 69 |
| 78 SkMatrix shaderMatrix; | 70 SkMatrix shaderMatrix; |
| 79 shaderMatrix.setTranslate(SkIntToScalar(srcOffset.fX), | 71 shaderMatrix.setTranslate(SkIntToScalar(srcOffset.fX), |
| 80 SkIntToScalar(srcOffset.fY)); | 72 SkIntToScalar(srcOffset.fY)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 109 |
| 118 void SkTileImageFilter::flatten(SkWriteBuffer& buffer) const { | 110 void SkTileImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 119 this->INHERITED::flatten(buffer); | 111 this->INHERITED::flatten(buffer); |
| 120 buffer.writeRect(fSrcRect); | 112 buffer.writeRect(fSrcRect); |
| 121 buffer.writeRect(fDstRect); | 113 buffer.writeRect(fDstRect); |
| 122 } | 114 } |
| 123 | 115 |
| 124 #ifndef SK_IGNORE_TO_STRING | 116 #ifndef SK_IGNORE_TO_STRING |
| 125 void SkTileImageFilter::toString(SkString* str) const { | 117 void SkTileImageFilter::toString(SkString* str) const { |
| 126 str->appendf("SkTileImageFilter: ("); | 118 str->appendf("SkTileImageFilter: ("); |
| 127 this->getCropRect().toString(str); | |
| 128 str->appendf("src: %.2f %.2f %.2f %.2f", | 119 str->appendf("src: %.2f %.2f %.2f %.2f", |
| 129 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); | 120 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); |
| 130 str->appendf(" dst: %.2f %.2f %.2f %.2f", | 121 str->appendf(" dst: %.2f %.2f %.2f %.2f", |
| 131 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto
m); | 122 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto
m); |
| 132 if (this->getInput(0)) { | 123 if (this->getInput(0)) { |
| 133 str->appendf(" input: ("); | 124 str->appendf("input: ("); |
| 134 this->getInput(0)->toString(str); | 125 this->getInput(0)->toString(str); |
| 135 str->appendf(")"); | 126 str->appendf(")"); |
| 136 } | 127 } |
| 137 str->append(")"); | 128 str->append(")"); |
| 138 } | 129 } |
| 139 #endif | 130 #endif |
| OLD | NEW |