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