2015-08-01 14:33:41 +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/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/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImageInfo.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"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/fonts/RandomScalerContext.h"
|
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <utility>
|
|
|
|
|
2015-08-01 14:33:41 +00:00
|
|
|
namespace skiagm {
|
2021-06-03 14:14:16 +00:00
|
|
|
class TextBlobRandomFont : public GM {
|
2015-08-01 14:33:41 +00:00
|
|
|
public:
|
|
|
|
// This gm tests that textblobs can be translated and scaled with a font that returns random
|
|
|
|
// but deterministic masks
|
|
|
|
TextBlobRandomFont() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
const char* text = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
|
|
|
|
SkPaint paint;
|
2018-04-30 19:39:15 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-12-22 22:37:30 +00:00
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
|
|
|
|
// make textbloben
|
|
|
|
SkFont font;
|
|
|
|
font.setSize(32);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// Setup our random scaler context
|
2019-03-20 16:12:10 +00:00
|
|
|
auto typeface = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
|
2018-04-30 19:39:15 +00:00
|
|
|
if (!typeface) {
|
|
|
|
typeface = SkTypeface::MakeDefault();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false));
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
SkScalar y = 0;
|
2015-08-01 14:33:41 +00:00
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// A8
|
2015-08-01 17:33:40 +00:00
|
|
|
const char* bigtext1 = "The quick brown fox";
|
|
|
|
const char* bigtext2 = "jumps over the lazy dog.";
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setSize(160);
|
|
|
|
font.setSubpixel(false);
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(bigtext1, strlen(bigtext1), SkTextEncoding::kUTF8, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, bigtext1, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(bigtext2, strlen(bigtext2), SkTextEncoding::kUTF8, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, bigtext2, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
|
|
|
|
// color emoji
|
2019-03-20 16:12:10 +00:00
|
|
|
if (sk_sp<SkTypeface> origEmoji = ToolUtils::emoji_typeface()) {
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
|
2019-03-20 16:12:10 +00:00
|
|
|
const char* emojiText = ToolUtils::emoji_sample_text();
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(emojiText, strlen(emojiText), SkTextEncoding::kUTF8, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, emojiText, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
}
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// build
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("textblobrandomfont");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
|
|
|
if (!canvas->recordingContext()) {
|
|
|
|
*errorMsg = "Active context required to create SkSurface";
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dContext = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
|
2015-08-01 14:33:41 +00:00
|
|
|
// This GM exists to test a specific feature of the GPU backend.
|
2019-03-20 16:12:10 +00:00
|
|
|
// This GM uses ToolUtils::makeSurface which doesn't work well with vias.
|
2018-04-30 19:39:15 +00:00
|
|
|
// This GM uses SkRandomTypeface which doesn't work well with serialization.
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2016-12-02 17:07:17 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
|
|
|
|
kPremul_SkAlphaType,
|
2017-01-12 15:13:40 +00:00
|
|
|
canvas->imageInfo().refColorSpace());
|
2016-07-26 18:36:05 +00:00
|
|
|
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::makeSurface(canvas, info, &props));
|
2018-04-30 19:39:15 +00:00
|
|
|
if (!surface) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "This test requires a surface";
|
|
|
|
return DrawResult::kFail;
|
2018-04-30 19:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2018-05-24 18:32:26 +00:00
|
|
|
SkCanvas* surfaceCanvas = surface->getCanvas();
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height());
|
|
|
|
SkScalar yOffset = 5;
|
2018-05-24 18:32:26 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
// Originally we would alternate between rotating and not to force blob regeneration,
|
|
|
|
// but that code seems to have rotted. Keeping the rotate to match the old GM as
|
|
|
|
// much as possible, and it seems like a reasonable stress test for transformed
|
|
|
|
// color emoji.
|
|
|
|
canvas->rotate(-0.05f);
|
|
|
|
canvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
|
|
|
yOffset += stride;
|
|
|
|
canvas->restore();
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
// Rotate in the surface canvas, not the final canvas, to avoid aliasing
|
|
|
|
surfaceCanvas->rotate(-0.05f);
|
|
|
|
surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
2021-01-06 13:43:51 +00:00
|
|
|
surface->draw(canvas, 0, 0);
|
2018-05-24 18:32:26 +00:00
|
|
|
yOffset += stride;
|
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
if (dContext) {
|
2020-06-29 19:36:12 +00:00
|
|
|
// free gpu resources and verify
|
2021-06-03 14:14:16 +00:00
|
|
|
dContext->freeGpuResources();
|
2020-06-29 19:36:12 +00:00
|
|
|
}
|
2018-05-24 18:32:26 +00:00
|
|
|
|
|
|
|
canvas->rotate(-0.05f);
|
|
|
|
canvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
|
|
|
yOffset += stride;
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> fBlob;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kWidth = 2000;
|
|
|
|
inline static constexpr int kHeight = 1600;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-08-01 14:33:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextBlobRandomFont;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|