1 /* 2 * Copyright 2014 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 "SkMatrixImageFilter.h" 10 #include "SkRandom.h" 11 12 #define WIDTH 640 13 #define HEIGHT 480 14 15 #define RESIZE_FACTOR SkIntToScalar(2) 16 17 namespace skiagm { 18 19 class ImageResizeTiledGM : public GM { 20 public: ImageResizeTiledGM()21 ImageResizeTiledGM() { 22 } 23 24 protected: onShortName()25 virtual SkString onShortName() SK_OVERRIDE { 26 return SkString("imageresizetiled"); 27 } 28 onISize()29 virtual SkISize onISize() SK_OVERRIDE { 30 return SkISize::Make(WIDTH, HEIGHT); 31 } 32 onDraw(SkCanvas * canvas)33 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 34 SkPaint paint; 35 SkMatrix matrix; 36 matrix.setScale(RESIZE_FACTOR, RESIZE_FACTOR); 37 SkAutoTUnref<SkImageFilter> imageFilter( 38 SkMatrixImageFilter::Create(matrix, SkPaint::kNone_FilterLevel)); 39 paint.setImageFilter(imageFilter.get()); 40 const SkScalar tile_size = SkIntToScalar(100); 41 SkRect bounds; 42 canvas->getClipBounds(&bounds); 43 for (SkScalar y = 0; y < HEIGHT; y += tile_size) { 44 for (SkScalar x = 0; x < WIDTH; x += tile_size) { 45 canvas->save(); 46 canvas->clipRect(SkRect::MakeXYWH(x, y, tile_size, tile_size)); 47 canvas->scale(SkScalarInvert(RESIZE_FACTOR), 48 SkScalarInvert(RESIZE_FACTOR)); 49 canvas->saveLayer(NULL, &paint); 50 const char* str[] = { 51 "The quick", 52 "brown fox", 53 "jumped over", 54 "the lazy dog.", 55 }; 56 SkPaint textPaint; 57 textPaint.setAntiAlias(true); 58 textPaint.setTextSize(SkIntToScalar(100)); 59 int posY = 0; 60 for (unsigned i = 0; i < SK_ARRAY_COUNT(str); i++) { 61 posY += 100; 62 canvas->drawText(str[i], strlen(str[i]), SkIntToScalar(0), 63 SkIntToScalar(posY), textPaint); 64 } 65 canvas->restore(); 66 canvas->restore(); 67 } 68 } 69 } 70 71 private: 72 typedef GM INHERITED; 73 }; 74 75 ////////////////////////////////////////////////////////////////////////////// 76 77 DEF_GM(return new ImageResizeTiledGM(); ) 78 79 } 80