blob: f2049e7ccc861205b51d7f3f4ec81e7da6d5e53d [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
8#include <initializer_list>
9#include "Test.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
brianosman2d1ee792016-05-05 12:24:31 -070013
robertphillipsecf3dbe2016-07-28 15:17:34 -070014#include "SkCanvas.h"
Brian Osman964dec32017-01-26 09:32:33 -050015#include "SkColorFilter.h"
robertphillipsecf3dbe2016-07-28 15:17:34 -070016#include "SkSurface.h"
brianosman2d1ee792016-05-05 12:24:31 -070017#include "SkUtils.h"
18
brianosman2d1ee792016-05-05 12:24:31 -070019/** convert 0..1 linear value to 0..1 srgb */
Brian Osman964dec32017-01-26 09:32:33 -050020static float linear_to_srgb(float linear) {
brianosman2d1ee792016-05-05 12:24:31 -070021 if (linear <= 0.0031308) {
22 return linear * 12.92f;
23 } else {
24 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
25 }
26}
Brian Osman964dec32017-01-26 09:32:33 -050027
28/** convert 0..1 srgb value to 0..1 linear */
29static float srgb_to_linear(float srgb) {
30 if (srgb <= 0.04045f) {
31 return srgb / 12.92f;
32 } else {
33 return powf((srgb + 0.055f) / 1.055f, 2.4f);
34 }
brianosman2d1ee792016-05-05 12:24:31 -070035}
36
Brian Osman964dec32017-01-26 09:32:33 -050037bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error,
38 uint32_t* expected) {
39 bool result = true;
40 uint32_t expectedColor = src & 0xff000000;
brianosman2d1ee792016-05-05 12:24:31 -070041
Brian Osman964dec32017-01-26 09:32:33 -050042 // Alpha should always be exactly preserved.
43 if ((src & 0xff000000) != (dst & 0xff000000)) {
44 result = false;
45 }
46
Mike Reed98308fb2017-07-07 08:28:13 -040047 // need to unpremul before we can perform srgb magic
48 float invScale = 0;
49 float alpha = SkGetPackedA32(src);
50 if (alpha) {
51 invScale = 255.0f / alpha;
52 }
53
Brian Osman964dec32017-01-26 09:32:33 -050054 for (int c = 0; c < 3; ++c) {
Mike Reed98308fb2017-07-07 08:28:13 -040055 float srcComponent = ((src & (0xff << (c * 8))) >> (c * 8)) * invScale;
56 float lower = SkTMax(0.f, srcComponent - error);
57 float upper = SkTMin(255.f, srcComponent + error);
Brian Osman964dec32017-01-26 09:32:33 -050058 if (toSRGB) {
59 lower = linear_to_srgb(lower / 255.f);
60 upper = linear_to_srgb(upper / 255.f);
61 } else {
62 lower = srgb_to_linear(lower / 255.f);
63 upper = srgb_to_linear(upper / 255.f);
64 }
Mike Reed98308fb2017-07-07 08:28:13 -040065 lower *= alpha;
66 upper *= alpha;
Brian Osman964dec32017-01-26 09:32:33 -050067 SkASSERT(lower >= 0.f && lower <= 255.f);
68 SkASSERT(upper >= 0.f && upper <= 255.f);
69 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
Mike Reed98308fb2017-07-07 08:28:13 -040070 if (dstComponent < SkScalarFloorToInt(lower) ||
71 dstComponent > SkScalarCeilToInt(upper)) {
brianosman2d1ee792016-05-05 12:24:31 -070072 result = false;
73 }
Mike Reed98308fb2017-07-07 08:28:13 -040074 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f);
Brian Osman964dec32017-01-26 09:32:33 -050075 expectedColor |= expectedComponent << (c * 8);
brianosman2d1ee792016-05-05 12:24:31 -070076 }
Brian Osman964dec32017-01-26 09:32:33 -050077
78 *expected = expectedColor;
79 return result;
brianosman2d1ee792016-05-05 12:24:31 -070080}
81
82DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070083 GrContext* context = ctxInfo.grContext();
Mike Reed98308fb2017-07-07 08:28:13 -040084 static const int kW = 256;
85 static const int kH = 256;
brianosman2d1ee792016-05-05 12:24:31 -070086 static const size_t kRowBytes = sizeof(uint32_t) * kW;
87
88 GrSurfaceDesc baseDesc;
89 baseDesc.fConfig = kRGBA_8888_GrPixelConfig;
90 baseDesc.fWidth = kW;
91 baseDesc.fHeight = kH;
92
robertphillipsecf3dbe2016-07-28 15:17:34 -070093 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH);
94
brianosman2d1ee792016-05-05 12:24:31 -070095 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
Mike Reed98308fb2017-07-07 08:28:13 -040096 for (int y = 0; y < kH; ++y) {
97 for (int x = 0; x < kW; ++x) {
98 srcPixels.get()[y*kW+x] = SkPreMultiplyARGB(x, y, x, 0xFF);
99 }
brianosman2d1ee792016-05-05 12:24:31 -0700100 }
101
Brian Osman3f142b62017-01-17 10:06:20 -0500102 SkBitmap bm;
103 bm.installPixels(ii, srcPixels.get(), kRowBytes);
brianosman2d1ee792016-05-05 12:24:31 -0700104
105 SkAutoTMalloc<uint32_t> read(kW * kH);
106
107 // We allow more error on GPUs with lower precision shader variables.
108 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
109
Brian Osman964dec32017-01-26 09:32:33 -0500110 for (auto toSRGB : { false, true }) {
111 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
brianosman2d1ee792016-05-05 12:24:31 -0700112
Brian Osman964dec32017-01-26 09:32:33 -0500113 if (!dst) {
114 ERRORF(reporter, "Could not create surfaces for copy surface test.");
115 continue;
116 }
brianosman2d1ee792016-05-05 12:24:31 -0700117
Brian Osman964dec32017-01-26 09:32:33 -0500118 SkCanvas* dstCanvas = dst->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700119
Brian Osman964dec32017-01-26 09:32:33 -0500120 dstCanvas->clear(SK_ColorRED);
121 dstCanvas->flush();
brianosman2d1ee792016-05-05 12:24:31 -0700122
Brian Osman964dec32017-01-26 09:32:33 -0500123 SkPaint gammaPaint;
124 gammaPaint.setBlendMode(SkBlendMode::kSrc);
Mike Reed412cda72017-07-05 15:43:15 -0400125 gammaPaint.setColorFilter(toSRGB ? SkColorFilter::MakeLinearToSRGBGamma()
126 : SkColorFilter::MakeSRGBToLinearGamma());
robertphillipsecf3dbe2016-07-28 15:17:34 -0700127
Brian Osman964dec32017-01-26 09:32:33 -0500128 dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint);
129 dstCanvas->flush();
robertphillipsecf3dbe2016-07-28 15:17:34 -0700130
Brian Osman964dec32017-01-26 09:32:33 -0500131 sk_memset32(read.get(), 0, kW * kH);
Mike Reedf1942192017-07-21 14:24:29 -0400132 if (!dst->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
Brian Osman964dec32017-01-26 09:32:33 -0500133 ERRORF(reporter, "Error calling readPixels");
134 continue;
135 }
robertphillipsecf3dbe2016-07-28 15:17:34 -0700136
Brian Osman964dec32017-01-26 09:32:33 -0500137 bool abort = false;
138 // Validate that pixels were copied/transformed correctly.
139 for (int y = 0; y < kH && !abort; ++y) {
140 for (int x = 0; x < kW && !abort; ++x) {
141 uint32_t r = read.get()[y * kW + x];
142 uint32_t s = srcPixels.get()[y * kW + x];
143 uint32_t expected;
144 if (!check_gamma(s, r, toSRGB, error, &expected)) {
145 ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
146 "from src 0x%08x and mode %s. Got %08x", x, y, expected, s,
147 toSRGB ? "ToSRGB" : "ToLinear", r);
148 abort = true;
149 break;
brianosman2d1ee792016-05-05 12:24:31 -0700150 }
151 }
152 }
153 }
154}
155#endif