OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
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 "SkLightingImageFilter.h" | 8 #include "SkLightingImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 #else | 500 #else |
501 | 501 |
502 class GrGLLight; | 502 class GrGLLight; |
503 | 503 |
504 #endif | 504 #endif |
505 | 505 |
506 }; | 506 }; |
507 | 507 |
508 /////////////////////////////////////////////////////////////////////////////// | 508 /////////////////////////////////////////////////////////////////////////////// |
509 | 509 |
510 class SkLight : public SkFlattenable { | 510 class SkLight : public SkRefCnt { |
511 public: | 511 public: |
512 SK_DECLARE_INST_COUNT(SkLight) | 512 SK_DECLARE_INST_COUNT(SkLight) |
513 | 513 |
514 enum LightType { | 514 enum LightType { |
515 kDistant_LightType, | 515 kDistant_LightType, |
516 kPoint_LightType, | 516 kPoint_LightType, |
517 kSpot_LightType, | 517 kSpot_LightType, |
518 }; | 518 }; |
519 virtual LightType type() const = 0; | 519 virtual LightType type() const = 0; |
520 const SkPoint3& color() const { return fColor; } | 520 const SkPoint3& color() const { return fColor; } |
521 virtual GrGLLight* createGLLight() const = 0; | 521 virtual GrGLLight* createGLLight() const = 0; |
522 virtual bool isEqual(const SkLight& other) const { | 522 virtual bool isEqual(const SkLight& other) const { |
523 return fColor == other.fColor; | 523 return fColor == other.fColor; |
524 } | 524 } |
525 // Called to know whether the generated GrGLLight will require access to the
fragment position. | 525 // Called to know whether the generated GrGLLight will require access to the
fragment position. |
526 virtual bool requiresFragmentPosition() const = 0; | 526 virtual bool requiresFragmentPosition() const = 0; |
527 virtual SkLight* transform(const SkMatrix& matrix) const = 0; | 527 virtual SkLight* transform(const SkMatrix& matrix) const = 0; |
528 | 528 |
| 529 // Defined below SkLight's subclasses. |
| 530 void flattenLight(SkFlattenableWriteBuffer& buffer) const; |
| 531 static SkLight* UnflattenLight(SkFlattenableReadBuffer& buffer); |
| 532 |
529 protected: | 533 protected: |
530 SkLight(SkColor color) | 534 SkLight(SkColor color) |
531 : fColor(SkIntToScalar(SkColorGetR(color)), | 535 : fColor(SkIntToScalar(SkColorGetR(color)), |
532 SkIntToScalar(SkColorGetG(color)), | 536 SkIntToScalar(SkColorGetG(color)), |
533 SkIntToScalar(SkColorGetB(color))) {} | 537 SkIntToScalar(SkColorGetB(color))) {} |
534 SkLight(const SkPoint3& color) | 538 SkLight(const SkPoint3& color) |
535 : fColor(color) {} | 539 : fColor(color) {} |
536 SkLight(SkFlattenableReadBuffer& buffer) | 540 SkLight(SkFlattenableReadBuffer& buffer) { |
537 : INHERITED(buffer) { | |
538 fColor = readPoint3(buffer); | 541 fColor = readPoint3(buffer); |
539 } | 542 } |
540 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { | 543 |
541 INHERITED::flatten(buffer); | 544 virtual void onFlattenLight(SkFlattenableWriteBuffer& buffer) const = 0; |
542 writePoint3(fColor, buffer); | 545 |
543 } | |
544 | 546 |
545 private: | 547 private: |
546 typedef SkFlattenable INHERITED; | 548 typedef SkRefCnt INHERITED; |
547 SkPoint3 fColor; | 549 SkPoint3 fColor; |
548 }; | 550 }; |
549 | 551 |
550 SK_DEFINE_INST_COUNT(SkLight) | 552 SK_DEFINE_INST_COUNT(SkLight) |
551 | 553 |
552 /////////////////////////////////////////////////////////////////////////////// | 554 /////////////////////////////////////////////////////////////////////////////// |
553 | 555 |
554 class SkDistantLight : public SkLight { | 556 class SkDistantLight : public SkLight { |
555 public: | 557 public: |
556 SkDistantLight(const SkPoint3& direction, SkColor color) | 558 SkDistantLight(const SkPoint3& direction, SkColor color) |
(...skipping 19 matching lines...) Expand all Loading... |
576 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { | 578 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
577 if (other.type() != kDistant_LightType) { | 579 if (other.type() != kDistant_LightType) { |
578 return false; | 580 return false; |
579 } | 581 } |
580 | 582 |
581 const SkDistantLight& o = static_cast<const SkDistantLight&>(other); | 583 const SkDistantLight& o = static_cast<const SkDistantLight&>(other); |
582 return INHERITED::isEqual(other) && | 584 return INHERITED::isEqual(other) && |
583 fDirection == o.fDirection; | 585 fDirection == o.fDirection; |
584 } | 586 } |
585 | 587 |
586 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDistantLight) | 588 SkDistantLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
| 589 fDirection = readPoint3(buffer); |
| 590 } |
587 | 591 |
588 protected: | 592 protected: |
589 SkDistantLight(const SkPoint3& direction, const SkPoint3& color) | 593 SkDistantLight(const SkPoint3& direction, const SkPoint3& color) |
590 : INHERITED(color), fDirection(direction) { | 594 : INHERITED(color), fDirection(direction) { |
591 } | 595 } |
592 SkDistantLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | |
593 fDirection = readPoint3(buffer); | |
594 } | |
595 virtual SkLight* transform(const SkMatrix& matrix) const { | 596 virtual SkLight* transform(const SkMatrix& matrix) const { |
596 return new SkDistantLight(direction(), color()); | 597 return new SkDistantLight(direction(), color()); |
597 } | 598 } |
598 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 599 virtual void onFlattenLight(SkFlattenableWriteBuffer& buffer) const SK_OVERR
IDE { |
599 INHERITED::flatten(buffer); | |
600 writePoint3(fDirection, buffer); | 600 writePoint3(fDirection, buffer); |
601 } | 601 } |
602 | 602 |
603 private: | 603 private: |
604 typedef SkLight INHERITED; | 604 typedef SkLight INHERITED; |
605 SkPoint3 fDirection; | 605 SkPoint3 fDirection; |
606 }; | 606 }; |
607 | 607 |
608 /////////////////////////////////////////////////////////////////////////////// | 608 /////////////////////////////////////////////////////////////////////////////// |
609 | 609 |
(...skipping 29 matching lines...) Expand all Loading... |
639 return INHERITED::isEqual(other) && | 639 return INHERITED::isEqual(other) && |
640 fLocation == o.fLocation; | 640 fLocation == o.fLocation; |
641 } | 641 } |
642 virtual SkLight* transform(const SkMatrix& matrix) const { | 642 virtual SkLight* transform(const SkMatrix& matrix) const { |
643 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); | 643 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
644 matrix.mapPoints(&location2, 1); | 644 matrix.mapPoints(&location2, 1); |
645 SkPoint3 location(location2.fX, location2.fY, fLocation.fZ); | 645 SkPoint3 location(location2.fX, location2.fY, fLocation.fZ); |
646 return new SkPointLight(location, color()); | 646 return new SkPointLight(location, color()); |
647 } | 647 } |
648 | 648 |
649 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPointLight) | |
650 | |
651 protected: | |
652 SkPointLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | 649 SkPointLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
653 fLocation = readPoint3(buffer); | 650 fLocation = readPoint3(buffer); |
654 } | 651 } |
| 652 |
| 653 protected: |
655 SkPointLight(const SkPoint3& location, const SkPoint3& color) | 654 SkPointLight(const SkPoint3& location, const SkPoint3& color) |
656 : INHERITED(color), fLocation(location) {} | 655 : INHERITED(color), fLocation(location) {} |
657 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 656 virtual void onFlattenLight(SkFlattenableWriteBuffer& buffer) const SK_OVERR
IDE { |
658 INHERITED::flatten(buffer); | |
659 writePoint3(fLocation, buffer); | 657 writePoint3(fLocation, buffer); |
660 } | 658 } |
661 | 659 |
662 private: | 660 private: |
663 typedef SkLight INHERITED; | 661 typedef SkLight INHERITED; |
664 SkPoint3 fLocation; | 662 SkPoint3 fLocation; |
665 }; | 663 }; |
666 | 664 |
667 /////////////////////////////////////////////////////////////////////////////// | 665 /////////////////////////////////////////////////////////////////////////////// |
668 | 666 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } | 720 virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } |
723 virtual LightType type() const { return kSpot_LightType; } | 721 virtual LightType type() const { return kSpot_LightType; } |
724 const SkPoint3& location() const { return fLocation; } | 722 const SkPoint3& location() const { return fLocation; } |
725 const SkPoint3& target() const { return fTarget; } | 723 const SkPoint3& target() const { return fTarget; } |
726 SkScalar specularExponent() const { return fSpecularExponent; } | 724 SkScalar specularExponent() const { return fSpecularExponent; } |
727 SkScalar cosInnerConeAngle() const { return fCosInnerConeAngle; } | 725 SkScalar cosInnerConeAngle() const { return fCosInnerConeAngle; } |
728 SkScalar cosOuterConeAngle() const { return fCosOuterConeAngle; } | 726 SkScalar cosOuterConeAngle() const { return fCosOuterConeAngle; } |
729 SkScalar coneScale() const { return fConeScale; } | 727 SkScalar coneScale() const { return fConeScale; } |
730 const SkPoint3& s() const { return fS; } | 728 const SkPoint3& s() const { return fS; } |
731 | 729 |
732 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpotLight) | |
733 | |
734 protected: | |
735 SkSpotLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | 730 SkSpotLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
736 fLocation = readPoint3(buffer); | 731 fLocation = readPoint3(buffer); |
737 fTarget = readPoint3(buffer); | 732 fTarget = readPoint3(buffer); |
738 fSpecularExponent = buffer.readScalar(); | 733 fSpecularExponent = buffer.readScalar(); |
739 fCosOuterConeAngle = buffer.readScalar(); | 734 fCosOuterConeAngle = buffer.readScalar(); |
740 fCosInnerConeAngle = buffer.readScalar(); | 735 fCosInnerConeAngle = buffer.readScalar(); |
741 fConeScale = buffer.readScalar(); | 736 fConeScale = buffer.readScalar(); |
742 fS = readPoint3(buffer); | 737 fS = readPoint3(buffer); |
743 } | 738 } |
| 739 protected: |
744 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu
larExponent, SkScalar cosOuterConeAngle, SkScalar cosInnerConeAngle, SkScalar co
neScale, const SkPoint3& s, const SkPoint3& color) | 740 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu
larExponent, SkScalar cosOuterConeAngle, SkScalar cosInnerConeAngle, SkScalar co
neScale, const SkPoint3& s, const SkPoint3& color) |
745 : INHERITED(color), | 741 : INHERITED(color), |
746 fLocation(location), | 742 fLocation(location), |
747 fTarget(target), | 743 fTarget(target), |
748 fSpecularExponent(specularExponent), | 744 fSpecularExponent(specularExponent), |
749 fCosOuterConeAngle(cosOuterConeAngle), | 745 fCosOuterConeAngle(cosOuterConeAngle), |
750 fCosInnerConeAngle(cosInnerConeAngle), | 746 fCosInnerConeAngle(cosInnerConeAngle), |
751 fConeScale(coneScale), | 747 fConeScale(coneScale), |
752 fS(s) | 748 fS(s) |
753 { | 749 { |
754 } | 750 } |
755 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 751 virtual void onFlattenLight(SkFlattenableWriteBuffer& buffer) const SK_OVERR
IDE { |
756 INHERITED::flatten(buffer); | |
757 writePoint3(fLocation, buffer); | 752 writePoint3(fLocation, buffer); |
758 writePoint3(fTarget, buffer); | 753 writePoint3(fTarget, buffer); |
759 buffer.writeScalar(fSpecularExponent); | 754 buffer.writeScalar(fSpecularExponent); |
760 buffer.writeScalar(fCosOuterConeAngle); | 755 buffer.writeScalar(fCosOuterConeAngle); |
761 buffer.writeScalar(fCosInnerConeAngle); | 756 buffer.writeScalar(fCosInnerConeAngle); |
762 buffer.writeScalar(fConeScale); | 757 buffer.writeScalar(fConeScale); |
763 writePoint3(fS, buffer); | 758 writePoint3(fS, buffer); |
764 } | 759 } |
765 | 760 |
766 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { | 761 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
(...skipping 23 matching lines...) Expand all Loading... |
790 SkPoint3 fS; | 785 SkPoint3 fS; |
791 }; | 786 }; |
792 | 787 |
793 // According to the spec, the specular term should be in the range [1, 128] : | 788 // According to the spec, the specular term should be in the range [1, 128] : |
794 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttri
bute | 789 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttri
bute |
795 const SkScalar SkSpotLight::kSpecularExponentMin = SkFloatToScalar(1.0f); | 790 const SkScalar SkSpotLight::kSpecularExponentMin = SkFloatToScalar(1.0f); |
796 const SkScalar SkSpotLight::kSpecularExponentMax = SkFloatToScalar(128.0f); | 791 const SkScalar SkSpotLight::kSpecularExponentMax = SkFloatToScalar(128.0f); |
797 | 792 |
798 /////////////////////////////////////////////////////////////////////////////// | 793 /////////////////////////////////////////////////////////////////////////////// |
799 | 794 |
| 795 void SkLight::flattenLight(SkFlattenableWriteBuffer& buffer) const { |
| 796 // Write type first, then baseclass, then subclass. |
| 797 buffer.writeInt(this->type()); |
| 798 writePoint3(fColor, buffer); |
| 799 this->onFlattenLight(buffer); |
| 800 } |
| 801 |
| 802 /*static*/ SkLight* SkLight::UnflattenLight(SkFlattenableReadBuffer& buffer) { |
| 803 // Read type first. |
| 804 const SkLight::LightType type = (SkLight::LightType)buffer.readInt(); |
| 805 switch (type) { |
| 806 // Each of these constructors must first call SkLight's, so we'll read t
he baseclass |
| 807 // then subclass, same order as flattenLight. |
| 808 case SkLight::kDistant_LightType: return SkNEW_ARGS(SkDistantLight, (buf
fer)); |
| 809 case SkLight::kPoint_LightType: return SkNEW_ARGS(SkPointLight, (buffe
r)); |
| 810 case SkLight::kSpot_LightType: return SkNEW_ARGS(SkSpotLight, (buffer
)); |
| 811 default: |
| 812 SkDEBUGFAIL("Unknown LightType."); |
| 813 return NULL; |
| 814 } |
| 815 } |
| 816 /////////////////////////////////////////////////////////////////////////////// |
| 817 |
800 SkLightingImageFilter::SkLightingImageFilter(SkLight* light, SkScalar surfaceSca
le, SkImageFilter* input, const CropRect* cropRect) | 818 SkLightingImageFilter::SkLightingImageFilter(SkLight* light, SkScalar surfaceSca
le, SkImageFilter* input, const CropRect* cropRect) |
801 : INHERITED(input, cropRect), | 819 : INHERITED(input, cropRect), |
802 fLight(light), | 820 fLight(light), |
803 fSurfaceScale(SkScalarDiv(surfaceScale, SkIntToScalar(255))) | 821 fSurfaceScale(SkScalarDiv(surfaceScale, SkIntToScalar(255))) |
804 { | 822 { |
805 SkASSERT(fLight); | 823 SkASSERT(fLight); |
806 // our caller knows that we take ownership of the light, so we don't | 824 // our caller knows that we take ownership of the light, so we don't |
807 // need to call ref() here. | 825 // need to call ref() here. |
808 } | 826 } |
809 | 827 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 return SkNEW_ARGS(SkSpecularLightingImageFilter, | 876 return SkNEW_ARGS(SkSpecularLightingImageFilter, |
859 (SkNEW_ARGS(SkSpotLight, (location, target, specularExponent, cutoffAngl
e, lightColor)), | 877 (SkNEW_ARGS(SkSpotLight, (location, target, specularExponent, cutoffAngl
e, lightColor)), |
860 surfaceScale, ks, shininess, input, cropRect)); | 878 surfaceScale, ks, shininess, input, cropRect)); |
861 } | 879 } |
862 | 880 |
863 SkLightingImageFilter::~SkLightingImageFilter() { | 881 SkLightingImageFilter::~SkLightingImageFilter() { |
864 fLight->unref(); | 882 fLight->unref(); |
865 } | 883 } |
866 | 884 |
867 SkLightingImageFilter::SkLightingImageFilter(SkFlattenableReadBuffer& buffer) | 885 SkLightingImageFilter::SkLightingImageFilter(SkFlattenableReadBuffer& buffer) |
868 : INHERITED(buffer) | 886 : INHERITED(buffer) { |
869 { | 887 fLight = SkLight::UnflattenLight(buffer); |
870 fLight = buffer.readFlattenableT<SkLight>(); | |
871 fSurfaceScale = buffer.readScalar(); | 888 fSurfaceScale = buffer.readScalar(); |
872 } | 889 } |
873 | 890 |
874 void SkLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { | 891 void SkLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
875 this->INHERITED::flatten(buffer); | 892 this->INHERITED::flatten(buffer); |
876 buffer.writeFlattenable(fLight); | 893 fLight->flattenLight(buffer); |
877 buffer.writeScalar(fSurfaceScale); | 894 buffer.writeScalar(fSurfaceScale); |
878 } | 895 } |
879 | 896 |
880 /////////////////////////////////////////////////////////////////////////////// | 897 /////////////////////////////////////////////////////////////////////////////// |
881 | 898 |
882 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkLight* light, SkSca
lar surfaceScale, SkScalar kd, SkImageFilter* input, const CropRect* cropRect =
NULL) | 899 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkLight* light, SkSca
lar surfaceScale, SkScalar kd, SkImageFilter* input, const CropRect* cropRect =
NULL) |
883 : SkLightingImageFilter(light, surfaceScale, input, cropRect), | 900 : SkLightingImageFilter(light, surfaceScale, input, cropRect), |
884 fKD(kd) | 901 fKD(kd) |
885 { | 902 { |
886 } | 903 } |
(...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1555 &fLightColorFunc); | 1572 &fLightColorFunc); |
1556 | 1573 |
1557 builder->fsCodeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1574 builder->fsCodeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
1558 } | 1575 } |
1559 | 1576 |
1560 #endif | 1577 #endif |
1561 | 1578 |
1562 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1579 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
1563 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1580 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
1564 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1581 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
1565 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDistantLight) | |
1566 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPointLight) | |
1567 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotLight) | |
1568 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1582 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |