Debian Codegraph @

gm/bitmapshader.cpp

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