blob: b46753d6d2cc8967efbac13733011e869c882caa [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000011#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkLayerRasterizer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
reed@google.comf2183392011-04-22 14:10:48 +000014static void r0(SkLayerRasterizer* rast, SkPaint& p) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000015 p.setMaskFilter(SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
16 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3))))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000018
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 p.setMaskFilter(NULL);
20 p.setStyle(SkPaint::kStroke_Style);
21 p.setStrokeWidth(SK_Scalar1);
22 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 p.setAlpha(0x11);
25 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000026 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 rast->addLayer(p);
28}
29
reed@google.comf2183392011-04-22 14:10:48 +000030static void r1(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +000034 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 p.setStyle(SkPaint::kStroke_Style);
36 p.setStrokeWidth(SK_Scalar1*2);
37 rast->addLayer(p);
38}
reed@google.com82065d62011-02-07 15:30:46 +000039
reed@google.comf2183392011-04-22 14:10:48 +000040static void r2(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 p.setStyle(SkPaint::kStrokeAndFill_Style);
42 p.setStrokeWidth(SK_Scalar1*4);
43 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 p.setStyle(SkPaint::kStroke_Style);
46 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +000047 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 rast->addLayer(p);
49}
50
reed@google.comf2183392011-04-22 14:10:48 +000051static void r3(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 p.setStyle(SkPaint::kStroke_Style);
53 p.setStrokeWidth(SK_Scalar1*3);
54 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 p.setAlpha(0x20);
57 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000058 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 rast->addLayer(p);
60}
61
reed@google.comf2183392011-04-22 14:10:48 +000062static void r4(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 p.setAlpha(0x60);
64 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000065
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +000067 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 p.setXfermode(NULL);
71 rast->addLayer(p);
72}
73
74#include "SkDiscretePathEffect.h"
75
reed@google.comf2183392011-04-22 14:10:48 +000076static void r5(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000078
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000080 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 rast->addLayer(p);
82}
83
reed@google.comf2183392011-04-22 14:10:48 +000084static void r6(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 p.setAntiAlias(false);
88 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
89 r5(rast2, p);
90 p.setRasterizer(rast2)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000091 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 rast->addLayer(p);
93}
94
95#include "Sk2DPathEffect.h"
96
reed@google.com18dc4772011-08-09 18:47:40 +000097static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
98 SkPath path;
99 path.addCircle(0, 0, radius);
100 return new SkPath2DPathEffect(matrix, path);
101}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102
reed@google.comf2183392011-04-22 14:10:48 +0000103static void r7(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 SkMatrix lattice;
105 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
106 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000107 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 rast->addLayer(p);
109}
110
reed@google.comf2183392011-04-22 14:10:48 +0000111static void r8(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000113
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkMatrix lattice;
115 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
116 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000117 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000118 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 p.setPathEffect(NULL);
122 p.setXfermode(NULL);
123 p.setStyle(SkPaint::kStroke_Style);
124 p.setStrokeWidth(SK_Scalar1);
125 rast->addLayer(p);
126}
127
reed@google.comf2183392011-04-22 14:10:48 +0000128static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 SkMatrix lattice;
132 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
133 lattice.postRotate(SkIntToScalar(30), 0, 0);
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000134 p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000135 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 p.setPathEffect(NULL);
139 p.setXfermode(NULL);
140 p.setStyle(SkPaint::kStroke_Style);
141 p.setStrokeWidth(SK_Scalar1);
142 rast->addLayer(p);
143}
144
145typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
146
147static const raster_proc gRastProcs[] = {
148 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
149};
150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151#include "SkXfermode.h"
152
reed@google.comf2183392011-04-22 14:10:48 +0000153static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 raster_proc proc = gRastProcs[index];
155 if (proc)
156 {
157 SkPaint p;
158 SkLayerRasterizer* rast = new SkLayerRasterizer;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 p.setAntiAlias(true);
161 proc(rast, p);
162 paint->setRasterizer(rast)->unref();
163 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165#if 0
166 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000167 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168#endif
169 paint->setColor(SK_ColorBLUE);
170}
171
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000172class TextEffectsGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000174 TextEffectsGM() {}
175
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176protected:
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000177 virtual SkString onShortName() SK_OVERRIDE {
178 return SkString("texteffects");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000181 virtual SkISize onISize() SK_OVERRIDE {
mike@reedtribe.org3e044402012-08-13 01:13:38 +0000182 return SkISize::Make(460, 680);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000183 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000185 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 paint.setAntiAlias(true);
reed@android.com0bb6d062010-05-17 14:50:04 +0000190 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191
reed@android.com0bb6d062010-05-17 14:50:04 +0000192 SkScalar x = SkIntToScalar(20);
193 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000195 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000197 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000200 // paint.setMaskFilter(NULL);
201 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000202
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 canvas->drawText(str.c_str(), str.size(), x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 y += paint.getFontSpacing();
206 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209 }
mike@reedtribe.orgd6c38132012-08-13 01:57:05 +0000210
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211private:
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000212 typedef skiagm::GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000214
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215//////////////////////////////////////////////////////////////////////////////
216
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000217static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; }
218static skiagm::GMRegistry reg(MyFactory);