blob: 5f281e9cf7ce5a8bd17cd123fe8753b3dd964742 [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"
Brian Osmancc813ae2016-10-25 11:45:14 -040011#include "SkColorSpace_Base.h"
reed395eabe2016-01-30 18:52:31 -080012#include "SkShader.h"
13#include "SkSurface.h"
14
15#include "SkColorMatrixFilter.h"
16#include "SkGradientShader.h"
17
reed2ad1aa62016-03-09 09:50:50 -080018static sk_sp<SkShader> make_opaque_color() {
19 return SkShader::MakeColorShader(0xFFFF0000);
reed395eabe2016-01-30 18:52:31 -080020}
21
reed2ad1aa62016-03-09 09:50:50 -080022static sk_sp<SkShader> make_alpha_color() {
23 return SkShader::MakeColorShader(0x80FF0000);
reed395eabe2016-01-30 18:52:31 -080024}
25
reedd053ce92016-03-22 10:17:23 -070026static sk_sp<SkColorFilter> make_cf_null() {
reed395eabe2016-01-30 18:52:31 -080027 return nullptr;
28}
29
reedd053ce92016-03-22 10:17:23 -070030static sk_sp<SkColorFilter> make_cf0() {
reed395eabe2016-01-30 18:52:31 -080031 SkColorMatrix cm;
32 cm.setSaturation(0.75f);
reedd053ce92016-03-22 10:17:23 -070033 return SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat);
reed395eabe2016-01-30 18:52:31 -080034}
35
reedd053ce92016-03-22 10:17:23 -070036static sk_sp<SkColorFilter> make_cf1() {
reedf7cdb062016-02-04 11:35:27 -080037 SkColorMatrix cm;
38 cm.setSaturation(0.75f);
reedd053ce92016-03-22 10:17:23 -070039 auto a(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
reedf7cdb062016-02-04 11:35:27 -080040 // CreateComposedFilter will try to concat these two matrices, resulting in a single
41 // filter (which is good for speed). For this test, we want to force a real compose of
42 // these two, so our inner filter has a scale-up, which disables the optimization of
43 // combining the two matrices.
44 cm.setScale(1.1f, 0.9f, 1);
reedd053ce92016-03-22 10:17:23 -070045 auto b(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
46 return SkColorFilter::MakeComposeFilter(a, b);
reedf7cdb062016-02-04 11:35:27 -080047}
48
reedd053ce92016-03-22 10:17:23 -070049static sk_sp<SkColorFilter> make_cf2() {
Mike Reed7d954ad2016-10-28 15:42:34 -040050 return SkColorFilter::MakeModeFilter(0x8044CC88, SkBlendMode::kSrcATop);
reed31255652016-02-08 12:56:56 -080051}
52
reed395eabe2016-01-30 18:52:31 -080053static void draw_into_canvas(SkCanvas* canvas) {
reed31255652016-02-08 12:56:56 -080054 const SkRect r = SkRect::MakeWH(50, 100);
reed2ad1aa62016-03-09 09:50:50 -080055 sk_sp<SkShader> (*shaders[])() { make_opaque_color, make_alpha_color };
reedd053ce92016-03-22 10:17:23 -070056 sk_sp<SkColorFilter> (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
halcanary9d524f22016-03-29 09:03:52 -070057
reed395eabe2016-01-30 18:52:31 -080058 SkPaint paint;
59 for (auto shProc : shaders) {
reed2ad1aa62016-03-09 09:50:50 -080060 paint.setShader(shProc());
reed395eabe2016-01-30 18:52:31 -080061 for (auto cfProc : filters) {
reedd053ce92016-03-22 10:17:23 -070062 paint.setColorFilter(cfProc());
reed395eabe2016-01-30 18:52:31 -080063 canvas->drawRect(r, paint);
reed31255652016-02-08 12:56:56 -080064 canvas->translate(60, 0);
reed395eabe2016-01-30 18:52:31 -080065 }
66 }
67}
68
reed31255652016-02-08 12:56:56 -080069DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
70 canvas->translate(10, 10);
reed395eabe2016-01-30 18:52:31 -080071
72 SkPaint bg;
73 // need the target to be opaque, so we can draw it to the screen
74 // even if it holds sRGB values.
75 bg.setColor(0xFFFFFFFF);
76
brianosman52ede1d2016-06-20 08:25:02 -070077 sk_sp<SkColorSpace> colorSpaces[]{
78 nullptr,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050079 SkColorSpace::MakeSRGB()
brianosman52ede1d2016-06-20 08:25:02 -070080 };
81 for (auto colorSpace : colorSpaces) {
reed31255652016-02-08 12:56:56 -080082 const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
brianosman52ede1d2016-06-20 08:25:02 -070083 colorSpace);
reede8f30622016-03-23 18:59:25 -070084 auto surface(SkSurface::MakeRaster(info));
reed395eabe2016-01-30 18:52:31 -080085 surface->getCanvas()->drawPaint(bg);
86 draw_into_canvas(surface->getCanvas());
87 surface->draw(canvas, 0, 0, nullptr);
88 canvas->translate(0, 120);
89 }
90}
reed0ccc62d2016-05-04 13:09:39 -070091
92///////////////////////////////////////////////////////////////////////////////////////////////////
93#include "SkColorSpace.h"
94
Brian Osmancc813ae2016-10-25 11:45:14 -040095DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
reed0ccc62d2016-05-04 13:09:39 -070096 canvas->translate(10, 10);
97
Matt Sarett77a7a1b2017-02-07 13:56:11 -050098 auto srgb = SkColorSpace::MakeSRGB();
Brian Osman36703d92017-12-12 14:09:31 -050099 auto spin = srgb->makeColorSpin(); // RGB -> GBR
reed0ccc62d2016-05-04 13:09:39 -0700100
101 const SkColor4f colors[] {
reed0ccc62d2016-05-04 13:09:39 -0700102 { 1, 0, 0, 1 },
brianosmane074d1f2016-06-24 06:31:47 -0700103 { 0, 1, 0, 1 },
104 { 0, 0, 1, 1 },
105 { 0.5, 0.5, 0.5, 1 },
reed0ccc62d2016-05-04 13:09:39 -0700106 };
107
108 SkPaint paint;
109 SkRect r = SkRect::MakeWH(100, 100);
110
111 for (const auto& c4 : colors) {
112 sk_sp<SkShader> shaders[] {
113 SkShader::MakeColorShader(c4, nullptr),
Brian Osmancc813ae2016-10-25 11:45:14 -0400114 SkShader::MakeColorShader(c4, srgb),
Brian Osman0387d352017-11-14 11:13:54 -0500115 SkShader::MakeColorShader(c4, spin),
reed0ccc62d2016-05-04 13:09:39 -0700116 };
117
118 canvas->save();
119 for (const auto& s : shaders) {
120 paint.setShader(s);
121 canvas->drawRect(r, paint);
122 canvas->translate(r.width() * 6 / 5, 0);
123 }
124 canvas->restore();
125 canvas->translate(0, r.height() * 6 / 5);
126 }
127}