1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkDropShadowImageFilter.h" 9 10 #include "SkBitmap.h" 11 #include "SkBlurImageFilter.h" 12 #include "SkCanvas.h" 13 #include "SkColorMatrixFilter.h" 14 #include "SkDevice.h" 15 #include "SkFlattenableBuffers.h" 16 17 SkDropShadowImageFilter::SkDropShadowImageFilter(SkScalar dx, SkScalar dy, SkScalar sigma, SkColor color, SkImageFilter* input) 18 : SkImageFilter(input) 19 , fDx(dx) 20 , fDy(dy) 21 , fSigma(sigma) 22 , fColor(color) 23 { 24 } 25 26 SkDropShadowImageFilter::SkDropShadowImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) 27 { 28 fDx = buffer.readScalar(); 29 fDy = buffer.readScalar(); 30 fSigma = buffer.readScalar(); 31 fColor = buffer.readColor(); 32 } 33 34 void SkDropShadowImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const 35 { 36 this->INHERITED::flatten(buffer); 37 buffer.writeScalar(fDx); 38 buffer.writeScalar(fDy); 39 buffer.writeScalar(fSigma); 40 buffer.writeColor(fColor); 41 } 42 43 bool SkDropShadowImageFilter::onFilterImage(Proxy* proxy, const SkBitmap& source, const SkMatrix& matrix, SkBitmap* result, SkIPoint* loc) 44 { 45 SkBitmap src = source; 46 if (getInput(0) && !getInput(0)->filterImage(proxy, source, matrix, &src, loc)) 47 return false; 48 49 SkAutoTUnref<SkDevice> device(proxy->createDevice(src.width(), src.height())); 50 SkCanvas canvas(device.get()); 51 52 SkAutoTUnref<SkImageFilter> blurFilter(new SkBlurImageFilter(fSigma, fSigma)); 53 SkAutoTUnref<SkColorFilter> colorFilter(SkColorFilter::CreateModeFilter(fColor, SkXfermode::kSrcIn_Mode)); 54 SkPaint paint; 55 paint.setImageFilter(blurFilter.get()); 56 paint.setColorFilter(colorFilter.get()); 57 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); 58 canvas.drawBitmap(src, fDx, fDy, &paint); 59 canvas.drawBitmap(src, 0, 0); 60 *result = device->accessBitmap(false); 61 return true; 62 } 63