OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
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 "SkLinearGradient.h" | 8 #include "SkLinearGradient.h" |
9 | 9 |
10 static inline int repeat_bits(int x, const int bits) { | 10 static inline int repeat_bits(int x, const int bits) { |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 private: | 473 private: |
474 | 474 |
475 typedef GrGLGradientEffect INHERITED; | 475 typedef GrGLGradientEffect INHERITED; |
476 }; | 476 }; |
477 | 477 |
478 ///////////////////////////////////////////////////////////////////// | 478 ///////////////////////////////////////////////////////////////////// |
479 | 479 |
480 class GrLinearGradient : public GrGradientEffect { | 480 class GrLinearGradient : public GrGradientEffect { |
481 public: | 481 public: |
482 | 482 |
483 static GrEffectRef* Create(GrContext* ctx, | 483 static GrEffect* Create(GrContext* ctx, |
484 const SkLinearGradient& shader, | 484 const SkLinearGradient& shader, |
485 const SkMatrix& matrix, | 485 const SkMatrix& matrix, |
486 SkShader::TileMode tm) { | 486 SkShader::TileMode tm) { |
487 return SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)); | 487 return SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)); |
488 } | 488 } |
489 | 489 |
490 virtual ~GrLinearGradient() { } | 490 virtual ~GrLinearGradient() { } |
491 | 491 |
492 static const char* Name() { return "Linear Gradient"; } | 492 static const char* Name() { return "Linear Gradient"; } |
493 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 493 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
494 return GrTBackendEffectFactory<GrLinearGradient>::getInstance(); | 494 return GrTBackendEffectFactory<GrLinearGradient>::getInstance(); |
495 } | 495 } |
496 | 496 |
497 typedef GrGLLinearGradient GLEffect; | 497 typedef GrGLLinearGradient GLEffect; |
498 | 498 |
499 private: | 499 private: |
500 GrLinearGradient(GrContext* ctx, | 500 GrLinearGradient(GrContext* ctx, |
501 const SkLinearGradient& shader, | 501 const SkLinearGradient& shader, |
502 const SkMatrix& matrix, | 502 const SkMatrix& matrix, |
503 SkShader::TileMode tm) | 503 SkShader::TileMode tm) |
504 : INHERITED(ctx, shader, matrix, tm) { } | 504 : INHERITED(ctx, shader, matrix, tm) { } |
505 GR_DECLARE_EFFECT_TEST; | 505 GR_DECLARE_EFFECT_TEST; |
506 | 506 |
507 typedef GrGradientEffect INHERITED; | 507 typedef GrGradientEffect INHERITED; |
508 }; | 508 }; |
509 | 509 |
510 ///////////////////////////////////////////////////////////////////// | 510 ///////////////////////////////////////////////////////////////////// |
511 | 511 |
512 GR_DEFINE_EFFECT_TEST(GrLinearGradient); | 512 GR_DEFINE_EFFECT_TEST(GrLinearGradient); |
513 | 513 |
514 GrEffectRef* GrLinearGradient::TestCreate(SkRandom* random, | 514 GrEffect* GrLinearGradient::TestCreate(SkRandom* random, |
515 GrContext* context, | 515 GrContext* context, |
516 const GrDrawTargetCaps&, | 516 const GrDrawTargetCaps&, |
517 GrTexture**) { | 517 GrTexture**) { |
518 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, | 518 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, |
519 {random->nextUScalar1(), random->nextUScalar1()}}; | 519 {random->nextUScalar1(), random->nextUScalar1()}}; |
520 | 520 |
521 SkColor colors[kMaxRandomGradientColors]; | 521 SkColor colors[kMaxRandomGradientColors]; |
522 SkScalar stopsArray[kMaxRandomGradientColors]; | 522 SkScalar stopsArray[kMaxRandomGradientColors]; |
523 SkScalar* stops = stopsArray; | 523 SkScalar* stops = stopsArray; |
524 SkShader::TileMode tm; | 524 SkShader::TileMode tm; |
525 int colorCount = RandomGradientParams(random, colors, &stops, &tm); | 525 int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
526 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, | 526 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, |
527 colors, stops,
colorCount, | 527 colors, stops,
colorCount, |
528 tm)); | 528 tm)); |
529 SkPaint paint; | 529 SkPaint paint; |
530 GrColor grColor; | 530 GrColor paintColor; |
531 GrEffectRef* effect; | 531 GrEffect* effect; |
532 shader->asNewEffect(context, paint, NULL, &grColor, &effect); | 532 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec
t)); |
533 return effect; | 533 return effect; |
534 } | 534 } |
535 | 535 |
536 ///////////////////////////////////////////////////////////////////// | 536 ///////////////////////////////////////////////////////////////////// |
537 | 537 |
538 void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder, | 538 void GrGLLinearGradient::emitCode(GrGLShaderBuilder* builder, |
539 const GrDrawEffect&, | 539 const GrDrawEffect&, |
540 EffectKey key, | 540 EffectKey key, |
541 const char* outputColor, | 541 const char* outputColor, |
542 const char* inputColor, | 542 const char* inputColor, |
543 const TransformedCoordsArray& coords, | 543 const TransformedCoordsArray& coords, |
544 const TextureSamplerArray& samplers) { | 544 const TextureSamplerArray& samplers) { |
545 this->emitUniforms(builder, key); | 545 this->emitUniforms(builder, key); |
546 SkString t = builder->ensureFSCoords2D(coords, 0); | 546 SkString t = builder->ensureFSCoords2D(coords, 0); |
547 t.append(".x"); | 547 t.append(".x"); |
548 this->emitColor(builder, t.c_str(), key, outputColor, inputColor, samplers); | 548 this->emitColor(builder, t.c_str(), key, outputColor, inputColor, samplers); |
549 } | 549 } |
550 | 550 |
551 ///////////////////////////////////////////////////////////////////// | 551 ///////////////////////////////////////////////////////////////////// |
552 | 552 |
553 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 553 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
554 const SkMatrix* localMatrix, GrColor* grColor
, | 554 const SkMatrix* localMatrix, GrColor* paintCo
lor, |
555 GrEffectRef** grEffect) const { | 555 GrEffect** effect) const { |
556 SkASSERT(NULL != context); | 556 SkASSERT(NULL != context); |
557 | 557 |
558 SkMatrix matrix; | 558 SkMatrix matrix; |
559 if (!this->getLocalMatrix().invert(&matrix)) { | 559 if (!this->getLocalMatrix().invert(&matrix)) { |
560 return false; | 560 return false; |
561 } | 561 } |
562 if (localMatrix) { | 562 if (localMatrix) { |
563 SkMatrix inv; | 563 SkMatrix inv; |
564 if (!localMatrix->invert(&inv)) { | 564 if (!localMatrix->invert(&inv)) { |
565 return false; | 565 return false; |
566 } | 566 } |
567 matrix.postConcat(inv); | 567 matrix.postConcat(inv); |
568 } | 568 } |
569 matrix.postConcat(fPtsToUnit); | 569 matrix.postConcat(fPtsToUnit); |
570 | 570 |
571 *grColor = SkColor2GrColorJustAlpha(paint.getColor()); | 571 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
572 *grEffect = GrLinearGradient::Create(context, *this, matrix, fTileMode); | 572 *effect = GrLinearGradient::Create(context, *this, matrix, fTileMode); |
573 | 573 |
574 return true; | 574 return true; |
575 } | 575 } |
576 | 576 |
577 #else | 577 #else |
578 | 578 |
579 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 579 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, |
580 const SkMatrix* localMatrix, GrColor* grColor
, | 580 const SkMatrix* localMatrix, GrColor* paintCo
lor, |
581 GrEffect** grEffect) const { | 581 GrEffect** effect) const { |
582 SkDEBUGFAIL("Should not call in GPU-less build"); | 582 SkDEBUGFAIL("Should not call in GPU-less build"); |
583 return false; | 583 return false; |
584 } | 584 } |
585 | 585 |
586 #endif | 586 #endif |
587 | 587 |
588 #ifndef SK_IGNORE_TO_STRING | 588 #ifndef SK_IGNORE_TO_STRING |
589 void SkLinearGradient::toString(SkString* str) const { | 589 void SkLinearGradient::toString(SkString* str) const { |
590 str->append("SkLinearGradient ("); | 590 str->append("SkLinearGradient ("); |
591 | 591 |
592 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); | 592 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); |
593 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); | 593 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); |
594 | 594 |
595 this->INHERITED::toString(str); | 595 this->INHERITED::toString(str); |
596 | 596 |
597 str->append(")"); | 597 str->append(")"); |
598 } | 598 } |
599 #endif | 599 #endif |
OLD | NEW |