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