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 "SkColor.h"
     10 #include "SkImageSource.h"
     11 #include "SkRefCnt.h"
     12 #include "SkSurface.h"
     13 
     14 namespace skiagm {
     15 
     16 class ResizeGM : public GM {
     17 public:
     18     ResizeGM() {
     19         this->setBGColor(0x00000000);
     20     }
     21 
     22 protected:
     23     SkString onShortName() override {
     24         return SkString("resizeimagefilter");
     25     }
     26 
     27     void draw(SkCanvas* canvas,
     28               const SkRect& rect,
     29               const SkSize& deviceSize,
     30               SkFilterQuality filterQuality,
     31               sk_sp<SkImageFilter> input) {
     32         SkRect dstRect;
     33         canvas->getTotalMatrix().mapRect(&dstRect, rect);
     34         canvas->save();
     35         SkScalar deviceScaleX = deviceSize.width() / dstRect.width();
     36         SkScalar deviceScaleY = deviceSize.height() / dstRect.height();
     37         canvas->translate(rect.x(), rect.y());
     38         canvas->scale(deviceScaleX, deviceScaleY);
     39         canvas->translate(-rect.x(), -rect.y());
     40         SkMatrix matrix;
     41         matrix.setScale(SkScalarInvert(deviceScaleX), SkScalarInvert(deviceScaleY));
     42         sk_sp<SkImageFilter> filter(SkImageFilter::MakeMatrixFilter(matrix,
     43                                                                     filterQuality,
     44                                                                     std::move(input)));
     45         SkPaint filteredPaint;
     46         filteredPaint.setImageFilter(std::move(filter));
     47         canvas->saveLayer(&rect, &filteredPaint);
     48         SkPaint paint;
     49         paint.setColor(0xFF00FF00);
     50         SkRect ovalRect = rect;
     51         ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
     52         canvas->drawOval(ovalRect, paint);
     53         canvas->restore(); // for saveLayer
     54         canvas->restore();
     55     }
     56 
     57     SkISize onISize() override {
     58         return SkISize::Make(520, 100);
     59     }
     60 
     61     void onDraw(SkCanvas* canvas) override {
     62         canvas->clear(SK_ColorBLACK);
     63 
     64         SkRect srcRect = SkRect::MakeWH(96, 96);
     65 
     66         SkSize deviceSize = SkSize::Make(16, 16);
     67         this->draw(canvas,
     68                    srcRect,
     69                    deviceSize,
     70                    kNone_SkFilterQuality,
     71                    nullptr);
     72 
     73         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     74         this->draw(canvas,
     75                    srcRect,
     76                    deviceSize,
     77                    kLow_SkFilterQuality,
     78                    nullptr);
     79 
     80         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     81         this->draw(canvas,
     82                    srcRect,
     83                    deviceSize,
     84                    kMedium_SkFilterQuality,
     85                    nullptr);
     86 
     87         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     88         this->draw(canvas,
     89                    srcRect,
     90                    deviceSize,
     91                    kHigh_SkFilterQuality,
     92                    nullptr);
     93 
     94         sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(16, 16));
     95         SkCanvas* surfaceCanvas = surface->getCanvas();
     96         surfaceCanvas->clear(0x000000);
     97         {
     98             SkPaint paint;
     99             paint.setColor(0xFF00FF00);
    100             SkRect ovalRect = SkRect::MakeWH(16, 16);
    101             ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3);
    102             surfaceCanvas->drawOval(ovalRect, paint);
    103         }
    104         sk_sp<SkImage> image(surface->makeImageSnapshot());
    105         SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
    106         SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
    107         sk_sp<SkImageFilter> source(
    108             SkImageSource::Make(std::move(image), inRect, outRect, kHigh_SkFilterQuality));
    109         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
    110         this->draw(canvas,
    111                    srcRect,
    112                    deviceSize,
    113                    kHigh_SkFilterQuality,
    114                    std::move(source));
    115     }
    116 
    117 private:
    118     typedef GM INHERITED;
    119 };
    120 
    121 //////////////////////////////////////////////////////////////////////////////
    122 
    123 DEF_GM(return new ResizeGM; )
    124 
    125 }
    126