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::kTwo_ColorType == getColorType(key)){ // 3 Colo r 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 } | |
838 } | 855 } |
839 | 856 |
840 void GrGLGradientEffect::setData(const GrGLUniformManager& uman, | 857 void GrGLGradientEffect::setData(const GrGLUniformManager& uman, |
841 const GrDrawEffect& drawEffect) { | 858 const GrDrawEffect& drawEffect) { |
859 | |
842 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); | 860 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
843 const GrTexture* texture = e.texture(0); | |
844 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); | |
845 | 861 |
846 SkScalar yCoord = e.getYCoord(); | 862 |
847 if (yCoord != fCachedYCoord) { | 863 if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ |
848 uman.set1f(fFSYUni, yCoord); | 864 uman.set4f(fColorStartUni, 1.f, 1.f, 1.f, 1.f); |
849 fCachedYCoord = yCoord; | 865 uman.set4f(fColorEndUni, 0.f, 0.f, 0.f, 1.f); |
866 | |
867 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ | |
868 uman.set4f(fColorStartUni, 1.f, 1.f, 1.f, 1.f); | |
869 uman.set4f(fColorMidUni, 1.f, 0.f, 0.f, 0.f); | |
870 uman.set4f(fColorEndUni, 1.f, 1.f, 1.f, 1.f); | |
871 } else { | |
872 const GrTexture* texture = e.texture(0); | |
873 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture); | |
874 | |
875 SkScalar yCoord = e.getYCoord(); | |
876 if (yCoord != fCachedYCoord) { | |
877 uman.set1f(fFSYUni, yCoord); | |
878 fCachedYCoord = yCoord; | |
879 } | |
850 } | 880 } |
851 } | 881 } |
852 | 882 |
853 GrGLEffect::EffectKey GrGLGradientEffect::GenMatrixKey(const GrDrawEffect& drawE ffect) { | 883 |
884 GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { | |
854 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); | 885 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); |
855 const GrTexture* texture = e.texture(0); | 886 const GrTexture* texture = NULL; |
856 return GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsType, text ure); | 887 |
888 if (e.getNumColors() > 3){ | |
889 texture = e.texture(0); | |
890 } | |
891 | |
892 EffectKey key = GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsT ype, texture); | |
893 | |
894 if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { | |
895 key = key | kTwoColorKey; | |
896 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ | |
897 key = key | kThreeColorKey; | |
898 } | |
899 | |
900 return key; | |
857 } | 901 } |
858 | 902 |
859 void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, | 903 void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder, |
860 EffectKey key, | 904 EffectKey key, |
861 const char** fsCoordName, | 905 const char** fsCoordName, |
862 const char** vsVaryingName, | 906 const char** vsVaryingName, |
863 GrSLType* vsVaryingType) { | 907 GrSLType* vsVaryingType) { |
864 fEffectMatrix.emitCodeMakeFSCoords2D(builder, | 908 fEffectMatrix.emitCodeMakeFSCoords2D(builder, |
865 key & kMatrixKeyMask, | 909 key & kMatrixKeyMask, |
866 fsCoordName, | 910 fsCoordName, |
867 vsVaryingName, | 911 vsVaryingName, |
868 vsVaryingType); | 912 vsVaryingType); |
869 } | 913 } |
870 | 914 |
871 void GrGLGradientEffect::emitColorLookup(GrGLShaderBuilder* builder, | 915 void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, |
872 const char* gradientTValue, | 916 const char* gradientTValue, |
873 const char* outputColor, | 917 EffectKey key, |
874 const char* inputColor, | 918 const char* outputColor, |
875 const GrGLShaderBuilder::TextureSampler & sampler) { | 919 const char* inputColor, |
876 | 920 const GrGLShaderBuilder::TextureSamplerArray& samplers) { |
877 builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", | 921 if (GrGradientEffect::kTwo_ColorType == getColorType(key)){ |
878 gradientTValue, | 922 builder->fsCodeAppendf("\t%s = mix(%s, %s, %s);\n", |
879 builder->getUniformVariable(fFSYUni).c_str()); | 923 outputColor, |
880 builder->fsCodeAppendf("\t%s = ", outputColor); | 924 builder->getUniformVariable(fColorStartUni).c_str (), |
881 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderT ype, | 925 builder->getUniformVariable(fColorEndUni).c_str() , |
882 inputColor, | 926 gradientTValue); |
883 sampler, | 927 } else if (GrGradientEffect::kThree_ColorType == getColorType(key)){ |
884 "coord"); | 928 //same as 2 color for now... causes bug from unused UNI |
dierk
2013/08/13 19:50:18
How to mix for 3 colors on the GPU? Is there a way
bsalomon
2013/08/13 20:40:03
I think this works, but not sure if it's faster th
| |
885 builder->fsCodeAppend(";\n"); | 929 builder->fsCodeAppendf("\t%s = mix(%s, %s, %s);\n", |
930 outputColor, | |
931 builder->getUniformVariable(fColorStartUni).c_str (), | |
932 builder->getUniformVariable(fColorEndUni).c_str() , | |
933 gradientTValue); | |
934 } else { | |
935 builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", | |
936 gradientTValue, | |
937 builder->getUniformVariable(fFSYUni).c_str()); | |
938 builder->fsCodeAppendf("\t%s = ", outputColor); | |
939 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha derType, | |
940 inputColor, | |
941 samplers[0], | |
942 "coord"); | |
943 builder->fsCodeAppend(";\n"); | |
944 } | |
886 } | 945 } |
887 | 946 |
888 ///////////////////////////////////////////////////////////////////// | 947 ///////////////////////////////////////////////////////////////////// |
889 | 948 |
890 GrGradientEffect::GrGradientEffect(GrContext* ctx, | 949 GrGradientEffect::GrGradientEffect(GrContext* ctx, |
891 const SkGradientShaderBase& shader, | 950 const SkGradientShaderBase& shader, |
892 const SkMatrix& matrix, | 951 const SkMatrix& matrix, |
893 SkShader::TileMode tileMode) { | 952 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 | 953 |
899 fMatrix = matrix; | 954 fMatrix = matrix; |
900 | 955 |
901 SkBitmap bitmap; | 956 SkBitmap bitmap; |
902 shader.getGradientTableBitmap(&bitmap); | 957 shader.getGradientTableBitmap(&bitmap); |
903 | 958 |
904 fIsOpaque = shader.isOpaque(); | 959 fIsOpaque = shader.isOpaque(); |
905 | 960 |
906 GrTextureStripAtlas::Desc desc; | 961 // TODO: check for simple cases where we don't need a texture: |
907 desc.fWidth = bitmap.width(); | 962 SkShader::GradientInfo info; |
908 desc.fHeight = 32; | 963 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 GrAssert(NULL != fAtlas); | |
914 | 964 |
915 // We always filter the gradient table. Each table is one row of a texture, so always y-clamp. | 965 info.fColorCount = 3; |
916 GrTextureParams params; | 966 info.fColors = NULL; |
917 params.setFilterMode(GrTextureParams::kBilerp_FilterMode); | 967 info.fColorOffsets = &pos[0]; |
918 params.setTileModeX(tileMode); | 968 shader.asAGradient(&info); |
919 | 969 |
920 fRow = fAtlas->lockRow(bitmap); | 970 if (2 == info.fColorCount) { // if 2 color |
921 if (-1 != fRow) { | 971 fRow = -1; |
922 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * | 972 fNumColors = 2; |
923 fAtlas->getVerticalScaleFactor(); | 973 |
924 fTextureAccess.reset(fAtlas->getTexture(), params); | 974 } else if (3 == info.fColorCount && (SkScalarAbs(pos[1] - 0.5) < 0.01)) { // if 3 color symmetric |
975 fRow = -1; | |
976 fNumColors = 3; | |
977 | |
925 } else { | 978 } else { |
926 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶m s); | 979 fNumColors = 4; // if anything else use 4 as code for use texture instea d |
927 fTextureAccess.reset(texture, params); | 980 |
928 fYCoord = SK_ScalarHalf; | 981 GrTextureStripAtlas::Desc desc; |
982 desc.fWidth = bitmap.width(); | |
983 desc.fHeight = 32; | |
984 desc.fRowHeight = bitmap.height(); | |
985 desc.fContext = ctx; | |
986 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); | |
987 fAtlas = GrTextureStripAtlas::GetAtlas(desc); | |
988 GrAssert(NULL != fAtlas); | |
929 | 989 |
930 // Unlock immediately, this is not great, but we don't have a way of | 990 // We always filter the gradient table. Each table is one row of a textu re, so always y-clamp. |
931 // knowing when else to unlock it currently, so it may get purged from | 991 GrTextureParams params; |
932 // the cache, but it'll still be ref'd until it's no longer being used. | 992 params.setFilterMode(GrTextureParams::kBilerp_FilterMode); |
933 GrUnlockAndUnrefCachedBitmapTexture(texture); | 993 params.setTileModeX(tileMode); |
994 | |
995 fRow = fAtlas->lockRow(bitmap); | |
996 if (-1 != fRow) { | |
997 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * | |
998 fAtlas->getVerticalScaleFactor(); | |
999 fTextureAccess.reset(fAtlas->getTexture(), params); | |
1000 } else { | |
1001 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &p arams); | |
1002 fTextureAccess.reset(texture, params); | |
1003 fYCoord = SK_ScalarHalf; | |
1004 | |
1005 // Unlock immediately, this is not great, but we don't have a way of | |
1006 // knowing when else to unlock it currently, so it may get purged fr om | |
1007 // the cache, but it'll still be ref'd until it's no longer being us ed. | |
1008 GrUnlockAndUnrefCachedBitmapTexture(texture); | |
1009 } | |
1010 this->addTextureAccess(&fTextureAccess); | |
934 } | 1011 } |
935 this->addTextureAccess(&fTextureAccess); | |
936 } | 1012 } |
937 | 1013 |
938 GrGradientEffect::~GrGradientEffect() { | 1014 GrGradientEffect::~GrGradientEffect() { |
939 if (this->useAtlas()) { | 1015 if (this->useAtlas()) { |
940 fAtlas->unlockRow(fRow); | 1016 fAtlas->unlockRow(fRow); |
941 } | 1017 } |
942 } | 1018 } |
943 | 1019 |
944 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { | 1020 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { |
945 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); | 1021 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); |
946 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && | 1022 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && |
947 fTextureAccess.getParams().getTileModeX() == | 1023 fTextureAccess.getParams().getTileModeX() == |
948 s.fTextureAccess.getParams().getTileModeX() && | 1024 s.fTextureAccess.getParams().getTileModeX() && |
949 this->useAtlas() == s.useAtlas() && | 1025 this->useAtlas() == s.useAtlas() && |
950 fYCoord == s.getYCoord() && | 1026 fYCoord == s.getYCoord() && |
951 fMatrix.cheapEqualTo(s.getMatrix()); | 1027 fMatrix.cheapEqualTo(s.getMatrix()) |
1028 && this->fNumColors == s.fNumColors; | |
1029 // also check for colors of 2 / 3 gradients! | |
952 } | 1030 } |
953 | 1031 |
954 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const { | 1032 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const { |
955 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) { | 1033 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) { |
956 *validFlags = kA_GrColorComponentFlag; | 1034 *validFlags = kA_GrColorComponentFlag; |
957 } else { | 1035 } else { |
958 *validFlags = 0; | 1036 *validFlags = 0; |
959 } | 1037 } |
960 } | 1038 } |
961 | 1039 |
(...skipping 15 matching lines...) Expand all Loading... | |
977 (*stops)[i] = stop; | 1055 (*stops)[i] = stop; |
978 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; | 1056 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; |
979 } | 1057 } |
980 } | 1058 } |
981 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); | 1059 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); |
982 | 1060 |
983 return outColors; | 1061 return outColors; |
984 } | 1062 } |
985 | 1063 |
986 #endif | 1064 #endif |
OLD | NEW |