C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / gm / bitmapshader.cpp
1
2 /*
3  * Copyright 2013 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "gm.h"
9
10 #include "SkBitmap.h"
11 #include "SkPaint.h"
12 #include "SkShader.h"
13
14 namespace skiagm {
15
16 static void draw_bm(SkBitmap* bm) {
17     SkPaint bluePaint;
18     bluePaint.setColor(SK_ColorBLUE);
19
20     bm->allocN32Pixels(20, 20);
21     bm->eraseColor(SK_ColorRED);
22
23     SkCanvas canvas(*bm);
24     canvas.drawCircle(10, 10, 5, bluePaint);
25 }
26
27 static void draw_mask(SkBitmap* bm) {
28     SkPaint circlePaint;
29     circlePaint.setColor(SK_ColorBLACK);
30
31     bm->allocPixels(SkImageInfo::MakeA8(20, 20));
32     bm->eraseColor(SK_ColorTRANSPARENT);
33
34     SkCanvas canvas(*bm);
35     canvas.drawCircle(10, 10, 10, circlePaint);
36 }
37
38 static void adopt_shader(SkPaint* paint, SkShader* shader) {
39     paint->setShader(shader);
40     SkSafeUnref(shader);
41 }
42
43 class BitmapShaderGM : public GM {
44
45 protected:
46     void onOnceBeforeDraw() override {
47         this->setBGColor(SK_ColorGRAY);
48         draw_bm(&fBitmap);
49         draw_mask(&fMask);
50     }
51
52     virtual SkString onShortName() {
53         return SkString("bitmapshaders");
54     }
55
56     virtual SkISize onISize() {
57         return SkISize::Make(150, 100);
58     }
59
60     virtual void onDraw(SkCanvas* canvas) {
61         SkPaint paint;
62
63         for (int i = 0; i < 2; i++) {
64             SkMatrix s;
65             s.reset();
66             if (1 == i) {
67                 s.setScale(1.5f, 1.5f);
68                 s.postTranslate(2, 2);
69             }
70
71             canvas->save();
72             adopt_shader(&paint, SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode,
73                                                               SkShader::kClamp_TileMode, &s));
74
75             // draw the shader with a bitmap mask
76             canvas->drawBitmap(fMask, 0, 0, &paint);
77             canvas->drawBitmap(fMask, 30, 0, &paint);
78
79             canvas->translate(0, 25);
80
81             canvas->drawCircle(10, 10, 10, paint);
82             canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
83
84             canvas->translate(0, 25);
85
86             // clear the shader, colorized by a solid color with a bitmap mask
87             paint.setShader(NULL);
88             paint.setColor(SK_ColorGREEN);
89             canvas->drawBitmap(fMask, 0, 0, &paint);
90             canvas->drawBitmap(fMask, 30, 0, &paint);
91
92             canvas->translate(0, 25);
93
94             adopt_shader(&paint, SkShader::CreateBitmapShader(fMask, SkShader::kRepeat_TileMode,
95                                                               SkShader::kRepeat_TileMode, &s));
96             paint.setColor(SK_ColorRED);
97
98             // draw the mask using the shader and a color
99             canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
100             canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
101             canvas->restore();
102             canvas->translate(60, 0);
103         }
104     }
105
106 private:
107     SkBitmap fBitmap;
108     SkBitmap fMask;
109
110     typedef GM INHERITED;
111 };
112
113 //////////////////////////////////////////////////////////////////////////////
114
115 static GM* MyFactory(void*) { return new BitmapShaderGM; }
116 static GMRegistry reg(MyFactory);
117
118 }