add SkShader::newWithColorFilter
authorreed <reed@google.com>
Thu, 7 Jan 2016 23:47:29 +0000 (15:47 -0800)
committerCommit bot <commit-bot@chromium.org>
Thu, 7 Jan 2016 23:47:29 +0000 (15:47 -0800)
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1556693003

Review URL: https://codereview.chromium.org/1562193002

gm/colorfilterimagefilter.cpp
gyp/core.gypi
include/core/SkShader.h
src/core/SkBlitter.cpp
src/core/SkColorFilterShader.cpp [new file with mode: 0644]
src/core/SkColorFilterShader.h [moved from src/core/SkFilterShader.h with 51% similarity]
src/core/SkFilterShader.cpp [deleted file]
src/ports/SkGlobalInitialization_chromium.cpp
src/ports/SkGlobalInitialization_default.cpp

index efcd9b3..711a7e4 100644 (file)
 #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);
 }
 
@@ -129,3 +187,29 @@ DEF_SIMPLE_GM(colorfilterimagefilter_layer, canvas, 32, 32) {
     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);
+    }
+}
index 04f74fe..a4e64e3 100644 (file)
@@ -70,6 +70,7 @@
         '<(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',
index 4d3ac5c..60ef280 100644 (file)
@@ -15,6 +15,7 @@
 #include "SkPaint.h"
 #include "../gpu/GrColor.h"
 
+class SkColorFilter;
 class SkPath;
 class SkPicture;
 class SkXfermode;
@@ -309,6 +310,12 @@ public:
      *  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
index 064dc16..b73be40 100644 (file)
@@ -9,7 +9,6 @@
 #include "SkAntiRun.h"
 #include "SkColor.h"
 #include "SkColorFilter.h"
-#include "SkFilterShader.h"
 #include "SkReadBuffer.h"
 #include "SkWriteBuffer.h"
 #include "SkMask.h"
@@ -858,7 +857,7 @@ SkBlitter* SkBlitter::Choose(const SkPixmap& device,
 
     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.
diff --git a/src/core/SkColorFilterShader.cpp b/src/core/SkColorFilterShader.cpp
new file mode 100644 (file)
index 0000000..dc90f95
--- /dev/null
@@ -0,0 +1,136 @@
+/*
+ * 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);
+}
similarity index 51%
rename from src/core/SkFilterShader.h
rename to src/core/SkColorFilterShader.h
index ea5eaed..e92908e 100644 (file)
@@ -1,57 +1,62 @@
 /*
- * 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;
 };
 
diff --git a/src/core/SkFilterShader.cpp b/src/core/SkFilterShader.cpp
deleted file mode 100644 (file)
index 2f07c23..0000000
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * 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
index 5da6379..f86fded 100644 (file)
@@ -25,6 +25,7 @@
 #include "SkColorCubeFilter.h"
 #include "SkColorFilter.h"
 #include "SkColorFilterImageFilter.h"
+#include "SkColorFilterShader.h"
 #include "SkColorMatrixFilter.h"
 #include "SkColorShader.h"
 #include "SkComposeImageFilter.h"
@@ -91,6 +92,7 @@ public:
         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)
index 7c39ad6..e710806 100644 (file)
@@ -21,6 +21,7 @@
 #include "SkColorCubeFilter.h"
 #include "SkColorFilter.h"
 #include "SkColorFilterImageFilter.h"
+#include "SkColorFilterShader.h"
 #include "SkColorMatrixFilter.h"
 #include "SkColorShader.h"
 #include "SkComposeImageFilter.h"
@@ -71,6 +72,7 @@ public:
         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)