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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorFilter.h" |
| 13 | #include "include/core/SkColorPriv.h" |
| 14 | #include "include/core/SkImageInfo.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSurface.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 19 | #include "include/core/SkTypes.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrDirectContext.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 21 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/private/SkTemplates.h" |
| 23 | #include "src/core/SkUtils.h" |
| 24 | #include "src/gpu/GrCaps.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/GrShaderCaps.h" |
| 27 | #include "tests/Test.h" |
| 28 | #include "tools/gpu/GrContextFactory.h" |
Ben Wagner | 1ebeefe | 2018-03-02 16:59:53 -0500 | [diff] [blame] | 29 | |
| 30 | #include <math.h> |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 31 | #include <initializer_list> |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 32 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 33 | /** convert 0..1 linear value to 0..1 srgb */ |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 34 | static float linear_to_srgb(float linear) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 35 | if (linear <= 0.0031308) { |
| 36 | return linear * 12.92f; |
| 37 | } else { |
| 38 | return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; |
| 39 | } |
| 40 | } |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 41 | |
| 42 | /** convert 0..1 srgb value to 0..1 linear */ |
| 43 | static float srgb_to_linear(float srgb) { |
| 44 | if (srgb <= 0.04045f) { |
| 45 | return srgb / 12.92f; |
| 46 | } else { |
| 47 | return powf((srgb + 0.055f) / 1.055f, 2.4f); |
| 48 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 51 | bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error, |
| 52 | uint32_t* expected) { |
| 53 | bool result = true; |
| 54 | uint32_t expectedColor = src & 0xff000000; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 55 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 56 | // Alpha should always be exactly preserved. |
| 57 | if ((src & 0xff000000) != (dst & 0xff000000)) { |
| 58 | result = false; |
| 59 | } |
| 60 | |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 61 | // need to unpremul before we can perform srgb magic |
| 62 | float invScale = 0; |
| 63 | float alpha = SkGetPackedA32(src); |
| 64 | if (alpha) { |
| 65 | invScale = 255.0f / alpha; |
| 66 | } |
| 67 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 68 | for (int c = 0; c < 3; ++c) { |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 69 | float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 70 | float lower = std::max(0.f, srcComponent - error); |
| 71 | float upper = std::min(255.f, srcComponent + error); |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 72 | if (toSRGB) { |
| 73 | lower = linear_to_srgb(lower / 255.f); |
| 74 | upper = linear_to_srgb(upper / 255.f); |
| 75 | } else { |
| 76 | lower = srgb_to_linear(lower / 255.f); |
| 77 | upper = srgb_to_linear(upper / 255.f); |
| 78 | } |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 79 | lower *= alpha; |
| 80 | upper *= alpha; |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 81 | SkASSERT(lower >= 0.f && lower <= 255.f); |
| 82 | SkASSERT(upper >= 0.f && upper <= 255.f); |
| 83 | uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 84 | if (dstComponent < SkScalarFloorToInt(lower) || |
| 85 | dstComponent > SkScalarCeilToInt(upper)) { |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 86 | result = false; |
| 87 | } |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 88 | uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f); |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 89 | expectedColor |= expectedComponent << (c * 8); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 90 | } |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 91 | |
| 92 | *expected = expectedColor; |
| 93 | return result; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 97 | auto context = ctxInfo.directContext(); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 98 | static constexpr SkISize kBaseSize{256, 256}; |
| 99 | static const size_t kRowBytes = sizeof(uint32_t) * kBaseSize.fWidth; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 100 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 101 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(kBaseSize); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 102 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 103 | SkAutoTMalloc<uint32_t> srcPixels(kBaseSize.area()); |
| 104 | for (int y = 0; y < kBaseSize.fHeight; ++y) { |
| 105 | for (int x = 0; x < kBaseSize.fWidth; ++x) { |
| 106 | srcPixels.get()[y*kBaseSize.fWidth+x] = SkPreMultiplyARGB(x, y, x, 0xFF); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 107 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Brian Osman | 3f142b6 | 2017-01-17 10:06:20 -0500 | [diff] [blame] | 110 | SkBitmap bm; |
| 111 | bm.installPixels(ii, srcPixels.get(), kRowBytes); |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 112 | auto img = bm.asImage(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 113 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 114 | SkAutoTMalloc<uint32_t> read(kBaseSize.area()); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 115 | |
| 116 | // We allow more error on GPUs with lower precision shader variables. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 117 | float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 118 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 119 | for (auto toSRGB : { false, true }) { |
| 120 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 121 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 122 | if (!dst) { |
| 123 | ERRORF(reporter, "Could not create surfaces for copy surface test."); |
| 124 | continue; |
| 125 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 126 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 127 | SkCanvas* dstCanvas = dst->getCanvas(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 128 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 129 | dstCanvas->clear(SK_ColorRED); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 130 | dst->flushAndSubmit(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 131 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 132 | SkPaint gammaPaint; |
| 133 | gammaPaint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 134 | gammaPaint.setColorFilter(toSRGB ? SkColorFilters::LinearToSRGBGamma() |
| 135 | : SkColorFilters::SRGBToLinearGamma()); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 136 | |
Mike Reed | 069e484 | 2021-01-24 11:39:58 -0500 | [diff] [blame] | 137 | dstCanvas->drawImage(img, 0, 0, SkSamplingOptions(), &gammaPaint); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 138 | dst->flushAndSubmit(); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 139 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 140 | sk_memset32(read.get(), 0, kBaseSize.fWidth * kBaseSize.fHeight); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 141 | if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) { |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 142 | ERRORF(reporter, "Error calling readPixels"); |
| 143 | continue; |
| 144 | } |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 145 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 146 | bool abort = false; |
| 147 | // Validate that pixels were copied/transformed correctly. |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 148 | for (int y = 0; y < kBaseSize.fHeight && !abort; ++y) { |
| 149 | for (int x = 0; x < kBaseSize.fWidth && !abort; ++x) { |
| 150 | uint32_t r = read.get()[y * kBaseSize.fWidth + x]; |
| 151 | uint32_t s = srcPixels.get()[y * kBaseSize.fWidth + x]; |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 152 | uint32_t expected; |
| 153 | if (!check_gamma(s, r, toSRGB, error, &expected)) { |
| 154 | ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " |
| 155 | "from src 0x%08x and mode %s. Got %08x", x, y, expected, s, |
| 156 | toSRGB ? "ToSRGB" : "ToLinear", r); |
| 157 | abort = true; |
| 158 | break; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |