Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
bitmapshader.cpp
Go to the documentation of this file.
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"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
24
25namespace skiagm {
26
28 SkPaint bluePaint;
29 bluePaint.setColor(SK_ColorBLUE);
30
31 SkBitmap bm;
32 bm.allocN32Pixels(20, 20);
34 SkCanvas(bm).drawCircle(10, 10, 5, bluePaint);
35 return bm.asImage();
36}
37
39 SkPaint circlePaint;
40 circlePaint.setColor(SK_ColorBLACK);
41
42 SkBitmap bm;
45 SkCanvas(bm).drawCircle(10, 10, 10, circlePaint);
46 return bm.asImage();
47}
48
49class BitmapShaderGM : public GM {
50
51protected:
52 void onOnceBeforeDraw() override {
54 fImage = draw_bm();
55 fMask = draw_mask();
56 }
57
58 SkString getName() const override { return SkString("bitmapshaders"); }
59
60 SkISize getISize() override { return SkISize::Make(150, 100); }
61
62 void onDraw(SkCanvas* canvas) override {
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
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
108private:
109 sk_sp<SkImage> fImage, fMask;
110
111 using INHERITED = GM;
112};
113
114DEF_SIMPLE_GM(hugebitmapshader, canvas, 100, 100) {
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
136 paint.setColor(SK_ColorRED);
137 paint.setAntiAlias(true);
138 canvas->drawCircle(50, 50, 50, paint);
139 delete [] pixels;
140}
141
142//////////////////////////////////////////////////////////////////////////////
143
144DEF_GM( return new BitmapShaderGM; )
145
146} // namespace skiagm
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
void setColor(SkColor color)
Definition SkPaint.cpp:119
void onOnceBeforeDraw() override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
void setBGColor(SkColor)
Definition gm.cpp:159
const Paint & paint
struct MyStruct s
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static sk_sp< SkImage > draw_bm()
static sk_sp< SkImage > draw_mask()
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeA8(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659