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; | |
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 19:21:36
Only allocate fNonAmbLightCnt
vjiaoblack
2016/08/29 19:56:38
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()-> | |
658 peekPixels(&fShadowMapPixels[i]); | |
659 } | |
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 19:21:36
We should probably delete this first
vjiaoblack
2016/08/29 19:56:39
Done.
| |
671 delete [] fShadowMapPixels; | |
654 } | 672 } |
655 | 673 |
656 static inline SkPMColor convert(SkColor3f color, U8CPU a) { | 674 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
657 if (color.fX <= 0.0f) { | 675 if (color.fX <= 0.0f) { |
658 color.fX = 0.0f; | 676 color.fX = 0.0f; |
659 } else if (color.fX >= 255.0f) { | 677 } else if (color.fX >= 255.0f) { |
660 color.fX = 255.0f; | 678 color.fX = 255.0f; |
661 } | 679 } |
662 | 680 |
663 if (color.fY <= 0.0f) { | 681 if (color.fY <= 0.0f) { |
(...skipping 12 matching lines...) Expand all Loading... | |
676 } | 694 } |
677 | 695 |
678 // larger is better (fewer times we have to loop), but we shouldn't | 696 // 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) | 697 // take up too much stack-space (each one here costs 16 bytes) |
680 #define BUFFER_MAX 16 | 698 #define BUFFER_MAX 16 |
681 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, | 699 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, |
682 SkPMColor result[], int count) { | 700 SkPMColor result[], int count) { |
683 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); | 701 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); |
684 | 702 |
685 SkPMColor diffuse[BUFFER_MAX]; | 703 SkPMColor diffuse[BUFFER_MAX]; |
704 SkPMColor povDepth[BUFFER_MAX]; | |
686 | 705 |
687 do { | 706 do { |
688 int n = SkTMin(count, BUFFER_MAX); | 707 int n = SkTMin(count, BUFFER_MAX); |
689 | 708 |
690 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
691 fDiffuseContext->shadeSpan(x, y, diffuse, n); | 709 fDiffuseContext->shadeSpan(x, y, diffuse, n); |
710 fPovDepthContext->shadeSpan(x, y, povDepth, n); | |
692 | 711 |
693 for (int i = 0; i < n; ++i) { | 712 for (int i = 0; i < n; ++i) { |
713 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
714 SkColor povDepthColor = povDepth[i]; | |
694 | 715 |
robertphillips
2016/08/29 19:21:36
It doesn't look like you need totalColor
vjiaoblack
2016/08/29 19:56:39
Well, we need it at the end to multiply with total
| |
695 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | 716 SkColor3f totalColor = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
696 | 717 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) | 718 // This is all done in linear unpremul color space (each component 0 ..255.0f though) |
699 | 719 |
700 accum.fX += lightShader.fLights->ambientLightColor().fX * SkColorGet R(diffColor); | |
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) { | 720 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
705 const SkLights::Light& light = lightShader.fLights->light(l); | 721 const SkLights::Light& light = lightShader.fLights->light(l); |
706 | 722 |
707 if (SkLights::Light::kDirectional_LightType == light.type()) { | 723 if (light.type() == SkLights::Light::kDirectional_LightType) { |
708 // scaling by fZ accounts for lighting direction | 724 int xOffset = SkScalarRoundToInt(light.dir().fX * |
709 accum.fX += light.color().makeScale(light.dir().fZ).fX * | 725 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
710 SkColorGetR(diffColor); | 726 int yOffset = SkScalarRoundToInt(light.dir().fY * |
711 accum.fY += light.color().makeScale(light.dir().fZ).fY * | 727 SkIntToScalar(SkColorGetB(p ovDepthColor))); |
712 SkColorGetG(diffColor); | 728 |
713 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | 729 int shX = (x + i + xOffset); |
714 SkColorGetB(diffColor); | 730 int shY = (y + yOffset); |
731 | |
732 shX = SkClampPos(shX); | |
733 shY = SkClampPos(shY); | |
734 | |
735 shX = SkClampMax(shX, light.getShadowMap()->width() - 1); | |
736 shY = SkClampMax(shY, light.getShadowMap()->height() - 1); | |
737 | |
738 int shDepth = 0; | |
robertphillips
2016/08/29 19:21:36
Can we declare this earlier and then reuse it in t
vjiaoblack
2016/08/29 19:56:38
Done.
| |
739 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel | |
740 | |
741 // pixmaps that point to things have nonzero heights | |
742 if (fShadowMapPixels[l].height() > 0) { | |
743 uint32_t pix = *fShadowMapPixels[l].addr32(shX, shY); | |
744 SkColor shColor(pix); | |
745 | |
746 shDepth += SkColorGetB(shColor); | |
747 } else { | |
748 // TODO: handle not being able to read shadow map pixels | |
749 } | |
750 | |
751 if (pvDepth >= shDepth) { | |
752 // assume object normals are pointing straight up | |
753 totalLight.fX += light.dir().fZ * light.color().fX; | |
754 totalLight.fY += light.dir().fZ * light.color().fY; | |
755 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
756 } | |
715 } else { | 757 } else { |
716 accum.fX += light.color().fX * SkColorGetR(diffColor); | 758 totalLight += light.color(); |
717 accum.fY += light.color().fY * SkColorGetG(diffColor); | |
718 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | |
719 } | 759 } |
720 | |
721 } | 760 } |
722 | 761 |
723 result[i] = convert(accum, SkColorGetA(diffColor)); | 762 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
763 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
764 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
765 | |
766 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
724 } | 767 } |
725 | 768 |
726 result += n; | 769 result += n; |
727 x += n; | 770 x += n; |
728 count -= n; | 771 count -= n; |
729 } while (count > 0); | 772 } while (count > 0); |
730 } | 773 } |
731 | 774 |
732 //////////////////////////////////////////////////////////////////////////// | 775 //////////////////////////////////////////////////////////////////////////// |
733 | 776 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
836 | 879 |
837 /////////////////////////////////////////////////////////////////////////////// | 880 /////////////////////////////////////////////////////////////////////////////// |
838 | 881 |
839 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 882 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
840 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 883 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
841 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 884 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
842 | 885 |
843 /////////////////////////////////////////////////////////////////////////////// | 886 /////////////////////////////////////////////////////////////////////////////// |
844 | 887 |
845 #endif | 888 #endif |
OLD | NEW |