blob: 773fd3d7efbd98cc1d2f7eacf1d640c58ada2987 [file] [log] [blame]
commit-bot@chromium.org7842c812013-12-06 20:14:39 +00001/*
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 */
Ben Wagner6a34f3a2019-05-01 10:59:30 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkBitmap.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040010#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPaint.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#include "include/core/SkRect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040021#include "include/gpu/GrRecordingContext.h"
Greg Daniel719239c2022-04-07 11:20:24 -040022#include "src/gpu/ganesh/GrCaps.h"
23#include "src/gpu/ganesh/GrRecordingContextPriv.h"
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000024
25namespace skiagm {
26
Mike Reed607a3822021-01-24 19:49:21 -050027static sk_sp<SkImage> draw_bm() {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000028 SkPaint bluePaint;
29 bluePaint.setColor(SK_ColorBLUE);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000030
Mike Reed607a3822021-01-24 19:49:21 -050031 SkBitmap bm;
32 bm.allocN32Pixels(20, 20);
33 bm.eraseColor(SK_ColorRED);
34 SkCanvas(bm).drawCircle(10, 10, 5, bluePaint);
35 return bm.asImage();
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000036}
37
Mike Reed607a3822021-01-24 19:49:21 -050038static sk_sp<SkImage> draw_mask() {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000039 SkPaint circlePaint;
40 circlePaint.setColor(SK_ColorBLACK);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000041
Mike Reed607a3822021-01-24 19:49:21 -050042 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();
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000047}
48
49class BitmapShaderGM : public GM {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000050
caryclark63c684a2015-02-25 09:04:04 -080051protected:
mtklein36352bf2015-03-25 18:17:31 -070052 void onOnceBeforeDraw() override {
Mike Kleind46dce32018-08-16 10:17:03 -040053 this->setBGColor(SK_ColorGRAY);
Mike Reed607a3822021-01-24 19:49:21 -050054 fImage = draw_bm();
55 fMask = draw_mask();
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000056 }
57
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000058 SkString getName() const override { return SkString("bitmapshaders"); }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000059
Leandro Lovisolo8f023882023-08-15 21:13:52 +000060 SkISize getISize() override { return SkISize::Make(150, 100); }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000061
mtkleinf0599002015-07-13 06:18:39 -070062 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000063 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000064
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000065 for (int i = 0; i < 2; i++) {
66 SkMatrix s;
67 s.reset();
68 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000069 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000070 s.postTranslate(2, 2);
71 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000072
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000073 canvas->save();
Mike Reed607a3822021-01-24 19:49:21 -050074 paint.setShader(fImage->makeShader(SkSamplingOptions(), s));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000075
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000076 // draw the shader with a bitmap mask
Mike Reed607a3822021-01-24 19:49:21 -050077 canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
Michael Ludwigb7d64b92019-02-11 11:09:15 -050078 // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
Mike Reed607a3822021-01-24 19:49:21 -050079 canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000080
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000081 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000082
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000083 canvas->drawCircle(10, 10, 10, paint);
84 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000085
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000086 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000087
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000088 // clear the shader, colorized by a solid color with a bitmap mask
halcanary96fcdcc2015-08-27 07:41:13 -070089 paint.setShader(nullptr);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000090 paint.setColor(SK_ColorGREEN);
Mike Reed607a3822021-01-24 19:49:21 -050091 canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
92 canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &paint);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000093
94 canvas->translate(0, 25);
95
Mike Reed607a3822021-01-24 19:49:21 -050096 paint.setShader(fMask->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
97 SkSamplingOptions(), s));
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000098 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 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000106 }
107
108private:
Mike Reed607a3822021-01-24 19:49:21 -0500109 sk_sp<SkImage> fImage, fMask;
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000110
John Stiles7571f9e2020-09-02 22:42:33 -0400111 using INHERITED = GM;
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000112};
113
Yuqian Lif3c1f092017-11-29 16:07:22 -0500114DEF_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;
Robert Phillips95c250c2020-06-29 15:36:12 -0400124 if (auto ctx = canvas->recordingContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500125 bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
Yuqian Lif3c1f092017-11-29 16:07:22 -0500126 }
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
Mike Reedb41bd152020-12-12 11:18:31 -0500134 paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror,
135 SkSamplingOptions()));
Yuqian Lif3c1f092017-11-29 16:07:22 -0500136 paint.setColor(SK_ColorRED);
137 paint.setAntiAlias(true);
138 canvas->drawCircle(50, 50, 50, paint);
139 delete [] pixels;
140}
141
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000142//////////////////////////////////////////////////////////////////////////////
143
Hal Canarye964c182019-01-23 10:22:01 -0500144DEF_GM( return new BitmapShaderGM; )
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000145
John Stilesa6841be2020-08-06 14:11:56 -0400146} // namespace skiagm