Home | History | Annotate | Download | only in gm
      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 "sk_tool_utils.h"
      9 #include "SkColorFilterImageFilter.h"
     10 #include "SkColorMatrixFilter.h"
     11 #include "SkImage.h"
     12 #include "SkImageSource.h"
     13 #include "SkTileImageFilter.h"
     14 #include "gm.h"
     15 
     16 #define WIDTH 400
     17 #define HEIGHT 100
     18 #define MARGIN 12
     19 
     20 namespace skiagm {
     21 
     22 class TileImageFilterGM : public GM {
     23 public:
     24     TileImageFilterGM() {
     25         this->setBGColor(0xFF000000);
     26     }
     27 
     28 protected:
     29     SkString onShortName() override {
     30         return SkString("tileimagefilter");
     31     }
     32 
     33     SkISize onISize() override{
     34         return SkISize::Make(WIDTH, HEIGHT);
     35     }
     36 
     37     void onOnceBeforeDraw() override {
     38         fBitmap.reset(SkImage::NewFromBitmap(
     39             sk_tool_utils::create_string_bitmap(50, 50, 0xD000D000, 10, 45, 50, "e")));
     40 
     41         fCheckerboard.reset(SkImage::NewFromBitmap(
     42             sk_tool_utils::create_checkerboard_bitmap(80, 80,
     43                                                       sk_tool_utils::color_to_565(0xFFA0A0A0),
     44                                                       sk_tool_utils::color_to_565(0xFF404040),
     45                                                       8)));
     46     }
     47 
     48     void onDraw(SkCanvas* canvas) override {
     49         canvas->clear(SK_ColorBLACK);
     50         SkPaint red;
     51         red.setColor(SK_ColorRED);
     52         red.setStyle(SkPaint::kStroke_Style);
     53         SkPaint blue;
     54         blue.setColor(SK_ColorBLUE);
     55         blue.setStyle(SkPaint::kStroke_Style);
     56 
     57         int x = 0, y = 0;
     58         for (size_t i = 0; i < 4; i++) {
     59             const SkImage* image = (i & 0x01) ? fCheckerboard : fBitmap;
     60             SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(image->width()/4),
     61                                               SkIntToScalar(image->height()/4),
     62                                               SkIntToScalar(image->width()/(i+1)),
     63                                               SkIntToScalar(image->height()/(i+1)));
     64             SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(i * 8),
     65                                               SkIntToScalar(i * 4),
     66                                               SkIntToScalar(image->width() - i * 12),
     67                                               SkIntToScalar(image->height()) - i * 12);
     68             SkAutoTUnref<SkImageFilter> tileInput(SkImageSource::Create(image));
     69             SkAutoTUnref<SkImageFilter> filter(
     70                 SkTileImageFilter::Create(srcRect, dstRect, tileInput));
     71             canvas->save();
     72             canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     73             SkPaint paint;
     74             paint.setImageFilter(filter);
     75             canvas->drawImage(fBitmap, 0, 0, &paint);
     76             canvas->drawRect(srcRect, red);
     77             canvas->drawRect(dstRect, blue);
     78             canvas->restore();
     79             x += image->width() + MARGIN;
     80             if (x + image->width() > WIDTH) {
     81                 x = 0;
     82                 y += image->height() + MARGIN;
     83             }
     84         }
     85 
     86         SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
     87                                 0, SK_Scalar1, 0, 0, 0,
     88                                 0, 0, SK_Scalar1, 0, 0,
     89                                 0, 0, 0, SK_Scalar1, 0 };
     90 
     91         SkRect srcRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width()),
     92                                         SkIntToScalar(fBitmap->height()));
     93         SkRect dstRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width() * 2),
     94                                         SkIntToScalar(fBitmap->height() * 2));
     95         SkAutoTUnref<SkImageFilter> tile(SkTileImageFilter::Create(srcRect, dstRect, nullptr));
     96         SkAutoTUnref<SkColorFilter> cf(SkColorMatrixFilter::Create(matrix));
     97 
     98         SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf, tile.get()));
     99         SkPaint paint;
    100         paint.setImageFilter(cfif);
    101         canvas->save();
    102         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
    103         canvas->clipRect(dstRect);
    104         canvas->saveLayer(&dstRect, &paint);
    105         canvas->drawImage(fBitmap, 0, 0);
    106         canvas->restore();
    107         canvas->drawRect(srcRect, red);
    108         canvas->drawRect(dstRect, blue);
    109         canvas->restore();
    110     }
    111 private:
    112     SkAutoTUnref<SkImage> fBitmap, fCheckerboard;
    113 
    114     typedef GM INHERITED;
    115 };
    116 
    117 //////////////////////////////////////////////////////////////////////////////
    118 
    119 DEF_GM(return new TileImageFilterGM;)
    120 }
    121