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
9 #include "SkBitmap.h"
10 #include "SkPaint.h"
11 #include "SkShader.h"
12
13 #include "GrCaps.h"
14 #include "GrContext.h"
15 #include "GrContextPriv.h"
16
17 namespace skiagm {
18
draw_bm(SkBitmap * bm)19 static void draw_bm(SkBitmap* bm) {
20 SkPaint bluePaint;
21 bluePaint.setColor(SK_ColorBLUE);
22
23 bm->allocN32Pixels(20, 20);
24 bm->eraseColor(SK_ColorRED);
25
26 SkCanvas canvas(*bm);
27 canvas.drawCircle(10, 10, 5, bluePaint);
28 }
29
draw_mask(SkBitmap * bm)30 static void draw_mask(SkBitmap* bm) {
31 SkPaint circlePaint;
32 circlePaint.setColor(SK_ColorBLACK);
33
34 bm->allocPixels(SkImageInfo::MakeA8(20, 20));
35 bm->eraseColor(SK_ColorTRANSPARENT);
36
37 SkCanvas canvas(*bm);
38 canvas.drawCircle(10, 10, 10, circlePaint);
39 }
40
41 class BitmapShaderGM : public GM {
42
43 protected:
onOnceBeforeDraw()44 void onOnceBeforeDraw() override {
45 this->setBGColor(SK_ColorGRAY);
46 draw_bm(&fBitmap);
47 draw_mask(&fMask);
48 }
49
onShortName()50 SkString onShortName() override {
51 return SkString("bitmapshaders");
52 }
53
onISize()54 SkISize onISize() override {
55 return SkISize::Make(150, 100);
56 }
57
onDraw(SkCanvas * canvas)58 void onDraw(SkCanvas* canvas) override {
59 SkPaint paint;
60
61 for (int i = 0; i < 2; i++) {
62 SkMatrix s;
63 s.reset();
64 if (1 == i) {
65 s.setScale(1.5f, 1.5f);
66 s.postTranslate(2, 2);
67 }
68
69 canvas->save();
70 paint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode,
71 SkShader::kClamp_TileMode, &s));
72
73 // draw the shader with a bitmap mask
74 canvas->drawBitmap(fMask, 0, 0, &paint);
75 // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
76 canvas->drawBitmap(fMask, 30, 0, &paint);
77
78 canvas->translate(0, 25);
79
80 canvas->drawCircle(10, 10, 10, paint);
81 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
82
83 canvas->translate(0, 25);
84
85 // clear the shader, colorized by a solid color with a bitmap mask
86 paint.setShader(nullptr);
87 paint.setColor(SK_ColorGREEN);
88 canvas->drawBitmap(fMask, 0, 0, &paint);
89 canvas->drawBitmap(fMask, 30, 0, &paint);
90
91 canvas->translate(0, 25);
92
93 paint.setShader(SkShader::MakeBitmapShader(fMask, SkShader::kRepeat_TileMode,
94 SkShader::kRepeat_TileMode, &s));
95 paint.setColor(SK_ColorRED);
96
97 // draw the mask using the shader and a color
98 canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
99 canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
100 canvas->restore();
101 canvas->translate(60, 0);
102 }
103 }
104
105 private:
106 SkBitmap fBitmap;
107 SkBitmap fMask;
108
109 typedef GM INHERITED;
110 };
111
112 DEF_SIMPLE_GM(hugebitmapshader, canvas, 100, 100) {
113 SkPaint paint;
114 SkBitmap bitmap;
115
116 // The huge height will exceed GL_MAX_TEXTURE_SIZE. We test that the GL backend will at least
117 // draw something with a default paint instead of drawing nothing.
118 //
119 // (See https://skia-review.googlesource.com/c/skia/+/73200)
120 int bitmapW = 1;
121 int bitmapH = 60000;
122 if (auto* ctx = canvas->getGrContext()) {
123 bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
124 }
125 bitmap.setInfo(SkImageInfo::MakeA8(bitmapW, bitmapH), bitmapW);
126 uint8_t* pixels = new uint8_t[bitmapH];
127 for(int i = 0; i < bitmapH; ++i) {
128 pixels[i] = i & 0xff;
129 }
130 bitmap.setPixels(pixels);
131
132 paint.setShader(SkShader::MakeBitmapShader(bitmap,
133 SkShader::kMirror_TileMode, SkShader::kMirror_TileMode));
134 paint.setColor(SK_ColorRED);
135 paint.setAntiAlias(true);
136 canvas->drawCircle(50, 50, 50, paint);
137 delete [] pixels;
138 }
139
140 //////////////////////////////////////////////////////////////////////////////
141
142 DEF_GM( return new BitmapShaderGM; )
143
144 }
145