Chromium Code Reviews| 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 |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDevice.h" | 11 #include "SkImage.h" |
| 12 #include "SkMatrix.h" | |
| 12 #include "SkOffsetImageFilter.h" | 13 #include "SkOffsetImageFilter.h" |
| 14 #include "SkPaint.h" | |
| 13 #include "SkReadBuffer.h" | 15 #include "SkReadBuffer.h" |
| 16 #include "SkShader.h" | |
| 17 #include "SkSpecialImage.h" | |
| 18 #include "SkSpecialSurface.h" | |
| 19 #include "SkSurface.h" | |
| 20 #include "SkValidationUtils.h" | |
| 14 #include "SkWriteBuffer.h" | 21 #include "SkWriteBuffer.h" |
| 15 #include "SkMatrix.h" | |
| 16 #include "SkPaint.h" | |
| 17 #include "SkShader.h" | |
| 18 #include "SkValidationUtils.h" | |
| 19 | 22 |
| 20 SkImageFilter* SkTileImageFilter::Create(const SkRect& srcRect, const SkRect& ds tRect, | 23 SkImageFilter* SkTileImageFilter::Create(const SkRect& srcRect, const SkRect& ds tRect, |
| 21 SkImageFilter* input) { | 24 SkImageFilter* input) { |
| 22 if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { | 25 if (!SkIsValidRect(srcRect) || !SkIsValidRect(dstRect)) { |
| 23 return nullptr; | 26 return nullptr; |
| 24 } | 27 } |
| 25 if (srcRect.width() == dstRect.width() && srcRect.height() == dstRect.height ()) { | 28 if (srcRect.width() == dstRect.width() && srcRect.height() == dstRect.height ()) { |
| 26 SkRect ir = dstRect; | 29 SkRect ir = dstRect; |
| 27 if (!ir.intersect(srcRect)) { | 30 if (!ir.intersect(srcRect)) { |
| 28 return SkSafeRef(input); | 31 return SkSafeRef(input); |
| 29 } | 32 } |
| 30 CropRect cropRect(ir); | 33 CropRect cropRect(ir); |
| 31 return SkOffsetImageFilter::Create(dstRect.x() - srcRect.x(), | 34 return SkOffsetImageFilter::Create(dstRect.x() - srcRect.x(), |
| 32 dstRect.y() - srcRect.y(), | 35 dstRect.y() - srcRect.y(), |
| 33 input, &cropRect); | 36 input, &cropRect); |
| 34 } | 37 } |
| 35 return new SkTileImageFilter(srcRect, dstRect, input); | 38 return new SkTileImageFilter(srcRect, dstRect, input); |
| 36 } | 39 } |
| 37 | 40 |
| 38 bool SkTileImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& sr c, | 41 SkSpecialImage* SkTileImageFilter::onFilterImage(SkSpecialImage* source, |
| 39 const Context& ctx, | 42 const Context& ctx, |
| 40 SkBitmap* dst, SkIPoint* offset) const { | 43 SkIPoint* offset) const { |
| 41 SkBitmap source = src; | 44 SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 42 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 45 SkAutoTUnref<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputO ffset)); |
| 43 if (!this->filterInputDeprecated(0, proxy, src, ctx, &source, &srcOffset)) { | 46 if (!input) { |
| 44 return false; | 47 return nullptr; |
| 45 } | 48 } |
| 46 | 49 |
| 47 SkRect dstRect; | 50 SkRect dstRect; |
| 48 ctx.ctm().mapRect(&dstRect, fDstRect); | 51 ctx.ctm().mapRect(&dstRect, fDstRect); |
| 49 if (!dstRect.intersect(SkRect::Make(ctx.clipBounds()))) { | 52 if (!dstRect.intersect(SkRect::Make(ctx.clipBounds()))) { |
| 50 offset->fX = offset->fY = 0; | 53 return nullptr; |
|
Stephen White
2016/03/23 14:10:47
As in some other filters, I'm concerned that we're
robertphillips
2016/03/25 16:22:45
I have updated this site and the following one to
| |
| 51 return true; | |
| 52 } | 54 } |
| 53 const SkIRect dstIRect = dstRect.roundOut(); | 55 const SkIRect dstIRect = dstRect.roundOut(); |
| 54 int w = dstIRect.width(); | 56 int dstWidth = dstIRect.width(); |
| 55 int h = dstIRect.height(); | 57 int dstHeight = dstIRect.height(); |
| 56 if (!fSrcRect.width() || !fSrcRect.height() || !w || !h) { | 58 if (!fSrcRect.width() || !fSrcRect.height() || !dstWidth || !dstHeight) { |
| 57 return false; | 59 return nullptr; |
| 58 } | 60 } |
| 59 | 61 |
| 60 SkRect srcRect; | 62 SkRect srcRect; |
| 61 ctx.ctm().mapRect(&srcRect, fSrcRect); | 63 ctx.ctm().mapRect(&srcRect, fSrcRect); |
| 62 SkIRect srcIRect; | 64 SkIRect srcIRect; |
| 63 srcRect.roundOut(&srcIRect); | 65 srcRect.roundOut(&srcIRect); |
| 64 srcIRect.offset(-srcOffset); | 66 srcIRect.offset(-inputOffset); |
| 65 SkBitmap subset; | 67 const SkIRect inputBounds = SkIRect::MakeWH(input->width(), input->height()) ; |
| 66 SkIRect srcBounds; | |
| 67 source.getBounds(&srcBounds); | |
| 68 | 68 |
| 69 if (!SkIRect::Intersects(srcIRect, srcBounds)) { | 69 if (!SkIRect::Intersects(srcIRect, inputBounds)) { |
| 70 offset->fX = offset->fY = 0; | 70 return nullptr; |
| 71 return true; | |
| 72 } | 71 } |
| 73 if (srcBounds.contains(srcIRect)) { | 72 |
| 74 if (!source.extractSubset(&subset, srcIRect)) { | 73 // We create an SkImage here b.c. it needs to be a tight fit for the tiling |
| 75 return false; | 74 sk_sp<SkImage> subset; |
| 75 if (inputBounds.contains(srcIRect)) { | |
| 76 subset = input->makeTightSubset(srcIRect); | |
| 77 if (!subset) { | |
| 78 return nullptr; | |
| 76 } | 79 } |
| 77 } else { | 80 } else { |
| 78 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(srcIRect.width(), | 81 const SkImageInfo info = SkImageInfo::MakeN32(srcIRect.width(), srcIRect .height(), |
| 79 srcIRect.height(), | 82 kPremul_SkAlphaType); |
| 80 kPossible_TileUsag e)); | 83 sk_sp<SkSurface> surf(input->makeTightSurface(info)); |
| 81 if (!device) { | 84 if (!surf) { |
| 82 return false; | 85 return nullptr; |
| 83 } | 86 } |
| 84 SkCanvas canvas(device); | 87 |
| 85 canvas.drawBitmap(src, SkIntToScalar(srcOffset.x()), | 88 SkCanvas* canvas = surf->getCanvas(); |
| 86 SkIntToScalar(srcOffset.y())); | 89 SkASSERT(canvas); |
| 87 subset = device->accessBitmap(false); | 90 |
| 91 SkPaint paint; | |
| 92 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
| 93 | |
| 94 input->draw(canvas, | |
| 95 SkIntToScalar(inputOffset.x()), SkIntToScalar(inputOffset.y( )), | |
| 96 &paint); | |
| 97 | |
| 98 subset = surf->makeImageSnapshot(); | |
| 88 } | 99 } |
| 89 SkASSERT(subset.width() == srcIRect.width()); | 100 SkASSERT(subset->width() == srcIRect.width()); |
| 90 SkASSERT(subset.height() == srcIRect.height()); | 101 SkASSERT(subset->height() == srcIRect.height()); |
| 91 | 102 |
| 92 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(w, h)); | 103 const SkImageInfo info = SkImageInfo::MakeN32(dstWidth, dstHeight, kPremul_S kAlphaType); |
| 93 if (nullptr == device.get()) { | 104 |
| 94 return false; | 105 sk_sp<SkSpecialSurface> surf(source->makeSurface(info)); |
| 106 if (!surf) { | |
| 107 return nullptr; | |
| 95 } | 108 } |
| 96 SkCanvas canvas(device); | 109 |
| 110 SkCanvas* canvas = surf->getCanvas(); | |
| 111 SkASSERT(canvas); | |
| 112 | |
| 97 SkPaint paint; | 113 SkPaint paint; |
| 98 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 114 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 99 paint.setShader(SkShader::MakeBitmapShader(subset, SkShader::kRepeat_TileMod e, | 115 paint.setShader(subset->makeShader(SkShader::kRepeat_TileMode, SkShader::kRe peat_TileMode)); |
| 100 SkShader::kRepeat_TileMode)); | 116 canvas->translate(-dstRect.fLeft, -dstRect.fTop); |
| 101 canvas.translate(-dstRect.fLeft, -dstRect.fTop); | 117 canvas->drawRect(dstRect, paint); |
| 102 canvas.drawRect(dstRect, paint); | |
| 103 *dst = device->accessBitmap(false); | |
| 104 offset->fX = dstIRect.fLeft; | 118 offset->fX = dstIRect.fLeft; |
| 105 offset->fY = dstIRect.fTop; | 119 offset->fY = dstIRect.fTop; |
| 106 return true; | 120 return surf->makeImageSnapshot().release(); |
| 107 } | 121 } |
| 108 | 122 |
| 109 SkIRect SkTileImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix & ctm, | 123 SkIRect SkTileImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix & ctm, |
| 110 MapDirection direction) const { | 124 MapDirection direction) const { |
| 111 SkRect rect = kReverse_MapDirection == direction ? fSrcRect : fDstRect; | 125 SkRect rect = kReverse_MapDirection == direction ? fSrcRect : fDstRect; |
| 112 ctm.mapRect(&rect); | 126 ctm.mapRect(&rect); |
| 113 return rect.roundOut(); | 127 return rect.roundOut(); |
| 114 } | 128 } |
| 115 | 129 |
| 116 SkIRect SkTileImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix&, M apDirection) const { | 130 SkIRect SkTileImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix&, M apDirection) const { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 144 str->appendf(" dst: %.2f %.2f %.2f %.2f", | 158 str->appendf(" dst: %.2f %.2f %.2f %.2f", |
| 145 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto m); | 159 fDstRect.fLeft, fDstRect.fTop, fDstRect.fRight, fDstRect.fBotto m); |
| 146 if (this->getInput(0)) { | 160 if (this->getInput(0)) { |
| 147 str->appendf("input: ("); | 161 str->appendf("input: ("); |
| 148 this->getInput(0)->toString(str); | 162 this->getInput(0)->toString(str); |
| 149 str->appendf(")"); | 163 str->appendf(")"); |
| 150 } | 164 } |
| 151 str->append(")"); | 165 str->append(")"); |
| 152 } | 166 } |
| 153 #endif | 167 #endif |
| OLD | NEW |