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 | 8 |
9 #include "SkLights.h" | 9 #include "SkLights.h" |
10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 str->appendf("ShadowShader: ()"); | 585 str->appendf("ShadowShader: ()"); |
586 } | 586 } |
587 #endif | 587 #endif |
588 | 588 |
589 sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) { | 589 sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) { |
590 | 590 |
591 // Discarding SkShader flattenable params | 591 // Discarding SkShader flattenable params |
592 bool hasLocalMatrix = buf.readBool(); | 592 bool hasLocalMatrix = buf.readBool(); |
593 SkAssertResult(!hasLocalMatrix); | 593 SkAssertResult(!hasLocalMatrix); |
594 | 594 |
595 int numLights = buf.readInt(); | 595 sk_sp<SkLights> lights = SkLights::MakeFromBuffer(buf); |
596 | |
597 SkLights::Builder builder; | |
598 | |
599 for (int l = 0; l < numLights; ++l) { | |
600 bool isAmbient = buf.readBool(); | |
601 | |
602 SkColor3f color; | |
603 if (!buf.readScalarArray(&color.fX, 3)) { | |
604 return nullptr; | |
605 } | |
606 | |
607 if (isAmbient) { | |
608 builder.add(SkLights::Light(color)); | |
609 } else { | |
610 SkVector3 dir; | |
611 if (!buf.readScalarArray(&dir.fX, 3)) { | |
612 return nullptr; | |
613 } | |
614 | |
615 sk_sp<SkImage> depthMap; | |
616 if (!(depthMap = sk_ref_sp<SkImage>(buf.readImage()))) { | |
617 return nullptr; | |
618 } | |
619 | |
620 SkLights::Light light = SkLights::Light(color, dir); | |
621 light.setShadowMap(depthMap); | |
622 | |
623 builder.add(light); | |
624 } | |
625 } | |
626 | |
627 sk_sp<SkLights> lights(builder.finish()); | |
628 | 596 |
629 int diffuseWidth = buf.readInt(); | 597 int diffuseWidth = buf.readInt(); |
630 int diffuseHeight = buf.readInt(); | 598 int diffuseHeight = buf.readInt(); |
631 | 599 |
632 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); | 600 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
633 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); | 601 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); |
634 | 602 |
635 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 603 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
636 std::move(diffuseShader), | 604 std::move(diffuseShader), |
637 std::move(lights), | 605 std::move(lights), |
638 diffuseWidth, diffuseHeight); | 606 diffuseWidth, diffuseHeight); |
639 } | 607 } |
640 | 608 |
641 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { | 609 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { |
642 this->INHERITED::flatten(buf); | 610 this->INHERITED::flatten(buf); |
643 | 611 |
644 buf.writeInt(fLights->numLights()); | 612 fLights->flatten(buf); |
645 | |
646 for (int l = 0; l < fLights->numLights(); ++l) { | |
647 const SkLights::Light& light = fLights->light(l); | |
648 | |
649 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | |
650 | |
651 buf.writeBool(isAmbient); | |
652 buf.writeScalarArray(&light.color().fX, 3); | |
653 if (!isAmbient) { | |
654 buf.writeScalarArray(&light.dir().fX, 3); | |
655 } | |
656 | |
657 buf.writeImage(light.getShadowMap()); | |
658 } | |
659 | 613 |
660 buf.writeInt(fDiffuseWidth); | 614 buf.writeInt(fDiffuseWidth); |
661 buf.writeInt(fDiffuseHeight); | 615 buf.writeInt(fDiffuseHeight); |
662 | 616 |
663 buf.writeFlattenable(fPovDepthShader.get()); | 617 buf.writeFlattenable(fPovDepthShader.get()); |
664 buf.writeFlattenable(fDiffuseShader.get()); | 618 buf.writeFlattenable(fDiffuseShader.get()); |
665 } | 619 } |
666 | 620 |
667 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | 621 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
668 return sizeof(ShadowShaderContext); | 622 return sizeof(ShadowShaderContext); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 | 671 |
718 /////////////////////////////////////////////////////////////////////////////// | 672 /////////////////////////////////////////////////////////////////////////////// |
719 | 673 |
720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 674 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 675 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 676 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
723 | 677 |
724 /////////////////////////////////////////////////////////////////////////////// | 678 /////////////////////////////////////////////////////////////////////////////// |
725 | 679 |
726 #endif | 680 #endif |
OLD | NEW |