blob: dc183bc3ae9d01531a85bc69e5906fe647ab3311 [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
8#include "gm.h"
9#include "SkColorCubeFilter.h"
sugoice686272014-10-09 05:27:23 -070010#include "SkData.h"
11#include "SkGradientShader.h"
scroggo565901d2015-12-10 10:44:13 -080012#include "SkTemplates.h"
sugoice686272014-10-09 05:27:23 -070013
14namespace skiagm {
15
reed2ad1aa62016-03-09 09:50:50 -080016static sk_sp<SkShader> MakeLinear() {
mtkleindbfd7ab2016-09-01 11:24:54 -070017 constexpr SkPoint pts[2] = {
sugoice686272014-10-09 05:27:23 -070018 { 0, 0 },
19 { SkIntToScalar(80), SkIntToScalar(80) }
20 };
mtkleindbfd7ab2016-09-01 11:24:54 -070021 constexpr SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
reed2ad1aa62016-03-09 09:50:50 -080022 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0,
23 &SkMatrix::I());
sugoice686272014-10-09 05:27:23 -070024}
25
26class ColorCubeGM : public GM {
27public:
reedd053ce92016-03-22 10:17:23 -070028 ColorCubeGM() : fInitialized(false) {
sugoice686272014-10-09 05:27:23 -070029 this->setBGColor(0xFF000000);
30 }
31
sugoice686272014-10-09 05:27:23 -070032protected:
reedd053ce92016-03-22 10:17:23 -070033 SkString onShortName() override {
sugoice686272014-10-09 05:27:23 -070034 return SkString("colorcube");
35 }
36
37 void make_3Dluts() {
38 make_3Dlut(&f3DLut4, 4, true, false, false);
39 make_3Dlut(&f3DLut8, 8, false, true, false);
40 make_3Dlut(&f3DLut16, 16, false, true, true);
41 make_3Dlut(&f3DLut32, 32, true, true, false);
42 make_3Dlut(&f3DLut64, 64, true, false, true);
43 }
44
45 void make_bitmap() {
46 fBitmap.allocN32Pixels(80, 80);
47 SkCanvas canvas(fBitmap);
48 canvas.clear(0x00000000);
49 SkPaint paint;
50 paint.setAntiAlias(true);
reed2ad1aa62016-03-09 09:50:50 -080051 paint.setShader(MakeLinear());
52 canvas.drawRect(SkRect::MakeWH(80, 80), paint);
sugoice686272014-10-09 05:27:23 -070053 }
54
reedd053ce92016-03-22 10:17:23 -070055 void make_3Dlut(sk_sp<SkData>* data, int size, bool invR, bool invG, bool invB) {
56 *data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
sugoice686272014-10-09 05:27:23 -070057 SkColor* pixels = (SkColor*)((*data)->writable_data());
scroggo565901d2015-12-10 10:44:13 -080058 SkAutoTMalloc<uint8_t> lutMemory(size);
59 SkAutoTMalloc<uint8_t> invLutMemory(size);
60 uint8_t* lut = lutMemory.get();
61 uint8_t* invLut = invLutMemory.get();
sugoice686272014-10-09 05:27:23 -070062 const int maxIndex = size - 1;
63 for (int i = 0; i < size; i++) {
64 lut[i] = (i * 255) / maxIndex;
65 invLut[i] = ((maxIndex - i) * 255) / maxIndex;
66 }
67 for (int r = 0; r < size; ++r) {
68 for (int g = 0; g < size; ++g) {
69 for (int b = 0; b < size; ++b) {
caryclark12596012015-07-29 05:27:47 -070070 pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color_to_565(
71 SkColorSetARGB(0xFF,
sugoice686272014-10-09 05:27:23 -070072 invR ? invLut[r] : lut[r],
73 invG ? invLut[g] : lut[g],
caryclark12596012015-07-29 05:27:47 -070074 invB ? invLut[b] : lut[b]));
sugoice686272014-10-09 05:27:23 -070075 }
76 }
77 }
78 }
79
reedd053ce92016-03-22 10:17:23 -070080 SkISize onISize() override {
sugoice686272014-10-09 05:27:23 -070081 return SkISize::Make(500, 100);
82 }
83
reedd053ce92016-03-22 10:17:23 -070084 void onDraw(SkCanvas* canvas) override {
sugoice686272014-10-09 05:27:23 -070085 if (!fInitialized) {
86 this->make_bitmap();
87 this->make_3Dluts();
88 fInitialized = true;
89 }
90 canvas->clear(0x00000000);
91 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -070092 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut4, 4));
sugoice686272014-10-09 05:27:23 -070093 canvas->drawBitmap(fBitmap, 10, 10, &paint);
94
reedd053ce92016-03-22 10:17:23 -070095 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut8, 8));
sugoice686272014-10-09 05:27:23 -070096 canvas->drawBitmap(fBitmap, 110, 10, &paint);
97
reedd053ce92016-03-22 10:17:23 -070098 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut16, 16));
sugoice686272014-10-09 05:27:23 -070099 canvas->drawBitmap(fBitmap, 210, 10, &paint);
100
reedd053ce92016-03-22 10:17:23 -0700101 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut32, 32));
sugoice686272014-10-09 05:27:23 -0700102 canvas->drawBitmap(fBitmap, 310, 10, &paint);
103
reedd053ce92016-03-22 10:17:23 -0700104 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut64, 64));
sugoice686272014-10-09 05:27:23 -0700105 canvas->drawBitmap(fBitmap, 410, 10, &paint);
106 }
107
108private:
109 typedef GM INHERITED;
110 bool fInitialized;
111 SkBitmap fBitmap;
reedd053ce92016-03-22 10:17:23 -0700112 sk_sp<SkData> f3DLut4;
113 sk_sp<SkData> f3DLut8;
114 sk_sp<SkData> f3DLut16;
115 sk_sp<SkData> f3DLut32;
116 sk_sp<SkData> f3DLut64;
sugoice686272014-10-09 05:27:23 -0700117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121static GM* MyFactory(void*) { return new ColorCubeGM; }
122static GMRegistry reg(MyFactory);
123
124}