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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkView.h" |
| 11 | #include "Sk1DPathEffect.h" |
| 12 | #include "Sk2DPathEffect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkBlurMaskFilter.h" |
reed | 0daf5dd | 2016-01-11 12:34:04 -0800 | [diff] [blame] | 14 | #include "SkColorMatrixFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkColorPriv.h" |
| 16 | #include "SkCornerPathEffect.h" |
| 17 | #include "SkDashPathEffect.h" |
| 18 | #include "SkDiscretePathEffect.h" |
| 19 | #include "SkEmbossMaskFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 20 | #include "SkReadBuffer.h" |
| 21 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkGradientShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkMath.h" |
| 24 | #include "SkPath.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 25 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #include "SkRegion.h" |
| 27 | #include "SkShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | #include "SkCornerPathEffect.h" |
| 29 | #include "SkPathMeasure.h" |
| 30 | #include "SkPicture.h" |
| 31 | #include "SkRandom.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #include "SkTypeface.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
| 35 | #include <math.h> |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 36 | #include "DecodeFile.h" |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | class Dot2DPathEffect : public Sk2DPathEffect { |
| 39 | public: |
| 40 | Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix) |
| 41 | : Sk2DPathEffect(matrix), fRadius(radius) {} |
| 42 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 43 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | |
| 45 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void next(const SkPoint& loc, int u, int v, SkPath* dst) const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | dst->addCircle(loc.fX, loc.fY, fRadius); |
| 48 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | void flatten(SkWriteBuffer& buffer) const override { |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 51 | this->INHERITED::flatten(buffer); |
| 52 | buffer.writeScalar(fRadius); |
| 53 | } |
| 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | private: |
| 56 | SkScalar fRadius; |
| 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | typedef Sk2DPathEffect INHERITED; |
| 59 | }; |
| 60 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 61 | class DemoView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | public: |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 63 | DemoView() {} |
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 | protected: |
| 66 | // overrides from SkEventSink |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 67 | virtual bool onQuery(SkEvent* evt) { |
| 68 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | SampleCode::TitleR(evt, "Demo"); |
| 70 | return true; |
| 71 | } |
| 72 | return this->INHERITED::onQuery(evt); |
| 73 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 75 | virtual bool onClick(Click* click) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | return this->INHERITED::onClick(click); |
| 77 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 78 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 79 | void makePath(SkPath& path) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | path.addCircle(SkIntToScalar(20), SkIntToScalar(20), SkIntToScalar(20), |
| 81 | SkPath::kCCW_Direction); |
| 82 | for (int index = 0; index < 10; index++) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 83 | SkScalar x = (float) cos(index / 10.0f * 2 * 3.1415925358f); |
| 84 | SkScalar y = (float) sin(index / 10.0f * 2 * 3.1415925358f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | x *= index & 1 ? 7 : 14; |
| 86 | y *= index & 1 ? 7 : 14; |
| 87 | x += SkIntToScalar(20); |
| 88 | y += SkIntToScalar(20); |
| 89 | if (index == 0) |
| 90 | path.moveTo(x, y); |
| 91 | else |
| 92 | path.lineTo(x, y); |
| 93 | } |
| 94 | path.close(); |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 97 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | canvas->save(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 99 | this->drawPicture(canvas, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | canvas->restore(); |
| 101 | |
| 102 | { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 103 | SkPictureRecorder recorder; |
| 104 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | SkCanvas* record = recorder.beginRecording(320, 480, nullptr, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 106 | this->drawPicture(record, 120); |
| 107 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 108 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 109 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | canvas->translate(0, SkIntToScalar(120)); |
| 111 | |
| 112 | SkRect clip; |
| 113 | clip.set(0, 0, SkIntToScalar(160), SkIntToScalar(160)); |
| 114 | do { |
| 115 | canvas->save(); |
| 116 | canvas->clipRect(clip); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 117 | picture->playback(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | canvas->restore(); |
| 119 | if (clip.fRight < SkIntToScalar(320)) |
| 120 | clip.offset(SkIntToScalar(160), 0); |
| 121 | else if (clip.fBottom < SkIntToScalar(480)) |
| 122 | clip.offset(-SkIntToScalar(320), SkIntToScalar(160)); |
| 123 | else |
| 124 | break; |
| 125 | } while (true); |
| 126 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 128 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 129 | void drawPicture(SkCanvas* canvas, int spriteOffset) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | SkMatrix matrix; matrix.reset(); |
| 131 | SkPaint paint; |
| 132 | SkPath path; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | SkPoint start = {0, 0}; |
| 134 | SkPoint stop = { SkIntToScalar(40), SkIntToScalar(40) }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 135 | SkRect rect = {0, 0, SkIntToScalar(40), SkIntToScalar(40) }; |
| 136 | SkRect rect2 = {0, 0, SkIntToScalar(65), SkIntToScalar(20) }; |
| 137 | SkScalar left = 0, top = 0, x = 0, y = 0; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 138 | int index; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
| 140 | char ascii[] = "ascii..."; |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 141 | int asciiLength = sizeof(ascii) - 1; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | char utf8[] = "utf8" "\xe2\x80\xa6"; |
| 143 | short utf16[] = {'u', 't', 'f', '1', '6', 0x2026 }; |
| 144 | short utf16simple[] = {'u', 't', 'f', '1', '6', '!' }; |
| 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | makePath(path); |
Ben Wagner | 1a1d241 | 2017-10-17 17:33:44 -0400 | [diff] [blame] | 147 | SkTDArray<SkPoint> pos; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 148 | pos.setCount(asciiLength); |
| 149 | for (index = 0; index < asciiLength; index++) |
| 150 | pos[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 151 | SkIntToScalar((unsigned int)index * 2)); |
Ben Wagner | 1a1d241 | 2017-10-17 17:33:44 -0400 | [diff] [blame] | 152 | SkTDArray<SkPoint> pos2; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | pos2.setCount(asciiLength); |
| 154 | for (index = 0; index < asciiLength; index++) |
| 155 | pos2[index].set(SkIntToScalar((unsigned int)index * 10), |
tomhudson@google.com | ffe39bd | 2012-05-17 15:38:00 +0000 | [diff] [blame] | 156 | SkIntToScalar(20)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | // shaders |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 159 | SkPoint linearPoints[] = { { 0, 0, }, { SkIntToScalar(40), SkIntToScalar(40) } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | SkColor linearColors[] = { SK_ColorRED, SK_ColorBLUE }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | SkScalar* linearPos = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | int linearCount = 2; |
| 163 | SkShader::TileMode linearMode = SkShader::kMirror_TileMode; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 164 | auto linear = SkGradientShader::MakeLinear(linearPoints, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 165 | linearColors, linearPos, linearCount, linearMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | |
| 167 | SkPoint radialCenter = { SkIntToScalar(25), SkIntToScalar(25) }; |
| 168 | SkScalar radialRadius = SkIntToScalar(25); |
| 169 | SkColor radialColors[] = { SK_ColorGREEN, SK_ColorGRAY, SK_ColorRED }; |
| 170 | SkScalar radialPos[] = { 0, SkIntToScalar(3) / 5, SkIntToScalar(1)}; |
| 171 | int radialCount = 3; |
| 172 | SkShader::TileMode radialMode = SkShader::kRepeat_TileMode; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 173 | auto radial = SkGradientShader::MakeRadial(radialCenter, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | radialRadius, radialColors, radialPos, radialCount, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 175 | radialMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | SkEmbossMaskFilter::Light light; |
| 178 | light.fDirection[0] = SK_Scalar1/2; |
| 179 | light.fDirection[1] = SK_Scalar1/2; |
| 180 | light.fDirection[2] = SK_Scalar1/3; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | light.fAmbient = 0x48; |
| 182 | light.fSpecular = 0x80; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 184 | auto lightingFilter = SkColorMatrixFilter::MakeLightingFilter( |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 185 | 0xff89bc45, 0xff112233); |
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 | canvas->save(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 188 | canvas->translate(SkIntToScalar(0), SkIntToScalar(5)); |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 189 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 190 | paint.setFilterQuality(kLow_SkFilterQuality); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 191 | // !!! draw through a clip |
| 192 | paint.setColor(SK_ColorLTGRAY); |
| 193 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | SkRect clip = {0, 0, SkIntToScalar(320), SkIntToScalar(120)}; |
| 195 | canvas->clipRect(clip); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 196 | paint.setShader(SkShader::MakeBitmapShader(fTx, |
| 197 | SkShader::kMirror_TileMode, SkShader::kRepeat_TileMode)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 198 | canvas->drawPaint(paint); |
| 199 | canvas->save(); |
| 200 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | // line (exercises xfermode, colorShader, colorFilter, filterShader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 202 | paint.setColor(SK_ColorGREEN); |
| 203 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 204 | paint.setStyle(SkPaint::kStroke_Style); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 205 | paint.setBlendMode(SkBlendMode::kXor); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 206 | paint.setColorFilter(lightingFilter); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 207 | canvas->drawLine(start, stop, paint); // should not be green |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 208 | paint.setBlendMode(SkBlendMode::kSrcOver); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 209 | paint.setColorFilter(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 210 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 211 | // rectangle |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 212 | paint.setStyle(SkPaint::kFill_Style); |
| 213 | canvas->translate(SkIntToScalar(50), 0); |
| 214 | paint.setColor(SK_ColorYELLOW); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 215 | paint.setShader(linear); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 216 | paint.setPathEffect(pathEffectTest()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 217 | canvas->drawRect(rect, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | paint.setPathEffect(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 219 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | // circle w/ emboss & transparent (exercises 3dshader) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 221 | canvas->translate(SkIntToScalar(50), 0); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 222 | paint.setMaskFilter(SkEmbossMaskFilter::Make( |
| 223 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(12)/5), light)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 225 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 226 | // paint.setShader(transparentShader)->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | canvas->drawOval(rect, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 228 | canvas->translate(0, SkIntToScalar(-10)); |
| 229 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 230 | // path |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 231 | canvas->translate(SkIntToScalar(50), 0); |
| 232 | paint.setColor(SK_ColorRED); |
| 233 | paint.setStyle(SkPaint::kStroke_Style); |
| 234 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 235 | paint.setShader(radial); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 236 | paint.setMaskFilter(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 237 | canvas->drawPath(path, paint); |
| 238 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 239 | paint.setShader(nullptr); |
reed | 6f45c17 | 2015-11-04 08:34:07 -0800 | [diff] [blame] | 240 | // bitmap |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 241 | canvas->translate(SkIntToScalar(50), 0); |
| 242 | paint.setStyle(SkPaint::kFill_Style); |
| 243 | canvas->drawBitmap(fBug, left, top, &paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 245 | canvas->translate(-SkIntToScalar(30), SkIntToScalar(30)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 246 | paint.setShader(shaderTest()); // test compose shader |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 247 | canvas->drawRect(rect2, paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | paint.setShader(nullptr); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | canvas->restore(); |
| 251 | // text |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | canvas->translate(0, SkIntToScalar(60)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 253 | canvas->save(); |
| 254 | paint.setColor(SK_ColorGRAY); |
| 255 | canvas->drawPosText(ascii, asciiLength, pos.begin(), paint); |
| 256 | canvas->drawPosText(ascii, asciiLength, pos2.begin(), paint); |
| 257 | |
| 258 | canvas->translate(SkIntToScalar(50), 0); |
| 259 | paint.setColor(SK_ColorCYAN); |
| 260 | canvas->drawText(utf8, sizeof(utf8) - 1, x, y, paint); |
| 261 | |
| 262 | canvas->translate(SkIntToScalar(30), 0); |
| 263 | paint.setColor(SK_ColorMAGENTA); |
| 264 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 265 | matrix.setTranslate(SkIntToScalar(10), SkIntToScalar(10)); |
| 266 | canvas->drawTextOnPath((void*) utf16, sizeof(utf16), path, &matrix, paint); |
| 267 | canvas->translate(0, SkIntToScalar(20)); |
| 268 | canvas->drawTextOnPath((void*) utf16simple, sizeof(utf16simple), path, &matrix, paint); |
| 269 | canvas->restore(); |
| 270 | |
| 271 | canvas->translate(0, SkIntToScalar(60)); |
| 272 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | canvas->restore(); |
| 274 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 275 | |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 276 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | fClickPt.set(x, y); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 278 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 280 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 281 | sk_sp<SkPathEffect> pathEffectTest() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | static const int gXY[] = { 1, 0, 0, -1, 2, -1, 3, 0, 2, 1, 0, 1 }; |
| 283 | SkScalar gPhase = 0; |
| 284 | SkPath path; |
| 285 | path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1])); |
| 286 | for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2) |
| 287 | path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1])); |
| 288 | path.close(); |
| 289 | path.offset(SkIntToScalar(-6), 0); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 290 | auto outer = SkPath1DPathEffect::Make(path, SkIntToScalar(12), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | gPhase, SkPath1DPathEffect::kRotate_Style); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 292 | auto inner = SkDiscretePathEffect::Make(SkIntToScalar(2), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | SkIntToScalar(1)/10); // SkCornerPathEffect(SkIntToScalar(2)); |
Mike Reed | a07741a | 2017-02-25 22:34:32 -0500 | [diff] [blame] | 294 | return SkPathEffect::MakeCompose(outer, inner); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | } |
reed@google.com | 963a8fa | 2012-05-10 13:04:59 +0000 | [diff] [blame] | 296 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 297 | sk_sp<SkShader> shaderTest() { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 298 | SkPoint pts[] = { { 0, 0, }, { SkIntToScalar(100), 0 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 299 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 300 | auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | 2, SkShader::kClamp_TileMode); |
| 302 | pts[1].set(0, SkIntToScalar(100)); |
| 303 | SkColor colors2[] = {SK_ColorBLACK, SkColorSetARGB(0x80, 0, 0, 0)}; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 304 | auto shaderB = SkGradientShader::MakeLinear(pts, colors2, nullptr, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | 2, SkShader::kClamp_TileMode); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 306 | return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB), |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 307 | SkBlendMode::kDstIn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | virtual void startTest() { |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 311 | decode_file("/Users/caryclark/Desktop/bugcirc.gif", &fBug); |
| 312 | decode_file("/Users/caryclark/Desktop/tbcirc.gif", &fTb); |
| 313 | decode_file("/Users/caryclark/Desktop/05psp04.gif", &fTx); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 314 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | private: |
| 317 | SkPoint fClickPt; |
| 318 | SkBitmap fBug, fTb, fTx; |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 319 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | ////////////////////////////////////////////////////////////////////////////// |
| 323 | |
| 324 | static SkView* MyFactory() { return new DemoView; } |
| 325 | static SkViewRegister reg(MyFactory); |