Home | History | Annotate | Download | only in gm
      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:
     21     ImageResizeTiledGM() {
     22     }
     23 
     24 protected:
     25     virtual uint32_t onGetFlags() const SK_OVERRIDE { return kNoBBH_Flag; }
     26 
     27     virtual SkString onShortName() SK_OVERRIDE {
     28         return SkString("imageresizetiled");
     29     }
     30 
     31     virtual SkISize onISize() SK_OVERRIDE {
     32         return SkISize::Make(WIDTH, HEIGHT);
     33     }
     34 
     35     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     36         SkPaint paint;
     37         SkMatrix matrix;
     38         matrix.setScale(RESIZE_FACTOR, RESIZE_FACTOR);
     39         SkAutoTUnref<SkImageFilter> imageFilter(
     40             SkMatrixImageFilter::Create(matrix, SkPaint::kNone_FilterLevel));
     41         paint.setImageFilter(imageFilter.get());
     42         const SkScalar tile_size = SkIntToScalar(100);
     43         SkRect bounds;
     44         canvas->getClipBounds(&bounds);
     45         for (SkScalar y = 0; y < HEIGHT; y += tile_size) {
     46             for (SkScalar x = 0; x < WIDTH; x += tile_size) {
     47                 canvas->save();
     48                 canvas->clipRect(SkRect::MakeXYWH(x, y, tile_size, tile_size));
     49                 canvas->scale(SkScalarInvert(RESIZE_FACTOR),
     50                               SkScalarInvert(RESIZE_FACTOR));
     51                 canvas->saveLayer(NULL, &paint);
     52                 const char* str[] = {
     53                     "The quick",
     54                     "brown fox",
     55                     "jumped over",
     56                     "the lazy dog.",
     57                 };
     58                 SkPaint textPaint;
     59                 textPaint.setAntiAlias(true);
     60                 sk_tool_utils::set_portable_typeface(&textPaint);
     61                 textPaint.setTextSize(SkIntToScalar(100));
     62                 int posY = 0;
     63                 for (unsigned i = 0; i < SK_ARRAY_COUNT(str); i++) {
     64                     posY += 100;
     65                     canvas->drawText(str[i], strlen(str[i]), SkIntToScalar(0),
     66                                      SkIntToScalar(posY), textPaint);
     67                 }
     68                 canvas->restore();
     69                 canvas->restore();
     70             }
     71         }
     72     }
     73 
     74 private:
     75     typedef GM INHERITED;
     76 };
     77 
     78 //////////////////////////////////////////////////////////////////////////////
     79 
     80 DEF_GM(return new ImageResizeTiledGM(); )
     81 
     82 }
     83