change colorfilter to return an array of frag processors

BUG=skia:

Review URL: https://codereview.chromium.org/973593002
This commit is contained in:
reed 2015-03-03 06:41:45 -08:00 committed by Commit bot
parent 3ebb16df93
commit cff10b21a9
15 changed files with 155 additions and 53 deletions

View File

@ -193,16 +193,29 @@ DEF_GM( return new TableColorFilterGM; )
//////////////////////////////////////////////////////////////////////////////
class ComposeColorFilterGM : public skiagm::GM {
enum {
COLOR_COUNT = 3,
MODE_COUNT = 4,
};
const SkColor* fColors;
const SkXfermode::Mode* fModes;
SkString fName;
public:
ComposeColorFilterGM() {}
ComposeColorFilterGM(const SkColor colors[], const SkXfermode::Mode modes[],
const char suffix[])
: fColors(colors), fModes(modes)
{
fName.printf("colorcomposefilter_%s", suffix);
}
protected:
virtual SkString onShortName() {
return SkString("composecolorfilter");
return fName;
}
virtual SkISize onISize() {
return SkISize::Make(730, 730);
return SkISize::Make(790, 790);
}
virtual void onDraw(SkCanvas* canvas) {
@ -211,20 +224,12 @@ protected:
canvas->drawColor(0xFFDDDDDD);
SkColor colors[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
SkXfermode::Mode modes[] = {
SkXfermode::kOverlay_Mode,
SkXfermode::kDarken_Mode,
SkXfermode::kColorBurn_Mode,
SkXfermode::kExclusion_Mode,
};
const int MODES = SK_ARRAY_COUNT(modes) * SK_ARRAY_COUNT(colors);
const int MODES = MODE_COUNT * COLOR_COUNT;
SkAutoTUnref<SkColorFilter> filters[MODES];
int index = 0;
for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
for (size_t j = 0; j < SK_ARRAY_COUNT(colors); ++j) {
filters[index++].reset(SkColorFilter::CreateModeFilter(colors[j], modes[i]));
for (int i = 0; i < MODE_COUNT; ++i) {
for (int j = 0; j < COLOR_COUNT; ++j) {
filters[index++].reset(SkColorFilter::CreateModeFilter(fColors[j], fModes[i]));
}
}
@ -235,9 +240,27 @@ protected:
canvas->translate(spacer, spacer);
for (size_t y = 0; y < MODES; ++y) {
canvas->drawRect(r, paint); // orig
for (int i = 0; i < MODES; ++i) {
paint.setColorFilter(filters[i]);
canvas->save();
for (size_t x = 0; x < MODES; ++x) {
canvas->translate((i + 1) * (r.width() + spacer), 0);
canvas->drawRect(r, paint);
canvas->restore();
canvas->save();
canvas->translate(0, (i + 1) * (r.width() + spacer));
canvas->drawRect(r, paint);
canvas->restore();
}
canvas->translate(r.width() + spacer, r.width() + spacer);
for (int y = 0; y < MODES; ++y) {
canvas->save();
for (int x = 0; x < MODES; ++x) {
SkAutoTUnref<SkColorFilter> compose(SkColorFilter::CreateComposeFilter(filters[y],
filters[x]));
paint.setColorFilter(compose);
@ -252,5 +275,21 @@ protected:
private:
typedef GM INHERITED;
};
DEF_GM( return new ComposeColorFilterGM; )
const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
const SkXfermode::Mode gModes0[] = {
SkXfermode::kOverlay_Mode,
SkXfermode::kDarken_Mode,
SkXfermode::kColorBurn_Mode,
SkXfermode::kExclusion_Mode,
};
DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "wacky"); )
const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
const SkXfermode::Mode gModes1[] = {
SkXfermode::kSrcOver_Mode,
SkXfermode::kXor_Mode,
SkXfermode::kDstOut_Mode,
SkXfermode::kSrcATop_Mode,
};
DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "alpha"); )

View File

@ -1,4 +1,3 @@
/*
* Copyright 2006 The Android Open Source Project
*
@ -6,12 +5,12 @@
* found in the LICENSE file.
*/
#ifndef SkColorFilter_DEFINED
#define SkColorFilter_DEFINED
#include "SkColor.h"
#include "SkFlattenable.h"
#include "SkTDArray.h"
#include "SkXfermode.h"
class SkBitmap;
@ -138,10 +137,18 @@ public:
*/
static SkColorFilter* CreateComposeFilter(SkColorFilter* outer, SkColorFilter* inner);
/** A subclass may implement this factory function to work with the GPU backend. If the return
is non-NULL then the caller owns a ref on the returned object.
/**
* A subclass may implement this factory function to work with the GPU backend.
* If it returns true, then 1 or more fragment processors will have been appended to the
* array, each of which has been ref'd, so that the caller is responsible for calling unref()
* on them when they are finished. If more than one processor is appended, they will be
* applied in FIFO order.
*
* If the subclass returns false, then it should not modify the array at all.
*/
virtual GrFragmentProcessor* asFragmentProcessor(GrContext*) const;
virtual bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const {
return false;
}
SK_TO_STRING_PUREVIRT()

View File

@ -25,7 +25,7 @@ public:
uint32_t getFlags() const SK_OVERRIDE;
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext*) const SK_OVERRIDE;
bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const SK_OVERRIDE;
#endif
SK_TO_STRING_OVERRIDE()

View File

@ -28,7 +28,7 @@ public:
SkColorFilter* newComposed(const SkColorFilter*) const SK_OVERRIDE;
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext*) const SK_OVERRIDE;
bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const SK_OVERRIDE;
#endif
struct State {

View File

@ -28,7 +28,7 @@ public:
void filterSpan(const SkPMColor src[], int count, SkPMColor[]) const SK_OVERRIDE;
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext*) const SK_OVERRIDE;
bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const SK_OVERRIDE;
#endif
SK_TO_STRING_OVERRIDE()

View File

@ -40,7 +40,7 @@ public:
#endif
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext*) const SK_OVERRIDE;
bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const SK_OVERRIDE;
#endif
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkModeColorFilter)

View File

@ -37,10 +37,6 @@ SkColor SkColorFilter::filterColor(SkColor c) const {
return SkUnPreMultiply::PMColorToColor(dst);
}
GrFragmentProcessor* SkColorFilter::asFragmentProcessor(GrContext*) const {
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
class SkComposeColorFilter : public SkColorFilter {
@ -75,10 +71,13 @@ public:
}
#endif
#if 0 // TODO: should we support composing the fragments?
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext*) const SK_OVERRIDE;
#endif
bool asFragmentProcessors(GrContext* context,
SkTDArray<GrFragmentProcessor*>* array) const SK_OVERRIDE {
bool hasFrags = fInner->asFragmentProcessors(context, array);
hasFrags |= fOuter->asFragmentProcessors(context, array);
return hasFrags;
}
#endif
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkComposeColorFilter)

View File

@ -337,7 +337,8 @@ void GrColorCubeEffect::GLProcessor::GenKey(const GrProcessor& proc,
const GrGLCaps&, GrProcessorKeyBuilder* b) {
}
GrFragmentProcessor* SkColorCubeFilter::asFragmentProcessor(GrContext* context) const {
bool SkColorCubeFilter::asFragmentProcessors(GrContext* context,
SkTDArray<GrFragmentProcessor*>* array) const {
static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
GrUniqueKey key;
GrUniqueKey::Builder builder(&key, kDomain, 2);
@ -358,6 +359,13 @@ GrFragmentProcessor* SkColorCubeFilter::asFragmentProcessor(GrContext* context)
}
}
return textureCube ? GrColorCubeEffect::Create(textureCube) : NULL;
GrFragmentProcessor* frag = textureCube ? GrColorCubeEffect::Create(textureCube) : NULL;
if (frag) {
if (array) {
*array->append() = frag;
}
return true;
}
return false;
}
#endif

View File

@ -366,11 +366,18 @@ GrFragmentProcessor* ModeColorFilterEffect::TestCreate(SkRandom* rand,
return ModeColorFilterEffect::Create(color, mode);
}
GrFragmentProcessor* SkModeColorFilter::asFragmentProcessor(GrContext*) const {
bool SkModeColorFilter::asFragmentProcessors(GrContext*,
SkTDArray<GrFragmentProcessor*>* array) const {
if (SkXfermode::kDst_Mode != fMode) {
return ModeColorFilterEffect::Create(SkColor2GrColor(fColor), fMode);
GrFragmentProcessor* frag = ModeColorFilterEffect::Create(SkColor2GrColor(fColor), fMode);
if (frag) {
if (array) {
*array->append() = frag;
}
return true;
}
}
return NULL;
return false;
}
#endif

View File

@ -494,8 +494,16 @@ GrFragmentProcessor* ColorMatrixEffect::TestCreate(SkRandom* random,
return ColorMatrixEffect::Create(colorMatrix);
}
GrFragmentProcessor* SkColorMatrixFilter::asFragmentProcessor(GrContext*) const {
return ColorMatrixEffect::Create(fMatrix);
bool SkColorMatrixFilter::asFragmentProcessors(GrContext*,
SkTDArray<GrFragmentProcessor*>* array) const {
GrFragmentProcessor* frag = ColorMatrixEffect::Create(fMatrix);
if (frag) {
if (array) {
*array->append() = frag;
}
return true;
}
return false;
}
#endif

View File

@ -119,7 +119,16 @@ private:
}
};
GrFragmentProcessor* SkLumaColorFilter::asFragmentProcessor(GrContext*) const {
return LumaColorFilterEffect::Create();
bool SkLumaColorFilter::asFragmentProcessors(GrContext*,
SkTDArray<GrFragmentProcessor*>* array) const {
GrFragmentProcessor* frag = LumaColorFilterEffect::Create();
if (frag) {
if (array) {
*array->append() = frag;
}
return true;
}
return false;
}
#endif

View File

@ -967,7 +967,11 @@ bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint&
}
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(
clearColor, SkXfermode::kSrc_Mode));
*fp = cf->asFragmentProcessor(context);
SkTDArray<GrFragmentProcessor*> array;
if (cf->asFragmentProcessors(context, &array)) {
SkASSERT(1 == array.count()); // modecolorfilter only returns one
*fp = array[0]; // transfer ownership to fp
}
return true;
}

View File

@ -44,7 +44,7 @@ public:
SkColorFilter* newComposed(const SkColorFilter* inner) const SK_OVERRIDE;
#if SK_SUPPORT_GPU
GrFragmentProcessor* asFragmentProcessor(GrContext* context) const SK_OVERRIDE;
bool asFragmentProcessors(GrContext*, SkTDArray<GrFragmentProcessor*>*) const SK_OVERRIDE;
#endif
void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const SK_OVERRIDE;
@ -567,14 +567,28 @@ GrFragmentProcessor* ColorTableEffect::TestCreate(SkRandom* random,
(flags & (1 << 2)) ? luts[2] : NULL,
(flags & (1 << 3)) ? luts[3] : NULL
));
return filter->asFragmentProcessor(context);
SkTDArray<GrFragmentProcessor*> array;
if (filter->asFragmentProcessors(context, &array)) {
SkASSERT(1 == array.count()); // TableColorFilter only returns 1
return array[0];
}
return NULL;
}
GrFragmentProcessor* SkTable_ColorFilter::asFragmentProcessor(GrContext* context) const {
bool SkTable_ColorFilter::asFragmentProcessors(GrContext* context,
SkTDArray<GrFragmentProcessor*>* array) const {
SkBitmap bitmap;
this->asComponentTable(&bitmap);
return ColorTableEffect::Create(context, bitmap, fFlags);
GrFragmentProcessor* frag = ColorTableEffect::Create(context, bitmap, fFlags);
if (frag) {
if (array) {
*array->append() = frag;
}
return true;
}
return false;
}
#endif // SK_SUPPORT_GPU

View File

@ -662,9 +662,12 @@ void SkPaint2GrPaintNoShader(GrContext* context, GrRenderTarget* rt, const SkPai
SkColor filtered = colorFilter->filterColor(skPaint.getColor());
grPaint->setColor(SkColor2GrColor(filtered));
} else {
SkAutoTUnref<GrFragmentProcessor> fp(colorFilter->asFragmentProcessor(context));
if (fp.get()) {
grPaint->addColorProcessor(fp);
SkTDArray<GrFragmentProcessor*> array;
if (colorFilter->asFragmentProcessors(context, &array)) {
for (int i = 0; i < array.count(); ++i) {
grPaint->addColorProcessor(array[i]);
array[i]->unref();
}
}
}
}

View File

@ -99,14 +99,18 @@ static void test_getConstantColorComponents(skiatest::Reporter* reporter, GrCont
for (size_t i = 0; i < SK_ARRAY_COUNT(filterTests); ++i) {
const GetConstantComponentTestCase& test = filterTests[i];
SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(test.filterColor, test.filterMode));
SkAutoTUnref<GrFragmentProcessor> effect(cf->asFragmentProcessor(grContext));
SkTDArray<GrFragmentProcessor*> array;
bool hasFrag = cf->asFragmentProcessors(grContext, &array);
REPORTER_ASSERT(reporter, hasFrag);
REPORTER_ASSERT(reporter, 1 == array.count());
GrInvariantOutput inout(test.inputColor,
static_cast<GrColorComponentFlags>(test.inputComponents),
false);
effect->computeInvariantOutput(&inout);
array[0]->computeInvariantOutput(&inout);
REPORTER_ASSERT(reporter, filterColor(inout.color(), inout.validFlags()) == test.outputColor);
REPORTER_ASSERT(reporter, test.outputComponents == inout.validFlags());
array[0]->unref();
}
}