brianosman | 2d1ee79 | 2016-05-05 12:24:31 -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 <initializer_list> |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #if SK_SUPPORT_GPU |
| 12 | #include "GrContext.h" |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 13 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 15 | #include "SkColorFilter.h" |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 17 | #include "SkUtils.h" |
| 18 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 19 | /** convert 0..1 linear value to 0..1 srgb */ |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 20 | static float linear_to_srgb(float linear) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 21 | if (linear <= 0.0031308) { |
| 22 | return linear * 12.92f; |
| 23 | } else { |
| 24 | return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; |
| 25 | } |
| 26 | } |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 27 | |
| 28 | /** convert 0..1 srgb value to 0..1 linear */ |
| 29 | static float srgb_to_linear(float srgb) { |
| 30 | if (srgb <= 0.04045f) { |
| 31 | return srgb / 12.92f; |
| 32 | } else { |
| 33 | return powf((srgb + 0.055f) / 1.055f, 2.4f); |
| 34 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 37 | bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error, |
| 38 | uint32_t* expected) { |
| 39 | bool result = true; |
| 40 | uint32_t expectedColor = src & 0xff000000; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 41 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 42 | // Alpha should always be exactly preserved. |
| 43 | if ((src & 0xff000000) != (dst & 0xff000000)) { |
| 44 | result = false; |
| 45 | } |
| 46 | |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 47 | // need to unpremul before we can perform srgb magic |
| 48 | float invScale = 0; |
| 49 | float alpha = SkGetPackedA32(src); |
| 50 | if (alpha) { |
| 51 | invScale = 255.0f / alpha; |
| 52 | } |
| 53 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 54 | for (int c = 0; c < 3; ++c) { |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 55 | float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale; |
| 56 | float lower = SkTMax(0.f, srcComponent - error); |
| 57 | float upper = SkTMin(255.f, srcComponent + error); |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 58 | if (toSRGB) { |
| 59 | lower = linear_to_srgb(lower / 255.f); |
| 60 | upper = linear_to_srgb(upper / 255.f); |
| 61 | } else { |
| 62 | lower = srgb_to_linear(lower / 255.f); |
| 63 | upper = srgb_to_linear(upper / 255.f); |
| 64 | } |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 65 | lower *= alpha; |
| 66 | upper *= alpha; |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 67 | SkASSERT(lower >= 0.f && lower <= 255.f); |
| 68 | SkASSERT(upper >= 0.f && upper <= 255.f); |
| 69 | uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 70 | if (dstComponent < SkScalarFloorToInt(lower) || |
| 71 | dstComponent > SkScalarCeilToInt(upper)) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 72 | result = false; |
| 73 | } |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 74 | uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f); |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 75 | expectedColor |= expectedComponent << (c * 8); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 76 | } |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 77 | |
| 78 | *expected = expectedColor; |
| 79 | return result; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 83 | GrContext* context = ctxInfo.grContext(); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 84 | static const int kW = 256; |
| 85 | static const int kH = 256; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 86 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 87 | |
| 88 | GrSurfaceDesc baseDesc; |
| 89 | baseDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 90 | baseDesc.fWidth = kW; |
| 91 | baseDesc.fHeight = kH; |
| 92 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 93 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH); |
| 94 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 95 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 96 | for (int y = 0; y < kH; ++y) { |
| 97 | for (int x = 0; x < kW; ++x) { |
| 98 | srcPixels.get()[y*kW+x] = SkPreMultiplyARGB(x, y, x, 0xFF); |
| 99 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Brian Osman | 3f142b6 | 2017-01-17 10:06:20 -0500 | [diff] [blame] | 102 | SkBitmap bm; |
| 103 | bm.installPixels(ii, srcPixels.get(), kRowBytes); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 104 | |
| 105 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 106 | |
| 107 | // We allow more error on GPUs with lower precision shader variables. |
| 108 | float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f; |
| 109 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 110 | for (auto toSRGB : { false, true }) { |
| 111 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 112 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 113 | if (!dst) { |
| 114 | ERRORF(reporter, "Could not create surfaces for copy surface test."); |
| 115 | continue; |
| 116 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 117 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 118 | SkCanvas* dstCanvas = dst->getCanvas(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 119 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 120 | dstCanvas->clear(SK_ColorRED); |
| 121 | dstCanvas->flush(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 122 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 123 | SkPaint gammaPaint; |
| 124 | gammaPaint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | 412cda7 | 2017-07-05 15:43:15 -0400 | [diff] [blame] | 125 | gammaPaint.setColorFilter(toSRGB ? SkColorFilter::MakeLinearToSRGBGamma() |
| 126 | : SkColorFilter::MakeSRGBToLinearGamma()); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 127 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 128 | dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint); |
| 129 | dstCanvas->flush(); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 130 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 131 | sk_memset32(read.get(), 0, kW * kH); |
| 132 | if (!dstCanvas->readPixels(ii, read.get(), kRowBytes, 0, 0)) { |
| 133 | ERRORF(reporter, "Error calling readPixels"); |
| 134 | continue; |
| 135 | } |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 136 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 137 | bool abort = false; |
| 138 | // Validate that pixels were copied/transformed correctly. |
| 139 | for (int y = 0; y < kH && !abort; ++y) { |
| 140 | for (int x = 0; x < kW && !abort; ++x) { |
| 141 | uint32_t r = read.get()[y * kW + x]; |
| 142 | uint32_t s = srcPixels.get()[y * kW + x]; |
| 143 | uint32_t expected; |
| 144 | if (!check_gamma(s, r, toSRGB, error, &expected)) { |
| 145 | ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " |
| 146 | "from src 0x%08x and mode %s. Got %08x", x, y, expected, s, |
| 147 | toSRGB ? "ToSRGB" : "ToLinear", r); |
| 148 | abort = true; |
| 149 | break; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | #endif |