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
8#include <initializer_list>
9#include "Test.h"
10
11#if SK_SUPPORT_GPU
12#include "GrContext.h"
13
14#include "SkCanvas.h"
15#include "SkColorFilter.h"
16#include "SkSurface.h"
17#include "SkUtils.h"
18#include "sk_tool_utils.h"
19
20/** convert 0..1 linear value to 0..1 srgb */
21static float linear_to_srgb(float linear) {
22    if (linear <= 0.0031308) {
23        return linear * 12.92f;
24    } else {
25        return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
26    }
27}
28
29/** convert 0..1 srgb value to 0..1 linear */
30static float srgb_to_linear(float srgb) {
31    if (srgb <= 0.04045f) {
32        return srgb / 12.92f;
33    } else {
34        return powf((srgb + 0.055f) / 1.055f, 2.4f);
35    }
36}
37
38bool check_gamma(uint32_t src, uint32_t dst, bool toSRGB, float error,
39                 uint32_t* expected) {
40    bool result = true;
41    uint32_t expectedColor = src & 0xff000000;
42
43    // Alpha should always be exactly preserved.
44    if ((src & 0xff000000) != (dst & 0xff000000)) {
45        result = false;
46    }
47
48    for (int c = 0; c < 3; ++c) {
49        uint8_t srcComponent = (src & (0xff << (c * 8))) >> (c * 8);
50        float lower = SkTMax(0.f, (float)srcComponent - error);
51        float upper = SkTMin(255.f, (float)srcComponent + error);
52        if (toSRGB) {
53            lower = linear_to_srgb(lower / 255.f);
54            upper = linear_to_srgb(upper / 255.f);
55        } else {
56            lower = srgb_to_linear(lower / 255.f);
57            upper = srgb_to_linear(upper / 255.f);
58        }
59        SkASSERT(lower >= 0.f && lower <= 255.f);
60        SkASSERT(upper >= 0.f && upper <= 255.f);
61        uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
62        if (dstComponent < SkScalarFloorToInt(lower * 255.f) ||
63            dstComponent > SkScalarCeilToInt(upper * 255.f)) {
64            result = false;
65        }
66        uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 127.5f);
67        expectedColor |= expectedComponent << (c * 8);
68    }
69
70    *expected = expectedColor;
71    return result;
72}
73
74DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
75    GrContext* context = ctxInfo.grContext();
76    static const int kW = 10;
77    static const int kH = 10;
78    static const size_t kRowBytes = sizeof(uint32_t) * kW;
79
80    GrSurfaceDesc baseDesc;
81    baseDesc.fConfig = kRGBA_8888_GrPixelConfig;
82    baseDesc.fWidth = kW;
83    baseDesc.fHeight = kH;
84
85    const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH);
86
87    SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
88    for (int i = 0; i < kW * kH; ++i) {
89        srcPixels.get()[i] = i;
90    }
91
92    SkBitmap bm;
93    bm.installPixels(ii, srcPixels.get(), kRowBytes);
94
95    SkAutoTMalloc<uint32_t> read(kW * kH);
96
97    // We allow more error on GPUs with lower precision shader variables.
98    float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
99
100    for (auto toSRGB : { false, true }) {
101        sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii));
102
103        if (!dst) {
104            ERRORF(reporter, "Could not create surfaces for copy surface test.");
105            continue;
106        }
107
108        SkCanvas* dstCanvas = dst->getCanvas();
109
110        dstCanvas->clear(SK_ColorRED);
111        dstCanvas->flush();
112
113        SkPaint gammaPaint;
114        gammaPaint.setBlendMode(SkBlendMode::kSrc);
115        gammaPaint.setColorFilter(toSRGB ? sk_tool_utils::MakeLinearToSRGBColorFilter()
116                                         : sk_tool_utils::MakeSRGBToLinearColorFilter());
117
118        dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint);
119        dstCanvas->flush();
120
121        sk_memset32(read.get(), 0, kW * kH);
122        if (!dstCanvas->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
123            ERRORF(reporter, "Error calling readPixels");
124            continue;
125        }
126
127        bool abort = false;
128        // Validate that pixels were copied/transformed correctly.
129        for (int y = 0; y < kH && !abort; ++y) {
130            for (int x = 0; x < kW && !abort; ++x) {
131                uint32_t r = read.get()[y * kW + x];
132                uint32_t s = srcPixels.get()[y * kW + x];
133                uint32_t expected;
134                if (!check_gamma(s, r, toSRGB, error, &expected)) {
135                    ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
136                           "from src 0x%08x and mode %s. Got %08x", x, y, expected, s,
137                           toSRGB ? "ToSRGB" : "ToLinear", r);
138                    abort = true;
139                    break;
140                }
141            }
142        }
143    }
144}
145#endif
146