2016-02-26 19:57:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
#include "ToolUtils.h"
|
2016-02-26 19:57:23 +00:00
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "Resources.h"
|
2016-11-15 21:44:34 +00:00
|
|
|
#include "SkBlendModePriv.h"
|
2016-02-26 19:57:23 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
2016-09-13 15:24:56 +00:00
|
|
|
DEF_SIMPLE_GM(gamma, canvas, 850, 200) {
|
2016-02-26 19:57:23 +00:00
|
|
|
SkPaint p;
|
|
|
|
const SkScalar sz = 50.0f;
|
|
|
|
const int szInt = SkScalarTruncToInt(sz);
|
2016-08-30 14:08:07 +00:00
|
|
|
const SkScalar tx = sz + 15.0f;
|
2016-02-26 19:57:23 +00:00
|
|
|
const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz);
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode rpt = SkTileMode::kRepeat;
|
2017-02-07 18:56:11 +00:00
|
|
|
auto srgbColorSpace = SkColorSpace::MakeSRGB();
|
2016-02-26 19:57:23 +00:00
|
|
|
|
|
|
|
SkBitmap ditherBmp;
|
|
|
|
ditherBmp.allocN32Pixels(2, 2);
|
|
|
|
SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels());
|
|
|
|
pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF);
|
|
|
|
pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0);
|
|
|
|
|
|
|
|
SkBitmap linearGreyBmp;
|
2016-06-20 15:25:02 +00:00
|
|
|
SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, nullptr);
|
2016-02-26 19:57:23 +00:00
|
|
|
linearGreyBmp.allocPixels(linearGreyInfo);
|
|
|
|
linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F);
|
|
|
|
|
|
|
|
SkBitmap srgbGreyBmp;
|
|
|
|
SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType,
|
2016-06-20 15:25:02 +00:00
|
|
|
srgbColorSpace);
|
2016-02-26 19:57:23 +00:00
|
|
|
srgbGreyBmp.allocPixels(srgbGreyInfo);
|
2016-06-02 12:49:21 +00:00
|
|
|
// 0xBC = 255 * linear_to_srgb(0.5f)
|
2016-02-26 19:57:23 +00:00
|
|
|
srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC);
|
|
|
|
|
2016-06-02 12:49:21 +00:00
|
|
|
SkBitmap mipmapBmp;
|
2016-06-10 18:41:47 +00:00
|
|
|
SkImageInfo mipmapInfo = SkImageInfo::Make(2, 2, kN32_SkColorType, kOpaque_SkAlphaType,
|
2016-06-20 15:25:02 +00:00
|
|
|
srgbColorSpace);
|
2016-06-02 12:49:21 +00:00
|
|
|
mipmapBmp.allocPixels(mipmapInfo);
|
|
|
|
SkPMColor* mipmapPixels = reinterpret_cast<SkPMColor*>(mipmapBmp.getPixels());
|
2016-06-10 18:41:47 +00:00
|
|
|
unsigned s25 = 0x89; // 255 * linear_to_srgb(0.25f)
|
|
|
|
unsigned s75 = 0xE1; // 255 * linear_to_srgb(0.75f)
|
|
|
|
mipmapPixels[0] = mipmapPixels[3] = SkPackARGB32(0xFF, s25, s25, s25);
|
|
|
|
mipmapPixels[1] = mipmapPixels[2] = SkPackARGB32(0xFF, s75, s75, s75);
|
2016-06-02 12:49:21 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2018-12-22 03:22:31 +00:00
|
|
|
|
2016-02-26 19:57:23 +00:00
|
|
|
SkPaint textPaint;
|
2016-08-30 14:08:07 +00:00
|
|
|
textPaint.setAntiAlias(true);
|
2016-02-26 19:57:23 +00:00
|
|
|
textPaint.setColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
// Helpers:
|
|
|
|
auto advance = [&]() {
|
|
|
|
canvas->translate(tx, 0);
|
|
|
|
p.reset();
|
|
|
|
};
|
|
|
|
|
2018-12-22 03:22:31 +00:00
|
|
|
auto drawString = [&](const char str[], SkScalar x, SkScalar y) {
|
|
|
|
canvas->drawSimpleText(str, strlen(str), kUTF8_SkTextEncoding, x, y, font, textPaint);
|
|
|
|
};
|
|
|
|
|
2016-02-26 19:57:23 +00:00
|
|
|
auto nextRect = [&](const char* label, const char* label2) {
|
|
|
|
canvas->drawRect(r, p);
|
2018-12-22 03:22:31 +00:00
|
|
|
drawString(label, 0, sz + font.getSpacing());
|
2016-02-26 19:57:23 +00:00
|
|
|
if (label2) {
|
2018-12-22 03:22:31 +00:00
|
|
|
drawString(label2, 0, sz + 2 * font.getSpacing());
|
2016-02-26 19:57:23 +00:00
|
|
|
}
|
|
|
|
advance();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto nextBitmap = [&](const SkBitmap& bmp, const char* label) {
|
|
|
|
canvas->drawBitmap(bmp, 0, 0);
|
2018-12-22 03:22:31 +00:00
|
|
|
drawString(label, 0, sz + font.getSpacing());
|
2016-02-26 19:57:23 +00:00
|
|
|
advance();
|
|
|
|
};
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
auto nextXferRect = [&](SkColor srcColor, SkBlendMode mode, SkColor dstColor) {
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setColor(dstColor);
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
p.setColor(srcColor);
|
2016-10-06 00:33:02 +00:00
|
|
|
p.setBlendMode(mode);
|
2016-02-26 19:57:23 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
SkString srcText = SkStringPrintf("%08X", srcColor);
|
|
|
|
SkString dstText = SkStringPrintf("%08X", dstColor);
|
2018-12-22 03:22:31 +00:00
|
|
|
drawString(srcText.c_str(), 0, sz + font.getSpacing());
|
2016-11-15 21:44:34 +00:00
|
|
|
const char* modeName = SkBlendMode_Name(mode);
|
2018-12-22 03:22:31 +00:00
|
|
|
drawString(modeName, 0, sz + 2 * font.getSpacing());
|
|
|
|
drawString(dstText.c_str(), 0, sz + 3 * font.getSpacing());
|
2016-02-26 19:57:23 +00:00
|
|
|
advance();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Necessary for certain Xfermode tests to work (ie some of them output white @ 50% alpha):
|
|
|
|
canvas->clear(SK_ColorBLACK);
|
|
|
|
|
|
|
|
// *Everything* should be perceptually 50% grey. Only the first rectangle
|
|
|
|
// is guaranteed to draw that way, though.
|
|
|
|
canvas->save();
|
|
|
|
|
|
|
|
// Black/white dither, pixel perfect. This is ground truth.
|
2016-03-09 17:50:50 +00:00
|
|
|
p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt));
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality);
|
|
|
|
nextRect("Dither", "Reference");
|
|
|
|
|
|
|
|
// Black/white dither, sampled at half-texel offset. Tests bilerp.
|
|
|
|
// NOTE: We need to apply a non-identity scale and/or rotation to trick
|
|
|
|
// the raster pipeline into *not* snapping to nearest.
|
|
|
|
SkMatrix offsetMatrix = SkMatrix::Concat(
|
|
|
|
SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f));
|
2016-03-09 17:50:50 +00:00
|
|
|
p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt, &offsetMatrix));
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
|
|
|
|
nextRect("Dither", "Bilerp");
|
|
|
|
|
|
|
|
// Black/white dither, scaled down by 2x. Tests minification.
|
|
|
|
SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f);
|
2016-03-09 17:50:50 +00:00
|
|
|
p.setShader(SkShader::MakeBitmapShader(ditherBmp, rpt, rpt, &scaleMatrix));
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
|
|
|
|
nextRect("Dither", "Scale");
|
|
|
|
|
2016-06-02 12:49:21 +00:00
|
|
|
// 25%/75% dither, scaled down by 2x. Tests ALL aspects of minification. Specifically, are
|
|
|
|
// sRGB sources decoded to linear before computing mipmaps?
|
|
|
|
p.setShader(SkShader::MakeBitmapShader(mipmapBmp, rpt, rpt, &scaleMatrix));
|
|
|
|
p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
|
2017-08-28 14:34:05 +00:00
|
|
|
nextRect("MipMaps", nullptr);
|
2016-06-02 12:49:21 +00:00
|
|
|
|
2016-08-30 14:08:07 +00:00
|
|
|
// 50% grey via paint color. Paint color (SkColor) is specified to be sRGB!
|
|
|
|
p.setColor(0xffbcbcbc);
|
2017-08-28 14:34:05 +00:00
|
|
|
nextRect("Color", nullptr);
|
2016-02-26 19:57:23 +00:00
|
|
|
|
2016-09-13 15:24:56 +00:00
|
|
|
{
|
|
|
|
// Black -> White gradient, scaled to sample just the middle.
|
|
|
|
// Tests gradient interpolation.
|
|
|
|
SkPoint points[2] = {
|
|
|
|
SkPoint::Make(0 - (sz * 10), 0),
|
|
|
|
SkPoint::Make(sz + (sz * 10), 0)
|
|
|
|
};
|
|
|
|
SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE };
|
2019-04-03 14:27:45 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
|
2016-09-13 15:24:56 +00:00
|
|
|
nextRect("Gradient", "Interpolation");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Shallow gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
|
|
|
|
// Tests gamma-correction of gradient stops before interpolation in two-stop case
|
|
|
|
SkPoint points[2] = {
|
|
|
|
SkPoint::Make(0, 0),
|
|
|
|
SkPoint::Make(sz, 0)
|
|
|
|
};
|
|
|
|
SkColor colors[2] = { 0xffbbbbbb, 0xffbdbdbd };
|
2019-04-03 14:27:45 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp));
|
2016-09-13 15:24:56 +00:00
|
|
|
nextRect("Gradient", "Endpoints");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Shallow 3-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
|
|
|
|
// Tests gamma-correction of gradient stops before interpolation in three-stop case
|
|
|
|
SkPoint points[2] = {
|
|
|
|
SkPoint::Make(0, 0),
|
|
|
|
SkPoint::Make(sz, 0)
|
|
|
|
};
|
|
|
|
SkColor colors[3] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
|
2019-04-03 14:27:45 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 3, SkTileMode::kClamp));
|
2016-09-13 15:24:56 +00:00
|
|
|
nextRect("Gradient", "3-Stop");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Shallow N-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB.
|
|
|
|
// Tests gamma-correction of gradient stops before interpolation in texture implementation
|
|
|
|
SkPoint points[2] = {
|
|
|
|
SkPoint::Make(0, 0),
|
|
|
|
SkPoint::Make(sz, 0)
|
|
|
|
};
|
|
|
|
SkColor colors[5] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb };
|
2019-04-03 14:27:45 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 5, SkTileMode::kClamp));
|
2016-09-13 15:24:56 +00:00
|
|
|
nextRect("Gradient", "Texture");
|
|
|
|
}
|
2016-02-26 19:57:23 +00:00
|
|
|
|
|
|
|
// 50% grey from linear bitmap, with drawBitmap
|
|
|
|
nextBitmap(linearGreyBmp, "Lnr BMP");
|
|
|
|
|
|
|
|
// 50% grey from sRGB bitmap, with drawBitmap
|
|
|
|
nextBitmap(srgbGreyBmp, "sRGB BMP");
|
|
|
|
|
|
|
|
// Bitmap wrapped in a shader (linear):
|
2016-03-09 17:50:50 +00:00
|
|
|
p.setShader(SkShader::MakeBitmapShader(linearGreyBmp, rpt, rpt));
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
|
|
|
|
nextRect("Lnr BMP", "Shader");
|
|
|
|
|
|
|
|
// Bitmap wrapped in a shader (sRGB):
|
2016-03-09 17:50:50 +00:00
|
|
|
p.setShader(SkShader::MakeBitmapShader(srgbGreyBmp, rpt, rpt));
|
2016-02-26 19:57:23 +00:00
|
|
|
p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality);
|
|
|
|
nextRect("sRGB BMP", "Shader");
|
|
|
|
|
|
|
|
// Carriage return.
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, 2 * sz);
|
|
|
|
|
|
|
|
// Xfermode tests, all done off-screen so certain modes work...
|
|
|
|
|
|
|
|
canvas->saveLayer(nullptr, nullptr);
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
nextXferRect(0x7fffffff, SkBlendMode::kSrcOver, SK_ColorBLACK);
|
|
|
|
nextXferRect(0x7f000000, SkBlendMode::kSrcOver, SK_ColorWHITE);
|
2016-02-26 19:57:23 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
nextXferRect(SK_ColorBLACK, SkBlendMode::kDstOver, 0x7fffffff);
|
|
|
|
nextXferRect(SK_ColorWHITE, SkBlendMode::kSrcIn, 0x7fff00ff);
|
|
|
|
nextXferRect(0x7fff00ff, SkBlendMode::kDstIn, SK_ColorWHITE);
|
2016-02-26 19:57:23 +00:00
|
|
|
|
2016-08-30 14:08:07 +00:00
|
|
|
// 0x89 = 255 * linear_to_srgb(0.25)
|
2016-10-06 00:33:02 +00:00
|
|
|
nextXferRect(0xff898989, SkBlendMode::kPlus, 0xff898989);
|
2016-08-30 14:08:07 +00:00
|
|
|
|
|
|
|
// 0xDB = 255 * linear_to_srgb(sqrt(0.5))
|
2016-10-06 00:33:02 +00:00
|
|
|
nextXferRect(0xffdbdbdb, SkBlendMode::kModulate, 0xffdbdbdb);
|
2016-02-26 19:57:23 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|