OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkBitmapProcShader.h" | 8 #include "SkBitmapProcShader.h" |
9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
10 #include "SkColor.h" | 10 #include "SkColor.h" |
11 #include "SkEmptyShader.h" | 11 #include "SkEmptyShader.h" |
12 #include "SkErrorInternals.h" | 12 #include "SkErrorInternals.h" |
13 #include "SkLightingShader.h" | 13 #include "SkLightingShader.h" |
14 #include "SkMathPriv.h" | 14 #include "SkMathPriv.h" |
| 15 #include "SkNormalSource.h" |
15 #include "SkPoint3.h" | 16 #include "SkPoint3.h" |
16 #include "SkReadBuffer.h" | 17 #include "SkReadBuffer.h" |
17 #include "SkWriteBuffer.h" | 18 #include "SkWriteBuffer.h" |
18 | 19 |
19 //////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////// |
20 | 21 |
21 /* | 22 /* |
22 SkLightingShader TODOs: | 23 SkLightingShader TODOs: |
23 support other than clamp mode | 24 support other than clamp mode |
24 allow 'diffuse' & 'normal' to be of different dimensions? | 25 allow 'diffuse' & 'normal' to be of different dimensions? |
(...skipping 23 matching lines...) Expand all Loading... |
48 @param lights the lights applied to the normal map | 49 @param lights the lights applied to the normal map |
49 @param invNormRotation rotation applied to the normal map's normals | 50 @param invNormRotation rotation applied to the normal map's normals |
50 @param diffLocalM the local matrix for the diffuse coordinates | 51 @param diffLocalM the local matrix for the diffuse coordinates |
51 @param normLocalM the local matrix for the normal coordinates | 52 @param normLocalM the local matrix for the normal coordinates |
52 @param normalSource the normal source for GPU computations | 53 @param normalSource the normal source for GPU computations |
53 */ | 54 */ |
54 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | 55 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, |
55 const sk_sp<SkLights> lights, | 56 const sk_sp<SkLights> lights, |
56 const SkVector& invNormRotation, | 57 const SkVector& invNormRotation, |
57 const SkMatrix* diffLocalM, const SkMatrix* normLocalM, | 58 const SkMatrix* diffLocalM, const SkMatrix* normLocalM, |
58 sk_sp<SkLightingShader::NormalSource> normalSource) | 59 sk_sp<SkNormalSource> normalSource) |
59 : INHERITED(diffLocalM) | 60 : INHERITED(diffLocalM) |
60 , fDiffuseMap(diffuse) | 61 , fDiffuseMap(diffuse) |
61 , fNormalMap(normal) | 62 , fNormalMap(normal) |
62 , fLights(std::move(lights)) | 63 , fLights(std::move(lights)) |
63 , fInvNormRotation(invNormRotation) { | 64 , fInvNormRotation(invNormRotation) { |
64 | 65 |
65 if (normLocalM) { | 66 if (normLocalM) { |
66 fNormLocalMatrix = *normLocalM; | 67 fNormLocalMatrix = *normLocalM; |
67 } else { | 68 } else { |
68 fNormLocalMatrix.reset(); | 69 fNormLocalMatrix.reset(); |
(...skipping 12 matching lines...) Expand all Loading... |
81 const SkMatrix* localMatrix, | 82 const SkMatrix* localMatrix, |
82 SkFilterQuality, | 83 SkFilterQuality, |
83 SkSourceGammaTreatment) const
override; | 84 SkSourceGammaTreatment) const
override; |
84 #endif | 85 #endif |
85 | 86 |
86 class LightingShaderContext : public SkShader::Context { | 87 class LightingShaderContext : public SkShader::Context { |
87 public: | 88 public: |
88 // The context takes ownership of the states. It will call their destruc
tors | 89 // The context takes ownership of the states. It will call their destruc
tors |
89 // but will NOT free the memory. | 90 // but will NOT free the memory. |
90 LightingShaderContext(const SkLightingShaderImpl&, const ContextRec&, | 91 LightingShaderContext(const SkLightingShaderImpl&, const ContextRec&, |
91 SkBitmapProcState* diffuseState, SkBitmapProcState
* normalState); | 92 SkBitmapProcState* diffuseState, SkNormalSource::P
rovider*); |
92 ~LightingShaderContext() override; | 93 ~LightingShaderContext() override; |
93 | 94 |
94 void shadeSpan(int x, int y, SkPMColor[], int count) override; | 95 void shadeSpan(int x, int y, SkPMColor[], int count) override; |
95 | 96 |
96 uint32_t getFlags() const override { return fFlags; } | 97 uint32_t getFlags() const override { return fFlags; } |
97 | 98 |
98 private: | 99 private: |
99 SkBitmapProcState* fDiffuseState; | 100 SkBitmapProcState* fDiffuseState; |
100 SkBitmapProcState* fNormalState; | 101 SkNormalSource::Provider* fNormalProvider; |
101 uint32_t fFlags; | 102 uint32_t fFlags; |
102 | 103 |
103 typedef SkShader::Context INHERITED; | 104 typedef SkShader::Context INHERITED; |
104 }; | 105 }; |
105 | 106 |
106 SK_TO_STRING_OVERRIDE() | 107 SK_TO_STRING_OVERRIDE() |
107 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) | 108 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) |
108 | 109 |
109 protected: | 110 protected: |
110 void flatten(SkWriteBuffer&) const override; | 111 void flatten(SkWriteBuffer&) const override; |
111 size_t onContextSize(const ContextRec&) const override; | 112 size_t onContextSize(const ContextRec&) const override; |
112 Context* onCreateContext(const ContextRec&, void*) const override; | 113 Context* onCreateContext(const ContextRec&, void*) const override; |
113 bool computeNormTotalInverse(const ContextRec& rec, SkMatrix* normTotalInver
se) const; | |
114 | 114 |
115 private: | 115 private: |
116 SkBitmap fDiffuseMap; | 116 SkBitmap fDiffuseMap; |
117 SkBitmap fNormalMap; | 117 SkBitmap fNormalMap; |
118 | 118 |
119 sk_sp<SkLights> fLights; | 119 sk_sp<SkLights> fLights; |
120 | 120 |
121 SkMatrix fNormLocalMatrix; | 121 SkMatrix fNormLocalMatrix; |
122 SkVector fInvNormRotation; | 122 SkVector fInvNormRotation; |
123 | 123 |
124 sk_sp<SkLightingShader::NormalSource> fNormalSource; | 124 sk_sp<SkNormalSource> fNormalSource; |
125 | 125 |
126 friend class SkLightingShader; | 126 friend class SkLightingShader; |
127 | 127 |
128 typedef SkShader INHERITED; | 128 typedef SkShader INHERITED; |
129 }; | 129 }; |
130 | 130 |
131 //////////////////////////////////////////////////////////////////////////// | 131 //////////////////////////////////////////////////////////////////////////// |
132 | 132 |
133 #if SK_SUPPORT_GPU | 133 #if SK_SUPPORT_GPU |
134 | 134 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 360 |
361 #endif | 361 #endif |
362 | 362 |
363 //////////////////////////////////////////////////////////////////////////// | 363 //////////////////////////////////////////////////////////////////////////// |
364 | 364 |
365 bool SkLightingShaderImpl::isOpaque() const { | 365 bool SkLightingShaderImpl::isOpaque() const { |
366 return fDiffuseMap.isOpaque(); | 366 return fDiffuseMap.isOpaque(); |
367 } | 367 } |
368 | 368 |
369 SkLightingShaderImpl::LightingShaderContext::LightingShaderContext( | 369 SkLightingShaderImpl::LightingShaderContext::LightingShaderContext( |
370 const SkLighting
ShaderImpl& shader, | 370 const SkLightingShaderImpl& shader, const ContextRec& rec, SkBitmapProcS
tate* diffuseState, |
371 const ContextRec
& rec, | 371 SkNormalSource::Provider* normalProvider) |
372 SkBitmapProcStat
e* diffuseState, | |
373 SkBitmapProcStat
e* normalState) | |
374 : INHERITED(shader, rec) | 372 : INHERITED(shader, rec) |
375 , fDiffuseState(diffuseState) | 373 , fDiffuseState(diffuseState) |
376 , fNormalState(normalState) { | 374 , fNormalProvider(normalProvider) { |
377 const SkPixmap& pixmap = fDiffuseState->fPixmap; | 375 const SkPixmap& pixmap = fDiffuseState->fPixmap; |
378 bool isOpaque = pixmap.isOpaque(); | 376 bool isOpaque = pixmap.isOpaque(); |
379 | 377 |
380 // update fFlags | 378 // update fFlags |
381 uint32_t flags = 0; | 379 uint32_t flags = 0; |
382 if (isOpaque && (255 == this->getPaintAlpha())) { | 380 if (isOpaque && (255 == this->getPaintAlpha())) { |
383 flags |= kOpaqueAlpha_Flag; | 381 flags |= kOpaqueAlpha_Flag; |
384 } | 382 } |
385 | 383 |
386 fFlags = flags; | 384 fFlags = flags; |
387 } | 385 } |
388 | 386 |
389 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { | 387 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { |
390 // The bitmap proc states have been created outside of the context on memory
that will be freed | 388 // The bitmap proc states have been created outside of the context on memory
that will be freed |
391 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. | 389 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. |
392 fDiffuseState->~SkBitmapProcState(); | 390 fDiffuseState->~SkBitmapProcState(); |
393 fNormalState->~SkBitmapProcState(); | 391 fNormalProvider->~Provider(); |
394 } | 392 } |
395 | 393 |
396 static inline SkPMColor convert(SkColor3f color, U8CPU a) { | 394 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
397 if (color.fX <= 0.0f) { | 395 if (color.fX <= 0.0f) { |
398 color.fX = 0.0f; | 396 color.fX = 0.0f; |
399 } else if (color.fX >= 255.0f) { | 397 } else if (color.fX >= 255.0f) { |
400 color.fX = 255.0f; | 398 color.fX = 255.0f; |
401 } | 399 } |
402 | 400 |
403 if (color.fY <= 0.0f) { | 401 if (color.fY <= 0.0f) { |
404 color.fY = 0.0f; | 402 color.fY = 0.0f; |
405 } else if (color.fY >= 255.0f) { | 403 } else if (color.fY >= 255.0f) { |
406 color.fY = 255.0f; | 404 color.fY = 255.0f; |
407 } | 405 } |
408 | 406 |
409 if (color.fZ <= 0.0f) { | 407 if (color.fZ <= 0.0f) { |
410 color.fZ = 0.0f; | 408 color.fZ = 0.0f; |
411 } else if (color.fZ >= 255.0f) { | 409 } else if (color.fZ >= 255.0f) { |
412 color.fZ = 255.0f; | 410 color.fZ = 255.0f; |
413 } | 411 } |
414 | 412 |
415 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ)
; | 413 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ)
; |
416 } | 414 } |
417 | 415 |
418 // larger is better (fewer times we have to loop), but we shouldn't | 416 // larger is better (fewer times we have to loop), but we shouldn't |
419 // take up too much stack-space (each one here costs 16 bytes) | 417 // take up too much stack-space (each one here costs 16 bytes) |
420 #define TMP_COUNT 16 | 418 #define TMP_COUNT 16 |
421 | 419 #define BUFFER_MAX ((int)(TMP_COUNT * sizeof(uint32_t))) |
422 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, | 420 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, |
423 SkPMColor result[],
int count) { | 421 SkPMColor result[],
int count) { |
424 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader
Impl&>(fShader); | 422 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader
Impl&>(fShader); |
425 | 423 |
426 uint32_t tmpColor[TMP_COUNT], tmpNormal[TMP_COUNT]; | 424 uint32_t tmpColor[TMP_COUNT]; |
427 SkPMColor tmpColor2[2*TMP_COUNT], tmpNormal2[2*TMP_COUNT]; | 425 SkPMColor tmpColor2[2*TMP_COUNT]; |
428 | 426 |
429 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); | 427 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); |
430 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32()
; | 428 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32()
; |
431 | 429 |
432 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); | 430 int max = fDiffuseState->maxCountForBufferSize(BUFFER_MAX); |
433 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32(
); | |
434 | |
435 int diffMax = fDiffuseState->maxCountForBufferSize(sizeof(tmpColor[0]) * TMP
_COUNT); | |
436 int normMax = fNormalState->maxCountForBufferSize(sizeof(tmpNormal[0]) * TMP
_COUNT); | |
437 int max = SkTMin(diffMax, normMax); | |
438 | 431 |
439 SkASSERT(fDiffuseState->fPixmap.addr()); | 432 SkASSERT(fDiffuseState->fPixmap.addr()); |
440 SkASSERT(fNormalState->fPixmap.addr()); | |
441 | 433 |
442 SkPoint3 norm, xformedNorm; | 434 SkASSERT(max <= BUFFER_MAX); |
| 435 SkPoint3 normals[BUFFER_MAX]; |
443 | 436 |
444 do { | 437 do { |
445 int n = count; | 438 int n = count; |
446 if (n > max) { | 439 if (n > max) { |
447 n = max; | 440 n = max; |
448 } | 441 } |
449 | 442 |
450 diffMProc(*fDiffuseState, tmpColor, n, x, y); | 443 diffMProc(*fDiffuseState, tmpColor, n, x, y); |
451 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); | 444 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); |
452 | 445 |
453 normalMProc(*fNormalState, tmpNormal, n, x, y); | 446 fNormalProvider->fillScanLine(x, y, normals, n); |
454 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); | |
455 | 447 |
456 for (int i = 0; i < n; ++i) { | 448 for (int i = 0; i < n; ++i) { |
457 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | |
458 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, | |
459 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, | |
460 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); | |
461 norm.normalize(); | |
462 | |
463 xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX + | |
464 lightShader.fInvNormRotation.fY * norm.fY; | |
465 xformedNorm.fY = -lightShader.fInvNormRotation.fY * norm.fX + | |
466 lightShader.fInvNormRotation.fX * norm.fY; | |
467 xformedNorm.fZ = norm.fZ; | |
468 | 449 |
469 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | 450 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); |
470 | 451 |
471 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | 452 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
472 // This is all done in linear unpremul color space (each component 0
..255.0f though) | 453 // This is all done in linear unpremul color space (each component 0
..255.0f though) |
473 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 454 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
474 const SkLights::Light& light = lightShader.fLights->light(l); | 455 const SkLights::Light& light = lightShader.fLights->light(l); |
475 | 456 |
476 if (SkLights::Light::kAmbient_LightType == light.type()) { | 457 if (SkLights::Light::kAmbient_LightType == light.type()) { |
477 accum += light.color().makeScale(255.0f); | 458 accum += light.color().makeScale(255.0f); |
478 } else { | 459 } else { |
479 SkScalar NdotL = xformedNorm.dot(light.dir()); | 460 SkScalar NdotL = normals[i].dot(light.dir()); |
480 if (NdotL < 0.0f) { | 461 if (NdotL < 0.0f) { |
481 NdotL = 0.0f; | 462 NdotL = 0.0f; |
482 } | 463 } |
483 | 464 |
484 accum.fX += light.color().fX * SkColorGetR(diffColor) * Ndot
L; | 465 accum.fX += light.color().fX * SkColorGetR(diffColor) * Ndot
L; |
485 accum.fY += light.color().fY * SkColorGetG(diffColor) * Ndot
L; | 466 accum.fY += light.color().fY * SkColorGetG(diffColor) * Ndot
L; |
486 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * Ndot
L; | 467 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * Ndot
L; |
487 } | 468 } |
488 } | 469 } |
489 | 470 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 } | 537 } |
557 } | 538 } |
558 | 539 |
559 sk_sp<SkLights> lights(builder.finish()); | 540 sk_sp<SkLights> lights(builder.finish()); |
560 | 541 |
561 SkVector invNormRotation = {1,0}; | 542 SkVector invNormRotation = {1,0}; |
562 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { | 543 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { |
563 invNormRotation = buf.readPoint(); | 544 invNormRotation = buf.readPoint(); |
564 } | 545 } |
565 | 546 |
566 sk_sp<SkLightingShader::NormalSource> normalSource( | 547 sk_sp<SkNormalSource> normalSource(buf.readFlattenable<SkNormalSource>()); |
567 buf.readFlattenable<SkLightingShader::NormalSource>()); | |
568 | 548 |
569 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights),
invNormRotation, | 549 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights),
invNormRotation, |
570 &diffLocalM, &normLocalM, std::move(
normalSource)); | 550 &diffLocalM, &normLocalM, std::move(
normalSource)); |
571 } | 551 } |
572 | 552 |
573 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | 553 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
574 this->INHERITED::flatten(buf); | 554 this->INHERITED::flatten(buf); |
575 | 555 |
576 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); | 556 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); |
577 buf.writeBool(hasNormLocalM); | 557 buf.writeBool(hasNormLocalM); |
(...skipping 14 matching lines...) Expand all Loading... |
592 buf.writeScalarArray(&light.color().fX, 3); | 572 buf.writeScalarArray(&light.color().fX, 3); |
593 if (!isAmbient) { | 573 if (!isAmbient) { |
594 buf.writeScalarArray(&light.dir().fX, 3); | 574 buf.writeScalarArray(&light.dir().fX, 3); |
595 } | 575 } |
596 } | 576 } |
597 buf.writePoint(fInvNormRotation); | 577 buf.writePoint(fInvNormRotation); |
598 | 578 |
599 buf.writeFlattenable(fNormalSource.get()); | 579 buf.writeFlattenable(fNormalSource.get()); |
600 } | 580 } |
601 | 581 |
602 bool SkLightingShaderImpl::computeNormTotalInverse(const ContextRec& rec, | 582 size_t SkLightingShaderImpl::onContextSize(const ContextRec& rec) const { |
603 SkMatrix* normTotalInverse) c
onst { | 583 return sizeof(LightingShaderContext) + |
604 SkMatrix total; | 584 sizeof(SkBitmapProcState) + |
605 total.setConcat(*rec.fMatrix, fNormLocalMatrix); | 585 fNormalSource->providerSize(rec); |
606 | |
607 const SkMatrix* m = &total; | |
608 if (rec.fLocalMatrix) { | |
609 total.setConcat(*m, *rec.fLocalMatrix); | |
610 m = &total; | |
611 } | |
612 return m->invert(normTotalInverse); | |
613 } | |
614 | |
615 size_t SkLightingShaderImpl::onContextSize(const ContextRec&) const { | |
616 return 2 * sizeof(SkBitmapProcState) + sizeof(LightingShaderContext); | |
617 } | 586 } |
618 | 587 |
619 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, | 588 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, |
620 void* storage) const { | 589 void* storage) const { |
621 | 590 |
622 SkMatrix diffTotalInv; | 591 SkMatrix diffTotalInv; |
623 // computeTotalInverse was called in SkShader::createContext so we know it w
ill succeed | 592 // computeTotalInverse was called in SkShader::createContext so we know it w
ill succeed |
624 SkAssertResult(this->computeTotalInverse(rec, &diffTotalInv)); | 593 SkAssertResult(this->computeTotalInverse(rec, &diffTotalInv)); |
625 | 594 |
626 SkMatrix normTotalInv; | |
627 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { | |
628 return nullptr; | |
629 } | |
630 | |
631 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); | 595 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); |
632 SkBitmapProcState* diffuseState = new (diffuseStateStorage) SkBitmapProcStat
e(fDiffuseMap, | 596 SkBitmapProcState* diffuseState = new (diffuseStateStorage) SkBitmapProcStat
e(fDiffuseMap, |
633 SkShader::kClamp_TileMode, SkShade
r::kClamp_TileMode, | 597 SkShader::kClamp_TileMode, SkShade
r::kClamp_TileMode, |
634 SkMipMap::De
duceTreatment(rec)); | 598 SkMipMap::De
duceTreatment(rec)); |
635 SkASSERT(diffuseState); | 599 SkASSERT(diffuseState); |
636 if (!diffuseState->setup(diffTotalInv, *rec.fPaint)) { | 600 if (!diffuseState->setup(diffTotalInv, *rec.fPaint)) { |
637 diffuseState->~SkBitmapProcState(); | 601 diffuseState->~SkBitmapProcState(); |
638 return nullptr; | 602 return nullptr; |
639 } | 603 } |
| 604 void* normalProviderStorage = (char*)storage + |
| 605 sizeof(LightingShaderContext) + |
| 606 sizeof(SkBitmapProcState); |
| 607 |
| 608 SkNormalSource::Provider* normalProvider = fNormalSource->asProvider(rec, |
| 609 normalP
roviderStorage); |
| 610 if (!normalProvider) { |
| 611 diffuseState->~SkBitmapProcState(); |
| 612 return nullptr; |
| 613 } |
640 | 614 |
641 void* normalStateStorage = (char*)storage + | 615 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalP
rovider); |
642 sizeof(LightingShaderContext) + | |
643 sizeof(SkBitmapProcState); | |
644 SkBitmapProcState* normalState = new (normalStateStorage) SkBitmapProcState(
fNormalMap, | |
645 SkShader::kClamp_TileMode, SkShader:
:kClamp_TileMode, | |
646 SkMipMap::De
duceTreatment(rec)); | |
647 SkASSERT(normalState); | |
648 if (!normalState->setup(normTotalInv, *rec.fPaint)) { | |
649 diffuseState->~SkBitmapProcState(); | |
650 normalState->~SkBitmapProcState(); | |
651 return nullptr; | |
652 } | |
653 | |
654 return new (storage) LightingShaderContext(*this, rec, diffuseState, normalS
tate); | |
655 } | 616 } |
656 | 617 |
657 /////////////////////////////////////////////////////////////////////////////// | 618 /////////////////////////////////////////////////////////////////////////////// |
658 | 619 |
659 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap&
normal, | 620 sk_sp<SkShader> SkLightingShader::Make(const SkBitmap& diffuse, const SkBitmap&
normal, |
660 sk_sp<SkLights> lights, | 621 sk_sp<SkLights> lights, |
661 const SkVector& invNormRotation, | 622 const SkVector& invNormRotation, |
662 const SkMatrix* diffLocalM, const SkMatri
x* normLocalM) { | 623 const SkMatrix* diffLocalM, const SkMatri
x* normLocalM) { |
663 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse) || | 624 if (diffuse.isNull() || SkBitmapProcShader::BitmapIsTooBig(diffuse) || |
664 normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal) || | 625 normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal) || |
665 diffuse.width() != normal.width() || | 626 diffuse.width() != normal.width() || |
666 diffuse.height() != normal.height()) { | 627 diffuse.height() != normal.height()) { |
667 return nullptr; | 628 return nullptr; |
668 } | 629 } |
669 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); | 630 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); |
670 | 631 |
671 sk_sp<SkLightingShader::NormalSource> normalSource = | 632 // TODO: support other tile modes |
672 SkLightingShader::NormalSource::MakeMap(normal, invNormRotation, nor
mLocalM); | 633 sk_sp<SkShader> mapShader = SkMakeBitmapShader(normal, SkShader::kClamp_Tile
Mode, |
| 634 SkShader::kClamp_TileMode, no
rmLocalM, nullptr); |
| 635 |
| 636 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(mapSh
ader, |
| 637 invNo
rmRotation); |
673 | 638 |
674 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), | 639 return sk_make_sp<SkLightingShaderImpl>(diffuse, normal, std::move(lights), |
675 invNormRotation, diffLocalM, normLocalM, std::move(normalSource)); | 640 invNormRotation, diffLocalM, normLocalM, std::move(normalSource)); |
676 } | 641 } |
677 | 642 |
678 /////////////////////////////////////////////////////////////////////////////// | 643 /////////////////////////////////////////////////////////////////////////////// |
679 | 644 |
680 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 645 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
681 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 646 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
682 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 647 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
683 | 648 |
684 /////////////////////////////////////////////////////////////////////////////// | 649 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |