1 /* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #include "SkColorMatrixFilterRowMajor255.h" 9 #include "SkColorPriv.h" 10 #include "SkNx.h" 11 #include "SkPM4fPriv.h" 12 #include "SkRasterPipeline.h" 13 #include "SkReadBuffer.h" 14 #include "SkRefCnt.h" 15 #include "SkString.h" 16 #include "SkUnPreMultiply.h" 17 #include "SkWriteBuffer.h" 18 19 static void transpose_and_scale01(float dst[20], const float src[20]) { 20 const float* srcR = src + 0; 21 const float* srcG = src + 5; 22 const float* srcB = src + 10; 23 const float* srcA = src + 15; 24 25 for (int i = 0; i < 16; i += 4) { 26 dst[i + 0] = *srcR++; 27 dst[i + 1] = *srcG++; 28 dst[i + 2] = *srcB++; 29 dst[i + 3] = *srcA++; 30 } 31 // Might as well scale these translates down to [0,1] here instead of every filter call. 32 dst[16] = *srcR * (1/255.0f); 33 dst[17] = *srcG * (1/255.0f); 34 dst[18] = *srcB * (1/255.0f); 35 dst[19] = *srcA * (1/255.0f); 36 } 37 38 void SkColorMatrixFilterRowMajor255::initState() { 39 transpose_and_scale01(fTranspose, fMatrix); 40 41 const float* array = fMatrix; 42 43 // check if we have to munge Alpha 44 bool changesAlpha = (array[15] || array[16] || array[17] || (array[18] - 1) || array[19]); 45 bool usesAlpha = (array[3] || array[8] || array[13]); 46 47 if (changesAlpha || usesAlpha) { 48 fFlags = changesAlpha ? 0 : kAlphaUnchanged_Flag; 49 } else { 50 fFlags = kAlphaUnchanged_Flag; 51 } 52 } 53 54 /////////////////////////////////////////////////////////////////////////////// 55 56 SkColorMatrixFilterRowMajor255::SkColorMatrixFilterRowMajor255(const SkScalar array[20]) { 57 memcpy(fMatrix, array, 20 * sizeof(SkScalar)); 58 this->initState(); 59 } 60 61 uint32_t SkColorMatrixFilterRowMajor255::getFlags() const { 62 return this->INHERITED::getFlags() | fFlags; 63 } 64 65 /////////////////////////////////////////////////////////////////////////////// 66 67 void SkColorMatrixFilterRowMajor255::flatten(SkWriteBuffer& buffer) const { 68 SkASSERT(sizeof(fMatrix)/sizeof(SkScalar) == 20); 69 buffer.writeScalarArray(fMatrix, 20); 70 } 71 72 sk_sp<SkFlattenable> SkColorMatrixFilterRowMajor255::CreateProc(SkReadBuffer& buffer) { 73 SkScalar matrix[20]; 74 if (buffer.readScalarArray(matrix, 20)) { 75 return sk_make_sp<SkColorMatrixFilterRowMajor255>(matrix); 76 } 77 return nullptr; 78 } 79 80 bool SkColorMatrixFilterRowMajor255::asColorMatrix(SkScalar matrix[20]) const { 81 if (matrix) { 82 memcpy(matrix, fMatrix, 20 * sizeof(SkScalar)); 83 } 84 return true; 85 } 86 87 /////////////////////////////////////////////////////////////////////////////// 88 // This code was duplicated from src/effects/SkColorMatrixc.cpp in order to be used in core. 89 ////// 90 91 // To detect if we need to apply clamping after applying a matrix, we check if 92 // any output component might go outside of [0, 255] for any combination of 93 // input components in [0..255]. 94 // Each output component is an affine transformation of the input component, so 95 // the minimum and maximum values are for any combination of minimum or maximum 96 // values of input components (i.e. 0 or 255). 97 // E.g. if R' = x*R + y*G + z*B + w*A + t 98 // Then the maximum value will be for R=255 if x>0 or R=0 if x<0, and the 99 // minimum value will be for R=0 if x>0 or R=255 if x<0. 100 // Same goes for all components. 101 static bool component_needs_clamping(const SkScalar row[5]) { 102 SkScalar maxValue = row[4] / 255; 103 SkScalar minValue = row[4] / 255; 104 for (int i = 0; i < 4; ++i) { 105 if (row[i] > 0) 106 maxValue += row[i]; 107 else 108 minValue += row[i]; 109 } 110 return (maxValue > 1) || (minValue < 0); 111 } 112 113 static bool needs_clamping(const SkScalar matrix[20]) { 114 return component_needs_clamping(matrix) 115 || component_needs_clamping(matrix+5) 116 || component_needs_clamping(matrix+10) 117 || component_needs_clamping(matrix+15); 118 } 119 120 static void set_concat(SkScalar result[20], const SkScalar outer[20], const SkScalar inner[20]) { 121 int index = 0; 122 for (int j = 0; j < 20; j += 5) { 123 for (int i = 0; i < 4; i++) { 124 result[index++] = outer[j + 0] * inner[i + 0] + 125 outer[j + 1] * inner[i + 5] + 126 outer[j + 2] * inner[i + 10] + 127 outer[j + 3] * inner[i + 15]; 128 } 129 result[index++] = outer[j + 0] * inner[4] + 130 outer[j + 1] * inner[9] + 131 outer[j + 2] * inner[14] + 132 outer[j + 3] * inner[19] + 133 outer[j + 4]; 134 } 135 } 136 137 /////////////////////////////////////////////////////////////////////////////// 138 // End duplication 139 ////// 140 141 void SkColorMatrixFilterRowMajor255::onAppendStages(SkRasterPipeline* p, 142 SkColorSpace* dst, 143 SkArenaAlloc* scratch, 144 bool shaderIsOpaque) const { 145 bool willStayOpaque = shaderIsOpaque && (fFlags & kAlphaUnchanged_Flag); 146 bool needsClamp0 = false, 147 needsClamp1 = false; 148 for (int i = 0; i < 4; i++) { 149 SkScalar min = fTranspose[i+16], 150 max = fTranspose[i+16]; 151 (fTranspose[i+ 0] < 0 ? min : max) += fTranspose[i+ 0]; 152 (fTranspose[i+ 4] < 0 ? min : max) += fTranspose[i+ 4]; 153 (fTranspose[i+ 8] < 0 ? min : max) += fTranspose[i+ 8]; 154 (fTranspose[i+12] < 0 ? min : max) += fTranspose[i+12]; 155 needsClamp0 = needsClamp0 || min < 0; 156 needsClamp1 = needsClamp1 || max > 1; 157 } 158 159 if (!shaderIsOpaque) { p->append(SkRasterPipeline::unpremul); } 160 if ( true) { p->append(SkRasterPipeline::matrix_4x5, fTranspose); } 161 if (!willStayOpaque) { p->append(SkRasterPipeline::premul); } 162 if ( needsClamp0) { p->append(SkRasterPipeline::clamp_0); } 163 if ( needsClamp1) { p->append(SkRasterPipeline::clamp_a); } 164 } 165 166 sk_sp<SkColorFilter> 167 SkColorMatrixFilterRowMajor255::makeComposed(sk_sp<SkColorFilter> innerFilter) const { 168 SkScalar innerMatrix[20]; 169 if (innerFilter->asColorMatrix(innerMatrix) && !needs_clamping(innerMatrix)) { 170 SkScalar concat[20]; 171 set_concat(concat, fMatrix, innerMatrix); 172 return sk_make_sp<SkColorMatrixFilterRowMajor255>(concat); 173 } 174 return nullptr; 175 } 176 177 #if SK_SUPPORT_GPU 178 #include "GrFragmentProcessor.h" 179 #include "glsl/GrGLSLFragmentProcessor.h" 180 #include "glsl/GrGLSLFragmentShaderBuilder.h" 181 #include "glsl/GrGLSLProgramDataManager.h" 182 #include "glsl/GrGLSLUniformHandler.h" 183 184 class ColorMatrixEffect : public GrFragmentProcessor { 185 public: 186 static sk_sp<GrFragmentProcessor> Make(const SkScalar matrix[20]) { 187 return sk_sp<GrFragmentProcessor>(new ColorMatrixEffect(matrix)); 188 } 189 190 const char* name() const override { return "Color Matrix"; } 191 192 GR_DECLARE_FRAGMENT_PROCESSOR_TEST 193 194 class GLSLProcessor : public GrGLSLFragmentProcessor { 195 public: 196 // this class always generates the same code. 197 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*) {} 198 199 void emitCode(EmitArgs& args) override { 200 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 201 fMatrixHandle = uniformHandler->addUniform(kFragment_GrShaderFlag, 202 kMat44f_GrSLType, kDefault_GrSLPrecision, 203 "ColorMatrix"); 204 fVectorHandle = uniformHandler->addUniform(kFragment_GrShaderFlag, 205 kVec4f_GrSLType, kDefault_GrSLPrecision, 206 "ColorMatrixVector"); 207 208 if (nullptr == args.fInputColor) { 209 // could optimize this case, but we aren't for now. 210 args.fInputColor = "vec4(1)"; 211 } 212 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; 213 // The max() is to guard against 0 / 0 during unpremul when the incoming color is 214 // transparent black. 215 fragBuilder->codeAppendf("\tfloat nonZeroAlpha = max(%s.a, 0.00001);\n", 216 args.fInputColor); 217 fragBuilder->codeAppendf("\t%s = %s * vec4(%s.rgb / nonZeroAlpha, nonZeroAlpha) + %s;\n", 218 args.fOutputColor, 219 uniformHandler->getUniformCStr(fMatrixHandle), 220 args.fInputColor, 221 uniformHandler->getUniformCStr(fVectorHandle)); 222 fragBuilder->codeAppendf("\t%s = clamp(%s, 0.0, 1.0);\n", 223 args.fOutputColor, args.fOutputColor); 224 fragBuilder->codeAppendf("\t%s.rgb *= %s.a;\n", args.fOutputColor, args.fOutputColor); 225 } 226 227 protected: 228 void onSetData(const GrGLSLProgramDataManager& uniManager, 229 const GrFragmentProcessor& proc) override { 230 const ColorMatrixEffect& cme = proc.cast<ColorMatrixEffect>(); 231 const float* m = cme.fMatrix; 232 // The GL matrix is transposed from SkColorMatrix. 233 float mt[] = { 234 m[0], m[5], m[10], m[15], 235 m[1], m[6], m[11], m[16], 236 m[2], m[7], m[12], m[17], 237 m[3], m[8], m[13], m[18], 238 }; 239 static const float kScale = 1.0f / 255.0f; 240 float vec[] = { 241 m[4] * kScale, m[9] * kScale, m[14] * kScale, m[19] * kScale, 242 }; 243 uniManager.setMatrix4fv(fMatrixHandle, 1, mt); 244 uniManager.set4fv(fVectorHandle, 1, vec); 245 } 246 247 private: 248 GrGLSLProgramDataManager::UniformHandle fMatrixHandle; 249 GrGLSLProgramDataManager::UniformHandle fVectorHandle; 250 251 typedef GrGLSLFragmentProcessor INHERITED; 252 }; 253 private: 254 // We could implement the constant input->constant output optimization but haven't. Other 255 // optimizations would be matrix-dependent. 256 ColorMatrixEffect(const SkScalar matrix[20]) : INHERITED(kNone_OptimizationFlags) { 257 memcpy(fMatrix, matrix, sizeof(SkScalar) * 20); 258 this->initClassID<ColorMatrixEffect>(); 259 } 260 261 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { 262 return new GLSLProcessor; 263 } 264 265 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, 266 GrProcessorKeyBuilder* b) const override { 267 GLSLProcessor::GenKey(*this, caps, b); 268 } 269 270 bool onIsEqual(const GrFragmentProcessor& s) const override { 271 const ColorMatrixEffect& cme = s.cast<ColorMatrixEffect>(); 272 return 0 == memcmp(fMatrix, cme.fMatrix, sizeof(fMatrix)); 273 } 274 275 SkScalar fMatrix[20]; 276 277 typedef GrFragmentProcessor INHERITED; 278 }; 279 280 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ColorMatrixEffect); 281 282 #if GR_TEST_UTILS 283 sk_sp<GrFragmentProcessor> ColorMatrixEffect::TestCreate(GrProcessorTestData* d) { 284 SkScalar colorMatrix[20]; 285 for (size_t i = 0; i < SK_ARRAY_COUNT(colorMatrix); ++i) { 286 colorMatrix[i] = d->fRandom->nextSScalar1(); 287 } 288 return ColorMatrixEffect::Make(colorMatrix); 289 } 290 #endif 291 292 sk_sp<GrFragmentProcessor> SkColorMatrixFilterRowMajor255::asFragmentProcessor( 293 GrContext*, SkColorSpace*) const { 294 return ColorMatrixEffect::Make(fMatrix); 295 } 296 297 #endif 298 299 #ifndef SK_IGNORE_TO_STRING 300 void SkColorMatrixFilterRowMajor255::toString(SkString* str) const { 301 str->append("SkColorMatrixFilterRowMajor255: "); 302 303 str->append("matrix: ("); 304 for (int i = 0; i < 20; ++i) { 305 str->appendScalar(fMatrix[i]); 306 if (i < 19) { 307 str->append(", "); 308 } 309 } 310 str->append(")"); 311 } 312 #endif 313 314 /////////////////////////////////////////////////////////////////////////////// 315 316 sk_sp<SkColorFilter> SkColorFilter::MakeMatrixFilterRowMajor255(const SkScalar array[20]) { 317 return sk_sp<SkColorFilter>(new SkColorMatrixFilterRowMajor255(array)); 318 } 319 320 /////////////////////////////////////////////////////////////////////////////// 321 322 sk_sp<SkColorFilter> 323 SkColorMatrixFilterRowMajor255::MakeSingleChannelOutput(const SkScalar row[5]) { 324 SkASSERT(row); 325 auto cf = sk_make_sp<SkColorMatrixFilterRowMajor255>(); 326 static_assert(sizeof(SkScalar) * 5 * 4 == sizeof(cf->fMatrix), "sizes don't match"); 327 for (int i = 0; i < 4; ++i) { 328 memcpy(cf->fMatrix + 5 * i, row, sizeof(SkScalar) * 5); 329 } 330 cf->initState(); 331 return cf; 332 } 333