//////////////////////////////////////////////////////////////////////////////
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) {
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]));
}
}
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();
+ 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 (size_t x = 0; x < MODES; ++x) {
+ for (int x = 0; x < MODES; ++x) {
SkAutoTUnref<SkColorFilter> compose(SkColorFilter::CreateComposeFilter(filters[y],
filters[x]));
paint.setColorFilter(compose);
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"); )
-
/*
* Copyright 2006 The Android Open Source Project
*
* 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;
*/
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()
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()
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 {
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()
#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)
return SkUnPreMultiply::PMColorToColor(dst);
}
-GrFragmentProcessor* SkColorFilter::asFragmentProcessor(GrContext*) const {
- return NULL;
-}
-
///////////////////////////////////////////////////////////////////////////////////////////////////
class SkComposeColorFilter : public SkColorFilter {
}
#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)
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);
}
}
- 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
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
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
}
};
-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
}
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;
}
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;
(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
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();
+ }
}
}
}
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();
}
}