fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 12 | #include "SkFontPriv.h" |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkImageGenerator.h" |
| 15 | #include "SkPaint.h" |
Mike Reed | c090c64 | 2017-05-16 10:39:06 -0400 | [diff] [blame] | 16 | #include "SkPath.h" |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 17 | #include "SkPathOps.h" |
| 18 | #include "SkPicture.h" |
| 19 | #include "SkPictureRecorder.h" |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 20 | #include "SkTextUtils.h" |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 21 | |
| 22 | static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 23 | constexpr char kSkiaStr[] = "SKIA"; |
| 24 | constexpr SkScalar kGradientPad = .1f; |
| 25 | constexpr SkScalar kVerticalSpacing = 0.25f; |
| 26 | constexpr SkScalar kAccentScale = 1.20f; |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 27 | |
| 28 | SkPaint paint; |
| 29 | paint.setAntiAlias(true); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 30 | |
| 31 | SkFont font(sk_tool_utils::create_portable_typeface()); |
| 32 | font.setSubpixel(true); |
| 33 | font.setEmbolden(true); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 34 | |
| 35 | SkPath path; |
| 36 | SkRect iBox, skiBox, skiaBox; |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 37 | SkTextUtils::GetPath("SKI", 3, kUTF8_SkTextEncoding, 0, 0, font, &path); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 38 | TightBounds(path, &skiBox); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 39 | SkTextUtils::GetPath("I", 1, kUTF8_SkTextEncoding, 0, 0, font, &path); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 40 | TightBounds(path, &iBox); |
| 41 | iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop); |
| 42 | |
| 43 | const size_t textLen = strlen(kSkiaStr); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 44 | SkTextUtils::GetPath(kSkiaStr, textLen, kUTF8_SkTextEncoding, 0, 0, font, &path); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 45 | TightBounds(path, &skiaBox); |
| 46 | skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1)); |
| 47 | |
| 48 | const SkScalar accentSize = iBox.width() * kAccentScale; |
| 49 | const SkScalar underlineY = iBox.bottom() + |
| 50 | (kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize; |
| 51 | SkMatrix m; |
| 52 | m.setRectToRect(skiaBox, viewBox, SkMatrix::kFill_ScaleToFit); |
| 53 | SkAutoCanvasRestore acr(canvas, true); |
| 54 | canvas->concat(m); |
| 55 | |
| 56 | canvas->drawCircle(iBox.centerX(), |
| 57 | iBox.y() - (0.5f + kVerticalSpacing) * accentSize, |
| 58 | accentSize / 2, |
| 59 | paint); |
| 60 | |
| 61 | path.reset(); |
| 62 | path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize); |
| 63 | path.rLineTo(accentSize, 0); |
| 64 | path.lineTo(iBox.centerX(), underlineY); |
| 65 | canvas->drawPath(path, paint); |
| 66 | |
| 67 | SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3, |
| 68 | underlineY, |
| 69 | iBox.centerX(), |
| 70 | underlineY + accentSize / 10); |
| 71 | const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0), |
| 72 | SkPoint::Make(iBox.centerX(), 0) }; |
| 73 | const SkScalar pos1[] = { 0, 0.75f }; |
| 74 | const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK }; |
| 75 | SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1), |
| 77 | SkShader::kClamp_TileMode)); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 78 | canvas->drawRect(underlineRect, paint); |
| 79 | |
| 80 | const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0), |
| 81 | SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) }; |
| 82 | const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 }; |
| 83 | const SkColor colors2[] = { |
| 84 | SK_ColorBLACK, |
| 85 | 0xffca5139, |
| 86 | 0xffca5139, |
| 87 | 0xff8dbd53, |
| 88 | 0xff8dbd53, |
| 89 | 0xff5460a5, |
| 90 | 0xff5460a5, |
| 91 | SK_ColorBLACK |
| 92 | }; |
| 93 | SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 94 | paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2), |
| 95 | SkShader::kClamp_TileMode)); |
Mike Reed | f78b7ea | 2018-12-25 22:06:17 -0500 | [diff] [blame] | 96 | canvas->drawSimpleText(kSkiaStr, textLen, kUTF8_SkTextEncoding, 0, 0, font, paint); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // This GM exercises SkPictureImageGenerator features |
| 100 | // (in particular its matrix vs. bounds semantics). |
| 101 | class PictureGeneratorGM : public skiagm::GM { |
| 102 | protected: |
| 103 | SkString onShortName() override { |
| 104 | return SkString("pictureimagegenerator"); |
| 105 | } |
| 106 | |
| 107 | SkISize onISize() override { |
| 108 | return SkISize::Make(1160, 860); |
| 109 | } |
| 110 | |
| 111 | void onOnceBeforeDraw() override { |
| 112 | const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight); |
| 113 | SkPictureRecorder recorder; |
| 114 | SkCanvas* canvas = recorder.beginRecording(rect); |
| 115 | draw_vector_logo(canvas, rect); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 116 | fPicture = recorder.finishRecordingAsPicture(); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void onDraw(SkCanvas* canvas) override { |
| 120 | const struct { |
| 121 | SkISize size; |
| 122 | SkScalar scaleX, scaleY; |
| 123 | SkScalar opacity; |
| 124 | } configs[] = { |
| 125 | { SkISize::Make(200, 100), 1, 1, 1 }, |
| 126 | { SkISize::Make(200, 200), 1, 1, 1 }, |
| 127 | { SkISize::Make(200, 200), 1, 2, 1 }, |
| 128 | { SkISize::Make(400, 200), 2, 2, 1 }, |
| 129 | |
| 130 | { SkISize::Make(200, 100), 1, 1, 0.9f }, |
| 131 | { SkISize::Make(200, 200), 1, 1, 0.75f }, |
| 132 | { SkISize::Make(200, 200), 1, 2, 0.5f }, |
| 133 | { SkISize::Make(400, 200), 2, 2, 0.25f }, |
| 134 | |
| 135 | { SkISize::Make(200, 200), 0.5f, 1, 1 }, |
| 136 | { SkISize::Make(200, 200), 1, 0.5f, 1 }, |
| 137 | { SkISize::Make(200, 200), 0.5f, 0.5f, 1 }, |
| 138 | { SkISize::Make(200, 200), 2, 2, 1 }, |
| 139 | |
| 140 | { SkISize::Make(200, 100), -1, 1, 1 }, |
| 141 | { SkISize::Make(200, 100), 1, -1, 1 }, |
| 142 | { SkISize::Make(200, 100), -1, -1, 1 }, |
| 143 | { SkISize::Make(200, 100), -1, -1, 0.5f }, |
| 144 | }; |
| 145 | |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 146 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 147 | const unsigned kDrawsPerRow = 4; |
| 148 | const SkScalar kDrawSize = 250; |
| 149 | |
| 150 | for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) { |
| 151 | SkPaint p; |
| 152 | p.setAlpha(SkScalarRoundToInt(255 * configs[i].opacity)); |
| 153 | |
| 154 | SkMatrix m = SkMatrix::MakeScale(configs[i].scaleX, configs[i].scaleY); |
| 155 | if (configs[i].scaleX < 0) { |
| 156 | m.postTranslate(SkIntToScalar(configs[i].size.width()), 0); |
| 157 | } |
| 158 | if (configs[i].scaleY < 0) { |
| 159 | m.postTranslate(0, SkIntToScalar(configs[i].size.height())); |
| 160 | } |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 161 | std::unique_ptr<SkImageGenerator> gen = |
| 162 | SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m, |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 163 | p.getAlpha() != 255 ? &p : nullptr, |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 164 | SkImage::BitDepth::kU8, srgbColorSpace); |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 165 | |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 166 | SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace()); |
Brian Osman | 138ea97 | 2016-12-16 11:55:18 -0500 | [diff] [blame] | 167 | |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 168 | SkBitmap bm; |
Mike Reed | 4e3abc1 | 2017-04-07 12:04:23 -0400 | [diff] [blame] | 169 | bm.allocPixels(bmInfo); |
| 170 | SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes())); |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 171 | |
| 172 | const SkScalar x = kDrawSize * (i % kDrawsPerRow); |
| 173 | const SkScalar y = kDrawSize * (i / kDrawsPerRow); |
| 174 | |
| 175 | p.setColor(0xfff0f0f0); |
| 176 | p.setAlpha(255); |
| 177 | canvas->drawRect(SkRect::MakeXYWH(x, y, |
| 178 | SkIntToScalar(bm.width()), |
| 179 | SkIntToScalar(bm.height())), p); |
| 180 | canvas->drawBitmap(bm, x, y); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 185 | sk_sp<SkPicture> fPicture; |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 186 | |
| 187 | const SkScalar kPictureWidth = 200; |
| 188 | const SkScalar kPictureHeight = 100; |
| 189 | |
| 190 | typedef skiagm::GM INHERITED; |
| 191 | }; |
| 192 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 193 | DEF_GM(return new PictureGeneratorGM;) |