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