blob: 3436018963d55abb0d2adc6bf6561e2b12d5e72b [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00009#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkLayerRasterizer.h"
11#include "SkBlurMaskFilter.h"
12
reed@google.comf2183392011-04-22 14:10:48 +000013static void r0(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
15 SkBlurMaskFilter::kNormal_BlurStyle))->unref();
16 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000017
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 p.setMaskFilter(NULL);
19 p.setStyle(SkPaint::kStroke_Style);
20 p.setStrokeWidth(SK_Scalar1);
21 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000022
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 p.setAlpha(0x11);
24 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000025 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 rast->addLayer(p);
27}
28
reed@google.comf2183392011-04-22 14:10:48 +000029static void r1(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 p.setAlpha(0x40);
reed@android.com0baf1932009-06-24 12:41:42 +000033 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 p.setStyle(SkPaint::kStroke_Style);
35 p.setStrokeWidth(SK_Scalar1*2);
36 rast->addLayer(p);
37}
reed@google.com82065d62011-02-07 15:30:46 +000038
reed@google.comf2183392011-04-22 14:10:48 +000039static void r2(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 p.setStyle(SkPaint::kStrokeAndFill_Style);
41 p.setStrokeWidth(SK_Scalar1*4);
42 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 p.setStyle(SkPaint::kStroke_Style);
45 p.setStrokeWidth(SK_Scalar1*3/2);
reed@android.com0baf1932009-06-24 12:41:42 +000046 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 rast->addLayer(p);
48}
49
reed@google.comf2183392011-04-22 14:10:48 +000050static void r3(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 p.setStyle(SkPaint::kStroke_Style);
52 p.setStrokeWidth(SK_Scalar1*3);
53 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 p.setAlpha(0x20);
56 p.setStyle(SkPaint::kFill_Style);
reed@android.com0baf1932009-06-24 12:41:42 +000057 p.setXfermodeMode(SkXfermode::kSrc_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 rast->addLayer(p);
59}
60
reed@google.comf2183392011-04-22 14:10:48 +000061static void r4(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 p.setAlpha(0x60);
63 rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3));
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 p.setAlpha(0xFF);
reed@android.com0baf1932009-06-24 12:41:42 +000066 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2);
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 p.setXfermode(NULL);
70 rast->addLayer(p);
71}
72
73#include "SkDiscretePathEffect.h"
74
reed@google.comf2183392011-04-22 14:10:48 +000075static void r5(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000079 p.setXfermodeMode(SkXfermode::kSrcOut_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 rast->addLayer(p);
81}
82
reed@google.comf2183392011-04-22 14:10:48 +000083static void r6(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000084 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@android.com8a1c16f2008-12-17 15:59:43 +000086 p.setAntiAlias(false);
87 SkLayerRasterizer* rast2 = new SkLayerRasterizer;
88 r5(rast2, p);
89 p.setRasterizer(rast2)->unref();
reed@android.com0baf1932009-06-24 12:41:42 +000090 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 rast->addLayer(p);
92}
93
94#include "Sk2DPathEffect.h"
95
reed@google.com18dc4772011-08-09 18:47:40 +000096static SkPathEffect* MakeDotEffect(SkScalar radius, const SkMatrix& matrix) {
97 SkPath path;
98 path.addCircle(0, 0, radius);
99 return new SkPath2DPathEffect(matrix, path);
100}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
reed@google.comf2183392011-04-22 14:10:48 +0000102static void r7(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 SkMatrix lattice;
104 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
105 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000106 p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 rast->addLayer(p);
108}
109
reed@google.comf2183392011-04-22 14:10:48 +0000110static void r8(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 SkMatrix lattice;
114 lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0);
115 lattice.postSkew(SK_Scalar1/3, 0, 0, 0);
reed@google.com18dc4772011-08-09 18:47:40 +0000116 p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000117 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000119
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 p.setPathEffect(NULL);
121 p.setXfermode(NULL);
122 p.setStyle(SkPaint::kStroke_Style);
123 p.setStrokeWidth(SK_Scalar1);
124 rast->addLayer(p);
125}
126
reed@google.comf2183392011-04-22 14:10:48 +0000127static void r9(SkLayerRasterizer* rast, SkPaint& p) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000129
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkMatrix lattice;
131 lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0);
132 lattice.postRotate(SkIntToScalar(30), 0, 0);
scroggo@google.comd8a6cc82012-09-12 18:53:49 +0000133 p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref();
reed@android.com0baf1932009-06-24 12:41:42 +0000134 p.setXfermodeMode(SkXfermode::kClear_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 rast->addLayer(p);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 p.setPathEffect(NULL);
138 p.setXfermode(NULL);
139 p.setStyle(SkPaint::kStroke_Style);
140 p.setStrokeWidth(SK_Scalar1);
141 rast->addLayer(p);
142}
143
144typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&);
145
146static const raster_proc gRastProcs[] = {
147 r0, r1, r2, r3, r4, r5, r6, r7, r8, r9
148};
149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#include "SkXfermode.h"
151
reed@google.comf2183392011-04-22 14:10:48 +0000152static void apply_shader(SkPaint* paint, int index) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 raster_proc proc = gRastProcs[index];
154 if (proc)
155 {
156 SkPaint p;
157 SkLayerRasterizer* rast = new SkLayerRasterizer;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 p.setAntiAlias(true);
160 proc(rast, p);
161 paint->setRasterizer(rast)->unref();
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164#if 0
165 SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 };
reed@google.com82065d62011-02-07 15:30:46 +0000166 paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167#endif
168 paint->setColor(SK_ColorBLUE);
169}
170
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000171class TextEffectsGM : public skiagm::GM {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173 TextEffectsGM() {}
174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175protected:
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000176 virtual SkString onShortName() SK_OVERRIDE {
177 return SkString("texteffects");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000180 virtual SkISize onISize() SK_OVERRIDE {
mike@reedtribe.org3e044402012-08-13 01:13:38 +0000181 return SkISize::Make(460, 680);
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000182 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000184 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 canvas->save();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 paint.setAntiAlias(true);
reed@android.com0bb6d062010-05-17 14:50:04 +0000189 paint.setTextSize(SkIntToScalar(56));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190
reed@android.com0bb6d062010-05-17 14:50:04 +0000191 SkScalar x = SkIntToScalar(20);
192 SkScalar y = paint.getTextSize();
rmistry@google.comae933ce2012-08-23 18:19:56 +0000193
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000194 SkString str("Hamburgefons");
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000196 for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 apply_shader(&paint, i);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000199 // paint.setMaskFilter(NULL);
200 // paint.setColor(SK_ColorBLACK);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 canvas->drawText(str.c_str(), str.size(), x, y, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 y += paint.getFontSpacing();
205 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000206
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 }
mike@reedtribe.orgd6c38132012-08-13 01:57:05 +0000209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210private:
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000211 typedef skiagm::GM INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000213
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214//////////////////////////////////////////////////////////////////////////////
215
mike@reedtribe.org3d1cb972012-08-13 00:52:07 +0000216static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; }
217static skiagm::GMRegistry reg(MyFactory);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000218