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