OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "SkNormalMapSource.h" |
| 9 |
8 #include "SkError.h" | 10 #include "SkError.h" |
9 #include "SkErrorInternals.h" | 11 #include "SkErrorInternals.h" |
10 #include "SkLightingShader.h" | 12 #include "SkLightingShader.h" |
11 #include "SkMatrix.h" | 13 #include "SkMatrix.h" |
12 #include "SkNormalSource.h" | 14 #include "SkNormalSource.h" |
13 #include "SkPM4f.h" | 15 #include "SkPM4f.h" |
14 #include "SkReadBuffer.h" | 16 #include "SkReadBuffer.h" |
15 #include "SkWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
16 | 18 |
17 // Genretating vtable | |
18 SkNormalSource::~SkNormalSource() {} | |
19 | |
20 /////////////////////////////////////////////////////////////////////////////// | |
21 | |
22 class NormalMapSourceImpl : public SkNormalSource { | |
23 public: | |
24 NormalMapSourceImpl(sk_sp<SkShader> mapShader, const SkMatrix& invCTM) | |
25 : fMapShader(std::move(mapShader)) | |
26 , fInvCTM(invCTM) {} | |
27 | |
28 #if SK_SUPPORT_GPU | 19 #if SK_SUPPORT_GPU |
29 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | |
30 const SkMatrix& viewM, | |
31 const SkMatrix* localMatrix, | |
32 SkFilterQuality, | |
33 SkSourceGammaTreatment) const
override; | |
34 #endif | |
35 | |
36 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec, | |
37 void* storage) const ov
erride; | |
38 | |
39 size_t providerSize(const SkShader::ContextRec& rec) const override; | |
40 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) | |
41 | |
42 protected: | |
43 void flatten(SkWriteBuffer& buf) const override; | |
44 | |
45 bool computeNormTotalInverse(const SkShader::ContextRec& rec, SkMatrix* norm
TotalInverse) const; | |
46 | |
47 private: | |
48 class Provider : public SkNormalSource::Provider { | |
49 public: | |
50 Provider(const NormalMapSourceImpl& source, SkShader::Context* mapContex
t, | |
51 SkPaint* overridePaint); | |
52 | |
53 virtual ~Provider() override; | |
54 | |
55 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over
ride; | |
56 | |
57 private: | |
58 const NormalMapSourceImpl& fSource; | |
59 SkShader::Context* fMapContext; | |
60 | |
61 SkPaint* fOverridePaint; | |
62 | |
63 typedef SkNormalSource::Provider INHERITED; | |
64 }; | |
65 | |
66 sk_sp<SkShader> fMapShader; | |
67 SkMatrix fInvCTM; // Inverse of the canvas total matrix, used for rot
ating normals. | |
68 | |
69 friend class SkNormalSource; | |
70 | |
71 typedef SkNormalSource INHERITED; | |
72 }; | |
73 | |
74 //////////////////////////////////////////////////////////////////////////// | |
75 | |
76 #if SK_SUPPORT_GPU | |
77 | |
78 #include "GrCoordTransform.h" | 20 #include "GrCoordTransform.h" |
79 #include "GrInvariantOutput.h" | 21 #include "GrInvariantOutput.h" |
80 #include "GrTextureParams.h" | 22 #include "GrTextureParams.h" |
81 #include "glsl/GrGLSLFragmentProcessor.h" | 23 #include "glsl/GrGLSLFragmentProcessor.h" |
82 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 24 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
83 #include "SkGr.h" | 25 #include "SkGr.h" |
| 26 #endif |
| 27 |
| 28 #if SK_SUPPORT_GPU |
84 | 29 |
85 class NormalMapFP : public GrFragmentProcessor { | 30 class NormalMapFP : public GrFragmentProcessor { |
86 public: | 31 public: |
87 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM) | 32 NormalMapFP(sk_sp<GrFragmentProcessor> mapFP, const SkMatrix& invCTM) |
88 : fInvCTM(invCTM) { | 33 : fInvCTM(invCTM) { |
89 this->registerChildProcessor(mapFP); | 34 this->registerChildProcessor(mapFP); |
90 | 35 |
91 this->initClassID<NormalMapFP>(); | 36 this->initClassID<NormalMapFP>(); |
92 } | 37 } |
93 | 38 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalMapFP; } | 115 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalMapFP; } |
171 | 116 |
172 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 117 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
173 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); | 118 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); |
174 return fInvCTM == normalMapFP.fInvCTM; | 119 return fInvCTM == normalMapFP.fInvCTM; |
175 } | 120 } |
176 | 121 |
177 SkMatrix fInvCTM; | 122 SkMatrix fInvCTM; |
178 }; | 123 }; |
179 | 124 |
180 sk_sp<GrFragmentProcessor> NormalMapSourceImpl::asFragmentProcessor( | 125 sk_sp<GrFragmentProcessor> SkNormalMapSourceImpl::asFragmentProcessor( |
181 GrContext *context, | 126 GrContext *context, |
182 const SkMatrix &viewM, | 127 const SkMatrix &viewM, |
183 const SkMatrix *localMatrix
, | 128 const SkMatrix *localMatrix
, |
184 SkFilterQuality filterQuali
ty, | 129 SkFilterQuality filterQuali
ty, |
185 SkSourceGammaTreatment gamm
aTreatment) const { | 130 SkSourceGammaTreatment gamm
aTreatment) const { |
186 sk_sp<GrFragmentProcessor> mapFP = fMapShader->asFragmentProcessor(context,
viewM, | 131 sk_sp<GrFragmentProcessor> mapFP = fMapShader->asFragmentProcessor(context,
viewM, |
187 localMatrix, filterQuality, gammaTreatment); | 132 localMatrix, filterQuality, gammaTreatment); |
188 if (!mapFP) { | 133 if (!mapFP) { |
189 return nullptr; | 134 return nullptr; |
190 } | 135 } |
191 | 136 |
192 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); | 137 return sk_make_sp<NormalMapFP>(std::move(mapFP), fInvCTM); |
193 } | 138 } |
194 | 139 |
195 #endif // SK_SUPPORT_GPU | 140 #endif // SK_SUPPORT_GPU |
196 | 141 |
197 //////////////////////////////////////////////////////////////////////////// | 142 //////////////////////////////////////////////////////////////////////////// |
198 | 143 |
199 NormalMapSourceImpl::Provider::Provider(const NormalMapSourceImpl& source, | 144 SkNormalMapSourceImpl::Provider::Provider(const SkNormalMapSourceImpl& source, |
200 SkShader::Context* mapContext, | 145 SkShader::Context* mapContext, |
201 SkPaint* overridePaint) | 146 SkPaint* overridePaint) |
202 : fSource(source) | 147 : fSource(source) |
203 , fMapContext(mapContext) | 148 , fMapContext(mapContext) |
204 , fOverridePaint(overridePaint) {} | 149 , fOverridePaint(overridePaint) {} |
205 | 150 |
206 NormalMapSourceImpl::Provider::~Provider() { | 151 SkNormalMapSourceImpl::Provider::~Provider() { |
207 fMapContext->~Context(); | 152 fMapContext->~Context(); |
208 fOverridePaint->~SkPaint(); | 153 fOverridePaint->~SkPaint(); |
209 } | 154 } |
210 | 155 |
211 SkNormalSource::Provider* NormalMapSourceImpl::asProvider( | 156 SkNormalSource::Provider* SkNormalMapSourceImpl::asProvider( |
212 const SkShader::ContextRec &rec, void *storage) const { | 157 const SkShader::ContextRec &rec, void *storage) const { |
213 SkMatrix normTotalInv; | 158 SkMatrix normTotalInv; |
214 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | 159 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { |
215 return nullptr; | 160 return nullptr; |
216 } | 161 } |
217 | 162 |
218 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd | 163 // Overriding paint's alpha because we need the normal map's RGB channels to
be unpremul'd |
219 void* paintStorage = (char*)storage + sizeof(Provider); | 164 void* paintStorage = (char*)storage + sizeof(Provider); |
220 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); | 165 SkPaint* overridePaint = new (paintStorage) SkPaint(*(rec.fPaint)); |
221 overridePaint->setAlpha(0xFF); | 166 overridePaint->setAlpha(0xFF); |
222 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, | 167 SkShader::ContextRec overrideRec(*overridePaint, *(rec.fMatrix), rec.fLocalM
atrix, |
223 rec.fPreferredDstType); | 168 rec.fPreferredDstType); |
224 | 169 |
225 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); | 170 void* mapContextStorage = (char*) paintStorage + sizeof(SkPaint); |
226 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); | 171 SkShader::Context* context = fMapShader->createContext(overrideRec, mapConte
xtStorage); |
227 if (!context) { | 172 if (!context) { |
228 return nullptr; | 173 return nullptr; |
229 } | 174 } |
230 | 175 |
231 return new (storage) Provider(*this, context, overridePaint); | 176 return new (storage) Provider(*this, context, overridePaint); |
232 } | 177 } |
233 | 178 |
234 size_t NormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) const
{ | 179 size_t SkNormalMapSourceImpl::providerSize(const SkShader::ContextRec& rec) cons
t { |
235 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); | 180 return sizeof(Provider) + sizeof(SkPaint) + fMapShader->contextSize(rec); |
236 } | 181 } |
237 | 182 |
238 bool NormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec& re
c, | 183 bool SkNormalMapSourceImpl::computeNormTotalInverse(const SkShader::ContextRec&
rec, |
239 SkMatrix* normTotalInverse) co
nst { | 184 SkMatrix* normTotalInverse) co
nst { |
240 SkMatrix total; | 185 SkMatrix total; |
241 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); | 186 total.setConcat(*rec.fMatrix, fMapShader->getLocalMatrix()); |
242 | 187 |
243 const SkMatrix* m = &total; | 188 const SkMatrix* m = &total; |
244 if (rec.fLocalMatrix) { | 189 if (rec.fLocalMatrix) { |
245 total.setConcat(*m, *rec.fLocalMatrix); | 190 total.setConcat(*m, *rec.fLocalMatrix); |
246 m = &total; | 191 m = &total; |
247 } | 192 } |
248 return m->invert(normTotalInverse); | 193 return m->invert(normTotalInverse); |
249 } | 194 } |
250 | 195 |
251 #define BUFFER_MAX 16 | 196 #define BUFFER_MAX 16 |
252 void NormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[]
, | 197 void SkNormalMapSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output
[], |
253 int count) const { | 198 int count) const { |
254 SkPMColor tmpNormalColors[BUFFER_MAX]; | 199 SkPMColor tmpNormalColors[BUFFER_MAX]; |
255 | 200 |
256 do { | 201 do { |
257 int n = SkTMin(count, BUFFER_MAX); | 202 int n = SkTMin(count, BUFFER_MAX); |
258 | 203 |
259 fMapContext->shadeSpan(x, y, tmpNormalColors, n); | 204 fMapContext->shadeSpan(x, y, tmpNormalColors, n); |
260 | 205 |
261 for (int i = 0; i < n; i++) { | 206 for (int i = 0; i < n; i++) { |
262 SkPoint3 tempNorm; | 207 SkPoint3 tempNorm; |
(...skipping 30 matching lines...) Expand all Loading... |
293 } | 238 } |
294 | 239 |
295 output += n; | 240 output += n; |
296 x += n; | 241 x += n; |
297 count -= n; | 242 count -= n; |
298 } while (count > 0); | 243 } while (count > 0); |
299 } | 244 } |
300 | 245 |
301 //////////////////////////////////////////////////////////////////////////////// | 246 //////////////////////////////////////////////////////////////////////////////// |
302 | 247 |
303 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { | 248 sk_sp<SkFlattenable> SkNormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { |
304 | 249 |
305 sk_sp<SkShader> mapShader = buf.readFlattenable<SkShader>(); | 250 sk_sp<SkShader> mapShader = buf.readFlattenable<SkShader>(); |
306 | 251 |
307 SkMatrix invCTM; | 252 SkMatrix invCTM; |
308 buf.readMatrix(&invCTM); | 253 buf.readMatrix(&invCTM); |
309 | 254 |
310 return sk_make_sp<NormalMapSourceImpl>(std::move(mapShader), invCTM); | 255 return sk_make_sp<SkNormalMapSourceImpl>(std::move(mapShader), invCTM); |
311 } | 256 } |
312 | 257 |
313 void NormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { | 258 void SkNormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { |
314 this->INHERITED::flatten(buf); | 259 this->INHERITED::flatten(buf); |
315 | 260 |
316 buf.writeFlattenable(fMapShader.get()); | 261 buf.writeFlattenable(fMapShader.get()); |
317 buf.writeMatrix(fInvCTM); | 262 buf.writeMatrix(fInvCTM); |
318 } | 263 } |
319 | 264 |
320 //////////////////////////////////////////////////////////////////////////// | 265 //////////////////////////////////////////////////////////////////////////// |
321 | 266 |
322 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { | 267 sk_sp<SkNormalSource> SkNormalSource::MakeFromNormalMap(sk_sp<SkShader> map, con
st SkMatrix& ctm) { |
323 SkMatrix invCTM; | 268 SkMatrix invCTM; |
324 | 269 |
325 if (!ctm.invert(&invCTM) || !map) { | 270 if (!ctm.invert(&invCTM) || !map) { |
326 return nullptr; | 271 return nullptr; |
327 } | 272 } |
328 | 273 |
329 return sk_make_sp<NormalMapSourceImpl>(std::move(map), invCTM); | 274 return sk_make_sp<SkNormalMapSourceImpl>(std::move(map), invCTM); |
330 } | 275 } |
331 | |
332 /////////////////////////////////////////////////////////////////////////////// | |
333 | |
334 class SK_API NormalFlatSourceImpl : public SkNormalSource { | |
335 public: | |
336 NormalFlatSourceImpl(){} | |
337 | |
338 #if SK_SUPPORT_GPU | |
339 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | |
340 const SkMatrix& viewM, | |
341 const SkMatrix* localMatrix, | |
342 SkFilterQuality, | |
343 SkSourceGammaTreatment) const
override; | |
344 #endif | |
345 | |
346 SkNormalSource::Provider* asProvider(const SkShader::ContextRec& rec, | |
347 void* storage) const override; | |
348 size_t providerSize(const SkShader::ContextRec& rec) const override; | |
349 | |
350 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalFlatSourceImpl) | |
351 | |
352 protected: | |
353 void flatten(SkWriteBuffer& buf) const override; | |
354 | |
355 private: | |
356 class Provider : public SkNormalSource::Provider { | |
357 public: | |
358 Provider(); | |
359 | |
360 virtual ~Provider(); | |
361 | |
362 void fillScanLine(int x, int y, SkPoint3 output[], int count) const over
ride; | |
363 | |
364 private: | |
365 typedef SkNormalSource::Provider INHERITED; | |
366 }; | |
367 | |
368 friend class SkNormalSource; | |
369 | |
370 typedef SkNormalSource INHERITED; | |
371 }; | |
372 | |
373 //////////////////////////////////////////////////////////////////////////// | |
374 | |
375 #if SK_SUPPORT_GPU | |
376 | |
377 class NormalFlatFP : public GrFragmentProcessor { | |
378 public: | |
379 NormalFlatFP() { | |
380 this->initClassID<NormalFlatFP>(); | |
381 } | |
382 | |
383 class GLSLNormalFlatFP : public GrGLSLFragmentProcessor { | |
384 public: | |
385 GLSLNormalFlatFP() {} | |
386 | |
387 void emitCode(EmitArgs& args) override { | |
388 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
389 | |
390 fragBuilder->codeAppendf("%s = vec4(0, 0, 1, 0);", args.fOutputColor
); | |
391 } | |
392 | |
393 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
394 GrProcessorKeyBuilder* b) { | |
395 b->add32(0x0); | |
396 } | |
397 | |
398 protected: | |
399 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override {} | |
400 }; | |
401 | |
402 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | |
403 GLSLNormalFlatFP::GenKey(*this, caps, b); | |
404 } | |
405 | |
406 const char* name() const override { return "NormalFlatFP"; } | |
407 | |
408 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
409 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); | |
410 } | |
411 | |
412 private: | |
413 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalFlatFP; } | |
414 | |
415 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
416 return true; | |
417 } | |
418 }; | |
419 | |
420 sk_sp<GrFragmentProcessor> NormalFlatSourceImpl::asFragmentProcessor( | |
421 GrContext *context, | |
422 const SkMatrix &viewM, | |
423 const SkMatrix *localMatrix
, | |
424 SkFilterQuality filterQuali
ty, | |
425 SkSourceGammaTreatment gamm
aTreatment) const { | |
426 | |
427 return sk_make_sp<NormalFlatFP>(); | |
428 } | |
429 | |
430 #endif // SK_SUPPORT_GPU | |
431 | |
432 //////////////////////////////////////////////////////////////////////////// | |
433 | |
434 NormalFlatSourceImpl::Provider::Provider() {} | |
435 | |
436 NormalFlatSourceImpl::Provider::~Provider() {} | |
437 | |
438 SkNormalSource::Provider* NormalFlatSourceImpl::asProvider(const SkShader::Conte
xtRec &rec, | |
439 void *storage) const
{ | |
440 return new (storage) Provider(); | |
441 } | |
442 | |
443 size_t NormalFlatSourceImpl::providerSize(const SkShader::ContextRec&) const { | |
444 return sizeof(Provider); | |
445 } | |
446 | |
447 void NormalFlatSourceImpl::Provider::fillScanLine(int x, int y, SkPoint3 output[
], | |
448 int count) const { | |
449 for (int i = 0; i < count; i++) { | |
450 output[i] = {0.0f, 0.0f, 1.0f}; | |
451 } | |
452 } | |
453 | |
454 //////////////////////////////////////////////////////////////////////////////// | |
455 | |
456 sk_sp<SkFlattenable> NormalFlatSourceImpl::CreateProc(SkReadBuffer& buf) { | |
457 return sk_make_sp<NormalFlatSourceImpl>(); | |
458 } | |
459 | |
460 void NormalFlatSourceImpl::flatten(SkWriteBuffer& buf) const { | |
461 this->INHERITED::flatten(buf); | |
462 } | |
463 | |
464 //////////////////////////////////////////////////////////////////////////// | |
465 | |
466 sk_sp<SkNormalSource> SkNormalSource::MakeFlat() { | |
467 return sk_make_sp<NormalFlatSourceImpl>(); | |
468 } | |
469 | |
470 //////////////////////////////////////////////////////////////////////////// | |
471 | |
472 //////////////////////////////////////////////////////////////////////////// | |
473 | |
474 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkNormalSource) | |
475 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) | |
476 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalFlatSourceImpl) | |
477 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
478 | |
479 //////////////////////////////////////////////////////////////////////////// | |
OLD | NEW |