#define FILTER_HEIGHT SkIntToScalar(30)
#define MARGIN SkIntToScalar(10)
+static SkColorFilter* cf_make_brightness(float brightness) {
+ SkScalar amount255 = SkScalarMul(brightness, SkIntToScalar(255));
+ SkScalar matrix[20] = {
+ 1, 0, 0, 0, amount255,
+ 0, 1, 0, 0, amount255,
+ 0, 0, 1, 0, amount255,
+ 0, 0, 0, 1, 0 };
+ return SkColorMatrixFilter::Create(matrix);
+}
+
+static SkColorFilter* cf_make_grayscale() {
+ SkScalar matrix[20];
+ memset(matrix, 0, 20 * sizeof(SkScalar));
+ matrix[0] = matrix[5] = matrix[10] = 0.2126f;
+ matrix[1] = matrix[6] = matrix[11] = 0.7152f;
+ matrix[2] = matrix[7] = matrix[12] = 0.0722f;
+ matrix[18] = 1.0f;
+ return SkColorMatrixFilter::Create(matrix);
+}
+
+static SkColorFilter* cf_make_colorize(SkColor color) {
+ return SkColorFilter::CreateModeFilter(color, SkXfermode::kSrc_Mode);
+}
+
+static const SkTDArray<SkColorFilter*>& sk_gm_get_colorfilters() {
+ static SkTDArray<SkColorFilter*> gColorFilters;
+
+ if (gColorFilters.count() == 0) {
+ *gColorFilters.append() = cf_make_brightness(0.5f);
+ *gColorFilters.append() = cf_make_grayscale();
+ *gColorFilters.append() = cf_make_colorize(SK_ColorBLUE);
+ }
+ return gColorFilters;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+#include "SkGradientShader.h"
+#include "SkImage.h"
+#include "Resources.h"
+
+static SkShader* sh_make_lineargradient0() {
+ const SkPoint pts[] = { { 0, 0 }, { 100, 100 } };
+ const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
+ return SkGradientShader::CreateLinear(pts, colors, nullptr, 3, SkShader::kRepeat_TileMode);
+}
+
+static SkShader* sh_make_lineargradient1() {
+ const SkPoint pts[] = { { 0, 0 }, { 100, 100 } };
+ const SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
+ return SkGradientShader::CreateLinear(pts, colors, nullptr, 3, SkShader::kRepeat_TileMode);
+}
+
+static SkShader* sh_make_image() {
+ SkAutoTUnref<SkImage> image(GetResourceAsImage("mandrill_128.png"));
+ return image->newShader(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
+}
+
+static const SkTDArray<SkShader*>& sk_gm_get_shaders() {
+ static SkTDArray<SkShader*> gShaders;
+
+ if (gShaders.count() == 0) {
+ *gShaders.append() = sh_make_lineargradient0();
+ *gShaders.append() = sh_make_lineargradient1();
+ *gShaders.append() = sh_make_image();
+ }
+ return gShaders;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
static SkImageFilter* make_blur(float amount, SkImageFilter* input = nullptr) {
return SkBlurImageFilter::Create(amount, amount, input);
}
static SkImageFilter* make_brightness(float amount, SkImageFilter* input = nullptr) {
- SkScalar amount255 = SkScalarMul(amount, SkIntToScalar(255));
- SkScalar matrix[20] = { 1, 0, 0, 0, amount255,
- 0, 1, 0, 0, amount255,
- 0, 0, 1, 0, amount255,
- 0, 0, 0, 1, 0 };
- SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
+ SkAutoTUnref<SkColorFilter> filter(cf_make_brightness(amount));
return SkColorFilterImageFilter::Create(filter, input);
}
static SkImageFilter* make_grayscale(SkImageFilter* input = nullptr) {
- SkScalar matrix[20];
- memset(matrix, 0, 20 * sizeof(SkScalar));
- matrix[0] = matrix[5] = matrix[10] = 0.2126f;
- matrix[1] = matrix[6] = matrix[11] = 0.7152f;
- matrix[2] = matrix[7] = matrix[12] = 0.0722f;
- matrix[18] = 1.0f;
- SkAutoTUnref<SkColorFilter> filter(SkColorMatrixFilter::Create(matrix));
+ SkAutoTUnref<SkColorFilter> filter(cf_make_grayscale());
return SkColorFilterImageFilter::Create(filter, input);
}
static SkImageFilter* make_mode_blue(SkImageFilter* input = nullptr) {
- SkAutoTUnref<SkColorFilter> filter(
- SkColorFilter::CreateModeFilter(SK_ColorBLUE, SkXfermode::kSrc_Mode));
+ SkAutoTUnref<SkColorFilter> filter(cf_make_colorize(SK_ColorBLUE));
return SkColorFilterImageFilter::Create(filter, input);
}
canvas->saveLayer(NULL, &p);
canvas->clear(SK_ColorRED);
}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+DEF_SIMPLE_GM(colorfiltershader, canvas, 800, 800) {
+ const SkTDArray<SkColorFilter*>& filters = sk_gm_get_colorfilters();
+ const SkTDArray<SkShader*>& shaders = sk_gm_get_shaders();
+
+ SkPaint paint;
+ SkRect r = SkRect::MakeWH(120, 120);
+
+ canvas->translate(20, 20);
+ for (int y = 0; y < shaders.count(); ++y) {
+ SkShader* shader = shaders[y];
+
+ canvas->save();
+ for (int x = -1; x < filters.count(); ++x) {
+ SkColorFilter* filter = x >= 0 ? filters[x] : nullptr;
+
+ paint.setShader(shader->newWithColorFilter(filter))->unref();
+ canvas->drawRect(r, paint);
+ canvas->translate(150, 0);
+ }
+ canvas->restore();
+ canvas->translate(0, 150);
+ }
+}
'<(skia_src_path)/core/SkClipStack.cpp',
'<(skia_src_path)/core/SkColor.cpp',
'<(skia_src_path)/core/SkColorFilter.cpp',
+ '<(skia_src_path)/core/SkColorFilterShader.cpp',
'<(skia_src_path)/core/SkColorShader.h',
'<(skia_src_path)/core/SkColorTable.cpp',
'<(skia_src_path)/core/SkComposeShader.cpp',
'<(skia_src_path)/core/SkFDot6.h',
'<(skia_src_path)/core/SkFilterProc.cpp',
'<(skia_src_path)/core/SkFilterProc.h',
- '<(skia_src_path)/core/SkFilterShader.cpp',
'<(skia_src_path)/core/SkFindAndPlaceGlyph.h',
'<(skia_src_path)/core/SkFlattenable.cpp',
'<(skia_src_path)/core/SkFlattenableSerialization.cpp',
#include "SkPaint.h"
#include "../gpu/GrColor.h"
+class SkColorFilter;
class SkPath;
class SkPicture;
class SkXfermode;
* The specified matrix will be applied before any matrix associated with this shader.
*/
SkShader* newWithLocalMatrix(const SkMatrix&) const;
+
+ /**
+ * Create a new shader that produces the same colors as invoking this shader and then applying
+ * the colorfilter.
+ */
+ SkShader* newWithColorFilter(SkColorFilter*) const;
//////////////////////////////////////////////////////////////////////////
// Factory methods for stock shaders
#include "SkAntiRun.h"
#include "SkColor.h"
#include "SkColorFilter.h"
-#include "SkFilterShader.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkMask.h"
if (cf) {
SkASSERT(shader);
- shader = new SkFilterShader(shader, cf);
+ shader = shader->newWithColorFilter(cf);
paint.writable()->setShader(shader)->unref();
// blitters should ignore the presence/absence of a filter, since
// if there is one, the shader will take care of it.
--- /dev/null
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkColorFilterShader.h"
+#include "SkReadBuffer.h"
+#include "SkWriteBuffer.h"
+#include "SkShader.h"
+#include "SkString.h"
+
+#if SK_SUPPORT_GPU
+#include "GrFragmentProcessor.h"
+#endif
+
+SkColorFilterShader::SkColorFilterShader(SkShader* shader, SkColorFilter* filter)
+ : fShader(SkRef(shader))
+ , fFilter(SkRef(filter))
+{
+ SkASSERT(shader);
+ SkASSERT(filter);
+}
+
+SkFlattenable* SkColorFilterShader::CreateProc(SkReadBuffer& buffer) {
+ SkAutoTUnref<SkShader> shader(buffer.readShader());
+ SkAutoTUnref<SkColorFilter> filter(buffer.readColorFilter());
+ if (!shader.get() || !filter.get()) {
+ return nullptr;
+ }
+ return new SkColorFilterShader(shader, filter);
+}
+
+void SkColorFilterShader::flatten(SkWriteBuffer& buffer) const {
+ buffer.writeFlattenable(fShader);
+ buffer.writeFlattenable(fFilter);
+}
+
+uint32_t SkColorFilterShader::FilterShaderContext::getFlags() const {
+ const SkColorFilterShader& filterShader = static_cast<const SkColorFilterShader&>(fShader);
+
+ uint32_t shaderF = fShaderContext->getFlags();
+ uint32_t filterF = filterShader.fFilter->getFlags();
+
+ // if the filter might change alpha, clear the opaque flag in the shader
+ if (!(filterF & SkColorFilter::kAlphaUnchanged_Flag)) {
+ shaderF &= ~SkShader::kOpaqueAlpha_Flag;
+ }
+ return shaderF;
+}
+
+SkShader::Context* SkColorFilterShader::onCreateContext(const ContextRec& rec,
+ void* storage) const {
+ char* shaderContextStorage = (char*)storage + sizeof(FilterShaderContext);
+ SkShader::Context* shaderContext = fShader->createContext(rec, shaderContextStorage);
+ if (nullptr == shaderContext) {
+ return nullptr;
+ }
+ return new (storage) FilterShaderContext(*this, shaderContext, rec);
+}
+
+size_t SkColorFilterShader::contextSize() const {
+ return sizeof(FilterShaderContext) + fShader->contextSize();
+}
+
+SkColorFilterShader::FilterShaderContext::FilterShaderContext(
+ const SkColorFilterShader& filterShader,
+ SkShader::Context* shaderContext,
+ const ContextRec& rec)
+ : INHERITED(filterShader, rec)
+ , fShaderContext(shaderContext)
+{}
+
+SkColorFilterShader::FilterShaderContext::~FilterShaderContext() {
+ fShaderContext->~Context();
+}
+
+void SkColorFilterShader::FilterShaderContext::shadeSpan(int x, int y, SkPMColor result[],
+ int count) {
+ const SkColorFilterShader& filterShader = static_cast<const SkColorFilterShader&>(fShader);
+
+ fShaderContext->shadeSpan(x, y, result, count);
+ filterShader.fFilter->filterSpan(result, count, result);
+}
+
+#if SK_SUPPORT_GPU
+/////////////////////////////////////////////////////////////////////
+
+const GrFragmentProcessor* SkColorFilterShader::asFragmentProcessor(
+ GrContext* context,
+ const SkMatrix& viewM,
+ const SkMatrix* localMatrix,
+ SkFilterQuality fq) const {
+
+ SkAutoTUnref<const GrFragmentProcessor> fp1(fShader->asFragmentProcessor(context, viewM,
+ localMatrix, fq));
+ if (!fp1.get()) {
+ return nullptr;
+ }
+
+ SkAutoTUnref<const GrFragmentProcessor> fp2(fFilter->asFragmentProcessor(context));
+ if (!fp2.get()) {
+ return fp1.release();
+ }
+
+ const GrFragmentProcessor* fpSeries[] = { fp1.get(), fp2.get() };
+
+ return GrFragmentProcessor::RunInSeries(fpSeries, 2);
+}
+#endif
+
+#ifndef SK_IGNORE_TO_STRING
+void SkColorFilterShader::toString(SkString* str) const {
+ str->append("SkColorFilterShader: (");
+
+ str->append("Shader: ");
+ fShader->toString(str);
+ str->append(" Filter: ");
+ // TODO: add "fFilter->toString(str);" once SkColorFilter::toString is added
+
+ this->INHERITED::toString(str);
+
+ str->append(")");
+}
+#endif
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+SkShader* SkShader::newWithColorFilter(SkColorFilter* filter) const {
+ SkShader* base = const_cast<SkShader*>(this);
+ if (!filter) {
+ return SkRef(base);
+ }
+ return new SkColorFilterShader(base, filter);
+}
/*
- * Copyright 2012 Google Inc.
+ * Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-#ifndef SkFilterShader_DEFINED
-#define SkFilterShader_DEFINED
+#ifndef SkColorFilterShader_DEFINED
+#define SkColorFilterShader_DEFINED
+#include "SkColorFilter.h"
#include "SkShader.h"
-class SkColorFilter;
-
-class SkFilterShader : public SkShader {
+class SkColorFilterShader : public SkShader {
public:
- SkFilterShader(SkShader* shader, SkColorFilter* filter);
- virtual ~SkFilterShader();
-
+ SkColorFilterShader(SkShader* shader, SkColorFilter* filter);
+
size_t contextSize() const override;
+
+#if SK_SUPPORT_GPU
+ const GrFragmentProcessor* asFragmentProcessor(GrContext*,
+ const SkMatrix& viewM,
+ const SkMatrix* localMatrix,
+ SkFilterQuality) const override;
+#endif
class FilterShaderContext : public SkShader::Context {
public:
// Takes ownership of shaderContext and calls its destructor.
- FilterShaderContext(const SkFilterShader&, SkShader::Context*, const ContextRec&);
+ FilterShaderContext(const SkColorFilterShader&, SkShader::Context*, const ContextRec&);
virtual ~FilterShaderContext();
-
+
uint32_t getFlags() const override;
-
+
void shadeSpan(int x, int y, SkPMColor[], int count) override;
-
+
void set3DMask(const SkMask* mask) override {
// forward to our proxy
fShaderContext->set3DMask(mask);
}
-
+
private:
SkShader::Context* fShaderContext;
-
+
typedef SkShader::Context INHERITED;
};
-
+
SK_TO_STRING_OVERRIDE()
- SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkFilterShader)
-
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorFilterShader)
+
protected:
void flatten(SkWriteBuffer&) const override;
Context* onCreateContext(const ContextRec&, void* storage) const override;
-
-
+
+
private:
- SkShader* fShader;
- SkColorFilter* fFilter;
-
+ SkAutoTUnref<SkShader> fShader;
+ SkAutoTUnref<SkColorFilter> fFilter;
+
typedef SkShader INHERITED;
};
+++ /dev/null
-/*
- * Copyright 2013 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "SkFilterShader.h"
-
-#include "SkColorFilter.h"
-#include "SkReadBuffer.h"
-#include "SkWriteBuffer.h"
-#include "SkShader.h"
-#include "SkString.h"
-
-SkFilterShader::SkFilterShader(SkShader* shader, SkColorFilter* filter) {
- fShader = shader;
- shader->ref();
-
- fFilter = filter;
- filter->ref();
-}
-
-SkFilterShader::~SkFilterShader() {
- fFilter->unref();
- fShader->unref();
-}
-
-SkFlattenable* SkFilterShader::CreateProc(SkReadBuffer& buffer) {
- SkAutoTUnref<SkShader> shader(buffer.readShader());
- SkAutoTUnref<SkColorFilter> filter(buffer.readColorFilter());
- if (!shader.get() || !filter.get()) {
- return nullptr;
- }
- return new SkFilterShader(shader, filter);
-}
-
-void SkFilterShader::flatten(SkWriteBuffer& buffer) const {
- buffer.writeFlattenable(fShader);
- buffer.writeFlattenable(fFilter);
-}
-
-uint32_t SkFilterShader::FilterShaderContext::getFlags() const {
- const SkFilterShader& filterShader = static_cast<const SkFilterShader&>(fShader);
-
- uint32_t shaderF = fShaderContext->getFlags();
- uint32_t filterF = filterShader.fFilter->getFlags();
-
- // if the filter might change alpha, clear the opaque flag in the shader
- if (!(filterF & SkColorFilter::kAlphaUnchanged_Flag)) {
- shaderF &= ~SkShader::kOpaqueAlpha_Flag;
- }
- return shaderF;
-}
-
-SkShader::Context* SkFilterShader::onCreateContext(const ContextRec& rec, void* storage) const {
- char* shaderContextStorage = (char*)storage + sizeof(FilterShaderContext);
- SkShader::Context* shaderContext = fShader->createContext(rec, shaderContextStorage);
- if (nullptr == shaderContext) {
- return nullptr;
- }
- return new (storage) FilterShaderContext(*this, shaderContext, rec);
-}
-
-size_t SkFilterShader::contextSize() const {
- return sizeof(FilterShaderContext) + fShader->contextSize();
-}
-
-SkFilterShader::FilterShaderContext::FilterShaderContext(const SkFilterShader& filterShader,
- SkShader::Context* shaderContext,
- const ContextRec& rec)
- : INHERITED(filterShader, rec)
- , fShaderContext(shaderContext) {}
-
-SkFilterShader::FilterShaderContext::~FilterShaderContext() {
- fShaderContext->~Context();
-}
-
-void SkFilterShader::FilterShaderContext::shadeSpan(int x, int y, SkPMColor result[], int count) {
- const SkFilterShader& filterShader = static_cast<const SkFilterShader&>(fShader);
-
- fShaderContext->shadeSpan(x, y, result, count);
- filterShader.fFilter->filterSpan(result, count, result);
-}
-
-#ifndef SK_IGNORE_TO_STRING
-void SkFilterShader::toString(SkString* str) const {
- str->append("SkFilterShader: (");
-
- str->append("Shader: ");
- fShader->toString(str);
- str->append(" Filter: ");
- // TODO: add "fFilter->toString(str);" once SkColorFilter::toString is added
-
- this->INHERITED::toString(str);
-
- str->append(")");
-}
-#endif
#include "SkColorCubeFilter.h"
#include "SkColorFilter.h"
#include "SkColorFilterImageFilter.h"
+#include "SkColorFilterShader.h"
#include "SkColorMatrixFilter.h"
#include "SkColorShader.h"
#include "SkComposeImageFilter.h"
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurDrawLooper)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorCubeFilter)
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorFilterShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorMatrixFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkComposePathEffect)
#include "SkColorCubeFilter.h"
#include "SkColorFilter.h"
#include "SkColorFilterImageFilter.h"
+#include "SkColorFilterShader.h"
#include "SkColorMatrixFilter.h"
#include "SkColorShader.h"
#include "SkComposeImageFilter.h"
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurImageFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorCubeFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorMatrixFilter)
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorFilterShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkColorShader)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkComposePathEffect)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkComposeShader)