blob: e912654bc911fd1a95f40741b41e0b38bd129c4d [file] [log] [blame]
reed395eabe2016-01-30 18:52:31 -08001/*
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
17static SkShader* make_opaque_color() {
18 return SkShader::CreateColorShader(0xFFFF0000);
19}
20
21static SkShader* make_alpha_color() {
22 return SkShader::CreateColorShader(0x80FF0000);
23}
24
25static SkColorFilter* make_cf_null() {
26 return nullptr;
27}
28
29static SkColorFilter* make_cf0() {
30 SkColorMatrix cm;
31 cm.setSaturation(0.75f);
32 return SkColorMatrixFilter::Create(cm);
33}
34
reedf7cdb062016-02-04 11:35:27 -080035static 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
reed395eabe2016-01-30 18:52:31 -080048static void draw_into_canvas(SkCanvas* canvas) {
49 const SkRect r = SkRect::MakeWH(100, 100);
50 SkShader* (*shaders[])() { make_opaque_color, make_alpha_color };
reedf7cdb062016-02-04 11:35:27 -080051 SkColorFilter* (*filters[])() { make_cf_null, make_cf0, make_cf1 };
reed395eabe2016-01-30 18:52:31 -080052
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
reedf7cdb062016-02-04 11:35:27 -080064DEF_SIMPLE_GM(color4f, canvas, 620, 260) {
reed395eabe2016-01-30 18:52:31 -080065 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) {
reedf7cdb062016-02-04 11:35:27 -080074 const SkImageInfo info = SkImageInfo::Make(600, 100, kN32_SkColorType, kPremul_SkAlphaType,
reed395eabe2016-01-30 18:52:31 -080075 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}