OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 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 "SkGradientShaderPriv.h" | 8 #include "SkGradientShaderPriv.h" |
9 #include "SkLinearGradient.h" | 9 #include "SkLinearGradient.h" |
10 #include "SkRadialGradient.h" | 10 #include "SkRadialGradient.h" |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 | 825 |
826 GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) | 826 GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) |
827 : INHERITED(factory) | 827 : INHERITED(factory) |
828 , fCachedYCoord(SK_ScalarMax) | 828 , fCachedYCoord(SK_ScalarMax) |
829 , fFSYUni(GrGLUniformManager::kInvalidUniformHandle) | 829 , fFSYUni(GrGLUniformManager::kInvalidUniformHandle) |
830 , fEffectMatrix(kCoordsType) { | 830 , fEffectMatrix(kCoordsType) { |
831 } | 831 } |
832 | 832 |
833 GrGLGradientEffect::~GrGLGradientEffect() { } | 833 GrGLGradientEffect::~GrGLGradientEffect() { } |
834 | 834 |
835 void GrGLGradientEffect::emitYCoordUniform(GrGLShaderBuilder* builder) { | 835 void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { |
836 fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 836 |
837 kFloat_GrSLType, "GradientYCoordFS"); | 837 if (GrGradientEffect::kTwo_ColorType == getColorType(key)) { // 2 Color case |
838 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader Type, | |
839 kVec4f_GrSLType, "GradientStartColo r"); | |
840 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderTy pe, | |
841 kVec4f_GrSLType, "GradientEndColor"); | |
842 | |
843 } else if (GrGradientEffect::kThree_ColorType == getColorType(key)){ // 3 Co lor Case | |
844 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Shader Type, | |
845 kVec4f_GrSLType, "GradientStartColo r"); | |
846 fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderTy pe, | |
847 kVec4f_GrSLType, "GradientMidColor"); | |
848 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderTy pe, | |
849 kVec4f_GrSLType, "GradientEndColor" ); | |
850 | |
851 } else { // if not a fast case | |
852 fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | |
853 kFloat_GrSLType, "GradientYCoordFS"); | |
854 } | |
855 } | |
856 | |
857 static void setColorUni(const GrGLUniformManager& uman, | |
858 const GrGLUniformManager::UniformHandle uni, | |
859 const SkColor color){ | |
860 uman.set4f(uni, | |
861 SkColorGetR(color)/255.f, | |
862 SkColorGetG(color)/255.f, | |
863 SkColorGetB(color)/255.f, | |
864 SkColorGetA(color)/255.f | |
865 ); | |
838 } | 866 } |
839 | 867 |
840 void GrGLGradientEffect::setData(const GrGLUniformManager& uman, | 868 void GrGLGradientEffect::setData(const GrGLUniformManager& uman, |
841 const GrDrawEffect& drawEffect) { | 869 const GrDrawEffect& drawEffect) { |
870 | |
842 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); | 871 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
843 const GrTexture* texture = e.texture(0); | |
844 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); | |
845 | 872 |
846 SkScalar yCoord = e.getYCoord(); | 873 |
847 if (yCoord != fCachedYCoord) { | 874 if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ |
848 uman.set1f(fFSYUni, yCoord); | 875 |
849 fCachedYCoord = yCoord; | 876 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, NULL); |
877 const SkColor* colors = e.getColors(); | |
878 setColorUni(uman, fColorStartUni, colors[0]); | |
879 setColorUni(uman, fColorEndUni, colors[1]); | |
880 | |
881 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ | |
882 | |
883 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, NULL); | |
884 const SkColor* colors = e.getColors(); | |
885 setColorUni(uman, fColorStartUni, colors[0]); | |
886 setColorUni(uman, fColorMidUni, colors[1]); | |
887 setColorUni(uman, fColorEndUni, colors[2]); | |
888 | |
889 } else { | |
890 | |
891 const GrTexture* texture = e.texture(0); | |
892 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); | |
893 | |
894 SkScalar yCoord = e.getYCoord(); | |
895 if (yCoord != fCachedYCoord) { | |
896 uman.set1f(fFSYUni, yCoord); | |
897 fCachedYCoord = yCoord; | |
898 } | |
850 } | 899 } |
851 } | 900 } |
852 | 901 |
853 GrGLEffect::EffectKey GrGLGradientEffect::GenMatrixKey(const GrDrawEffect& drawE ffect) { | 902 |
903 GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { | |
854 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); | 904 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
855 const GrTexture* texture = e.texture(0); | 905 const GrTexture* texture = NULL; |
856 return GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsType, text ure); | 906 |
907 if (GrGradientEffect::kTexture_ColorType == e.getColorType()){ | |
908 texture = e.texture(0); | |
909 } | |
910 | |
911 EffectKey key = GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsT ype, texture); | |
912 | |
913 if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { | |
914 key = key | kTwoColorKey; | |
915 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ | |
916 key = key | kThreeColorKey; | |
917 } | |
918 | |
919 return key; | |
857 } | 920 } |
858 | 921 |
859 void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, | 922 void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, |
860 EffectKey key, | 923 EffectKey key, |
861 const char** fsCoordName, | 924 const char** fsCoordName, |
862 const char** vsVaryingName, | 925 const char** vsVaryingName, |
863 GrSLType* vsVaryingType) { | 926 GrSLType* vsVaryingType) { |
864 fEffectMatrix.emitCodeMakeFSCoords2D(builder, | 927 fEffectMatrix.emitCodeMakeFSCoords2D(builder, |
865 key & kMatrixKeyMask, | 928 key & kMatrixKeyMask, |
866 fsCoordName, | 929 fsCoordName, |
867 vsVaryingName, | 930 vsVaryingName, |
868 vsVaryingType); | 931 vsVaryingType); |
869 } | 932 } |
870 | 933 |
871 void GrGLGradientEffect::emitColorLookup(GrGLShaderBuilder* builder, | 934 void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, |
872 const char* gradientTValue, | 935 const char* gradientTValue, |
873 const char* outputColor, | 936 EffectKey key, |
874 const char* inputColor, | 937 const char* outputColor, |
875 const GrGLShaderBuilder::TextureSampler & sampler) { | 938 const char* inputColor, |
939 const GrGLShaderBuilder::TextureSamplerArray& samplers) { | |
940 if (GrGradientEffect::kTwo_ColorType == getColorType(key)){ | |
941 | |
942 builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, %s);\n", | |
943 builder->getUniformVariable(fColorStartUni).c_str (), | |
944 builder->getUniformVariable(fColorEndUni).c_str() , | |
945 gradientTValue); | |
876 | 946 |
877 builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", | 947 SkString output, colorTemp; |
878 gradientTValue, | 948 colorTemp.append("colorTemp"); |
879 builder->getUniformVariable(fFSYUni).c_str()); | 949 builder->fsCodeAppendf("\t%s = ", outputColor); |
880 builder->fsCodeAppendf("\t%s = ", outputColor); | 950 GrGLSLModulatef<4>(&output, inputColor, colorTemp.c_str()); |
881 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderT ype, | 951 builder->fsCodeAppend(output.c_str()); |
882 inputColor, | 952 builder->fsCodeAppend(";\n"); |
883 sampler, | 953 |
884 "coord"); | 954 } else if (GrGradientEffect::kThree_ColorType == getColorType(key)){ |
885 builder->fsCodeAppend(";\n"); | 955 |
956 builder->fsCodeAppendf("\tfloat oneMinus2t = 1 - (2*%s);\n", | |
bsalomon
2013/08/15 13:16:42
Some GLSL compilers will give you heartache about
| |
957 gradientTValue); | |
958 builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0, 1) * %s +(1 - abs(oneMinus2t)) * %s + clamp(-oneMinus2t, 0, 1) * %s;\n", | |
959 builder->getUniformVariable(fColorStartUni).c_str (), | |
960 builder->getUniformVariable(fColorMidUni).c_str() , | |
961 builder->getUniformVariable(fColorEndUni).c_str() ); | |
962 | |
963 SkString output, colorTemp; | |
964 colorTemp.append("colorTemp"); | |
965 builder->fsCodeAppendf("\t%s = ", outputColor); | |
966 GrGLSLModulatef<4>(&output, inputColor, colorTemp.c_str()); | |
967 builder->fsCodeAppend(output.c_str()); | |
968 builder->fsCodeAppend(";\n"); | |
969 | |
970 } else { | |
971 builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", | |
972 gradientTValue, | |
973 builder->getUniformVariable(fFSYUni).c_str()); | |
974 builder->fsCodeAppendf("\t%s = ", outputColor); | |
975 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha derType, | |
976 inputColor, | |
977 samplers[0], | |
978 "coord"); | |
979 builder->fsCodeAppend(";\n"); | |
980 } | |
886 } | 981 } |
887 | 982 |
888 ///////////////////////////////////////////////////////////////////// | 983 ///////////////////////////////////////////////////////////////////// |
889 | 984 |
890 GrGradientEffect::GrGradientEffect(GrContext* ctx, | 985 GrGradientEffect::GrGradientEffect(GrContext* ctx, |
891 const SkGradientShaderBase& shader, | 986 const SkGradientShaderBase& shader, |
892 const SkMatrix& matrix, | 987 const SkMatrix& matrix, |
893 SkShader::TileMode tileMode) { | 988 SkShader::TileMode tileMode) { |
894 // TODO: check for simple cases where we don't need a texture: | |
895 //GradientInfo info; | |
896 //shader.asAGradient(&info); | |
897 //if (info.fColorCount == 2) { ... | |
898 | 989 |
899 fMatrix = matrix; | 990 fMatrix = matrix; |
900 | 991 |
901 SkBitmap bitmap; | 992 SkBitmap bitmap; |
902 shader.getGradientTableBitmap(&bitmap); | 993 shader.getGradientTableBitmap(&bitmap); |
903 | 994 |
904 fIsOpaque = shader.isOpaque(); | 995 fIsOpaque = shader.isOpaque(); |
905 | 996 |
906 GrTextureStripAtlas::Desc desc; | 997 // TODO: check for simple cases where we don't need a texture: |
907 desc.fWidth = bitmap.width(); | 998 SkShader::GradientInfo info; |
908 desc.fHeight = 32; | 999 SkScalar pos[3] = {0}; |
909 desc.fRowHeight = bitmap.height(); | |
910 desc.fContext = ctx; | |
911 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); | |
912 fAtlas = GrTextureStripAtlas::GetAtlas(desc); | |
913 SkASSERT(NULL != fAtlas); | |
914 | 1000 |
915 // We always filter the gradient table. Each table is one row of a texture, so always y-clamp. | 1001 info.fColorCount = 3; |
916 GrTextureParams params; | 1002 info.fColors = &fColors[0]; |
917 params.setFilterMode(GrTextureParams::kBilerp_FilterMode); | 1003 info.fColorOffsets = &pos[0]; |
918 params.setTileModeX(tileMode); | 1004 shader.asAGradient(&info); |
919 | 1005 |
920 fRow = fAtlas->lockRow(bitmap); | 1006 if (2 == info.fColorCount) { // if 2 color |
921 if (-1 != fRow) { | 1007 fRow = -1; // flag for no atlas |
922 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * | 1008 fColorType = kTwo_ColorType; |
923 fAtlas->getVerticalScaleFactor(); | 1009 } else if (3 == info.fColorCount && (SkScalarAbs(pos[1] - 0.5) < 0.01)) { // if 3 color symmetric |
bsalomon
2013/08/15 13:16:42
nit: line wrap
| |
924 fTextureAccess.reset(fAtlas->getTexture(), params); | 1010 fRow = -1; // flag for no atlas |
1011 fColorType = kThree_ColorType; | |
925 } else { | 1012 } else { |
926 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶m s); | 1013 fColorType = kTexture_ColorType; |
927 fTextureAccess.reset(texture, params); | 1014 |
928 fYCoord = SK_ScalarHalf; | 1015 GrTextureStripAtlas::Desc desc; |
1016 desc.fWidth = bitmap.width(); | |
1017 desc.fHeight = 32; | |
1018 desc.fRowHeight = bitmap.height(); | |
1019 desc.fContext = ctx; | |
1020 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); | |
1021 fAtlas = GrTextureStripAtlas::GetAtlas(desc); | |
1022 GrAssert(NULL != fAtlas); | |
929 | 1023 |
930 // Unlock immediately, this is not great, but we don't have a way of | 1024 // We always filter the gradient table. Each table is one row of a textu re, so always y-clamp. |
bsalomon
2013/08/15 13:16:42
nit: line wrap
| |
931 // knowing when else to unlock it currently, so it may get purged from | 1025 GrTextureParams params; |
932 // the cache, but it'll still be ref'd until it's no longer being used. | 1026 params.setFilterMode(GrTextureParams::kBilerp_FilterMode); |
933 GrUnlockAndUnrefCachedBitmapTexture(texture); | 1027 params.setTileModeX(tileMode); |
1028 | |
1029 fRow = fAtlas->lockRow(bitmap); | |
1030 if (-1 != fRow) { | |
1031 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * | |
1032 fAtlas->getVerticalScaleFactor(); | |
1033 fTextureAccess.reset(fAtlas->getTexture(), params); | |
1034 } else { | |
1035 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &p arams); | |
1036 fTextureAccess.reset(texture, params); | |
1037 fYCoord = SK_ScalarHalf; | |
1038 | |
1039 // Unlock immediately, this is not great, but we don't have a way of | |
1040 // knowing when else to unlock it currently, so it may get purged fr om | |
1041 // the cache, but it'll still be ref'd until it's no longer being us ed. | |
1042 GrUnlockAndUnrefCachedBitmapTexture(texture); | |
1043 } | |
1044 this->addTextureAccess(&fTextureAccess); | |
934 } | 1045 } |
935 this->addTextureAccess(&fTextureAccess); | |
936 } | 1046 } |
937 | 1047 |
938 GrGradientEffect::~GrGradientEffect() { | 1048 GrGradientEffect::~GrGradientEffect() { |
939 if (this->useAtlas()) { | 1049 if (this->useAtlas()) { |
940 fAtlas->unlockRow(fRow); | 1050 fAtlas->unlockRow(fRow); |
941 } | 1051 } |
942 } | 1052 } |
943 | 1053 |
944 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { | 1054 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { |
945 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); | 1055 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); |
946 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && | 1056 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && |
947 fTextureAccess.getParams().getTileModeX() == | 1057 fTextureAccess.getParams().getTileModeX() == |
948 s.fTextureAccess.getParams().getTileModeX() && | 1058 s.fTextureAccess.getParams().getTileModeX() && |
949 this->useAtlas() == s.useAtlas() && | 1059 this->useAtlas() == s.useAtlas() && |
950 fYCoord == s.getYCoord() && | 1060 fYCoord == s.getYCoord() && |
951 fMatrix.cheapEqualTo(s.getMatrix()); | 1061 fMatrix.cheapEqualTo(s.getMatrix()) |
1062 && this->fColorType == s.getColorType(); | |
1063 // also check for colors of 2 / 3 gradients! | |
952 } | 1064 } |
953 | 1065 |
954 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const { | 1066 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const { |
955 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) { | 1067 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) { |
956 *validFlags = kA_GrColorComponentFlag; | 1068 *validFlags = kA_GrColorComponentFlag; |
957 } else { | 1069 } else { |
958 *validFlags = 0; | 1070 *validFlags = 0; |
959 } | 1071 } |
960 } | 1072 } |
961 | 1073 |
(...skipping 15 matching lines...) Expand all Loading... | |
977 (*stops)[i] = stop; | 1089 (*stops)[i] = stop; |
978 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; | 1090 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; |
979 } | 1091 } |
980 } | 1092 } |
981 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); | 1093 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); |
982 | 1094 |
983 return outColors; | 1095 return outColors; |
984 } | 1096 } |
985 | 1097 |
986 #endif | 1098 #endif |
OLD | NEW |