skia2/gm/coloremoji_blendmodes.cpp

175 lines
5.4 KiB
C++
Raw Normal View History

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkFontTypes.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/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"
#include "include/effects/SkGradientShader.h"
#include "include/utils/SkTextUtils.h"
#include "src/utils/SkUTF.h"
#include "tools/ToolUtils.h"
#include <string.h>
namespace {
static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
class ColorEmojiBlendModesGM : public skiagm::GM {
public:
const static int W = 64;
const static int H = 64;
ColorEmojiBlendModesGM() {}
Move font loading in gm tests and benches out of constructors Constructing the gm tests and benches causes many calls to font loads. This is visible as profiling samples in fontconfig and freetype on Linux for all profiling runs of nanobench. This complicates analysis of test-cases that are suspected of being slow due to font-related issues. Move the font loading to GM::onOnceBeforeDraw and Benchmark::onPreDraw. This way the code is not executed if the testcase does not match the nanobench --match filter. This way the samples in font-related code are more easy to identify as legitimate occurances caused by the testcase. This should not cause differences in timings, because: * Benchmark::preDraw / onPreDraw is defined to be run outside the timer * GM::runAsBench is not enabled for any of the modified testcases. Also nanobench untimed warmup round should run the onOnceBeforeDraw. (and there are other GM::runAsBench gms already doing loading in onOnceBeforeDraw). Changes the behavior: In TextBench: Before, the test would report two different gms with the same name if the color emoji font was not loaded successfully. After, the test always reports all tests as individual names. Generally: The errors from loading fonts now print inbetween each testcase, as opposed to printing during construction phase. Sample output: ( 143/145 MB 1872) 14.7ms 8888 gm quadclosepathResource /fonts/Funkster.ttf not a valid font. ( 160/160 MB 1831) 575µs 8888 gm surfacenewResource /fonts/Funkster.ttf not a valid font. ( 163/165 MB 1816) 12.5ms 8888 gm linepathResource /fonts/Funkster.ttf not a valid font. ( 263/411 MB 1493) 118ms 8888 gm typefacestyles_kerningResource /fonts/Funkster.ttf not a valid font. ( 374/411 MB 1231) 7.16ms 565 gm getpostextpathResource /fonts/Funkster.ttf not a valid font. ( 323/411 MB 1179) 4.92ms 565 gm stringartResource /fonts/Funkster.ttf not a valid font. ( 347/493 MB 917) 191ms 565 gm patch_gridResource /fonts/Funkster.ttf not a valid font. ( 375/493 MB 857) 23.9ms gpu gm clipdrawdrawCannot render path (0) ( 393/493 MB 706) 2.91ms unit test ParsePath------ png error IEND: CRC error ( 394/493 MB 584) 166ms gpu gm hairmodesResource /fonts/Funkster.ttf not a valid font. Resource /fonts/Funkster.ttf not a valid font. Resource /fonts/Funkster.ttf not a valid font. ... Review URL: https://codereview.chromium.org/1144023002
2015-05-21 13:15:28 +00:00
protected:
void onOnceBeforeDraw() override {
const SkColor colors[] = {
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW
};
SkMatrix local;
local.setRotate(180);
SkPaint paint;
paint.setAntiAlias(true);
paint.setShader(SkGradientShader::MakeSweep(0, 0, colors, nullptr, SK_ARRAY_COUNT(colors),
0, &local));
sk_sp<SkTypeface> orig(ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold()));
if (nullptr == orig) {
orig = SkTypeface::MakeDefault();
}
fColorType = ToolUtils::emoji_typeface();
Move font loading in gm tests and benches out of constructors Constructing the gm tests and benches causes many calls to font loads. This is visible as profiling samples in fontconfig and freetype on Linux for all profiling runs of nanobench. This complicates analysis of test-cases that are suspected of being slow due to font-related issues. Move the font loading to GM::onOnceBeforeDraw and Benchmark::onPreDraw. This way the code is not executed if the testcase does not match the nanobench --match filter. This way the samples in font-related code are more easy to identify as legitimate occurances caused by the testcase. This should not cause differences in timings, because: * Benchmark::preDraw / onPreDraw is defined to be run outside the timer * GM::runAsBench is not enabled for any of the modified testcases. Also nanobench untimed warmup round should run the onOnceBeforeDraw. (and there are other GM::runAsBench gms already doing loading in onOnceBeforeDraw). Changes the behavior: In TextBench: Before, the test would report two different gms with the same name if the color emoji font was not loaded successfully. After, the test always reports all tests as individual names. Generally: The errors from loading fonts now print inbetween each testcase, as opposed to printing during construction phase. Sample output: ( 143/145 MB 1872) 14.7ms 8888 gm quadclosepathResource /fonts/Funkster.ttf not a valid font. ( 160/160 MB 1831) 575µs 8888 gm surfacenewResource /fonts/Funkster.ttf not a valid font. ( 163/165 MB 1816) 12.5ms 8888 gm linepathResource /fonts/Funkster.ttf not a valid font. ( 263/411 MB 1493) 118ms 8888 gm typefacestyles_kerningResource /fonts/Funkster.ttf not a valid font. ( 374/411 MB 1231) 7.16ms 565 gm getpostextpathResource /fonts/Funkster.ttf not a valid font. ( 323/411 MB 1179) 4.92ms 565 gm stringartResource /fonts/Funkster.ttf not a valid font. ( 347/493 MB 917) 191ms 565 gm patch_gridResource /fonts/Funkster.ttf not a valid font. ( 375/493 MB 857) 23.9ms gpu gm clipdrawdrawCannot render path (0) ( 393/493 MB 706) 2.91ms unit test ParsePath------ png error IEND: CRC error ( 394/493 MB 584) 166ms gpu gm hairmodesResource /fonts/Funkster.ttf not a valid font. Resource /fonts/Funkster.ttf not a valid font. Resource /fonts/Funkster.ttf not a valid font. ... Review URL: https://codereview.chromium.org/1144023002
2015-05-21 13:15:28 +00:00
fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
kOpaque_SkAlphaType), gData, 4);
}
SkString onShortName() override {
return SkString("coloremoji_blendmodes");
}
SkISize onISize() override {
return {400, 640};
}
void onDraw(SkCanvas* canvas) override {
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
const SkBlendMode gModes[] = {
SkBlendMode::kClear,
SkBlendMode::kSrc,
SkBlendMode::kDst,
SkBlendMode::kSrcOver,
SkBlendMode::kDstOver,
SkBlendMode::kSrcIn,
SkBlendMode::kDstIn,
SkBlendMode::kSrcOut,
SkBlendMode::kDstOut,
SkBlendMode::kSrcATop,
SkBlendMode::kDstATop,
SkBlendMode::kXor,
SkBlendMode::kPlus,
SkBlendMode::kModulate,
SkBlendMode::kScreen,
SkBlendMode::kOverlay,
SkBlendMode::kDarken,
SkBlendMode::kLighten,
SkBlendMode::kColorDodge,
SkBlendMode::kColorBurn,
SkBlendMode::kHardLight,
SkBlendMode::kSoftLight,
SkBlendMode::kDifference,
SkBlendMode::kExclusion,
SkBlendMode::kMultiply,
SkBlendMode::kHue,
SkBlendMode::kSaturation,
SkBlendMode::kColor,
SkBlendMode::kLuminosity,
};
const SkScalar w = SkIntToScalar(W);
const SkScalar h = SkIntToScalar(H);
SkMatrix m;
m.setScale(SkIntToScalar(6), SkIntToScalar(6));
auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m);
SkFont labelFont(ToolUtils::create_portable_typeface());
SkPaint textP;
textP.setAntiAlias(true);
SkFont textFont(fColorType, 70);
const int kWrap = 5;
SkScalar x0 = 0;
SkScalar y0 = 0;
SkScalar x = x0, y = y0;
for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
SkRect r;
r.setLTRB(x, y, x+w, y+h);
SkPaint p;
p.setStyle(SkPaint::kFill_Style);
p.setShader(s);
canvas->drawRect(r, p);
r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
p.setStyle(SkPaint::kStroke_Style);
p.setShader(nullptr);
canvas->drawRect(r, p);
{
SkAutoCanvasRestore arc(canvas, true);
canvas->clipRect(r);
textP.setBlendMode(gModes[i]);
const char* text = ToolUtils::emoji_sample_text();
SkUnichar unichar = SkUTF::NextUTF8(&text, text + strlen(text));
SkASSERT(unichar >= 0);
canvas->drawSimpleText(&unichar, 4, SkTextEncoding::kUTF32,
x+ w/10.f, y + 7.f*h/8.f, textFont, textP);
}
#if 1
const char* label = SkBlendMode_Name(gModes[i]);
SkTextUtils::DrawString(canvas, label, x + w/2, y - labelFont.getSize()/2,
labelFont, SkPaint(), SkTextUtils::kCenter_Align);
#endif
x += w + SkIntToScalar(10);
if ((i % kWrap) == kWrap - 1) {
x = x0;
y += h + SkIntToScalar(30);
}
}
}
private:
SkBitmap fBG;
sk_sp<SkTypeface> fColorType;
using INHERITED = GM;
};
} // namespace
DEF_GM( return new ColorEmojiBlendModesGM; )