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 "SkLightingImageFilter.h" | 8 #include "SkLightingImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkDevice.h" | |
12 #include "SkPoint3.h" | 11 #include "SkPoint3.h" |
13 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
13 #include "SkSpecialImage.h" | |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
16 | 16 |
17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
18 #include "GrContext.h" | 18 #include "GrContext.h" |
19 #include "GrDrawContext.h" | 19 #include "GrDrawContext.h" |
20 #include "GrFragmentProcessor.h" | 20 #include "GrFragmentProcessor.h" |
21 #include "GrInvariantOutput.h" | 21 #include "GrInvariantOutput.h" |
22 #include "GrPaint.h" | 22 #include "GrPaint.h" |
23 #include "SkGr.h" | 23 #include "SkGr.h" |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 class SkLightingImageFilterInternal : public SkLightingImageFilter { | 348 class SkLightingImageFilterInternal : public SkLightingImageFilter { |
349 protected: | 349 protected: |
350 SkLightingImageFilterInternal(sk_sp<SkImageFilterLight> light, | 350 SkLightingImageFilterInternal(sk_sp<SkImageFilterLight> light, |
351 SkScalar surfaceScale, | 351 SkScalar surfaceScale, |
352 sk_sp<SkImageFilter> input, | 352 sk_sp<SkImageFilter> input, |
353 const CropRect* cropRect) | 353 const CropRect* cropRect) |
354 : INHERITED(std::move(light), surfaceScale, std::move(input), cropRect) { | 354 : INHERITED(std::move(light), surfaceScale, std::move(input), cropRect) { |
355 } | 355 } |
356 | 356 |
357 #if SK_SUPPORT_GPU | 357 #if SK_SUPPORT_GPU |
358 bool canFilterImageGPU() const override { return true; } | 358 sk_sp<SkSpecialImage> onFilterImageGPU(SkSpecialImage* source, |
Stephen White
2016/04/14 18:58:25
microNit: onFoo() is usually reserved for things t
reed1
2016/04/14 19:58:16
Agreed. onFoo almost always means: protected virtu
robertphillips
2016/04/15 11:35:25
Done.
| |
359 bool filterImageGPUDeprecated(Proxy*, const SkBitmap& src, const Context&, | 359 SkSpecialImage* input, |
360 SkBitmap* result, SkIPoint* offset) const over ride; | 360 const SkIRect& bounds, |
361 const SkMatrix& matrix) const; | |
361 virtual GrFragmentProcessor* getFragmentProcessor(GrTexture*, | 362 virtual GrFragmentProcessor* getFragmentProcessor(GrTexture*, |
362 const SkMatrix&, | 363 const SkMatrix&, |
363 const SkIRect* srcBounds, | 364 const SkIRect* srcBounds, |
364 BoundaryMode boundaryMode) const = 0; | 365 BoundaryMode boundaryMode) const = 0; |
365 #endif | 366 #endif |
366 private: | 367 private: |
367 #if SK_SUPPORT_GPU | 368 #if SK_SUPPORT_GPU |
368 void drawRect(GrDrawContext* drawContext, | 369 void drawRect(GrDrawContext* drawContext, |
369 GrTexture* src, | 370 GrTexture* src, |
370 const SkMatrix& matrix, | 371 const SkMatrix& matrix, |
(...skipping 17 matching lines...) Expand all Loading... | |
388 const SkIRect& bounds) const { | 389 const SkIRect& bounds) const { |
389 SkRect srcRect = dstRect.makeOffset(SkIntToScalar(bounds.x()), SkIntToScalar (bounds.y())); | 390 SkRect srcRect = dstRect.makeOffset(SkIntToScalar(bounds.x()), SkIntToScalar (bounds.y())); |
390 GrPaint paint; | 391 GrPaint paint; |
391 // SRGBTODO: AllowSRGBInputs? | 392 // SRGBTODO: AllowSRGBInputs? |
392 GrFragmentProcessor* fp = this->getFragmentProcessor(src, matrix, srcBounds, boundaryMode); | 393 GrFragmentProcessor* fp = this->getFragmentProcessor(src, matrix, srcBounds, boundaryMode); |
393 paint.addColorFragmentProcessor(fp)->unref(); | 394 paint.addColorFragmentProcessor(fp)->unref(); |
394 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 395 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
395 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); | 396 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), dstRect, srcRect); |
396 } | 397 } |
397 | 398 |
398 bool SkLightingImageFilterInternal::filterImageGPUDeprecated(Proxy* proxy, | 399 sk_sp<SkSpecialImage> SkLightingImageFilterInternal::onFilterImageGPU( |
399 const SkBitmap& src , | 400 SkSpecialIma ge* source, |
400 const Context& ctx, | 401 SkSpecialIma ge* input, |
401 SkBitmap* result, | 402 const SkIRec t& offsetBounds, |
402 SkIPoint* offset) c onst { | 403 const SkMatr ix& matrix) const { |
403 SkBitmap input = src; | 404 SkASSERT(source->isTextureBacked()); |
404 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 405 |
405 if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { | 406 GrContext* context = source->getContext(); |
406 return false; | 407 |
407 } | 408 sk_sp<GrTexture> inputTexture(input->asTextureRef(context)); |
408 SkIRect srcBounds = input.bounds(); | 409 SkASSERT(inputTexture); |
409 srcBounds.offset(srcOffset); | |
410 SkIRect bounds; | |
411 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { | |
412 return false; | |
413 } | |
414 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(bounds.width()), | |
415 SkIntToScalar(bounds.height())); | |
416 GrTexture* srcTexture = input.getTexture(); | |
417 GrContext* context = srcTexture->getContext(); | |
418 | 410 |
419 GrSurfaceDesc desc; | 411 GrSurfaceDesc desc; |
420 desc.fFlags = kRenderTarget_GrSurfaceFlag, | 412 desc.fFlags = kRenderTarget_GrSurfaceFlag, |
421 desc.fWidth = bounds.width(); | 413 desc.fWidth = offsetBounds.width(); |
422 desc.fHeight = bounds.height(); | 414 desc.fHeight = offsetBounds.height(); |
423 desc.fConfig = kRGBA_8888_GrPixelConfig; | 415 desc.fConfig = kRGBA_8888_GrPixelConfig; |
424 | 416 |
425 SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture( desc)); | 417 sk_sp<GrTexture> dst(context->textureProvider()->createApproxTexture(desc)); |
426 if (!dst) { | 418 if (!dst) { |
427 return false; | 419 return nullptr; |
428 } | 420 } |
429 | 421 |
422 sk_sp<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget()) ); | |
423 if (!drawContext) { | |
424 return nullptr; | |
425 } | |
426 | |
427 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(offsetBounds.width()), | |
428 SkIntToScalar(offsetBounds.height())); | |
429 | |
430 // setup new clip | 430 // setup new clip |
431 GrClip clip(dstRect); | 431 GrClip clip(dstRect); |
432 | 432 |
433 offset->fX = bounds.left(); | 433 const SkIRect inputBounds = SkIRect::MakeWH(input->width(), input->height()) ; |
434 offset->fY = bounds.top(); | |
435 SkMatrix matrix(ctx.ctm()); | |
436 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.to p())); | |
437 bounds.offset(-srcOffset); | |
438 srcBounds.offset(-srcOffset); | |
439 SkRect topLeft = SkRect::MakeXYWH(0, 0, 1, 1); | 434 SkRect topLeft = SkRect::MakeXYWH(0, 0, 1, 1); |
440 SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1); | 435 SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1); |
441 SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1); | 436 SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1); |
442 SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2); | 437 SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2); |
443 SkRect interior = dstRect.makeInset(1, 1); | 438 SkRect interior = dstRect.makeInset(1, 1); |
444 SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2); | 439 SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2); |
445 SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1); | 440 SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1); |
446 SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1); | 441 SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1); |
447 SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1); | 442 SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1); |
448 | 443 |
449 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTa rget())); | 444 const SkIRect* pSrcBounds = inputBounds.contains(offsetBounds) ? nullptr : & inputBounds; |
450 if (!drawContext) { | 445 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, topLeft, |
451 return false; | 446 kTopLeft_BoundaryMode, pSrcBounds, offsetBounds); |
452 } | 447 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, top, kTo p_BoundaryMode, |
448 pSrcBounds, offsetBounds); | |
449 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, topRight , | |
450 kTopRight_BoundaryMode, pSrcBounds, offsetBounds); | |
451 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, left, kL eft_BoundaryMode, | |
452 pSrcBounds, offsetBounds); | |
453 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, interior , | |
454 kInterior_BoundaryMode, pSrcBounds, offsetBounds); | |
455 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, right, k Right_BoundaryMode, | |
456 pSrcBounds, offsetBounds); | |
457 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottomLe ft, | |
458 kBottomLeft_BoundaryMode, pSrcBounds, offsetBounds); | |
459 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottom, | |
460 kBottom_BoundaryMode, pSrcBounds, offsetBounds); | |
461 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottomRi ght, | |
462 kBottomRight_BoundaryMode, pSrcBounds, offsetBounds); | |
453 | 463 |
454 const SkIRect* pSrcBounds = srcBounds.contains(bounds) ? nullptr : &srcBound s; | 464 return SkSpecialImage::MakeFromGpu(source->internal_getProxy(), |
455 this->drawRect(drawContext, srcTexture, matrix, clip, topLeft, kTopLeft_Boun daryMode, | 465 SkIRect::MakeWH(offsetBounds.width(), off setBounds.height()), |
456 pSrcBounds, bounds); | 466 kNeedNewImageUniqueID_SpecialImage, |
457 this->drawRect(drawContext, srcTexture, matrix, clip, top, kTop_BoundaryMode , | 467 dst.get()); |
458 pSrcBounds, bounds); | |
459 this->drawRect(drawContext, srcTexture, matrix, clip, topRight, kTopRight_Bo undaryMode, | |
460 pSrcBounds, bounds); | |
461 this->drawRect(drawContext, srcTexture, matrix, clip, left, kLeft_BoundaryMo de, | |
462 pSrcBounds, bounds); | |
463 this->drawRect(drawContext, srcTexture, matrix, clip, interior, kInterior_Bo undaryMode, | |
464 pSrcBounds, bounds); | |
465 this->drawRect(drawContext, srcTexture, matrix, clip, right, kRight_Boundary Mode, | |
466 pSrcBounds, bounds); | |
467 this->drawRect(drawContext, srcTexture, matrix, clip, bottomLeft, kBottomLef t_BoundaryMode, | |
468 pSrcBounds, bounds); | |
469 this->drawRect(drawContext, srcTexture, matrix, clip, bottom, kBottom_Bounda ryMode, | |
470 pSrcBounds, bounds); | |
471 this->drawRect(drawContext, srcTexture, matrix, clip, bottomRight, | |
472 kBottomRight_BoundaryMode, pSrcBounds, bounds); | |
473 GrWrapTextureInBitmap(dst, bounds.width(), bounds.height(), false, result); | |
474 return true; | |
475 } | 468 } |
476 #endif | 469 #endif |
477 | 470 |
478 class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal { | 471 class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal { |
479 public: | 472 public: |
480 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, | 473 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, |
481 SkScalar surfaceScale, | 474 SkScalar surfaceScale, |
482 SkScalar kd, | 475 SkScalar kd, |
483 sk_sp<SkImageFilter>, | 476 sk_sp<SkImageFilter>, |
484 const CropRect*); | 477 const CropRect*); |
485 | 478 |
486 SK_TO_STRING_OVERRIDE() | 479 SK_TO_STRING_OVERRIDE() |
487 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi lter) | 480 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi lter) |
488 SkScalar kd() const { return fKD; } | 481 SkScalar kd() const { return fKD; } |
489 | 482 |
490 protected: | 483 protected: |
491 SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light, SkScalar surfa ceScale, | 484 SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light, SkScalar surfa ceScale, |
492 SkScalar kd, | 485 SkScalar kd, |
493 sk_sp<SkImageFilter> input, const CropRect* cro pRect); | 486 sk_sp<SkImageFilter> input, const CropRect* cro pRect); |
494 void flatten(SkWriteBuffer& buffer) const override; | 487 void flatten(SkWriteBuffer& buffer) const override; |
495 bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, | 488 |
496 SkBitmap* result, SkIPoint* offset) const overr ide; | 489 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
490 SkIPoint* offset) const override; | |
491 | |
497 #if SK_SUPPORT_GPU | 492 #if SK_SUPPORT_GPU |
498 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, | 493 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, |
499 BoundaryMode) const override; | 494 BoundaryMode) const override; |
500 #endif | 495 #endif |
501 | 496 |
502 private: | 497 private: |
503 friend class SkLightingImageFilter; | 498 friend class SkLightingImageFilter; |
499 SkScalar fKD; | |
500 | |
504 typedef SkLightingImageFilterInternal INHERITED; | 501 typedef SkLightingImageFilterInternal INHERITED; |
505 SkScalar fKD; | |
506 }; | 502 }; |
507 | 503 |
508 class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal { | 504 class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal { |
509 public: | 505 public: |
510 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, | 506 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, |
511 SkScalar surfaceScale, | 507 SkScalar surfaceScale, |
512 SkScalar ks, SkScalar shininess, | 508 SkScalar ks, SkScalar shininess, |
513 sk_sp<SkImageFilter>, const CropRect*); | 509 sk_sp<SkImageFilter>, const CropRect*); |
514 | 510 |
515 SK_TO_STRING_OVERRIDE() | 511 SK_TO_STRING_OVERRIDE() |
516 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF ilter) | 512 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF ilter) |
517 | 513 |
518 SkScalar ks() const { return fKS; } | 514 SkScalar ks() const { return fKS; } |
519 SkScalar shininess() const { return fShininess; } | 515 SkScalar shininess() const { return fShininess; } |
520 | 516 |
521 protected: | 517 protected: |
522 SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light, | 518 SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light, |
523 SkScalar surfaceScale, SkScalar ks, | 519 SkScalar surfaceScale, SkScalar ks, |
524 SkScalar shininess, | 520 SkScalar shininess, |
525 sk_sp<SkImageFilter> input, const CropRect*); | 521 sk_sp<SkImageFilter> input, const CropRect*); |
526 void flatten(SkWriteBuffer& buffer) const override; | 522 void flatten(SkWriteBuffer& buffer) const override; |
527 bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, | 523 |
528 SkBitmap* result, SkIPoint* offset) const overr ide; | 524 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
525 SkIPoint* offset) const override; | |
526 | |
529 #if SK_SUPPORT_GPU | 527 #if SK_SUPPORT_GPU |
530 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, | 528 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, |
531 BoundaryMode) const override; | 529 BoundaryMode) const override; |
532 #endif | 530 #endif |
533 | 531 |
534 private: | 532 private: |
535 SkScalar fKS; | 533 SkScalar fKS; |
536 SkScalar fShininess; | 534 SkScalar fShininess; |
537 friend class SkLightingImageFilter; | 535 friend class SkLightingImageFilter; |
538 typedef SkLightingImageFilterInternal INHERITED; | 536 typedef SkLightingImageFilterInternal INHERITED; |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1232 SkScalar surfaceScale = buffer.readScalar(); | 1230 SkScalar surfaceScale = buffer.readScalar(); |
1233 SkScalar kd = buffer.readScalar(); | 1231 SkScalar kd = buffer.readScalar(); |
1234 return Make(std::move(light), surfaceScale, kd, common.getInput(0), &common. cropRect()); | 1232 return Make(std::move(light), surfaceScale, kd, common.getInput(0), &common. cropRect()); |
1235 } | 1233 } |
1236 | 1234 |
1237 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 1235 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
1238 this->INHERITED::flatten(buffer); | 1236 this->INHERITED::flatten(buffer); |
1239 buffer.writeScalar(fKD); | 1237 buffer.writeScalar(fKD); |
1240 } | 1238 } |
1241 | 1239 |
1242 bool SkDiffuseLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, | 1240 sk_sp<SkSpecialImage> SkDiffuseLightingImageFilter::onFilterImage(SkSpecialImage * source, |
1243 const SkBitmap& sourc e, | 1241 const Context& ctx, |
1244 const Context& ctx, | 1242 SkIPoint* offs et) const { |
1245 SkBitmap* dst, | 1243 SkIPoint inputOffset = SkIPoint::Make(0, 0); |
1246 SkIPoint* offset) con st { | 1244 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)) ; |
1247 SkBitmap src = source; | 1245 if (!input) { |
1248 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 1246 return nullptr; |
1249 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { | |
1250 return false; | |
1251 } | 1247 } |
1252 | 1248 |
1253 if (src.colorType() != kN32_SkColorType) { | 1249 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y (), |
1254 return false; | 1250 input->width(), input->height( )); |
1255 } | |
1256 SkIRect srcBounds = src.bounds(); | |
1257 srcBounds.offset(srcOffset); | |
1258 SkIRect bounds; | 1251 SkIRect bounds; |
1259 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { | 1252 if (!this->applyCropRect(ctx, inputBounds, &bounds)) { |
1260 return false; | 1253 return nullptr; |
1261 } | 1254 } |
1262 | 1255 |
1256 offset->fX = bounds.left(); | |
1257 offset->fY = bounds.top(); | |
1258 bounds.offset(-inputOffset); | |
1259 | |
1260 #if SK_SUPPORT_GPU | |
1261 if (source->isTextureBacked()) { | |
1262 SkMatrix matrix(ctx.ctm()); | |
1263 matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset-> fY)); | |
1264 | |
1265 return this->onFilterImageGPU(source, input.get(), bounds, matrix); | |
1266 } | |
1267 #endif | |
1268 | |
1263 if (bounds.width() < 2 || bounds.height() < 2) { | 1269 if (bounds.width() < 2 || bounds.height() < 2) { |
1264 return false; | 1270 return nullptr; |
1265 } | 1271 } |
1266 | 1272 |
1267 SkAutoLockPixels alp(src); | 1273 SkBitmap inputBM; |
1268 if (!src.getPixels()) { | 1274 |
1269 return false; | 1275 if (!input->getROPixels(&inputBM)) { |
1276 return nullptr; | |
1270 } | 1277 } |
1271 | 1278 |
1272 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds .height())); | 1279 if (inputBM.colorType() != kN32_SkColorType) { |
1273 if (!device) { | 1280 return nullptr; |
1274 return false; | |
1275 } | 1281 } |
1276 *dst = device->accessBitmap(false); | 1282 |
1277 SkAutoLockPixels alp_dst(*dst); | 1283 SkAutoLockPixels alp(inputBM); |
1284 if (!inputBM.getPixels()) { | |
1285 return nullptr; | |
1286 } | |
1287 | |
1288 const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.h eight()); | |
1289 | |
1290 SkBitmap dst; | |
1291 if (!dst.tryAllocPixels(info)) { | |
1292 return nullptr; | |
1293 } | |
1294 | |
1295 SkAutoLockPixels dstLock(dst); | |
1278 | 1296 |
1279 SkMatrix matrix(ctx.ctm()); | 1297 SkMatrix matrix(ctx.ctm()); |
1280 matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset .y())); | 1298 matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOf fset.y())); |
1281 SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix) ); | 1299 |
1300 sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix)); | |
1282 | 1301 |
1283 DiffuseLightingType lightingType(fKD); | 1302 DiffuseLightingType lightingType(fKD); |
1284 offset->fX = bounds.left(); | |
1285 offset->fY = bounds.top(); | |
1286 bounds.offset(-srcOffset); | |
1287 switch (transformedLight->type()) { | 1303 switch (transformedLight->type()) { |
1288 case SkImageFilterLight::kDistant_LightType: | 1304 case SkImageFilterLight::kDistant_LightType: |
1289 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, | 1305 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, |
1290 transformedLight, | 1306 transformedLight.ge t(), |
1291 src, | 1307 inputBM, |
1292 dst, | 1308 &dst, |
1293 surfaceScale(), | 1309 surfaceScale(), |
1294 bounds); | 1310 bounds); |
1295 break; | 1311 break; |
1296 case SkImageFilterLight::kPoint_LightType: | 1312 case SkImageFilterLight::kPoint_LightType: |
1297 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, | 1313 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, |
1298 transformedLight, | 1314 transformedLight.get( ), |
1299 src, | 1315 inputBM, |
1300 dst, | 1316 &dst, |
1301 surfaceScale(), | 1317 surfaceScale(), |
1302 bounds); | 1318 bounds); |
1303 break; | 1319 break; |
1304 case SkImageFilterLight::kSpot_LightType: | 1320 case SkImageFilterLight::kSpot_LightType: |
1305 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, | 1321 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, |
1306 transformedLight, | 1322 transformedLight.get() , |
1307 src, | 1323 inputBM, |
1308 dst, | 1324 &dst, |
1309 surfaceScale(), | 1325 surfaceScale(), |
1310 bounds); | 1326 bounds); |
1311 break; | 1327 break; |
1312 } | 1328 } |
1313 | 1329 |
1314 return true; | 1330 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(), |
1331 SkIRect::MakeWH(bounds.width(), bounds .height()), | |
1332 dst); | |
1315 } | 1333 } |
1316 | 1334 |
1317 #ifndef SK_IGNORE_TO_STRING | 1335 #ifndef SK_IGNORE_TO_STRING |
1318 void SkDiffuseLightingImageFilter::toString(SkString* str) const { | 1336 void SkDiffuseLightingImageFilter::toString(SkString* str) const { |
1319 str->appendf("SkDiffuseLightingImageFilter: ("); | 1337 str->appendf("SkDiffuseLightingImageFilter: ("); |
1320 str->appendf("kD: %f\n", fKD); | 1338 str->appendf("kD: %f\n", fKD); |
1321 str->append(")"); | 1339 str->append(")"); |
1322 } | 1340 } |
1323 #endif | 1341 #endif |
1324 | 1342 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1378 return Make(std::move(light), surfaceScale, ks, shine, common.getInput(0), | 1396 return Make(std::move(light), surfaceScale, ks, shine, common.getInput(0), |
1379 &common.cropRect()); | 1397 &common.cropRect()); |
1380 } | 1398 } |
1381 | 1399 |
1382 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 1400 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
1383 this->INHERITED::flatten(buffer); | 1401 this->INHERITED::flatten(buffer); |
1384 buffer.writeScalar(fKS); | 1402 buffer.writeScalar(fKS); |
1385 buffer.writeScalar(fShininess); | 1403 buffer.writeScalar(fShininess); |
1386 } | 1404 } |
1387 | 1405 |
1388 bool SkSpecularLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, | 1406 sk_sp<SkSpecialImage> SkSpecularLightingImageFilter::onFilterImage(SkSpecialImag e* source, |
1389 const SkBitmap& sour ce, | 1407 const Context & ctx, |
1390 const Context& ctx, | 1408 SkIPoint* off set) const { |
1391 SkBitmap* dst, | 1409 SkIPoint inputOffset = SkIPoint::Make(0, 0); |
1392 SkIPoint* offset) co nst { | 1410 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)) ; |
1393 SkBitmap src = source; | 1411 if (!input) { |
1394 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 1412 return nullptr; |
1395 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { | 1413 } |
1414 | |
1415 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y (), | |
1416 input->width(), input->height( )); | |
1417 SkIRect bounds; | |
1418 if (!this->applyCropRect(ctx, inputBounds, &bounds)) { | |
1419 return nullptr; | |
1420 } | |
1421 | |
1422 offset->fX = bounds.left(); | |
1423 offset->fY = bounds.top(); | |
1424 bounds.offset(-inputOffset); | |
1425 | |
1426 #if SK_SUPPORT_GPU | |
1427 if (source->isTextureBacked()) { | |
1428 SkMatrix matrix(ctx.ctm()); | |
1429 matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset-> fY)); | |
1430 | |
1431 return this->onFilterImageGPU(source, input.get(), bounds, matrix); | |
1432 } | |
1433 #endif | |
1434 | |
1435 if (bounds.width() < 2 || bounds.height() < 2) { | |
1436 return nullptr; | |
1437 } | |
1438 | |
1439 SkBitmap inputBM; | |
1440 | |
1441 if (!input->getROPixels(&inputBM)) { | |
1442 return nullptr; | |
1443 } | |
1444 | |
1445 if (inputBM.colorType() != kN32_SkColorType) { | |
1396 return false; | 1446 return false; |
1397 } | 1447 } |
1398 | 1448 |
1399 if (src.colorType() != kN32_SkColorType) { | 1449 SkAutoLockPixels alp(inputBM); |
1450 if (!inputBM.getPixels()) { | |
1400 return false; | 1451 return false; |
1401 } | 1452 } |
1402 | 1453 |
1403 SkIRect srcBounds = src.bounds(); | 1454 const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.h eight()); |
1404 srcBounds.offset(srcOffset); | 1455 |
1405 SkIRect bounds; | 1456 SkBitmap dst; |
1406 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { | 1457 if (!dst.tryAllocPixels(info)) { |
1407 return false; | 1458 return nullptr; |
1408 } | 1459 } |
1409 | 1460 |
1410 if (bounds.width() < 2 || bounds.height() < 2) { | 1461 SkAutoLockPixels dstLock(dst); |
1411 return false; | |
1412 } | |
1413 | |
1414 SkAutoLockPixels alp(src); | |
1415 if (!src.getPixels()) { | |
1416 return false; | |
1417 } | |
1418 | |
1419 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds .height())); | |
1420 if (!device) { | |
1421 return false; | |
1422 } | |
1423 *dst = device->accessBitmap(false); | |
1424 SkAutoLockPixels alp_dst(*dst); | |
1425 | 1462 |
1426 SpecularLightingType lightingType(fKS, fShininess); | 1463 SpecularLightingType lightingType(fKS, fShininess); |
1427 offset->fX = bounds.left(); | 1464 |
1428 offset->fY = bounds.top(); | |
1429 SkMatrix matrix(ctx.ctm()); | 1465 SkMatrix matrix(ctx.ctm()); |
1430 matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset .y())); | 1466 matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOf fset.y())); |
1431 SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix) ); | 1467 |
1432 bounds.offset(-srcOffset); | 1468 sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix)); |
1469 | |
1433 switch (transformedLight->type()) { | 1470 switch (transformedLight->type()) { |
1434 case SkImageFilterLight::kDistant_LightType: | 1471 case SkImageFilterLight::kDistant_LightType: |
1435 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, | 1472 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, |
1436 transformedLight, | 1473 transformedLight.g et(), |
1437 src, | 1474 inputBM, |
1438 dst, | 1475 &dst, |
1439 surfaceScale(), | 1476 surfaceScale(), |
1440 bounds); | 1477 bounds); |
1441 break; | 1478 break; |
1442 case SkImageFilterLight::kPoint_LightType: | 1479 case SkImageFilterLight::kPoint_LightType: |
1443 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, | 1480 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, |
1444 transformedLight, | 1481 transformedLight.get (), |
1445 src, | 1482 inputBM, |
1446 dst, | 1483 &dst, |
1447 surfaceScale(), | 1484 surfaceScale(), |
1448 bounds); | 1485 bounds); |
1449 break; | 1486 break; |
1450 case SkImageFilterLight::kSpot_LightType: | 1487 case SkImageFilterLight::kSpot_LightType: |
1451 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, | 1488 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, |
1452 transformedLight, | 1489 transformedLight.get( ), |
1453 src, | 1490 inputBM, |
1454 dst, | 1491 &dst, |
1455 surfaceScale(), | 1492 surfaceScale(), |
1456 bounds); | 1493 bounds); |
1457 break; | 1494 break; |
1458 } | 1495 } |
1459 return true; | 1496 |
1497 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(), | |
1498 SkIRect::MakeWH(bounds.width(), bounds .height()), | |
1499 dst); | |
1460 } | 1500 } |
1461 | 1501 |
1462 #ifndef SK_IGNORE_TO_STRING | 1502 #ifndef SK_IGNORE_TO_STRING |
1463 void SkSpecularLightingImageFilter::toString(SkString* str) const { | 1503 void SkSpecularLightingImageFilter::toString(SkString* str) const { |
1464 str->appendf("SkSpecularLightingImageFilter: ("); | 1504 str->appendf("SkSpecularLightingImageFilter: ("); |
1465 str->appendf("kS: %f shininess: %f", fKS, fShininess); | 1505 str->appendf("kS: %f shininess: %f", fKS, fShininess); |
1466 str->append(")"); | 1506 str->append(")"); |
1467 } | 1507 } |
1468 #endif | 1508 #endif |
1469 | 1509 |
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2136 | 2176 |
2137 fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 2177 fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
2138 } | 2178 } |
2139 | 2179 |
2140 #endif | 2180 #endif |
2141 | 2181 |
2142 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 2182 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
2143 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 2183 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
2144 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 2184 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
2145 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 2185 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |