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

Side by Side Diff: gm/pictureimagegenerator.cpp

Issue 1240093004: SkPictureImageGenerator (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: build fix Created 5 years, 4 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
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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 */
7
8 #include "gm.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkImageGenerator.h"
13 #include "SkPaint.h"
14 #include "SkPathOps.h"
15 #include "SkPicture.h"
16 #include "SkPictureRecorder.h"
17
18 static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
19 static const char kSkiaStr[] = "SKIA";
20 static const SkScalar kGradientPad = .1f;
21 static const SkScalar kVerticalSpacing = 0.25f;
22 static const SkScalar kAccentScale = 1.20f;
23
24 SkPaint paint;
25 paint.setAntiAlias(true);
26 paint.setSubpixelText(true);
27 paint.setFakeBoldText(true);
28 sk_tool_utils::set_portable_typeface(&paint);
29
30 SkPath path;
31 SkRect iBox, skiBox, skiaBox;
32 paint.getTextPath("SKI", 3, 0, 0, &path);
33 TightBounds(path, &skiBox);
34 paint.getTextPath("I", 1, 0, 0, &path);
35 TightBounds(path, &iBox);
36 iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
37
38 const size_t textLen = strlen(kSkiaStr);
39 paint.getTextPath(kSkiaStr, textLen, 0, 0, &path);
40 TightBounds(path, &skiaBox);
41 skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
42
43 const SkScalar accentSize = iBox.width() * kAccentScale;
44 const SkScalar underlineY = iBox.bottom() +
45 (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
46 SkMatrix m;
47 m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit);
48 SkAutoCanvasRestore acr(canvas, true);
49 canvas->concat(m);
50
51 canvas->drawCircle(iBox.centerX(),
52 iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
53 accentSize / 2,
54 paint);
55
56 path.reset();
57 path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacin g * accentSize);
58 path.rLineTo(accentSize, 0);
59 path.lineTo(iBox.centerX(), underlineY);
60 canvas->drawPath(path, paint);
61
62 SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * acce ntSize * 3,
63 underlineY,
64 iBox.centerX(),
65 underlineY + accentSize / 10);
66 const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
67 SkPoint::Make(iBox.centerX(), 0) };
68 const SkScalar pos1[] = { 0, 0.75f };
69 const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
70 SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1));
71 SkAutoTUnref<SkShader> gradient1(SkGradientShader::CreateLinear(pts1, colors 1, pos1,
72 SK_ARRAY_COU NT(pos1),
73 SkShader::kC lamp_TileMode));
74 paint.setShader(gradient1.get());
75 canvas->drawRect(underlineRect, paint);
76
77 const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPa d, 0),
78 SkPoint::Make(iBox.right() + iBox.width() * kGradie ntPad, 0) };
79 const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 } ;
80 const SkColor colors2[] = {
81 SK_ColorBLACK,
82 0xffca5139,
83 0xffca5139,
84 0xff8dbd53,
85 0xff8dbd53,
86 0xff5460a5,
87 0xff5460a5,
88 SK_ColorBLACK
89 };
90 SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2));
91 SkAutoTUnref<SkShader> gradient2(SkGradientShader::CreateLinear(pts2, colors 2, pos2,
92 SK_ARRAY_COU NT(pos2),
93 SkShader::kC lamp_TileMode));
94 paint.setShader(gradient2.get());
95 canvas->drawText(kSkiaStr, textLen, 0, 0, paint);
96 }
97
98 // This GM exercises SkPictureImageGenerator features
99 // (in particular its matrix vs. bounds semantics).
100 class PictureGeneratorGM : public skiagm::GM {
101 protected:
102 SkString onShortName() override {
103 return SkString("pictureimagegenerator");
104 }
105
106 SkISize onISize() override {
107 return SkISize::Make(1160, 860);
108 }
109
110 void onOnceBeforeDraw() override {
111 const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
112 SkPictureRecorder recorder;
113 SkCanvas* canvas = recorder.beginRecording(rect);
114 draw_vector_logo(canvas, rect);
115 fPicture.reset(recorder.endRecording());
116 }
117
118 void onDraw(SkCanvas* canvas) override {
119 const struct {
120 SkISize size;
121 SkScalar scaleX, scaleY;
122 SkScalar opacity;
123 } configs[] = {
124 { SkISize::Make(200, 100), 1, 1, 1 },
125 { SkISize::Make(200, 200), 1, 1, 1 },
126 { SkISize::Make(200, 200), 1, 2, 1 },
127 { SkISize::Make(400, 200), 2, 2, 1 },
128
129 { SkISize::Make(200, 100), 1, 1, 0.9f },
130 { SkISize::Make(200, 200), 1, 1, 0.75f },
131 { SkISize::Make(200, 200), 1, 2, 0.5f },
132 { SkISize::Make(400, 200), 2, 2, 0.25f },
133
134 { SkISize::Make(200, 200), 0.5f, 1, 1 },
135 { SkISize::Make(200, 200), 1, 0.5f, 1 },
136 { SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
137 { SkISize::Make(200, 200), 2, 2, 1 },
138
139 { SkISize::Make(200, 100), -1, 1, 1 },
140 { SkISize::Make(200, 100), 1, -1, 1 },
141 { SkISize::Make(200, 100), -1, -1, 1 },
142 { SkISize::Make(200, 100), -1, -1, 0.5f },
143 };
144
145 const unsigned kDrawsPerRow = 4;
146 const SkScalar kDrawSize = 250;
147
148 for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
149 SkPaint p;
150 p.setAlpha(SkScalarRoundToInt(255 * configs[i].opacity));
151
152 SkMatrix m = SkMatrix::MakeScale(configs[i].scaleX, configs[i].scale Y);
153 if (configs[i].scaleX < 0) {
154 m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
155 }
156 if (configs[i].scaleY < 0) {
157 m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
158 }
159 SkAutoTDelete<SkImageGenerator> gen(
160 SkImageGenerator::NewFromPicture(configs[i].size, fPicture.get() , &m,
161 p.getAlpha() != 255 ? &p : null ptr));
162 SkBitmap bm;
163 SkAssertResult(SkInstallDiscardablePixelRef(gen.detach(), &bm));
164
165 const SkScalar x = kDrawSize * (i % kDrawsPerRow);
166 const SkScalar y = kDrawSize * (i / kDrawsPerRow);
167
168 p.setColor(0xfff0f0f0);
169 p.setAlpha(255);
170 canvas->drawRect(SkRect::MakeXYWH(x, y,
171 SkIntToScalar(bm.width()),
172 SkIntToScalar(bm.height())), p);
173 canvas->drawBitmap(bm, x, y);
174 }
175 }
176
177 private:
178 SkAutoTUnref<SkPicture> fPicture;
179
180 const SkScalar kPictureWidth = 200;
181 const SkScalar kPictureHeight = 100;
182
183 typedef skiagm::GM INHERITED;
184 };
185
186 DEF_GM( return SkNEW(PictureGeneratorGM); )
OLDNEW
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698