blob: 8db8fe09f65f4a6de7f7e6fc241a6e638a211f47 [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"
robertphillipsecf3dbe2016-07-28 15:17:34 -070013#include "GrDrawContext.h"
brianosman2d1ee792016-05-05 12:24:31 -070014#include "GrTexture.h"
15#include "GrTextureProvider.h"
16
robertphillipsecf3dbe2016-07-28 15:17:34 -070017#include "SkCanvas.h"
18#include "SkPixmap.h"
19#include "SkSurface.h"
brianosman2d1ee792016-05-05 12:24:31 -070020#include "SkUtils.h"
21
22 // using anonymous namespace because these functions are used as template params.
23namespace {
24/** convert 0..1 linear value to 0..1 srgb */
25float linear_to_srgb(float linear) {
26 if (linear <= 0.0031308) {
27 return linear * 12.92f;
28 } else {
29 return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f;
30 }
31}
32}
33
34bool check_gamma(uint32_t src, uint32_t dst, float gamma, float error, uint32_t* expected) {
35 if (SkScalarNearlyEqual(gamma, 1.f)) {
36 *expected = src;
37 return src == dst;
38 } else {
39 bool result = true;
40 uint32_t expectedColor = src & 0xff000000;
41
42 // Alpha should always be exactly preserved.
43 if ((src & 0xff000000) != (dst & 0xff000000)) {
44 result = false;
45 }
46
47 for (int c = 0; c < 3; ++c) {
48 uint8_t srcComponent = (src & (0xff << (c * 8))) >> (c * 8);
49 float lower = SkTMax(0.f, (float)srcComponent - error);
50 float upper = SkTMin(255.f, (float)srcComponent + error);
51 if (SkScalarNearlyEqual(gamma, 1.0f / 2.2f)) {
52 lower = linear_to_srgb(lower / 255.f);
53 upper = linear_to_srgb(upper / 255.f);
54 } else {
55 lower = powf(lower / 255.f, gamma);
56 upper = powf(upper / 255.f, gamma);
57 }
58 SkASSERT(lower >= 0.f && lower <= 255.f);
59 SkASSERT(upper >= 0.f && upper <= 255.f);
60 uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8);
61 if (dstComponent < SkScalarFloorToInt(lower * 255.f) ||
62 dstComponent > SkScalarCeilToInt(upper * 255.f)) {
63 result = false;
64 }
65 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 127.5f);
66 expectedColor |= expectedComponent << (c * 8);
67 }
68
69 *expected = expectedColor;
70 return result;
71 }
72}
73
74DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -070075 GrContext* context = ctxInfo.grContext();
brianosman2d1ee792016-05-05 12:24:31 -070076 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
robertphillipsecf3dbe2016-07-28 15:17:34 -070085 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH);
86
brianosman2d1ee792016-05-05 12:24:31 -070087 SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
88 for (int i = 0; i < kW * kH; ++i) {
89 srcPixels.get()[i] = i;
90 }
91
robertphillipsecf3dbe2016-07-28 15:17:34 -070092 SkPixmap pm(ii, srcPixels.get(), kRowBytes);
brianosman2d1ee792016-05-05 12:24:31 -070093
94 SkAutoTMalloc<uint32_t> read(kW * kH);
95
96 // We allow more error on GPUs with lower precision shader variables.
97 float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f;
98
robertphillipsecf3dbe2016-07-28 15:17:34 -070099 for (auto dOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
100 for (auto gamma : { 1.0f, 1.0f / 1.8f, 1.0f / 2.2f }) {
101 sk_sp<SkImage> src(SkImage::MakeTextureFromPixmap(context, pm, SkBudgeted::kNo));
brianosman2d1ee792016-05-05 12:24:31 -0700102
robertphillipsecf3dbe2016-07-28 15:17:34 -0700103 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
104 ii, 0, dOrigin, nullptr));
brianosman2d1ee792016-05-05 12:24:31 -0700105
robertphillipsecf3dbe2016-07-28 15:17:34 -0700106 if (!src || !dst) {
107 ERRORF(reporter, "Could not create surfaces for copy surface test.");
108 continue;
109 }
brianosman2d1ee792016-05-05 12:24:31 -0700110
robertphillipsecf3dbe2016-07-28 15:17:34 -0700111 SkCanvas* dstCanvas = dst->getCanvas();
brianosman2d1ee792016-05-05 12:24:31 -0700112
robertphillipsecf3dbe2016-07-28 15:17:34 -0700113 dstCanvas->clear(SK_ColorRED);
114 dstCanvas->flush();
brianosman2d1ee792016-05-05 12:24:31 -0700115
robertphillipsecf3dbe2016-07-28 15:17:34 -0700116 // Temporary code until applyGamma is replaced
117 GrDrawContext* dc = dstCanvas->internal_private_accessTopLayerDrawContext();
118 GrRenderTarget* rt = dc->accessRenderTarget();
119 GrTexture* texture = src->getTexture();
120 SkASSERT(texture);
121
122 bool result = context->applyGamma(rt, texture, gamma);
123
124 // To make the copied src rect correct we would apply any dst clipping
125 // back to the src rect, but we don't use it again so don't bother.
126 if (!result) {
127 ERRORF(reporter, "Unexpected failure from applyGamma.");
128 continue;
129 }
130
131 sk_memset32(read.get(), 0, kW * kH);
132 if (!dstCanvas->readPixels(ii, read.get(), kRowBytes, 0, 0)) {
133 ERRORF(reporter, "Error calling readPixels");
134 continue;
135 }
136
137 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, gamma, error, &expected)) {
145 ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x "
146 "from src 0x%08x and gamma %f. Got %08x",
147 x, y, expected, s, gamma, r);
148 abort = true;
149 break;
brianosman2d1ee792016-05-05 12:24:31 -0700150 }
151 }
152 }
153 }
154 }
155}
156#endif