brianosman | 2c4b64e | 2016-09-07 07:04:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "SkSurface.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkPM4fPriv.h" |
| 13 | |
| 14 | static const int gRectSize = 50; |
| 15 | static const SkScalar gScalarSize = SkIntToScalar(gRectSize); |
| 16 | static const int gTestWidth = 700; |
| 17 | static const int gTestHeight = 300; |
| 18 | |
| 19 | struct CellRenderer { |
| 20 | virtual void draw(SkCanvas* canvas) = 0; |
| 21 | virtual const char* label() = 0; |
| 22 | virtual ~CellRenderer() {} |
| 23 | }; |
| 24 | |
| 25 | struct PaintColorCellRenderer : public CellRenderer { |
| 26 | PaintColorCellRenderer(SkColor color) : fColor(color) {} |
| 27 | void draw(SkCanvas* canvas) override { |
| 28 | canvas->drawColor(fColor); |
| 29 | } |
| 30 | const char* label() override { |
| 31 | return "Paint Color"; |
| 32 | } |
| 33 | protected: |
| 34 | SkColor fColor; |
| 35 | }; |
| 36 | |
| 37 | struct BitmapCellRenderer : public CellRenderer { |
| 38 | BitmapCellRenderer(SkColor color, SkFilterQuality quality, float scale = 1.0f) |
| 39 | : fQuality(quality) { |
| 40 | int scaledSize = SkFloatToIntRound(scale * gRectSize); |
| 41 | fBitmap.allocPixels(SkImageInfo::MakeS32(scaledSize, scaledSize, kPremul_SkAlphaType)); |
| 42 | fBitmap.eraseColor(color); |
| 43 | const char* qualityNames[] = { "None", "Low", "Medium", "High" }; |
| 44 | fLabel = SkStringPrintf("Bitmap (%s)", qualityNames[quality]); |
| 45 | } |
| 46 | void draw(SkCanvas* canvas) override { |
| 47 | SkPaint paint; |
| 48 | paint.setFilterQuality(fQuality); |
| 49 | canvas->drawBitmapRect(fBitmap, SkRect::MakeIWH(gRectSize, gRectSize), &paint); |
| 50 | } |
| 51 | const char* label() override { |
| 52 | return fLabel.c_str(); |
| 53 | } |
| 54 | protected: |
| 55 | SkFilterQuality fQuality; |
| 56 | SkBitmap fBitmap; |
| 57 | SkString fLabel; |
| 58 | }; |
| 59 | |
| 60 | struct GradientCellRenderer : public CellRenderer { |
| 61 | GradientCellRenderer(SkColor colorOne, SkColor colorTwo) { |
| 62 | fColors[0] = colorOne; |
| 63 | fColors[1] = colorTwo; |
| 64 | } |
| 65 | void draw(SkCanvas* canvas) override { |
| 66 | SkPoint points[2] = { |
| 67 | SkPoint::Make(0, 0), |
| 68 | SkPoint::Make(0, gScalarSize) |
| 69 | }; |
| 70 | SkPaint paint; |
| 71 | paint.setShader(SkGradientShader::MakeLinear(points, fColors, nullptr, 2, |
| 72 | SkShader::kClamp_TileMode)); |
| 73 | canvas->drawPaint(paint); |
| 74 | } |
| 75 | const char* label() override { |
| 76 | return "Linear Gradient"; |
| 77 | } |
| 78 | protected: |
| 79 | SkColor fColors[2]; |
| 80 | }; |
| 81 | |
| 82 | struct VerticesCellRenderer : public CellRenderer { |
| 83 | VerticesCellRenderer(SkColor colorOne, SkColor colorTwo) { |
| 84 | fColors[0] = fColors[1] = colorOne; |
| 85 | fColors[2] = fColors[3] = colorTwo; |
| 86 | } |
| 87 | void draw(SkCanvas* canvas) override { |
| 88 | SkPaint paint; |
| 89 | SkPoint vertices[4] = { |
| 90 | SkPoint::Make(0, 0), |
| 91 | SkPoint::Make(gScalarSize, 0), |
| 92 | SkPoint::Make(gScalarSize, gScalarSize), |
| 93 | SkPoint::Make(0, gScalarSize) |
| 94 | }; |
| 95 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, vertices, nullptr, fColors, |
| 96 | nullptr, nullptr, 0, paint); |
| 97 | } |
| 98 | const char* label() override { |
| 99 | return "Vertices"; |
| 100 | } |
| 101 | protected: |
| 102 | SkColor fColors[4]; |
| 103 | }; |
| 104 | |
| 105 | static void draw_gamut_grid(SkCanvas* canvas, SkTArray<SkAutoTDelete<CellRenderer>>& renderers) { |
| 106 | // We want our colors in our wide gamut to be obviously visibly distorted from sRGB, so we use |
| 107 | // Wide Gamut RGB (with sRGB gamma, for HW acceleration) as the working space for this test: |
| 108 | const float gWideGamutRGB_toXYZD50[]{ |
brianosman | de68d6c | 2016-09-09 10:36:17 -0700 | [diff] [blame] | 109 | 0.7161046f, 0.1009296f, 0.1471858f, // -> X |
| 110 | 0.2581874f, 0.7249378f, 0.0168748f, // -> Y |
| 111 | 0.0000000f, 0.0517813f, 0.7734287f, // -> Z |
brianosman | 2c4b64e | 2016-09-07 07:04:44 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | SkMatrix44 wideGamutRGB_toXYZD50(SkMatrix44::kUninitialized_Constructor); |
| 115 | wideGamutRGB_toXYZD50.set3x3RowMajorf(gWideGamutRGB_toXYZD50); |
| 116 | |
| 117 | // Use the original canvas' color type, but account for gamma requirements |
| 118 | SkImageInfo origInfo = canvas->imageInfo(); |
| 119 | auto srgbCS = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
msarett | 48ba2b8 | 2016-09-07 18:55:49 -0700 | [diff] [blame] | 120 | auto wideCS = SkColorSpace::NewRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 121 | wideGamutRGB_toXYZD50); |
brianosman | 2c4b64e | 2016-09-07 07:04:44 -0700 | [diff] [blame] | 122 | switch (origInfo.colorType()) { |
| 123 | case kRGBA_8888_SkColorType: |
| 124 | case kBGRA_8888_SkColorType: |
| 125 | break; |
| 126 | case kRGBA_F16_SkColorType: |
| 127 | srgbCS = srgbCS->makeLinearGamma(); |
| 128 | wideCS = wideCS->makeLinearGamma(); |
| 129 | break; |
| 130 | default: |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // Make our two working surfaces (one sRGB, one Adobe) |
| 135 | SkImageInfo srgbGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo.colorType(), |
| 136 | kPremul_SkAlphaType, srgbCS); |
| 137 | SkImageInfo wideGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo.colorType(), |
| 138 | kPremul_SkAlphaType, wideCS); |
| 139 | // readPixels doesn't do color conversion (yet), so we can use it to see the raw (wide) data |
| 140 | SkImageInfo dstInfo = srgbGamutInfo.makeColorSpace(nullptr); |
| 141 | sk_sp<SkSurface> srgbGamutSurface = canvas->makeSurface(srgbGamutInfo); |
| 142 | sk_sp<SkSurface> wideGamutSurface = canvas->makeSurface(wideGamutInfo); |
| 143 | if (!srgbGamutSurface || !wideGamutSurface) { |
| 144 | return; |
| 145 | } |
| 146 | SkCanvas* srgbGamutCanvas = srgbGamutSurface->getCanvas(); |
| 147 | SkCanvas* wideGamutCanvas = wideGamutSurface->getCanvas(); |
| 148 | |
| 149 | SkPaint textPaint; |
| 150 | textPaint.setAntiAlias(true); |
| 151 | textPaint.setColor(SK_ColorWHITE); |
| 152 | sk_tool_utils::set_portable_typeface(&textPaint); |
| 153 | |
| 154 | SkScalar x = 0, y = 0; |
| 155 | SkScalar textHeight = textPaint.getFontSpacing(); |
| 156 | |
| 157 | for (const auto& renderer : renderers) { |
| 158 | srgbGamutCanvas->clear(SK_ColorBLACK); |
| 159 | renderer->draw(srgbGamutCanvas); |
| 160 | wideGamutCanvas->clear(SK_ColorBLACK); |
| 161 | renderer->draw(wideGamutCanvas); |
| 162 | |
| 163 | canvas->drawText(renderer->label(), strlen(renderer->label()), x, y + textHeight, |
| 164 | textPaint); |
| 165 | |
| 166 | SkBitmap srgbBitmap; |
| 167 | srgbBitmap.setInfo(dstInfo); |
| 168 | srgbGamutCanvas->readPixels(&srgbBitmap, 0, 0); |
| 169 | canvas->drawBitmap(srgbBitmap, x, y + textHeight + 5); |
| 170 | x += (gScalarSize + 1); |
| 171 | |
| 172 | SkBitmap wideBitmap; |
| 173 | wideBitmap.setInfo(dstInfo); |
| 174 | wideGamutCanvas->readPixels(&wideBitmap, 0, 0); |
| 175 | canvas->drawBitmap(wideBitmap, x, y + textHeight + 5); |
| 176 | x += (gScalarSize + 10); |
| 177 | |
| 178 | if (x + (2 * gScalarSize + 1) > gTestWidth) { |
| 179 | x = 0; |
| 180 | y += (textHeight + gScalarSize + 10); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | DEF_SIMPLE_GM_BG(gamut, canvas, gTestWidth, gTestHeight, SK_ColorBLACK) { |
| 186 | SkTArray<SkAutoTDelete<CellRenderer>> renderers; |
| 187 | |
| 188 | // sRGB primaries, rendered as paint color |
| 189 | renderers.push_back(new PaintColorCellRenderer(SK_ColorRED)); |
| 190 | renderers.push_back(new PaintColorCellRenderer(SK_ColorGREEN)); |
| 191 | |
| 192 | // sRGB primaries, rendered as bitmaps |
| 193 | renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kNone_SkFilterQuality)); |
| 194 | renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kLow_SkFilterQuality)); |
| 195 | // Larger bitmap to trigger mipmaps |
| 196 | renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kMedium_SkFilterQuality, 2.0f)); |
| 197 | // Smaller bitmap to trigger bicubic |
| 198 | renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kHigh_SkFilterQuality, 0.5f)); |
| 199 | |
| 200 | // Various gradients involving sRGB primaries and white/black |
| 201 | renderers.push_back(new GradientCellRenderer(SK_ColorRED, SK_ColorGREEN)); |
| 202 | renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorBLACK)); |
| 203 | renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorWHITE)); |
| 204 | |
| 205 | // Vertex colors |
| 206 | renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorRED)); |
| 207 | renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorGREEN)); |
| 208 | |
| 209 | draw_gamut_grid(canvas, renderers); |
| 210 | } |