2015-07-17 19:47:39 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sk_tool_utils.h"
|
2015-08-21 18:08:00 +00:00
|
|
|
|
2015-07-17 19:47:39 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkFontMgr.h"
|
2018-09-04 17:27:00 +00:00
|
|
|
#include "SkGlyphRun.h"
|
2015-07-17 19:47:39 +00:00
|
|
|
#include "SkGraphics.h"
|
2017-11-10 16:33:43 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPoint.h"
|
|
|
|
#include "SkRandomScalerContext.h"
|
2015-07-17 19:47:39 +00:00
|
|
|
#include "SkSurface.h"
|
2017-11-10 16:33:43 +00:00
|
|
|
#include "SkTextBlob.h"
|
2015-07-17 19:47:39 +00:00
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
#include "SkTypeface_win.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
#include "GrContext.h"
|
2018-03-06 13:20:37 +00:00
|
|
|
#include "GrContextPriv.h"
|
2015-07-17 19:47:39 +00:00
|
|
|
|
2016-09-13 17:00:23 +00:00
|
|
|
static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) {
|
2015-07-22 18:00:32 +00:00
|
|
|
int yOffset = 0;
|
2015-07-17 19:47:39 +00:00
|
|
|
for (int r = 0; r < redraw; r++) {
|
|
|
|
for (int i = 0; i < blobs.count(); i++) {
|
2016-09-13 17:00:23 +00:00
|
|
|
const auto& blob = blobs[i];
|
2015-07-22 18:00:32 +00:00
|
|
|
const SkRect& bounds = blob->bounds();
|
|
|
|
yOffset += SkScalarCeilToInt(bounds.height());
|
2015-07-17 19:47:39 +00:00
|
|
|
SkPaint paint;
|
2015-07-22 18:00:32 +00:00
|
|
|
canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
|
2015-07-17 19:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 15:30:25 +00:00
|
|
|
static const int kWidth = 1024;
|
|
|
|
static const int kHeight = 768;
|
2015-07-17 19:47:39 +00:00
|
|
|
|
2018-09-04 17:27:00 +00:00
|
|
|
static void setup_always_evict_atlas(GrContext* context) {
|
|
|
|
int dim = SkGlyphCacheCommon::kSkSideTooBigForAtlas;
|
|
|
|
// These sizes were selected because they allow each atlas to hold a single plot and will thus
|
|
|
|
// stress the atlas
|
|
|
|
GrDrawOpAtlasConfig configs[3];
|
|
|
|
configs[kA8_GrMaskFormat].fWidth = dim;
|
|
|
|
configs[kA8_GrMaskFormat].fHeight = dim;
|
|
|
|
configs[kA8_GrMaskFormat].fPlotWidth = dim;
|
|
|
|
configs[kA8_GrMaskFormat].fPlotHeight = dim;
|
|
|
|
|
|
|
|
configs[kA565_GrMaskFormat].fWidth = dim;
|
|
|
|
configs[kA565_GrMaskFormat].fHeight = dim;
|
|
|
|
configs[kA565_GrMaskFormat].fPlotWidth = dim;
|
|
|
|
configs[kA565_GrMaskFormat].fPlotHeight = dim;
|
|
|
|
|
|
|
|
configs[kARGB_GrMaskFormat].fWidth = dim;
|
|
|
|
configs[kARGB_GrMaskFormat].fHeight = dim;
|
|
|
|
configs[kARGB_GrMaskFormat].fPlotWidth = dim;
|
|
|
|
configs[kARGB_GrMaskFormat].fPlotHeight = dim;
|
|
|
|
|
|
|
|
context->contextPriv().setTextContextAtlasSizes_ForTesting(configs);
|
|
|
|
}
|
|
|
|
|
2015-07-17 19:47:39 +00:00
|
|
|
// This test hammers the GPU textblobcache and font atlas
|
2015-12-01 12:35:26 +00:00
|
|
|
static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
|
2015-08-21 18:08:00 +00:00
|
|
|
int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
|
|
|
|
bool stressTest) {
|
2015-07-17 19:47:39 +00:00
|
|
|
// setup surface
|
|
|
|
uint32_t flags = 0;
|
|
|
|
SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
|
|
|
|
|
2015-08-21 18:08:00 +00:00
|
|
|
// configure our context for maximum stressing of cache and atlas
|
|
|
|
if (stressTest) {
|
2018-09-04 17:27:00 +00:00
|
|
|
setup_always_evict_atlas(context);
|
2018-03-06 13:20:37 +00:00
|
|
|
context->contextPriv().setTextBlobCacheLimit_ForTesting(0);
|
2015-08-21 18:08:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 15:30:25 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
|
2015-07-17 19:47:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, surface);
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
|
2015-07-17 19:47:39 +00:00
|
|
|
|
2015-07-31 18:45:22 +00:00
|
|
|
int count = SkMin32(fm->countFamilies(), maxFamilies);
|
2015-07-17 19:47:39 +00:00
|
|
|
|
|
|
|
// make a ton of text
|
2015-07-31 18:45:22 +00:00
|
|
|
SkAutoTArray<uint16_t> text(maxTotalText);
|
|
|
|
for (int i = 0; i < maxTotalText; i++) {
|
|
|
|
text[i] = i % maxGlyphID;
|
2015-07-17 19:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate textblobs
|
2016-09-13 17:00:23 +00:00
|
|
|
SkTArray<sk_sp<SkTextBlob>> blobs;
|
2015-07-17 19:47:39 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
2015-07-23 15:30:25 +00:00
|
|
|
paint.setTextSize(48); // draw big glyphs to really stress the atlas
|
2015-07-17 19:47:39 +00:00
|
|
|
|
|
|
|
SkString familyName;
|
|
|
|
fm->getFamilyName(i, &familyName);
|
2016-11-04 15:49:42 +00:00
|
|
|
sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
|
2015-07-17 19:47:39 +00:00
|
|
|
for (int j = 0; j < set->count(); ++j) {
|
|
|
|
SkFontStyle fs;
|
2015-08-27 14:41:13 +00:00
|
|
|
set->getStyle(j, &fs, nullptr);
|
2015-07-17 19:47:39 +00:00
|
|
|
|
2015-07-31 18:45:22 +00:00
|
|
|
// We use a typeface which randomy returns unexpected mask formats to fuzz
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> orig(set->createTypeface(j));
|
2015-07-31 18:45:22 +00:00
|
|
|
if (normal) {
|
|
|
|
paint.setTypeface(orig);
|
|
|
|
} else {
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true));
|
2015-07-31 18:45:22 +00:00
|
|
|
}
|
2015-07-17 19:47:39 +00:00
|
|
|
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
for (int aa = 0; aa < 2; aa++) {
|
|
|
|
for (int subpixel = 0; subpixel < 2; subpixel++) {
|
|
|
|
for (int lcd = 0; lcd < 2; lcd++) {
|
|
|
|
paint.setAntiAlias(SkToBool(aa));
|
|
|
|
paint.setSubpixelText(SkToBool(subpixel));
|
|
|
|
paint.setLCDRenderText(SkToBool(lcd));
|
2015-07-31 18:45:22 +00:00
|
|
|
if (!SkToBool(lcd)) {
|
|
|
|
paint.setTextSize(160);
|
|
|
|
}
|
2015-07-17 19:47:39 +00:00
|
|
|
const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
|
2015-07-31 18:45:22 +00:00
|
|
|
maxTotalText,
|
2015-07-17 19:47:39 +00:00
|
|
|
0, 0,
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr);
|
2015-07-31 18:45:22 +00:00
|
|
|
memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
|
2015-07-17 19:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 17:00:23 +00:00
|
|
|
blobs.emplace_back(builder.make());
|
2015-07-17 19:47:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 15:30:25 +00:00
|
|
|
// create surface where LCD is impossible
|
|
|
|
info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
|
|
|
|
SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
|
2015-07-23 15:30:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, surface);
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
|
|
|
|
|
2015-07-17 19:47:39 +00:00
|
|
|
// test redraw
|
|
|
|
draw(canvas, 2, blobs);
|
2015-07-23 15:30:25 +00:00
|
|
|
draw(canvasNoLCD, 2, blobs);
|
2015-07-17 19:47:39 +00:00
|
|
|
|
|
|
|
// test draw after free
|
2015-12-01 12:35:26 +00:00
|
|
|
context->freeGpuResources();
|
2015-07-17 19:47:39 +00:00
|
|
|
draw(canvas, 1, blobs);
|
|
|
|
|
2015-12-01 12:35:26 +00:00
|
|
|
context->freeGpuResources();
|
2015-07-23 15:30:25 +00:00
|
|
|
draw(canvasNoLCD, 1, blobs);
|
|
|
|
|
2015-07-17 19:47:39 +00:00
|
|
|
// test draw after abandon
|
2015-12-01 12:35:26 +00:00
|
|
|
context->abandonContext();
|
2015-07-17 19:47:39 +00:00
|
|
|
draw(canvas, 1, blobs);
|
|
|
|
}
|
2015-07-31 18:45:22 +00:00
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobCache, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
|
2015-08-21 18:08:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
|
2015-07-31 18:45:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
|
2015-08-21 18:08:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:02:39 +00:00
|
|
|
DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
|
2016-05-11 13:33:06 +00:00
|
|
|
text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
|
2015-07-31 18:45:22 +00:00
|
|
|
}
|