blob: 2f3d99d36453f5ec586d83ef9819254f6471fa60 [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
wangyixd7059582015-09-03 08:32:22 -070010#include "SkBitmapProcShader.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000011#include "SkCanvas.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkShader.h"
15#include "SkString.h"
16#include "SkXfermode.h"
17
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000018static SkShader* make_shader(SkXfermode::Mode mode) {
19 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;
halcanary96fcdcc2015-08-27 07:41:13 -070026 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, nullptr, 2,
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000027 SkShader::kClamp_TileMode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000028
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000029 pts[0].set(0, 0);
30 pts[1].set(0, SkIntToScalar(100));
31 colors[0] = SK_ColorBLACK;
32 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070033 SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, nullptr, 2,
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000034 SkShader::kClamp_TileMode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000035
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000036 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000037
reed8681e602016-02-15 08:27:14 -080038 return SkShader::CreateComposeShader(shaderA, shaderB, xfer);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000039}
40
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000041class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000042public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000043 ComposeShaderGM() {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000044 fShader = make_shader(SkXfermode::kDstIn_Mode);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000045 }
46
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000047 virtual ~ComposeShaderGM() {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000048 SkSafeUnref(fShader);
49 }
50
51protected:
mtklein36352bf2015-03-25 18:17:31 -070052 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000053 return SkString("composeshader");
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000057 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000058 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000061
62 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000063
64 paint.setColor(SK_ColorGREEN);
65 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
66 paint.setShader(fShader);
67 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
68 }
69
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000070protected:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000071 SkShader* fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000072
73private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000074 typedef GM INHERITED ;
75};
76
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000077class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000078public:
79 ComposeShaderAlphaGM() {}
80
81protected:
mtklein36352bf2015-03-25 18:17:31 -070082 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000083 return SkString("composeshader_alpha");
84 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000085
mtklein36352bf2015-03-25 18:17:31 -070086 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070087 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000088 }
reed@google.com775bf662014-05-05 19:37:22 +000089
mtklein36352bf2015-03-25 18:17:31 -070090 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000091 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
92 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
93 SkShader* shaders[] = { shader0.get(), shader1.get() };
94
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000095 SkPaint paint;
96 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000097
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000098 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000099
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000100 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
101 SkShader* shader = shaders[y];
102 canvas->save();
103 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
104 paint.setAlpha(0xFF);
halcanary96fcdcc2015-08-27 07:41:13 -0700105 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000106 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000107
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000108 paint.setAlpha(alpha);
109 paint.setShader(shader);
110 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000111
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000112 canvas->translate(r.width() + 5, 0);
113 }
114 canvas->restore();
115 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000116 }
117 }
wangyixd7059582015-09-03 08:32:22 -0700118
119private:
120 typedef GM INHERITED ;
121};
122
123
124// creates a square bitmap with red background and a green circle in the center
125static void draw_color_bm(SkBitmap* bm, int length) {
126 SkPaint paint;
127 paint.setColor(SK_ColorGREEN);
128
129 bm->allocN32Pixels(length, length);
130 bm->eraseColor(SK_ColorRED);
131
132 SkCanvas canvas(*bm);
133 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
134 paint);
135}
136
137// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
138static void draw_alpha8_bm(SkBitmap* bm, int length) {
139 SkPaint circlePaint;
140 circlePaint.setColor(SK_ColorBLACK);
141
142 bm->allocPixels(SkImageInfo::MakeA8(length, length));
143 bm->eraseColor(SK_ColorTRANSPARENT);
144
145 SkCanvas canvas(*bm);
146 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
147 circlePaint);
148}
149
150// creates a linear gradient shader
151static SkShader* make_linear_gradient_shader(int length) {
152 SkPoint pts[2];
153 SkColor colors[2];
154 pts[0].set(0, 0);
155 pts[1].set(SkIntToScalar(length), 0);
156 colors[0] = SK_ColorBLUE;
157 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
158 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
159}
160
161
162class ComposeShaderBitmapGM : public skiagm::GM {
163public:
herbb10fe492016-01-08 13:48:43 -0800164 ComposeShaderBitmapGM() {}
165 ~ComposeShaderBitmapGM() {
166 SkSafeUnref(fColorBitmapShader);
167 SkSafeUnref(fAlpha8BitmapShader);
168 SkSafeUnref(fLinearGradientShader);
caryclark6df61152016-01-04 14:17:47 -0800169 }
170
herbb10fe492016-01-08 13:48:43 -0800171protected:
caryclark6df61152016-01-04 14:17:47 -0800172 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700173 draw_color_bm(&fColorBitmap, squareLength);
174 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
175 SkMatrix s;
176 s.reset();
177 fColorBitmapShader = new SkBitmapProcShader(fColorBitmap, SkShader::kRepeat_TileMode,
178 SkShader::kRepeat_TileMode, &s);
179 fAlpha8BitmapShader = new SkBitmapProcShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
180 SkShader::kRepeat_TileMode, &s);
181 fLinearGradientShader = make_linear_gradient_shader(squareLength);
182 }
caryclark6df61152016-01-04 14:17:47 -0800183
wangyixd7059582015-09-03 08:32:22 -0700184 SkString onShortName() override {
185 return SkString("composeshader_bitmap");
186 }
187
188 SkISize onISize() override {
189 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
190 }
191
192 void onDraw(SkCanvas* canvas) override {
193 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
194
195 // gradient should appear over color bitmap
reed8681e602016-02-15 08:27:14 -0800196 SkAutoTUnref<SkShader> shader0(SkShader::CreateComposeShader(fLinearGradientShader,
wangyixd7059582015-09-03 08:32:22 -0700197 fColorBitmapShader,
198 xfer));
199 // gradient should appear over alpha8 bitmap colorized by the paint color
reed8681e602016-02-15 08:27:14 -0800200 SkAutoTUnref<SkShader> shader1(SkShader::CreateComposeShader(fLinearGradientShader,
wangyixd7059582015-09-03 08:32:22 -0700201 fAlpha8BitmapShader,
202 xfer));
203
204 SkShader* shaders[] = { shader0.get(), shader1.get() };
205
206 SkPaint paint;
207 paint.setColor(SK_ColorYELLOW);
208
209 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
210 SkIntToScalar(squareLength));
211
212 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
213 SkShader* shader = shaders[y];
214 canvas->save();
215 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
216 paint.setAlpha(alpha);
217 paint.setShader(shader);
218 canvas->drawRect(r, paint);
219
220 canvas->translate(r.width() + 5, 0);
221 }
222 canvas->restore();
223 canvas->translate(0, r.height() + 5);
224 }
225 }
herbb10fe492016-01-08 13:48:43 -0800226
wangyixd7059582015-09-03 08:32:22 -0700227private:
reed8681e602016-02-15 08:27:14 -0800228 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700229 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
230 * work in a release build. You can change this parameter and then compile a release build
231 * to have this GM draw larger bitmaps for easier visual inspection.
232 */
233 static const int squareLength = 20;
234
235 SkBitmap fColorBitmap;
236 SkBitmap fAlpha8Bitmap;
herbb10fe492016-01-08 13:48:43 -0800237 SkShader* fColorBitmapShader{nullptr};
238 SkShader* fAlpha8BitmapShader{nullptr};
239 SkShader* fLinearGradientShader{nullptr};
wangyixd7059582015-09-03 08:32:22 -0700240
241 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000242};
243
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000244//////////////////////////////////////////////////////////////////////////////
245
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000246DEF_GM( return new ComposeShaderGM; )
247DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700248DEF_GM( return new ComposeShaderBitmapGM; )