2017-12-19 16:09:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 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/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/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSurfaceProps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2017-12-19 16:09:32 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2017-12-19 16:09:32 +00:00
|
|
|
/**
|
|
|
|
* This GM tests reusing the same text blobs with distance fields rendering using various
|
|
|
|
* combinations of perspective and non-perspetive matrices, scissor clips, and different x,y params
|
|
|
|
* passed to the draw.
|
|
|
|
*/
|
|
|
|
class DFTextBlobPerspGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
DFTextBlobPerspGM() { this->setBGColor(0xFFFFFFFF); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkString("dftext_blob_persp");
|
2017-12-19 16:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(900, 350); }
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
2018-12-22 22:37:30 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(32);
|
|
|
|
font.setEdging(i == 0 ? SkFont::Edging::kAlias :
|
|
|
|
(i == 1 ? SkFont::Edging::kAntiAlias :
|
|
|
|
SkFont::Edging::kSubpixelAntiAlias));
|
|
|
|
font.setSubpixel(true);
|
2017-12-19 16:09:32 +00:00
|
|
|
SkTextBlobBuilder builder;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, "SkiaText", font, 0, 0);
|
2017-12-19 16:09:32 +00:00
|
|
|
fBlobs.emplace_back(builder.make());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:37:30 +00:00
|
|
|
void onDraw(SkCanvas* inputCanvas) override {
|
2020-07-07 14:20:27 +00:00
|
|
|
// set up offscreen rendering with distance field text
|
|
|
|
auto ctx = inputCanvas->recordingContext();
|
2017-12-19 16:09:32 +00:00
|
|
|
SkISize size = this->onISize();
|
|
|
|
if (!inputCanvas->getBaseLayerSize().isEmpty()) {
|
|
|
|
size = inputCanvas->getBaseLayerSize();
|
|
|
|
}
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
|
|
|
|
inputCanvas->imageInfo().refColorSpace());
|
2020-09-24 18:49:00 +00:00
|
|
|
SkSurfaceProps inputProps;
|
|
|
|
inputCanvas->getProps(&inputProps);
|
|
|
|
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(),
|
|
|
|
inputProps.pixelGeometry());
|
2017-12-19 16:09:32 +00:00
|
|
|
auto surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
|
|
|
|
SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
|
|
|
|
// init our new canvas with the old canvas's matrix
|
2020-12-03 16:21:31 +00:00
|
|
|
canvas->setMatrix(inputCanvas->getLocalToDeviceAs3x3());
|
2017-12-19 16:09:32 +00:00
|
|
|
SkScalar x = 0, y = 0;
|
|
|
|
SkScalar maxH = 0;
|
|
|
|
for (auto twm : {TranslateWithMatrix::kNo, TranslateWithMatrix::kYes}) {
|
|
|
|
for (auto pm : {PerspMode::kNone, PerspMode::kX, PerspMode::kY, PerspMode::kXY}) {
|
|
|
|
for (auto& blob : fBlobs) {
|
|
|
|
for (bool clip : {false, true}) {
|
|
|
|
canvas->save();
|
|
|
|
SkScalar w = blob->bounds().width();
|
|
|
|
SkScalar h = blob->bounds().height();
|
|
|
|
if (clip) {
|
|
|
|
auto rect =
|
|
|
|
SkRect::MakeXYWH(x + 5, y + 5, w * 3.f / 4.f, h * 3.f / 4.f);
|
|
|
|
canvas->clipRect(rect, false);
|
|
|
|
}
|
|
|
|
this->drawBlob(canvas, blob.get(), SK_ColorBLACK, x, y + h, pm, twm);
|
|
|
|
x += w + 20.f;
|
2020-02-07 15:36:46 +00:00
|
|
|
maxH = std::max(h, maxH);
|
2017-12-19 16:09:32 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x = 0;
|
|
|
|
y += maxH + 20.f;
|
|
|
|
maxH = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// render offscreen buffer
|
|
|
|
if (surface) {
|
|
|
|
SkAutoCanvasRestore acr(inputCanvas, true);
|
|
|
|
// since we prepended this matrix already, we blit using identity
|
|
|
|
inputCanvas->resetMatrix();
|
2021-01-23 17:23:23 +00:00
|
|
|
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
|
2017-12-19 16:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class PerspMode { kNone, kX, kY, kXY };
|
|
|
|
|
|
|
|
enum class TranslateWithMatrix : bool { kNo, kYes };
|
|
|
|
|
|
|
|
void drawBlob(SkCanvas* canvas, SkTextBlob* blob, SkColor color, SkScalar x, SkScalar y,
|
|
|
|
PerspMode perspMode, TranslateWithMatrix translateWithMatrix) {
|
|
|
|
canvas->save();
|
|
|
|
SkMatrix persp = SkMatrix::I();
|
|
|
|
switch (perspMode) {
|
|
|
|
case PerspMode::kNone:
|
|
|
|
break;
|
|
|
|
case PerspMode::kX:
|
|
|
|
persp.setPerspX(0.005f);
|
|
|
|
break;
|
|
|
|
case PerspMode::kY:
|
|
|
|
persp.setPerspY(00.005f);
|
|
|
|
break;
|
|
|
|
case PerspMode::kXY:
|
|
|
|
persp.setPerspX(-0.001f);
|
|
|
|
persp.setPerspY(-0.0015f);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-21 16:11:27 +00:00
|
|
|
persp = SkMatrix::Concat(persp, SkMatrix::Translate(-x, -y));
|
|
|
|
persp = SkMatrix::Concat(SkMatrix::Translate(x, y), persp);
|
2017-12-19 16:09:32 +00:00
|
|
|
canvas->concat(persp);
|
|
|
|
if (TranslateWithMatrix::kYes == translateWithMatrix) {
|
|
|
|
canvas->translate(x, y);
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas->drawTextBlob(blob, x, y, paint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:41:23 +00:00
|
|
|
SkTArray<sk_sp<SkTextBlob>> fBlobs;
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2017-12-19 16:09:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new DFTextBlobPerspGM;)
|