1 /* 2 * Copyright 2012 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 "SkMorphologyImageFilter.h" 9 10 #include "SkBitmap.h" 11 #include "SkColorData.h" 12 #include "SkColorSpaceXformer.h" 13 #include "SkImageFilterPriv.h" 14 #include "SkOpts.h" 15 #include "SkReadBuffer.h" 16 #include "SkRect.h" 17 #include "SkSpecialImage.h" 18 #include "SkWriteBuffer.h" 19 20 #if SK_SUPPORT_GPU 21 #include "../private/GrGLSL.h" 22 #include "GrContext.h" 23 #include "GrCoordTransform.h" 24 #include "GrFixedClip.h" 25 #include "GrRenderTargetContext.h" 26 #include "GrTexture.h" 27 #include "GrTextureProxy.h" 28 #include "SkGr.h" 29 #include "glsl/GrGLSLFragmentProcessor.h" 30 #include "glsl/GrGLSLFragmentShaderBuilder.h" 31 #include "glsl/GrGLSLProgramDataManager.h" 32 #include "glsl/GrGLSLUniformHandler.h" 33 #endif 34 35 sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY, 36 sk_sp<SkImageFilter> input, 37 const CropRect* cropRect) { 38 if (radiusX < 0 || radiusY < 0) { 39 return nullptr; 40 } 41 return sk_sp<SkImageFilter>(new SkDilateImageFilter(radiusX, radiusY, 42 std::move(input), 43 cropRect)); 44 } 45 46 47 sk_sp<SkImageFilter> SkErodeImageFilter::Make(int radiusX, int radiusY, 48 sk_sp<SkImageFilter> input, 49 const CropRect* cropRect) { 50 if (radiusX < 0 || radiusY < 0) { 51 return nullptr; 52 } 53 return sk_sp<SkImageFilter>(new SkErodeImageFilter(radiusX, radiusY, 54 std::move(input), 55 cropRect)); 56 } 57 58 SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, 59 int radiusY, 60 sk_sp<SkImageFilter> input, 61 const CropRect* cropRect) 62 : INHERITED(&input, 1, cropRect) 63 , fRadius(SkISize::Make(radiusX, radiusY)) { 64 } 65 66 void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const { 67 this->INHERITED::flatten(buffer); 68 buffer.writeInt(fRadius.fWidth); 69 buffer.writeInt(fRadius.fHeight); 70 } 71 72 static void call_proc_X(SkMorphologyImageFilter::Proc procX, 73 const SkBitmap& src, SkBitmap* dst, 74 int radiusX, const SkIRect& bounds) { 75 procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), 76 radiusX, bounds.width(), bounds.height(), 77 src.rowBytesAsPixels(), dst->rowBytesAsPixels()); 78 } 79 80 static void call_proc_Y(SkMorphologyImageFilter::Proc procY, 81 const SkPMColor* src, int srcRowBytesAsPixels, SkBitmap* dst, 82 int radiusY, const SkIRect& bounds) { 83 procY(src, dst->getAddr32(0, 0), 84 radiusY, bounds.height(), bounds.width(), 85 srcRowBytesAsPixels, dst->rowBytesAsPixels()); 86 } 87 88 SkRect SkMorphologyImageFilter::computeFastBounds(const SkRect& src) const { 89 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; 90 bounds.outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height())); 91 return bounds; 92 } 93 94 SkIRect SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, 95 MapDirection) const { 96 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), 97 SkIntToScalar(this->radius().height())); 98 ctm.mapVectors(&radius, 1); 99 return src.makeOutset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y())); 100 } 101 102 sk_sp<SkFlattenable> SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) { 103 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); 104 const int width = buffer.readInt(); 105 const int height = buffer.readInt(); 106 return Make(width, height, common.getInput(0), &common.cropRect()); 107 } 108 109 sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) { 110 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); 111 const int width = buffer.readInt(); 112 const int height = buffer.readInt(); 113 return Make(width, height, common.getInput(0), &common.cropRect()); 114 } 115 116 #ifndef SK_IGNORE_TO_STRING 117 void SkErodeImageFilter::toString(SkString* str) const { 118 str->appendf("SkErodeImageFilter: ("); 119 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); 120 str->append(")"); 121 } 122 #endif 123 124 #ifndef SK_IGNORE_TO_STRING 125 void SkDilateImageFilter::toString(SkString* str) const { 126 str->appendf("SkDilateImageFilter: ("); 127 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); 128 str->append(")"); 129 } 130 #endif 131 132 #if SK_SUPPORT_GPU 133 134 /////////////////////////////////////////////////////////////////////////////// 135 /** 136 * Morphology effects. Depending upon the type of morphology, either the 137 * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the 138 * kernel is selected as the new color. The new color is modulated by the input 139 * color. 140 */ 141 class GrMorphologyEffect : public GrFragmentProcessor { 142 public: 143 enum class Direction { kX, kY }; 144 enum class Type { kErode, kDilate }; 145 146 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, Direction dir, 147 int radius, Type type) { 148 return std::unique_ptr<GrFragmentProcessor>( 149 new GrMorphologyEffect(std::move(proxy), dir, radius, type, nullptr)); 150 } 151 152 static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, Direction dir, 153 int radius, Type type, const float bounds[2]) { 154 return std::unique_ptr<GrFragmentProcessor>( 155 new GrMorphologyEffect(std::move(proxy), dir, radius, type, bounds)); 156 } 157 158 Type type() const { return fType; } 159 bool useRange() const { return fUseRange; } 160 const float* range() const { return fRange; } 161 Direction direction() const { return fDirection; } 162 int radius() const { return fRadius; } 163 int width() const { return 2 * fRadius + 1; } 164 165 const char* name() const override { return "Morphology"; } 166 167 std::unique_ptr<GrFragmentProcessor> clone() const override { 168 return std::unique_ptr<GrFragmentProcessor>(new GrMorphologyEffect(*this)); 169 } 170 171 private: 172 GrCoordTransform fCoordTransform; 173 TextureSampler fTextureSampler; 174 Direction fDirection; 175 int fRadius; 176 Type fType; 177 bool fUseRange; 178 float fRange[2]; 179 180 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; 181 182 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; 183 184 bool onIsEqual(const GrFragmentProcessor&) const override; 185 186 GrMorphologyEffect(sk_sp<GrTextureProxy>, Direction, int radius, Type, const float range[2]); 187 explicit GrMorphologyEffect(const GrMorphologyEffect&); 188 189 GR_DECLARE_FRAGMENT_PROCESSOR_TEST 190 191 typedef GrFragmentProcessor INHERITED; 192 }; 193 194 /////////////////////////////////////////////////////////////////////////////// 195 196 class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { 197 public: 198 void emitCode(EmitArgs&) override; 199 200 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); 201 202 protected: 203 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; 204 205 private: 206 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni; 207 GrGLSLProgramDataManager::UniformHandle fRangeUni; 208 209 typedef GrGLSLFragmentProcessor INHERITED; 210 }; 211 212 void GrGLMorphologyEffect::emitCode(EmitArgs& args) { 213 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>(); 214 215 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; 216 fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "PixelSize"); 217 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni); 218 fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kFloat2_GrSLType, "Range"); 219 const char* range = uniformHandler->getUniformCStr(fRangeUni); 220 221 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; 222 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); 223 const char* func; 224 switch (me.type()) { 225 case GrMorphologyEffect::Type::kErode: 226 fragBuilder->codeAppendf("\t\t%s = half4(1, 1, 1, 1);\n", args.fOutputColor); 227 func = "min"; 228 break; 229 case GrMorphologyEffect::Type::kDilate: 230 fragBuilder->codeAppendf("\t\t%s = half4(0, 0, 0, 0);\n", args.fOutputColor); 231 func = "max"; 232 break; 233 default: 234 SK_ABORT("Unexpected type"); 235 func = ""; // suppress warning 236 break; 237 } 238 239 const char* dir; 240 switch (me.direction()) { 241 case GrMorphologyEffect::Direction::kX: 242 dir = "x"; 243 break; 244 case GrMorphologyEffect::Direction::kY: 245 dir = "y"; 246 break; 247 default: 248 SK_ABORT("Unknown filter direction."); 249 dir = ""; // suppress warning 250 } 251 252 int width = me.width(); 253 254 // float2 coord = coord2D; 255 fragBuilder->codeAppendf("\t\tfloat2 coord = %s;\n", coords2D.c_str()); 256 // coord.x -= radius * pixelSize; 257 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc); 258 if (me.useRange()) { 259 // highBound = min(highBound, coord.x + (width-1) * pixelSize); 260 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);", 261 range, dir, float(width - 1), pixelSizeInc); 262 // coord.x = max(lowBound, coord.x); 263 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir); 264 } 265 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width); 266 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor); 267 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord"); 268 fragBuilder->codeAppend(");\n"); 269 // coord.x += pixelSize; 270 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc); 271 if (me.useRange()) { 272 // coord.x = min(highBound, coord.x); 273 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir); 274 } 275 fragBuilder->codeAppend("\t\t}\n"); 276 fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor); 277 } 278 279 void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, 280 const GrShaderCaps&, GrProcessorKeyBuilder* b) { 281 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); 282 uint32_t key = static_cast<uint32_t>(m.radius()); 283 key |= (static_cast<uint32_t>(m.type()) << 8); 284 key |= (static_cast<uint32_t>(m.direction()) << 9); 285 if (m.useRange()) { 286 key |= 1 << 10; 287 } 288 b->add32(key); 289 } 290 291 void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman, 292 const GrFragmentProcessor& proc) { 293 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); 294 GrSurfaceProxy* proxy = m.textureSampler(0).proxy(); 295 GrTexture& texture = *proxy->priv().peekTexture(); 296 297 float pixelSize = 0.0f; 298 switch (m.direction()) { 299 case GrMorphologyEffect::Direction::kX: 300 pixelSize = 1.0f / texture.width(); 301 break; 302 case GrMorphologyEffect::Direction::kY: 303 pixelSize = 1.0f / texture.height(); 304 break; 305 default: 306 SK_ABORT("Unknown filter direction."); 307 } 308 pdman.set1f(fPixelSizeUni, pixelSize); 309 310 if (m.useRange()) { 311 const float* range = m.range(); 312 if (GrMorphologyEffect::Direction::kY == m.direction() && 313 proxy->origin() == kBottomLeft_GrSurfaceOrigin) { 314 pdman.set2f(fRangeUni, 1.0f - (range[1]*pixelSize), 1.0f - (range[0]*pixelSize)); 315 } else { 316 pdman.set2f(fRangeUni, range[0] * pixelSize, range[1] * pixelSize); 317 } 318 } 319 } 320 321 /////////////////////////////////////////////////////////////////////////////// 322 323 GrMorphologyEffect::GrMorphologyEffect(sk_sp<GrTextureProxy> proxy, 324 Direction direction, 325 int radius, 326 Type type, 327 const float range[2]) 328 : INHERITED(kGrMorphologyEffect_ClassID, ModulateByConfigOptimizationFlags(proxy->config())) 329 , fCoordTransform(proxy.get()) 330 , fTextureSampler(std::move(proxy)) 331 , fDirection(direction) 332 , fRadius(radius) 333 , fType(type) 334 , fUseRange(SkToBool(range)) { 335 this->addCoordTransform(&fCoordTransform); 336 this->addTextureSampler(&fTextureSampler); 337 if (fUseRange) { 338 fRange[0] = range[0]; 339 fRange[1] = range[1]; 340 } 341 } 342 343 GrMorphologyEffect::GrMorphologyEffect(const GrMorphologyEffect& that) 344 : INHERITED(kGrMorphologyEffect_ClassID, that.optimizationFlags()) 345 , fCoordTransform(that.fCoordTransform) 346 , fTextureSampler(that.fTextureSampler) 347 , fDirection(that.fDirection) 348 , fRadius(that.fRadius) 349 , fType(that.fType) 350 , fUseRange(that.fUseRange) { 351 this->addCoordTransform(&fCoordTransform); 352 this->addTextureSampler(&fTextureSampler); 353 if (that.fUseRange) { 354 fRange[0] = that.fRange[0]; 355 fRange[1] = that.fRange[1]; 356 } 357 } 358 359 void GrMorphologyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, 360 GrProcessorKeyBuilder* b) const { 361 GrGLMorphologyEffect::GenKey(*this, caps, b); 362 } 363 364 GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { 365 return new GrGLMorphologyEffect; 366 } 367 bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { 368 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); 369 return (this->radius() == s.radius() && 370 this->direction() == s.direction() && 371 this->useRange() == s.useRange() && 372 this->type() == s.type()); 373 } 374 375 /////////////////////////////////////////////////////////////////////////////// 376 377 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect); 378 379 #if GR_TEST_UTILS 380 std::unique_ptr<GrFragmentProcessor> GrMorphologyEffect::TestCreate(GrProcessorTestData* d) { 381 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx 382 : GrProcessorUnitTest::kAlphaTextureIdx; 383 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); 384 385 Direction dir = d->fRandom->nextBool() ? Direction::kX : Direction::kY; 386 static const int kMaxRadius = 10; 387 int radius = d->fRandom->nextRangeU(1, kMaxRadius); 388 Type type = d->fRandom->nextBool() ? GrMorphologyEffect::Type::kErode 389 : GrMorphologyEffect::Type::kDilate; 390 391 return GrMorphologyEffect::Make(std::move(proxy), dir, radius, type); 392 } 393 #endif 394 395 static void apply_morphology_rect(GrRenderTargetContext* renderTargetContext, 396 const GrClip& clip, 397 sk_sp<GrTextureProxy> proxy, 398 const SkIRect& srcRect, 399 const SkIRect& dstRect, 400 int radius, 401 GrMorphologyEffect::Type morphType, 402 const float bounds[2], 403 GrMorphologyEffect::Direction direction) { 404 GrPaint paint; 405 paint.setGammaCorrect(renderTargetContext->colorSpaceInfo().isGammaCorrect()); 406 407 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), 408 direction, radius, morphType, 409 bounds)); 410 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); 411 renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), 412 SkRect::Make(dstRect), SkRect::Make(srcRect)); 413 } 414 415 static void apply_morphology_rect_no_bounds(GrRenderTargetContext* renderTargetContext, 416 const GrClip& clip, 417 sk_sp<GrTextureProxy> proxy, 418 const SkIRect& srcRect, 419 const SkIRect& dstRect, 420 int radius, 421 GrMorphologyEffect::Type morphType, 422 GrMorphologyEffect::Direction direction) { 423 GrPaint paint; 424 paint.setGammaCorrect(renderTargetContext->colorSpaceInfo().isGammaCorrect()); 425 426 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), 427 direction, radius, morphType)); 428 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); 429 renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), 430 SkRect::Make(dstRect), SkRect::Make(srcRect)); 431 } 432 433 static void apply_morphology_pass(GrRenderTargetContext* renderTargetContext, 434 const GrClip& clip, 435 sk_sp<GrTextureProxy> textureProxy, 436 const SkIRect& srcRect, 437 const SkIRect& dstRect, 438 int radius, 439 GrMorphologyEffect::Type morphType, 440 GrMorphologyEffect::Direction direction) { 441 float bounds[2] = { 0.0f, 1.0f }; 442 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; 443 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; 444 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; 445 if (direction == GrMorphologyEffect::Direction::kX) { 446 bounds[0] = SkIntToScalar(srcRect.left()) + 0.5f; 447 bounds[1] = SkIntToScalar(srcRect.right()) - 0.5f; 448 lowerSrcRect.fRight = srcRect.left() + radius; 449 lowerDstRect.fRight = dstRect.left() + radius; 450 upperSrcRect.fLeft = srcRect.right() - radius; 451 upperDstRect.fLeft = dstRect.right() - radius; 452 middleSrcRect.inset(radius, 0); 453 middleDstRect.inset(radius, 0); 454 } else { 455 bounds[0] = SkIntToScalar(srcRect.top()) + 0.5f; 456 bounds[1] = SkIntToScalar(srcRect.bottom()) - 0.5f; 457 lowerSrcRect.fBottom = srcRect.top() + radius; 458 lowerDstRect.fBottom = dstRect.top() + radius; 459 upperSrcRect.fTop = srcRect.bottom() - radius; 460 upperDstRect.fTop = dstRect.bottom() - radius; 461 middleSrcRect.inset(0, radius); 462 middleDstRect.inset(0, radius); 463 } 464 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { 465 // radius covers srcRect; use bounds over entire draw 466 apply_morphology_rect(renderTargetContext, clip, std::move(textureProxy), 467 srcRect, dstRect, radius, morphType, bounds, direction); 468 } else { 469 // Draw upper and lower margins with bounds; middle without. 470 apply_morphology_rect(renderTargetContext, clip, textureProxy, 471 lowerSrcRect, lowerDstRect, radius, morphType, bounds, direction); 472 apply_morphology_rect(renderTargetContext, clip, textureProxy, 473 upperSrcRect, upperDstRect, radius, morphType, bounds, direction); 474 apply_morphology_rect_no_bounds(renderTargetContext, clip, std::move(textureProxy), 475 middleSrcRect, middleDstRect, radius, morphType, direction); 476 } 477 } 478 479 static sk_sp<SkSpecialImage> apply_morphology( 480 GrContext* context, 481 SkSpecialImage* input, 482 const SkIRect& rect, 483 GrMorphologyEffect::Type morphType, 484 SkISize radius, 485 const SkImageFilter::OutputProperties& outputProperties) { 486 sk_sp<GrTextureProxy> srcTexture(input->asTextureProxyRef(context)); 487 SkASSERT(srcTexture); 488 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace()); 489 GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get()); 490 491 // setup new clip 492 const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->height())); 493 494 const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); 495 SkIRect srcRect = rect; 496 497 SkASSERT(radius.width() > 0 || radius.height() > 0); 498 499 if (radius.fWidth > 0) { 500 sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( 501 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); 502 if (!dstRTContext) { 503 return nullptr; 504 } 505 506 apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), srcRect, dstRect, 507 radius.fWidth, morphType, GrMorphologyEffect::Direction::kX); 508 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, 509 dstRect.width(), radius.fHeight); 510 GrColor clearColor = 511 GrMorphologyEffect::Type::kErode == morphType ? SK_ColorWHITE : SK_ColorTRANSPARENT; 512 dstRTContext->clear(&clearRect, clearColor, GrRenderTargetContext::CanClearFullscreen::kNo); 513 514 srcTexture = dstRTContext->asTextureProxyRef(); 515 srcRect = dstRect; 516 } 517 if (radius.fHeight > 0) { 518 sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( 519 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); 520 if (!dstRTContext) { 521 return nullptr; 522 } 523 524 apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), srcRect, dstRect, 525 radius.fHeight, morphType, GrMorphologyEffect::Direction::kY); 526 527 srcTexture = dstRTContext->asTextureProxyRef(); 528 } 529 530 return SkSpecialImage::MakeDeferredFromGpu(context, 531 SkIRect::MakeWH(rect.width(), rect.height()), 532 kNeedNewImageUniqueID_SpecialImage, 533 std::move(srcTexture), std::move(colorSpace), 534 &input->props()); 535 } 536 #endif 537 538 sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source, 539 const Context& ctx, 540 SkIPoint* offset) const { 541 SkIPoint inputOffset = SkIPoint::Make(0, 0); 542 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); 543 if (!input) { 544 return nullptr; 545 } 546 547 SkIRect bounds; 548 input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds); 549 if (!input) { 550 return nullptr; 551 } 552 553 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), 554 SkIntToScalar(this->radius().height())); 555 ctx.ctm().mapVectors(&radius, 1); 556 int width = SkScalarFloorToInt(radius.fX); 557 int height = SkScalarFloorToInt(radius.fY); 558 559 if (width < 0 || height < 0) { 560 return nullptr; 561 } 562 563 SkIRect srcBounds = bounds; 564 srcBounds.offset(-inputOffset); 565 566 if (0 == width && 0 == height) { 567 offset->fX = bounds.left(); 568 offset->fY = bounds.top(); 569 return input->makeSubset(srcBounds); 570 } 571 572 #if SK_SUPPORT_GPU 573 if (source->isTextureBacked()) { 574 GrContext* context = source->getContext(); 575 576 // Ensure the input is in the destination color space. Typically applyCropRect will have 577 // called pad_image to account for our dilation of bounds, so the result will already be 578 // moved to the destination color space. If a filter DAG avoids that, then we use this 579 // fall-back, which saves us from having to do the xform during the filter itself. 580 input = ImageToColorSpace(input.get(), ctx.outputProperties()); 581 582 auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::Type::kDilate 583 : GrMorphologyEffect::Type::kErode; 584 sk_sp<SkSpecialImage> result(apply_morphology(context, input.get(), srcBounds, type, 585 SkISize::Make(width, height), 586 ctx.outputProperties())); 587 if (result) { 588 offset->fX = bounds.left(); 589 offset->fY = bounds.top(); 590 } 591 return result; 592 } 593 #endif 594 595 SkBitmap inputBM; 596 597 if (!input->getROPixels(&inputBM)) { 598 return nullptr; 599 } 600 601 if (inputBM.colorType() != kN32_SkColorType) { 602 return nullptr; 603 } 604 605 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), 606 inputBM.colorType(), inputBM.alphaType()); 607 608 SkBitmap dst; 609 if (!dst.tryAllocPixels(info)) { 610 return nullptr; 611 } 612 613 SkMorphologyImageFilter::Proc procX, procY; 614 615 if (kDilate_Op == this->op()) { 616 procX = SkOpts::dilate_x; 617 procY = SkOpts::dilate_y; 618 } else { 619 procX = SkOpts::erode_x; 620 procY = SkOpts::erode_y; 621 } 622 623 if (width > 0 && height > 0) { 624 SkBitmap tmp; 625 if (!tmp.tryAllocPixels(info)) { 626 return nullptr; 627 } 628 629 call_proc_X(procX, inputBM, &tmp, width, srcBounds); 630 SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height()); 631 call_proc_Y(procY, 632 tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(), 633 &dst, height, tmpBounds); 634 } else if (width > 0) { 635 call_proc_X(procX, inputBM, &dst, width, srcBounds); 636 } else if (height > 0) { 637 call_proc_Y(procY, 638 inputBM.getAddr32(srcBounds.left(), srcBounds.top()), 639 inputBM.rowBytesAsPixels(), 640 &dst, height, srcBounds); 641 } 642 offset->fX = bounds.left(); 643 offset->fY = bounds.top(); 644 645 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), 646 dst, &source->props()); 647 } 648 649 sk_sp<SkImageFilter> SkMorphologyImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const{ 650 SkASSERT(1 == this->countInputs()); 651 auto input = xformer->apply(this->getInput(0)); 652 if (input.get() != this->getInput(0)) { 653 return (SkMorphologyImageFilter::kDilate_Op == this->op()) 654 ? SkDilateImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), 655 this->getCropRectIfSet()) 656 : SkErodeImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), 657 this->getCropRectIfSet()); 658 } 659 return this->refMe(); 660 } 661