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