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     virtual SkString onShortName() {
     24         return SkString("resizeimagefilter");
     25     }
     26 
     27     void draw(SkCanvas* canvas,
     28               const SkRect& rect,
     29               const SkSize& deviceSize,
     30               SkFilterQuality filterQuality,
     31               SkImageFilter* input = nullptr) {
     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),
     42                         SkScalarInvert(deviceScaleY));
     43         SkAutoTUnref<SkImageFilter> imageFilter(
     44             SkImageFilter::CreateMatrixFilter(matrix, filterQuality, input));
     45         SkPaint filteredPaint;
     46         filteredPaint.setImageFilter(imageFilter.get());
     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     virtual SkISize onISize() {
     58         return SkISize::Make(520, 100);
     59     }
     60 
     61     virtual void onDraw(SkCanvas* canvas) {
     62         canvas->clear(SK_ColorBLACK);
     63 
     64         SkRect srcRect = SkRect::MakeWH(96, 96);
     65 
     66         SkSize deviceSize = SkSize::Make(16, 16);
     67         draw(canvas,
     68              srcRect,
     69              deviceSize,
     70              kNone_SkFilterQuality);
     71 
     72         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     73         draw(canvas,
     74              srcRect,
     75              deviceSize,
     76              kLow_SkFilterQuality);
     77 
     78         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     79         draw(canvas,
     80              srcRect,
     81              deviceSize,
     82              kMedium_SkFilterQuality);
     83 
     84         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
     85         draw(canvas,
     86              srcRect,
     87              deviceSize,
     88              kHigh_SkFilterQuality);
     89 
     90         SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(16, 16));
     91         SkCanvas* surfaceCanvas = surface->getCanvas();
     92         surfaceCanvas->clear(0x000000);
     93         {
     94             SkPaint paint;
     95             paint.setColor(0xFF00FF00);
     96             SkRect ovalRect = SkRect::MakeWH(16, 16);
     97             ovalRect.inset(SkIntToScalar(2)/3, SkIntToScalar(2)/3);
     98             surfaceCanvas->drawOval(ovalRect, paint);
     99         }
    100         SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
    101         SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
    102         SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
    103         SkAutoTUnref<SkImageFilter> source(
    104             SkImageSource::Create(image, inRect, outRect, kHigh_SkFilterQuality));
    105         canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
    106         draw(canvas,
    107              srcRect,
    108              deviceSize,
    109              kHigh_SkFilterQuality,
    110              source.get());
    111     }
    112 
    113 private:
    114     typedef GM INHERITED;
    115 };
    116 
    117 //////////////////////////////////////////////////////////////////////////////
    118 
    119 static GM* MyFactory(void*) { return new ResizeGM; }
    120 static GMRegistry reg(MyFactory);
    121 
    122 }
    123