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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrContext.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" |
| 25 | #include "src/gpu/GrContextPriv.h" |
| 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; |
| 70 | float lower = SkTMax(0.f, srcComponent - error); |
| 71 | float upper = SkTMin(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) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 97 | GrContext* context = ctxInfo.grContext(); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 98 | static const int kW = 256; |
| 99 | static const int kH = 256; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 100 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 101 | |
| 102 | GrSurfaceDesc baseDesc; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 103 | baseDesc.fWidth = kW; |
| 104 | baseDesc.fHeight = kH; |
| 105 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 106 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH); |
| 107 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 108 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
Mike Reed | 98308fb | 2017-07-07 08:28:13 -0400 | [diff] [blame] | 109 | for (int y = 0; y < kH; ++y) { |
| 110 | for (int x = 0; x < kW; ++x) { |
| 111 | srcPixels.get()[y*kW+x] = SkPreMultiplyARGB(x, y, x, 0xFF); |
| 112 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Brian Osman | 3f142b6 | 2017-01-17 10:06:20 -0500 | [diff] [blame] | 115 | SkBitmap bm; |
| 116 | bm.installPixels(ii, srcPixels.get(), kRowBytes); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 117 | |
| 118 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 119 | |
| 120 | // We allow more error on GPUs with lower precision shader variables. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 121 | float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f; |
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 | for (auto toSRGB : { false, true }) { |
| 124 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 125 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 126 | if (!dst) { |
| 127 | ERRORF(reporter, "Could not create surfaces for copy surface test."); |
| 128 | continue; |
| 129 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 130 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 131 | SkCanvas* dstCanvas = dst->getCanvas(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 132 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 133 | dstCanvas->clear(SK_ColorRED); |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 134 | dst->flush(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 135 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 136 | SkPaint gammaPaint; |
| 137 | gammaPaint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 138 | gammaPaint.setColorFilter(toSRGB ? SkColorFilters::LinearToSRGBGamma() |
| 139 | : SkColorFilters::SRGBToLinearGamma()); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 140 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 141 | dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint); |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 142 | dst->flush(); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 143 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 144 | sk_memset32(read.get(), 0, kW * kH); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 145 | if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) { |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 146 | ERRORF(reporter, "Error calling readPixels"); |
| 147 | continue; |
| 148 | } |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 149 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 150 | bool abort = false; |
| 151 | // Validate that pixels were copied/transformed correctly. |
| 152 | for (int y = 0; y < kH && !abort; ++y) { |
| 153 | for (int x = 0; x < kW && !abort; ++x) { |
| 154 | uint32_t r = read.get()[y * kW + x]; |
| 155 | uint32_t s = srcPixels.get()[y * kW + x]; |
| 156 | uint32_t expected; |
| 157 | if (!check_gamma(s, r, toSRGB, error, &expected)) { |
| 158 | ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " |
| 159 | "from src 0x%08x and mode %s. Got %08x", x, y, expected, s, |
| 160 | toSRGB ? "ToSRGB" : "ToLinear", r); |
| 161 | abort = true; |
| 162 | break; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |