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 "gm.h"
      9 #include "sk_tool_utils.h"
     10 
     11 #include "SkImage.h"
     12 #include "SkImageSource.h"
     13 
     14 // This GM exercises the SkImageSource ImageFilter class.
     15 
     16 static void fill_rect_filtered(SkCanvas* canvas,
     17                                const SkRect& clipRect,
     18                                sk_sp<SkImageFilter> filter) {
     19     SkPaint paint;
     20     paint.setImageFilter(std::move(filter));
     21     canvas->save();
     22     canvas->clipRect(clipRect);
     23     canvas->drawPaint(paint);
     24     canvas->restore();
     25 }
     26 
     27 class ImageSourceGM : public skiagm::GM {
     28 public:
     29     ImageSourceGM() { }
     30 
     31 protected:
     32     SkString onShortName() override {
     33         return SkString("imagesource");
     34     }
     35 
     36     SkISize onISize() override { return SkISize::Make(500, 150); }
     37 
     38     void onOnceBeforeDraw() override {
     39         SkBitmap bm = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e");
     40         fImage = SkImage::MakeFromBitmap(bm);
     41     }
     42 
     43     void onDraw(SkCanvas* canvas) override {
     44         canvas->clear(SK_ColorBLACK);
     45 
     46         const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
     47         const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60);
     48         const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100);
     49         const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height());
     50 
     51         {
     52             // Draw an unscaled bitmap.
     53             sk_sp<SkImageFilter> imageSource(SkImageSource::Make(fImage));
     54             fill_rect_filtered(canvas, clipRect, std::move(imageSource));
     55             canvas->translate(SkIntToScalar(100), 0);
     56         }
     57         {
     58             // Draw an unscaled subset of the source bitmap (srcRect -> srcRect).
     59             sk_sp<SkImageFilter> imageSourceSrcRect(
     60                 SkImageSource::Make(fImage, srcRect, srcRect, kHigh_SkFilterQuality));
     61             fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect));
     62             canvas->translate(SkIntToScalar(100), 0);
     63         }
     64         {
     65             // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect).
     66             sk_sp<SkImageFilter> imageSourceSrcRectDstRect(
     67                 SkImageSource::Make(fImage, srcRect, dstRect, kHigh_SkFilterQuality));
     68             fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect));
     69             canvas->translate(SkIntToScalar(100), 0);
     70         }
     71         {
     72             // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect).
     73             sk_sp<SkImageFilter> imageSourceDstRectOnly(
     74                 SkImageSource::Make(fImage, bounds, dstRect, kHigh_SkFilterQuality));
     75             fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly));
     76             canvas->translate(SkIntToScalar(100), 0);
     77         }
     78     }
     79 
     80 private:
     81     sk_sp<SkImage> fImage;
     82     typedef GM INHERITED;
     83 };
     84 
     85 ///////////////////////////////////////////////////////////////////////////////
     86 
     87 DEF_GM( return new ImageSourceGM; )
     88