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