Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: src/effects/SkLightingImageFilter.cpp

Issue 1887103002: Switch SkLightingImageFilter over to new onFilterImage interface (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: switch false -> nullptr Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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> filterImageGPU(SkSpecialImage* source,
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
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::filterImageGPU(SkSpecialIma ge* source,
399 const SkBitmap& src , 400 SkSpecialIma ge* input,
400 const Context& ctx, 401 const SkIRec t& offsetBounds,
401 SkBitmap* result, 402 const SkMatr ix& matrix) const {
402 SkIPoint* offset) c onst { 403 SkASSERT(source->isTextureBacked());
403 SkBitmap input = src; 404
404 SkIPoint srcOffset = SkIPoint::Make(0, 0); 405 GrContext* context = source->getContext();
405 if (!this->filterInputGPUDeprecated(0, proxy, src, ctx, &input, &srcOffset)) { 406
406 return false; 407 sk_sp<GrTexture> inputTexture(input->asTextureRef(context));
407 } 408 SkASSERT(inputTexture);
408 SkIRect srcBounds = input.bounds();
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 409
419 GrSurfaceDesc desc; 410 GrSurfaceDesc desc;
420 desc.fFlags = kRenderTarget_GrSurfaceFlag, 411 desc.fFlags = kRenderTarget_GrSurfaceFlag,
421 desc.fWidth = bounds.width(); 412 desc.fWidth = offsetBounds.width();
422 desc.fHeight = bounds.height(); 413 desc.fHeight = offsetBounds.height();
423 desc.fConfig = kRGBA_8888_GrPixelConfig; 414 desc.fConfig = kRGBA_8888_GrPixelConfig;
424 415
425 SkAutoTUnref<GrTexture> dst(context->textureProvider()->createApproxTexture( desc)); 416 sk_sp<GrTexture> dst(context->textureProvider()->createApproxTexture(desc));
426 if (!dst) { 417 if (!dst) {
427 return false; 418 return nullptr;
428 } 419 }
429 420
421 sk_sp<GrDrawContext> drawContext(context->drawContext(dst->asRenderTarget()) );
422 if (!drawContext) {
423 return nullptr;
424 }
425
426 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(offsetBounds.width()),
427 SkIntToScalar(offsetBounds.height()));
428
430 // setup new clip 429 // setup new clip
431 GrClip clip(dstRect); 430 GrClip clip(dstRect);
432 431
433 offset->fX = bounds.left(); 432 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); 433 SkRect topLeft = SkRect::MakeXYWH(0, 0, 1, 1);
440 SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1); 434 SkRect top = SkRect::MakeXYWH(1, 0, dstRect.width() - 2, 1);
441 SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1); 435 SkRect topRight = SkRect::MakeXYWH(dstRect.width() - 1, 0, 1, 1);
442 SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2); 436 SkRect left = SkRect::MakeXYWH(0, 1, 1, dstRect.height() - 2);
443 SkRect interior = dstRect.makeInset(1, 1); 437 SkRect interior = dstRect.makeInset(1, 1);
444 SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2); 438 SkRect right = SkRect::MakeXYWH(dstRect.width() - 1, 1, 1, dstRect.height() - 2);
445 SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1); 439 SkRect bottomLeft = SkRect::MakeXYWH(0, dstRect.height() - 1, 1, 1);
446 SkRect bottom = SkRect::MakeXYWH(1, dstRect.height() - 1, dstRect.width() - 2, 1); 440 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); 441 SkRect bottomRight = SkRect::MakeXYWH(dstRect.width() - 1, dstRect.height() - 1, 1, 1);
448 442
449 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(dst->asRenderTa rget())); 443 const SkIRect* pSrcBounds = inputBounds.contains(offsetBounds) ? nullptr : & inputBounds;
450 if (!drawContext) { 444 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, topLeft,
451 return false; 445 kTopLeft_BoundaryMode, pSrcBounds, offsetBounds);
452 } 446 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, top, kTo p_BoundaryMode,
447 pSrcBounds, offsetBounds);
448 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, topRight ,
449 kTopRight_BoundaryMode, pSrcBounds, offsetBounds);
450 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, left, kL eft_BoundaryMode,
451 pSrcBounds, offsetBounds);
452 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, interior ,
453 kInterior_BoundaryMode, pSrcBounds, offsetBounds);
454 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, right, k Right_BoundaryMode,
455 pSrcBounds, offsetBounds);
456 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottomLe ft,
457 kBottomLeft_BoundaryMode, pSrcBounds, offsetBounds);
458 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottom,
459 kBottom_BoundaryMode, pSrcBounds, offsetBounds);
460 this->drawRect(drawContext.get(), inputTexture.get(), matrix, clip, bottomRi ght,
461 kBottomRight_BoundaryMode, pSrcBounds, offsetBounds);
453 462
454 const SkIRect* pSrcBounds = srcBounds.contains(bounds) ? nullptr : &srcBound s; 463 return SkSpecialImage::MakeFromGpu(source->internal_getProxy(),
455 this->drawRect(drawContext, srcTexture, matrix, clip, topLeft, kTopLeft_Boun daryMode, 464 SkIRect::MakeWH(offsetBounds.width(), off setBounds.height()),
456 pSrcBounds, bounds); 465 kNeedNewImageUniqueID_SpecialImage,
457 this->drawRect(drawContext, srcTexture, matrix, clip, top, kTop_BoundaryMode , 466 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 } 467 }
476 #endif 468 #endif
477 469
478 class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal { 470 class SkDiffuseLightingImageFilter : public SkLightingImageFilterInternal {
479 public: 471 public:
480 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, 472 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light,
481 SkScalar surfaceScale, 473 SkScalar surfaceScale,
482 SkScalar kd, 474 SkScalar kd,
483 sk_sp<SkImageFilter>, 475 sk_sp<SkImageFilter>,
484 const CropRect*); 476 const CropRect*);
485 477
486 SK_TO_STRING_OVERRIDE() 478 SK_TO_STRING_OVERRIDE()
487 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi lter) 479 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi lter)
488 SkScalar kd() const { return fKD; } 480 SkScalar kd() const { return fKD; }
489 481
490 protected: 482 protected:
491 SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light, SkScalar surfa ceScale, 483 SkDiffuseLightingImageFilter(sk_sp<SkImageFilterLight> light, SkScalar surfa ceScale,
492 SkScalar kd, 484 SkScalar kd,
493 sk_sp<SkImageFilter> input, const CropRect* cro pRect); 485 sk_sp<SkImageFilter> input, const CropRect* cro pRect);
494 void flatten(SkWriteBuffer& buffer) const override; 486 void flatten(SkWriteBuffer& buffer) const override;
495 bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, 487
496 SkBitmap* result, SkIPoint* offset) const overr ide; 488 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
489 SkIPoint* offset) const override;
490
497 #if SK_SUPPORT_GPU 491 #if SK_SUPPORT_GPU
498 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, 492 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds,
499 BoundaryMode) const override; 493 BoundaryMode) const override;
500 #endif 494 #endif
501 495
502 private: 496 private:
503 friend class SkLightingImageFilter; 497 friend class SkLightingImageFilter;
498 SkScalar fKD;
499
504 typedef SkLightingImageFilterInternal INHERITED; 500 typedef SkLightingImageFilterInternal INHERITED;
505 SkScalar fKD;
506 }; 501 };
507 502
508 class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal { 503 class SkSpecularLightingImageFilter : public SkLightingImageFilterInternal {
509 public: 504 public:
510 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light, 505 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilterLight> light,
511 SkScalar surfaceScale, 506 SkScalar surfaceScale,
512 SkScalar ks, SkScalar shininess, 507 SkScalar ks, SkScalar shininess,
513 sk_sp<SkImageFilter>, const CropRect*); 508 sk_sp<SkImageFilter>, const CropRect*);
514 509
515 SK_TO_STRING_OVERRIDE() 510 SK_TO_STRING_OVERRIDE()
516 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF ilter) 511 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF ilter)
517 512
518 SkScalar ks() const { return fKS; } 513 SkScalar ks() const { return fKS; }
519 SkScalar shininess() const { return fShininess; } 514 SkScalar shininess() const { return fShininess; }
520 515
521 protected: 516 protected:
522 SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light, 517 SkSpecularLightingImageFilter(sk_sp<SkImageFilterLight> light,
523 SkScalar surfaceScale, SkScalar ks, 518 SkScalar surfaceScale, SkScalar ks,
524 SkScalar shininess, 519 SkScalar shininess,
525 sk_sp<SkImageFilter> input, const CropRect*); 520 sk_sp<SkImageFilter> input, const CropRect*);
526 void flatten(SkWriteBuffer& buffer) const override; 521 void flatten(SkWriteBuffer& buffer) const override;
527 bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, 522
528 SkBitmap* result, SkIPoint* offset) const overr ide; 523 sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
524 SkIPoint* offset) const override;
525
529 #if SK_SUPPORT_GPU 526 #if SK_SUPPORT_GPU
530 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds, 527 GrFragmentProcessor* getFragmentProcessor(GrTexture*, const SkMatrix&, const SkIRect* bounds,
531 BoundaryMode) const override; 528 BoundaryMode) const override;
532 #endif 529 #endif
533 530
534 private: 531 private:
535 SkScalar fKS; 532 SkScalar fKS;
536 SkScalar fShininess; 533 SkScalar fShininess;
537 friend class SkLightingImageFilter; 534 friend class SkLightingImageFilter;
538 typedef SkLightingImageFilterInternal INHERITED; 535 typedef SkLightingImageFilterInternal INHERITED;
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after
1232 SkScalar surfaceScale = buffer.readScalar(); 1229 SkScalar surfaceScale = buffer.readScalar();
1233 SkScalar kd = buffer.readScalar(); 1230 SkScalar kd = buffer.readScalar();
1234 return Make(std::move(light), surfaceScale, kd, common.getInput(0), &common. cropRect()); 1231 return Make(std::move(light), surfaceScale, kd, common.getInput(0), &common. cropRect());
1235 } 1232 }
1236 1233
1237 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { 1234 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const {
1238 this->INHERITED::flatten(buffer); 1235 this->INHERITED::flatten(buffer);
1239 buffer.writeScalar(fKD); 1236 buffer.writeScalar(fKD);
1240 } 1237 }
1241 1238
1242 bool SkDiffuseLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, 1239 sk_sp<SkSpecialImage> SkDiffuseLightingImageFilter::onFilterImage(SkSpecialImage * source,
1243 const SkBitmap& sourc e, 1240 const Context& ctx,
1244 const Context& ctx, 1241 SkIPoint* offs et) const {
1245 SkBitmap* dst, 1242 SkIPoint inputOffset = SkIPoint::Make(0, 0);
1246 SkIPoint* offset) con st { 1243 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)) ;
1247 SkBitmap src = source; 1244 if (!input) {
1248 SkIPoint srcOffset = SkIPoint::Make(0, 0); 1245 return nullptr;
1249 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) {
1250 return false;
1251 } 1246 }
1252 1247
1253 if (src.colorType() != kN32_SkColorType) { 1248 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y (),
1254 return false; 1249 input->width(), input->height( ));
1255 }
1256 SkIRect srcBounds = src.bounds();
1257 srcBounds.offset(srcOffset);
1258 SkIRect bounds; 1250 SkIRect bounds;
1259 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { 1251 if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
1260 return false; 1252 return nullptr;
1261 } 1253 }
1262 1254
1255 offset->fX = bounds.left();
1256 offset->fY = bounds.top();
1257 bounds.offset(-inputOffset);
1258
1259 #if SK_SUPPORT_GPU
1260 if (source->isTextureBacked()) {
1261 SkMatrix matrix(ctx.ctm());
1262 matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset-> fY));
1263
1264 return this->filterImageGPU(source, input.get(), bounds, matrix);
1265 }
1266 #endif
1267
1263 if (bounds.width() < 2 || bounds.height() < 2) { 1268 if (bounds.width() < 2 || bounds.height() < 2) {
1264 return false; 1269 return nullptr;
1265 } 1270 }
1266 1271
1267 SkAutoLockPixels alp(src); 1272 SkBitmap inputBM;
1268 if (!src.getPixels()) { 1273
1269 return false; 1274 if (!input->getROPixels(&inputBM)) {
1275 return nullptr;
1270 } 1276 }
1271 1277
1272 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds .height())); 1278 if (inputBM.colorType() != kN32_SkColorType) {
1273 if (!device) { 1279 return nullptr;
1274 return false;
1275 } 1280 }
1276 *dst = device->accessBitmap(false); 1281
1277 SkAutoLockPixels alp_dst(*dst); 1282 SkAutoLockPixels alp(inputBM);
1283 if (!inputBM.getPixels()) {
1284 return nullptr;
1285 }
1286
1287 const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.h eight());
1288
1289 SkBitmap dst;
1290 if (!dst.tryAllocPixels(info)) {
1291 return nullptr;
1292 }
1293
1294 SkAutoLockPixels dstLock(dst);
1278 1295
1279 SkMatrix matrix(ctx.ctm()); 1296 SkMatrix matrix(ctx.ctm());
1280 matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset .y())); 1297 matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOf fset.y()));
1281 SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix) ); 1298
1299 sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix));
1282 1300
1283 DiffuseLightingType lightingType(fKD); 1301 DiffuseLightingType lightingType(fKD);
1284 offset->fX = bounds.left();
1285 offset->fY = bounds.top();
1286 bounds.offset(-srcOffset);
1287 switch (transformedLight->type()) { 1302 switch (transformedLight->type()) {
1288 case SkImageFilterLight::kDistant_LightType: 1303 case SkImageFilterLight::kDistant_LightType:
1289 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, 1304 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType,
1290 transformedLight, 1305 transformedLight.ge t(),
1291 src, 1306 inputBM,
1292 dst, 1307 &dst,
1293 surfaceScale(), 1308 surfaceScale(),
1294 bounds); 1309 bounds);
1295 break; 1310 break;
1296 case SkImageFilterLight::kPoint_LightType: 1311 case SkImageFilterLight::kPoint_LightType:
1297 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, 1312 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType,
1298 transformedLight, 1313 transformedLight.get( ),
1299 src, 1314 inputBM,
1300 dst, 1315 &dst,
1301 surfaceScale(), 1316 surfaceScale(),
1302 bounds); 1317 bounds);
1303 break; 1318 break;
1304 case SkImageFilterLight::kSpot_LightType: 1319 case SkImageFilterLight::kSpot_LightType:
1305 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, 1320 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType,
1306 transformedLight, 1321 transformedLight.get() ,
1307 src, 1322 inputBM,
1308 dst, 1323 &dst,
1309 surfaceScale(), 1324 surfaceScale(),
1310 bounds); 1325 bounds);
1311 break; 1326 break;
1312 } 1327 }
1313 1328
1314 return true; 1329 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(),
1330 SkIRect::MakeWH(bounds.width(), bounds .height()),
1331 dst);
1315 } 1332 }
1316 1333
1317 #ifndef SK_IGNORE_TO_STRING 1334 #ifndef SK_IGNORE_TO_STRING
1318 void SkDiffuseLightingImageFilter::toString(SkString* str) const { 1335 void SkDiffuseLightingImageFilter::toString(SkString* str) const {
1319 str->appendf("SkDiffuseLightingImageFilter: ("); 1336 str->appendf("SkDiffuseLightingImageFilter: (");
1320 str->appendf("kD: %f\n", fKD); 1337 str->appendf("kD: %f\n", fKD);
1321 str->append(")"); 1338 str->append(")");
1322 } 1339 }
1323 #endif 1340 #endif
1324 1341
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1378 return Make(std::move(light), surfaceScale, ks, shine, common.getInput(0), 1395 return Make(std::move(light), surfaceScale, ks, shine, common.getInput(0),
1379 &common.cropRect()); 1396 &common.cropRect());
1380 } 1397 }
1381 1398
1382 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { 1399 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const {
1383 this->INHERITED::flatten(buffer); 1400 this->INHERITED::flatten(buffer);
1384 buffer.writeScalar(fKS); 1401 buffer.writeScalar(fKS);
1385 buffer.writeScalar(fShininess); 1402 buffer.writeScalar(fShininess);
1386 } 1403 }
1387 1404
1388 bool SkSpecularLightingImageFilter::onFilterImageDeprecated(Proxy* proxy, 1405 sk_sp<SkSpecialImage> SkSpecularLightingImageFilter::onFilterImage(SkSpecialImag e* source,
1389 const SkBitmap& sour ce, 1406 const Context & ctx,
1390 const Context& ctx, 1407 SkIPoint* off set) const {
1391 SkBitmap* dst, 1408 SkIPoint inputOffset = SkIPoint::Make(0, 0);
1392 SkIPoint* offset) co nst { 1409 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)) ;
1393 SkBitmap src = source; 1410 if (!input) {
1394 SkIPoint srcOffset = SkIPoint::Make(0, 0); 1411 return nullptr;
1395 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) {
1396 return false;
1397 } 1412 }
1398 1413
1399 if (src.colorType() != kN32_SkColorType) { 1414 const SkIRect inputBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y (),
1400 return false; 1415 input->width(), input->height( ));
1416 SkIRect bounds;
1417 if (!this->applyCropRect(ctx, inputBounds, &bounds)) {
1418 return nullptr;
1401 } 1419 }
1402 1420
1403 SkIRect srcBounds = src.bounds(); 1421 offset->fX = bounds.left();
1404 srcBounds.offset(srcOffset); 1422 offset->fY = bounds.top();
1405 SkIRect bounds; 1423 bounds.offset(-inputOffset);
1406 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { 1424
1407 return false; 1425 #if SK_SUPPORT_GPU
1426 if (source->isTextureBacked()) {
1427 SkMatrix matrix(ctx.ctm());
1428 matrix.postTranslate(SkIntToScalar(-offset->fX), SkIntToScalar(-offset-> fY));
1429
1430 return this->filterImageGPU(source, input.get(), bounds, matrix);
1431 }
1432 #endif
1433
1434 if (bounds.width() < 2 || bounds.height() < 2) {
1435 return nullptr;
1408 } 1436 }
1409 1437
1410 if (bounds.width() < 2 || bounds.height() < 2) { 1438 SkBitmap inputBM;
1411 return false; 1439
1440 if (!input->getROPixels(&inputBM)) {
1441 return nullptr;
1412 } 1442 }
1413 1443
1414 SkAutoLockPixels alp(src); 1444 if (inputBM.colorType() != kN32_SkColorType) {
1415 if (!src.getPixels()) { 1445 return nullptr;
1416 return false;
1417 } 1446 }
1418 1447
1419 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds .height())); 1448 SkAutoLockPixels alp(inputBM);
1420 if (!device) { 1449 if (!inputBM.getPixels()) {
1421 return false; 1450 return nullptr;
1422 } 1451 }
1423 *dst = device->accessBitmap(false); 1452
1424 SkAutoLockPixels alp_dst(*dst); 1453 const SkImageInfo info = SkImageInfo::MakeN32Premul(bounds.width(), bounds.h eight());
1454
1455 SkBitmap dst;
1456 if (!dst.tryAllocPixels(info)) {
1457 return nullptr;
1458 }
1459
1460 SkAutoLockPixels dstLock(dst);
1425 1461
1426 SpecularLightingType lightingType(fKS, fShininess); 1462 SpecularLightingType lightingType(fKS, fShininess);
1427 offset->fX = bounds.left(); 1463
1428 offset->fY = bounds.top();
1429 SkMatrix matrix(ctx.ctm()); 1464 SkMatrix matrix(ctx.ctm());
1430 matrix.postTranslate(SkIntToScalar(-srcOffset.x()), SkIntToScalar(-srcOffset .y())); 1465 matrix.postTranslate(SkIntToScalar(-inputOffset.x()), SkIntToScalar(-inputOf fset.y()));
1431 SkAutoTUnref<SkImageFilterLight> transformedLight(light()->transform(matrix) ); 1466
1432 bounds.offset(-srcOffset); 1467 sk_sp<SkImageFilterLight> transformedLight(light()->transform(matrix));
1468
1433 switch (transformedLight->type()) { 1469 switch (transformedLight->type()) {
1434 case SkImageFilterLight::kDistant_LightType: 1470 case SkImageFilterLight::kDistant_LightType:
1435 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, 1471 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType,
1436 transformedLight, 1472 transformedLight.g et(),
1437 src, 1473 inputBM,
1438 dst, 1474 &dst,
1439 surfaceScale(), 1475 surfaceScale(),
1440 bounds); 1476 bounds);
1441 break; 1477 break;
1442 case SkImageFilterLight::kPoint_LightType: 1478 case SkImageFilterLight::kPoint_LightType:
1443 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, 1479 lightBitmap<SpecularLightingType, SkPointLight>(lightingType,
1444 transformedLight, 1480 transformedLight.get (),
1445 src, 1481 inputBM,
1446 dst, 1482 &dst,
1447 surfaceScale(), 1483 surfaceScale(),
1448 bounds); 1484 bounds);
1449 break; 1485 break;
1450 case SkImageFilterLight::kSpot_LightType: 1486 case SkImageFilterLight::kSpot_LightType:
1451 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, 1487 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType,
1452 transformedLight, 1488 transformedLight.get( ),
1453 src, 1489 inputBM,
1454 dst, 1490 &dst,
1455 surfaceScale(), 1491 surfaceScale(),
1456 bounds); 1492 bounds);
1457 break; 1493 break;
1458 } 1494 }
1459 return true; 1495
1496 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(),
1497 SkIRect::MakeWH(bounds.width(), bounds .height()),
1498 dst);
1460 } 1499 }
1461 1500
1462 #ifndef SK_IGNORE_TO_STRING 1501 #ifndef SK_IGNORE_TO_STRING
1463 void SkSpecularLightingImageFilter::toString(SkString* str) const { 1502 void SkSpecularLightingImageFilter::toString(SkString* str) const {
1464 str->appendf("SkSpecularLightingImageFilter: ("); 1503 str->appendf("SkSpecularLightingImageFilter: (");
1465 str->appendf("kS: %f shininess: %f", fKS, fShininess); 1504 str->appendf("kS: %f shininess: %f", fKS, fShininess);
1466 str->append(")"); 1505 str->append(")");
1467 } 1506 }
1468 #endif 1507 #endif
1469 1508
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
2136 2175
2137 fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); 2176 fragBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight);
2138 } 2177 }
2139 2178
2140 #endif 2179 #endif
2141 2180
2142 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) 2181 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter)
2143 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) 2182 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter)
2144 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) 2183 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter)
2145 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 2184 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698