epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 7 | |
| 8 | #include "gm.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 9 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkLayerRasterizer.h" |
| 11 | #include "SkBlurMaskFilter.h" |
| 12 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 13 | static void r0(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3), |
| 15 | SkBlurMaskFilter::kNormal_BlurStyle))->unref(); |
| 16 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | p.setMaskFilter(NULL); |
| 19 | p.setStyle(SkPaint::kStroke_Style); |
| 20 | p.setStrokeWidth(SK_Scalar1); |
| 21 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | p.setAlpha(0x11); |
| 24 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 25 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | rast->addLayer(p); |
| 27 | } |
| 28 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 29 | static void r1(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | p.setAlpha(0x40); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 33 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | p.setStyle(SkPaint::kStroke_Style); |
| 35 | p.setStrokeWidth(SK_Scalar1*2); |
| 36 | rast->addLayer(p); |
| 37 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 38 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 39 | static void r2(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 41 | p.setStrokeWidth(SK_Scalar1*4); |
| 42 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | p.setStyle(SkPaint::kStroke_Style); |
| 45 | p.setStrokeWidth(SK_Scalar1*3/2); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 46 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | rast->addLayer(p); |
| 48 | } |
| 49 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 50 | static void r3(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | p.setStyle(SkPaint::kStroke_Style); |
| 52 | p.setStrokeWidth(SK_Scalar1*3); |
| 53 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | p.setAlpha(0x20); |
| 56 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 57 | p.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | rast->addLayer(p); |
| 59 | } |
| 60 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 61 | static void r4(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | p.setAlpha(0x60); |
| 63 | rast->addLayer(p, SkIntToScalar(3), SkIntToScalar(3)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | p.setAlpha(0xFF); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 66 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | rast->addLayer(p, SK_Scalar1*3/2, SK_Scalar1*3/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | p.setXfermode(NULL); |
| 70 | rast->addLayer(p); |
| 71 | } |
| 72 | |
| 73 | #include "SkDiscretePathEffect.h" |
| 74 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 75 | static void r5(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | p.setPathEffect(new SkDiscretePathEffect(SK_Scalar1*4, SK_Scalar1*3))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 79 | p.setXfermodeMode(SkXfermode::kSrcOut_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | rast->addLayer(p); |
| 81 | } |
| 82 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 83 | static void r6(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | p.setAntiAlias(false); |
| 87 | SkLayerRasterizer* rast2 = new SkLayerRasterizer; |
| 88 | r5(rast2, p); |
| 89 | p.setRasterizer(rast2)->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 90 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | rast->addLayer(p); |
| 92 | } |
| 93 | |
| 94 | #include "Sk2DPathEffect.h" |
| 95 | |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 96 | static 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 102 | static void r7(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | SkMatrix lattice; |
| 104 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 105 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 106 | p.setPathEffect(MakeDotEffect(SK_Scalar1*4, lattice))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | rast->addLayer(p); |
| 108 | } |
| 109 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 110 | static void r8(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | SkMatrix lattice; |
| 114 | lattice.setScale(SK_Scalar1*6, SK_Scalar1*6, 0, 0); |
| 115 | lattice.postSkew(SK_Scalar1/3, 0, 0, 0); |
reed@google.com | 18dc477 | 2011-08-09 18:47:40 +0000 | [diff] [blame] | 116 | p.setPathEffect(MakeDotEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 117 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | 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.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 127 | static void r9(SkLayerRasterizer* rast, SkPaint& p) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkMatrix lattice; |
| 131 | lattice.setScale(SK_Scalar1, SK_Scalar1*6, 0, 0); |
| 132 | lattice.postRotate(SkIntToScalar(30), 0, 0); |
scroggo@google.com | d8a6cc8 | 2012-09-12 18:53:49 +0000 | [diff] [blame] | 133 | p.setPathEffect(new SkLine2DPathEffect(SK_Scalar1*2, lattice))->unref(); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 134 | p.setXfermodeMode(SkXfermode::kClear_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | rast->addLayer(p); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | p.setPathEffect(NULL); |
| 138 | p.setXfermode(NULL); |
| 139 | p.setStyle(SkPaint::kStroke_Style); |
| 140 | p.setStrokeWidth(SK_Scalar1); |
| 141 | rast->addLayer(p); |
| 142 | } |
| 143 | |
| 144 | typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); |
| 145 | |
| 146 | static const raster_proc gRastProcs[] = { |
| 147 | r0, r1, r2, r3, r4, r5, r6, r7, r8, r9 |
| 148 | }; |
| 149 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | #include "SkXfermode.h" |
| 151 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 152 | static void apply_shader(SkPaint* paint, int index) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | raster_proc proc = gRastProcs[index]; |
| 154 | if (proc) |
| 155 | { |
| 156 | SkPaint p; |
| 157 | SkLayerRasterizer* rast = new SkLayerRasterizer; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | p.setAntiAlias(true); |
| 160 | proc(rast, p); |
| 161 | paint->setRasterizer(rast)->unref(); |
| 162 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 163 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | #if 0 |
| 165 | SkScalar dir[] = { SK_Scalar1, SK_Scalar1, SK_Scalar1 }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 166 | paint->setMaskFilter(SkBlurMaskFilter::CreateEmboss(dir, SK_Scalar1/4, SkIntToScalar(4), SkIntToScalar(3)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | #endif |
| 168 | paint->setColor(SK_ColorBLUE); |
| 169 | } |
| 170 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 171 | class TextEffectsGM : public skiagm::GM { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 173 | TextEffectsGM() {} |
| 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | protected: |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 176 | virtual SkString onShortName() SK_OVERRIDE { |
| 177 | return SkString("texteffects"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 180 | virtual SkISize onISize() SK_OVERRIDE { |
mike@reedtribe.org | 3e04440 | 2012-08-13 01:13:38 +0000 | [diff] [blame] | 181 | return SkISize::Make(460, 680); |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 182 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 184 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 186 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | paint.setAntiAlias(true); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 189 | paint.setTextSize(SkIntToScalar(56)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 190 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 191 | SkScalar x = SkIntToScalar(20); |
| 192 | SkScalar y = paint.getTextSize(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 193 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 194 | SkString str("Hamburgefons"); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 195 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 196 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRastProcs); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | apply_shader(&paint, i); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 198 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 199 | // paint.setMaskFilter(NULL); |
| 200 | // paint.setColor(SK_ColorBLACK); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | canvas->drawText(str.c_str(), str.size(), x, y, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | y += paint.getFontSpacing(); |
| 205 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 206 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | canvas->restore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | } |
mike@reedtribe.org | d6c3813 | 2012-08-13 01:57:05 +0000 | [diff] [blame] | 209 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | private: |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 211 | typedef skiagm::GM INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 213 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | ////////////////////////////////////////////////////////////////////////////// |
| 215 | |
mike@reedtribe.org | 3d1cb97 | 2012-08-13 00:52:07 +0000 | [diff] [blame] | 216 | static skiagm::GM* MyFactory(void*) { return new TextEffectsGM; } |
| 217 | static skiagm::GMRegistry reg(MyFactory); |