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 "SkCanvas.h" | 8 #include "SkCanvas.h" |
9 #include "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkShadowShader.h" | 10 #include "SkShadowShader.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 | 54 |
55 uint32_t getFlags() const override { return fFlags; } | 55 uint32_t getFlags() const override { return fFlags; } |
56 | 56 |
57 private: | 57 private: |
58 SkShader::Context* fPovDepthContext; | 58 SkShader::Context* fPovDepthContext; |
59 SkShader::Context* fDiffuseContext; | 59 SkShader::Context* fDiffuseContext; |
60 uint32_t fFlags; | 60 uint32_t fFlags; |
61 | 61 |
62 void* fHeapAllocated; | 62 void* fHeapAllocated; |
63 | 63 |
64 int fNonAmbLightCnt; | |
robertphillips
2016/08/29 18:03:40
This should be SkPixmap*
vjiaoblack
2016/08/29 18:22:07
Done.
| |
65 SkPixmap** fShadowMapPixels; | |
66 | |
67 | |
64 typedef SkShader::Context INHERITED; | 68 typedef SkShader::Context INHERITED; |
65 }; | 69 }; |
66 | 70 |
67 SK_TO_STRING_OVERRIDE() | 71 SK_TO_STRING_OVERRIDE() |
68 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl) | 72 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl) |
69 | 73 |
70 protected: | 74 protected: |
71 void flatten(SkWriteBuffer&) const override; | 75 void flatten(SkWriteBuffer&) const override; |
72 size_t onContextSize(const ContextRec&) const override; | 76 size_t onContextSize(const ContextRec&) const override; |
73 Context* onCreateContext(const ContextRec&, void*) const override; | 77 Context* onCreateContext(const ContextRec&, void*) const override; |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 , fHeapAllocated(heapAllocated) { | 639 , fHeapAllocated(heapAllocated) { |
636 bool isOpaque = shader.isOpaque(); | 640 bool isOpaque = shader.isOpaque(); |
637 | 641 |
638 // update fFlags | 642 // update fFlags |
639 uint32_t flags = 0; | 643 uint32_t flags = 0; |
640 if (isOpaque && (255 == this->getPaintAlpha())) { | 644 if (isOpaque && (255 == this->getPaintAlpha())) { |
641 flags |= kOpaqueAlpha_Flag; | 645 flags |= kOpaqueAlpha_Flag; |
642 } | 646 } |
643 | 647 |
644 fFlags = flags; | 648 fFlags = flags; |
649 | |
650 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); | |
651 | |
robertphillips
2016/08/29 18:03:39
fShadowMapPixels = new SkPixmap[fNonAmbLightCnt];
vjiaoblack
2016/08/29 18:22:07
Done.
| |
652 fShadowMapPixels = new SkPixmap*[SkShadowShader::kMaxNonAmbientLights]; | |
653 | |
654 fNonAmbLightCnt = lightShader.fLights->numLights(); | |
655 for (int i = 0; i < fNonAmbLightCnt; i++) { | |
656 if (lightShader.fLights->light(i).type() == SkLights::Light::kDirectiona l_LightType) { | |
657 lightShader.fLights->light(i).getShadowMap()-> | |
robertphillips
2016/08/29 18:03:39
shouldn't fNonAmbLightCnt be i here ?
vjiaoblack
2016/08/29 18:22:07
Done.
| |
658 peekPixels(fShadowMapPixels[fNonAmbLightCnt]); | |
659 } | |
robertphillips
2016/08/29 18:03:39
else {
fShadowMapPixels[i].reset();
}
- this i
vjiaoblack
2016/08/29 18:22:07
Yeah, because the uninitialized ones just will be
| |
660 } | |
645 } | 661 } |
646 | 662 |
647 SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() { | 663 SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() { |
648 // The dependencies have been created outside of the context on memory that was allocated by | 664 // The dependencies have been created outside of the context on memory that was allocated by |
649 // the onCreateContext() method. Call the destructors and free the memory. | 665 // the onCreateContext() method. Call the destructors and free the memory. |
650 fPovDepthContext->~Context(); | 666 fPovDepthContext->~Context(); |
651 fDiffuseContext->~Context(); | 667 fDiffuseContext->~Context(); |
652 | 668 |
653 sk_free(fHeapAllocated); | 669 sk_free(fHeapAllocated); |
670 | |
robertphillips
2016/08/29 18:03:39
This can then become "delete [] fShadowMapPixels;"
vjiaoblack
2016/08/29 18:22:07
Done.
| |
671 for (int i = 0; i < SkShadowShader::kMaxNonAmbientLights; i++) { | |
672 if (fShadowMapPixels[i] && fShadowMapPixels[i]->height() > 0) { | |
673 delete fShadowMapPixels[i]; | |
674 } | |
675 } | |
robertphillips
2016/08/29 18:03:39
as is, you're leaking fShadowMapPixels itself here
vjiaoblack
2016/08/29 18:22:07
Done.
| |
654 } | 676 } |
655 | 677 |
656 static inline SkPMColor convert(SkColor3f color, U8CPU a) { | 678 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
657 if (color.fX <= 0.0f) { | 679 if (color.fX <= 0.0f) { |
658 color.fX = 0.0f; | 680 color.fX = 0.0f; |
659 } else if (color.fX >= 255.0f) { | 681 } else if (color.fX >= 255.0f) { |
660 color.fX = 255.0f; | 682 color.fX = 255.0f; |
661 } | 683 } |
662 | 684 |
663 if (color.fY <= 0.0f) { | 685 if (color.fY <= 0.0f) { |
(...skipping 12 matching lines...) Expand all Loading... | |
676 } | 698 } |
677 | 699 |
678 // larger is better (fewer times we have to loop), but we shouldn't | 700 // larger is better (fewer times we have to loop), but we shouldn't |
679 // take up too much stack-space (each one here costs 16 bytes) | 701 // take up too much stack-space (each one here costs 16 bytes) |
680 #define BUFFER_MAX 16 | 702 #define BUFFER_MAX 16 |
681 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, | 703 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, |
682 SkPMColor result[], int count) { | 704 SkPMColor result[], int count) { |
683 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); | 705 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); |
684 | 706 |
685 SkPMColor diffuse[BUFFER_MAX]; | 707 SkPMColor diffuse[BUFFER_MAX]; |
708 SkPMColor povDepth[BUFFER_MAX]; | |
686 | 709 |
687 do { | 710 do { |
688 int n = SkTMin(count, BUFFER_MAX); | 711 int n = SkTMin(count, BUFFER_MAX); |
689 | 712 |
690 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
691 fDiffuseContext->shadeSpan(x, y, diffuse, n); | 713 fDiffuseContext->shadeSpan(x, y, diffuse, n); |
714 fPovDepthContext->shadeSpan(x, y, povDepth, n); | |
692 | 715 |
693 for (int i = 0; i < n; ++i) { | 716 for (int i = 0; i < n; ++i) { |
717 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
718 SkColor povDepthColor = povDepth[i]; | |
694 | 719 |
695 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 720 SkColor3f totalColor = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
696 | 721 SkColor3f totalLight = lightShader.fLights->ambientLightColor(); |
697 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
698 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | 722 // This is all done in linear unpremul color space (each component 0 ..255.0f though) |
699 | 723 |
robertphillips
2016/08/29 18:03:40
Do you intend to be changing fNonAmbLightCnt in th
vjiaoblack
2016/08/29 18:22:07
nope, good catch. Thanks.
| |
700 accum.fX += lightShader.fLights->ambientLightColor().fX * SkColorGet R(diffColor); | 724 fNonAmbLightCnt = 0; |
701 accum.fY += lightShader.fLights->ambientLightColor().fY * SkColorGet G(diffColor); | |
702 accum.fZ += lightShader.fLights->ambientLightColor().fZ * SkColorGet B(diffColor); | |
703 | |
704 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | 725 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
705 const SkLights::Light& light = lightShader.fLights->light(l); | 726 const SkLights::Light& light = lightShader.fLights->light(l); |
706 | 727 |
707 if (SkLights::Light::kDirectional_LightType == light.type()) { | 728 if (light.type() == SkLights::Light::kDirectional_LightType) { |
708 // scaling by fZ accounts for lighting direction | 729 int xOffset = SkScalarRoundToInt(light.dir().fX * |
709 accum.fX += light.color().makeScale(light.dir().fZ).fX * | 730 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
710 SkColorGetR(diffColor); | 731 int yOffset = SkScalarRoundToInt(light.dir().fY * |
711 accum.fY += light.color().makeScale(light.dir().fZ).fY * | 732 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
712 SkColorGetG(diffColor); | 733 |
713 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | 734 int shX = (x + i + xOffset); |
714 SkColorGetB(diffColor); | 735 int shY = (y + yOffset); |
736 | |
737 shX = SkClampPos(shX); | |
738 shY = SkClampPos(shY); | |
739 | |
740 shX = SkClampMax(shX, light.getShadowMap()->width() - 1); | |
741 shY = SkClampMax(shY, light.getShadowMap()->height() - 1); | |
742 | |
743 int shDepth = 0; | |
744 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel | |
745 | |
746 if (fShadowMapPixels[fNonAmbLightCnt] && | |
747 fShadowMapPixels[fNonAmbLightCnt]->height() > 0) { | |
748 uint32_t pix = *fShadowMapPixels[fNonAmbLightCnt]->addr3 2(shX, shY); | |
749 SkColor shColor(pix); | |
750 | |
751 shDepth += SkColorGetB(shColor); | |
752 } else { | |
753 // TODO: handle not being able to read shadow map pixels | |
754 } | |
755 | |
756 if (pvDepth >= shDepth) { | |
757 // assume object normals are pointing straight up | |
758 totalLight.fX += light.dir().fZ * light.color().fX; | |
759 totalLight.fY += light.dir().fZ * light.color().fY; | |
760 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
761 } | |
762 fNonAmbLightCnt++; | |
715 } else { | 763 } else { |
716 accum.fX += light.color().fX * SkColorGetR(diffColor); | 764 totalLight += light.color(); |
717 accum.fY += light.color().fY * SkColorGetG(diffColor); | |
718 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | |
719 } | 765 } |
720 | |
721 } | 766 } |
722 | 767 |
723 result[i] = convert(accum, SkColorGetA(diffColor)); | 768 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
769 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
770 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
771 | |
772 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
724 } | 773 } |
725 | 774 |
726 result += n; | 775 result += n; |
727 x += n; | 776 x += n; |
728 count -= n; | 777 count -= n; |
729 } while (count > 0); | 778 } while (count > 0); |
730 } | 779 } |
731 | 780 |
732 //////////////////////////////////////////////////////////////////////////// | 781 //////////////////////////////////////////////////////////////////////////// |
733 | 782 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 | 885 |
837 /////////////////////////////////////////////////////////////////////////////// | 886 /////////////////////////////////////////////////////////////////////////////// |
838 | 887 |
839 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 888 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
840 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 889 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
841 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 890 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
842 | 891 |
843 /////////////////////////////////////////////////////////////////////////////// | 892 /////////////////////////////////////////////////////////////////////////////// |
844 | 893 |
845 #endif | 894 #endif |
OLD | NEW |