SkBicubicImageFilter.cpp revision 36df7ed46b41ac31cb2205bfd3ae37659d61e2fb
1/* 2 * Copyright 2013 The Android Open Source Project 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 "SkBicubicImageFilter.h" 9#include "SkBitmap.h" 10#include "SkColorPriv.h" 11#include "SkFlattenableBuffers.h" 12#include "SkMatrix.h" 13#include "SkRect.h" 14#include "SkUnPreMultiply.h" 15 16#if SK_SUPPORT_GPU 17#include "gl/GrGLEffectMatrix.h" 18#include "effects/GrSingleTextureEffect.h" 19#include "GrTBackendEffectFactory.h" 20#include "GrContext.h" 21#include "GrTexture.h" 22#endif 23 24SkBicubicImageFilter::SkBicubicImageFilter(const SkSize& scale, const SkScalar coefficients[16], SkImageFilter* input) 25 : INHERITED(input), 26 fScale(scale) { 27 memcpy(fCoefficients, coefficients, sizeof(fCoefficients)); 28} 29 30#define DS(x) SkDoubleToScalar(x) 31 32SkBicubicImageFilter* SkBicubicImageFilter::CreateMitchell(const SkSize& scale, 33 SkImageFilter* input) { 34 static const SkScalar coefficients[16] = { 35 DS( 1.0 / 18.0), DS(-9.0 / 18.0), DS( 15.0 / 18.0), DS( -7.0 / 18.0), 36 DS(16.0 / 18.0), DS( 0.0 / 18.0), DS(-36.0 / 18.0), DS( 21.0 / 18.0), 37 DS( 1.0 / 18.0), DS( 9.0 / 18.0), DS( 27.0 / 18.0), DS(-21.0 / 18.0), 38 DS( 0.0 / 18.0), DS( 0.0 / 18.0), DS( -6.0 / 18.0), DS( 7.0 / 18.0), 39 }; 40 return SkNEW_ARGS(SkBicubicImageFilter, (scale, coefficients, input)); 41} 42 43SkBicubicImageFilter::SkBicubicImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { 44 SkDEBUGCODE(uint32_t readSize =) buffer.readScalarArray(fCoefficients); 45 SkASSERT(readSize == 16); 46 fScale.fWidth = buffer.readScalar(); 47 fScale.fHeight = buffer.readScalar(); 48} 49 50void SkBicubicImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { 51 this->INHERITED::flatten(buffer); 52 buffer.writeScalarArray(fCoefficients, 16); 53 buffer.writeScalar(fScale.fWidth); 54 buffer.writeScalar(fScale.fHeight); 55} 56 57SkBicubicImageFilter::~SkBicubicImageFilter() { 58} 59 60inline SkPMColor cubicBlend(const SkScalar c[16], SkScalar t, SkPMColor c0, SkPMColor c1, SkPMColor c2, SkPMColor c3) { 61 SkScalar t2 = t * t, t3 = t2 * t; 62 SkScalar cc[4]; 63 // FIXME: For the fractx case, this should be refactored out of this function. 64 cc[0] = c[0] + SkScalarMul(c[1], t) + SkScalarMul(c[2], t2) + SkScalarMul(c[3], t3); 65 cc[1] = c[4] + SkScalarMul(c[5], t) + SkScalarMul(c[6], t2) + SkScalarMul(c[7], t3); 66 cc[2] = c[8] + SkScalarMul(c[9], t) + SkScalarMul(c[10], t2) + SkScalarMul(c[11], t3); 67 cc[3] = c[12] + SkScalarMul(c[13], t) + SkScalarMul(c[14], t2) + SkScalarMul(c[15], t3); 68 SkScalar a = SkScalarMul(cc[0], SkGetPackedA32(c0)) + SkScalarMul(cc[1], SkGetPackedA32(c1)) + SkScalarMul(cc[2], SkGetPackedA32(c2)) + SkScalarMul(cc[3], SkGetPackedA32(c3)); 69 SkScalar r = SkScalarMul(cc[0], SkGetPackedR32(c0)) + SkScalarMul(cc[1], SkGetPackedR32(c1)) + SkScalarMul(cc[2], SkGetPackedR32(c2)) + SkScalarMul(cc[3], SkGetPackedR32(c3)); 70 SkScalar g = SkScalarMul(cc[0], SkGetPackedG32(c0)) + SkScalarMul(cc[1], SkGetPackedG32(c1)) + SkScalarMul(cc[2], SkGetPackedG32(c2)) + SkScalarMul(cc[3], SkGetPackedG32(c3)); 71 SkScalar b = SkScalarMul(cc[0], SkGetPackedB32(c0)) + SkScalarMul(cc[1], SkGetPackedB32(c1)) + SkScalarMul(cc[2], SkGetPackedB32(c2)) + SkScalarMul(cc[3], SkGetPackedB32(c3)); 72 return SkPackARGB32(SkScalarRoundToInt(SkScalarClampMax(a, 255)), SkScalarRoundToInt(SkScalarClampMax(r, 255)), SkScalarRoundToInt(SkScalarClampMax(g, 255)), SkScalarRoundToInt(SkScalarClampMax(b, 255))); 73} 74 75bool SkBicubicImageFilter::onFilterImage(Proxy* proxy, 76 const SkBitmap& source, 77 const SkMatrix& matrix, 78 SkBitmap* result, 79 SkIPoint* loc) { 80 SkBitmap src = this->getInputResult(proxy, source, matrix, loc); 81 if (src.config() != SkBitmap::kARGB_8888_Config) { 82 return false; 83 } 84 85 SkAutoLockPixels alp(src); 86 if (!src.getPixels()) { 87 return false; 88 } 89 90 SkRect dstRect = SkRect::MakeWH(SkScalarMul(SkIntToScalar(src.width()), fScale.fWidth), 91 SkScalarMul(SkIntToScalar(src.height()), fScale.fHeight)); 92 SkIRect dstIRect; 93 dstRect.roundOut(&dstIRect); 94 result->setConfig(src.config(), dstIRect.width(), dstIRect.height()); 95 result->allocPixels(); 96 if (!result->getPixels()) { 97 return false; 98 } 99 100 SkRect srcRect; 101 src.getBounds(&srcRect); 102 SkMatrix inverse; 103 inverse.setRectToRect(dstRect, srcRect, SkMatrix::kFill_ScaleToFit); 104 inverse.postTranslate(SkFloatToScalar(-0.5f), SkFloatToScalar(-0.5f)); 105 106 for (int y = dstIRect.fTop; y < dstIRect.fBottom; ++y) { 107 SkPMColor* dptr = result->getAddr32(dstIRect.fLeft, y); 108 for (int x = dstIRect.fLeft; x < dstIRect.fRight; ++x) { 109 SkPoint srcPt, dstPt = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); 110 inverse.mapPoints(&srcPt, &dstPt, 1); 111 SkScalar fractx = srcPt.fX - SkScalarFloorToScalar(srcPt.fX); 112 SkScalar fracty = srcPt.fY - SkScalarFloorToScalar(srcPt.fY); 113 int sx = SkScalarFloorToInt(srcPt.fX); 114 int sy = SkScalarFloorToInt(srcPt.fY); 115 int x0 = SkClampMax(sx - 1, src.width() - 1); 116 int x1 = SkClampMax(sx , src.width() - 1); 117 int x2 = SkClampMax(sx + 1, src.width() - 1); 118 int x3 = SkClampMax(sx + 2, src.width() - 1); 119 int y0 = SkClampMax(sy - 1, src.height() - 1); 120 int y1 = SkClampMax(sy , src.height() - 1); 121 int y2 = SkClampMax(sy + 1, src.height() - 1); 122 int y3 = SkClampMax(sy + 2, src.height() - 1); 123 SkPMColor s00 = *src.getAddr32(x0, y0); 124 SkPMColor s10 = *src.getAddr32(x1, y0); 125 SkPMColor s20 = *src.getAddr32(x2, y0); 126 SkPMColor s30 = *src.getAddr32(x3, y0); 127 SkPMColor s0 = cubicBlend(fCoefficients, fractx, s00, s10, s20, s30); 128 SkPMColor s01 = *src.getAddr32(x0, y1); 129 SkPMColor s11 = *src.getAddr32(x1, y1); 130 SkPMColor s21 = *src.getAddr32(x2, y1); 131 SkPMColor s31 = *src.getAddr32(x3, y1); 132 SkPMColor s1 = cubicBlend(fCoefficients, fractx, s01, s11, s21, s31); 133 SkPMColor s02 = *src.getAddr32(x0, y2); 134 SkPMColor s12 = *src.getAddr32(x1, y2); 135 SkPMColor s22 = *src.getAddr32(x2, y2); 136 SkPMColor s32 = *src.getAddr32(x3, y2); 137 SkPMColor s2 = cubicBlend(fCoefficients, fractx, s02, s12, s22, s32); 138 SkPMColor s03 = *src.getAddr32(x0, y3); 139 SkPMColor s13 = *src.getAddr32(x1, y3); 140 SkPMColor s23 = *src.getAddr32(x2, y3); 141 SkPMColor s33 = *src.getAddr32(x3, y3); 142 SkPMColor s3 = cubicBlend(fCoefficients, fractx, s03, s13, s23, s33); 143 *dptr++ = cubicBlend(fCoefficients, fracty, s0, s1, s2, s3); 144 } 145 } 146 return true; 147} 148 149/////////////////////////////////////////////////////////////////////////////// 150 151#if SK_SUPPORT_GPU 152class GrGLBicubicEffect; 153 154class GrBicubicEffect : public GrSingleTextureEffect { 155public: 156 virtual ~GrBicubicEffect(); 157 158 static const char* Name() { return "Bicubic"; } 159 const float* coefficients() const { return fCoefficients; } 160 161 typedef GrGLBicubicEffect GLEffect; 162 163 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; 164 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; 165 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; 166 167 static GrEffectRef* Create(GrTexture* tex, const SkScalar coefficients[16]) { 168 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrBicubicEffect, (tex, coefficients))); 169 return CreateEffectRef(effect); 170 } 171 172private: 173 GrBicubicEffect(GrTexture*, const SkScalar coefficients[16]); 174 float fCoefficients[16]; 175 176 GR_DECLARE_EFFECT_TEST; 177 178 typedef GrSingleTextureEffect INHERITED; 179}; 180 181class GrGLBicubicEffect : public GrGLEffect { 182public: 183 GrGLBicubicEffect(const GrBackendEffectFactory& factory, 184 const GrEffect& effect); 185 virtual void emitCode(GrGLShaderBuilder*, 186 const GrEffectStage&, 187 EffectKey, 188 const char* vertexCoords, 189 const char* outputColor, 190 const char* inputColor, 191 const TextureSamplerArray&) SK_OVERRIDE; 192 193 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); 194 195 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; 196 197private: 198 typedef GrGLUniformManager::UniformHandle UniformHandle; 199 200 UniformHandle fCoefficientsUni; 201 UniformHandle fImageIncrementUni; 202 203 GrGLEffectMatrix fEffectMatrix; 204 205 typedef GrGLEffect INHERITED; 206}; 207 208GrGLBicubicEffect::GrGLBicubicEffect(const GrBackendEffectFactory& factory, 209 const GrEffect& effect) 210 : INHERITED(factory) 211 , fCoefficientsUni(GrGLUniformManager::kInvalidUniformHandle) 212 , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) { 213} 214 215void GrGLBicubicEffect::emitCode(GrGLShaderBuilder* builder, 216 const GrEffectStage&, 217 EffectKey key, 218 const char* vertexCoords, 219 const char* outputColor, 220 const char* inputColor, 221 const TextureSamplerArray& samplers) { 222 const char* coords; 223 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); 224 fCoefficientsUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, 225 kMat44f_GrSLType, "Coefficients"); 226 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, 227 kVec2f_GrSLType, "ImageIncrement"); 228 SkString* code = &builder->fFSCode; 229 230 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); 231 const char* coeff = builder->getUniformCStr(fCoefficientsUni); 232 233 SkString cubicBlendName; 234 235 static const GrGLShaderVar gCubicBlendArgs[] = { 236 GrGLShaderVar("coefficients", kMat44f_GrSLType), 237 GrGLShaderVar("t", kFloat_GrSLType), 238 GrGLShaderVar("c0", kVec4f_GrSLType), 239 GrGLShaderVar("c1", kVec4f_GrSLType), 240 GrGLShaderVar("c2", kVec4f_GrSLType), 241 GrGLShaderVar("c3", kVec4f_GrSLType), 242 }; 243 builder->emitFunction(GrGLShaderBuilder::kFragment_ShaderType, 244 kVec4f_GrSLType, 245 "cubicBlend", 246 SK_ARRAY_COUNT(gCubicBlendArgs), 247 gCubicBlendArgs, 248 "\tvec4 ts = vec4(1.0, t, t * t, t * t * t);\n" 249 "\tvec4 c = coefficients * ts;\n" 250 "\treturn c.x * c0 + c.y * c1 + c.z * c2 + c.w * c3;\n", 251 &cubicBlendName); 252 code->appendf("\tvec2 coord = %s - %s * vec2(0.5, 0.5);\n", coords, imgInc); 253 code->appendf("\tvec2 f = fract(coord / %s);\n", imgInc); 254 for (int y = 0; y < 4; ++y) { 255 for (int x = 0; x < 4; ++x) { 256 SkString coord; 257 coord.printf("coord + %s * vec2(%d, %d)", imgInc, x - 1, y - 1); 258 code->appendf("\tvec4 s%d%d = ", x, y); 259 builder->appendTextureLookup(&builder->fFSCode, samplers[0], coord.c_str()); 260 code->appendf(";\n"); 261 } 262 code->appendf("\tvec4 s%d = %s(%s, f.x, s0%d, s1%d, s2%d, s3%d);\n", y, cubicBlendName.c_str(), coeff, y, y, y, y); 263 } 264 code->appendf("\t%s = %s(%s, f.y, s0, s1, s2, s3);\n", outputColor, cubicBlendName.c_str(), coeff); 265} 266 267GrGLEffect::EffectKey GrGLBicubicEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) { 268 const GrBicubicEffect& m = 269 static_cast<const GrBicubicEffect&>(*s.getEffect()); 270 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), 271 s.getCoordChangeMatrix(), 272 m.texture(0)); 273 return matrixKey; 274} 275 276void GrGLBicubicEffect::setData(const GrGLUniformManager& uman, 277 const GrEffectStage& stage) { 278 const GrBicubicEffect& effect = 279 static_cast<const GrBicubicEffect&>(*stage.getEffect()); 280 GrTexture& texture = *effect.texture(0); 281 float imageIncrement[2]; 282 imageIncrement[0] = 1.0f / texture.width(); 283 imageIncrement[1] = 1.0f / texture.height(); 284 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); 285 uman.setMatrix4f(fCoefficientsUni, effect.coefficients()); 286 fEffectMatrix.setData(uman, 287 effect.getMatrix(), 288 stage.getCoordChangeMatrix(), 289 effect.texture(0)); 290} 291 292GrBicubicEffect::GrBicubicEffect(GrTexture* texture, 293 const SkScalar coefficients[16]) 294 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) { 295 for (int y = 0; y < 4; y++) { 296 for (int x = 0; x < 4; x++) { 297 // Convert from row-major scalars to column-major floats. 298 fCoefficients[x * 4 + y] = SkScalarToFloat(coefficients[y * 4 + x]); 299 } 300 } 301} 302 303GrBicubicEffect::~GrBicubicEffect() { 304} 305 306const GrBackendEffectFactory& GrBicubicEffect::getFactory() const { 307 return GrTBackendEffectFactory<GrBicubicEffect>::getInstance(); 308} 309 310bool GrBicubicEffect::onIsEqual(const GrEffect& sBase) const { 311 const GrBicubicEffect& s = 312 static_cast<const GrBicubicEffect&>(sBase); 313 return this->texture(0) == s.texture(0) && 314 !memcmp(fCoefficients, s.coefficients(), 16); 315} 316 317void GrBicubicEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { 318 // FIXME: Perhaps we can do better. 319 *validFlags = 0; 320 return; 321} 322 323GR_DEFINE_EFFECT_TEST(GrBicubicEffect); 324 325GrEffectRef* GrBicubicEffect::TestCreate(SkRandom* random, 326 GrContext* context, 327 GrTexture* textures[]) { 328 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 329 GrEffectUnitTest::kAlphaTextureIdx; 330 SkScalar coefficients[16]; 331 for (int i = 0; i < 16; i++) { 332 coefficients[i] = random->nextSScalar1(); 333 } 334 return GrBicubicEffect::Create(textures[texIdx], coefficients); 335} 336 337GrTexture* SkBicubicImageFilter::filterImageGPU(Proxy* proxy, GrTexture* src, const SkRect& rect) { 338 SkAutoTUnref<GrTexture> srcTexture(this->getInputResultAsTexture(proxy, src, rect)); 339 GrContext* context = srcTexture->getContext(); 340 341 SkRect dstRect = SkRect::MakeWH(rect.width() * fScale.fWidth, 342 rect.height() * fScale.fHeight); 343 344 GrTextureDesc desc; 345 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; 346 desc.fWidth = SkScalarCeilToInt(dstRect.width()); 347 desc.fHeight = SkScalarCeilToInt(dstRect.height()); 348 desc.fConfig = kRGBA_8888_GrPixelConfig; 349 350 GrAutoScratchTexture ast(context, desc); 351 if (!ast.texture()) { 352 return NULL; 353 } 354 GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); 355 GrPaint paint; 356 paint.colorStage(0)->setEffect(GrBicubicEffect::Create(srcTexture, fCoefficients))->unref(); 357 context->drawRectToRect(paint, dstRect, rect); 358 return ast.detach(); 359} 360#endif 361 362/////////////////////////////////////////////////////////////////////////////// 363