blob: cbf153a5d136fec4e7d8dcfdb6f760b1e41dce06 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrCaps.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040023#include "src/gpu/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
mtkleinf0599002015-07-13 06:18:39 -070058 SkString onShortName() override {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000059 return SkString("bitmapshaders");
60 }
61
mtkleinf0599002015-07-13 06:18:39 -070062 SkISize onISize() override {
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000063 return SkISize::Make(150, 100);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000064 }
65
mtkleinf0599002015-07-13 06:18:39 -070066 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000067 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000068
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000069 for (int i = 0; i < 2; i++) {
70 SkMatrix s;
71 s.reset();
72 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000073 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000074 s.postTranslate(2, 2);
75 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000076
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000077 canvas->save();
Mike Reed607a3822021-01-24 19:49:21 -050078 paint.setShader(fImage->makeShader(SkSamplingOptions(), s));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000079
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000080 // draw the shader with a bitmap mask
Mike Reed607a3822021-01-24 19:49:21 -050081 canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
Michael Ludwigb7d64b92019-02-11 11:09:15 -050082 // no blue circle expected (the bitmap shader's coordinates are aligned to CTM still)
Mike Reed607a3822021-01-24 19:49:21 -050083 canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000084
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000085 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000086
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000087 canvas->drawCircle(10, 10, 10, paint);
88 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000089
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000090 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000091
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000092 // clear the shader, colorized by a solid color with a bitmap mask
halcanary96fcdcc2015-08-27 07:41:13 -070093 paint.setShader(nullptr);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000094 paint.setColor(SK_ColorGREEN);
Mike Reed607a3822021-01-24 19:49:21 -050095 canvas->drawImage(fMask, 0, 0, SkSamplingOptions(), &paint);
96 canvas->drawImage(fMask, 30, 0, SkSamplingOptions(), &paint);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000097
98 canvas->translate(0, 25);
99
Mike Reed607a3822021-01-24 19:49:21 -0500100 paint.setShader(fMask->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
101 SkSamplingOptions(), s));
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +0000102 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 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000110 }
111
112private:
Mike Reed607a3822021-01-24 19:49:21 -0500113 sk_sp<SkImage> fImage, fMask;
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000114
John Stiles7571f9e2020-09-02 22:42:33 -0400115 using INHERITED = GM;
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000116};
117
Yuqian Lif3c1f092017-11-29 16:07:22 -0500118DEF_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;
Robert Phillips95c250c2020-06-29 15:36:12 -0400128 if (auto ctx = canvas->recordingContext()) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500129 bitmapH = ctx->priv().caps()->maxTextureSize() + 1;
Yuqian Lif3c1f092017-11-29 16:07:22 -0500130 }
131 bitmap.setInfo(SkImageInfo::MakeA8(bitmapW, bitmapH), bitmapW);
132 uint8_t* pixels = new uint8_t[bitmapH];
133 for(int i = 0; i < bitmapH; ++i) {
134 pixels[i] = i & 0xff;
135 }
136 bitmap.setPixels(pixels);
137
Mike Reedb41bd152020-12-12 11:18:31 -0500138 paint.setShader(bitmap.makeShader(SkTileMode::kMirror, SkTileMode::kMirror,
139 SkSamplingOptions()));
Yuqian Lif3c1f092017-11-29 16:07:22 -0500140 paint.setColor(SK_ColorRED);
141 paint.setAntiAlias(true);
142 canvas->drawCircle(50, 50, 50, paint);
143 delete [] pixels;
144}
145
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000146//////////////////////////////////////////////////////////////////////////////
147
Hal Canarye964c182019-01-23 10:22:01 -0500148DEF_GM( return new BitmapShaderGM; )
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000149
John Stilesa6841be2020-08-06 14:11:56 -0400150} // namespace skiagm