skia2/bench/ColorCubeBench.cpp
reed d053ce9c54 Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #1 id:1 of https://codereview.chromium.org/1821103004/ )
Reason for revert:
guard has now landed in chrome

Original issue's description:
> Revert of Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (patchset #3 id:40001 of https://codereview.chromium.org/1825073002/ )
>
> Reason for revert:
> CreateModeFilter not compiling
>
> Original issue's description:
> > Revert[2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.org/1822623002/ )"
> >
> > Fixed legacy withColorFilter to call new(er) make method
> >
> > This reverts commit 1eb81db650.
> >
> > BUG=skia:
> > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1825073002
> >
> > TBR=
> >
> > Committed: https://skia.googlesource.com/skia/+/4c9776b046dd5e9e46e2d1ce35154855c8fcb381
>
> TBR=
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/d6889293dd0942f27f9593f679722c956831f2c4

TBR=
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=skia:

Review URL: https://codereview.chromium.org/1827433002
2016-03-22 10:17:23 -07:00

103 lines
3.1 KiB
C++

/*
* 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"
#include "SkTemplates.h"
class ColorCubeBench : public Benchmark {
SkISize fSize;
int fCubeDimension;
sk_sp<SkData> fCubeData;
SkBitmap fBitmap;
public:
ColorCubeBench() : fCubeDimension(0) {
fSize = SkISize::Make(2880, 1800); // 2014 Macbook Pro resolution
}
protected:
const char* onGetName() override {
return "colorcube";
}
void onDelayedSetup() override {
if (!SkToBool(fCubeData)) {
this->makeCubeData();
this->make_bitmap();
}
}
void onDraw(int loops, SkCanvas* canvas) override {
this->test(loops, canvas);
}
SkIPoint onGetSize() override {
return SkIPoint::Make(fSize.width(), fSize.height());
}
private:
static sk_sp<SkShader> MakeLinear(const SkISize& size) {
const SkPoint pts[2] = {
{ 0, 0 },
{ SkIntToScalar(size.width()), SkIntToScalar(size.height()) }
};
static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
return SkGradientShader::MakeLinear(
pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
}
void make_bitmap() {
fBitmap.allocN32Pixels(fSize.width(), fSize.height());
SkCanvas canvas(fBitmap);
canvas.clear(0x00000000);
SkPaint paint;
paint.setAntiAlias(true);
paint.setShader(MakeLinear(fSize));
SkRect r = { 0, 0, SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()) };
canvas.drawRect(r, paint);
}
void makeCubeData() {
fCubeDimension = 32;
fCubeData = SkData::MakeUninitialized(sizeof(SkColor) *
fCubeDimension * fCubeDimension * fCubeDimension);
SkColor* pixels = (SkColor*)(fCubeData->writable_data());
SkAutoTMalloc<uint8_t> lutMemory(fCubeDimension);
uint8_t* lut = lutMemory.get();
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]);
}
}
}
}
void test(int loops, SkCanvas* canvas) {
SkPaint paint;
for (int i = 0; i < loops; i++) {
paint.setColorFilter(SkColorCubeFilter::Make(fCubeData, fCubeDimension));
canvas->drawBitmap(fBitmap, 0, 0, &paint);
}
}
typedef Benchmark INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
DEF_BENCH( return new ColorCubeBench(); )