blob: a854549801aed9453eed1da081339e824375a17b [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContext.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"
25#include "src/gpu/GrContextPriv.h"
26#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;
70 float lower = SkTMax(0.f, srcComponent - error);
71 float upper = SkTMin(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) {
bsalomon8b7451a2016-05-11 06:33:06 -070097 GrContext* context = ctxInfo.grContext();
Mike Reed98308fb2017-07-07 08:28:13 -040098 static const int kW = 256;
99 static const int kH = 256;
brianosman2d1ee792016-05-05 12:24:31 -0700100 static const size_t kRowBytes = sizeof(uint32_t) * kW;
101
102 GrSurfaceDesc baseDesc;
brianosman2d1ee792016-05-05 12:24:31 -0700103 baseDesc.fWidth = kW;
104 baseDesc.fHeight = kH;
105
robertphillipsecf3dbe2016-07-28 15:17:34 -0700106 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH);
107
brianosman2d1ee792016-05-05 12:24:31 -0700108 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
Mike Reed98308fb2017-07-07 08:28:13 -0400109 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 }
brianosman2d1ee792016-05-05 12:24:31 -0700113 }
114
Brian Osman3f142b62017-01-17 10:06:20 -0500115 SkBitmap bm;
116 bm.installPixels(ii, srcPixels.get(), kRowBytes);
brianosman2d1ee792016-05-05 12:24:31 -0700117
118 SkAutoTMalloc<uint32_t> read(kW * kH);
119
120 // We allow more error on GPUs with lower precision shader variables.
Robert Phillips9da87e02019-02-04 13:26:26 -0500121 float error = context->priv().caps()->shaderCaps()->halfIs32Bits() ? 0.5f : 1.2f;
brianosman2d1ee792016-05-05 12:24:31 -0700122
Brian Osman964dec32017-01-26 09:32:33 -0500123 for (auto toSRGB : { false, true }) {
124 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
brianosman2d1ee792016-05-05 12:24:31 -0700125
Brian Osman964dec32017-01-26 09:32:33 -0500126 if (!dst) {
127 ERRORF(reporter, "Could not create surfaces for copy surface test.");
128 continue;
129 }
brianosman2d1ee792016-05-05 12:24:31 -0700130
Brian Osman964dec32017-01-26 09:32:33 -0500131 SkCanvas* dstCanvas = dst->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700132
Brian Osman964dec32017-01-26 09:32:33 -0500133 dstCanvas->clear(SK_ColorRED);
Robert Phillips9882dae2019-03-04 11:00:10 -0500134 dst->flush();
brianosman2d1ee792016-05-05 12:24:31 -0700135
Brian Osman964dec32017-01-26 09:32:33 -0500136 SkPaint gammaPaint;
137 gammaPaint.setBlendMode(SkBlendMode::kSrc);
Mike Reedb286bc22019-04-08 16:23:20 -0400138 gammaPaint.setColorFilter(toSRGB ? SkColorFilters::LinearToSRGBGamma()
139 : SkColorFilters::SRGBToLinearGamma());
robertphillipsecf3dbe2016-07-28 15:17:34 -0700140
Brian Osman964dec32017-01-26 09:32:33 -0500141 dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint);
Robert Phillips9882dae2019-03-04 11:00:10 -0500142 dst->flush();
robertphillipsecf3dbe2016-07-28 15:17:34 -0700143
Brian Osman964dec32017-01-26 09:32:33 -0500144 sk_memset32(read.get(), 0, kW * kH);
Mike Reedf1942192017-07-21 14:24:29 -0400145 if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
Brian Osman964dec32017-01-26 09:32:33 -0500146 ERRORF(reporter, "Error calling readPixels");
147 continue;
148 }
robertphillipsecf3dbe2016-07-28 15:17:34 -0700149
Brian Osman964dec32017-01-26 09:32:33 -0500150 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;
brianosman2d1ee792016-05-05 12:24:31 -0700163 }
164 }
165 }
166 }
167}