1/* 2 * Copyright 2011 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 "SkCanvas.h" 10#include "SkColorFilter.h" 11#include "SkColorPriv.h" 12#include "SkShader.h" 13 14#include "SkBlurImageFilter.h" 15#include "SkColorFilterImageFilter.h" 16#include "SkTestImageFilters.h" 17 18/////////////////////////////////////////////////////////////////////////////// 19 20static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 21 SkPaint paint; 22 paint.setImageFilter(imf); 23 paint.setColor(SK_ColorBLACK); 24 canvas->save(); 25 canvas->clipRect(r); 26 canvas->drawPaint(paint); 27 canvas->restore(); 28} 29 30static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 31 SkPaint paint; 32 paint.setColor(SK_ColorMAGENTA); 33 paint.setImageFilter(imf); 34 paint.setAntiAlias(true); 35 canvas->save(); 36 canvas->clipRect(r); 37 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint); 38 canvas->restore(); 39} 40 41static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 42 SkPaint paint; 43 paint.setImageFilter(imf); 44 paint.setColor(SK_ColorGREEN); 45 paint.setAntiAlias(true); 46 paint.setTextSize(r.height()/2); 47 paint.setTextAlign(SkPaint::kCenter_Align); 48 canvas->save(); 49 canvas->clipRect(r); 50 canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); 51 canvas->restore(); 52} 53 54static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 55 SkPaint paint; 56 57 SkIRect bounds; 58 r.roundOut(&bounds); 59 60 SkBitmap bm; 61 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height()); 62 bm.allocPixels(); 63 bm.eraseColor(SK_ColorTRANSPARENT); 64 SkCanvas c(bm); 65 draw_path(&c, r, NULL); 66 67 paint.setImageFilter(imf); 68 canvas->save(); 69 canvas->clipRect(r); 70 canvas->drawBitmap(bm, 0, 0, &paint); 71 canvas->restore(); 72} 73 74static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { 75 SkPaint paint; 76 77 SkIRect bounds; 78 r.roundOut(&bounds); 79 80 SkBitmap bm; 81 bm.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), bounds.height()); 82 bm.allocPixels(); 83 bm.eraseColor(SK_ColorRED); 84 SkCanvas c(bm); 85 86 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44); 87 paint.setColor(SK_ColorGREEN); 88 c.drawRect(SkRect::Make(cropRect), paint); 89 90 paint.setImageFilter(imf); 91 SkPoint loc = { r.fLeft, r.fTop }; 92 canvas->getTotalMatrix().mapPoints(&loc, 1); 93 canvas->drawSprite(bm, 94 SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY), 95 &paint); 96} 97 98/////////////////////////////////////////////////////////////////////////////// 99 100class ImageFiltersCroppedGM : public skiagm::GM { 101public: 102 ImageFiltersCroppedGM () {} 103 104protected: 105 106 virtual SkString onShortName() { 107 return SkString("imagefilterscropped"); 108 } 109 110 virtual SkISize onISize() { return SkISize::Make(400, 640); } 111 112 void draw_frame(SkCanvas* canvas, const SkRect& r) { 113 SkPaint paint; 114 paint.setStyle(SkPaint::kStroke_Style); 115 paint.setColor(SK_ColorRED); 116 canvas->drawRect(r, paint); 117 } 118 119 virtual uint32_t onGetFlags() const { 120 // Because of the use of drawSprite, this test is excluded 121 // from scaled replay tests because drawSprite ignores the 122 // reciprocal scale that is applied at record time, which is 123 // the intended behavior of drawSprite. 124 return kSkipScaledReplay_Flag; 125 } 126 127 virtual void onDraw(SkCanvas* canvas) { 128 void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { 129 draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text 130 }; 131 132 SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorRED, 133 SkXfermode::kSrcIn_Mode); 134 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44); 135 SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10); 136 137 SkImageFilter* filters[] = { 138 NULL, 139 SkColorFilterImageFilter::Create(cf, NULL, &cropRect), 140 new SkBlurImageFilter(1.0f, 1.0f, NULL, &cropRect), 141 new SkBlurImageFilter(8.0f, 0.0f, NULL, &cropRect), 142 new SkBlurImageFilter(0.0f, 8.0f, NULL, &cropRect), 143 new SkBlurImageFilter(8.0f, 8.0f, NULL, &cropRect), 144 new SkBlurImageFilter(8.0f, 8.0f, NULL, &bogusRect), 145 SkColorFilterImageFilter::Create(cf, NULL, &bogusRect), 146 }; 147 cf->unref(); 148 149 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); 150 SkScalar MARGIN = SkIntToScalar(16); 151 SkScalar DX = r.width() + MARGIN; 152 SkScalar DY = r.height() + MARGIN; 153 154 canvas->translate(MARGIN, MARGIN); 155 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { 156 canvas->save(); 157 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { 158 drawProc[j](canvas, r, filters[i]); 159 canvas->translate(0, DY); 160 } 161 canvas->restore(); 162 canvas->translate(DX, 0); 163 } 164 165 for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { 166 SkSafeUnref(filters[j]); 167 } 168 } 169 170private: 171 typedef GM INHERITED; 172}; 173 174/////////////////////////////////////////////////////////////////////////////// 175 176static skiagm::GM* MyFactory(void*) { return new ImageFiltersCroppedGM; } 177static skiagm::GMRegistry reg(MyFactory); 178