1 /* 2 * Copyright 2012 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 "gm/gm.h" 9 #include "include/core/SkBitmap.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkImageFilter.h" 14 #include "include/core/SkPaint.h" 15 #include "include/core/SkPoint.h" 16 #include "include/core/SkRect.h" 17 #include "include/core/SkScalar.h" 18 #include "include/core/SkShader.h" 19 #include "include/core/SkSize.h" 20 #include "include/core/SkString.h" 21 #include "include/core/SkTileMode.h" 22 #include "include/core/SkTypeface.h" 23 #include "include/effects/SkGradientShader.h" 24 #include "include/effects/SkImageFilters.h" 25 #include "tools/ToolUtils.h" 26 27 namespace skiagm { 28 29 class MatrixConvolutionGM : public GM { 30 public: MatrixConvolutionGM(SkColor colorOne,SkColor colorTwo,const char * nameSuffix)31 MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix) 32 : fNameSuffix(nameSuffix) { 33 this->setBGColor(0x00000000); 34 fColors[0] = colorOne; 35 fColors[1] = colorTwo; 36 } 37 38 protected: 39 onShortName()40 SkString onShortName() override { 41 return SkStringPrintf("matrixconvolution%s", fNameSuffix); 42 } 43 makeBitmap()44 void makeBitmap() { 45 // Draw our bitmap in N32, so legacy devices get "premul" values they understand 46 fBitmap.allocN32Pixels(80, 80); 47 SkCanvas canvas(fBitmap); 48 canvas.clear(0x00000000); 49 SkPaint paint; 50 paint.setColor(0xFFFFFFFF); 51 SkPoint pts[2] = { {0, 0}, 52 {0, 80.0f} }; 53 SkScalar pos[2] = { 0, 80.0f }; 54 paint.setShader(SkGradientShader::MakeLinear( 55 pts, fColors, pos, 2, SkTileMode::kClamp)); 56 SkFont font(ToolUtils::create_portable_typeface(), 180.0f); 57 canvas.drawString("e", -10.0f, 80.0f, font, paint); 58 } 59 onISize()60 SkISize onISize() override { 61 return SkISize::Make(500, 300); 62 } 63 draw(SkCanvas * canvas,int x,int y,const SkIPoint & kernelOffset,SkTileMode tileMode,bool convolveAlpha,const SkIRect * cropRect=nullptr)64 void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset, 65 SkTileMode tileMode, bool convolveAlpha, 66 const SkIRect* cropRect = nullptr) { 67 SkScalar kernel[9] = { 68 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 69 SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1), 70 SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1), 71 }; 72 SkISize kernelSize = SkISize::Make(3, 3); 73 SkScalar gain = 0.3f, bias = SkIntToScalar(100); 74 SkPaint paint; 75 paint.setImageFilter(SkImageFilters::MatrixConvolution(kernelSize, kernel, gain, bias, 76 kernelOffset, tileMode, 77 convolveAlpha, nullptr, cropRect)); 78 canvas->save(); 79 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 80 const SkRect layerBounds = SkRect::MakeIWH(fBitmap.width(), fBitmap.height()); 81 canvas->clipRect(layerBounds); 82 // This GM is, in part, intended to display the wrapping behavior of the 83 // matrix image filter. The only (rational) way to achieve that for repeat mode 84 // is to create a tight layer. 85 canvas->saveLayer(layerBounds, &paint); 86 canvas->drawBitmap(fBitmap, 0, 0, nullptr); 87 canvas->restore(); 88 canvas->restore(); 89 } 90 onOnceBeforeDraw()91 void onOnceBeforeDraw() override { 92 this->makeBitmap(); 93 } 94 onDraw(SkCanvas * canvas)95 void onDraw(SkCanvas* canvas) override { 96 canvas->clear(SK_ColorBLACK); 97 SkIPoint kernelOffset = SkIPoint::Make(1, 0); 98 SkIRect rect = fBitmap.bounds(); 99 for (int x = 10; x < 310; x += 100) { 100 this->draw(canvas, x, 10, kernelOffset, SkTileMode::kClamp, true, &rect); 101 this->draw(canvas, x, 110, kernelOffset, SkTileMode::kDecal, true, &rect); 102 this->draw(canvas, x, 210, kernelOffset, SkTileMode::kRepeat, true, &rect); 103 kernelOffset.fY++; 104 } 105 kernelOffset.fY = 1; 106 SkIRect smallRect = SkIRect::MakeXYWH(10, 5, 60, 60); 107 this->draw(canvas, 310, 10, kernelOffset, SkTileMode::kClamp, true, &smallRect); 108 this->draw(canvas, 310, 110, kernelOffset, SkTileMode::kDecal, true, &smallRect); 109 this->draw(canvas, 310, 210, kernelOffset, SkTileMode::kRepeat, true, &smallRect); 110 111 this->draw(canvas, 410, 10, kernelOffset, SkTileMode::kClamp, false, &rect); 112 this->draw(canvas, 410, 110, kernelOffset, SkTileMode::kDecal, false, &rect); 113 this->draw(canvas, 410, 210, kernelOffset, SkTileMode::kRepeat, false, &rect); 114 } 115 116 private: 117 SkBitmap fBitmap; 118 SkColor fColors[2]; 119 const char* fNameSuffix; 120 121 typedef GM INHERITED; 122 }; 123 124 ////////////////////////////////////////////////////////////////////////////// 125 126 DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");) 127 DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");) 128 129 } 130