blob: 97ad2d96c8660759de21e66a34353a0aca5ae911 [file] [log] [blame]
brianosmane5824b92016-02-26 11:57:23 -08001/*
2 * Copyright 2015 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"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkColorPriv.h"
14#include "include/core/SkColorSpace.h"
15#include "include/core/SkFilterQuality.h"
16#include "include/core/SkFont.h"
17#include "include/core/SkFontTypes.h"
18#include "include/core/SkImageInfo.h"
19#include "include/core/SkMatrix.h"
20#include "include/core/SkPaint.h"
21#include "include/core/SkPoint.h"
22#include "include/core/SkRect.h"
23#include "include/core/SkScalar.h"
24#include "include/core/SkShader.h"
25#include "include/core/SkString.h"
26#include "include/core/SkTileMode.h"
27#include "include/core/SkTypeface.h"
28#include "include/effects/SkGradientShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "tools/ToolUtils.h"
brianosmane5824b92016-02-26 11:57:23 -080030
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include <string.h>
brianosmane5824b92016-02-26 11:57:23 -080032
brianosman490452e2016-09-13 08:24:56 -070033DEF_SIMPLE_GM(gamma, canvas, 850, 200) {
brianosmane5824b92016-02-26 11:57:23 -080034 SkPaint p;
35 const SkScalar sz = 50.0f;
36 const int szInt = SkScalarTruncToInt(sz);
brianosman7a99a0a2016-08-30 07:08:07 -070037 const SkScalar tx = sz + 15.0f;
brianosmane5824b92016-02-26 11:57:23 -080038 const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz);
Mike Reedfae8fce2019-04-03 10:27:45 -040039 SkTileMode rpt = SkTileMode::kRepeat;
Matt Sarett77a7a1b2017-02-07 13:56:11 -050040 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmane5824b92016-02-26 11:57:23 -080041
42 SkBitmap ditherBmp;
43 ditherBmp.allocN32Pixels(2, 2);
44 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels());
45 pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF);
46 pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0);
47
48 SkBitmap linearGreyBmp;
brianosman52ede1d2016-06-20 08:25:02 -070049 SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, nullptr);
brianosmane5824b92016-02-26 11:57:23 -080050 linearGreyBmp.allocPixels(linearGreyInfo);
51 linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F);
52
53 SkBitmap srgbGreyBmp;
54 SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType,
brianosman52ede1d2016-06-20 08:25:02 -070055 srgbColorSpace);
brianosmane5824b92016-02-26 11:57:23 -080056 srgbGreyBmp.allocPixels(srgbGreyInfo);
brianosman33f6b3f2016-06-02 05:49:21 -070057 // 0xBC = 255 * linear_to_srgb(0.5f)
brianosmane5824b92016-02-26 11:57:23 -080058 srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC);
59
brianosman33f6b3f2016-06-02 05:49:21 -070060 SkBitmap mipmapBmp;
reed6644d932016-06-10 11:41:47 -070061 SkImageInfo mipmapInfo = SkImageInfo::Make(2, 2, kN32_SkColorType, kOpaque_SkAlphaType,
brianosman52ede1d2016-06-20 08:25:02 -070062 srgbColorSpace);
brianosman33f6b3f2016-06-02 05:49:21 -070063 mipmapBmp.allocPixels(mipmapInfo);
64 SkPMColor* mipmapPixels = reinterpret_cast<SkPMColor*>(mipmapBmp.getPixels());
reed6644d932016-06-10 11:41:47 -070065 unsigned s25 = 0x89; // 255 * linear_to_srgb(0.25f)
66 unsigned s75 = 0xE1; // 255 * linear_to_srgb(0.75f)
67 mipmapPixels[0] = mipmapPixels[3] = SkPackARGB32(0xFF, s25, s25, s25);
68 mipmapPixels[1] = mipmapPixels[2] = SkPackARGB32(0xFF, s75, s75, s75);
brianosman33f6b3f2016-06-02 05:49:21 -070069
Mike Kleinea3f0142019-03-20 11:12:10 -050070 SkFont font(ToolUtils::create_portable_typeface());
Mike Reed12a6d452018-12-21 22:22:31 -050071
brianosmane5824b92016-02-26 11:57:23 -080072 SkPaint textPaint;
brianosman7a99a0a2016-08-30 07:08:07 -070073 textPaint.setAntiAlias(true);
brianosmane5824b92016-02-26 11:57:23 -080074 textPaint.setColor(SK_ColorWHITE);
75
76 // Helpers:
77 auto advance = [&]() {
78 canvas->translate(tx, 0);
79 p.reset();
80 };
81
Mike Reed12a6d452018-12-21 22:22:31 -050082 auto drawString = [&](const char str[], SkScalar x, SkScalar y) {
Ben Wagner51e15a62019-05-07 15:38:46 -040083 canvas->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, textPaint);
Mike Reed12a6d452018-12-21 22:22:31 -050084 };
85
brianosmane5824b92016-02-26 11:57:23 -080086 auto nextRect = [&](const char* label, const char* label2) {
87 canvas->drawRect(r, p);
Mike Reed12a6d452018-12-21 22:22:31 -050088 drawString(label, 0, sz + font.getSpacing());
brianosmane5824b92016-02-26 11:57:23 -080089 if (label2) {
Mike Reed12a6d452018-12-21 22:22:31 -050090 drawString(label2, 0, sz + 2 * font.getSpacing());
brianosmane5824b92016-02-26 11:57:23 -080091 }
92 advance();
93 };
94
95 auto nextBitmap = [&](const SkBitmap& bmp, const char* label) {
96 canvas->drawBitmap(bmp, 0, 0);
Mike Reed12a6d452018-12-21 22:22:31 -050097 drawString(label, 0, sz + font.getSpacing());
brianosmane5824b92016-02-26 11:57:23 -080098 advance();
99 };
100
reed374772b2016-10-05 17:33:02 -0700101 auto nextXferRect = [&](SkColor srcColor, SkBlendMode mode, SkColor dstColor) {
brianosmane5824b92016-02-26 11:57:23 -0800102 p.setColor(dstColor);
103 canvas->drawRect(r, p);
104 p.setColor(srcColor);
reed374772b2016-10-05 17:33:02 -0700105 p.setBlendMode(mode);
brianosmane5824b92016-02-26 11:57:23 -0800106 canvas->drawRect(r, p);
107
108 SkString srcText = SkStringPrintf("%08X", srcColor);
109 SkString dstText = SkStringPrintf("%08X", dstColor);
Mike Reed12a6d452018-12-21 22:22:31 -0500110 drawString(srcText.c_str(), 0, sz + font.getSpacing());
Mike Reedd4706732016-11-15 16:44:34 -0500111 const char* modeName = SkBlendMode_Name(mode);
Mike Reed12a6d452018-12-21 22:22:31 -0500112 drawString(modeName, 0, sz + 2 * font.getSpacing());
113 drawString(dstText.c_str(), 0, sz + 3 * font.getSpacing());
brianosmane5824b92016-02-26 11:57:23 -0800114 advance();
115 };
116
117 // Necessary for certain Xfermode tests to work (ie some of them output white @ 50% alpha):
118 canvas->clear(SK_ColorBLACK);
119
120 // *Everything* should be perceptually 50% grey. Only the first rectangle
121 // is guaranteed to draw that way, though.
122 canvas->save();
123
124 // Black/white dither, pixel perfect. This is ground truth.
Mike Reed50acf8f2019-04-08 13:20:23 -0400125 p.setShader(ditherBmp.makeShader(rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -0800126 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
127 nextRect("Dither", "Reference");
128
129 // Black/white dither, sampled at half-texel offset. Tests bilerp.
130 // NOTE: We need to apply a non-identity scale and/or rotation to trick
131 // the raster pipeline into *not* snapping to nearest.
132 SkMatrix offsetMatrix = SkMatrix::Concat(
133 SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f));
Mike Reed50acf8f2019-04-08 13:20:23 -0400134 p.setShader(ditherBmp.makeShader(rpt, rpt, &offsetMatrix));
brianosmane5824b92016-02-26 11:57:23 -0800135 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
136 nextRect("Dither", "Bilerp");
137
138 // Black/white dither, scaled down by 2x. Tests minification.
139 SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f);
Mike Reed50acf8f2019-04-08 13:20:23 -0400140 p.setShader(ditherBmp.makeShader(rpt, rpt, &scaleMatrix));
brianosmane5824b92016-02-26 11:57:23 -0800141 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
142 nextRect("Dither", "Scale");
143
brianosman33f6b3f2016-06-02 05:49:21 -0700144 // 25%/75% dither, scaled down by 2x. Tests ALL aspects of minification. Specifically, are
145 // sRGB sources decoded to linear before computing mipmaps?
Mike Reed50acf8f2019-04-08 13:20:23 -0400146 p.setShader(mipmapBmp.makeShader(rpt, rpt, &scaleMatrix));
brianosman33f6b3f2016-06-02 05:49:21 -0700147 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400148 nextRect("MipMaps", nullptr);
brianosman33f6b3f2016-06-02 05:49:21 -0700149
brianosman7a99a0a2016-08-30 07:08:07 -0700150 // 50% grey via paint color. Paint color (SkColor) is specified to be sRGB!
151 p.setColor(0xffbcbcbc);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400152 nextRect("Color", nullptr);
brianosmane5824b92016-02-26 11:57:23 -0800153
brianosman490452e2016-09-13 08:24:56 -0700154 {
155 // Black -> White gradient, scaled to sample just the middle.
156 // Tests gradient interpolation.
157 SkPoint points[2] = {
158 SkPoint::Make(0 - (sz * 10), 0),
159 SkPoint::Make(sz + (sz * 10), 0)
160 };
161 SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
Mike Reedfae8fce2019-04-03 10:27:45 -0400162 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
brianosman490452e2016-09-13 08:24:56 -0700163 nextRect("Gradient", "Interpolation");
164 }
165
166 {
167 // Shallow gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
168 // Tests gamma-correction of gradient stops before interpolation in two-stop case
169 SkPoint points[2] = {
170 SkPoint::Make(0, 0),
171 SkPoint::Make(sz, 0)
172 };
173 SkColor colors[2] = { 0xffbbbbbb, 0xffbdbdbd };
Mike Reedfae8fce2019-04-03 10:27:45 -0400174 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
brianosman490452e2016-09-13 08:24:56 -0700175 nextRect("Gradient", "Endpoints");
176 }
177
178 {
179 // Shallow 3-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
180 // Tests gamma-correction of gradient stops before interpolation in three-stop case
181 SkPoint points[2] = {
182 SkPoint::Make(0, 0),
183 SkPoint::Make(sz, 0)
184 };
185 SkColor colors[3] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
Mike Reedfae8fce2019-04-03 10:27:45 -0400186 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 3, SkTileMode::kClamp));
brianosman490452e2016-09-13 08:24:56 -0700187 nextRect("Gradient", "3-Stop");
188 }
189
190 {
191 // Shallow N-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
192 // Tests gamma-correction of gradient stops before interpolation in texture implementation
193 SkPoint points[2] = {
194 SkPoint::Make(0, 0),
195 SkPoint::Make(sz, 0)
196 };
197 SkColor colors[5] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
Mike Reedfae8fce2019-04-03 10:27:45 -0400198 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 5, SkTileMode::kClamp));
brianosman490452e2016-09-13 08:24:56 -0700199 nextRect("Gradient", "Texture");
200 }
brianosmane5824b92016-02-26 11:57:23 -0800201
202 // 50% grey from linear bitmap, with drawBitmap
203 nextBitmap(linearGreyBmp, "Lnr BMP");
204
205 // 50% grey from sRGB bitmap, with drawBitmap
206 nextBitmap(srgbGreyBmp, "sRGB BMP");
207
208 // Bitmap wrapped in a shader (linear):
Mike Reed50acf8f2019-04-08 13:20:23 -0400209 p.setShader(linearGreyBmp.makeShader(rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -0800210 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
211 nextRect("Lnr BMP", "Shader");
212
213 // Bitmap wrapped in a shader (sRGB):
Mike Reed50acf8f2019-04-08 13:20:23 -0400214 p.setShader(srgbGreyBmp.makeShader(rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -0800215 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
216 nextRect("sRGB BMP", "Shader");
217
218 // Carriage return.
219 canvas->restore();
220 canvas->translate(0, 2 * sz);
221
brianosmane5824b92016-02-26 11:57:23 -0800222 // Xfermode tests, all done off-screen so certain modes work...
223
224 canvas->saveLayer(nullptr, nullptr);
225
reed374772b2016-10-05 17:33:02 -0700226 nextXferRect(0x7fffffff, SkBlendMode::kSrcOver, SK_ColorBLACK);
227 nextXferRect(0x7f000000, SkBlendMode::kSrcOver, SK_ColorWHITE);
brianosmane5824b92016-02-26 11:57:23 -0800228
reed374772b2016-10-05 17:33:02 -0700229 nextXferRect(SK_ColorBLACK, SkBlendMode::kDstOver, 0x7fffffff);
230 nextXferRect(SK_ColorWHITE, SkBlendMode::kSrcIn, 0x7fff00ff);
231 nextXferRect(0x7fff00ff, SkBlendMode::kDstIn, SK_ColorWHITE);
brianosmane5824b92016-02-26 11:57:23 -0800232
brianosman7a99a0a2016-08-30 07:08:07 -0700233 // 0x89 = 255 * linear_to_srgb(0.25)
reed374772b2016-10-05 17:33:02 -0700234 nextXferRect(0xff898989, SkBlendMode::kPlus, 0xff898989);
brianosman7a99a0a2016-08-30 07:08:07 -0700235
236 // 0xDB = 255 * linear_to_srgb(sqrt(0.5))
reed374772b2016-10-05 17:33:02 -0700237 nextXferRect(0xffdbdbdb, SkBlendMode::kModulate, 0xffdbdbdb);
brianosmane5824b92016-02-26 11:57:23 -0800238
239 canvas->restore();
brianosmane5824b92016-02-26 11:57:23 -0800240}