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 "gm.h" 9#include "SkAlphaThresholdFilter.h" 10#include "SkRandom.h" 11#include "SkSurface.h" 12 13#define WIDTH 500 14#define HEIGHT 500 15 16namespace { 17 18void draw_rects(SkCanvas* canvas) { 19 SkPaint rectPaint; 20 rectPaint.setColor(0xFF0000FF); 21 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint); 22 rectPaint.setColor(0xBFFF0000); 23 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint); 24 rectPaint.setColor(0x3F00FF00); 25 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint); 26 rectPaint.setColor(0x00000000); 27 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint); 28} 29 30SkPaint create_filter_paint() { 31 SkIRect rects[2]; 32 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300); 33 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT); 34 SkRegion region; 35 region.setRects(rects, 2); 36 37 SkPaint paint; 38 paint.setImageFilter(SkAlphaThresholdFilter::Create(region, 0.2f, 0.7f))->unref(); 39 return paint; 40} 41 42}; 43 44namespace skiagm { 45 46class ImageAlphaThresholdGM : public GM { 47public: 48 ImageAlphaThresholdGM() { 49 this->setBGColor(0xFFFFFFFF); 50 } 51 52protected: 53 54 SkString onShortName() override { 55 return SkString("imagealphathreshold"); 56 } 57 58 SkISize onISize() override { 59 return SkISize::Make(WIDTH, HEIGHT); 60 } 61 62 void onDraw(SkCanvas* canvas) override { 63 SkMatrix matrix; 64 matrix.reset(); 65 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f); 66 matrix.postScale(.8f, .8f); 67 68 canvas->concat(matrix); 69 70 SkPaint paint = create_filter_paint(); 71 canvas->saveLayer(nullptr, &paint); 72 draw_rects(canvas); 73 74 canvas->restore(); 75 } 76 77private: 78 typedef GM INHERITED; 79}; 80 81class ImageAlphaThresholdSurfaceGM : public GM { 82public: 83 ImageAlphaThresholdSurfaceGM() { 84 this->setBGColor(0xFFFFFFFF); 85 } 86 87protected: 88 SkString onShortName() override { 89 return SkString("imagealphathreshold_surface"); 90 } 91 92 SkISize onISize() override { 93 return SkISize::Make(WIDTH, HEIGHT); 94 } 95 96 void onDraw(SkCanvas* canvas) override { 97 SkImageInfo info = SkImageInfo::MakeN32(WIDTH, HEIGHT, kOpaque_SkAlphaType); 98 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); 99 if (nullptr == surface) { 100 surface.reset(SkSurface::NewRaster(info)); 101 } 102 surface->getCanvas()->clear(SK_ColorWHITE); 103 draw_rects(surface->getCanvas()); 104 105 SkAutoTUnref<SkImage> image(surface->newImageSnapshot()); 106 SkPaint paint = create_filter_paint(); 107 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100)); 108 canvas->drawImage(image, 0, 0, &paint); 109 } 110 111private: 112 typedef GM INHERITED; 113}; 114 115////////////////////////////////////////////////////////////////////////////// 116 117DEF_GM(return new ImageAlphaThresholdGM();) 118DEF_GM(return new ImageAlphaThresholdSurfaceGM();) 119 120} 121