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