• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
make_opaque_color()17 static SkShader* make_opaque_color() {
18     return SkShader::CreateColorShader(0xFFFF0000);
19 }
20 
make_alpha_color()21 static SkShader* make_alpha_color() {
22     return SkShader::CreateColorShader(0x80FF0000);
23 }
24 
make_cf_null()25 static SkColorFilter* make_cf_null() {
26     return nullptr;
27 }
28 
make_cf0()29 static SkColorFilter* make_cf0() {
30     SkColorMatrix cm;
31     cm.setSaturation(0.75f);
32     return SkColorMatrixFilter::Create(cm);
33 }
34 
make_cf1()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 
make_cf2()48 static SkColorFilter* make_cf2() {
49     return SkColorFilter::CreateModeFilter(0x8044CC88, SkXfermode::kSrcATop_Mode);
50 }
51 
draw_into_canvas(SkCanvas * canvas)52 static void draw_into_canvas(SkCanvas* canvas) {
53     const SkRect r = SkRect::MakeWH(50, 100);
54     SkShader* (*shaders[])() { make_opaque_color, make_alpha_color };
55     SkColorFilter* (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
56 
57     SkPaint paint;
58     for (auto shProc : shaders) {
59         paint.setShader(shProc())->unref();
60         for (auto cfProc : filters) {
61             SkSafeUnref(paint.setColorFilter(cfProc()));
62             canvas->drawRect(r, paint);
63             canvas->translate(60, 0);
64         }
65     }
66 }
67 
68 DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
69     canvas->translate(10, 10);
70 
71     SkPaint bg;
72     // need the target to be opaque, so we can draw it to the screen
73     // even if it holds sRGB values.
74     bg.setColor(0xFFFFFFFF);
75 
76     SkColorProfileType const profiles[] { kLinear_SkColorProfileType, kSRGB_SkColorProfileType };
77     for (auto profile : profiles) {
78         const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
79                                                    profile);
80         SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
81         surface->getCanvas()->drawPaint(bg);
82         draw_into_canvas(surface->getCanvas());
83         surface->draw(canvas, 0, 0, nullptr);
84         canvas->translate(0, 120);
85     }
86 }
87