OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 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 "SkColorPriv.h" | |
9 #include "SkOverdrawMode.h" | |
10 #include "SkString.h" | |
11 #include "SkXfermode.h" | |
12 | |
13 #if SK_SUPPORT_GPU | |
14 #include "GrFragmentProcessor.h" | |
15 #include "GrInvariantOutput.h" | |
16 #include "GrXferProcessor.h" | |
17 #include "glsl/GrGLSLFragmentProcessor.h" | |
18 #include "glsl/GrGLSLFragmentShaderBuilder.h" | |
19 #include "glsl/GrGLSLXferProcessor.h" | |
20 | |
21 /////////////////////////////////////////////////////////////////////////////// | |
22 // Fragment Processor | |
23 /////////////////////////////////////////////////////////////////////////////// | |
24 | |
25 class GLOverdrawFP; | |
26 | |
27 class GrOverdrawFP : public GrFragmentProcessor { | |
28 public: | |
29 static const GrFragmentProcessor* Create(const GrFragmentProcessor* dst) { | |
30 return new GrOverdrawFP(dst); | |
31 } | |
32 | |
33 ~GrOverdrawFP() override { } | |
34 | |
35 const char* name() const override { return "Overdraw"; } | |
36 | |
37 SkString dumpInfo() const override { | |
38 SkString str; | |
39 return str; | |
40 } | |
41 | |
42 private: | |
43 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; | |
44 | |
45 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder* b) cons
t override; | |
46 | |
47 bool onIsEqual(const GrFragmentProcessor&) const override { return true; } | |
48 | |
49 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
50 inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); | |
51 } | |
52 | |
53 GrOverdrawFP(const GrFragmentProcessor* dst) { | |
54 this->initClassID<GrOverdrawFP>(); | |
55 | |
56 SkASSERT(dst); | |
57 SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst); | |
58 SkASSERT(0 == dstIndex); | |
59 } | |
60 | |
61 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | |
62 typedef GrFragmentProcessor INHERITED; | |
63 }; | |
64 | |
65 /////////////////////////////////////////////////////////////////////////////// | |
66 | |
67 static void add_overdraw_code(GrGLSLFragmentBuilder* fragBuilder, | |
68 const char* dstColor, | |
69 const char* outputColor) { | |
70 | |
71 fragBuilder->codeAppend("vec4 gTable[11] = vec4[11]("); | |
72 fragBuilder->codeAppend("vec4(0, 0, 0, 0.0),"); | |
73 fragBuilder->codeAppend("vec4(0.5, 0.617, 1.0, 1.0),"); | |
74 fragBuilder->codeAppend("vec4(0.664, 0.723, 0.83, 1.0),"); | |
75 fragBuilder->codeAppend("vec4(0.832, 0.762, 0.664, 1.0),"); | |
76 fragBuilder->codeAppend("vec4(1, 0.75, 0.496, 1.0),"); | |
77 fragBuilder->codeAppend("vec4(1, 0.723, 0.332, 1.0),"); | |
78 fragBuilder->codeAppend("vec4(1, 0.645, 0.164, 1.0),"); | |
79 fragBuilder->codeAppend("vec4(1, 0.527, 0, 1.0),"); | |
80 fragBuilder->codeAppend("vec4(1, 0.371, 0, 1.0),"); | |
81 fragBuilder->codeAppend("vec4(1, 0.195, 0, 1.0),"); | |
82 fragBuilder->codeAppend("vec4(1, 0, 0, 1.0)"); | |
83 fragBuilder->codeAppend(");"); | |
84 | |
85 fragBuilder->codeAppend("int nextIdx;"); | |
86 fragBuilder->codeAppendf("vec4 dst = %s;", dstColor); | |
87 fragBuilder->codeAppend("if (dst.r < 0.25) { nextIdx = 1; }"); | |
88 // cap 'idx' at 10 | |
89 fragBuilder->codeAppend("else if (dst.g < 0.0977) { nextIdx = 10; }"); | |
90 fragBuilder->codeAppend("else if (dst.b > 0.08) { nextIdx = 8 - int(6 * dst.
b + 0.5); }"); | |
91 fragBuilder->codeAppend("else { nextIdx = 11 - int(5.7 * dst.g + 0.5); }"); | |
92 fragBuilder->codeAppendf("%s = gTable[nextIdx];", outputColor); | |
93 } | |
94 | |
95 class GLOverdrawFP : public GrGLSLFragmentProcessor { | |
96 public: | |
97 GLOverdrawFP(const GrOverdrawFP&) {} | |
98 | |
99 ~GLOverdrawFP() override {} | |
100 | |
101 void emitCode(EmitArgs& args) override { | |
102 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
103 SkString dstColor("dstColor"); | |
104 this->emitChild(0, nullptr, &dstColor, args); | |
105 | |
106 add_overdraw_code(fragBuilder, dstColor.c_str(), args.fOutputColor); | |
107 } | |
108 | |
109 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) { } | |
110 | |
111 private: | |
112 typedef GrGLSLFragmentProcessor INHERITED; | |
113 }; | |
114 | |
115 /////////////////////////////////////////////////////////////////////////////// | |
116 | |
117 GrGLSLFragmentProcessor* GrOverdrawFP::onCreateGLSLInstance() const { | |
118 return new GLOverdrawFP(*this); | |
119 } | |
120 | |
121 void GrOverdrawFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyB
uilder* b) const { | |
122 GLOverdrawFP::GenKey(*this, caps, b); | |
123 } | |
124 | |
125 const GrFragmentProcessor* GrOverdrawFP::TestCreate(GrProcessorTestData* d) { | |
126 SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChild
FP(d)); | |
127 return new GrOverdrawFP(dst); | |
128 } | |
129 | |
130 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrOverdrawFP); | |
131 | |
132 /////////////////////////////////////////////////////////////////////////////// | |
133 // Xfer Processor | |
134 /////////////////////////////////////////////////////////////////////////////// | |
135 | |
136 class OverdrawXP : public GrXferProcessor { | |
137 public: | |
138 OverdrawXP(const DstTexture* dstTexture, bool hasMixedSamples) | |
139 : INHERITED(dstTexture, true, hasMixedSamples) { | |
140 this->initClassID<OverdrawXP>(); | |
141 } | |
142 | |
143 const char* name() const override { return "Overdraw"; } | |
144 | |
145 GrGLSLXferProcessor* createGLSLInstance() const override; | |
146 | |
147 private: | |
148 GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations&
optimizations, | |
149 bool doesStencilWrite, | |
150 GrColor* overrideColor, | |
151 const GrCaps& caps) const overr
ide { | |
152 // We never look at the color input | |
153 return GrXferProcessor::kIgnoreColor_OptFlag; | |
154 } | |
155 | |
156 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override; | |
157 | |
158 bool onIsEqual(const GrXferProcessor&) const override { return true; } | |
159 | |
160 typedef GrXferProcessor INHERITED; | |
161 }; | |
162 | |
163 /////////////////////////////////////////////////////////////////////////////// | |
164 | |
165 class GLOverdrawXP : public GrGLSLXferProcessor { | |
166 public: | |
167 GLOverdrawXP(const OverdrawXP&) { } | |
168 | |
169 ~GLOverdrawXP() override {} | |
170 | |
171 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuil
der*) { } | |
172 | |
173 private: | |
174 void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder, | |
175 GrGLSLUniformHandler* uniformHandler, | |
176 const char* srcColor, | |
177 const char* srcCoverage, | |
178 const char* dstColor, | |
179 const char* outColor, | |
180 const char* outColorSecondary, | |
181 const GrXferProcessor& proc) override { | |
182 add_overdraw_code(fragBuilder, dstColor, outColor); | |
183 | |
184 // Apply coverage. | |
185 INHERITED::DefaultCoverageModulation(fragBuilder, srcCoverage, dstColor,
outColor, | |
186 outColorSecondary, proc); | |
187 } | |
188 | |
189 void onSetData(const GrGLSLProgramDataManager&, const GrXferProcessor&) over
ride { }; | |
190 | |
191 typedef GrGLSLXferProcessor INHERITED; | |
192 }; | |
193 | |
194 /////////////////////////////////////////////////////////////////////////////// | |
195 | |
196 void OverdrawXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBui
lder* b) const { | |
197 GLOverdrawXP::GenKey(*this, caps, b); | |
198 } | |
199 | |
200 GrGLSLXferProcessor* OverdrawXP::createGLSLInstance() const { return new GLOverd
rawXP(*this); } | |
201 | |
202 /////////////////////////////////////////////////////////////////////////////// | |
203 class GrOverdrawXPFactory : public GrXPFactory { | |
204 public: | |
205 static GrXPFactory* Create() { return new GrOverdrawXPFactory(); } | |
206 | |
207 void getInvariantBlendedColor(const GrProcOptInfo& colorPOI, | |
208 GrXPFactory::InvariantBlendedColor* blendedCol
or) const override { | |
209 blendedColor->fWillBlendWithDst = true; | |
210 blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags; | |
211 } | |
212 | |
213 private: | |
214 GrOverdrawXPFactory() { | |
215 this->initClassID<GrOverdrawXPFactory>(); | |
216 } | |
217 | |
218 GrXferProcessor* onCreateXferProcessor(const GrCaps& caps, | |
219 const GrPipelineOptimizations& optimi
zations, | |
220 bool hasMixedSamples, | |
221 const DstTexture* dstTexture) const o
verride { | |
222 return new OverdrawXP(dstTexture, hasMixedSamples); | |
223 } | |
224 | |
225 bool willReadDstColor(const GrCaps& caps, | |
226 const GrPipelineOptimizations& optimizations, | |
227 bool hasMixedSamples) const override { | |
228 return true; | |
229 } | |
230 | |
231 bool onIsEqual(const GrXPFactory& xpfBase) const override { return true; } | |
232 | |
233 GR_DECLARE_XP_FACTORY_TEST; | |
234 | |
235 typedef GrXPFactory INHERITED; | |
236 }; | |
237 | |
238 GR_DEFINE_XP_FACTORY_TEST(GrOverdrawXPFactory); | |
239 | |
240 const GrXPFactory* GrOverdrawXPFactory::TestCreate(GrProcessorTestData* d) { | |
241 return GrOverdrawXPFactory::Create(); | |
242 } | |
243 #endif | |
244 | |
245 /////////////////////////////////////////////////////////////////////////////// | |
246 class SkOverdrawXfermode : public SkXfermode { | |
247 public: | |
248 static SkXfermode* Create() { | |
249 return new SkOverdrawXfermode; | |
250 } | |
251 | |
252 SkPMColor xferColor(SkPMColor src, SkPMColor dst) const override { | |
253 // This table encodes the color progression of the overdraw visualizatio
n | |
254 static const SkPMColor gTable[] = { | |
255 SkPackARGB32(0x00, 0x00, 0x00, 0x00), | |
256 SkPackARGB32(0xFF, 128, 158, 255), | |
257 SkPackARGB32(0xFF, 170, 185, 212), | |
258 SkPackARGB32(0xFF, 213, 195, 170), | |
259 SkPackARGB32(0xFF, 255, 192, 127), | |
260 SkPackARGB32(0xFF, 255, 185, 85), | |
261 SkPackARGB32(0xFF, 255, 165, 42), | |
262 SkPackARGB32(0xFF, 255, 135, 0), | |
263 SkPackARGB32(0xFF, 255, 95, 0), | |
264 SkPackARGB32(0xFF, 255, 50, 0), | |
265 SkPackARGB32(0xFF, 255, 0, 0) | |
266 }; | |
267 | |
268 | |
269 int nextIdx; | |
270 if (SkColorGetR(dst) < 64) { // dst color is the 0th color so the next c
olor is 1 | |
271 nextIdx = 1; | |
272 } else if (SkColorGetG(dst) < 25) { // dst color is the 10th color so ca
p there | |
273 nextIdx = 10; | |
274 } else if ((SkColorGetB(dst)+21)/42 > 0) { // dst color is one of 1-6 | |
275 nextIdx = 8 - (SkColorGetB(dst)+21)/42; | |
276 } else { // dst color is between 7 and 9 | |
277 nextIdx = 11 - (SkColorGetG(dst)+22)/45; | |
278 } | |
279 SkASSERT(nextIdx < (int)SK_ARRAY_COUNT(gTable)); | |
280 | |
281 return gTable[nextIdx]; | |
282 } | |
283 | |
284 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkOverdrawXfermode) | |
285 | |
286 #if SK_SUPPORT_GPU | |
287 bool asFragmentProcessor(const GrFragmentProcessor** output, | |
288 const GrFragmentProcessor* dst) const override { | |
289 if (output) { | |
290 *output = GrOverdrawFP::Create(dst); | |
291 } | |
292 return true; | |
293 } | |
294 | |
295 bool asXPFactory(GrXPFactory** xpf) const override { | |
296 if (xpf) { | |
297 *xpf = GrOverdrawXPFactory::Create(); | |
298 } | |
299 return true; | |
300 } | |
301 #endif | |
302 | |
303 #ifndef SK_IGNORE_TO_STRING | |
304 void toString(SkString* str) const override { str->set("SkOverdrawXfermode")
; } | |
305 #endif | |
306 | |
307 private: | |
308 friend class SkOverdrawMode; | |
309 | |
310 void flatten(SkWriteBuffer& buffer) const override { } | |
311 | |
312 typedef SkXfermode INHERITED; | |
313 }; | |
314 | |
315 SkFlattenable* SkOverdrawXfermode::CreateProc(SkReadBuffer& buffer) { | |
316 return Create(); | |
317 } | |
318 | |
319 SkXfermode* SkOverdrawMode::Create() { return new SkOverdrawXfermode; } | |
320 | |
321 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkOverdrawMode) | |
322 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkOverdrawXfermode) | |
323 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
OLD | NEW |