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 "SkBitmapDevice.h"
10#include "SkBitmapSource.h"
11#include "SkColor.h"
12#include "SkMatrixImageFilter.h"
13#include "SkRefCnt.h"
14
15namespace skiagm {
16
17class ResizeGM : public GM {
18public:
19    ResizeGM() {
20        this->setBGColor(0x00000000);
21    }
22
23protected:
24    virtual uint32_t onGetFlags() const SK_OVERRIDE {
25        return kSkipTiled_Flag;
26    }
27
28    virtual SkString onShortName() {
29        return SkString("resizeimagefilter");
30    }
31
32    void draw(SkCanvas* canvas,
33              const SkRect& rect,
34              const SkSize& deviceSize,
35              SkPaint::FilterLevel filterLevel,
36              SkImageFilter* input = NULL) {
37        SkRect dstRect;
38        canvas->getTotalMatrix().mapRect(&dstRect, rect);
39        canvas->save();
40        SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width());
41        SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height());
42        canvas->translate(rect.x(), rect.y());
43        canvas->scale(deviceScaleX, deviceScaleY);
44        canvas->translate(-rect.x(), -rect.y());
45        SkMatrix matrix;
46        matrix.setScale(SkScalarInvert(deviceScaleX),
47                        SkScalarInvert(deviceScaleY));
48        SkAutoTUnref<SkImageFilter> imageFilter(
49            SkMatrixImageFilter::Create(matrix, filterLevel, input));
50        SkPaint filteredPaint;
51        filteredPaint.setImageFilter(imageFilter.get());
52        canvas->saveLayer(&rect, &filteredPaint);
53        SkPaint paint;
54        paint.setColor(0xFF00FF00);
55        SkRect ovalRect = rect;
56        ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
57        canvas->drawOval(ovalRect, paint);
58        canvas->restore(); // for saveLayer
59        canvas->restore();
60    }
61
62    virtual SkISize onISize() {
63        return SkISize::Make(520, 100);
64    }
65
66    virtual void onDraw(SkCanvas* canvas) {
67        canvas->clear(0x00000000);
68
69        SkRect srcRect = SkRect::MakeWH(96, 96);
70
71        SkSize deviceSize = SkSize::Make(16, 16);
72        draw(canvas,
73             srcRect,
74             deviceSize,
75             SkPaint::kNone_FilterLevel);
76
77        canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
78        draw(canvas,
79             srcRect,
80             deviceSize,
81             SkPaint::kLow_FilterLevel);
82
83        canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
84        draw(canvas,
85             srcRect,
86             deviceSize,
87             SkPaint::kMedium_FilterLevel);
88
89        canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
90        draw(canvas,
91             srcRect,
92             deviceSize,
93             SkPaint::kHigh_FilterLevel);
94
95        SkBitmap bitmap;
96        bitmap.allocN32Pixels(16, 16);
97        bitmap.eraseARGB(0x00, 0x00, 0x00, 0x00);
98        {
99            SkBitmapDevice bitmapDevice(bitmap);
100            SkCanvas bitmapCanvas(&bitmapDevice);
101            SkPaint paint;
102            paint.setColor(0xFF00FF00);
103            SkRect ovalRect = SkRect::MakeWH(16, 16);
104            ovalRect.inset(SkScalarDiv(2.0f, 3.0f), SkScalarDiv(2.0f, 3.0f));
105            bitmapCanvas.drawOval(ovalRect, paint);
106        }
107        SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
108        SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
109        SkAutoTUnref<SkBitmapSource> source(SkBitmapSource::Create(bitmap, inRect, outRect));
110        canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
111        draw(canvas,
112             srcRect,
113             deviceSize,
114             SkPaint::kHigh_FilterLevel,
115             source.get());
116    }
117
118private:
119    typedef GM INHERITED;
120};
121
122//////////////////////////////////////////////////////////////////////////////
123
124static GM* MyFactory(void*) { return new ResizeGM; }
125static GMRegistry reg(MyFactory);
126
127}
128