blob: e248f58db0a908a345d55698b369e3776ddecdab [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.comed3ee642012-02-14 16:12:49 +000010#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkGradientShader.h"
13#include "SkTypeface.h"
14
reed2ad1aa62016-03-09 09:50:50 -080015static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
reed@google.comed3ee642012-02-14 16:12:49 +000016 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
17
reed2ad1aa62016-03-09 09:50:50 -080018 return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +000020}
21
reed@google.comed3ee642012-02-14 16:12:49 +000022/**
23 Test a set of clipping problems discovered while writing blitAntiRect,
24 and test all the code paths through the clipping blitters.
25 Each region should show as a blue center surrounded by a 2px green
26 border, with no red.
27*/
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
reed@google.comed3ee642012-02-14 16:12:49 +000029#define HEIGHT 480
30
reed8367b8c2014-08-22 08:30:20 -070031class GammaTextGM : public skiagm::GM {
reed@google.comed3ee642012-02-14 16:12:49 +000032protected:
mtklein36352bf2015-03-25 18:17:31 -070033 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070034 SkString name("gammatext");
Mike Kleinb251b722017-11-13 11:03:16 -050035 name.append(sk_tool_utils::platform_font_manager());
caryclark37213552015-07-24 11:08:01 -070036 return name;
reed@google.comed3ee642012-02-14 16:12:49 +000037 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070040 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000041 }
42
43 static void drawGrad(SkCanvas* canvas) {
reed2ad1aa62016-03-09 09:50:50 -080044 const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
rmistry@google.comd6176b02012-08-23 18:14:13 +000045
reed@google.comed3ee642012-02-14 16:12:49 +000046 canvas->clear(SK_ColorRED);
47 SkPaint paint;
reed2ad1aa62016-03-09 09:50:50 -080048 paint.setShader(make_heatGradient(pts));
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000049 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
reed@google.comed3ee642012-02-14 16:12:49 +000050 canvas->drawRect(r, paint);
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDraw(SkCanvas* canvas) override {
reed@google.comed3ee642012-02-14 16:12:49 +000054 drawGrad(canvas);
55
56 const SkColor fg[] = {
57 0xFFFFFFFF,
58 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
59 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
60 0xFF000000,
61 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@google.comed3ee642012-02-14 16:12:49 +000063 const char* text = "Hamburgefons";
64 size_t len = strlen(text);
65
66 SkPaint paint;
reed@google.comed3ee642012-02-14 16:12:49 +000067 paint.setTextSize(SkIntToScalar(16));
68 paint.setAntiAlias(true);
69 paint.setLCDRenderText(true);
70
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000071 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000072 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
73 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000075 SkScalar y = SkIntToScalar(40);
76 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000077 while (y < stopy) {
reed04f7e142014-11-13 12:46:24 -080078 canvas->drawText(text, len, x, y, paint);
reed@google.comed3ee642012-02-14 16:12:49 +000079 y += paint.getTextSize() * 2;
80 }
81 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
82 }
reed@google.comed3ee642012-02-14 16:12:49 +000083 }
84
85private:
reed8367b8c2014-08-22 08:30:20 -070086 typedef skiagm::GM INHERITED;
reed@google.comed3ee642012-02-14 16:12:49 +000087};
88
bungeman67b21a72015-02-19 07:45:14 -080089DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -070090
reed@google.comed3ee642012-02-14 16:12:49 +000091//////////////////////////////////////////////////////////////////////////////
92
reed2ad1aa62016-03-09 09:50:50 -080093static sk_sp<SkShader> make_gradient(SkColor c) {
reed8367b8c2014-08-22 08:30:20 -070094 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
95 SkColor colors[2];
96 colors[0] = c;
97 colors[1] = SkColorSetA(c, 0);
reed2ad1aa62016-03-09 09:50:50 -080098 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +000099}
reed8367b8c2014-08-22 08:30:20 -0700100
101static void set_face(SkPaint* paint) {
Ben Wagner71319502017-07-27 10:45:29 -0400102 paint->setTypeface(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()));
reed8367b8c2014-08-22 08:30:20 -0700103}
104
reed2ad1aa62016-03-09 09:50:50 -0800105static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
reed8367b8c2014-08-22 08:30:20 -0700106 const char text[] = "Now is the time for all good";
107 const size_t len = strlen(text);
halcanary9d524f22016-03-29 09:03:52 -0700108
halcanary96fcdcc2015-08-27 07:41:13 -0700109 paint->setShader(nullptr);
reed8367b8c2014-08-22 08:30:20 -0700110 canvas->drawText(text, len, 10, 20, *paint);
reed2ad1aa62016-03-09 09:50:50 -0800111 paint->setShader(SkShader::MakeColorShader(paint->getColor()));
reed8367b8c2014-08-22 08:30:20 -0700112 canvas->drawText(text, len, 10, 40, *paint);
113 paint->setShader(shader);
114 canvas->drawText(text, len, 10, 60, *paint);
115}
116
117class GammaShaderTextGM : public skiagm::GM {
reed2ad1aa62016-03-09 09:50:50 -0800118 sk_sp<SkShader> fShaders[3];
reed8367b8c2014-08-22 08:30:20 -0700119 SkColor fColors[3];
120
121public:
122 GammaShaderTextGM() {
123 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
124 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
reed8367b8c2014-08-22 08:30:20 -0700125 fColors[i] = colors[i];
126 }
127 }
128
reed8367b8c2014-08-22 08:30:20 -0700129protected:
mtklein36352bf2015-03-25 18:17:31 -0700130 SkString onShortName() override {
reed8367b8c2014-08-22 08:30:20 -0700131 return SkString("gammagradienttext");
132 }
halcanary9d524f22016-03-29 09:03:52 -0700133
mtklein36352bf2015-03-25 18:17:31 -0700134 SkISize onISize() override {
reed8367b8c2014-08-22 08:30:20 -0700135 return SkISize::Make(300, 300);
136 }
137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onOnceBeforeDraw() override {
reed8367b8c2014-08-22 08:30:20 -0700139 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
140 fShaders[i] = make_gradient(fColors[i]);
141 }
142 }
143
mtklein36352bf2015-03-25 18:17:31 -0700144 void onDraw(SkCanvas* canvas) override {
reed8367b8c2014-08-22 08:30:20 -0700145 SkPaint paint;
146 paint.setAntiAlias(true);
147 paint.setLCDRenderText(true);
148 paint.setTextSize(18);
149 set_face(&paint);
150
151 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
152 paint.setColor(fColors[i]);
153 draw_pair(canvas, &paint, fShaders[i]);
154 canvas->translate(0, 80);
155 }
156 }
halcanary9d524f22016-03-29 09:03:52 -0700157
reed8367b8c2014-08-22 08:30:20 -0700158private:
159 typedef skiagm::GM INHERITED;
160};
161
162DEF_GM( return new GammaShaderTextGM; )