reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkSurface.h" |
| 13 | |
| 14 | #include "SkColorMatrixFilter.h" |
| 15 | #include "SkGradientShader.h" |
| 16 | |
| 17 | static SkShader* make_opaque_color() { |
| 18 | return SkShader::CreateColorShader(0xFFFF0000); |
| 19 | } |
| 20 | |
| 21 | static SkShader* make_alpha_color() { |
| 22 | return SkShader::CreateColorShader(0x80FF0000); |
| 23 | } |
| 24 | |
| 25 | static SkColorFilter* make_cf_null() { |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | static SkColorFilter* make_cf0() { |
| 30 | SkColorMatrix cm; |
| 31 | cm.setSaturation(0.75f); |
| 32 | return SkColorMatrixFilter::Create(cm); |
| 33 | } |
| 34 | |
reed | f7cdb06 | 2016-02-04 11:35:27 -0800 | [diff] [blame^] | 35 | static SkColorFilter* make_cf1() { |
| 36 | SkColorMatrix cm; |
| 37 | cm.setSaturation(0.75f); |
| 38 | SkAutoTUnref<SkColorFilter> a(SkColorMatrixFilter::Create(cm)); |
| 39 | // CreateComposedFilter will try to concat these two matrices, resulting in a single |
| 40 | // filter (which is good for speed). For this test, we want to force a real compose of |
| 41 | // these two, so our inner filter has a scale-up, which disables the optimization of |
| 42 | // combining the two matrices. |
| 43 | cm.setScale(1.1f, 0.9f, 1); |
| 44 | SkAutoTUnref<SkColorFilter> b(SkColorMatrixFilter::Create(cm)); |
| 45 | return SkColorFilter::CreateComposeFilter(a, b); |
| 46 | } |
| 47 | |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 48 | static void draw_into_canvas(SkCanvas* canvas) { |
| 49 | const SkRect r = SkRect::MakeWH(100, 100); |
| 50 | SkShader* (*shaders[])() { make_opaque_color, make_alpha_color }; |
reed | f7cdb06 | 2016-02-04 11:35:27 -0800 | [diff] [blame^] | 51 | SkColorFilter* (*filters[])() { make_cf_null, make_cf0, make_cf1 }; |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 52 | |
| 53 | SkPaint paint; |
| 54 | for (auto shProc : shaders) { |
| 55 | paint.setShader(shProc())->unref(); |
| 56 | for (auto cfProc : filters) { |
| 57 | SkSafeUnref(paint.setColorFilter(cfProc())); |
| 58 | canvas->drawRect(r, paint); |
| 59 | canvas->translate(120, 0); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
reed | f7cdb06 | 2016-02-04 11:35:27 -0800 | [diff] [blame^] | 64 | DEF_SIMPLE_GM(color4f, canvas, 620, 260) { |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 65 | canvas->translate(20, 20); |
| 66 | |
| 67 | SkPaint bg; |
| 68 | // need the target to be opaque, so we can draw it to the screen |
| 69 | // even if it holds sRGB values. |
| 70 | bg.setColor(0xFFFFFFFF); |
| 71 | |
| 72 | SkColorProfileType const profiles[] { kLinear_SkColorProfileType, kSRGB_SkColorProfileType }; |
| 73 | for (auto profile : profiles) { |
reed | f7cdb06 | 2016-02-04 11:35:27 -0800 | [diff] [blame^] | 74 | const SkImageInfo info = SkImageInfo::Make(600, 100, kN32_SkColorType, kPremul_SkAlphaType, |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 75 | profile); |
| 76 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 77 | surface->getCanvas()->drawPaint(bg); |
| 78 | draw_into_canvas(surface->getCanvas()); |
| 79 | surface->draw(canvas, 0, 0, nullptr); |
| 80 | canvas->translate(0, 120); |
| 81 | } |
| 82 | } |