blob: fa9eea9f99098d6b5020070fd233367288ec1ca5 [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
16static SkShader* MakeLinear() {
17 static const SkPoint pts[2] = {
18 { 0, 0 },
19 { SkIntToScalar(80), SkIntToScalar(80) }
20 };
21 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
22 return SkGradientShader::CreateLinear(
halcanary96fcdcc2015-08-27 07:41:13 -070023 pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
sugoice686272014-10-09 05:27:23 -070024}
25
26class ColorCubeGM : public GM {
27public:
28 ColorCubeGM()
29 : fInitialized(false)
halcanary96fcdcc2015-08-27 07:41:13 -070030 , f3DLut4(nullptr)
31 , f3DLut8(nullptr)
32 , f3DLut16(nullptr)
33 , f3DLut32(nullptr)
34 , f3DLut64(nullptr)
sugoice686272014-10-09 05:27:23 -070035 {
36 this->setBGColor(0xFF000000);
37 }
38
39 ~ColorCubeGM() {
40 SkSafeUnref(f3DLut4);
41 SkSafeUnref(f3DLut8);
42 SkSafeUnref(f3DLut16);
43 SkSafeUnref(f3DLut32);
44 SkSafeUnref(f3DLut64);
45 }
46
47protected:
48 virtual SkString onShortName() {
49 return SkString("colorcube");
50 }
51
52 void make_3Dluts() {
53 make_3Dlut(&f3DLut4, 4, true, false, false);
54 make_3Dlut(&f3DLut8, 8, false, true, false);
55 make_3Dlut(&f3DLut16, 16, false, true, true);
56 make_3Dlut(&f3DLut32, 32, true, true, false);
57 make_3Dlut(&f3DLut64, 64, true, false, true);
58 }
59
60 void make_bitmap() {
61 fBitmap.allocN32Pixels(80, 80);
62 SkCanvas canvas(fBitmap);
63 canvas.clear(0x00000000);
64 SkPaint paint;
65 paint.setAntiAlias(true);
66 SkShader* shader = MakeLinear();
67 paint.setShader(shader);
68 SkRect r = { 0, 0, SkIntToScalar(80), SkIntToScalar(80) };
69 canvas.drawRect(r, paint);
70 shader->unref();
71 }
72
73 void make_3Dlut(SkData** data, int size, bool invR, bool invG, bool invB) {
74 *data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size);
75 SkColor* pixels = (SkColor*)((*data)->writable_data());
scroggo565901d2015-12-10 10:44:13 -080076 SkAutoTMalloc<uint8_t> lutMemory(size);
77 SkAutoTMalloc<uint8_t> invLutMemory(size);
78 uint8_t* lut = lutMemory.get();
79 uint8_t* invLut = invLutMemory.get();
sugoice686272014-10-09 05:27:23 -070080 const int maxIndex = size - 1;
81 for (int i = 0; i < size; i++) {
82 lut[i] = (i * 255) / maxIndex;
83 invLut[i] = ((maxIndex - i) * 255) / maxIndex;
84 }
85 for (int r = 0; r < size; ++r) {
86 for (int g = 0; g < size; ++g) {
87 for (int b = 0; b < size; ++b) {
caryclark12596012015-07-29 05:27:47 -070088 pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color_to_565(
89 SkColorSetARGB(0xFF,
sugoice686272014-10-09 05:27:23 -070090 invR ? invLut[r] : lut[r],
91 invG ? invLut[g] : lut[g],
caryclark12596012015-07-29 05:27:47 -070092 invB ? invLut[b] : lut[b]));
sugoice686272014-10-09 05:27:23 -070093 }
94 }
95 }
96 }
97
98 virtual SkISize onISize() {
99 return SkISize::Make(500, 100);
100 }
101
102 virtual void onDraw(SkCanvas* canvas) {
103 if (!fInitialized) {
104 this->make_bitmap();
105 this->make_3Dluts();
106 fInitialized = true;
107 }
108 canvas->clear(0x00000000);
109 SkPaint paint;
110 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut4, 4))->unref();
111 canvas->drawBitmap(fBitmap, 10, 10, &paint);
112
113 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut8, 8))->unref();
114 canvas->drawBitmap(fBitmap, 110, 10, &paint);
115
116 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut16, 16))->unref();
117 canvas->drawBitmap(fBitmap, 210, 10, &paint);
118
119 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut32, 32))->unref();
120 canvas->drawBitmap(fBitmap, 310, 10, &paint);
121
122 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut64, 64))->unref();
123 canvas->drawBitmap(fBitmap, 410, 10, &paint);
124 }
125
126private:
127 typedef GM INHERITED;
128 bool fInitialized;
129 SkBitmap fBitmap;
130 SkData* f3DLut4;
131 SkData* f3DLut8;
132 SkData* f3DLut16;
133 SkData* f3DLut32;
134 SkData* f3DLut64;
135};
136
137//////////////////////////////////////////////////////////////////////////////
138
139static GM* MyFactory(void*) { return new ColorCubeGM; }
140static GMRegistry reg(MyFactory);
141
142}