Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1163)

Side by Side Diff: gm/colorcube.cpp

Issue 1827433002: Reland of [2] of "switch colorfilters to sk_sp (patchset #11 id:200001 of https://codereview.chromium.o… (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gm/color4f.cpp ('k') | gm/coloremoji.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkColorCubeFilter.h" 9 #include "SkColorCubeFilter.h"
10 #include "SkData.h" 10 #include "SkData.h"
11 #include "SkGradientShader.h" 11 #include "SkGradientShader.h"
12 #include "SkTemplates.h" 12 #include "SkTemplates.h"
13 13
14 namespace skiagm { 14 namespace skiagm {
15 15
16 static sk_sp<SkShader> MakeLinear() { 16 static sk_sp<SkShader> MakeLinear() {
17 static const SkPoint pts[2] = { 17 static const SkPoint pts[2] = {
18 { 0, 0 }, 18 { 0, 0 },
19 { SkIntToScalar(80), SkIntToScalar(80) } 19 { SkIntToScalar(80), SkIntToScalar(80) }
20 }; 20 };
21 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE }; 21 static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
22 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepe at_TileMode, 0, 22 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepe at_TileMode, 0,
23 &SkMatrix::I()); 23 &SkMatrix::I());
24 } 24 }
25 25
26 class ColorCubeGM : public GM { 26 class ColorCubeGM : public GM {
27 public: 27 public:
28 ColorCubeGM() 28 ColorCubeGM() : fInitialized(false) {
29 : fInitialized(false)
30 , f3DLut4(nullptr)
31 , f3DLut8(nullptr)
32 , f3DLut16(nullptr)
33 , f3DLut32(nullptr)
34 , f3DLut64(nullptr)
35 {
36 this->setBGColor(0xFF000000); 29 this->setBGColor(0xFF000000);
37 } 30 }
38 31
39 ~ColorCubeGM() {
40 SkSafeUnref(f3DLut4);
41 SkSafeUnref(f3DLut8);
42 SkSafeUnref(f3DLut16);
43 SkSafeUnref(f3DLut32);
44 SkSafeUnref(f3DLut64);
45 }
46
47 protected: 32 protected:
48 virtual SkString onShortName() { 33 SkString onShortName() override {
49 return SkString("colorcube"); 34 return SkString("colorcube");
50 } 35 }
51 36
52 void make_3Dluts() { 37 void make_3Dluts() {
53 make_3Dlut(&f3DLut4, 4, true, false, false); 38 make_3Dlut(&f3DLut4, 4, true, false, false);
54 make_3Dlut(&f3DLut8, 8, false, true, false); 39 make_3Dlut(&f3DLut8, 8, false, true, false);
55 make_3Dlut(&f3DLut16, 16, false, true, true); 40 make_3Dlut(&f3DLut16, 16, false, true, true);
56 make_3Dlut(&f3DLut32, 32, true, true, false); 41 make_3Dlut(&f3DLut32, 32, true, true, false);
57 make_3Dlut(&f3DLut64, 64, true, false, true); 42 make_3Dlut(&f3DLut64, 64, true, false, true);
58 } 43 }
59 44
60 void make_bitmap() { 45 void make_bitmap() {
61 fBitmap.allocN32Pixels(80, 80); 46 fBitmap.allocN32Pixels(80, 80);
62 SkCanvas canvas(fBitmap); 47 SkCanvas canvas(fBitmap);
63 canvas.clear(0x00000000); 48 canvas.clear(0x00000000);
64 SkPaint paint; 49 SkPaint paint;
65 paint.setAntiAlias(true); 50 paint.setAntiAlias(true);
66 paint.setShader(MakeLinear()); 51 paint.setShader(MakeLinear());
67 canvas.drawRect(SkRect::MakeWH(80, 80), paint); 52 canvas.drawRect(SkRect::MakeWH(80, 80), paint);
68 } 53 }
69 54
70 void make_3Dlut(SkData** data, int size, bool invR, bool invG, bool invB) { 55 void make_3Dlut(sk_sp<SkData>* data, int size, bool invR, bool invG, bool in vB) {
71 *data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size); 56 *data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
72 SkColor* pixels = (SkColor*)((*data)->writable_data()); 57 SkColor* pixels = (SkColor*)((*data)->writable_data());
73 SkAutoTMalloc<uint8_t> lutMemory(size); 58 SkAutoTMalloc<uint8_t> lutMemory(size);
74 SkAutoTMalloc<uint8_t> invLutMemory(size); 59 SkAutoTMalloc<uint8_t> invLutMemory(size);
75 uint8_t* lut = lutMemory.get(); 60 uint8_t* lut = lutMemory.get();
76 uint8_t* invLut = invLutMemory.get(); 61 uint8_t* invLut = invLutMemory.get();
77 const int maxIndex = size - 1; 62 const int maxIndex = size - 1;
78 for (int i = 0; i < size; i++) { 63 for (int i = 0; i < size; i++) {
79 lut[i] = (i * 255) / maxIndex; 64 lut[i] = (i * 255) / maxIndex;
80 invLut[i] = ((maxIndex - i) * 255) / maxIndex; 65 invLut[i] = ((maxIndex - i) * 255) / maxIndex;
81 } 66 }
82 for (int r = 0; r < size; ++r) { 67 for (int r = 0; r < size; ++r) {
83 for (int g = 0; g < size; ++g) { 68 for (int g = 0; g < size; ++g) {
84 for (int b = 0; b < size; ++b) { 69 for (int b = 0; b < size; ++b) {
85 pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color _to_565( 70 pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color _to_565(
86 SkColorSetARGB(0xFF, 71 SkColorSetARGB(0xFF,
87 invR ? invLut[r] : lut[r], 72 invR ? invLut[r] : lut[r],
88 invG ? invLut[g] : lut[g], 73 invG ? invLut[g] : lut[g],
89 invB ? invLut[b] : lut[b])); 74 invB ? invLut[b] : lut[b]));
90 } 75 }
91 } 76 }
92 } 77 }
93 } 78 }
94 79
95 virtual SkISize onISize() { 80 SkISize onISize() override {
96 return SkISize::Make(500, 100); 81 return SkISize::Make(500, 100);
97 } 82 }
98 83
99 virtual void onDraw(SkCanvas* canvas) { 84 void onDraw(SkCanvas* canvas) override {
100 if (!fInitialized) { 85 if (!fInitialized) {
101 this->make_bitmap(); 86 this->make_bitmap();
102 this->make_3Dluts(); 87 this->make_3Dluts();
103 fInitialized = true; 88 fInitialized = true;
104 } 89 }
105 canvas->clear(0x00000000); 90 canvas->clear(0x00000000);
106 SkPaint paint; 91 SkPaint paint;
107 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut4, 4))->unref(); 92 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut4, 4));
108 canvas->drawBitmap(fBitmap, 10, 10, &paint); 93 canvas->drawBitmap(fBitmap, 10, 10, &paint);
109 94
110 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut8, 8))->unref(); 95 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut8, 8));
111 canvas->drawBitmap(fBitmap, 110, 10, &paint); 96 canvas->drawBitmap(fBitmap, 110, 10, &paint);
112 97
113 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut16, 16))->unref(); 98 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut16, 16));
114 canvas->drawBitmap(fBitmap, 210, 10, &paint); 99 canvas->drawBitmap(fBitmap, 210, 10, &paint);
115 100
116 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut32, 32))->unref(); 101 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut32, 32));
117 canvas->drawBitmap(fBitmap, 310, 10, &paint); 102 canvas->drawBitmap(fBitmap, 310, 10, &paint);
118 103
119 paint.setColorFilter(SkColorCubeFilter::Create(f3DLut64, 64))->unref(); 104 paint.setColorFilter(SkColorCubeFilter::Make(f3DLut64, 64));
120 canvas->drawBitmap(fBitmap, 410, 10, &paint); 105 canvas->drawBitmap(fBitmap, 410, 10, &paint);
121 } 106 }
122 107
123 private: 108 private:
124 typedef GM INHERITED; 109 typedef GM INHERITED;
125 bool fInitialized; 110 bool fInitialized;
126 SkBitmap fBitmap; 111 SkBitmap fBitmap;
127 SkData* f3DLut4; 112 sk_sp<SkData> f3DLut4;
128 SkData* f3DLut8; 113 sk_sp<SkData> f3DLut8;
129 SkData* f3DLut16; 114 sk_sp<SkData> f3DLut16;
130 SkData* f3DLut32; 115 sk_sp<SkData> f3DLut32;
131 SkData* f3DLut64; 116 sk_sp<SkData> f3DLut64;
132 }; 117 };
133 118
134 ////////////////////////////////////////////////////////////////////////////// 119 //////////////////////////////////////////////////////////////////////////////
135 120
136 static GM* MyFactory(void*) { return new ColorCubeGM; } 121 static GM* MyFactory(void*) { return new ColorCubeGM; }
137 static GMRegistry reg(MyFactory); 122 static GMRegistry reg(MyFactory);
138 123
139 } 124 }
OLDNEW
« no previous file with comments | « gm/color4f.cpp ('k') | gm/coloremoji.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698