blob: 6a7774012dab3412e831e234afdaaa732bef8f65 [file] [log] [blame]
sugoice686272014-10-09 05:27:23 -07001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "Benchmark.h"
8#include "SkCanvas.h"
9#include "SkColorCubeFilter.h"
10#include "SkGradientShader.h"
scroggo565901d2015-12-10 10:44:13 -080011#include "SkTemplates.h"
sugoice686272014-10-09 05:27:23 -070012
13class ColorCubeBench : public Benchmark {
14 SkISize fSize;
15 int fCubeDimension;
reedd053ce92016-03-22 10:17:23 -070016 sk_sp<SkData> fCubeData;
sugoice686272014-10-09 05:27:23 -070017 SkBitmap fBitmap;
18
19public:
reedd053ce92016-03-22 10:17:23 -070020 ColorCubeBench() : fCubeDimension(0) {
sugoice686272014-10-09 05:27:23 -070021 fSize = SkISize::Make(2880, 1800); // 2014 Macbook Pro resolution
22 }
23
sugoice686272014-10-09 05:27:23 -070024protected:
mtklein36352bf2015-03-25 18:17:31 -070025 const char* onGetName() override {
sugoice686272014-10-09 05:27:23 -070026 return "colorcube";
27 }
28
joshualitt8a6697a2015-09-30 12:11:07 -070029 void onDelayedSetup() override {
sugoice686272014-10-09 05:27:23 -070030 if (!SkToBool(fCubeData)) {
31 this->makeCubeData();
32 this->make_bitmap();
33 }
34 }
35
mtkleina1ebeb22015-10-01 09:43:39 -070036 void onDraw(int loops, SkCanvas* canvas) override {
sugoice686272014-10-09 05:27:23 -070037 this->test(loops, canvas);
38 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkIPoint onGetSize() override {
sugoice686272014-10-09 05:27:23 -070041 return SkIPoint::Make(fSize.width(), fSize.height());
42 }
43
44private:
reedc6f28f72016-03-14 12:22:10 -070045 static sk_sp<SkShader> MakeLinear(const SkISize& size) {
sugoice686272014-10-09 05:27:23 -070046 const SkPoint pts[2] = {
47 { 0, 0 },
48 { SkIntToScalar(size.width()), SkIntToScalar(size.height()) }
49 };
50 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
reedc6f28f72016-03-14 12:22:10 -070051 return SkGradientShader::MakeLinear(
halcanary96fcdcc2015-08-27 07:41:13 -070052 pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
sugoice686272014-10-09 05:27:23 -070053 }
54
55 void make_bitmap() {
56 fBitmap.allocN32Pixels(fSize.width(), fSize.height());
57 SkCanvas canvas(fBitmap);
58 canvas.clear(0x00000000);
59 SkPaint paint;
60 paint.setAntiAlias(true);
reedc6f28f72016-03-14 12:22:10 -070061 paint.setShader(MakeLinear(fSize));
sugoice686272014-10-09 05:27:23 -070062 SkRect r = { 0, 0, SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()) };
63 canvas.drawRect(r, paint);
sugoice686272014-10-09 05:27:23 -070064 }
65
66 void makeCubeData() {
67 fCubeDimension = 32;
reedd053ce92016-03-22 10:17:23 -070068 fCubeData = SkData::MakeUninitialized(sizeof(SkColor) *
sugoice686272014-10-09 05:27:23 -070069 fCubeDimension * fCubeDimension * fCubeDimension);
70 SkColor* pixels = (SkColor*)(fCubeData->writable_data());
scroggo565901d2015-12-10 10:44:13 -080071 SkAutoTMalloc<uint8_t> lutMemory(fCubeDimension);
72 uint8_t* lut = lutMemory.get();
sugoice686272014-10-09 05:27:23 -070073 const int maxIndex = fCubeDimension - 1;
74 for (int i = 0; i < fCubeDimension; ++i) {
75 // Make an invert lut, but the content of
76 // the lut shouldn't affect performance.
77 lut[i] = ((maxIndex - i) * 255) / maxIndex;
78 }
79 for (int r = 0; r < fCubeDimension; ++r) {
80 for (int g = 0; g < fCubeDimension; ++g) {
81 for (int b = 0; b < fCubeDimension; ++b) {
82 pixels[(fCubeDimension * ((fCubeDimension * b) + g)) + r] =
83 SkColorSetARGB(0xFF, lut[r], lut[g], lut[b]);
84 }
85 }
86 }
87 }
88
mtkleina1ebeb22015-10-01 09:43:39 -070089 void test(int loops, SkCanvas* canvas) {
sugoice686272014-10-09 05:27:23 -070090 SkPaint paint;
sugoice686272014-10-09 05:27:23 -070091 for (int i = 0; i < loops; i++) {
reedd053ce92016-03-22 10:17:23 -070092 paint.setColorFilter(SkColorCubeFilter::Make(fCubeData, fCubeDimension));
sugoice686272014-10-09 05:27:23 -070093 canvas->drawBitmap(fBitmap, 0, 0, &paint);
94 }
95 }
96
97 typedef Benchmark INHERITED;
98};
99
100///////////////////////////////////////////////////////////////////////////////
101
102DEF_BENCH( return new ColorCubeBench(); )