blob: 05809897394f5f43ed6c116869a0ff39a750b933 [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
17 return SkGradientShader::CreateLinear(pts, bw, NULL,
18 SK_ARRAY_COUNT(bw),
19 SkShader::kClamp_TileMode);
20}
21
22static bool setFont(SkPaint* paint, const char name[]) {
Cary Clark992c7b02014-07-31 08:58:44 -040023 SkTypeface* tf = sk_tool_utils::create_portable_typeface(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:
reed04f7e142014-11-13 12:46:24 -080042 SkString onShortName() SK_OVERRIDE {
reed@google.comed3ee642012-02-14 16:12:49 +000043 return SkString("gammatext");
44 }
45
reed04f7e142014-11-13 12:46:24 -080046 SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070047 return SkISize::Make(1024, HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000048 }
49
50 static void drawGrad(SkCanvas* canvas) {
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000051 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
reed@google.comed3ee642012-02-14 16:12:49 +000052 SkShader* s = make_heatGradient(pts);
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;
56 paint.setShader(s)->unref();
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
reed04f7e142014-11-13 12:46:24 -080061 void onDraw(SkCanvas* canvas) SK_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";
72 size_t len = strlen(text);
73
74 SkPaint paint;
75 setFont(&paint, "Times");
76 paint.setTextSize(SkIntToScalar(16));
77 paint.setAntiAlias(true);
78 paint.setLCDRenderText(true);
79
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000080 SkScalar x = SkIntToScalar(10);
reed@google.comed3ee642012-02-14 16:12:49 +000081 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
82 paint.setColor(fg[i]);
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
vandebo@chromium.orgc39c8672012-04-17 21:46:18 +000084 SkScalar y = SkIntToScalar(40);
85 SkScalar stopy = SkIntToScalar(HEIGHT);
reed@google.comed3ee642012-02-14 16:12:49 +000086 while (y < stopy) {
reed04f7e142014-11-13 12:46:24 -080087 canvas->drawText(text, len, x, y, paint);
reed@google.comed3ee642012-02-14 16:12:49 +000088 y += paint.getTextSize() * 2;
89 }
90 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
91 }
reed@google.comed3ee642012-02-14 16:12:49 +000092 }
93
94private:
reed8367b8c2014-08-22 08:30:20 -070095 typedef skiagm::GM INHERITED;
reed@google.comed3ee642012-02-14 16:12:49 +000096};
97
reed64c04882014-11-14 14:39:29 -080098// TEMP disable to see if nothing else flakes <reed>
99//DEF_GM( return new GammaTextGM; )
reed8367b8c2014-08-22 08:30:20 -0700100
reed@google.comed3ee642012-02-14 16:12:49 +0000101//////////////////////////////////////////////////////////////////////////////
102
reed8367b8c2014-08-22 08:30:20 -0700103static SkShader* make_gradient(SkColor c) {
104 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
105 SkColor colors[2];
106 colors[0] = c;
107 colors[1] = SkColorSetA(c, 0);
108 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
reed@google.comed3ee642012-02-14 16:12:49 +0000109}
reed8367b8c2014-08-22 08:30:20 -0700110
111static void set_face(SkPaint* paint) {
112 SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic);
113 SkSafeUnref(paint->setTypeface(face));
114}
115
116static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) {
117 const char text[] = "Now is the time for all good";
118 const size_t len = strlen(text);
119
120 paint->setShader(NULL);
121 canvas->drawText(text, len, 10, 20, *paint);
122 paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref();
123 canvas->drawText(text, len, 10, 40, *paint);
124 paint->setShader(shader);
125 canvas->drawText(text, len, 10, 60, *paint);
126}
127
128class GammaShaderTextGM : public skiagm::GM {
129 SkShader* fShaders[3];
130 SkColor fColors[3];
131
132public:
133 GammaShaderTextGM() {
134 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
135 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
136 fShaders[i] = NULL;
137 fColors[i] = colors[i];
138 }
139 }
140
mtklein72c9faa2015-01-09 10:06:39 -0800141 ~GammaShaderTextGM() SK_OVERRIDE {
reed8367b8c2014-08-22 08:30:20 -0700142 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
143 SkSafeUnref(fShaders[i]);
144 }
145 }
146
147protected:
reed04f7e142014-11-13 12:46:24 -0800148 SkString onShortName() SK_OVERRIDE {
reed8367b8c2014-08-22 08:30:20 -0700149 return SkString("gammagradienttext");
150 }
151
reed04f7e142014-11-13 12:46:24 -0800152 SkISize onISize() SK_OVERRIDE {
reed8367b8c2014-08-22 08:30:20 -0700153 return SkISize::Make(300, 300);
154 }
155
reed04f7e142014-11-13 12:46:24 -0800156 void onOnceBeforeDraw() SK_OVERRIDE {
reed8367b8c2014-08-22 08:30:20 -0700157 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
158 fShaders[i] = make_gradient(fColors[i]);
159 }
160 }
161
reed04f7e142014-11-13 12:46:24 -0800162 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed8367b8c2014-08-22 08:30:20 -0700163 SkPaint paint;
164 paint.setAntiAlias(true);
165 paint.setLCDRenderText(true);
166 paint.setTextSize(18);
167 set_face(&paint);
168
169 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
170 paint.setColor(fColors[i]);
171 draw_pair(canvas, &paint, fShaders[i]);
172 canvas->translate(0, 80);
173 }
174 }
175
176private:
177 typedef skiagm::GM INHERITED;
178};
179
180DEF_GM( return new GammaShaderTextGM; )
181