2015-08-04 20:53:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImage.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImageGenerator.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/pathops/SkPathOps.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2015-08-04 20:53:14 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
|
|
|
2015-08-04 20:53:14 +00:00
|
|
|
static void draw_vector_logo(SkCanvas* canvas, const SkRect& viewBox) {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr char kSkiaStr[] = "SKIA";
|
|
|
|
constexpr SkScalar kGradientPad = .1f;
|
|
|
|
constexpr SkScalar kVerticalSpacing = 0.25f;
|
|
|
|
constexpr SkScalar kAccentScale = 1.20f;
|
2015-08-04 20:53:14 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2018-12-26 03:06:17 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2018-12-26 03:06:17 +00:00
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setEmbolden(true);
|
2015-08-04 20:53:14 +00:00
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
SkRect iBox, skiBox, skiaBox;
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextUtils::GetPath("SKI", 3, SkTextEncoding::kUTF8, 0, 0, font, &path);
|
2015-08-04 20:53:14 +00:00
|
|
|
TightBounds(path, &skiBox);
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextUtils::GetPath("I", 1, SkTextEncoding::kUTF8, 0, 0, font, &path);
|
2015-08-04 20:53:14 +00:00
|
|
|
TightBounds(path, &iBox);
|
|
|
|
iBox.offsetTo(skiBox.fRight - iBox.width(), iBox.fTop);
|
|
|
|
|
|
|
|
const size_t textLen = strlen(kSkiaStr);
|
2019-05-07 19:38:46 +00:00
|
|
|
SkTextUtils::GetPath(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, &path);
|
2015-08-04 20:53:14 +00:00
|
|
|
TightBounds(path, &skiaBox);
|
|
|
|
skiaBox.outset(0, 2 * iBox.width() * (kVerticalSpacing + 1));
|
|
|
|
|
|
|
|
const SkScalar accentSize = iBox.width() * kAccentScale;
|
|
|
|
const SkScalar underlineY = iBox.bottom() +
|
|
|
|
(kVerticalSpacing + SkScalarSqrt(3) / 2) * accentSize;
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
2021-01-15 17:26:22 +00:00
|
|
|
canvas->concat(SkMatrix::RectToRect(skiaBox, viewBox));
|
2015-08-04 20:53:14 +00:00
|
|
|
|
|
|
|
canvas->drawCircle(iBox.centerX(),
|
|
|
|
iBox.y() - (0.5f + kVerticalSpacing) * accentSize,
|
|
|
|
accentSize / 2,
|
|
|
|
paint);
|
|
|
|
|
|
|
|
path.reset();
|
|
|
|
path.moveTo(iBox.centerX() - accentSize / 2, iBox.bottom() + kVerticalSpacing * accentSize);
|
|
|
|
path.rLineTo(accentSize, 0);
|
|
|
|
path.lineTo(iBox.centerX(), underlineY);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
|
|
|
SkRect underlineRect = SkRect::MakeLTRB(iBox.centerX() - iBox.width() * accentSize * 3,
|
|
|
|
underlineY,
|
|
|
|
iBox.centerX(),
|
|
|
|
underlineY + accentSize / 10);
|
|
|
|
const SkPoint pts1[] = { SkPoint::Make(underlineRect.x(), 0),
|
|
|
|
SkPoint::Make(iBox.centerX(), 0) };
|
|
|
|
const SkScalar pos1[] = { 0, 0.75f };
|
|
|
|
const SkColor colors1[] = { SK_ColorTRANSPARENT, SK_ColorBLACK };
|
|
|
|
SkASSERT(SK_ARRAY_COUNT(pos1) == SK_ARRAY_COUNT(colors1));
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts1, colors1, pos1, SK_ARRAY_COUNT(pos1),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2015-08-04 20:53:14 +00:00
|
|
|
canvas->drawRect(underlineRect, paint);
|
|
|
|
|
|
|
|
const SkPoint pts2[] = { SkPoint::Make(iBox.x() - iBox.width() * kGradientPad, 0),
|
|
|
|
SkPoint::Make(iBox.right() + iBox.width() * kGradientPad, 0) };
|
|
|
|
const SkScalar pos2[] = { 0, .01f, 1.0f/3, 1.0f/3, 2.0f/3, 2.0f/3, .99f, 1 };
|
|
|
|
const SkColor colors2[] = {
|
|
|
|
SK_ColorBLACK,
|
|
|
|
0xffca5139,
|
|
|
|
0xffca5139,
|
|
|
|
0xff8dbd53,
|
|
|
|
0xff8dbd53,
|
|
|
|
0xff5460a5,
|
|
|
|
0xff5460a5,
|
|
|
|
SK_ColorBLACK
|
|
|
|
};
|
|
|
|
SkASSERT(SK_ARRAY_COUNT(pos2) == SK_ARRAY_COUNT(colors2));
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts2, colors2, pos2, SK_ARRAY_COUNT(pos2),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(kSkiaStr, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2015-08-04 20:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This GM exercises SkPictureImageGenerator features
|
|
|
|
// (in particular its matrix vs. bounds semantics).
|
|
|
|
class PictureGeneratorGM : public skiagm::GM {
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("pictureimagegenerator");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(1160, 860);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
const SkRect rect = SkRect::MakeWH(kPictureWidth, kPictureHeight);
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkCanvas* canvas = recorder.beginRecording(rect);
|
|
|
|
draw_vector_logo(canvas, rect);
|
2016-03-18 14:25:55 +00:00
|
|
|
fPicture = recorder.finishRecordingAsPicture();
|
2015-08-04 20:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
const struct {
|
|
|
|
SkISize size;
|
|
|
|
SkScalar scaleX, scaleY;
|
|
|
|
SkScalar opacity;
|
|
|
|
} configs[] = {
|
|
|
|
{ SkISize::Make(200, 100), 1, 1, 1 },
|
|
|
|
{ SkISize::Make(200, 200), 1, 1, 1 },
|
|
|
|
{ SkISize::Make(200, 200), 1, 2, 1 },
|
|
|
|
{ SkISize::Make(400, 200), 2, 2, 1 },
|
|
|
|
|
|
|
|
{ SkISize::Make(200, 100), 1, 1, 0.9f },
|
|
|
|
{ SkISize::Make(200, 200), 1, 1, 0.75f },
|
|
|
|
{ SkISize::Make(200, 200), 1, 2, 0.5f },
|
|
|
|
{ SkISize::Make(400, 200), 2, 2, 0.25f },
|
|
|
|
|
|
|
|
{ SkISize::Make(200, 200), 0.5f, 1, 1 },
|
|
|
|
{ SkISize::Make(200, 200), 1, 0.5f, 1 },
|
|
|
|
{ SkISize::Make(200, 200), 0.5f, 0.5f, 1 },
|
|
|
|
{ SkISize::Make(200, 200), 2, 2, 1 },
|
|
|
|
|
|
|
|
{ SkISize::Make(200, 100), -1, 1, 1 },
|
|
|
|
{ SkISize::Make(200, 100), 1, -1, 1 },
|
|
|
|
{ SkISize::Make(200, 100), -1, -1, 1 },
|
|
|
|
{ SkISize::Make(200, 100), -1, -1, 0.5f },
|
|
|
|
};
|
|
|
|
|
2017-02-07 18:56:11 +00:00
|
|
|
auto srgbColorSpace = SkColorSpace::MakeSRGB();
|
2015-08-04 20:53:14 +00:00
|
|
|
const unsigned kDrawsPerRow = 4;
|
|
|
|
const SkScalar kDrawSize = 250;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
|
|
|
|
SkPaint p;
|
2019-02-15 21:13:57 +00:00
|
|
|
p.setAlphaf(configs[i].opacity);
|
2015-08-04 20:53:14 +00:00
|
|
|
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix m = SkMatrix::Scale(configs[i].scaleX, configs[i].scaleY);
|
2015-08-04 20:53:14 +00:00
|
|
|
if (configs[i].scaleX < 0) {
|
|
|
|
m.postTranslate(SkIntToScalar(configs[i].size.width()), 0);
|
|
|
|
}
|
|
|
|
if (configs[i].scaleY < 0) {
|
|
|
|
m.postTranslate(0, SkIntToScalar(configs[i].size.height()));
|
|
|
|
}
|
2017-02-15 20:14:16 +00:00
|
|
|
std::unique_ptr<SkImageGenerator> gen =
|
|
|
|
SkImageGenerator::MakeFromPicture(configs[i].size, fPicture, &m,
|
2016-12-16 16:55:18 +00:00
|
|
|
p.getAlpha() != 255 ? &p : nullptr,
|
2017-02-15 20:14:16 +00:00
|
|
|
SkImage::BitDepth::kU8, srgbColorSpace);
|
2016-12-16 16:55:18 +00:00
|
|
|
|
2017-01-12 15:13:40 +00:00
|
|
|
SkImageInfo bmInfo = gen->getInfo().makeColorSpace(canvas->imageInfo().refColorSpace());
|
2016-12-16 16:55:18 +00:00
|
|
|
|
2015-08-04 20:53:14 +00:00
|
|
|
SkBitmap bm;
|
2017-04-07 16:04:23 +00:00
|
|
|
bm.allocPixels(bmInfo);
|
|
|
|
SkAssertResult(gen->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()));
|
2015-08-04 20:53:14 +00:00
|
|
|
|
|
|
|
const SkScalar x = kDrawSize * (i % kDrawsPerRow);
|
|
|
|
const SkScalar y = kDrawSize * (i / kDrawsPerRow);
|
|
|
|
|
|
|
|
p.setColor(0xfff0f0f0);
|
2019-02-15 21:13:57 +00:00
|
|
|
p.setAlphaf(1.0f);
|
2015-08-04 20:53:14 +00:00
|
|
|
canvas->drawRect(SkRect::MakeXYWH(x, y,
|
|
|
|
SkIntToScalar(bm.width()),
|
|
|
|
SkIntToScalar(bm.height())), p);
|
|
|
|
canvas->drawBitmap(bm, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-18 14:25:55 +00:00
|
|
|
sk_sp<SkPicture> fPicture;
|
2015-08-04 20:53:14 +00:00
|
|
|
|
|
|
|
const SkScalar kPictureWidth = 200;
|
|
|
|
const SkScalar kPictureHeight = 100;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2015-08-04 20:53:14 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new PictureGeneratorGM;)
|