Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: src/effects/gradients/SkGradientShader.cpp

Issue 24853002: Make GPU coord transforms automatic (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/effects/SkTableColorFilter.cpp ('k') | src/effects/gradients/SkGradientShaderPriv.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 807 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 /////////////////////////////////////////////////////////////////////////////// 818 ///////////////////////////////////////////////////////////////////////////////
819 819
820 #if SK_SUPPORT_GPU 820 #if SK_SUPPORT_GPU
821 821
822 #include "effects/GrTextureStripAtlas.h" 822 #include "effects/GrTextureStripAtlas.h"
823 #include "GrTBackendEffectFactory.h" 823 #include "GrTBackendEffectFactory.h"
824 #include "SkGr.h" 824 #include "SkGr.h"
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 , fEffectMatrix(kCoordsType) {
830 } 829 }
831 830
832 GrGLGradientEffect::~GrGLGradientEffect() { } 831 GrGLGradientEffect::~GrGLGradientEffect() { }
833 832
834 void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { 833 void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) {
835 834
836 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)) { // 2 Color case 835 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)) { // 2 Color case
837 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity, 836 fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi lity,
838 kVec4f_GrSLType, "GradientStartColo r"); 837 kVec4f_GrSLType, "GradientStartColo r");
839 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty, 838 fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili ty,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 } 875 }
877 876
878 void GrGLGradientEffect::setData(const GrGLUniformManager& uman, 877 void GrGLGradientEffect::setData(const GrGLUniformManager& uman,
879 const GrDrawEffect& drawEffect) { 878 const GrDrawEffect& drawEffect) {
880 879
881 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); 880 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
882 881
883 882
884 if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ 883 if (GrGradientEffect::kTwo_ColorType == e.getColorType()){
885 884
886 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, NULL);
887 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { 885 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
888 set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); 886 set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
889 set_mul_color_uni(uman, fColorEndUni, e.getColors(1)); 887 set_mul_color_uni(uman, fColorEndUni, e.getColors(1));
890 } else { 888 } else {
891 set_color_uni(uman, fColorStartUni, e.getColors(0)); 889 set_color_uni(uman, fColorStartUni, e.getColors(0));
892 set_color_uni(uman, fColorEndUni, e.getColors(1)); 890 set_color_uni(uman, fColorEndUni, e.getColors(1));
893 } 891 }
894 892
895 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ 893 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){
896 894
897 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, NULL);
898 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { 895 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
899 set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); 896 set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
900 set_mul_color_uni(uman, fColorMidUni, e.getColors(1)); 897 set_mul_color_uni(uman, fColorMidUni, e.getColors(1));
901 set_mul_color_uni(uman, fColorEndUni, e.getColors(2)); 898 set_mul_color_uni(uman, fColorEndUni, e.getColors(2));
902 } else { 899 } else {
903 set_color_uni(uman, fColorStartUni, e.getColors(0)); 900 set_color_uni(uman, fColorStartUni, e.getColors(0));
904 set_color_uni(uman, fColorMidUni, e.getColors(1)); 901 set_color_uni(uman, fColorMidUni, e.getColors(1));
905 set_color_uni(uman, fColorEndUni, e.getColors(2)); 902 set_color_uni(uman, fColorEndUni, e.getColors(2));
906 } 903 }
907 } else { 904 } else {
908 const GrTexture* texture = e.texture(0);
909 fEffectMatrix.setData(uman, e.getMatrix(), drawEffect, texture);
910 905
911 SkScalar yCoord = e.getYCoord(); 906 SkScalar yCoord = e.getYCoord();
912 if (yCoord != fCachedYCoord) { 907 if (yCoord != fCachedYCoord) {
913 uman.set1f(fFSYUni, yCoord); 908 uman.set1f(fFSYUni, yCoord);
914 fCachedYCoord = yCoord; 909 fCachedYCoord = yCoord;
915 } 910 }
916 } 911 }
917 } 912 }
918 913
919 914
920 GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { 915 GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) {
921 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>(); 916 const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
922 const GrTexture* texture = NULL;
923 917
924 if (GrGradientEffect::kTexture_ColorType == e.getColorType()){ 918 EffectKey key = 0;
925 texture = e.texture(0);
926 }
927
928 EffectKey key = GrGLEffectMatrix::GenKey(e.getMatrix(), drawEffect, kCoordsT ype, texture);
929 919
930 if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { 920 if (GrGradientEffect::kTwo_ColorType == e.getColorType()) {
931 key |= kTwoColorKey; 921 key |= kTwoColorKey;
932 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ 922 } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){
933 key |= kThreeColorKey; 923 key |= kThreeColorKey;
934 } 924 }
935 925
936 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { 926 if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
937 key |= kPremulBeforeInterpKey; 927 key |= kPremulBeforeInterpKey;
938 } 928 }
939 929
940 return key; 930 return key;
941 } 931 }
942 932
943 void GrGLGradientEffect::setupMatrix(GrGLShaderBuilder* builder,
944 EffectKey key,
945 SkString* fsCoordName,
946 SkString* vsVaryingName,
947 GrSLType* vsVaryingType) {
948 fEffectMatrix.emitCodeMakeFSCoords2D(builder,
949 key & kMatrixKeyMask,
950 fsCoordName,
951 vsVaryingName,
952 vsVaryingType);
953 }
954
955 void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, 933 void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder,
956 const char* gradientTValue, 934 const char* gradientTValue,
957 EffectKey key, 935 EffectKey key,
958 const char* outputColor, 936 const char* outputColor,
959 const char* inputColor, 937 const char* inputColor,
960 const GrGLShaderBuilder::TextureSamplerArray& samplers) { 938 const GrGLShaderBuilder::TextureSamplerArray& samplers) {
961 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)){ 939 if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)){
962 builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1. 0));\n", 940 builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1. 0));\n",
963 builder->getUniformVariable(fColorStartUni).c_str (), 941 builder->getUniformVariable(fColorStartUni).c_str (),
964 builder->getUniformVariable(fColorEndUni).c_str() , 942 builder->getUniformVariable(fColorEndUni).c_str() ,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 } 994 }
1017 } 995 }
1018 996
1019 ///////////////////////////////////////////////////////////////////// 997 /////////////////////////////////////////////////////////////////////
1020 998
1021 GrGradientEffect::GrGradientEffect(GrContext* ctx, 999 GrGradientEffect::GrGradientEffect(GrContext* ctx,
1022 const SkGradientShaderBase& shader, 1000 const SkGradientShaderBase& shader,
1023 const SkMatrix& matrix, 1001 const SkMatrix& matrix,
1024 SkShader::TileMode tileMode) { 1002 SkShader::TileMode tileMode) {
1025 1003
1026
1027 fMatrix = matrix;
1028 fIsOpaque = shader.isOpaque(); 1004 fIsOpaque = shader.isOpaque();
1029 1005
1030 SkShader::GradientInfo info; 1006 SkShader::GradientInfo info;
1031 SkScalar pos[3] = {0}; 1007 SkScalar pos[3] = {0};
1032 1008
1033 info.fColorCount = 3; 1009 info.fColorCount = 3;
1034 info.fColors = &fColors[0]; 1010 info.fColors = &fColors[0];
1035 info.fColorOffsets = &pos[0]; 1011 info.fColorOffsets = &pos[0];
1036 shader.asAGradient(&info); 1012 shader.asAGradient(&info);
1037 1013
(...skipping 10 matching lines...) Expand all
1048 fColorType = kThree_ColorType; 1024 fColorType = kThree_ColorType;
1049 foundSpecialCase = true; 1025 foundSpecialCase = true;
1050 } 1026 }
1051 } 1027 }
1052 if (foundSpecialCase) { 1028 if (foundSpecialCase) {
1053 if (SkGradientShader::kInterpolateColorsInPremul_Flag & info.fGradientFl ags) { 1029 if (SkGradientShader::kInterpolateColorsInPremul_Flag & info.fGradientFl ags) {
1054 fPremulType = kBeforeInterp_PremulType; 1030 fPremulType = kBeforeInterp_PremulType;
1055 } else { 1031 } else {
1056 fPremulType = kAfterInterp_PremulType; 1032 fPremulType = kAfterInterp_PremulType;
1057 } 1033 }
1034 fCoordTransform.reset(kCoordSet, matrix);
1058 } else { 1035 } else {
1059 // doesn't matter how this is set, just be consistent because it is part of the effect key. 1036 // doesn't matter how this is set, just be consistent because it is part of the effect key.
1060 fPremulType = kBeforeInterp_PremulType; 1037 fPremulType = kBeforeInterp_PremulType;
1061 SkBitmap bitmap; 1038 SkBitmap bitmap;
1062 shader.getGradientTableBitmap(&bitmap); 1039 shader.getGradientTableBitmap(&bitmap);
1063 fColorType = kTexture_ColorType; 1040 fColorType = kTexture_ColorType;
1064 1041
1065 GrTextureStripAtlas::Desc desc; 1042 GrTextureStripAtlas::Desc desc;
1066 desc.fWidth = bitmap.width(); 1043 desc.fWidth = bitmap.width();
1067 desc.fHeight = 32; 1044 desc.fHeight = 32;
1068 desc.fRowHeight = bitmap.height(); 1045 desc.fRowHeight = bitmap.height();
1069 desc.fContext = ctx; 1046 desc.fContext = ctx;
1070 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config()); 1047 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
1071 fAtlas = GrTextureStripAtlas::GetAtlas(desc); 1048 fAtlas = GrTextureStripAtlas::GetAtlas(desc);
1072 SkASSERT(NULL != fAtlas); 1049 SkASSERT(NULL != fAtlas);
1073 1050
1074 // We always filter the gradient table. Each table is one row of a textu re, always y-clamp. 1051 // We always filter the gradient table. Each table is one row of a textu re, always y-clamp.
1075 GrTextureParams params; 1052 GrTextureParams params;
1076 params.setFilterMode(GrTextureParams::kBilerp_FilterMode); 1053 params.setFilterMode(GrTextureParams::kBilerp_FilterMode);
1077 params.setTileModeX(tileMode); 1054 params.setTileModeX(tileMode);
1078 1055
1079 fRow = fAtlas->lockRow(bitmap); 1056 fRow = fAtlas->lockRow(bitmap);
1080 if (-1 != fRow) { 1057 if (-1 != fRow) {
1081 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * 1058 fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf *
1082 fAtlas->getVerticalScaleFactor(); 1059 fAtlas->getVerticalScaleFactor();
1060 fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture());
1083 fTextureAccess.reset(fAtlas->getTexture(), params); 1061 fTextureAccess.reset(fAtlas->getTexture(), params);
1084 } else { 1062 } else {
1085 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &p arams); 1063 GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &p arams);
1064 fCoordTransform.reset(kCoordSet, matrix, texture);
1086 fTextureAccess.reset(texture, params); 1065 fTextureAccess.reset(texture, params);
1087 fYCoord = SK_ScalarHalf; 1066 fYCoord = SK_ScalarHalf;
1088 1067
1089 // Unlock immediately, this is not great, but we don't have a way of 1068 // Unlock immediately, this is not great, but we don't have a way of
1090 // knowing when else to unlock it currently, so it may get purged fr om 1069 // knowing when else to unlock it currently, so it may get purged fr om
1091 // the cache, but it'll still be ref'd until it's no longer being us ed. 1070 // the cache, but it'll still be ref'd until it's no longer being us ed.
1092 GrUnlockAndUnrefCachedBitmapTexture(texture); 1071 GrUnlockAndUnrefCachedBitmapTexture(texture);
1093 } 1072 }
1094 this->addTextureAccess(&fTextureAccess); 1073 this->addTextureAccess(&fTextureAccess);
1095 } 1074 }
1075 this->addCoordTransform(&fCoordTransform);
1096 } 1076 }
1097 1077
1098 GrGradientEffect::~GrGradientEffect() { 1078 GrGradientEffect::~GrGradientEffect() {
1099 if (this->useAtlas()) { 1079 if (this->useAtlas()) {
1100 fAtlas->unlockRow(fRow); 1080 fAtlas->unlockRow(fRow);
1101 } 1081 }
1102 } 1082 }
1103 1083
1104 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { 1084 bool GrGradientEffect::onIsEqual(const GrEffect& effect) const {
1105 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect); 1085 const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect);
(...skipping 11 matching lines...) Expand all
1117 *this->getColors(2) != *s.getColors(2)) { 1097 *this->getColors(2) != *s.getColors(2)) {
1118 return false; 1098 return false;
1119 } 1099 }
1120 } 1100 }
1121 1101
1122 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && 1102 return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() &&
1123 fTextureAccess.getParams().getTileModeX() == 1103 fTextureAccess.getParams().getTileModeX() ==
1124 s.fTextureAccess.getParams().getTileModeX() && 1104 s.fTextureAccess.getParams().getTileModeX() &&
1125 this->useAtlas() == s.useAtlas() && 1105 this->useAtlas() == s.useAtlas() &&
1126 fYCoord == s.getYCoord() && 1106 fYCoord == s.getYCoord() &&
1127 fMatrix.cheapEqualTo(s.getMatrix()); 1107 fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix ());
1128 } 1108 }
1129 1109
1130 return false; 1110 return false;
1131 } 1111 }
1132 1112
1133 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const { 1113 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* vali dFlags) const {
1134 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) { 1114 if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorU npackA(*color)) {
1135 *validFlags = kA_GrColorComponentFlag; 1115 *validFlags = kA_GrColorComponentFlag;
1136 } else { 1116 } else {
1137 *validFlags = 0; 1117 *validFlags = 0;
(...skipping 18 matching lines...) Expand all
1156 (*stops)[i] = stop; 1136 (*stops)[i] = stop;
1157 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f; 1137 stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - st op) : 1.f;
1158 } 1138 }
1159 } 1139 }
1160 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount)); 1140 *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileM odeCount));
1161 1141
1162 return outColors; 1142 return outColors;
1163 } 1143 }
1164 1144
1165 #endif 1145 #endif
OLDNEW
« no previous file with comments | « src/effects/SkTableColorFilter.cpp ('k') | src/effects/gradients/SkGradientShaderPriv.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698