blob: 633f036b23f44b09a038b5bb19d179499a0c8c0c [file] [log] [blame]
brianosman2d1ee792016-05-05 12:24:31 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Wagner9707a7e2019-05-06 17:17:19 -040019#include "include/core/SkTypes.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040020#include "include/gpu/GrDirectContext.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040021#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/private/SkTemplates.h"
23#include "src/core/SkUtils.h"
24#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040025#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrShaderCaps.h"
27#include "tests/Test.h"
28#include "tools/gpu/GrContextFactory.h"
Ben Wagner1ebeefe2018-03-02 16:59:53 -050029
30#include <math.h>
Ben Wagner9707a7e2019-05-06 17:17:19 -040031#include <initializer_list>
brianosman2d1ee792016-05-05 12:24:31 -070032
brianosman2d1ee792016-05-05 12:24:31 -070033/** convert 0..1 linear value to 0..1 srgb */
Brian Osman964dec32017-01-26 09:32:33 -050034static float linear_to_srgb(float linear) {
brianosman2d1ee792016-05-05 12:24:31 -070035 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 Osman964dec32017-01-26 09:32:33 -050041
42/** convert 0..1 srgb value to 0..1 linear */
43static 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 }
brianosman2d1ee792016-05-05 12:24:31 -070049}
50
Brian Osman964dec32017-01-26 09:32:33 -050051bool 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;
brianosman2d1ee792016-05-05 12:24:31 -070055
Brian Osman964dec32017-01-26 09:32:33 -050056 // Alpha should always be exactly preserved.
57 if ((src & 0xff000000) != (dst & 0xff000000)) {
58 result = false;
59 }
60
Mike Reed98308fb2017-07-07 08:28:13 -040061 // 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 Osman964dec32017-01-26 09:32:33 -050068 for (int c = 0; c < 3; ++c) {
Mike Reed98308fb2017-07-07 08:28:13 -040069 float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale;
Brian Osman788b9162020-02-07 10:36:46 -050070 float lower = std::max(0.f, srcComponent - error);
71 float upper = std::min(255.f, srcComponent + error);
Brian Osman964dec32017-01-26 09:32:33 -050072 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 Reed98308fb2017-07-07 08:28:13 -040079 lower *= alpha;
80 upper *= alpha;
Brian Osman964dec32017-01-26 09:32:33 -050081 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 Reed98308fb2017-07-07 08:28:13 -040084 if (dstComponent < SkScalarFloorToInt(lower) ||
85 dstComponent > SkScalarCeilToInt(upper)) {
brianosman2d1ee792016-05-05 12:24:31 -070086 result = false;
87 }
Mike Reed98308fb2017-07-07 08:28:13 -040088 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f);
Brian Osman964dec32017-01-26 09:32:33 -050089 expectedColor |= expectedComponent << (c * 8);
brianosman2d1ee792016-05-05 12:24:31 -070090 }
Brian Osman964dec32017-01-26 09:32:33 -050091
92 *expected = expectedColor;
93 return result;
brianosman2d1ee792016-05-05 12:24:31 -070094}
95
96DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040097 auto context = ctxInfo.directContext();
Brian Salomona56a7462020-02-07 14:17:25 -050098 static constexpr SkISize kBaseSize{256, 256};
99 static const size_t kRowBytes = sizeof(uint32_t) * kBaseSize.fWidth;
brianosman2d1ee792016-05-05 12:24:31 -0700100
Brian Salomona56a7462020-02-07 14:17:25 -0500101 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kBaseSize);
brianosman2d1ee792016-05-05 12:24:31 -0700102
Brian Salomona56a7462020-02-07 14:17:25 -0500103 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 Reed98308fb2017-07-07 08:28:13 -0400107 }
brianosman2d1ee792016-05-05 12:24:31 -0700108 }
109
Brian Osman3f142b62017-01-17 10:06:20 -0500110 SkBitmap bm;
111 bm.installPixels(ii, srcPixels.get(), kRowBytes);
Mike Reed069e4842021-01-24 11:39:58 -0500112 auto img = bm.asImage();
brianosman2d1ee792016-05-05 12:24:31 -0700113
Brian Salomona56a7462020-02-07 14:17:25 -0500114 SkAutoTMalloc<uint32_t> read(kBaseSize.area());
brianosman2d1ee792016-05-05 12:24:31 -0700115
116 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips9da87e02019-02-04 13:26:26 -0500117 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
brianosman2d1ee792016-05-05 12:24:31 -0700118
Brian Osman964dec32017-01-26 09:32:33 -0500119 for (auto toSRGB : { false, true }) {
120 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
brianosman2d1ee792016-05-05 12:24:31 -0700121
Brian Osman964dec32017-01-26 09:32:33 -0500122 if (!dst) {
123 ERRORF(reporter, "Could not create surfaces for copy surface test.");
124 continue;
125 }
brianosman2d1ee792016-05-05 12:24:31 -0700126
Brian Osman964dec32017-01-26 09:32:33 -0500127 SkCanvas* dstCanvas = dst->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700128
Brian Osman964dec32017-01-26 09:32:33 -0500129 dstCanvas->clear(SK_ColorRED);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400130 dst->flushAndSubmit();
brianosman2d1ee792016-05-05 12:24:31 -0700131
Brian Osman964dec32017-01-26 09:32:33 -0500132 SkPaint gammaPaint;
133 gammaPaint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb286bc22019-04-08 16:23:20 -0400134 gammaPaint.setColorFilter(toSRGB ? SkColorFilters::LinearToSRGBGamma()
135 : SkColorFilters::SRGBToLinearGamma());
robertphillipsecf3dbe2016-07-28 15:17:34 -0700136
Mike Reed069e4842021-01-24 11:39:58 -0500137 dstCanvas->drawImage(img, 0, 0, SkSamplingOptions(), &gammaPaint);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400138 dst->flushAndSubmit();
robertphillipsecf3dbe2016-07-28 15:17:34 -0700139
Brian Salomona56a7462020-02-07 14:17:25 -0500140 sk_memset32(read.get(), 0, kBaseSize.fWidth * kBaseSize.fHeight);
Mike Reedf1942192017-07-21 14:24:29 -0400141 if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
Brian Osman964dec32017-01-26 09:32:33 -0500142 ERRORF(reporter, "Error calling readPixels");
143 continue;
144 }
robertphillipsecf3dbe2016-07-28 15:17:34 -0700145
Brian Osman964dec32017-01-26 09:32:33 -0500146 bool abort = false;
147 // Validate that pixels were copied/transformed correctly.
Brian Salomona56a7462020-02-07 14:17:25 -0500148 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 Osman964dec32017-01-26 09:32:33 -0500152 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;
brianosman2d1ee792016-05-05 12:24:31 -0700159 }
160 }
161 }
162 }
163}