blob: 63acb40c134336ed493075661ced90b985a27e80 [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"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkGradientShader.h"
12#include "SkTypeface.h"
13
14static SkShader* make_heatGradient(const SkPoint pts[2]) {
reed@google.comed3ee642012-02-14 16:12:49 +000015 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
16
halcanary96fcdcc2015-08-27 07:41:13 -070017 return SkGradientShader::CreateLinear(pts, bw, nullptr,
reed@google.comed3ee642012-02-14 16:12:49 +000018 SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
20}
21
22static bool setFont(SkPaint* paint, const char name[]) {
caryclark1818acb2015-07-24 12:09:25 -070023 SkTypeface* tf = SkTypeface::CreateFromName(name, SkTypeface::kNormal);
reed@google.comed3ee642012-02-14 16:12:49 +000024 if (tf) {
25 paint->setTypeface(tf)->unref();
26 return true;
27 }
reed@google.comce151d02012-03-08 16:10:57 +000028 return false;
reed@google.comed3ee642012-02-14 16:12:49 +000029}
30
reed@google.comed3ee642012-02-14 16:12:49 +000031/**
32 Test a set of clipping problems discovered while writing blitAntiRect,
33 and test all the code paths through the clipping blitters.
34 Each region should show as a blue center surrounded by a 2px green
35 border, with no red.
36*/
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@google.comed3ee642012-02-14 16:12:49 +000038#define HEIGHT 480
39
reed8367b8c2014-08-22 08:30:20 -070040class GammaTextGM : public skiagm::GM {
reed@google.comed3ee642012-02-14 16:12:49 +000041protected:
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
caryclark37213552015-07-24 11:08:01 -070043 SkString name("gammatext");
44 name.append(sk_tool_utils::major_platform_os_name());
45 return name;
reed@google.comed3ee642012-02-14 16:12:49 +000046 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070049 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000050 }
51
52 static void drawGrad(SkCanvas* canvas) {
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000053 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
reed@google.comed3ee642012-02-14 16:12:49 +000054 SkShader* s = make_heatGradient(pts);
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
reed@google.comed3ee642012-02-14 16:12:49 +000056 canvas->clear(SK_ColorRED);
57 SkPaint paint;
58 paint.setShader(s)->unref();
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000059 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
reed@google.comed3ee642012-02-14 16:12:49 +000060 canvas->drawRect(r, paint);
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 void onDraw(SkCanvas* canvas) override {
reed@google.comed3ee642012-02-14 16:12:49 +000064 drawGrad(canvas);
65
66 const SkColor fg[] = {
67 0xFFFFFFFF,
68 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
69 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
70 0xFF000000,
71 };
rmistry@google.comd6176b02012-08-23 18:14:13 +000072
reed@google.comed3ee642012-02-14 16:12:49 +000073 const char* text = "Hamburgefons";
74 size_t len = strlen(text);
75
76 SkPaint paint;
caryclark37213552015-07-24 11:08:01 -070077 setFont(&paint, sk_tool_utils::platform_font_name("serif"));
reed@google.comed3ee642012-02-14 16:12:49 +000078 paint.setTextSize(SkIntToScalar(16));
79 paint.setAntiAlias(true);
80 paint.setLCDRenderText(true);
81
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000082 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000083 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
84 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000085
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000086 SkScalar y = SkIntToScalar(40);
87 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000088 while (y < stopy) {
reed04f7e142014-11-13 12:46:24 -080089 canvas->drawText(text, len, x, y, paint);
reed@google.comed3ee642012-02-14 16:12:49 +000090 y += paint.getTextSize() * 2;
91 }
92 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
93 }
reed@google.comed3ee642012-02-14 16:12:49 +000094 }
95
96private:
reed8367b8c2014-08-22 08:30:20 -070097 typedef skiagm::GM INHERITED;
reed@google.comed3ee642012-02-14 16:12:49 +000098};
99
bungeman67b21a72015-02-19 07:45:14 -0800100DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -0700101
reed@google.comed3ee642012-02-14 16:12:49 +0000102//////////////////////////////////////////////////////////////////////////////
103
reed8367b8c2014-08-22 08:30:20 -0700104static SkShader* make_gradient(SkColor c) {
105 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
106 SkColor colors[2];
107 colors[0] = c;
108 colors[1] = SkColorSetA(c, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +0000110}
reed8367b8c2014-08-22 08:30:20 -0700111
112static void set_face(SkPaint* paint) {
113 SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic);
114 SkSafeUnref(paint->setTypeface(face));
115}
116
117static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) {
118 const char text[] = "Now is the time for all good";
119 const size_t len = strlen(text);
120
halcanary96fcdcc2015-08-27 07:41:13 -0700121 paint->setShader(nullptr);
reed8367b8c2014-08-22 08:30:20 -0700122 canvas->drawText(text, len, 10, 20, *paint);
123 paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref();
124 canvas->drawText(text, len, 10, 40, *paint);
125 paint->setShader(shader);
126 canvas->drawText(text, len, 10, 60, *paint);
127}
128
129class GammaShaderTextGM : public skiagm::GM {
130 SkShader* fShaders[3];
131 SkColor fColors[3];
132
133public:
134 GammaShaderTextGM() {
135 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
136 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700137 fShaders[i] = nullptr;
reed8367b8c2014-08-22 08:30:20 -0700138 fColors[i] = colors[i];
139 }
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 ~GammaShaderTextGM() override {
reed8367b8c2014-08-22 08:30:20 -0700143 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
144 SkSafeUnref(fShaders[i]);
145 }
146 }
147
148protected:
mtklein36352bf2015-03-25 18:17:31 -0700149 SkString onShortName() override {
reed8367b8c2014-08-22 08:30:20 -0700150 return SkString("gammagradienttext");
151 }
152
mtklein36352bf2015-03-25 18:17:31 -0700153 SkISize onISize() override {
reed8367b8c2014-08-22 08:30:20 -0700154 return SkISize::Make(300, 300);
155 }
156
mtklein36352bf2015-03-25 18:17:31 -0700157 void onOnceBeforeDraw() override {
reed8367b8c2014-08-22 08:30:20 -0700158 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
159 fShaders[i] = make_gradient(fColors[i]);
160 }
161 }
162
mtklein36352bf2015-03-25 18:17:31 -0700163 void onDraw(SkCanvas* canvas) override {
reed8367b8c2014-08-22 08:30:20 -0700164 SkPaint paint;
165 paint.setAntiAlias(true);
166 paint.setLCDRenderText(true);
167 paint.setTextSize(18);
168 set_face(&paint);
169
170 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
171 paint.setColor(fColors[i]);
172 draw_pair(canvas, &paint, fShaders[i]);
173 canvas->translate(0, 80);
174 }
175 }
176
177private:
178 typedef skiagm::GM INHERITED;
179};
180
181DEF_GM( return new GammaShaderTextGM; )
182