Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Unified Diff: gm/colorfilterimagefilter.cpp

Issue 1562193002: add SkShader::newWithColorFilter (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tweak formatting Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/colorfilterimagefilter.cpp
diff --git a/gm/colorfilterimagefilter.cpp b/gm/colorfilterimagefilter.cpp
index efcd9b336df1e880480009187e103e17a056dc06..711a7e48c337e9cdd5bbd89af8981f9bfc844737 100644
--- a/gm/colorfilterimagefilter.cpp
+++ b/gm/colorfilterimagefilter.cpp
@@ -18,34 +18,92 @@
#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);
+ }
+}
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698