blob: 38069939fdddeeb41a46f933167afaf48fae90fb [file] [log] [blame]
scroggo@google.com32f20eb2012-06-29 17:28:50 +00001/*
2 * Copyright 2012 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.h"
9
10#include "SkCanvas.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000011#include "SkGradientShader.h"
12#include "SkGraphics.h"
Florin Malitaab244f02017-05-03 19:16:58 +000013#include "SkImage.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000014#include "SkShader.h"
15#include "SkString.h"
Mike Klein33d20552017-03-22 13:47:51 -040016#include "SkTDArray.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000017
Mike Reed7d954ad2016-10-28 15:42:34 -040018static sk_sp<SkShader> make_shader(SkBlendMode mode) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000019 SkPoint pts[2];
20 SkColor colors[2];
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000021
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000022 pts[0].set(0, 0);
23 pts[1].set(SkIntToScalar(100), 0);
24 colors[0] = SK_ColorRED;
25 colors[1] = SK_ColorBLUE;
reed2ad1aa62016-03-09 09:50:50 -080026 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000027
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000028 pts[0].set(0, 0);
29 pts[1].set(0, SkIntToScalar(100));
30 colors[0] = SK_ColorBLACK;
31 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
reed2ad1aa62016-03-09 09:50:50 -080032 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000033
Mike Reed7d954ad2016-10-28 15:42:34 -040034 return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB), mode);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000035}
36
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000037class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000038public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000039 ComposeShaderGM() {
Mike Reed7d954ad2016-10-28 15:42:34 -040040 fShader = make_shader(SkBlendMode::kDstIn);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000041 }
42
scroggo@google.com32f20eb2012-06-29 17:28:50 +000043protected:
mtklein36352bf2015-03-25 18:17:31 -070044 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000045 return SkString("composeshader");
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000049 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000050 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000053 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000054 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050055 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000056 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050057 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000058 }
59
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000060protected:
reed2ad1aa62016-03-09 09:50:50 -080061 sk_sp<SkShader> fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000062
63private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000064 typedef GM INHERITED ;
65};
66
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000067class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000068public:
69 ComposeShaderAlphaGM() {}
70
71protected:
mtklein36352bf2015-03-25 18:17:31 -070072 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000073 return SkString("composeshader_alpha");
74 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000075
mtklein36352bf2015-03-25 18:17:31 -070076 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070077 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000078 }
reed@google.com775bf662014-05-05 19:37:22 +000079
mtklein36352bf2015-03-25 18:17:31 -070080 void onDraw(SkCanvas* canvas) override {
reed2ad1aa62016-03-09 09:50:50 -080081 sk_sp<SkShader> shaders[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040082 make_shader(SkBlendMode::kDstIn),
83 make_shader(SkBlendMode::kSrcOver),
reed2ad1aa62016-03-09 09:50:50 -080084 };
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000085
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000086 SkPaint paint;
87 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000088
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000089 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000090
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000091 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000092 canvas->save();
93 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
94 paint.setAlpha(0xFF);
halcanary96fcdcc2015-08-27 07:41:13 -070095 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000096 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000097
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000098 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -080099 paint.setShader(shaders[y]);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000100 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000101
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000102 canvas->translate(r.width() + 5, 0);
103 }
104 canvas->restore();
105 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000106 }
107 }
wangyixd7059582015-09-03 08:32:22 -0700108
109private:
110 typedef GM INHERITED ;
111};
112
113
114// creates a square bitmap with red background and a green circle in the center
115static void draw_color_bm(SkBitmap* bm, int length) {
116 SkPaint paint;
117 paint.setColor(SK_ColorGREEN);
118
119 bm->allocN32Pixels(length, length);
120 bm->eraseColor(SK_ColorRED);
121
122 SkCanvas canvas(*bm);
123 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
124 paint);
125}
126
127// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
128static void draw_alpha8_bm(SkBitmap* bm, int length) {
129 SkPaint circlePaint;
130 circlePaint.setColor(SK_ColorBLACK);
131
132 bm->allocPixels(SkImageInfo::MakeA8(length, length));
133 bm->eraseColor(SK_ColorTRANSPARENT);
134
135 SkCanvas canvas(*bm);
136 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
137 circlePaint);
138}
139
140// creates a linear gradient shader
reed2ad1aa62016-03-09 09:50:50 -0800141static sk_sp<SkShader> make_linear_gradient_shader(int length) {
wangyixd7059582015-09-03 08:32:22 -0700142 SkPoint pts[2];
143 SkColor colors[2];
144 pts[0].set(0, 0);
145 pts[1].set(SkIntToScalar(length), 0);
146 colors[0] = SK_ColorBLUE;
147 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
reed2ad1aa62016-03-09 09:50:50 -0800148 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
wangyixd7059582015-09-03 08:32:22 -0700149}
150
151
152class ComposeShaderBitmapGM : public skiagm::GM {
153public:
herbb10fe492016-01-08 13:48:43 -0800154 ComposeShaderBitmapGM() {}
caryclark6df61152016-01-04 14:17:47 -0800155
herbb10fe492016-01-08 13:48:43 -0800156protected:
caryclark6df61152016-01-04 14:17:47 -0800157 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700158 draw_color_bm(&fColorBitmap, squareLength);
159 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
160 SkMatrix s;
161 s.reset();
reed2ad1aa62016-03-09 09:50:50 -0800162 fColorBitmapShader = SkShader::MakeBitmapShader(fColorBitmap, SkShader::kRepeat_TileMode,
163 SkShader::kRepeat_TileMode, &s);
164 fAlpha8BitmapShader = SkShader::MakeBitmapShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
165 SkShader::kRepeat_TileMode, &s);
wangyixd7059582015-09-03 08:32:22 -0700166 fLinearGradientShader = make_linear_gradient_shader(squareLength);
167 }
caryclark6df61152016-01-04 14:17:47 -0800168
wangyixd7059582015-09-03 08:32:22 -0700169 SkString onShortName() override {
170 return SkString("composeshader_bitmap");
171 }
172
173 SkISize onISize() override {
174 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
175 }
176
177 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400178 SkBlendMode mode = SkBlendMode::kDstOver;
wangyixd7059582015-09-03 08:32:22 -0700179
reed2ad1aa62016-03-09 09:50:50 -0800180 sk_sp<SkShader> shaders[] = {
181 // gradient should appear over color bitmap
Mike Reed7d954ad2016-10-28 15:42:34 -0400182 SkShader::MakeComposeShader(fLinearGradientShader, fColorBitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800183 // gradient should appear over alpha8 bitmap colorized by the paint color
Mike Reed7d954ad2016-10-28 15:42:34 -0400184 SkShader::MakeComposeShader(fLinearGradientShader, fAlpha8BitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800185 };
wangyixd7059582015-09-03 08:32:22 -0700186
187 SkPaint paint;
188 paint.setColor(SK_ColorYELLOW);
189
190 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
191 SkIntToScalar(squareLength));
192
193 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
wangyixd7059582015-09-03 08:32:22 -0700194 canvas->save();
195 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
196 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800197 paint.setShader(shaders[y]);
wangyixd7059582015-09-03 08:32:22 -0700198 canvas->drawRect(r, paint);
199
200 canvas->translate(r.width() + 5, 0);
201 }
202 canvas->restore();
203 canvas->translate(0, r.height() + 5);
204 }
205 }
halcanary9d524f22016-03-29 09:03:52 -0700206
wangyixd7059582015-09-03 08:32:22 -0700207private:
reed8681e602016-02-15 08:27:14 -0800208 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700209 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
210 * work in a release build. You can change this parameter and then compile a release build
211 * to have this GM draw larger bitmaps for easier visual inspection.
212 */
mtkleindbfd7ab2016-09-01 11:24:54 -0700213 static constexpr int squareLength = 20;
wangyixd7059582015-09-03 08:32:22 -0700214
215 SkBitmap fColorBitmap;
216 SkBitmap fAlpha8Bitmap;
reed2ad1aa62016-03-09 09:50:50 -0800217 sk_sp<SkShader> fColorBitmapShader;
218 sk_sp<SkShader> fAlpha8BitmapShader;
219 sk_sp<SkShader> fLinearGradientShader;
wangyixd7059582015-09-03 08:32:22 -0700220
221 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000222};
223
caryclarkc2ad65e2016-08-15 12:03:33 -0700224DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) {
225 int width = 255;
226 int height = 255;
227 SkTDArray<uint8_t> dst8Storage;
228 dst8Storage.setCount(width * height);
229 SkTDArray<uint32_t> dst32Storage;
230 dst32Storage.setCount(width * height * sizeof(int32_t));
231 for (int y = 0; y < height; ++y) {
232 for (int x = 0; x < width; ++x) {
233 dst8Storage[y * width + x] = (y + x) / 2;
234 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
235 }
236 }
237 SkPaint paint;
238 paint.setAntiAlias(true);
239 paint.setColor(SK_ColorBLUE);
240 SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)};
241 canvas->drawRect(r, paint);
242 SkBitmap skBitmap, skMask;
243 SkImageInfo imageInfo = SkImageInfo::Make(width, height,
244 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
245 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
Mike Reed086a4272017-07-18 10:53:11 -0400246 nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700247 imageInfo = SkImageInfo::Make(width, height,
248 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
Mike Reed086a4272017-07-18 10:53:11 -0400249 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700250 sk_sp<SkImage> skSrc = SkImage::MakeFromBitmap(skBitmap);
caryclarkc2ad65e2016-08-15 12:03:33 -0700251 sk_sp<SkImage> skMaskImage = SkImage::MakeFromBitmap(skMask);
caryclarkc2ad65e2016-08-15 12:03:33 -0700252 paint.setShader(
Mike Reed0acd7952017-04-28 11:12:19 -0400253 SkShader::MakeComposeShader(skMaskImage->makeShader(), skSrc->makeShader(),
254 SkBlendMode::kSrcIn));
caryclarkc2ad65e2016-08-15 12:03:33 -0700255 canvas->drawRect(r, paint);
256}
257
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000258//////////////////////////////////////////////////////////////////////////////
259
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000260DEF_GM( return new ComposeShaderGM; )
261DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700262DEF_GM( return new ComposeShaderBitmapGM; )