Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: samplecode/SampleShaderText.cpp

Issue 1772463002: use Make instead of Create to return a shared shader (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: partial update of skia call-sites Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkGradientShader.h" 10 #include "SkGradientShader.h"
11 #include "SkPath.h" 11 #include "SkPath.h"
12 #include "SkView.h" 12 #include "SkView.h"
13 13
14 static void makebm(SkBitmap* bm, int w, int h) { 14 static void makebm(SkBitmap* bm, int w, int h) {
15 bm->allocN32Pixels(w, h); 15 bm->allocN32Pixels(w, h);
16 bm->eraseColor(SK_ColorTRANSPARENT); 16 bm->eraseColor(SK_ColorTRANSPARENT);
17 17
18 SkCanvas canvas(*bm); 18 SkCanvas canvas(*bm);
19 SkScalar s = SkIntToScalar(w < h ? w : h); 19 SkScalar s = SkIntToScalar(w < h ? w : h);
20 SkPoint pts[] = { { 0, 0 }, { s, s } }; 20 SkPoint pts[] = { { 0, 0 }, { s, s } };
21 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; 21 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
22 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; 22 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 SkPaint paint; 23 SkPaint paint;
24 24
25 paint.setDither(true); 25 paint.setDither(true);
26 paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, 26 paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
27 SK_ARRAY_COUNT(colors), SkShader::kClamp_Til eMode))->unref(); 27 SK_ARRAY_COUNT(colors), SkShader::kClamp_Til eMode));
28 canvas.drawPaint(paint); 28 canvas.drawPaint(paint);
29 } 29 }
30 30
31 static SkShader* MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMode ty, 31 static sk_sp<SkShader> MakeBitmapShader(SkShader::TileMode tx, SkShader::TileMod e ty,
32 int w, int h) { 32 int w, int h) {
33 static SkBitmap bmp; 33 static SkBitmap bmp;
34 if (bmp.isNull()) { 34 if (bmp.isNull()) {
35 makebm(&bmp, w/2, h/4); 35 makebm(&bmp, w/2, h/4);
36 } 36 }
37 return SkShader::CreateBitmapShader(bmp, tx, ty); 37 return SkShader::MakeBitmapShader(bmp, tx, ty);
38 } 38 }
39 39
40 /////////////////////////////////////////////////////////////////////////////// 40 ///////////////////////////////////////////////////////////////////////////////
41 41
42 struct GradData { 42 struct GradData {
43 int fCount; 43 int fCount;
44 const SkColor* fColors; 44 const SkColor* fColors;
45 const SkScalar* fPos; 45 const SkScalar* fPos;
46 }; 46 };
47 47
48 static const SkColor gColors[] = { 48 static const SkColor gColors[] = {
49 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK 49 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
50 }; 50 };
51 51
52 static const GradData gGradData[] = { 52 static const GradData gGradData[] = {
53 { 2, gColors, nullptr }, 53 { 2, gColors, nullptr },
54 { 5, gColors, nullptr }, 54 { 5, gColors, nullptr },
55 }; 55 };
56 56
57 static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader ::TileMode tm) { 57 static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, Sk Shader::TileMode tm) {
58 return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCo unt, tm); 58 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCoun t, tm);
59 } 59 }
60 60
61 static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader ::TileMode tm) { 61 static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, Sk Shader::TileMode tm) {
62 SkPoint center; 62 SkPoint center;
63 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 63 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
64 SkScalarAve(pts[0].fY, pts[1].fY)); 64 SkScalarAve(pts[0].fY, pts[1].fY));
65 return SkGradientShader::CreateRadial(center, center.fX, data.fColors, 65 return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
66 data.fPos, data.fCount, tm); 66 data.fPos, data.fCount, tm);
67 } 67 }
68 68
69 static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader: :TileMode tm) { 69 static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkS hader::TileMode tm) {
70 SkPoint center; 70 SkPoint center;
71 center.set(SkScalarAve(pts[0].fX, pts[1].fX), 71 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
72 SkScalarAve(pts[0].fY, pts[1].fY)); 72 SkScalarAve(pts[0].fY, pts[1].fY));
73 return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, dat a.fPos, data.fCount); 73 return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data. fPos, data.fCount);
74 } 74 }
75 75
76 static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, SkShad er::TileMode tm) { 76 static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
77 SkPoint center0, center1; 77 SkPoint center0, center1;
78 center0.set(SkScalarAve(pts[0].fX, pts[1].fX), 78 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
79 SkScalarAve(pts[0].fY, pts[1].fY)); 79 SkScalarAve(pts[0].fY, pts[1].fY));
80 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), 80 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
81 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); 81 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
82 return SkGradientShader::CreateTwoPointConical( 82 return SkGradientShader::MakeTwoPointConical(
83 center1, (pts[1].fX - pts[0].fX) / 7, 83 center1, (pts[1].fX - pts[0].fX) / 7,
84 center0, (pts[1].fX - pts[0].fX) / 2, 84 center0, (pts[1].fX - pts[0].fX) / 2,
85 data.fColors, data.fPos, data.fCount, tm); 85 data.fColors, data.fPos, data.fCount, tm);
86 } 86 }
87 87
88 typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkSha der::TileMode tm); 88 typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
89 89
90 static const GradMaker gGradMakers[] = { 90 static const GradMaker gGradMakers[] = {
91 MakeLinear, MakeRadial, MakeSweep, Make2Conical 91 MakeLinear, MakeRadial, MakeSweep, Make2Conical
92 }; 92 };
93 93
94 /////////////////////////////////////////////////////////////////////////////// 94 ///////////////////////////////////////////////////////////////////////////////
95 95
96 class ShaderTextView : public SampleView { 96 class ShaderTextView : public SampleView {
97 public: 97 public:
98 ShaderTextView() { 98 ShaderTextView() {
(...skipping 27 matching lines...) Expand all
126 SkShader::TileMode tileModes[] = { 126 SkShader::TileMode tileModes[] = {
127 SkShader::kClamp_TileMode, 127 SkShader::kClamp_TileMode,
128 SkShader::kRepeat_TileMode, 128 SkShader::kRepeat_TileMode,
129 SkShader::kMirror_TileMode 129 SkShader::kMirror_TileMode
130 }; 130 };
131 131
132 static const int gradCount = SK_ARRAY_COUNT(gGradData) * 132 static const int gradCount = SK_ARRAY_COUNT(gGradData) *
133 SK_ARRAY_COUNT(gGradMakers); 133 SK_ARRAY_COUNT(gGradMakers);
134 static const int bmpCount = SK_ARRAY_COUNT(tileModes) * 134 static const int bmpCount = SK_ARRAY_COUNT(tileModes) *
135 SK_ARRAY_COUNT(tileModes); 135 SK_ARRAY_COUNT(tileModes);
136 SkShader* shaders[gradCount + bmpCount]; 136 sk_sp<SkShader> shaders[gradCount + bmpCount];
137 137
138 int shdIdx = 0; 138 int shdIdx = 0;
139 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) { 139 for (size_t d = 0; d < SK_ARRAY_COUNT(gGradData); ++d) {
140 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) { 140 for (size_t m = 0; m < SK_ARRAY_COUNT(gGradMakers); ++m) {
141 shaders[shdIdx++] = gGradMakers[m](pts, 141 shaders[shdIdx++] = gGradMakers[m](pts,
142 gGradData[d], 142 gGradData[d],
143 SkShader::kClamp_TileMode); 143 SkShader::kClamp_TileMode);
144 } 144 }
145 } 145 }
146 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) { 146 for (size_t tx = 0; tx < SK_ARRAY_COUNT(tileModes); ++tx) {
(...skipping 21 matching lines...) Expand all
168 168
169 static const int testsPerCol = 8; 169 static const int testsPerCol = 8;
170 static const int rowHeight = 60; 170 static const int rowHeight = 60;
171 static const int colWidth = 300; 171 static const int colWidth = 300;
172 canvas->save(); 172 canvas->save();
173 for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) { 173 for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); s++) {
174 canvas->save(); 174 canvas->save();
175 size_t i = 2*s; 175 size_t i = 2*s;
176 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), 176 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
177 SkIntToScalar((i % testsPerCol) * rowHeight)); 177 SkIntToScalar((i % testsPerCol) * rowHeight));
178 paint.setShader(shaders[s])->unref(); 178 paint.setShader(shaders[s]);
179 canvas->drawText(text, textLen, 0, textBase, paint); 179 canvas->drawText(text, textLen, 0, textBase, paint);
180 canvas->restore(); 180 canvas->restore();
181 canvas->save(); 181 canvas->save();
182 ++i; 182 ++i;
183 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth), 183 canvas->translate(SkIntToScalar((i / testsPerCol) * colWidth),
184 SkIntToScalar((i % testsPerCol) * rowHeight)); 184 SkIntToScalar((i % testsPerCol) * rowHeight));
185 canvas->drawTextOnPath(text, textLen, path, nullptr, paint); 185 canvas->drawTextOnPath(text, textLen, path, nullptr, paint);
186 canvas->restore(); 186 canvas->restore();
187 } 187 }
188 canvas->restore(); 188 canvas->restore();
189 } 189 }
190 190
191 private: 191 private:
192 typedef SampleView INHERITED; 192 typedef SampleView INHERITED;
193 }; 193 };
194 194
195 /////////////////////////////////////////////////////////////////////////////// 195 ///////////////////////////////////////////////////////////////////////////////
196 196
197 static SkView* MyFactory() { return new ShaderTextView; } 197 static SkView* MyFactory() { return new ShaderTextView; }
198 static SkViewRegister reg(MyFactory); 198 static SkViewRegister reg(MyFactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698