blob: 494949474e0f3977c7567e4ad9853315d0bb9008 [file] [log] [blame]
reed@google.comed3ee642012-02-14 16:12:49 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkFont.h"
12#include "include/core/SkFontStyle.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkShader.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/effects/SkGradientShader.h"
reed@google.comed3ee642012-02-14 16:12:49 +000025
reed2ad1aa62016-03-09 09:50:50 -080026static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
reed@google.comed3ee642012-02-14 16:12:49 +000027 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
28
Mike Reedfae8fce2019-04-03 10:27:45 -040029 return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw), SkTileMode::kClamp);
reed@google.comed3ee642012-02-14 16:12:49 +000030}
31
reed@google.comed3ee642012-02-14 16:12:49 +000032/**
33 Test a set of clipping problems discovered while writing blitAntiRect,
34 and test all the code paths through the clipping blitters.
35 Each region should show as a blue center surrounded by a 2px green
36 border, with no red.
37*/
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
reed@google.comed3ee642012-02-14 16:12:49 +000039#define HEIGHT 480
40
reed8367b8c2014-08-22 08:30:20 -070041class GammaTextGM : public skiagm::GM {
reed@google.comed3ee642012-02-14 16:12:49 +000042protected:
mtklein36352bf2015-03-25 18:17:31 -070043 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060044 return SkString("gammatext");
reed@google.comed3ee642012-02-14 16:12:49 +000045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070048 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000049 }
50
51 static void drawGrad(SkCanvas* canvas) {
reed2ad1aa62016-03-09 09:50:50 -080052 const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@google.comed3ee642012-02-14 16:12:49 +000054 canvas->clear(SK_ColorRED);
55 SkPaint paint;
reed2ad1aa62016-03-09 09:50:50 -080056 paint.setShader(make_heatGradient(pts));
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000057 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
reed@google.comed3ee642012-02-14 16:12:49 +000058 canvas->drawRect(r, paint);
59 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
reed@google.comed3ee642012-02-14 16:12:49 +000062 drawGrad(canvas);
63
64 const SkColor fg[] = {
65 0xFFFFFFFF,
66 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
67 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
68 0xFF000000,
69 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000070
reed@google.comed3ee642012-02-14 16:12:49 +000071 const char* text = "Hamburgefons";
reed@google.comed3ee642012-02-14 16:12:49 +000072
73 SkPaint paint;
Hal Canary6ac0df82019-01-07 16:01:22 -050074 SkFont font(nullptr, 16);
75 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
reed@google.comed3ee642012-02-14 16:12:49 +000076
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000077 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000078 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
79 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000081 SkScalar y = SkIntToScalar(40);
82 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000083 while (y < stopy) {
Hal Canary6ac0df82019-01-07 16:01:22 -050084 canvas->drawString(text, x, y, font, paint);
85 y += font.getSize() * 2;
reed@google.comed3ee642012-02-14 16:12:49 +000086 }
87 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
88 }
reed@google.comed3ee642012-02-14 16:12:49 +000089 }
90
91private:
John Stiles7571f9e2020-09-02 22:42:33 -040092 using INHERITED = skiagm::GM;
reed@google.comed3ee642012-02-14 16:12:49 +000093};
94
bungeman67b21a72015-02-19 07:45:14 -080095DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -070096
reed@google.comed3ee642012-02-14 16:12:49 +000097//////////////////////////////////////////////////////////////////////////////
98
reed2ad1aa62016-03-09 09:50:50 -080099static sk_sp<SkShader> make_gradient(SkColor c) {
reed8367b8c2014-08-22 08:30:20 -0700100 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
101 SkColor colors[2];
102 colors[0] = c;
103 colors[1] = SkColorSetA(c, 0);
Mike Reedfae8fce2019-04-03 10:27:45 -0400104 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
reed@google.comed3ee642012-02-14 16:12:49 +0000105}
reed8367b8c2014-08-22 08:30:20 -0700106
Hal Canary6ac0df82019-01-07 16:01:22 -0500107static void draw_pair(SkCanvas* canvas, const SkFont& font, SkColor color,
108 const sk_sp<SkShader>& shader) {
109 static const char text[] = "Now is the time for all good";
110 SkPaint paint;
111 paint.setColor(color);
112 canvas->drawString(text, 10, 20, font, paint);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400113 paint.setShader(SkShaders::Color(paint.getColor()));
Hal Canary6ac0df82019-01-07 16:01:22 -0500114 canvas->drawString(text, 10, 40, font, paint);
115 paint.setShader(shader);
116 canvas->drawString(text, 10, 60, font, paint);
reed8367b8c2014-08-22 08:30:20 -0700117}
118
119class GammaShaderTextGM : public skiagm::GM {
reed2ad1aa62016-03-09 09:50:50 -0800120 sk_sp<SkShader> fShaders[3];
reed8367b8c2014-08-22 08:30:20 -0700121 SkColor fColors[3];
122
123public:
124 GammaShaderTextGM() {
125 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
126 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
reed8367b8c2014-08-22 08:30:20 -0700127 fColors[i] = colors[i];
128 }
129 }
130
reed8367b8c2014-08-22 08:30:20 -0700131protected:
mtklein36352bf2015-03-25 18:17:31 -0700132 SkString onShortName() override {
reed8367b8c2014-08-22 08:30:20 -0700133 return SkString("gammagradienttext");
134 }
halcanary9d524f22016-03-29 09:03:52 -0700135
mtklein36352bf2015-03-25 18:17:31 -0700136 SkISize onISize() override {
reed8367b8c2014-08-22 08:30:20 -0700137 return SkISize::Make(300, 300);
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 void onOnceBeforeDraw() override {
reed8367b8c2014-08-22 08:30:20 -0700141 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
142 fShaders[i] = make_gradient(fColors[i]);
143 }
144 }
145
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDraw(SkCanvas* canvas) override {
reed8367b8c2014-08-22 08:30:20 -0700147 SkPaint paint;
Hal Canary58807912019-01-08 14:07:50 -0500148 paint.setAntiAlias(true);
Hal Canary6ac0df82019-01-07 16:01:22 -0500149 SkFont font(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()), 18);
150 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
reed8367b8c2014-08-22 08:30:20 -0700151
152 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
Hal Canary6ac0df82019-01-07 16:01:22 -0500153 draw_pair(canvas, font, fColors[i], fShaders[i]);
reed8367b8c2014-08-22 08:30:20 -0700154 canvas->translate(0, 80);
155 }
156 }
halcanary9d524f22016-03-29 09:03:52 -0700157
reed8367b8c2014-08-22 08:30:20 -0700158private:
John Stiles7571f9e2020-09-02 22:42:33 -0400159 using INHERITED = skiagm::GM;
reed8367b8c2014-08-22 08:30:20 -0700160};
161
162DEF_GM( return new GammaShaderTextGM; )