2014-08-15 20:30:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPatchGrid.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
/**
|
2016-03-29 16:03:52 +00:00
|
|
|
* This bench measures the rendering time of a gridof patches.
|
|
|
|
* This bench also tests the different combination of optional parameters for the function
|
2014-08-15 20:30:47 +00:00
|
|
|
* (passing texture coordinates and colors, only textures coordinates, only colors or none).
|
|
|
|
* Finally, it also has 3 possible sizes small, medium and big to test if the size of the patches
|
2016-03-29 16:03:52 +00:00
|
|
|
* in the grid affects time.
|
2014-08-15 20:30:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class PatchGridBench : public Benchmark {
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
public:
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
enum Size {
|
|
|
|
kSmall_Size,
|
|
|
|
kMedium_Size,
|
|
|
|
kBig_Size
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
enum VertexMode {
|
|
|
|
kNone_VertexMode,
|
|
|
|
kColors_VertexMode,
|
|
|
|
kTexCoords_VertexMode,
|
|
|
|
kBoth_VertexMode
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
PatchGridBench(Size size, VertexMode vertexMode)
|
|
|
|
: fVertexMode(vertexMode)
|
|
|
|
, fSize(size) { }
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
void setScale(SkCanvas* canvas){
|
|
|
|
switch (fSize) {
|
|
|
|
case kSmall_Size:
|
|
|
|
canvas->scale(0.1f, 0.1f);
|
|
|
|
break;
|
|
|
|
case kMedium_Size:
|
|
|
|
canvas->scale(1.0f, 1.0f);
|
|
|
|
break;
|
|
|
|
case kBig_Size:
|
|
|
|
canvas->scale(3.0f, 3.0f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
void setGrid() {
|
|
|
|
SkPoint vertices[4][5] = {
|
|
|
|
{{50,50}, {150,50}, {250,50},{350,50},{450,50}},
|
|
|
|
{{50,150}, {120,120}, {250,150},{350,150},{450,150}},
|
|
|
|
{{50,250}, {150,250}, {250,250},{350,250},{450,250}},
|
|
|
|
{{100,300}, {150,350}, {250,350},{350,350},{450,350}}
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkColor cornerColors[4][5] = {
|
|
|
|
{SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
|
|
|
|
{SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
|
|
|
|
{SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE},
|
|
|
|
{SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED},
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkPoint texCoords[4][5] = {
|
|
|
|
{{0.0f,0.0f}, {1.0f,0.0f}, {2.0f,0.0f}, {3.0f,0.0f}, {4.0f,0.0f}},
|
|
|
|
{{0.0f,1.0f}, {1.0f,1.0f}, {2.0f,1.0f}, {3.0f,1.0f}, {4.0f,1.0f}},
|
|
|
|
{{0.0f,2.0f}, {1.0f,2.0f}, {2.0f,2.0f}, {3.0f,2.0f}, {4.0f,2.0f}},
|
|
|
|
{{0.0f,3.0f}, {1.0f,3.0f}, {2.0f,3.0f}, {3.0f,3.0f}, {4.0f,3.0f}},
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkPoint hrzCtrl[4][8] = {
|
|
|
|
{{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,90}},
|
|
|
|
{{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150},{425,150}},
|
|
|
|
{{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250},{425,250}},
|
|
|
|
{{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350},{425,350}}
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkPoint vrtCtrl[6][5] = {
|
|
|
|
{{50,75},{150,75},{250,75},{350,75},{450,75}},
|
|
|
|
{{50,125},{150,125},{250,125},{350,125},{450,125}},
|
|
|
|
{{50,175},{150,175},{220,225},{350,175},{470,225}},
|
|
|
|
{{50,225},{150,225},{220,175},{350,225},{470,155}},
|
|
|
|
{{50,275},{150,275},{250,275},{350,275},{400,305}},
|
|
|
|
{{50,325},{150,325},{250,325},{350,325},{450,325}}
|
|
|
|
};
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
static const int kRows = 3;
|
|
|
|
static const int kCols = 4;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-10-28 19:42:34 +00:00
|
|
|
fGrid.reset(kRows, kCols, SkPatchGrid::kColors_VertexType, SkBlendMode::kModulate);
|
2014-08-15 20:30:47 +00:00
|
|
|
for (int i = 0; i < kRows; i++) {
|
|
|
|
for (int j = 0; j < kCols; j++) {
|
|
|
|
SkPoint points[12];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
//set corners
|
|
|
|
points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j];
|
|
|
|
points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1];
|
|
|
|
points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j];
|
|
|
|
points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j + 1];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2];
|
|
|
|
points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1];
|
|
|
|
points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j * 2];
|
|
|
|
points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j * 2 + 1];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j];
|
|
|
|
points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j];
|
|
|
|
points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j + 1];
|
|
|
|
points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j + 1];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkColor colors[4];
|
|
|
|
colors[0] = cornerColors[i][j];
|
|
|
|
colors[1] = cornerColors[i][j + 1];
|
|
|
|
colors[3] = cornerColors[i + 1][j];
|
|
|
|
colors[2] = cornerColors[i + 1][j + 1];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkPoint texs[4];
|
|
|
|
texs[0] = texCoords[i][j];
|
|
|
|
texs[1] = texCoords[i][j + 1];
|
|
|
|
texs[3] = texCoords[i + 1][j];
|
|
|
|
texs[2] = texCoords[i + 1][j + 1];
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
switch (fVertexMode) {
|
|
|
|
case kNone_VertexMode:
|
2015-08-27 14:41:13 +00:00
|
|
|
fGrid.setPatch(j, i, points, nullptr, nullptr);
|
2014-08-15 20:30:47 +00:00
|
|
|
break;
|
|
|
|
case kColors_VertexMode:
|
2015-08-27 14:41:13 +00:00
|
|
|
fGrid.setPatch(j, i, points, colors, nullptr);
|
2014-08-15 20:30:47 +00:00
|
|
|
break;
|
|
|
|
case kTexCoords_VertexMode:
|
2015-08-27 14:41:13 +00:00
|
|
|
fGrid.setPatch(j, i, points, nullptr, texs);
|
2014-08-15 20:30:47 +00:00
|
|
|
break;
|
|
|
|
case kBoth_VertexMode:
|
|
|
|
fGrid.setPatch(j, i, points, colors, texs);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
// override this method to change the shader
|
2016-03-14 19:22:10 +00:00
|
|
|
sk_sp<SkShader> createShader() {
|
2014-08-15 20:30:47 +00:00
|
|
|
const SkColor colors[] = {
|
|
|
|
SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE,
|
|
|
|
SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW,
|
|
|
|
};
|
|
|
|
const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } };
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-03-14 19:22:10 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kMirror_TileMode);
|
2014-08-15 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2014-08-15 20:30:47 +00:00
|
|
|
SkString vertexMode;
|
|
|
|
switch (fVertexMode) {
|
|
|
|
case kNone_VertexMode:
|
|
|
|
vertexMode.set("meshlines");
|
|
|
|
break;
|
|
|
|
case kColors_VertexMode:
|
|
|
|
vertexMode.set("colors");
|
|
|
|
break;
|
|
|
|
case kTexCoords_VertexMode:
|
|
|
|
vertexMode.set("texs");
|
|
|
|
break;
|
|
|
|
case kBoth_VertexMode:
|
|
|
|
vertexMode.set("colors_texs");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
SkString size;
|
|
|
|
switch (fSize) {
|
|
|
|
case kSmall_Size:
|
|
|
|
size.set("small");
|
|
|
|
break;
|
|
|
|
case kMedium_Size:
|
|
|
|
size.set("medium");
|
|
|
|
break;
|
|
|
|
case kBig_Size:
|
|
|
|
size.set("big");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fName.printf("patch_grid_%s_%s", vertexMode.c_str(), size.c_str());
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2014-08-15 20:30:47 +00:00
|
|
|
this->setGrid();
|
|
|
|
switch (fVertexMode) {
|
|
|
|
case kTexCoords_VertexMode:
|
|
|
|
case kBoth_VertexMode:
|
2016-03-14 19:22:10 +00:00
|
|
|
fPaint.setShader(createShader());
|
2014-08-15 20:30:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-27 14:41:13 +00:00
|
|
|
fPaint.setShader(nullptr);
|
2014-08-15 20:30:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->setupPaint(&fPaint);
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-08-15 20:30:47 +00:00
|
|
|
this->setScale(canvas);
|
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
fGrid.draw(canvas, fPaint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint fPaint;
|
|
|
|
SkString fName;
|
|
|
|
SkPatchGrid fGrid;
|
|
|
|
VertexMode fVertexMode;
|
|
|
|
Size fSize;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-15 20:30:47 +00:00
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
|
|
|
|
PatchGridBench::kNone_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
|
|
|
|
PatchGridBench::kColors_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
|
|
|
|
PatchGridBench::kTexCoords_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size,
|
|
|
|
PatchGridBench::kBoth_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
|
|
|
|
PatchGridBench::kNone_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
|
|
|
|
PatchGridBench::kColors_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
|
|
|
|
PatchGridBench::kTexCoords_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size,
|
|
|
|
PatchGridBench::kBoth_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
|
|
|
|
PatchGridBench::kNone_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
|
|
|
|
PatchGridBench::kColors_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
|
|
|
|
PatchGridBench::kTexCoords_VertexMode); )
|
|
|
|
DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size,
|
|
|
|
PatchGridBench::kBoth_VertexMode); )
|