• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gm.h"
8 #include "sk_tool_utils.h"
9 
10 #include "SkBitmap.h"
11 #include "SkPaint.h"
12 #include "SkShader.h"
13 
14 namespace skiagm {
15 
draw_bm(SkBitmap * bm)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 
draw_mask(SkBitmap * bm)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 class BitmapShaderGM : public GM {
39 
40 protected:
onOnceBeforeDraw()41     void onOnceBeforeDraw() override {
42         this->setBGColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
43         draw_bm(&fBitmap);
44         draw_mask(&fMask);
45     }
46 
onShortName()47     SkString onShortName() override {
48         return SkString("bitmapshaders");
49     }
50 
onISize()51     SkISize onISize() override {
52         return SkISize::Make(150, 100);
53     }
54 
onDraw(SkCanvas * canvas)55     void onDraw(SkCanvas* canvas) override {
56         SkPaint paint;
57 
58         for (int i = 0; i < 2; i++) {
59             SkMatrix s;
60             s.reset();
61             if (1 == i) {
62                 s.setScale(1.5f, 1.5f);
63                 s.postTranslate(2, 2);
64             }
65 
66             canvas->save();
67             paint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode,
68                                                        SkShader::kClamp_TileMode, &s));
69 
70             // draw the shader with a bitmap mask
71             canvas->drawBitmap(fMask, 0, 0, &paint);
72             canvas->drawBitmap(fMask, 30, 0, &paint);
73 
74             canvas->translate(0, 25);
75 
76             canvas->drawCircle(10, 10, 10, paint);
77             canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
78 
79             canvas->translate(0, 25);
80 
81             // clear the shader, colorized by a solid color with a bitmap mask
82             paint.setShader(nullptr);
83             paint.setColor(SK_ColorGREEN);
84             canvas->drawBitmap(fMask, 0, 0, &paint);
85             canvas->drawBitmap(fMask, 30, 0, &paint);
86 
87             canvas->translate(0, 25);
88 
89             paint.setShader(SkShader::MakeBitmapShader(fMask, SkShader::kRepeat_TileMode,
90                                                        SkShader::kRepeat_TileMode, &s));
91             paint.setColor(SK_ColorRED);
92 
93             // draw the mask using the shader and a color
94             canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
95             canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
96             canvas->restore();
97             canvas->translate(60, 0);
98         }
99     }
100 
101 private:
102     SkBitmap fBitmap;
103     SkBitmap fMask;
104 
105     typedef GM INHERITED;
106 };
107 
108 //////////////////////////////////////////////////////////////////////////////
109 
MyFactory(void *)110 static GM* MyFactory(void*) { return new BitmapShaderGM; }
111 static GMRegistry reg(MyFactory);
112 
113 }
114