OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 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 "SkMorphologyImageFilter.h" | 8 #include "SkMorphologyImageFilter.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkOpts.h" | 12 #include "SkOpts.h" |
13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
14 #include "SkRect.h" | 14 #include "SkRect.h" |
15 #include "SkSpecialImage.h" | 15 #include "SkSpecialImage.h" |
16 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
17 | 17 |
18 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
19 #include "GrContext.h" | 19 #include "GrContext.h" |
20 #include "GrRenderTargetContext.h" | 20 #include "GrRenderTargetContext.h" |
21 #include "GrFixedClip.h" | 21 #include "GrFixedClip.h" |
22 #include "GrInvariantOutput.h" | 22 #include "GrInvariantOutput.h" |
23 #include "GrTexture.h" | 23 #include "GrTexture.h" |
| 24 #include "GrTextureProxy.h" |
24 #include "SkGr.h" | 25 #include "SkGr.h" |
25 #include "SkGrPriv.h" | 26 #include "SkGrPriv.h" |
26 #include "effects/Gr1DKernelEffect.h" | 27 #include "effects/Gr1DKernelEffect.h" |
27 #include "glsl/GrGLSLFragmentProcessor.h" | 28 #include "glsl/GrGLSLFragmentProcessor.h" |
28 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 29 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
29 #include "glsl/GrGLSLProgramDataManager.h" | 30 #include "glsl/GrGLSLProgramDataManager.h" |
30 #include "glsl/GrGLSLUniformHandler.h" | 31 #include "glsl/GrGLSLUniformHandler.h" |
31 #endif | 32 #endif |
32 | 33 |
33 sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY, | 34 sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int ra
dius, | 149 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int ra
dius, |
149 MorphologyType type) { | 150 MorphologyType type) { |
150 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type)); | 151 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type)); |
151 } | 152 } |
152 | 153 |
153 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int ra
dius, | 154 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int ra
dius, |
154 MorphologyType type, float bounds[2])
{ | 155 MorphologyType type, float bounds[2])
{ |
155 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type, bounds)); | 156 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type, bounds)); |
156 } | 157 } |
157 | 158 |
| 159 static sk_sp<GrFragmentProcessor> Make(GrTextureProxy* tex, Direction dir, i
nt radius, |
| 160 MorphologyType type) { |
| 161 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type)); |
| 162 } |
| 163 |
| 164 static sk_sp<GrFragmentProcessor> Make(GrTextureProxy* tex, Direction dir, i
nt radius, |
| 165 MorphologyType type, float bounds[2])
{ |
| 166 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radiu
s, type, bounds)); |
| 167 } |
| 168 |
158 virtual ~GrMorphologyEffect(); | 169 virtual ~GrMorphologyEffect(); |
159 | 170 |
160 MorphologyType type() const { return fType; } | 171 MorphologyType type() const { return fType; } |
161 bool useRange() const { return fUseRange; } | 172 bool useRange() const { return fUseRange; } |
162 const float* range() const { return fRange; } | 173 const float* range() const { return fRange; } |
163 | 174 |
164 const char* name() const override { return "Morphology"; } | 175 const char* name() const override { return "Morphology"; } |
165 | 176 |
166 protected: | 177 protected: |
167 | 178 |
168 MorphologyType fType; | 179 MorphologyType fType; |
169 bool fUseRange; | 180 bool fUseRange; |
170 float fRange[2]; | 181 float fRange[2]; |
171 | 182 |
172 private: | 183 private: |
173 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; | 184 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
174 | 185 |
175 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const
override; | 186 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const
override; |
176 | 187 |
177 bool onIsEqual(const GrFragmentProcessor&) const override; | 188 bool onIsEqual(const GrFragmentProcessor&) const override; |
178 | 189 |
179 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | 190 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
180 | 191 |
181 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); | 192 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); |
182 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, | 193 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, float
bounds[2]); |
183 float bounds[2]); | 194 |
| 195 GrMorphologyEffect(GrTextureProxy*, Direction, int radius, MorphologyType); |
| 196 GrMorphologyEffect(GrTextureProxy*, Direction, int radius, MorphologyType, f
loat bounds[2]); |
184 | 197 |
185 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 198 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
186 | 199 |
187 typedef Gr1DKernelEffect INHERITED; | 200 typedef Gr1DKernelEffect INHERITED; |
188 }; | 201 }; |
189 | 202 |
190 /////////////////////////////////////////////////////////////////////////////// | 203 /////////////////////////////////////////////////////////////////////////////// |
191 | 204 |
192 class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { | 205 class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { |
193 public: | 206 public: |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 int radius = d->fRandom->nextRangeU(1, kMaxRadius); | 393 int radius = d->fRandom->nextRangeU(1, kMaxRadius); |
381 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_Mo
rphologyType : | 394 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_Mo
rphologyType : |
382 GrMorphologyEffect::kDilate_Morph
ologyType; | 395 GrMorphologyEffect::kDilate_Morph
ologyType; |
383 | 396 |
384 return GrMorphologyEffect::Make(d->fTextures[texIdx], dir, radius, type); | 397 return GrMorphologyEffect::Make(d->fTextures[texIdx], dir, radius, type); |
385 } | 398 } |
386 | 399 |
387 | 400 |
388 static void apply_morphology_rect(GrRenderTargetContext* renderTargetContext, | 401 static void apply_morphology_rect(GrRenderTargetContext* renderTargetContext, |
389 const GrClip& clip, | 402 const GrClip& clip, |
390 GrTexture* texture, | 403 GrTextureProxy* texture, |
391 const SkIRect& srcRect, | 404 const SkIRect& srcRect, |
392 const SkIRect& dstRect, | 405 const SkIRect& dstRect, |
393 int radius, | 406 int radius, |
394 GrMorphologyEffect::MorphologyType morphType, | 407 GrMorphologyEffect::MorphologyType morphType, |
395 float bounds[2], | 408 float bounds[2], |
396 Gr1DKernelEffect::Direction direction) { | 409 Gr1DKernelEffect::Direction direction) { |
397 GrPaint paint; | 410 GrPaint paint; |
398 paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); | 411 paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
399 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(texture, | 412 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(texture, |
400 direction, | 413 direction, |
401 radius, | 414 radius, |
402 morphType, | 415 morphType, |
403 bounds)); | 416 bounds)); |
404 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); | 417 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
405 renderTargetContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make
(dstRect), | 418 renderTargetContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make
(dstRect), |
406 SkRect::Make(srcRect)); | 419 SkRect::Make(srcRect)); |
407 } | 420 } |
408 | 421 |
409 static void apply_morphology_rect_no_bounds(GrRenderTargetContext* renderTargetC
ontext, | 422 static void apply_morphology_rect_no_bounds(GrRenderTargetContext* renderTargetC
ontext, |
410 const GrClip& clip, | 423 const GrClip& clip, |
411 GrTexture* texture, | 424 GrTextureProxy* texture, |
412 const SkIRect& srcRect, | 425 const SkIRect& srcRect, |
413 const SkIRect& dstRect, | 426 const SkIRect& dstRect, |
414 int radius, | 427 int radius, |
415 GrMorphologyEffect::MorphologyType m
orphType, | 428 GrMorphologyEffect::MorphologyType m
orphType, |
416 Gr1DKernelEffect::Direction directio
n) { | 429 Gr1DKernelEffect::Direction directio
n) { |
417 GrPaint paint; | 430 GrPaint paint; |
418 paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); | 431 paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
419 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(texture, direction,
radius, | 432 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(texture, direction,
radius, |
420 morphType)); | 433 morphType)); |
421 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); | 434 paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
422 renderTargetContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make
(dstRect), | 435 renderTargetContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make
(dstRect), |
423 SkRect::Make(srcRect)); | 436 SkRect::Make(srcRect)); |
424 } | 437 } |
425 | 438 |
426 static void apply_morphology_pass(GrRenderTargetContext* renderTargetContext, | 439 static void apply_morphology_pass(GrRenderTargetContext* renderTargetContext, |
427 const GrClip& clip, | 440 const GrClip& clip, |
428 GrTexture* texture, | 441 GrTextureProxy* texture, |
429 const SkIRect& srcRect, | 442 const SkIRect& srcRect, |
430 const SkIRect& dstRect, | 443 const SkIRect& dstRect, |
431 int radius, | 444 int radius, |
432 GrMorphologyEffect::MorphologyType morphType, | 445 GrMorphologyEffect::MorphologyType morphType, |
433 Gr1DKernelEffect::Direction direction) { | 446 Gr1DKernelEffect::Direction direction) { |
434 float bounds[2] = { 0.0f, 1.0f }; | 447 float bounds[2] = { 0.0f, 1.0f }; |
435 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; | 448 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
436 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; | 449 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
437 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; | 450 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
438 if (direction == Gr1DKernelEffect::kX_Direction) { | 451 if (direction == Gr1DKernelEffect::kX_Direction) { |
(...skipping 30 matching lines...) Expand all Loading... |
469 } | 482 } |
470 } | 483 } |
471 | 484 |
472 static sk_sp<SkSpecialImage> apply_morphology( | 485 static sk_sp<SkSpecialImage> apply_morphology( |
473 GrContext* context, | 486 GrContext* context, |
474 SkSpecialImage* input, | 487 SkSpecialImage* input, |
475 const SkIRect& rect, | 488 const SkIRect& rect, |
476 GrMorphologyEffect::MorphologyType mor
phType, | 489 GrMorphologyEffect::MorphologyType mor
phType, |
477 SkISize radius, | 490 SkISize radius, |
478 const SkImageFilter::OutputProperties&
outputProperties) { | 491 const SkImageFilter::OutputProperties&
outputProperties) { |
479 sk_sp<GrTexture> srcTexture(input->asTextureRef(context)); | 492 sk_sp<GrTextureProxy> srcTexture(input->asTextureProxy(context)); |
480 SkASSERT(srcTexture); | 493 SkASSERT(srcTexture); |
481 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace()); | 494 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace()); |
482 GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get()); | 495 GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get()); |
483 | 496 |
484 // setup new clip | 497 // setup new clip |
485 const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->heig
ht())); | 498 const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->heig
ht())); |
486 | 499 |
487 const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); | 500 const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
488 SkIRect srcRect = rect; | 501 SkIRect srcRect = rect; |
489 | 502 |
490 SkASSERT(radius.width() > 0 || radius.height() > 0); | 503 SkASSERT(radius.width() > 0 || radius.height() > 0); |
491 | 504 |
492 if (radius.fWidth > 0) { | 505 if (radius.fWidth > 0) { |
493 sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeRenderT
argetContext( | 506 sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeRenderT
argetContext( |
494 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpa
ce)); | 507 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpa
ce)); |
495 if (!dstRenderTargetContext) { | 508 if (!dstRenderTargetContext) { |
496 return nullptr; | 509 return nullptr; |
497 } | 510 } |
498 | 511 |
499 apply_morphology_pass(dstRenderTargetContext.get(), clip, srcTexture.get
(), | 512 apply_morphology_pass(dstRenderTargetContext.get(), clip, srcTexture.get
(), |
500 srcRect, dstRect, radius.fWidth, morphType, | 513 srcRect, dstRect, radius.fWidth, morphType, |
501 Gr1DKernelEffect::kX_Direction); | 514 Gr1DKernelEffect::kX_Direction); |
502 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, | 515 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
503 dstRect.width(), radius.fHeight); | 516 dstRect.width(), radius.fHeight); |
504 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype | 517 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype |
505 ? SK_ColorWHITE | 518 ? SK_ColorWHITE |
506 : SK_ColorTRANSPARENT; | 519 : SK_ColorTRANSPARENT; |
507 dstRenderTargetContext->clear(&clearRect, clearColor, false); | 520 dstRenderTargetContext->clear(&clearRect, clearColor, false); |
508 | 521 |
509 srcTexture = dstRenderTargetContext->asTexture(); | 522 srcTexture = dstRenderTargetContext->asDeferredTexture(); |
510 srcRect = dstRect; | 523 srcRect = dstRect; |
511 } | 524 } |
512 if (radius.fHeight > 0) { | 525 if (radius.fHeight > 0) { |
513 sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeRenderT
argetContext( | 526 sk_sp<GrRenderTargetContext> dstRenderTargetContext(context->makeRenderT
argetContext( |
514 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpa
ce)); | 527 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpa
ce)); |
515 if (!dstRenderTargetContext) { | 528 if (!dstRenderTargetContext) { |
516 return nullptr; | 529 return nullptr; |
517 } | 530 } |
518 | 531 |
519 apply_morphology_pass(dstRenderTargetContext.get(), clip, srcTexture.get
(), | 532 apply_morphology_pass(dstRenderTargetContext.get(), clip, srcTexture.get
(), |
520 srcRect, dstRect, radius.fHeight, morphType, | 533 srcRect, dstRect, radius.fHeight, morphType, |
521 Gr1DKernelEffect::kY_Direction); | 534 Gr1DKernelEffect::kY_Direction); |
522 | 535 |
523 srcTexture = dstRenderTargetContext->asTexture(); | 536 srcTexture = dstRenderTargetContext->asDeferredTexture(); |
524 } | 537 } |
525 | 538 |
526 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(rect.width(), rect.height
()), | 539 return SkSpecialImage::MakeDeferredFromGpu(SkIRect::MakeWH(rect.width(), rec
t.height()), |
527 kNeedNewImageUniqueID_SpecialImage, | 540 kNeedNewImageUniqueID_SpecialImag
e, |
528 std::move(srcTexture), std::move(colorSpa
ce), | 541 std::move(srcTexture), std::move(
colorSpace), |
529 &input->props()); | 542 &input->props()); |
530 } | 543 } |
531 #endif | 544 #endif |
532 | 545 |
533 sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* sou
rce, | 546 sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* sou
rce, |
534 const Context& ctx, | 547 const Context& ctx, |
535 SkIPoint* offset) c
onst { | 548 SkIPoint* offset) c
onst { |
536 SkIPoint inputOffset = SkIPoint::Make(0, 0); | 549 SkIPoint inputOffset = SkIPoint::Make(0, 0); |
537 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset))
; | 550 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset))
; |
538 if (!input) { | 551 if (!input) { |
539 return nullptr; | 552 return nullptr; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 inputBM.getAddr32(srcBounds.left(), srcBounds.top()), | 644 inputBM.getAddr32(srcBounds.left(), srcBounds.top()), |
632 inputBM.rowBytesAsPixels(), | 645 inputBM.rowBytesAsPixels(), |
633 &dst, height, srcBounds); | 646 &dst, height, srcBounds); |
634 } | 647 } |
635 offset->fX = bounds.left(); | 648 offset->fX = bounds.left(); |
636 offset->fY = bounds.top(); | 649 offset->fY = bounds.top(); |
637 | 650 |
638 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds
.height()), | 651 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds
.height()), |
639 dst, &source->props()); | 652 dst, &source->props()); |
640 } | 653 } |
OLD | NEW |