2014-10-09 12:27:23 +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 "SkColorCubeFilter.h"
|
|
|
|
#include "SkGradientShader.h"
|
2015-12-10 18:44:13 +00:00
|
|
|
#include "SkTemplates.h"
|
2014-10-09 12:27:23 +00:00
|
|
|
|
|
|
|
class ColorCubeBench : public Benchmark {
|
|
|
|
SkISize fSize;
|
|
|
|
int fCubeDimension;
|
2016-03-22 17:17:23 +00:00
|
|
|
sk_sp<SkData> fCubeData;
|
2014-10-09 12:27:23 +00:00
|
|
|
SkBitmap fBitmap;
|
|
|
|
|
|
|
|
public:
|
2016-03-22 17:17:23 +00:00
|
|
|
ColorCubeBench() : fCubeDimension(0) {
|
2014-10-09 12:27:23 +00:00
|
|
|
fSize = SkISize::Make(2880, 1800); // 2014 Macbook Pro resolution
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2014-10-09 12:27:23 +00:00
|
|
|
return "colorcube";
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2014-10-09 12:27:23 +00:00
|
|
|
if (!SkToBool(fCubeData)) {
|
|
|
|
this->makeCubeData();
|
|
|
|
this->make_bitmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2014-10-09 12:27:23 +00:00
|
|
|
this->test(loops, canvas);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkIPoint onGetSize() override {
|
2014-10-09 12:27:23 +00:00
|
|
|
return SkIPoint::Make(fSize.width(), fSize.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-14 19:22:10 +00:00
|
|
|
static sk_sp<SkShader> MakeLinear(const SkISize& size) {
|
2014-10-09 12:27:23 +00:00
|
|
|
const SkPoint pts[2] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(size.width()), SkIntToScalar(size.height()) }
|
|
|
|
};
|
|
|
|
static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
|
2016-03-14 19:22:10 +00:00
|
|
|
return SkGradientShader::MakeLinear(
|
2015-08-27 14:41:13 +00:00
|
|
|
pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
|
2014-10-09 12:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
|
|
|
fBitmap.allocN32Pixels(fSize.width(), fSize.height());
|
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(MakeLinear(fSize));
|
2014-10-09 12:27:23 +00:00
|
|
|
SkRect r = { 0, 0, SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()) };
|
|
|
|
canvas.drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void makeCubeData() {
|
|
|
|
fCubeDimension = 32;
|
2016-03-22 17:17:23 +00:00
|
|
|
fCubeData = SkData::MakeUninitialized(sizeof(SkColor) *
|
2014-10-09 12:27:23 +00:00
|
|
|
fCubeDimension * fCubeDimension * fCubeDimension);
|
|
|
|
SkColor* pixels = (SkColor*)(fCubeData->writable_data());
|
2015-12-10 18:44:13 +00:00
|
|
|
SkAutoTMalloc<uint8_t> lutMemory(fCubeDimension);
|
|
|
|
uint8_t* lut = lutMemory.get();
|
2014-10-09 12:27:23 +00:00
|
|
|
const int maxIndex = fCubeDimension - 1;
|
|
|
|
for (int i = 0; i < fCubeDimension; ++i) {
|
|
|
|
// Make an invert lut, but the content of
|
|
|
|
// the lut shouldn't affect performance.
|
|
|
|
lut[i] = ((maxIndex - i) * 255) / maxIndex;
|
|
|
|
}
|
|
|
|
for (int r = 0; r < fCubeDimension; ++r) {
|
|
|
|
for (int g = 0; g < fCubeDimension; ++g) {
|
|
|
|
for (int b = 0; b < fCubeDimension; ++b) {
|
|
|
|
pixels[(fCubeDimension * ((fCubeDimension * b) + g)) + r] =
|
|
|
|
SkColorSetARGB(0xFF, lut[r], lut[g], lut[b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void test(int loops, SkCanvas* canvas) {
|
2014-10-09 12:27:23 +00:00
|
|
|
SkPaint paint;
|
|
|
|
for (int i = 0; i < loops; i++) {
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(fCubeData, fCubeDimension));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0, &paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_BENCH( return new ColorCubeBench(); )
|