Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "SkPictureImageGenerator.h" | |
| 17 #include "SkPictureRecorder.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
|
robertphillips
2015/07/20 17:32:43
static ? no namespace ?
f(malita)
2015/07/20 20:13:38
Done.
| |
| 21 void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) { | |
| 22 static const char kSkiaStr[] = "SKIA"; | |
| 23 static const SkScalar kGradientPad = .1f; | |
| 24 static const SkScalar kVerticalSpacing = 0.25f; | |
| 25 static const SkScalar kAccentScale = 1.20; | |
| 26 | |
| 27 SkPaint paint; | |
| 28 paint.setAntiAlias(true); | |
| 29 paint.setSubpixelText(true); | |
| 30 paint.setFakeBoldText(true); | |
| 31 sk_tool_utils::set_portable_typeface_always(&paint); | |
| 32 | |
| 33 SkPath path; | |
| 34 SkRect iBox, skiBox, skiaBox; | |
| 35 paint.getTextPath("SKI", 3, 0, 0, &path); | |
| 36 TightBounds(path, &skiBox); | |
| 37 paint.getTextPath("I", 1, 0, 0, &path); | |
| 38 TightBounds(path, &iBox); | |
| 39 iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop); | |
| 40 | |
| 41 const unsigned textLen = strlen(kSkiaStr); | |
| 42 paint.getTextPath(kSkiaStr, textLen, 0, 0, &path); | |
| 43 TightBounds(path, &skiaBox); | |
| 44 skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1)); | |
| 45 | |
| 46 const SkScalar accentSize = iBox.width() * kAccentScale; | |
| 47 const SkScalar underlineY = iBox.bottom() + (kVerticalSpacing + sqrt(3) / 2) * accentSize; | |
| 48 SkMatrix m; | |
| 49 m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit); | |
| 50 canvas->concat(m); | |
| 51 | |
| 52 canvas->drawCircle(iBox.centerX(), | |
| 53 iBox.y() - (0.5f + kVerticalSpacing) * accentSize, | |
| 54 accentSize / 2, | |
| 55 paint); | |
| 56 | |
| 57 path.reset(); | |
| 58 path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacin g * accentSize); | |
| 59 path.rLineTo(accentSize, 0); | |
| 60 path.lineTo(iBox.centerX(), underlineY); | |
| 61 canvas->drawPath(path, paint); | |
| 62 | |
| 63 SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * acce ntSize * 3, | |
| 64 underlineY, | |
| 65 iBox.centerX(), | |
| 66 underlineY + accentSize / 10); | |
| 67 const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0), | |
| 68 SkPoint::Make(iBox.centerX(), 0) }; | |
| 69 const SkScalar pos1[] = { 0, 0.75f }; | |
| 70 const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK }; | |
| 71 SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1)); | |
| 72 SkAutoTUnref<SkShader> gradient1(SkGradientShader::CreateLinear(pts1, colors 1, pos1, | |
| 73 SK_ARRAY_COU NT(pos1), | |
| 74 SkShader::kC lamp_TileMode)); | |
| 75 paint.setShader(gradient1.get()); | |
| 76 canvas->drawRect(underlineRect, paint); | |
| 77 | |
| 78 const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPa d, 0), | |
| 79 SkPoint::Make(iBox.right() + iBox.width() * kGradie ntPad, 0) }; | |
| 80 const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 } ; | |
| 81 const SkColor colors2[] = { | |
| 82 SK_ColorBLACK, | |
| 83 0xffca5139, | |
| 84 0xffca5139, | |
| 85 0xff8dbd53, | |
| 86 0xff8dbd53, | |
| 87 0xff5460a5, | |
| 88 0xff5460a5, | |
| 89 SK_ColorBLACK | |
| 90 }; | |
| 91 SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2)); | |
| 92 SkAutoTUnref<SkShader> gradient2(SkGradientShader::CreateLinear(pts2, colors 2, pos2, | |
| 93 SK_ARRAY_COU NT(pos2), | |
| 94 SkShader::kC lamp_TileMode)); | |
| 95 paint.setShader(gradient2.get()); | |
| 96 canvas->drawText(kSkiaStr, textLen, 0, 0, paint); | |
| 97 } | |
| 98 | |
| 99 } | |
| 100 | |
|
robertphillips
2015/07/20 17:32:43
// This really interesting GM exercises ...
In par
f(malita)
2015/07/20 20:13:38
Done.
| |
| 101 class PictureGeneratorGM : public skiagm::GM { | |
| 102 public: | |
| 103 PictureGeneratorGM() : fRect(SkRect::MakeWH(200, 100)) { } | |
| 104 | |
| 105 protected: | |
| 106 SkString onShortName() override { | |
| 107 return SkString("pictureimagegenerator"); | |
| 108 } | |
| 109 | |
| 110 SkISize onISize() override { | |
| 111 return SkISize::Make(1160, 860); | |
| 112 } | |
| 113 | |
| 114 void onOnceBeforeDraw() override { | |
| 115 SkPictureRecorder recorder; | |
|
robertphillips
2015/07/20 17:32:43
Can we make 'fRect' just be local to this method ?
f(malita)
2015/07/20 20:13:37
Done.
| |
| 116 SkCanvas* canvas = recorder.beginRecording(fRect); | |
| 117 draw_vector_logo(canvas, fRect); | |
| 118 fPicture.reset(recorder.endRecording()); | |
| 119 } | |
| 120 | |
| 121 void onDraw(SkCanvas* canvas) override { | |
| 122 const struct { | |
| 123 SkISize size; | |
| 124 SkScalar scaleX, scaleY; | |
| 125 SkScalar opacity; | |
| 126 } configs[] = { | |
| 127 { SkISize::Make(200, 100), 1, 1, 1 }, | |
| 128 { SkISize::Make(200, 200), 1, 1, 1 }, | |
| 129 { SkISize::Make(200, 200), 1, 2, 1 }, | |
| 130 { SkISize::Make(400, 200), 2, 2, 1 }, | |
| 131 | |
| 132 { SkISize::Make(200, 100), 1, 1, 0.9f }, | |
| 133 { SkISize::Make(200, 200), 1, 1, 0.75f }, | |
| 134 { SkISize::Make(200, 200), 1, 2, 0.5f }, | |
| 135 { SkISize::Make(400, 200), 2, 2, 0.25f }, | |
| 136 | |
| 137 { SkISize::Make(200, 200), 0.5f, 1, 1 }, | |
| 138 { SkISize::Make(200, 200), 1, 0.5f, 1 }, | |
| 139 { SkISize::Make(200, 200), 0.5f, 0.5f, 1 }, | |
| 140 { SkISize::Make(200, 200), 2, 2, 1 }, | |
| 141 | |
| 142 { SkISize::Make(200, 100), -1, 1, 1 }, | |
| 143 { SkISize::Make(200, 100), 1, -1, 1 }, | |
| 144 { SkISize::Make(200, 100), -1, -1, 1 }, | |
| 145 { SkISize::Make(200, 100), -1, -1, 0.5f }, | |
| 146 }; | |
| 147 | |
| 148 const unsigned kDrawsPerRow = 4; | |
| 149 const unsigned kDrawSize = 250; | |
| 150 | |
| 151 for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) { | |
| 152 SkPaint p; | |
| 153 p.setAlpha(255 * configs[i].opacity); | |
| 154 | |
| 155 SkMatrix m = SkMatrix::MakeScale(configs[i].scaleX, configs[i].scale Y); | |
| 156 if (configs[i].scaleX < 0) { | |
| 157 m.postTranslate(configs[i].size.width(), 0); | |
| 158 } | |
| 159 if (configs[i].scaleY < 0) { | |
| 160 m.postTranslate(0, configs[i].size.height()); | |
| 161 } | |
| 162 SkAutoTDelete<SkImageGenerator> gen( | |
| 163 SkImageGenerator::NewFromPicture(configs[i].size, fPicture.get() , &m, | |
| 164 p.getAlpha() != 255 ? &p : null ptr)); | |
| 165 SkBitmap bm; | |
| 166 SkAssertResult(SkInstallDiscardablePixelRef(gen.detach(), &bm)); | |
| 167 | |
| 168 const SkScalar x = kDrawSize * (i % kDrawsPerRow); | |
| 169 const SkScalar y = kDrawSize * (i / kDrawsPerRow); | |
| 170 | |
| 171 p.setColor(0xfff0f0f0); | |
| 172 p.setAlpha(255); | |
| 173 canvas->drawRect(SkRect::MakeXYWH(x, y, bm.width(), bm.height()), p) ; | |
| 174 canvas->drawBitmap(bm, x, y); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 const SkRect fRect; | |
| 180 SkAutoTUnref<SkPicture> fPicture; | |
| 181 | |
| 182 typedef skiagm::GM INHERITED; | |
| 183 }; | |
| 184 | |
| 185 DEF_GM( return SkNEW(PictureGeneratorGM); ) | |
| OLD | NEW |