blob: 1699083471fb948f6a02612a656d09f3a43d64d3 [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
8#include "gm.h"
9
10#include "Resources.h"
11#include "SkGradientShader.h"
12
13DEF_SIMPLE_GM(gamma, canvas, 500, 200) {
14 SkPaint p;
15 const SkScalar sz = 50.0f;
16 const int szInt = SkScalarTruncToInt(sz);
17 const SkScalar tx = sz + 5.0f;
18 const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz);
19 SkShader::TileMode rpt = SkShader::kRepeat_TileMode;
20
21 SkBitmap ditherBmp;
22 ditherBmp.allocN32Pixels(2, 2);
23 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels());
24 pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF);
25 pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0);
26
27 SkBitmap linearGreyBmp;
28 SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType,
29 kLinear_SkColorProfileType);
30 linearGreyBmp.allocPixels(linearGreyInfo);
31 linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F);
32
33 SkBitmap srgbGreyBmp;
34 SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType,
35 kSRGB_SkColorProfileType);
36 srgbGreyBmp.allocPixels(srgbGreyInfo);
37 srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC);
38
39 SkPaint textPaint;
40 textPaint.setColor(SK_ColorWHITE);
41
42 // Helpers:
43 auto advance = [&]() {
44 canvas->translate(tx, 0);
45 p.reset();
46 };
47
48 auto nextRect = [&](const char* label, const char* label2) {
49 canvas->drawRect(r, p);
50 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing(), textPaint);
51 if (label2) {
52 canvas->drawText(label2, strlen(label2), 0, sz + 2 * textPaint.getFontSpacing(),
53 textPaint);
54 }
55 advance();
56 };
57
58 auto nextBitmap = [&](const SkBitmap& bmp, const char* label) {
59 canvas->drawBitmap(bmp, 0, 0);
60 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing(), textPaint);
61 advance();
62 };
63
64 auto nextXferRect = [&](SkColor srcColor, SkXfermode::Mode mode, SkColor dstColor) {
65 p.setColor(dstColor);
66 canvas->drawRect(r, p);
67 p.setColor(srcColor);
68 p.setXfermodeMode(mode);
69 canvas->drawRect(r, p);
70
71 SkString srcText = SkStringPrintf("%08X", srcColor);
72 SkString dstText = SkStringPrintf("%08X", dstColor);
73 canvas->drawText(srcText.c_str(), srcText.size(), 0, sz + textPaint.getFontSpacing(),
74 textPaint);
75 const char* modeName = SkXfermode::ModeName(mode);
76 canvas->drawText(modeName, strlen(modeName), 0, sz + 2 * textPaint.getFontSpacing(),
77 textPaint);
78 canvas->drawText(dstText.c_str(), dstText.size(), 0, sz + 3 * textPaint.getFontSpacing(),
79 textPaint);
80 advance();
81 };
82
83 // Necessary for certain Xfermode tests to work (ie some of them output white @ 50% alpha):
84 canvas->clear(SK_ColorBLACK);
85
86 // *Everything* should be perceptually 50% grey. Only the first rectangle
87 // is guaranteed to draw that way, though.
88 canvas->save();
89
90 // Black/white dither, pixel perfect. This is ground truth.
reed2ad1aa62016-03-09 09:50:50 -080091 p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -080092 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
93 nextRect("Dither", "Reference");
94
95 // Black/white dither, sampled at half-texel offset. Tests bilerp.
96 // NOTE: We need to apply a non-identity scale and/or rotation to trick
97 // the raster pipeline into *not* snapping to nearest.
98 SkMatrix offsetMatrix = SkMatrix::Concat(
99 SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f));
reed2ad1aa62016-03-09 09:50:50 -0800100 p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt, &offsetMatrix));
brianosmane5824b92016-02-26 11:57:23 -0800101 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
102 nextRect("Dither", "Bilerp");
103
104 // Black/white dither, scaled down by 2x. Tests minification.
105 SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f);
reed2ad1aa62016-03-09 09:50:50 -0800106 p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt, &scaleMatrix));
brianosmane5824b92016-02-26 11:57:23 -0800107 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
108 nextRect("Dither", "Scale");
109
110 // 50% grey via paint color.
111 p.setColor(0xff7f7f7f);
112 nextRect("Color", 0);
113
114 // Black -> White gradient, scaled to sample just the middle.
115 // Tests gradient interpolation.
116 SkPoint points[2] = {
117 SkPoint::Make(0 - (sz * 10), 0),
118 SkPoint::Make(sz + (sz * 10), 0)
119 };
120 SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
reed2ad1aa62016-03-09 09:50:50 -0800121 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2,
122 SkShader::kClamp_TileMode));
brianosmane5824b92016-02-26 11:57:23 -0800123 nextRect("Gradient", 0);
124
125 // 50% grey from linear bitmap, with drawBitmap
126 nextBitmap(linearGreyBmp, "Lnr BMP");
127
128 // 50% grey from sRGB bitmap, with drawBitmap
129 nextBitmap(srgbGreyBmp, "sRGB BMP");
130
131 // Bitmap wrapped in a shader (linear):
reed2ad1aa62016-03-09 09:50:50 -0800132 p.setShader(SkShader::MakeBitmapShader(linearGreyBmp, rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -0800133 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
134 nextRect("Lnr BMP", "Shader");
135
136 // Bitmap wrapped in a shader (sRGB):
reed2ad1aa62016-03-09 09:50:50 -0800137 p.setShader(SkShader::MakeBitmapShader(srgbGreyBmp, rpt, rpt));
brianosmane5824b92016-02-26 11:57:23 -0800138 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
139 nextRect("sRGB BMP", "Shader");
140
141 // Carriage return.
142 canvas->restore();
143 canvas->translate(0, 2 * sz);
144
145 const U8CPU sqrtHalf = 0xB4;
146 const SkColor sqrtHalfAlpha = SkColorSetARGB(sqrtHalf, 0, 0, 0);
147 const SkColor sqrtHalfWhite = SkColorSetARGB(0xFF, sqrtHalf, sqrtHalf, sqrtHalf);
148
149 // Xfermode tests, all done off-screen so certain modes work...
150
151 canvas->saveLayer(nullptr, nullptr);
152
153 nextXferRect(0x7fffffff, SkXfermode::kSrcOver_Mode, SK_ColorBLACK);
154 nextXferRect(0x7f000000, SkXfermode::kSrcOver_Mode, SK_ColorWHITE);
155
156 nextXferRect(SK_ColorBLACK, SkXfermode::kDstOver_Mode, 0x7fffffff);
157 nextXferRect(SK_ColorWHITE, SkXfermode::kSrcIn_Mode, 0x7fff00ff);
158 nextXferRect(0x7fff00ff, SkXfermode::kDstIn_Mode, SK_ColorWHITE);
159 nextXferRect(sqrtHalfWhite, SkXfermode::kSrcIn_Mode, sqrtHalfAlpha);
160 nextXferRect(sqrtHalfAlpha, SkXfermode::kDstIn_Mode, sqrtHalfWhite);
161
162 nextXferRect(0xff3f3f3f, SkXfermode::kPlus_Mode, 0xff3f3f3f);
163 nextXferRect(sqrtHalfWhite, SkXfermode::kModulate_Mode, sqrtHalfWhite);
164
165 canvas->restore();
166
167 canvas->restore();
168}