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" |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 buffer.writeScalar(point.fZ); | 258 buffer.writeScalar(point.fZ); |
259 }; | 259 }; |
260 | 260 |
261 class SkDiffuseLightingImageFilter : public SkLightingImageFilter { | 261 class SkDiffuseLightingImageFilter : public SkLightingImageFilter { |
262 public: | 262 public: |
263 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, | 263 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, |
264 SkScalar kd, SkImageFilter* input, const SkIRec
t* cropRect); | 264 SkScalar kd, SkImageFilter* input, const SkIRec
t* cropRect); |
265 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) | 265 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) |
266 | 266 |
267 #if SK_SUPPORT_GPU | 267 #if SK_SUPPORT_GPU |
268 virtual bool asNewEffect(GrEffectRef** effect, GrTexture*, const SkIPoint& o
ffset) const SK_OVERRIDE; | 268 virtual bool asNewEffect(GrEffectRef** effect, GrTexture*, const SkMatrix& m
atrix) const SK_OVERRIDE; |
269 #endif | 269 #endif |
270 SkScalar kd() const { return fKD; } | 270 SkScalar kd() const { return fKD; } |
271 | 271 |
272 protected: | 272 protected: |
273 explicit SkDiffuseLightingImageFilter(SkFlattenableReadBuffer& buffer); | 273 explicit SkDiffuseLightingImageFilter(SkFlattenableReadBuffer& buffer); |
274 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE; | 274 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE; |
275 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, | 275 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, |
276 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; | 276 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; |
277 | 277 |
278 | 278 |
279 private: | 279 private: |
280 typedef SkLightingImageFilter INHERITED; | 280 typedef SkLightingImageFilter INHERITED; |
281 SkScalar fKD; | 281 SkScalar fKD; |
282 }; | 282 }; |
283 | 283 |
284 class SkSpecularLightingImageFilter : public SkLightingImageFilter { | 284 class SkSpecularLightingImageFilter : public SkLightingImageFilter { |
285 public: | 285 public: |
286 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, SkScalar shininess, SkImageFilter* input, const SkIRect* cropRect); | 286 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, SkScalar shininess, SkImageFilter* input, const SkIRect* cropRect); |
287 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) | 287 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) |
288 | 288 |
289 #if SK_SUPPORT_GPU | 289 #if SK_SUPPORT_GPU |
290 virtual bool asNewEffect(GrEffectRef** effect, GrTexture*, const SkIPoint& o
ffset) const SK_OVERRIDE; | 290 virtual bool asNewEffect(GrEffectRef** effect, GrTexture*, const SkMatrix& m
atrix) const SK_OVERRIDE; |
291 #endif | 291 #endif |
292 | 292 |
293 SkScalar ks() const { return fKS; } | 293 SkScalar ks() const { return fKS; } |
294 SkScalar shininess() const { return fShininess; } | 294 SkScalar shininess() const { return fShininess; } |
295 | 295 |
296 protected: | 296 protected: |
297 explicit SkSpecularLightingImageFilter(SkFlattenableReadBuffer& buffer); | 297 explicit SkSpecularLightingImageFilter(SkFlattenableReadBuffer& buffer); |
298 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE; | 298 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE; |
299 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, | 299 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const SkMatrix&, |
300 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; | 300 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE; |
301 | 301 |
302 private: | 302 private: |
303 typedef SkLightingImageFilter INHERITED; | 303 typedef SkLightingImageFilter INHERITED; |
304 SkScalar fKS; | 304 SkScalar fKS; |
305 SkScalar fShininess; | 305 SkScalar fShininess; |
306 }; | 306 }; |
307 | 307 |
308 #if SK_SUPPORT_GPU | 308 #if SK_SUPPORT_GPU |
309 | 309 |
310 class GrLightingEffect : public GrSingleTextureEffect { | 310 class GrLightingEffect : public GrSingleTextureEffect { |
311 public: | 311 public: |
312 GrLightingEffect(GrTexture* texture, const SkLight* light, SkScalar surfaceS
cale, const SkIPoint& offset); | 312 GrLightingEffect(GrTexture* texture, const SkLight* light, SkScalar surfaceS
cale, const SkMatrix& matrix); |
313 virtual ~GrLightingEffect(); | 313 virtual ~GrLightingEffect(); |
314 | 314 |
315 const SkLight* light() const { return fLight; } | 315 const SkLight* light() const { return fLight; } |
316 SkScalar surfaceScale() const { return fSurfaceScale; } | 316 SkScalar surfaceScale() const { return fSurfaceScale; } |
317 const SkIPoint& offset() const { return fOffset; } | 317 const SkMatrix& filterMatrix() const { return fFilterMatrix; } |
318 | 318 |
319 virtual void getConstantColorComponents(GrColor* color, | 319 virtual void getConstantColorComponents(GrColor* color, |
320 uint32_t* validFlags) const SK_OVERR
IDE { | 320 uint32_t* validFlags) const SK_OVERR
IDE { |
321 // lighting shaders are complicated. We just throw up our hands. | 321 // lighting shaders are complicated. We just throw up our hands. |
322 *validFlags = 0; | 322 *validFlags = 0; |
323 } | 323 } |
324 | 324 |
325 protected: | 325 protected: |
326 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 326 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
327 | 327 |
328 private: | 328 private: |
329 typedef GrSingleTextureEffect INHERITED; | 329 typedef GrSingleTextureEffect INHERITED; |
330 const SkLight* fLight; | 330 const SkLight* fLight; |
331 SkScalar fSurfaceScale; | 331 SkScalar fSurfaceScale; |
332 SkIPoint fOffset; | 332 SkMatrix fFilterMatrix; |
333 }; | 333 }; |
334 | 334 |
335 class GrDiffuseLightingEffect : public GrLightingEffect { | 335 class GrDiffuseLightingEffect : public GrLightingEffect { |
336 public: | 336 public: |
337 static GrEffectRef* Create(GrTexture* texture, | 337 static GrEffectRef* Create(GrTexture* texture, |
338 const SkLight* light, | 338 const SkLight* light, |
339 SkScalar surfaceScale, | 339 SkScalar surfaceScale, |
340 const SkIPoint& offset, | 340 const SkMatrix& matrix, |
341 SkScalar kd) { | 341 SkScalar kd) { |
342 AutoEffectUnref effect(SkNEW_ARGS(GrDiffuseLightingEffect, (texture, | 342 AutoEffectUnref effect(SkNEW_ARGS(GrDiffuseLightingEffect, (texture, |
343 light, | 343 light, |
344 surfaceScale
, | 344 surfaceScale
, |
345 offset, | 345 matrix, |
346 kd))); | 346 kd))); |
347 return CreateEffectRef(effect); | 347 return CreateEffectRef(effect); |
348 } | 348 } |
349 | 349 |
350 static const char* Name() { return "DiffuseLighting"; } | 350 static const char* Name() { return "DiffuseLighting"; } |
351 | 351 |
352 typedef GrGLDiffuseLightingEffect GLEffect; | 352 typedef GrGLDiffuseLightingEffect GLEffect; |
353 | 353 |
354 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 354 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
355 SkScalar kd() const { return fKD; } | 355 SkScalar kd() const { return fKD; } |
356 | 356 |
357 private: | 357 private: |
358 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 358 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
359 | 359 |
360 GrDiffuseLightingEffect(GrTexture* texture, | 360 GrDiffuseLightingEffect(GrTexture* texture, |
361 const SkLight* light, | 361 const SkLight* light, |
362 SkScalar surfaceScale, | 362 SkScalar surfaceScale, |
363 const SkIPoint& offset, | 363 const SkMatrix& matrix, |
364 SkScalar kd); | 364 SkScalar kd); |
365 | 365 |
366 GR_DECLARE_EFFECT_TEST; | 366 GR_DECLARE_EFFECT_TEST; |
367 typedef GrLightingEffect INHERITED; | 367 typedef GrLightingEffect INHERITED; |
368 SkScalar fKD; | 368 SkScalar fKD; |
369 }; | 369 }; |
370 | 370 |
371 class GrSpecularLightingEffect : public GrLightingEffect { | 371 class GrSpecularLightingEffect : public GrLightingEffect { |
372 public: | 372 public: |
373 static GrEffectRef* Create(GrTexture* texture, | 373 static GrEffectRef* Create(GrTexture* texture, |
374 const SkLight* light, | 374 const SkLight* light, |
375 SkScalar surfaceScale, | 375 SkScalar surfaceScale, |
376 const SkIPoint& offset, | 376 const SkMatrix& matrix, |
377 SkScalar ks, | 377 SkScalar ks, |
378 SkScalar shininess) { | 378 SkScalar shininess) { |
379 AutoEffectUnref effect(SkNEW_ARGS(GrSpecularLightingEffect, (texture, | 379 AutoEffectUnref effect(SkNEW_ARGS(GrSpecularLightingEffect, (texture, |
380 light, | 380 light, |
381 surfaceScal
e, | 381 surfaceScal
e, |
382 offset, | 382 matrix, |
383 ks, | 383 ks, |
384 shininess))
); | 384 shininess))
); |
385 return CreateEffectRef(effect); | 385 return CreateEffectRef(effect); |
386 } | 386 } |
387 static const char* Name() { return "SpecularLighting"; } | 387 static const char* Name() { return "SpecularLighting"; } |
388 | 388 |
389 typedef GrGLSpecularLightingEffect GLEffect; | 389 typedef GrGLSpecularLightingEffect GLEffect; |
390 | 390 |
391 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 391 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
392 SkScalar ks() const { return fKS; } | 392 SkScalar ks() const { return fKS; } |
393 SkScalar shininess() const { return fShininess; } | 393 SkScalar shininess() const { return fShininess; } |
394 | 394 |
395 private: | 395 private: |
396 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 396 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
397 | 397 |
398 GrSpecularLightingEffect(GrTexture* texture, | 398 GrSpecularLightingEffect(GrTexture* texture, |
399 const SkLight* light, | 399 const SkLight* light, |
400 SkScalar surfaceScale, | 400 SkScalar surfaceScale, |
401 const SkIPoint& offset, | 401 const SkMatrix& matrix, |
402 SkScalar ks, | 402 SkScalar ks, |
403 SkScalar shininess); | 403 SkScalar shininess); |
404 | 404 |
405 GR_DECLARE_EFFECT_TEST; | 405 GR_DECLARE_EFFECT_TEST; |
406 typedef GrLightingEffect INHERITED; | 406 typedef GrLightingEffect INHERITED; |
407 SkScalar fKS; | 407 SkScalar fKS; |
408 SkScalar fShininess; | 408 SkScalar fShininess; |
409 }; | 409 }; |
410 | 410 |
411 /////////////////////////////////////////////////////////////////////////////// | 411 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 15 matching lines...) Expand all Loading... |
427 * the FS that is the color of the light. Either function may add functions
and/or uniforms to | 427 * the FS that is the color of the light. Either function may add functions
and/or uniforms to |
428 * the FS. The default of emitLightColor appends the name of the constant li
ght color uniform | 428 * the FS. The default of emitLightColor appends the name of the constant li
ght color uniform |
429 * and so this function only needs to be overridden if the light color varie
s spatially. | 429 * and so this function only needs to be overridden if the light color varie
s spatially. |
430 */ | 430 */ |
431 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) = 0; | 431 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) = 0; |
432 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight); | 432 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight); |
433 | 433 |
434 // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLig
ht must call | 434 // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLig
ht must call |
435 // INHERITED::setData(). | 435 // INHERITED::setData(). |
436 virtual void setData(const GrGLUniformManager&, | 436 virtual void setData(const GrGLUniformManager&, |
437 const SkLight* light, | 437 const SkLight* light) const; |
438 const SkIPoint& offset) const; | |
439 | 438 |
440 protected: | 439 protected: |
441 /** | 440 /** |
442 * Gets the constant light color uniform. Subclasses can use this in their e
mitLightColor | 441 * Gets the constant light color uniform. Subclasses can use this in their e
mitLightColor |
443 * function. | 442 * function. |
444 */ | 443 */ |
445 UniformHandle lightColorUni() const { return fColorUni; } | 444 UniformHandle lightColorUni() const { return fColorUni; } |
446 | 445 |
447 private: | 446 private: |
448 UniformHandle fColorUni; | 447 UniformHandle fColorUni; |
449 | 448 |
450 typedef SkRefCnt INHERITED; | 449 typedef SkRefCnt INHERITED; |
451 }; | 450 }; |
452 | 451 |
453 /////////////////////////////////////////////////////////////////////////////// | 452 /////////////////////////////////////////////////////////////////////////////// |
454 | 453 |
455 class GrGLDistantLight : public GrGLLight { | 454 class GrGLDistantLight : public GrGLLight { |
456 public: | 455 public: |
457 virtual ~GrGLDistantLight() {} | 456 virtual ~GrGLDistantLight() {} |
458 virtual void setData(const GrGLUniformManager&, | 457 virtual void setData(const GrGLUniformManager&, |
459 const SkLight* light, | 458 const SkLight* light) const SK_OVERRIDE; |
460 const SkIPoint& offset) const SK_OVERRIDE; | |
461 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 459 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; |
462 | 460 |
463 private: | 461 private: |
464 typedef GrGLLight INHERITED; | 462 typedef GrGLLight INHERITED; |
465 UniformHandle fDirectionUni; | 463 UniformHandle fDirectionUni; |
466 }; | 464 }; |
467 | 465 |
468 /////////////////////////////////////////////////////////////////////////////// | 466 /////////////////////////////////////////////////////////////////////////////// |
469 | 467 |
470 class GrGLPointLight : public GrGLLight { | 468 class GrGLPointLight : public GrGLLight { |
471 public: | 469 public: |
472 virtual ~GrGLPointLight() {} | 470 virtual ~GrGLPointLight() {} |
473 virtual void setData(const GrGLUniformManager&, | 471 virtual void setData(const GrGLUniformManager&, |
474 const SkLight* light, | 472 const SkLight* light) const SK_OVERRIDE; |
475 const SkIPoint& offset) const SK_OVERRIDE; | |
476 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 473 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; |
477 | 474 |
478 private: | 475 private: |
479 typedef GrGLLight INHERITED; | 476 typedef GrGLLight INHERITED; |
480 UniformHandle fLocationUni; | 477 UniformHandle fLocationUni; |
481 }; | 478 }; |
482 | 479 |
483 /////////////////////////////////////////////////////////////////////////////// | 480 /////////////////////////////////////////////////////////////////////////////// |
484 | 481 |
485 class GrGLSpotLight : public GrGLLight { | 482 class GrGLSpotLight : public GrGLLight { |
486 public: | 483 public: |
487 virtual ~GrGLSpotLight() {} | 484 virtual ~GrGLSpotLight() {} |
488 virtual void setData(const GrGLUniformManager&, | 485 virtual void setData(const GrGLUniformManager&, |
489 const SkLight* light, | 486 const SkLight* light) const SK_OVERRIDE; |
490 const SkIPoint& offset) const SK_OVERRIDE; | |
491 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 487 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; |
492 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight)
SK_OVERRIDE; | 488 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight)
SK_OVERRIDE; |
493 | 489 |
494 private: | 490 private: |
495 typedef GrGLLight INHERITED; | 491 typedef GrGLLight INHERITED; |
496 | 492 |
497 SkString fLightColorFunc; | 493 SkString fLightColorFunc; |
498 UniformHandle fLocationUni; | 494 UniformHandle fLocationUni; |
499 UniformHandle fExponentUni; | 495 UniformHandle fExponentUni; |
500 UniformHandle fCosOuterConeAngleUni; | 496 UniformHandle fCosOuterConeAngleUni; |
(...skipping 21 matching lines...) Expand all Loading... |
522 kSpot_LightType, | 518 kSpot_LightType, |
523 }; | 519 }; |
524 virtual LightType type() const = 0; | 520 virtual LightType type() const = 0; |
525 const SkPoint3& color() const { return fColor; } | 521 const SkPoint3& color() const { return fColor; } |
526 virtual GrGLLight* createGLLight() const = 0; | 522 virtual GrGLLight* createGLLight() const = 0; |
527 virtual bool isEqual(const SkLight& other) const { | 523 virtual bool isEqual(const SkLight& other) const { |
528 return fColor == other.fColor; | 524 return fColor == other.fColor; |
529 } | 525 } |
530 // Called to know whether the generated GrGLLight will require access to the
fragment position. | 526 // Called to know whether the generated GrGLLight will require access to the
fragment position. |
531 virtual bool requiresFragmentPosition() const = 0; | 527 virtual bool requiresFragmentPosition() const = 0; |
| 528 virtual SkLight* transform(const SkMatrix& matrix) const = 0; |
532 | 529 |
533 protected: | 530 protected: |
534 SkLight(SkColor color) | 531 SkLight(SkColor color) |
535 : fColor(SkIntToScalar(SkColorGetR(color)), | 532 : fColor(SkIntToScalar(SkColorGetR(color)), |
536 SkIntToScalar(SkColorGetG(color)), | 533 SkIntToScalar(SkColorGetG(color)), |
537 SkIntToScalar(SkColorGetB(color))) {} | 534 SkIntToScalar(SkColorGetB(color))) {} |
| 535 SkLight(const SkPoint3& color) |
| 536 : fColor(color) {} |
538 SkLight(SkFlattenableReadBuffer& buffer) | 537 SkLight(SkFlattenableReadBuffer& buffer) |
539 : INHERITED(buffer) { | 538 : INHERITED(buffer) { |
540 fColor = readPoint3(buffer); | 539 fColor = readPoint3(buffer); |
541 } | 540 } |
542 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { | 541 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { |
543 INHERITED::flatten(buffer); | 542 INHERITED::flatten(buffer); |
544 writePoint3(fColor, buffer); | 543 writePoint3(fColor, buffer); |
545 } | 544 } |
546 | 545 |
547 private: | 546 private: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 } | 580 } |
582 | 581 |
583 const SkDistantLight& o = static_cast<const SkDistantLight&>(other); | 582 const SkDistantLight& o = static_cast<const SkDistantLight&>(other); |
584 return INHERITED::isEqual(other) && | 583 return INHERITED::isEqual(other) && |
585 fDirection == o.fDirection; | 584 fDirection == o.fDirection; |
586 } | 585 } |
587 | 586 |
588 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDistantLight) | 587 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDistantLight) |
589 | 588 |
590 protected: | 589 protected: |
| 590 SkDistantLight(const SkPoint3& direction, const SkPoint3& color) |
| 591 : INHERITED(color), fDirection(direction) { |
| 592 } |
591 SkDistantLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | 593 SkDistantLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
592 fDirection = readPoint3(buffer); | 594 fDirection = readPoint3(buffer); |
593 } | 595 } |
| 596 virtual SkLight* transform(const SkMatrix& matrix) const { |
| 597 return new SkDistantLight(direction(), color()); |
| 598 } |
594 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 599 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { |
595 INHERITED::flatten(buffer); | 600 INHERITED::flatten(buffer); |
596 writePoint3(fDirection, buffer); | 601 writePoint3(fDirection, buffer); |
597 } | 602 } |
598 | 603 |
599 private: | 604 private: |
600 typedef SkLight INHERITED; | 605 typedef SkLight INHERITED; |
601 SkPoint3 fDirection; | 606 SkPoint3 fDirection; |
602 }; | 607 }; |
603 | 608 |
(...skipping 24 matching lines...) Expand all Loading... |
628 } | 633 } |
629 virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } | 634 virtual bool requiresFragmentPosition() const SK_OVERRIDE { return true; } |
630 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { | 635 virtual bool isEqual(const SkLight& other) const SK_OVERRIDE { |
631 if (other.type() != kPoint_LightType) { | 636 if (other.type() != kPoint_LightType) { |
632 return false; | 637 return false; |
633 } | 638 } |
634 const SkPointLight& o = static_cast<const SkPointLight&>(other); | 639 const SkPointLight& o = static_cast<const SkPointLight&>(other); |
635 return INHERITED::isEqual(other) && | 640 return INHERITED::isEqual(other) && |
636 fLocation == o.fLocation; | 641 fLocation == o.fLocation; |
637 } | 642 } |
| 643 virtual SkLight* transform(const SkMatrix& matrix) const { |
| 644 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 645 matrix.mapPoints(&location2, 1); |
| 646 SkPoint3 location(location2.fX, location2.fY, fLocation.fZ); |
| 647 return new SkPointLight(location, color()); |
| 648 } |
638 | 649 |
639 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPointLight) | 650 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPointLight) |
640 | 651 |
641 protected: | 652 protected: |
642 SkPointLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | 653 SkPointLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
643 fLocation = readPoint3(buffer); | 654 fLocation = readPoint3(buffer); |
644 } | 655 } |
| 656 SkPointLight(const SkPoint3& location, const SkPoint3& color) |
| 657 : INHERITED(color), fLocation(location) {} |
645 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 658 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { |
646 INHERITED::flatten(buffer); | 659 INHERITED::flatten(buffer); |
647 writePoint3(fLocation, buffer); | 660 writePoint3(fLocation, buffer); |
648 } | 661 } |
649 | 662 |
650 private: | 663 private: |
651 typedef SkLight INHERITED; | 664 typedef SkLight INHERITED; |
652 SkPoint3 fLocation; | 665 SkPoint3 fLocation; |
653 }; | 666 }; |
654 | 667 |
655 /////////////////////////////////////////////////////////////////////////////// | 668 /////////////////////////////////////////////////////////////////////////////// |
656 | 669 |
657 class SkSpotLight : public SkLight { | 670 class SkSpotLight : public SkLight { |
658 public: | 671 public: |
659 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu
larExponent, SkScalar cutoffAngle, SkColor color) | 672 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu
larExponent, SkScalar cutoffAngle, SkColor color) |
660 : INHERITED(color), | 673 : INHERITED(color), |
661 fLocation(location), | 674 fLocation(location), |
662 fTarget(target), | 675 fTarget(target), |
663 fSpecularExponent(specularExponent) | 676 fSpecularExponent(specularExponent) |
664 { | 677 { |
665 fS = target - location; | 678 fS = target - location; |
666 fS.normalize(); | 679 fS.normalize(); |
667 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); | 680 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
668 const SkScalar antiAliasThreshold = SkFloatToScalar(0.016f); | 681 const SkScalar antiAliasThreshold = SkFloatToScalar(0.016f); |
669 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; | 682 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; |
670 fConeScale = SkScalarInvert(antiAliasThreshold); | 683 fConeScale = SkScalarInvert(antiAliasThreshold); |
671 } | 684 } |
672 | 685 |
| 686 virtual SkLight* transform(const SkMatrix& matrix) const { |
| 687 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 688 matrix.mapPoints(&location2, 1); |
| 689 SkPoint3 location(location2.fX, location2.fY, fLocation.fZ); |
| 690 SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); |
| 691 matrix.mapPoints(&target2, 1); |
| 692 SkPoint3 target(target2.fX, target2.fY, fTarget.fZ); |
| 693 return new SkSpotLight(location, target, fSpecularExponent, fCosOuterCon
eAngle, fCosInnerConeAngle, fConeScale, fS, color()); |
| 694 } |
| 695 |
673 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { | 696 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
674 SkPoint3 direction(fLocation.fX - SkIntToScalar(x), | 697 SkPoint3 direction(fLocation.fX - SkIntToScalar(x), |
675 fLocation.fY - SkIntToScalar(y), | 698 fLocation.fY - SkIntToScalar(y), |
676 fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceS
cale)); | 699 fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceS
cale)); |
677 direction.normalize(); | 700 direction.normalize(); |
678 return direction; | 701 return direction; |
679 }; | 702 }; |
680 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { | 703 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { |
681 SkScalar cosAngle = -surfaceToLight.dot(fS); | 704 SkScalar cosAngle = -surfaceToLight.dot(fS); |
682 if (cosAngle < fCosOuterConeAngle) { | 705 if (cosAngle < fCosOuterConeAngle) { |
(...skipping 29 matching lines...) Expand all Loading... |
712 protected: | 735 protected: |
713 SkSpotLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { | 736 SkSpotLight(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
714 fLocation = readPoint3(buffer); | 737 fLocation = readPoint3(buffer); |
715 fTarget = readPoint3(buffer); | 738 fTarget = readPoint3(buffer); |
716 fSpecularExponent = buffer.readScalar(); | 739 fSpecularExponent = buffer.readScalar(); |
717 fCosOuterConeAngle = buffer.readScalar(); | 740 fCosOuterConeAngle = buffer.readScalar(); |
718 fCosInnerConeAngle = buffer.readScalar(); | 741 fCosInnerConeAngle = buffer.readScalar(); |
719 fConeScale = buffer.readScalar(); | 742 fConeScale = buffer.readScalar(); |
720 fS = readPoint3(buffer); | 743 fS = readPoint3(buffer); |
721 } | 744 } |
| 745 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu
larExponent, SkScalar cosOuterConeAngle, SkScalar cosInnerConeAngle, SkScalar co
neScale, const SkPoint3& s, const SkPoint3& color) |
| 746 : INHERITED(color), |
| 747 fLocation(location), |
| 748 fTarget(target), |
| 749 fSpecularExponent(specularExponent), |
| 750 fCosOuterConeAngle(cosOuterConeAngle), |
| 751 fCosInnerConeAngle(cosInnerConeAngle), |
| 752 fConeScale(coneScale), |
| 753 fS(s) |
| 754 { |
| 755 } |
722 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { | 756 virtual void flatten(SkFlattenableWriteBuffer& buffer) const { |
723 INHERITED::flatten(buffer); | 757 INHERITED::flatten(buffer); |
724 writePoint3(fLocation, buffer); | 758 writePoint3(fLocation, buffer); |
725 writePoint3(fTarget, buffer); | 759 writePoint3(fTarget, buffer); |
726 buffer.writeScalar(fSpecularExponent); | 760 buffer.writeScalar(fSpecularExponent); |
727 buffer.writeScalar(fCosOuterConeAngle); | 761 buffer.writeScalar(fCosOuterConeAngle); |
728 buffer.writeScalar(fCosInnerConeAngle); | 762 buffer.writeScalar(fCosInnerConeAngle); |
729 buffer.writeScalar(fConeScale); | 763 buffer.writeScalar(fConeScale); |
730 writePoint3(fS, buffer); | 764 writePoint3(fS, buffer); |
731 } | 765 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
850 fKD = buffer.readScalar(); | 884 fKD = buffer.readScalar(); |
851 } | 885 } |
852 | 886 |
853 void SkDiffuseLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) con
st { | 887 void SkDiffuseLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) con
st { |
854 this->INHERITED::flatten(buffer); | 888 this->INHERITED::flatten(buffer); |
855 buffer.writeScalar(fKD); | 889 buffer.writeScalar(fKD); |
856 } | 890 } |
857 | 891 |
858 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy*, | 892 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy*, |
859 const SkBitmap& src, | 893 const SkBitmap& src, |
860 const SkMatrix&, | 894 const SkMatrix& ctm, |
861 SkBitmap* dst, | 895 SkBitmap* dst, |
862 SkIPoint* offset) { | 896 SkIPoint* offset) { |
863 if (src.config() != SkBitmap::kARGB_8888_Config) { | 897 if (src.config() != SkBitmap::kARGB_8888_Config) { |
864 return false; | 898 return false; |
865 } | 899 } |
866 SkAutoLockPixels alp(src); | 900 SkAutoLockPixels alp(src); |
867 if (!src.getPixels()) { | 901 if (!src.getPixels()) { |
868 return false; | 902 return false; |
869 } | 903 } |
870 | 904 |
871 SkIRect bounds; | 905 SkIRect bounds; |
872 src.getBounds(&bounds); | 906 src.getBounds(&bounds); |
873 if (!this->applyCropRect(&bounds)) { | 907 if (!this->applyCropRect(&bounds, ctm)) { |
874 return false; | 908 return false; |
875 } | 909 } |
876 | 910 |
877 if (bounds.width() < 2 || bounds.height() < 2) { | 911 if (bounds.width() < 2 || bounds.height() < 2) { |
878 return false; | 912 return false; |
879 } | 913 } |
880 | 914 |
881 dst->setConfig(src.config(), bounds.width(), bounds.height()); | 915 dst->setConfig(src.config(), bounds.width(), bounds.height()); |
882 dst->allocPixels(); | 916 dst->allocPixels(); |
883 | 917 |
| 918 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctm)); |
| 919 |
884 DiffuseLightingType lightingType(fKD); | 920 DiffuseLightingType lightingType(fKD); |
885 switch (light()->type()) { | 921 switch (transformedLight->type()) { |
886 case SkLight::kDistant_LightType: | 922 case SkLight::kDistant_LightType: |
887 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, light
(), src, dst, surfaceScale(), bounds); | 923 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, trans
formedLight, src, dst, surfaceScale(), bounds); |
888 break; | 924 break; |
889 case SkLight::kPoint_LightType: | 925 case SkLight::kPoint_LightType: |
890 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, light()
, src, dst, surfaceScale(), bounds); | 926 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); |
891 break; | 927 break; |
892 case SkLight::kSpot_LightType: | 928 case SkLight::kSpot_LightType: |
893 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, light(),
src, dst, surfaceScale(), bounds); | 929 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transfor
medLight, src, dst, surfaceScale(), bounds); |
894 break; | 930 break; |
895 } | 931 } |
896 | 932 |
897 offset->fX += bounds.left(); | 933 offset->fX += bounds.left(); |
898 offset->fY += bounds.top(); | 934 offset->fY += bounds.top(); |
899 return true; | 935 return true; |
900 } | 936 } |
901 | 937 |
902 #if SK_SUPPORT_GPU | 938 #if SK_SUPPORT_GPU |
903 bool SkDiffuseLightingImageFilter::asNewEffect(GrEffectRef** effect, GrTexture*
texture, const SkIPoint& offset) const { | 939 bool SkDiffuseLightingImageFilter::asNewEffect(GrEffectRef** effect, GrTexture*
texture, const SkMatrix& matrix) const { |
904 if (effect) { | 940 if (effect) { |
905 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); | 941 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); |
906 *effect = GrDiffuseLightingEffect::Create(texture, light(), scale, offse
t, kd()); | 942 *effect = GrDiffuseLightingEffect::Create(texture, light(), scale, matri
x, kd()); |
907 } | 943 } |
908 return true; | 944 return true; |
909 } | 945 } |
910 #endif | 946 #endif |
911 | 947 |
912 /////////////////////////////////////////////////////////////////////////////// | 948 /////////////////////////////////////////////////////////////////////////////// |
913 | 949 |
914 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkLight* light, SkS
calar surfaceScale, SkScalar ks, SkScalar shininess, SkImageFilter* input, const
SkIRect* cropRect) | 950 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkLight* light, SkS
calar surfaceScale, SkScalar ks, SkScalar shininess, SkImageFilter* input, const
SkIRect* cropRect) |
915 : SkLightingImageFilter(light, surfaceScale, input, cropRect), | 951 : SkLightingImageFilter(light, surfaceScale, input, cropRect), |
916 fKS(ks), | 952 fKS(ks), |
917 fShininess(shininess) | 953 fShininess(shininess) |
918 { | 954 { |
919 } | 955 } |
920 | 956 |
921 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkFlattenableReadBu
ffer& buffer) | 957 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkFlattenableReadBu
ffer& buffer) |
922 : INHERITED(buffer) | 958 : INHERITED(buffer) |
923 { | 959 { |
924 fKS = buffer.readScalar(); | 960 fKS = buffer.readScalar(); |
925 fShininess = buffer.readScalar(); | 961 fShininess = buffer.readScalar(); |
926 } | 962 } |
927 | 963 |
928 void SkSpecularLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) co
nst { | 964 void SkSpecularLightingImageFilter::flatten(SkFlattenableWriteBuffer& buffer) co
nst { |
929 this->INHERITED::flatten(buffer); | 965 this->INHERITED::flatten(buffer); |
930 buffer.writeScalar(fKS); | 966 buffer.writeScalar(fKS); |
931 buffer.writeScalar(fShininess); | 967 buffer.writeScalar(fShininess); |
932 } | 968 } |
933 | 969 |
934 bool SkSpecularLightingImageFilter::onFilterImage(Proxy*, | 970 bool SkSpecularLightingImageFilter::onFilterImage(Proxy*, |
935 const SkBitmap& src, | 971 const SkBitmap& src, |
936 const SkMatrix&, | 972 const SkMatrix& ctm, |
937 SkBitmap* dst, | 973 SkBitmap* dst, |
938 SkIPoint* offset) { | 974 SkIPoint* offset) { |
939 if (src.config() != SkBitmap::kARGB_8888_Config) { | 975 if (src.config() != SkBitmap::kARGB_8888_Config) { |
940 return false; | 976 return false; |
941 } | 977 } |
942 SkAutoLockPixels alp(src); | 978 SkAutoLockPixels alp(src); |
943 if (!src.getPixels()) { | 979 if (!src.getPixels()) { |
944 return false; | 980 return false; |
945 } | 981 } |
946 | 982 |
947 SkIRect bounds; | 983 SkIRect bounds; |
948 src.getBounds(&bounds); | 984 src.getBounds(&bounds); |
949 if (!this->applyCropRect(&bounds)) { | 985 if (!this->applyCropRect(&bounds, ctm)) { |
950 return false; | 986 return false; |
951 } | 987 } |
952 | 988 |
953 if (bounds.width() < 2 || bounds.height() < 2) { | 989 if (bounds.width() < 2 || bounds.height() < 2) { |
954 return false; | 990 return false; |
955 } | 991 } |
956 | 992 |
957 dst->setConfig(src.config(), bounds.width(), bounds.height()); | 993 dst->setConfig(src.config(), bounds.width(), bounds.height()); |
958 dst->allocPixels(); | 994 dst->allocPixels(); |
959 | 995 |
960 SpecularLightingType lightingType(fKS, fShininess); | 996 SpecularLightingType lightingType(fKS, fShininess); |
961 switch (light()->type()) { | 997 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctm)); |
| 998 switch (transformedLight->type()) { |
962 case SkLight::kDistant_LightType: | 999 case SkLight::kDistant_LightType: |
963 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, ligh
t(), src, dst, surfaceScale(), bounds); | 1000 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, tran
sformedLight, src, dst, surfaceScale(), bounds); |
964 break; | 1001 break; |
965 case SkLight::kPoint_LightType: | 1002 case SkLight::kPoint_LightType: |
966 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, light(
), src, dst, surfaceScale(), bounds); | 1003 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transf
ormedLight, src, dst, surfaceScale(), bounds); |
967 break; | 1004 break; |
968 case SkLight::kSpot_LightType: | 1005 case SkLight::kSpot_LightType: |
969 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, light()
, src, dst, surfaceScale(), bounds); | 1006 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); |
970 break; | 1007 break; |
971 } | 1008 } |
972 offset->fX += bounds.left(); | 1009 offset->fX += bounds.left(); |
973 offset->fY += bounds.top(); | 1010 offset->fY += bounds.top(); |
974 return true; | 1011 return true; |
975 } | 1012 } |
976 | 1013 |
977 #if SK_SUPPORT_GPU | 1014 #if SK_SUPPORT_GPU |
978 bool SkSpecularLightingImageFilter::asNewEffect(GrEffectRef** effect, GrTexture*
texture, const SkIPoint& offset) const { | 1015 bool SkSpecularLightingImageFilter::asNewEffect(GrEffectRef** effect, GrTexture*
texture, const SkMatrix& matrix) const { |
979 if (effect) { | 1016 if (effect) { |
980 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); | 1017 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); |
981 *effect = GrSpecularLightingEffect::Create(texture, light(), scale, offs
et, ks(), shininess()); | 1018 *effect = GrSpecularLightingEffect::Create(texture, light(), scale, matr
ix, ks(), shininess()); |
982 } | 1019 } |
983 return true; | 1020 return true; |
984 } | 1021 } |
985 #endif | 1022 #endif |
986 | 1023 |
987 /////////////////////////////////////////////////////////////////////////////// | 1024 /////////////////////////////////////////////////////////////////////////////// |
988 | 1025 |
989 #if SK_SUPPORT_GPU | 1026 #if SK_SUPPORT_GPU |
990 | 1027 |
991 namespace { | 1028 namespace { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1080 | 1117 |
1081 UniformHandle fKSUni; | 1118 UniformHandle fKSUni; |
1082 UniformHandle fShininessUni; | 1119 UniformHandle fShininessUni; |
1083 }; | 1120 }; |
1084 | 1121 |
1085 /////////////////////////////////////////////////////////////////////////////// | 1122 /////////////////////////////////////////////////////////////////////////////// |
1086 | 1123 |
1087 GrLightingEffect::GrLightingEffect(GrTexture* texture, | 1124 GrLightingEffect::GrLightingEffect(GrTexture* texture, |
1088 const SkLight* light, | 1125 const SkLight* light, |
1089 SkScalar surfaceScale, | 1126 SkScalar surfaceScale, |
1090 const SkIPoint& offset) | 1127 const SkMatrix& matrix) |
1091 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) | 1128 : INHERITED(texture, MakeDivByTextureWHMatrix(texture)) |
1092 , fLight(light) | 1129 , fLight(light) |
1093 , fSurfaceScale(surfaceScale) | 1130 , fSurfaceScale(surfaceScale) |
1094 , fOffset(offset) { | 1131 , fFilterMatrix(matrix) { |
1095 fLight->ref(); | 1132 fLight->ref(); |
1096 if (light->requiresFragmentPosition()) { | 1133 if (light->requiresFragmentPosition()) { |
1097 this->setWillReadFragmentPosition(); | 1134 this->setWillReadFragmentPosition(); |
1098 } | 1135 } |
1099 } | 1136 } |
1100 | 1137 |
1101 GrLightingEffect::~GrLightingEffect() { | 1138 GrLightingEffect::~GrLightingEffect() { |
1102 fLight->unref(); | 1139 fLight->unref(); |
1103 } | 1140 } |
1104 | 1141 |
1105 bool GrLightingEffect::onIsEqual(const GrEffect& sBase) const { | 1142 bool GrLightingEffect::onIsEqual(const GrEffect& sBase) const { |
1106 const GrLightingEffect& s = CastEffect<GrLightingEffect>(sBase); | 1143 const GrLightingEffect& s = CastEffect<GrLightingEffect>(sBase); |
1107 return this->texture(0) == s.texture(0) && | 1144 return this->texture(0) == s.texture(0) && |
1108 fLight->isEqual(*s.fLight) && | 1145 fLight->isEqual(*s.fLight) && |
1109 fSurfaceScale == s.fSurfaceScale; | 1146 fSurfaceScale == s.fSurfaceScale; |
1110 } | 1147 } |
1111 | 1148 |
1112 /////////////////////////////////////////////////////////////////////////////// | 1149 /////////////////////////////////////////////////////////////////////////////// |
1113 | 1150 |
1114 GrDiffuseLightingEffect::GrDiffuseLightingEffect(GrTexture* texture, | 1151 GrDiffuseLightingEffect::GrDiffuseLightingEffect(GrTexture* texture, |
1115 const SkLight* light, | 1152 const SkLight* light, |
1116 SkScalar surfaceScale, | 1153 SkScalar surfaceScale, |
1117 const SkIPoint& offset, | 1154 const SkMatrix& matrix, |
1118 SkScalar kd) | 1155 SkScalar kd) |
1119 : INHERITED(texture, light, surfaceScale, offset), fKD(kd) { | 1156 : INHERITED(texture, light, surfaceScale, matrix), fKD(kd) { |
1120 } | 1157 } |
1121 | 1158 |
1122 const GrBackendEffectFactory& GrDiffuseLightingEffect::getFactory() const { | 1159 const GrBackendEffectFactory& GrDiffuseLightingEffect::getFactory() const { |
1123 return GrTBackendEffectFactory<GrDiffuseLightingEffect>::getInstance(); | 1160 return GrTBackendEffectFactory<GrDiffuseLightingEffect>::getInstance(); |
1124 } | 1161 } |
1125 | 1162 |
1126 bool GrDiffuseLightingEffect::onIsEqual(const GrEffect& sBase) const { | 1163 bool GrDiffuseLightingEffect::onIsEqual(const GrEffect& sBase) const { |
1127 const GrDiffuseLightingEffect& s = CastEffect<GrDiffuseLightingEffect>(sBase
); | 1164 const GrDiffuseLightingEffect& s = CastEffect<GrDiffuseLightingEffect>(sBase
); |
1128 return INHERITED::onIsEqual(sBase) && | 1165 return INHERITED::onIsEqual(sBase) && |
1129 this->kd() == s.kd(); | 1166 this->kd() == s.kd(); |
1130 } | 1167 } |
1131 | 1168 |
1132 GR_DEFINE_EFFECT_TEST(GrDiffuseLightingEffect); | 1169 GR_DEFINE_EFFECT_TEST(GrDiffuseLightingEffect); |
1133 | 1170 |
1134 GrEffectRef* GrDiffuseLightingEffect::TestCreate(SkMWCRandom* random, | 1171 GrEffectRef* GrDiffuseLightingEffect::TestCreate(SkMWCRandom* random, |
1135 GrContext* context, | 1172 GrContext* context, |
1136 const GrDrawTargetCaps&, | 1173 const GrDrawTargetCaps&, |
1137 GrTexture* textures[]) { | 1174 GrTexture* textures[]) { |
1138 SkScalar surfaceScale = random->nextSScalar1(); | 1175 SkScalar surfaceScale = random->nextSScalar1(); |
1139 SkScalar kd = random->nextUScalar1(); | 1176 SkScalar kd = random->nextUScalar1(); |
1140 SkAutoTUnref<SkLight> light(create_random_light(random)); | 1177 SkAutoTUnref<SkLight> light(create_random_light(random)); |
1141 SkIPoint offset = SkIPoint::Make(random->nextS(), random->nextS()); | 1178 SkMatrix matrix; |
| 1179 for (int i = 0; i < 9; i++) { |
| 1180 matrix[i] = random->nextUScalar1(); |
| 1181 } |
1142 return GrDiffuseLightingEffect::Create(textures[GrEffectUnitTest::kAlphaText
ureIdx], | 1182 return GrDiffuseLightingEffect::Create(textures[GrEffectUnitTest::kAlphaText
ureIdx], |
1143 light, surfaceScale, offset, kd); | 1183 light, surfaceScale, matrix, kd); |
1144 } | 1184 } |
1145 | 1185 |
1146 | 1186 |
1147 /////////////////////////////////////////////////////////////////////////////// | 1187 /////////////////////////////////////////////////////////////////////////////// |
1148 | 1188 |
1149 GrGLLightingEffect::GrGLLightingEffect(const GrBackendEffectFactory& factory, | 1189 GrGLLightingEffect::GrGLLightingEffect(const GrBackendEffectFactory& factory, |
1150 const GrDrawEffect& drawEffect) | 1190 const GrDrawEffect& drawEffect) |
1151 : INHERITED(factory) | 1191 : INHERITED(factory) |
1152 , fEffectMatrix(drawEffect.castEffect<GrLightingEffect>().coordsType()) { | 1192 , fEffectMatrix(drawEffect.castEffect<GrLightingEffect>().coordsType()) { |
1153 const GrLightingEffect& m = drawEffect.castEffect<GrLightingEffect>(); | 1193 const GrLightingEffect& m = drawEffect.castEffect<GrLightingEffect>(); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 return key | matrixKey; | 1311 return key | matrixKey; |
1272 } | 1312 } |
1273 | 1313 |
1274 void GrGLLightingEffect::setData(const GrGLUniformManager& uman, | 1314 void GrGLLightingEffect::setData(const GrGLUniformManager& uman, |
1275 const GrDrawEffect& drawEffect) { | 1315 const GrDrawEffect& drawEffect) { |
1276 const GrLightingEffect& lighting = drawEffect.castEffect<GrLightingEffect>()
; | 1316 const GrLightingEffect& lighting = drawEffect.castEffect<GrLightingEffect>()
; |
1277 GrTexture* texture = lighting.texture(0); | 1317 GrTexture* texture = lighting.texture(0); |
1278 float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; | 1318 float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; |
1279 uman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->hei
ght()); | 1319 uman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->hei
ght()); |
1280 uman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); | 1320 uman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); |
1281 fLight->setData(uman, lighting.light(), lighting.offset()); | 1321 SkAutoTUnref<SkLight> transformedLight(lighting.light()->transform(lighting.
filterMatrix())); |
| 1322 fLight->setData(uman, transformedLight); |
1282 fEffectMatrix.setData(uman, | 1323 fEffectMatrix.setData(uman, |
1283 lighting.getMatrix(), | 1324 lighting.getMatrix(), |
1284 drawEffect, | 1325 drawEffect, |
1285 lighting.texture(0)); | 1326 lighting.texture(0)); |
1286 } | 1327 } |
1287 | 1328 |
1288 /////////////////////////////////////////////////////////////////////////////// | 1329 /////////////////////////////////////////////////////////////////////////////// |
1289 | 1330 |
1290 /////////////////////////////////////////////////////////////////////////////// | 1331 /////////////////////////////////////////////////////////////////////////////// |
1291 | 1332 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1323 INHERITED::setData(uman, drawEffect); | 1364 INHERITED::setData(uman, drawEffect); |
1324 const GrDiffuseLightingEffect& diffuse = drawEffect.castEffect<GrDiffuseLigh
tingEffect>(); | 1365 const GrDiffuseLightingEffect& diffuse = drawEffect.castEffect<GrDiffuseLigh
tingEffect>(); |
1325 uman.set1f(fKDUni, diffuse.kd()); | 1366 uman.set1f(fKDUni, diffuse.kd()); |
1326 } | 1367 } |
1327 | 1368 |
1328 /////////////////////////////////////////////////////////////////////////////// | 1369 /////////////////////////////////////////////////////////////////////////////// |
1329 | 1370 |
1330 GrSpecularLightingEffect::GrSpecularLightingEffect(GrTexture* texture, | 1371 GrSpecularLightingEffect::GrSpecularLightingEffect(GrTexture* texture, |
1331 const SkLight* light, | 1372 const SkLight* light, |
1332 SkScalar surfaceScale, | 1373 SkScalar surfaceScale, |
1333 const SkIPoint& offset, | 1374 const SkMatrix& matrix, |
1334 SkScalar ks, | 1375 SkScalar ks, |
1335 SkScalar shininess) | 1376 SkScalar shininess) |
1336 : INHERITED(texture, light, surfaceScale, offset), | 1377 : INHERITED(texture, light, surfaceScale, matrix), |
1337 fKS(ks), | 1378 fKS(ks), |
1338 fShininess(shininess) { | 1379 fShininess(shininess) { |
1339 } | 1380 } |
1340 | 1381 |
1341 const GrBackendEffectFactory& GrSpecularLightingEffect::getFactory() const { | 1382 const GrBackendEffectFactory& GrSpecularLightingEffect::getFactory() const { |
1342 return GrTBackendEffectFactory<GrSpecularLightingEffect>::getInstance(); | 1383 return GrTBackendEffectFactory<GrSpecularLightingEffect>::getInstance(); |
1343 } | 1384 } |
1344 | 1385 |
1345 bool GrSpecularLightingEffect::onIsEqual(const GrEffect& sBase) const { | 1386 bool GrSpecularLightingEffect::onIsEqual(const GrEffect& sBase) const { |
1346 const GrSpecularLightingEffect& s = CastEffect<GrSpecularLightingEffect>(sBa
se); | 1387 const GrSpecularLightingEffect& s = CastEffect<GrSpecularLightingEffect>(sBa
se); |
1347 return INHERITED::onIsEqual(sBase) && | 1388 return INHERITED::onIsEqual(sBase) && |
1348 this->ks() == s.ks() && | 1389 this->ks() == s.ks() && |
1349 this->shininess() == s.shininess(); | 1390 this->shininess() == s.shininess(); |
1350 } | 1391 } |
1351 | 1392 |
1352 GR_DEFINE_EFFECT_TEST(GrSpecularLightingEffect); | 1393 GR_DEFINE_EFFECT_TEST(GrSpecularLightingEffect); |
1353 | 1394 |
1354 GrEffectRef* GrSpecularLightingEffect::TestCreate(SkMWCRandom* random, | 1395 GrEffectRef* GrSpecularLightingEffect::TestCreate(SkMWCRandom* random, |
1355 GrContext* context, | 1396 GrContext* context, |
1356 const GrDrawTargetCaps&, | 1397 const GrDrawTargetCaps&, |
1357 GrTexture* textures[]) { | 1398 GrTexture* textures[]) { |
1358 SkScalar surfaceScale = random->nextSScalar1(); | 1399 SkScalar surfaceScale = random->nextSScalar1(); |
1359 SkScalar ks = random->nextUScalar1(); | 1400 SkScalar ks = random->nextUScalar1(); |
1360 SkScalar shininess = random->nextUScalar1(); | 1401 SkScalar shininess = random->nextUScalar1(); |
1361 SkAutoTUnref<SkLight> light(create_random_light(random)); | 1402 SkAutoTUnref<SkLight> light(create_random_light(random)); |
1362 SkIPoint offset = SkIPoint::Make(random->nextS(), random->nextS()); | 1403 SkMatrix matrix; |
| 1404 for (int i = 0; i < 9; i++) { |
| 1405 matrix[i] = random->nextUScalar1(); |
| 1406 } |
1363 return GrSpecularLightingEffect::Create(textures[GrEffectUnitTest::kAlphaTex
tureIdx], | 1407 return GrSpecularLightingEffect::Create(textures[GrEffectUnitTest::kAlphaTex
tureIdx], |
1364 light, surfaceScale, offset, ks, shi
niness); | 1408 light, surfaceScale, matrix, ks, shi
niness); |
1365 } | 1409 } |
1366 | 1410 |
1367 /////////////////////////////////////////////////////////////////////////////// | 1411 /////////////////////////////////////////////////////////////////////////////// |
1368 | 1412 |
1369 GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrBackendEffectFact
ory& factory, | 1413 GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrBackendEffectFact
ory& factory, |
1370 const GrDrawEffect& drawE
ffect) | 1414 const GrDrawEffect& drawE
ffect) |
1371 : INHERITED(factory, drawEffect) { | 1415 : INHERITED(factory, drawEffect) { |
1372 } | 1416 } |
1373 | 1417 |
1374 void GrGLSpecularLightingEffect::emitLightFunc(GrGLShaderBuilder* builder, SkStr
ing* funcName) { | 1418 void GrGLSpecularLightingEffect::emitLightFunc(GrGLShaderBuilder* builder, SkStr
ing* funcName) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1412 fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 1456 fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
1413 kVec3f_GrSLType, "LightColor"); | 1457 kVec3f_GrSLType, "LightColor"); |
1414 } | 1458 } |
1415 | 1459 |
1416 void GrGLLight::emitLightColor(GrGLShaderBuilder* builder, | 1460 void GrGLLight::emitLightColor(GrGLShaderBuilder* builder, |
1417 const char *surfaceToLight) { | 1461 const char *surfaceToLight) { |
1418 builder->fsCodeAppend(builder->getUniformCStr(this->lightColorUni())); | 1462 builder->fsCodeAppend(builder->getUniformCStr(this->lightColorUni())); |
1419 } | 1463 } |
1420 | 1464 |
1421 void GrGLLight::setData(const GrGLUniformManager& uman, | 1465 void GrGLLight::setData(const GrGLUniformManager& uman, |
1422 const SkLight* light, | 1466 const SkLight* light) const { |
1423 const SkIPoint&) const { | |
1424 setUniformPoint3(uman, fColorUni, light->color() * SkScalarInvert(SkIntToSca
lar(255))); | 1467 setUniformPoint3(uman, fColorUni, light->color() * SkScalarInvert(SkIntToSca
lar(255))); |
1425 } | 1468 } |
1426 | 1469 |
1427 /////////////////////////////////////////////////////////////////////////////// | 1470 /////////////////////////////////////////////////////////////////////////////// |
1428 | 1471 |
1429 void GrGLDistantLight::setData(const GrGLUniformManager& uman, | 1472 void GrGLDistantLight::setData(const GrGLUniformManager& uman, |
1430 const SkLight* light, | 1473 const SkLight* light) const { |
1431 const SkIPoint& offset) const { | 1474 INHERITED::setData(uman, light); |
1432 INHERITED::setData(uman, light, offset); | |
1433 SkASSERT(light->type() == SkLight::kDistant_LightType); | 1475 SkASSERT(light->type() == SkLight::kDistant_LightType); |
1434 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); | 1476 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); |
1435 setUniformNormal3(uman, fDirectionUni, distantLight->direction()); | 1477 setUniformNormal3(uman, fDirectionUni, distantLight->direction()); |
1436 } | 1478 } |
1437 | 1479 |
1438 void GrGLDistantLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char
* z) { | 1480 void GrGLDistantLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char
* z) { |
1439 const char* dir; | 1481 const char* dir; |
1440 fDirectionUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, | 1482 fDirectionUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, |
1441 "LightDirection", &dir); | 1483 "LightDirection", &dir); |
1442 builder->fsCodeAppend(dir); | 1484 builder->fsCodeAppend(dir); |
1443 } | 1485 } |
1444 | 1486 |
1445 /////////////////////////////////////////////////////////////////////////////// | 1487 /////////////////////////////////////////////////////////////////////////////// |
1446 | 1488 |
1447 void GrGLPointLight::setData(const GrGLUniformManager& uman, | 1489 void GrGLPointLight::setData(const GrGLUniformManager& uman, |
1448 const SkLight* light, | 1490 const SkLight* light) const { |
1449 const SkIPoint& offset) const { | 1491 INHERITED::setData(uman, light); |
1450 INHERITED::setData(uman, light, offset); | |
1451 SkASSERT(light->type() == SkLight::kPoint_LightType); | 1492 SkASSERT(light->type() == SkLight::kPoint_LightType); |
1452 const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); | 1493 const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); |
1453 SkPoint3 location = pointLight->location(); | 1494 setUniformPoint3(uman, fLocationUni, pointLight->location()); |
1454 location.fX -= offset.fX; | |
1455 location.fY -= offset.fY; | |
1456 setUniformPoint3(uman, fLocationUni, location); | |
1457 } | 1495 } |
1458 | 1496 |
1459 void GrGLPointLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char*
z) { | 1497 void GrGLPointLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char*
z) { |
1460 const char* loc; | 1498 const char* loc; |
1461 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, | 1499 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
kVec3f_GrSLType, |
1462 "LightLocation", &loc); | 1500 "LightLocation", &loc); |
1463 builder->fsCodeAppendf("normalize(%s - vec3(%s.xy, %s))", loc, builder->frag
mentPosition(), z); | 1501 builder->fsCodeAppendf("normalize(%s - vec3(%s.xy, %s))", loc, builder->frag
mentPosition(), z); |
1464 } | 1502 } |
1465 | 1503 |
1466 /////////////////////////////////////////////////////////////////////////////// | 1504 /////////////////////////////////////////////////////////////////////////////// |
1467 | 1505 |
1468 void GrGLSpotLight::setData(const GrGLUniformManager& uman, | 1506 void GrGLSpotLight::setData(const GrGLUniformManager& uman, |
1469 const SkLight* light, | 1507 const SkLight* light) const { |
1470 const SkIPoint& offset) const { | 1508 INHERITED::setData(uman, light); |
1471 INHERITED::setData(uman, light, offset); | |
1472 SkASSERT(light->type() == SkLight::kSpot_LightType); | 1509 SkASSERT(light->type() == SkLight::kSpot_LightType); |
1473 const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); | 1510 const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); |
1474 SkPoint3 location = spotLight->location(); | 1511 setUniformPoint3(uman, fLocationUni, spotLight->location()); |
1475 location.fX -= offset.fX; | |
1476 location.fY -= offset.fY; | |
1477 setUniformPoint3(uman, fLocationUni, location); | |
1478 uman.set1f(fExponentUni, spotLight->specularExponent()); | 1512 uman.set1f(fExponentUni, spotLight->specularExponent()); |
1479 uman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); | 1513 uman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); |
1480 uman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); | 1514 uman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); |
1481 uman.set1f(fConeScaleUni, spotLight->coneScale()); | 1515 uman.set1f(fConeScaleUni, spotLight->coneScale()); |
1482 setUniformNormal3(uman, fSUni, spotLight->s()); | 1516 setUniformNormal3(uman, fSUni, spotLight->s()); |
1483 } | 1517 } |
1484 | 1518 |
1485 void GrGLSpotLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char* z
) { | 1519 void GrGLSpotLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char* z
) { |
1486 const char* location; | 1520 const char* location; |
1487 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 1521 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1538 | 1572 |
1539 #endif | 1573 #endif |
1540 | 1574 |
1541 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1575 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
1542 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1576 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
1543 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1577 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
1544 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDistantLight) | 1578 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDistantLight) |
1545 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPointLight) | 1579 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPointLight) |
1546 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotLight) | 1580 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotLight) |
1547 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1581 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |