Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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.0 f) | |
| 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, nul lptr, 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<CellRendere r>>& renderers) { | |
| 106 // We want our colors in our wide gamut to be obviously visibly distorted fr om sRGB, so we use | |
| 107 // Wide Gamut RGB (with sRGB gamma, for HW acceleration) as the working spac e for this test: | |
| 108 const float gWideGamutRGB_toXYZD50[]{ | |
| 109 0.7161046f, 0.2581874f, 0.0000000f, // * R | |
| 110 0.1009296f, 0.7249378f, 0.0517813f, // * G | |
| 111 0.1471858f, 0.0168748f, 0.7734287f, // * B | |
| 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); | |
| 120 auto wideCS = SkColorSpace::NewRGB(SkColorSpace::kSRGB_GammaNamed, wideGamut RGB_toXYZD50); | |
| 121 switch (origInfo.colorType()) { | |
| 122 case kRGBA_8888_SkColorType: | |
| 123 case kBGRA_8888_SkColorType: | |
| 124 break; | |
| 125 case kRGBA_F16_SkColorType: | |
| 126 srgbCS = srgbCS->makeLinearGamma(); | |
| 127 wideCS = wideCS->makeLinearGamma(); | |
| 128 break; | |
| 129 default: | |
| 130 return; | |
|
msarett
2016/09/07 13:42:20
I think if you treat default as RGBA/BGRA, you won
| |
| 131 } | |
| 132 | |
| 133 // Make our two working surfaces (one sRGB, one Adobe) | |
| 134 SkImageInfo srgbGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo .colorType(), | |
| 135 kPremul_SkAlphaType, srgbCS); | |
| 136 SkImageInfo wideGamutInfo = SkImageInfo::Make(gRectSize, gRectSize, origInfo .colorType(), | |
| 137 kPremul_SkAlphaType, wideCS); | |
| 138 // readPixels doesn't do color conversion (yet), so we can use it to see the raw (wide) data | |
| 139 SkImageInfo dstInfo = srgbGamutInfo.makeColorSpace(nullptr); | |
| 140 sk_sp<SkSurface> srgbGamutSurface = canvas->makeSurface(srgbGamutInfo); | |
| 141 sk_sp<SkSurface> wideGamutSurface = canvas->makeSurface(wideGamutInfo); | |
| 142 if (!srgbGamutSurface || !wideGamutSurface) { | |
| 143 return; | |
| 144 } | |
| 145 SkCanvas* srgbGamutCanvas = srgbGamutSurface->getCanvas(); | |
| 146 SkCanvas* wideGamutCanvas = wideGamutSurface->getCanvas(); | |
| 147 | |
| 148 SkPaint textPaint; | |
| 149 textPaint.setAntiAlias(true); | |
| 150 textPaint.setColor(SK_ColorWHITE); | |
| 151 sk_tool_utils::set_portable_typeface(&textPaint); | |
| 152 | |
| 153 SkScalar x = 0, y = 0; | |
| 154 SkScalar textHeight = textPaint.getFontSpacing(); | |
| 155 | |
| 156 for (const auto& renderer : renderers) { | |
| 157 srgbGamutCanvas->clear(SK_ColorBLACK); | |
| 158 renderer->draw(srgbGamutCanvas); | |
| 159 wideGamutCanvas->clear(SK_ColorBLACK); | |
| 160 renderer->draw(wideGamutCanvas); | |
| 161 | |
| 162 canvas->drawText(renderer->label(), strlen(renderer->label()), x, y + te xtHeight, | |
| 163 textPaint); | |
| 164 | |
| 165 SkBitmap srgbBitmap; | |
| 166 srgbBitmap.setInfo(dstInfo); | |
| 167 srgbGamutCanvas->readPixels(&srgbBitmap, 0, 0); | |
| 168 canvas->drawBitmap(srgbBitmap, x, y + textHeight + 5); | |
| 169 x += (gScalarSize + 1); | |
| 170 | |
| 171 SkBitmap wideBitmap; | |
| 172 wideBitmap.setInfo(dstInfo); | |
| 173 wideGamutCanvas->readPixels(&wideBitmap, 0, 0); | |
| 174 canvas->drawBitmap(wideBitmap, x, y + textHeight + 5); | |
| 175 x += (gScalarSize + 10); | |
| 176 | |
| 177 if (x + (2 * gScalarSize + 1) > gTestWidth) { | |
| 178 x = 0; | |
| 179 y += (textHeight + gScalarSize + 10); | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 DEF_SIMPLE_GM_BG(gamut, canvas, gTestWidth, gTestHeight, SK_ColorBLACK) { | |
| 185 SkTArray<SkAutoTDelete<CellRenderer>> renderers; | |
| 186 | |
| 187 // sRGB primaries, rendered as paint color | |
| 188 renderers.push_back(new PaintColorCellRenderer(SK_ColorRED)); | |
| 189 renderers.push_back(new PaintColorCellRenderer(SK_ColorGREEN)); | |
| 190 | |
| 191 // sRGB primaries, rendered as bitmaps | |
| 192 renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kNone_SkFilterQualit y)); | |
| 193 renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kLow_SkFilterQuali ty)); | |
| 194 // Larger bitmap to trigger mipmaps | |
| 195 renderers.push_back(new BitmapCellRenderer(SK_ColorRED, kMedium_SkFilterQual ity, 2.0f)); | |
| 196 // Smaller bitmap to trigger bicubic | |
| 197 renderers.push_back(new BitmapCellRenderer(SK_ColorGREEN, kHigh_SkFilterQual ity, 0.5f)); | |
| 198 | |
| 199 // Various gradients involving sRGB primaries and white/black | |
| 200 renderers.push_back(new GradientCellRenderer(SK_ColorRED, SK_ColorGREEN)); | |
| 201 renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorBLACK)); | |
| 202 renderers.push_back(new GradientCellRenderer(SK_ColorGREEN, SK_ColorWHITE)); | |
| 203 | |
| 204 // Vertex colors | |
| 205 renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorRED)); | |
| 206 renderers.push_back(new VerticesCellRenderer(SK_ColorRED, SK_ColorGREEN)); | |
| 207 | |
| 208 draw_gamut_grid(canvas, renderers); | |
| 209 } | |
| OLD | NEW |