| 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 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 *dstC++ = cache[toggle + index]; | 452 *dstC++ = cache[toggle + index]; |
| 453 toggle = next_dither_toggle16(toggle); | 453 toggle = next_dither_toggle16(toggle); |
| 454 | 454 |
| 455 dstX += SK_Scalar1; | 455 dstX += SK_Scalar1; |
| 456 } while (--count != 0); | 456 } while (--count != 0); |
| 457 } | 457 } |
| 458 } | 458 } |
| 459 | 459 |
| 460 #if SK_SUPPORT_GPU | 460 #if SK_SUPPORT_GPU |
| 461 | 461 |
| 462 #include "GrTBackendEffectFactory.h" | 462 #include "GrTBackendProcessorFactory.h" |
| 463 #include "gl/builders/GrGLProgramBuilder.h" | 463 #include "gl/builders/GrGLProgramBuilder.h" |
| 464 #include "SkGr.h" | 464 #include "SkGr.h" |
| 465 | 465 |
| 466 ///////////////////////////////////////////////////////////////////// | 466 ///////////////////////////////////////////////////////////////////// |
| 467 | 467 |
| 468 class GrGLLinearGradient : public GrGLGradientEffect { | 468 class GrGLLinearGradient : public GrGLGradientEffect { |
| 469 public: | 469 public: |
| 470 | 470 |
| 471 GrGLLinearGradient(const GrBackendEffectFactory& factory, const GrEffect&) | 471 GrGLLinearGradient(const GrBackendProcessorFactory& factory, const GrProcess
or&) |
| 472 : INHERITED (factory) { } | 472 : INHERITED (factory) { } |
| 473 | 473 |
| 474 virtual ~GrGLLinearGradient() { } | 474 virtual ~GrGLLinearGradient() { } |
| 475 | 475 |
| 476 virtual void emitCode(GrGLProgramBuilder*, | 476 virtual void emitCode(GrGLProgramBuilder*, |
| 477 const GrEffect&, | 477 const GrFragmentProcessor&, |
| 478 const GrEffectKey&, | 478 const GrProcessorKey&, |
| 479 const char* outputColor, | 479 const char* outputColor, |
| 480 const char* inputColor, | 480 const char* inputColor, |
| 481 const TransformedCoordsArray&, | 481 const TransformedCoordsArray&, |
| 482 const TextureSamplerArray&) SK_OVERRIDE; | 482 const TextureSamplerArray&) SK_OVERRIDE; |
| 483 | 483 |
| 484 static void GenKey(const GrEffect& effect, const GrGLCaps&, GrEffectKeyBuild
er* b) { | 484 static void GenKey(const GrProcessor& processor, const GrGLCaps&, GrProcesso
rKeyBuilder* b) { |
| 485 b->add32(GenBaseGradientKey(effect)); | 485 b->add32(GenBaseGradientKey(processor)); |
| 486 } | 486 } |
| 487 | 487 |
| 488 private: | 488 private: |
| 489 | 489 |
| 490 typedef GrGLGradientEffect INHERITED; | 490 typedef GrGLGradientEffect INHERITED; |
| 491 }; | 491 }; |
| 492 | 492 |
| 493 ///////////////////////////////////////////////////////////////////// | 493 ///////////////////////////////////////////////////////////////////// |
| 494 | 494 |
| 495 class GrLinearGradient : public GrGradientEffect { | 495 class GrLinearGradient : public GrGradientEffect { |
| 496 public: | 496 public: |
| 497 | 497 |
| 498 static GrEffect* Create(GrContext* ctx, | 498 static GrFragmentProcessor* Create(GrContext* ctx, |
| 499 const SkLinearGradient& shader, | 499 const SkLinearGradient& shader, |
| 500 const SkMatrix& matrix, | 500 const SkMatrix& matrix, |
| 501 SkShader::TileMode tm) { | 501 SkShader::TileMode tm) { |
| 502 return SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)); | 502 return SkNEW_ARGS(GrLinearGradient, (ctx, shader, matrix, tm)); |
| 503 } | 503 } |
| 504 | 504 |
| 505 virtual ~GrLinearGradient() { } | 505 virtual ~GrLinearGradient() { } |
| 506 | 506 |
| 507 static const char* Name() { return "Linear Gradient"; } | 507 static const char* Name() { return "Linear Gradient"; } |
| 508 const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 508 const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE { |
| 509 return GrTBackendEffectFactory<GrLinearGradient>::getInstance(); | 509 return GrTBackendFragmentProcessorFactory<GrLinearGradient>::getInstance
(); |
| 510 } | 510 } |
| 511 | 511 |
| 512 typedef GrGLLinearGradient GLEffect; | 512 typedef GrGLLinearGradient GLProcessor; |
| 513 | 513 |
| 514 private: | 514 private: |
| 515 GrLinearGradient(GrContext* ctx, | 515 GrLinearGradient(GrContext* ctx, |
| 516 const SkLinearGradient& shader, | 516 const SkLinearGradient& shader, |
| 517 const SkMatrix& matrix, | 517 const SkMatrix& matrix, |
| 518 SkShader::TileMode tm) | 518 SkShader::TileMode tm) |
| 519 : INHERITED(ctx, shader, matrix, tm) { } | 519 : INHERITED(ctx, shader, matrix, tm) { } |
| 520 GR_DECLARE_EFFECT_TEST; | 520 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 521 | 521 |
| 522 typedef GrGradientEffect INHERITED; | 522 typedef GrGradientEffect INHERITED; |
| 523 }; | 523 }; |
| 524 | 524 |
| 525 ///////////////////////////////////////////////////////////////////// | 525 ///////////////////////////////////////////////////////////////////// |
| 526 | 526 |
| 527 GR_DEFINE_EFFECT_TEST(GrLinearGradient); | 527 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrLinearGradient); |
| 528 | 528 |
| 529 GrEffect* GrLinearGradient::TestCreate(SkRandom* random, | 529 GrFragmentProcessor* GrLinearGradient::TestCreate(SkRandom* random, |
| 530 GrContext* context, | 530 GrContext* context, |
| 531 const GrDrawTargetCaps&, | 531 const GrDrawTargetCaps&, |
| 532 GrTexture**) { | 532 GrTexture**) { |
| 533 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, | 533 SkPoint points[] = {{random->nextUScalar1(), random->nextUScalar1()}, |
| 534 {random->nextUScalar1(), random->nextUScalar1()}}; | 534 {random->nextUScalar1(), random->nextUScalar1()}}; |
| 535 | 535 |
| 536 SkColor colors[kMaxRandomGradientColors]; | 536 SkColor colors[kMaxRandomGradientColors]; |
| 537 SkScalar stopsArray[kMaxRandomGradientColors]; | 537 SkScalar stopsArray[kMaxRandomGradientColors]; |
| 538 SkScalar* stops = stopsArray; | 538 SkScalar* stops = stopsArray; |
| 539 SkShader::TileMode tm; | 539 SkShader::TileMode tm; |
| 540 int colorCount = RandomGradientParams(random, colors, &stops, &tm); | 540 int colorCount = RandomGradientParams(random, colors, &stops, &tm); |
| 541 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, | 541 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(points, |
| 542 colors, stops,
colorCount, | 542 colors, stops,
colorCount, |
| 543 tm)); | 543 tm)); |
| 544 SkPaint paint; | 544 SkPaint paint; |
| 545 GrColor paintColor; | 545 GrColor paintColor; |
| 546 GrEffect* effect; | 546 GrFragmentProcessor* fp; |
| 547 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec
t)); | 547 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor
, &fp)); |
| 548 return effect; | 548 return fp; |
| 549 } | 549 } |
| 550 | 550 |
| 551 ///////////////////////////////////////////////////////////////////// | 551 ///////////////////////////////////////////////////////////////////// |
| 552 | 552 |
| 553 void GrGLLinearGradient::emitCode(GrGLProgramBuilder* builder, | 553 void GrGLLinearGradient::emitCode(GrGLProgramBuilder* builder, |
| 554 const GrEffect&, | 554 const GrFragmentProcessor&, |
| 555 const GrEffectKey& key, | 555 const GrProcessorKey& key, |
| 556 const char* outputColor, | 556 const char* outputColor, |
| 557 const char* inputColor, | 557 const char* inputColor, |
| 558 const TransformedCoordsArray& coords, | 558 const TransformedCoordsArray& coords, |
| 559 const TextureSamplerArray& samplers) { | 559 const TextureSamplerArray& samplers) { |
| 560 uint32_t baseKey = key.get32(0); | 560 uint32_t baseKey = key.get32(0); |
| 561 this->emitUniforms(builder, baseKey); | 561 this->emitUniforms(builder, baseKey); |
| 562 SkString t = builder->getFragmentShaderBuilder()->ensureFSCoords2D(coords, 0
); | 562 SkString t = builder->getFragmentShaderBuilder()->ensureFSCoords2D(coords, 0
); |
| 563 t.append(".x"); | 563 t.append(".x"); |
| 564 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample
rs); | 564 this->emitColor(builder, t.c_str(), baseKey, outputColor, inputColor, sample
rs); |
| 565 } | 565 } |
| 566 | 566 |
| 567 ///////////////////////////////////////////////////////////////////// | 567 ///////////////////////////////////////////////////////////////////// |
| 568 | 568 |
| 569 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 569 bool SkLinearGradient::asFragmentProcessor(GrContext* context, const SkPaint& pa
int, |
| 570 const SkMatrix* localMatrix, GrColor* paintCo
lor, | 570 const SkMatrix* localMatrix, GrColor*
paintColor, |
| 571 GrEffect** effect) const { | 571 GrFragmentProcessor** fp) const { |
| 572 SkASSERT(context); | 572 SkASSERT(context); |
| 573 | 573 |
| 574 SkMatrix matrix; | 574 SkMatrix matrix; |
| 575 if (!this->getLocalMatrix().invert(&matrix)) { | 575 if (!this->getLocalMatrix().invert(&matrix)) { |
| 576 return false; | 576 return false; |
| 577 } | 577 } |
| 578 if (localMatrix) { | 578 if (localMatrix) { |
| 579 SkMatrix inv; | 579 SkMatrix inv; |
| 580 if (!localMatrix->invert(&inv)) { | 580 if (!localMatrix->invert(&inv)) { |
| 581 return false; | 581 return false; |
| 582 } | 582 } |
| 583 matrix.postConcat(inv); | 583 matrix.postConcat(inv); |
| 584 } | 584 } |
| 585 matrix.postConcat(fPtsToUnit); | 585 matrix.postConcat(fPtsToUnit); |
| 586 | 586 |
| 587 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); | 587 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
| 588 *effect = GrLinearGradient::Create(context, *this, matrix, fTileMode); | 588 *fp = GrLinearGradient::Create(context, *this, matrix, fTileMode); |
| 589 | 589 |
| 590 return true; | 590 return true; |
| 591 } | 591 } |
| 592 | 592 |
| 593 #else | 593 #else |
| 594 | 594 |
| 595 bool SkLinearGradient::asNewEffect(GrContext* context, const SkPaint& paint, | 595 bool SkLinearGradient::asFragmentProcessor(GrContext*, const SkPaint&, const SkM
atrix*, GrColor*, |
| 596 const SkMatrix* localMatrix, GrColor* paintCo
lor, | 596 GrFragmentProcessor**) const { |
| 597 GrEffect** effect) const { | |
| 598 SkDEBUGFAIL("Should not call in GPU-less build"); | 597 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 599 return false; | 598 return false; |
| 600 } | 599 } |
| 601 | 600 |
| 602 #endif | 601 #endif |
| 603 | 602 |
| 604 #ifndef SK_IGNORE_TO_STRING | 603 #ifndef SK_IGNORE_TO_STRING |
| 605 void SkLinearGradient::toString(SkString* str) const { | 604 void SkLinearGradient::toString(SkString* str) const { |
| 606 str->append("SkLinearGradient ("); | 605 str->append("SkLinearGradient ("); |
| 607 | 606 |
| 608 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); | 607 str->appendf("start: (%f, %f)", fStart.fX, fStart.fY); |
| 609 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); | 608 str->appendf(" end: (%f, %f) ", fEnd.fX, fEnd.fY); |
| 610 | 609 |
| 611 this->INHERITED::toString(str); | 610 this->INHERITED::toString(str); |
| 612 | 611 |
| 613 str->append(")"); | 612 str->append(")"); |
| 614 } | 613 } |
| 615 #endif | 614 #endif |
| OLD | NEW |