Chromium Code Reviews| 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 | |
| 652 fNonAmbLightCnt = lightShader.fLights->numLights(); | |
| 653 fShadowMapPixels = new SkPixmap[fNonAmbLightCnt]; | |
| 654 | |
| 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() { |
| 664 delete[] fShadowMapPixels; | |
| 665 | |
| 648 // The dependencies have been created outside of the context on memory that was allocated by | 666 // 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. | 667 // the onCreateContext() method. Call the destructors and free the memory. |
| 650 fPovDepthContext->~Context(); | 668 fPovDepthContext->~Context(); |
| 651 fDiffuseContext->~Context(); | 669 fDiffuseContext->~Context(); |
| 652 | 670 |
| 653 sk_free(fHeapAllocated); | 671 sk_free(fHeapAllocated); |
| 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) { |
| (...skipping 18 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 |
| 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 shDepth = 0; |
| 709 accum.fX += light.color().makeScale(light.dir().fZ).fX * | 725 int pvDepth = SkColorGetB(povDepthColor); // depth stored in blue channel |
| 710 SkColorGetR(diffColor); | 726 |
|
robertphillips
2016/08/30 12:32:26
You don't really need to SkIntToScalars here
vjiaoblack
2016/08/30 12:48:41
Acknowledged.
| |
| 711 accum.fY += light.color().makeScale(light.dir().fZ).fY * | 727 int xOffset = SkScalarRoundToInt(light.dir().fX * SkIntToSca lar(pvDepth)); |
| 712 SkColorGetG(diffColor); | 728 int yOffset = SkScalarRoundToInt(light.dir().fY * SkIntToSca lar(pvDepth)); |
| 713 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * | 729 |
|
robertphillips
2016/08/30 12:32:26
Why does 'i' factor into the formula for 'shX' ?
vjiaoblack
2016/08/30 12:48:41
i is the offset on X.
What seems to happen is tha
| |
| 714 SkColorGetB(diffColor); | 730 int shX = (x + i + xOffset); |
| 731 int shY = (y + yOffset); | |
| 732 | |
| 733 shX = SkClampPos(shX); | |
| 734 shY = SkClampPos(shY); | |
| 735 | |
| 736 shX = SkClampMax(shX, light.getShadowMap()->width() - 1); | |
| 737 shY = SkClampMax(shY, light.getShadowMap()->height() - 1); | |
| 738 | |
| 739 // pixmaps that point to things have nonzero heights | |
|
robertphillips
2016/08/30 12:32:26
move the definition of 'shDepth' here, closer to i
vjiaoblack
2016/08/30 12:48:41
Done.
| |
| 740 if (fShadowMapPixels[l].height() > 0) { | |
| 741 uint32_t pix = *fShadowMapPixels[l].addr32(shX, shY); | |
| 742 SkColor shColor(pix); | |
| 743 | |
|
robertphillips
2016/08/30 12:32:26
Shouldn't this '+=' just be a '=' ?
vjiaoblack
2016/08/30 12:48:41
Done.
| |
| 744 shDepth += SkColorGetB(shColor); | |
| 745 } else { | |
| 746 // TODO: handle not being able to read shadow map pixels | |
|
robertphillips
2016/08/30 12:32:26
// Make lights w/o a shadow map receive the full l
vjiaoblack
2016/08/30 12:48:41
Done.
| |
| 747 } | |
| 748 | |
| 749 if (pvDepth >= shDepth) { | |
| 750 // assume object normals are pointing straight up | |
| 751 totalLight.fX += light.dir().fZ * light.color().fX; | |
| 752 totalLight.fY += light.dir().fZ * light.color().fY; | |
| 753 totalLight.fZ += light.dir().fZ * light.color().fZ; | |
| 754 } | |
| 715 } else { | 755 } else { |
| 716 accum.fX += light.color().fX * SkColorGetR(diffColor); | 756 totalLight += light.color(); |
| 717 accum.fY += light.color().fY * SkColorGetG(diffColor); | |
| 718 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | |
| 719 } | 757 } |
| 720 | |
| 721 } | 758 } |
| 722 | 759 |
|
robertphillips
2016/08/30 12:32:26
If you want to keep totalColor, move it down here
vjiaoblack
2016/08/30 12:48:42
Done.
| |
| 723 result[i] = convert(accum, SkColorGetA(diffColor)); | 760 totalColor.fX += SkColorGetR(diffColor) * totalLight.fX; |
| 761 totalColor.fY += SkColorGetG(diffColor) * totalLight.fY; | |
| 762 totalColor.fZ += SkColorGetB(diffColor) * totalLight.fZ; | |
| 763 | |
| 764 result[i] = convert(totalColor, SkColorGetA(diffColor)); | |
| 724 } | 765 } |
| 725 | 766 |
| 726 result += n; | 767 result += n; |
| 727 x += n; | 768 x += n; |
| 728 count -= n; | 769 count -= n; |
| 729 } while (count > 0); | 770 } while (count > 0); |
| 730 } | 771 } |
| 731 | 772 |
| 732 //////////////////////////////////////////////////////////////////////////// | 773 //////////////////////////////////////////////////////////////////////////// |
| 733 | 774 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 836 | 877 |
| 837 /////////////////////////////////////////////////////////////////////////////// | 878 /////////////////////////////////////////////////////////////////////////////// |
| 838 | 879 |
| 839 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 880 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 840 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 881 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 841 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 882 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 842 | 883 |
| 843 /////////////////////////////////////////////////////////////////////////////// | 884 /////////////////////////////////////////////////////////////////////////////// |
| 844 | 885 |
| 845 #endif | 886 #endif |
| OLD | NEW |