Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2015 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 "SkImageSource.h"
      9 #include "SkSurface.h"
     10 #include "SkTileImageFilter.h"
     11 #include "gm.h"
     12 
     13 static sk_sp<SkImage> create_circle_texture(int size, SkColor color) {
     14     auto surface(SkSurface::MakeRasterN32Premul(size, size));
     15     SkCanvas* canvas = surface->getCanvas();
     16     canvas->clear(0xFF000000);
     17 
     18     SkPaint paint;
     19     paint.setColor(color);
     20     paint.setStrokeWidth(3);
     21     paint.setStyle(SkPaint::kStroke_Style);
     22 
     23     canvas->drawCircle(SkScalarHalf(size), SkScalarHalf(size), SkScalarHalf(size), paint);
     24 
     25     return surface->makeImageSnapshot();
     26 }
     27 
     28 namespace skiagm {
     29 
     30 class BigTileImageFilterGM : public GM {
     31 public:
     32     BigTileImageFilterGM() {
     33         this->setBGColor(0xFF000000);
     34     }
     35 
     36 protected:
     37 
     38     SkString onShortName() override {
     39         return SkString("bigtileimagefilter");
     40     }
     41 
     42     SkISize onISize() override{
     43         return SkISize::Make(kWidth, kHeight);
     44     }
     45 
     46     void onOnceBeforeDraw() override {
     47         fRedImage = create_circle_texture(kBitmapSize, SK_ColorRED);
     48         fGreenImage = create_circle_texture(kBitmapSize, SK_ColorGREEN);
     49     }
     50 
     51     void onDraw(SkCanvas* canvas) override {
     52         canvas->clear(SK_ColorBLACK);
     53 
     54         {
     55             SkPaint p;
     56 
     57             const SkRect bound = SkRect::MakeIWH(kWidth, kHeight);
     58             sk_sp<SkImageFilter> imageSource(SkImageSource::Make(fRedImage));
     59 
     60             sk_sp<SkImageFilter> tif(SkTileImageFilter::Make(
     61                                                     SkRect::MakeIWH(kBitmapSize, kBitmapSize),
     62                                                     SkRect::MakeIWH(kWidth, kHeight),
     63                                                     std::move(imageSource)));
     64 
     65             p.setImageFilter(std::move(tif));
     66 
     67             canvas->saveLayer(&bound, &p);
     68             canvas->restore();
     69         }
     70 
     71         {
     72             SkPaint p2;
     73 
     74             const SkRect bound2 = SkRect::MakeIWH(kBitmapSize, kBitmapSize);
     75 
     76             sk_sp<SkImageFilter> tif(SkTileImageFilter::Make(
     77                                                         SkRect::MakeIWH(kBitmapSize, kBitmapSize),
     78                                                         SkRect::MakeIWH(kBitmapSize, kBitmapSize),
     79                                                         nullptr));
     80 
     81             p2.setImageFilter(std::move(tif));
     82 
     83             canvas->translate(320, 320);
     84             canvas->saveLayer(&bound2, &p2);
     85             canvas->setMatrix(SkMatrix::I());
     86 
     87             SkRect bound3 = SkRect::MakeXYWH(320, 320,
     88                                              SkIntToScalar(kBitmapSize),
     89                                              SkIntToScalar(kBitmapSize));
     90             canvas->drawImageRect(fGreenImage.get(), bound2, bound3, nullptr,
     91                                   SkCanvas::kStrict_SrcRectConstraint);
     92             canvas->restore();
     93         }
     94     }
     95 
     96 private:
     97     static constexpr int kWidth = 512;
     98     static constexpr int kHeight = 512;
     99     static constexpr int kBitmapSize = 64;
    100 
    101     sk_sp<SkImage> fRedImage;
    102     sk_sp<SkImage> fGreenImage;
    103 
    104     typedef GM INHERITED;
    105 };
    106 
    107 //////////////////////////////////////////////////////////////////////////////
    108 
    109 DEF_GM(return new BigTileImageFilterGM;)
    110 }
    111