blob: 902bed3ea89cced2017728eacc91d67a6f7b3dab [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +00007
8#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
10#include "SkBlurMaskFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000015static void r0(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000016 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000017 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000018 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
halcanary96fcdcc2015-08-27 07:41:13 -070020 p.setMaskFilter(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 p.setStyle(SkPaint::kStroke_Style);
22 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000023 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 p.setAlpha(0x11);
26 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000027 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000028 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000029}
30
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000031static void r1(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
32 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +000035 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 p.setStyle(SkPaint::kStroke_Style);
37 p.setStrokeWidth(SK_Scalar1*2);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000038 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039}
reed@google.com82065d62011-02-07 15:30:46 +000040
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000041static void r2(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 p.setStyle(SkPaint::kStrokeAndFill_Style);
43 p.setStrokeWidth(SK_Scalar1*4);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000044 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 p.setStyle(SkPaint::kStroke_Style);
47 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +000048 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000049 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000050}
51
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000052static void r3(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 p.setStyle(SkPaint::kStroke_Style);
54 p.setStrokeWidth(SK_Scalar1*3);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000055 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 p.setAlpha(0x20);
58 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000059 p.setXfermodeMode(SkXfermode::kSrc_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000060 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061}
62
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000063static void r4(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 p.setAlpha(0x60);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000065 rastBuilder->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000066
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +000068 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000069 rastBuilder->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
halcanary96fcdcc2015-08-27 07:41:13 -070071 p.setXfermode(nullptr);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000072 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000073}
74
75#include "SkDiscretePathEffect.h"
76
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000077static void r5(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
78 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000080 p.setPathEffect(SkDiscretePathEffect::Create(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000081 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000082 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083}
84
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000085static void r6(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
86 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 p.setAntiAlias(false);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000089 SkLayerRasterizer::Builder rastBuilder2;
90 r5(&rastBuilder2, p);
91 p.setRasterizer(rastBuilder2.detachRasterizer())->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000092 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +000093 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094}
95
96#include "Sk2DPathEffect.h"
97
reed@google.com18dc4772011-08-09 18:47:40 +000098static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
99 SkPath path;
100 path.addCircle(0, 0, radius);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000101 return SkPath2DPathEffect::Create(matrix, path);
reed@google.com18dc4772011-08-09 18:47:40 +0000102}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000104static void r7(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 SkMatrix lattice;
106 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
107 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000108 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000109 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110}
111
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000112static void r8(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
113 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkMatrix lattice;
116 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
117 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000118 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000119 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000120 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000121
halcanary96fcdcc2015-08-27 07:41:13 -0700122 p.setPathEffect(nullptr);
123 p.setXfermode(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 p.setStyle(SkPaint::kStroke_Style);
125 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000126 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127}
128
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000129static void r9(SkLayerRasterizer::Builder* rastBuilder, SkPaint& p) {
130 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 SkMatrix lattice;
133 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
134 lattice.postRotate(SkIntToScalar(30), 0, 0);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +0000135 p.setPathEffect(SkLine2DPathEffect::Create(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000136 p.setXfermodeMode(SkXfermode::kClear_Mode);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000137 rastBuilder->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
halcanary96fcdcc2015-08-27 07:41:13 -0700139 p.setPathEffect(nullptr);
140 p.setXfermode(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 p.setStyle(SkPaint::kStroke_Style);
142 p.setStrokeWidth(SK_Scalar1);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000143 rastBuilder->addLayer(p);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144}
145
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000146typedef void (*raster_proc)(SkLayerRasterizer::Builder*, SkPaint&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
148static const raster_proc gRastProcs[] = {
149 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
150};
151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152#include "SkXfermode.h"
153
reed@google.comf2183392011-04-22 14:10:48 +0000154static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 raster_proc proc = gRastProcs[index];
156 if (proc)
157 {
158 SkPaint p;
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000159 SkLayerRasterizer::Builder rastBuilder;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 p.setAntiAlias(true);
commit-bot@chromium.orgf792a1b2014-02-26 13:27:37 +0000162 proc(&rastBuilder, p);
163 paint->setRasterizer(rastBuilder.detachRasterizer())->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166#if 0
167 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000168 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169#endif
170 paint->setColor(SK_ColorBLUE);
171}
172
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000173class TextEffectsGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000175 TextEffectsGM() {}
176
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177protected:
mtklein36352bf2015-03-25 18:17:31 -0700178 SkString onShortName() override {
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000179 return SkString("texteffects");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181
mtklein36352bf2015-03-25 18:17:31 -0700182 SkISize onISize() override {
mike@reedtribe.org3e044402012-08-13 01:13:38 +0000183 return SkISize::Make(460, 680);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000184 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
mtklein36352bf2015-03-25 18:17:31 -0700186 void onDraw(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000188
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700191 sk_tool_utils::set_portable_typeface(&paint);
reed@android.com0bb6d062010-05-17 14:50:04 +0000192 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
reed@android.com0bb6d062010-05-17 14:50:04 +0000194 SkScalar x = SkIntToScalar(20);
195 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000197 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
robertphillips@google.come9cd27d2013-10-16 17:48:11 +0000199 for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
halcanary96fcdcc2015-08-27 07:41:13 -0700202 // paint.setMaskFilter(nullptr);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000203 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 canvas->drawText(str.c_str(), str.size(), x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 y += paint.getFontSpacing();
208 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211 }
mike@reedtribe.orgd6c38132012-08-13 01:57:05 +0000212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213private:
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000214 typedef skiagm::GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000216
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217//////////////////////////////////////////////////////////////////////////////
218
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000219static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; }
220static skiagm::GMRegistry reg(MyFactory);