2014-07-21 18:37:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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 "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2014-07-21 18:37:28 +00:00
|
|
|
|
|
|
|
enum ColorPattern {
|
|
|
|
kWhite_ColorPattern,
|
|
|
|
kBlue_ColorPattern,
|
|
|
|
kOpaqueBitmap_ColorPattern,
|
|
|
|
kAlphaBitmap_ColorPattern,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ColorPatternData{
|
|
|
|
SkColor fColor;
|
|
|
|
bool fIsBitmap;
|
|
|
|
const char* fName;
|
|
|
|
} gColorPatterns[] = {
|
|
|
|
// Keep this in same order as ColorPattern enum
|
|
|
|
{ SK_ColorWHITE, false, "white" }, // kWhite_ColorPattern
|
|
|
|
{ SK_ColorBLUE, false, "blue" }, // kBlue_ColorPattern
|
|
|
|
{ SK_ColorWHITE, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
|
|
|
|
{ 0x10000000, true, "alphaBitmap" }, // kAlphaBitmap_ColorPattern
|
|
|
|
};
|
|
|
|
|
|
|
|
enum DrawType {
|
|
|
|
kRect_DrawType,
|
|
|
|
kPath_DrawType,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void makebm(SkBitmap* bm, int w, int h) {
|
|
|
|
bm->allocN32Pixels(w, h);
|
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
2020-02-06 16:25:43 +00:00
|
|
|
SkScalar s = SkIntToScalar(std::min(w, h));
|
2014-07-21 18:37:28 +00:00
|
|
|
static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
|
|
|
|
static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
|
|
|
|
static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
|
|
|
|
static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
|
|
|
|
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, SK_ARRAY_COUNT(kColors0),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2014-07-21 18:37:28 +00:00
|
|
|
canvas.drawPaint(paint);
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp));
|
2014-07-21 18:37:28 +00:00
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This bench draws a grid of either rects or filled paths, with two alternating color patterns.
|
|
|
|
* This color patterns are passed in as enums to the class. The options are:
|
|
|
|
* 1) solid white color
|
|
|
|
* 2) solid blue color
|
|
|
|
* 3) opaque bitmap
|
|
|
|
* 4) partial alpha bitmap
|
|
|
|
* The same color pattern can be set for both arguments to create a uniform pattern on all draws.
|
|
|
|
*
|
|
|
|
* The bench is used to test a few things. First it can test any optimizations made for a specific
|
|
|
|
* color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
|
2016-12-21 16:14:46 +00:00
|
|
|
* be used to test the cost of program switching and/or GrDrawOp combining when alternating between
|
|
|
|
* different patterns when on the gpu.
|
2014-07-21 18:37:28 +00:00
|
|
|
*/
|
|
|
|
class AlternatingColorPatternBench : public Benchmark {
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
NX = 5,
|
|
|
|
NY = 5,
|
|
|
|
NUM_DRAWS = NX * NY,
|
|
|
|
};
|
2016-03-14 19:22:10 +00:00
|
|
|
sk_sp<SkShader> fBmShader;
|
2014-07-21 18:37:28 +00:00
|
|
|
|
|
|
|
SkPath fPaths[NUM_DRAWS];
|
|
|
|
SkRect fRects[NUM_DRAWS];
|
|
|
|
SkColor fColors[NUM_DRAWS];
|
2016-03-14 19:22:10 +00:00
|
|
|
sk_sp<SkShader> fShaders[NUM_DRAWS];
|
2014-07-21 18:37:28 +00:00
|
|
|
|
|
|
|
SkString fName;
|
|
|
|
ColorPatternData fPattern1;
|
|
|
|
ColorPatternData fPattern2;
|
|
|
|
DrawType fDrawType;
|
|
|
|
SkBitmap fBmp;
|
|
|
|
|
|
|
|
|
2016-03-14 19:22:10 +00:00
|
|
|
AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) {
|
2014-07-21 18:37:28 +00:00
|
|
|
fPattern1 = gColorPatterns[pattern1];
|
|
|
|
fPattern2 = gColorPatterns[pattern2];
|
|
|
|
fName.printf("colorPattern_%s_%s_%s",
|
|
|
|
fPattern1.fName, fPattern2.fName,
|
|
|
|
kRect_DrawType == drawType ? "rect" : "path");
|
|
|
|
fDrawType = drawType;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2014-07-21 18:37:28 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2014-07-21 18:37:28 +00:00
|
|
|
int w = 40;
|
|
|
|
int h = 40;
|
|
|
|
makebm(&fBmp, w, h);
|
2020-12-12 14:51:11 +00:00
|
|
|
fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
2021-01-15 02:59:01 +00:00
|
|
|
SkSamplingOptions(SkFilterMode::kLinear));
|
2014-07-21 18:37:28 +00:00
|
|
|
int offset = 2;
|
|
|
|
int count = 0;
|
|
|
|
for (int j = 0; j < NY; ++j) {
|
|
|
|
for (int i = 0; i < NX; ++i) {
|
|
|
|
int x = (w + offset) * i;
|
|
|
|
int y = (h * offset) * j;
|
|
|
|
if (kRect_DrawType == fDrawType) {
|
2019-08-24 23:39:13 +00:00
|
|
|
fRects[count].setXYWH(SkIntToScalar(x), SkIntToScalar(y),
|
|
|
|
SkIntToScalar(w), SkIntToScalar(h));
|
2014-07-21 18:37:28 +00:00
|
|
|
} else {
|
|
|
|
fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
fPaths[count].rLineTo(SkIntToScalar(w), 0);
|
|
|
|
fPaths[count].rLineTo(0, SkIntToScalar(h));
|
|
|
|
fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
|
|
|
|
}
|
|
|
|
if (0 == count % 2) {
|
|
|
|
fColors[count] = fPattern1.fColor;
|
2015-08-27 14:41:13 +00:00
|
|
|
fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
|
2014-07-21 18:37:28 +00:00
|
|
|
} else {
|
|
|
|
fColors[count] = fPattern2.fColor;
|
2015-08-27 14:41:13 +00:00
|
|
|
fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
|
2014-07-21 18:37:28 +00:00
|
|
|
}
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-07-21 18:37:28 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(false);
|
|
|
|
|
|
|
|
for (int i = 0; i < loops; ++i) {
|
|
|
|
for (int j = 0; j < NUM_DRAWS; ++j) {
|
|
|
|
paint.setColor(fColors[j]);
|
|
|
|
paint.setShader(fShaders[j]);
|
|
|
|
if (kRect_DrawType == fDrawType) {
|
|
|
|
canvas->drawRect(fRects[j], paint);
|
|
|
|
} else {
|
|
|
|
canvas->drawPath(fPaths[j], paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2014-07-21 18:37:28 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
|
|
|
|
kWhite_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
|
|
|
|
kBlue_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
|
|
|
|
kBlue_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
|
|
|
|
kOpaqueBitmap_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
|
|
|
|
kAlphaBitmap_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
|
|
|
|
kAlphaBitmap_ColorPattern,
|
|
|
|
kPath_DrawType);)
|
|
|
|
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
|
|
|
|
kOpaqueBitmap_ColorPattern,
|
|
|
|
kRect_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
|
|
|
|
kAlphaBitmap_ColorPattern,
|
|
|
|
kRect_DrawType);)
|
|
|
|
DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
|
|
|
|
kAlphaBitmap_ColorPattern,
|
|
|
|
kRect_DrawType);)
|