blob: 785f2bf4a5683443dac6ca22e225a5891743e367 [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"
13#include "SkShader.h"
14#include "SkString.h"
15#include "SkXfermode.h"
16
Mike Reed7d954ad2016-10-28 15:42:34 -040017static sk_sp<SkShader> make_shader(SkBlendMode mode) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000018 SkPoint pts[2];
19 SkColor colors[2];
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000020
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000021 pts[0].set(0, 0);
22 pts[1].set(SkIntToScalar(100), 0);
23 colors[0] = SK_ColorRED;
24 colors[1] = SK_ColorBLUE;
reed2ad1aa62016-03-09 09:50:50 -080025 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000026
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000027 pts[0].set(0, 0);
28 pts[1].set(0, SkIntToScalar(100));
29 colors[0] = SK_ColorBLACK;
30 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
reed2ad1aa62016-03-09 09:50:50 -080031 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000032
Mike Reed7d954ad2016-10-28 15:42:34 -040033 return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB), mode);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000034}
35
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000036class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000037public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000038 ComposeShaderGM() {
Mike Reed7d954ad2016-10-28 15:42:34 -040039 fShader = make_shader(SkBlendMode::kDstIn);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000040 }
41
scroggo@google.com32f20eb2012-06-29 17:28:50 +000042protected:
mtklein36352bf2015-03-25 18:17:31 -070043 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000044 return SkString("composeshader");
45 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000048 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000049 }
50
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000052 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000053 paint.setColor(SK_ColorGREEN);
54 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
55 paint.setShader(fShader);
56 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
57 }
58
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000059protected:
reed2ad1aa62016-03-09 09:50:50 -080060 sk_sp<SkShader> fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000061
62private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000063 typedef GM INHERITED ;
64};
65
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000066class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000067public:
68 ComposeShaderAlphaGM() {}
69
70protected:
mtklein36352bf2015-03-25 18:17:31 -070071 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000072 return SkString("composeshader_alpha");
73 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000074
mtklein36352bf2015-03-25 18:17:31 -070075 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070076 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000077 }
reed@google.com775bf662014-05-05 19:37:22 +000078
mtklein36352bf2015-03-25 18:17:31 -070079 void onDraw(SkCanvas* canvas) override {
reed2ad1aa62016-03-09 09:50:50 -080080 sk_sp<SkShader> shaders[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040081 make_shader(SkBlendMode::kDstIn),
82 make_shader(SkBlendMode::kSrcOver),
reed2ad1aa62016-03-09 09:50:50 -080083 };
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000084
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000085 SkPaint paint;
86 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000087
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000088 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000089
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000090 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000091 canvas->save();
92 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
93 paint.setAlpha(0xFF);
halcanary96fcdcc2015-08-27 07:41:13 -070094 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000095 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000096
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000097 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -080098 paint.setShader(shaders[y]);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000099 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000100
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000101 canvas->translate(r.width() + 5, 0);
102 }
103 canvas->restore();
104 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000105 }
106 }
wangyixd7059582015-09-03 08:32:22 -0700107
108private:
109 typedef GM INHERITED ;
110};
111
112
113// creates a square bitmap with red background and a green circle in the center
114static void draw_color_bm(SkBitmap* bm, int length) {
115 SkPaint paint;
116 paint.setColor(SK_ColorGREEN);
117
118 bm->allocN32Pixels(length, length);
119 bm->eraseColor(SK_ColorRED);
120
121 SkCanvas canvas(*bm);
122 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
123 paint);
124}
125
126// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
127static void draw_alpha8_bm(SkBitmap* bm, int length) {
128 SkPaint circlePaint;
129 circlePaint.setColor(SK_ColorBLACK);
130
131 bm->allocPixels(SkImageInfo::MakeA8(length, length));
132 bm->eraseColor(SK_ColorTRANSPARENT);
133
134 SkCanvas canvas(*bm);
135 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
136 circlePaint);
137}
138
139// creates a linear gradient shader
reed2ad1aa62016-03-09 09:50:50 -0800140static sk_sp<SkShader> make_linear_gradient_shader(int length) {
wangyixd7059582015-09-03 08:32:22 -0700141 SkPoint pts[2];
142 SkColor colors[2];
143 pts[0].set(0, 0);
144 pts[1].set(SkIntToScalar(length), 0);
145 colors[0] = SK_ColorBLUE;
146 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
reed2ad1aa62016-03-09 09:50:50 -0800147 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
wangyixd7059582015-09-03 08:32:22 -0700148}
149
150
151class ComposeShaderBitmapGM : public skiagm::GM {
152public:
herbb10fe492016-01-08 13:48:43 -0800153 ComposeShaderBitmapGM() {}
caryclark6df61152016-01-04 14:17:47 -0800154
herbb10fe492016-01-08 13:48:43 -0800155protected:
caryclark6df61152016-01-04 14:17:47 -0800156 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700157 draw_color_bm(&fColorBitmap, squareLength);
158 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
159 SkMatrix s;
160 s.reset();
reed2ad1aa62016-03-09 09:50:50 -0800161 fColorBitmapShader = SkShader::MakeBitmapShader(fColorBitmap, SkShader::kRepeat_TileMode,
162 SkShader::kRepeat_TileMode, &s);
163 fAlpha8BitmapShader = SkShader::MakeBitmapShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
164 SkShader::kRepeat_TileMode, &s);
wangyixd7059582015-09-03 08:32:22 -0700165 fLinearGradientShader = make_linear_gradient_shader(squareLength);
166 }
caryclark6df61152016-01-04 14:17:47 -0800167
wangyixd7059582015-09-03 08:32:22 -0700168 SkString onShortName() override {
169 return SkString("composeshader_bitmap");
170 }
171
172 SkISize onISize() override {
173 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
174 }
175
176 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400177 SkBlendMode mode = SkBlendMode::kDstOver;
wangyixd7059582015-09-03 08:32:22 -0700178
reed2ad1aa62016-03-09 09:50:50 -0800179 sk_sp<SkShader> shaders[] = {
180 // gradient should appear over color bitmap
Mike Reed7d954ad2016-10-28 15:42:34 -0400181 SkShader::MakeComposeShader(fLinearGradientShader, fColorBitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800182 // gradient should appear over alpha8 bitmap colorized by the paint color
Mike Reed7d954ad2016-10-28 15:42:34 -0400183 SkShader::MakeComposeShader(fLinearGradientShader, fAlpha8BitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800184 };
wangyixd7059582015-09-03 08:32:22 -0700185
186 SkPaint paint;
187 paint.setColor(SK_ColorYELLOW);
188
189 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
190 SkIntToScalar(squareLength));
191
192 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
wangyixd7059582015-09-03 08:32:22 -0700193 canvas->save();
194 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
195 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800196 paint.setShader(shaders[y]);
wangyixd7059582015-09-03 08:32:22 -0700197 canvas->drawRect(r, paint);
198
199 canvas->translate(r.width() + 5, 0);
200 }
201 canvas->restore();
202 canvas->translate(0, r.height() + 5);
203 }
204 }
halcanary9d524f22016-03-29 09:03:52 -0700205
wangyixd7059582015-09-03 08:32:22 -0700206private:
reed8681e602016-02-15 08:27:14 -0800207 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700208 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
209 * work in a release build. You can change this parameter and then compile a release build
210 * to have this GM draw larger bitmaps for easier visual inspection.
211 */
mtkleindbfd7ab2016-09-01 11:24:54 -0700212 static constexpr int squareLength = 20;
wangyixd7059582015-09-03 08:32:22 -0700213
214 SkBitmap fColorBitmap;
215 SkBitmap fAlpha8Bitmap;
reed2ad1aa62016-03-09 09:50:50 -0800216 sk_sp<SkShader> fColorBitmapShader;
217 sk_sp<SkShader> fAlpha8BitmapShader;
218 sk_sp<SkShader> fLinearGradientShader;
wangyixd7059582015-09-03 08:32:22 -0700219
220 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000221};
222
caryclarkc2ad65e2016-08-15 12:03:33 -0700223DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) {
224 int width = 255;
225 int height = 255;
226 SkTDArray<uint8_t> dst8Storage;
227 dst8Storage.setCount(width * height);
228 SkTDArray<uint32_t> dst32Storage;
229 dst32Storage.setCount(width * height * sizeof(int32_t));
230 for (int y = 0; y < height; ++y) {
231 for (int x = 0; x < width; ++x) {
232 dst8Storage[y * width + x] = (y + x) / 2;
233 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
234 }
235 }
236 SkPaint paint;
237 paint.setAntiAlias(true);
238 paint.setColor(SK_ColorBLUE);
239 SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)};
240 canvas->drawRect(r, paint);
241 SkBitmap skBitmap, skMask;
242 SkImageInfo imageInfo = SkImageInfo::Make(width, height,
243 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
244 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
245 nullptr, nullptr, nullptr);
246 imageInfo = SkImageInfo::Make(width, height,
247 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
248 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr, nullptr);
249 sk_sp<SkImage> skSrc = SkImage::MakeFromBitmap(skBitmap);
250 sk_sp<SkShader> skSrcShader =
251 skSrc->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
252 sk_sp<SkImage> skMaskImage = SkImage::MakeFromBitmap(skMask);
253 sk_sp<SkShader> skMaskShader = skMaskImage->makeShader(
254 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
caryclarkc2ad65e2016-08-15 12:03:33 -0700255 paint.setShader(
Mike Reed7d954ad2016-10-28 15:42:34 -0400256 SkShader::MakeComposeShader(skMaskShader, skSrcShader, SkBlendMode::kSrcIn));
caryclarkc2ad65e2016-08-15 12:03:33 -0700257 canvas->drawRect(r, paint);
258}
259
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000260//////////////////////////////////////////////////////////////////////////////
261
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000262DEF_GM( return new ComposeShaderGM; )
263DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700264DEF_GM( return new ComposeShaderBitmapGM; )