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