brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 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" |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 13 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
robertphillips | 1e87618 | 2016-07-29 08:28:25 -0700 | [diff] [blame] | 15 | #include "SkGammaColorFilter.h" |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 16 | #include "SkPixmap.h" |
| 17 | #include "SkSurface.h" |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 18 | #include "SkUtils.h" |
| 19 | |
| 20 | // using anonymous namespace because these functions are used as template params. |
| 21 | namespace { |
| 22 | /** convert 0..1 linear value to 0..1 srgb */ |
| 23 | float linear_to_srgb(float linear) { |
| 24 | if (linear <= 0.0031308) { |
| 25 | return linear * 12.92f; |
| 26 | } else { |
| 27 | return 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | bool check_gamma(uint32_t src, uint32_t dst, float gamma, float error, uint32_t* expected) { |
| 33 | if (SkScalarNearlyEqual(gamma, 1.f)) { |
| 34 | *expected = src; |
| 35 | return src == dst; |
| 36 | } else { |
| 37 | bool result = true; |
| 38 | uint32_t expectedColor = src & 0xff000000; |
| 39 | |
| 40 | // Alpha should always be exactly preserved. |
| 41 | if ((src & 0xff000000) != (dst & 0xff000000)) { |
| 42 | result = false; |
| 43 | } |
| 44 | |
| 45 | for (int c = 0; c < 3; ++c) { |
| 46 | uint8_t srcComponent = (src & (0xff << (c * 8))) >> (c * 8); |
| 47 | float lower = SkTMax(0.f, (float)srcComponent - error); |
| 48 | float upper = SkTMin(255.f, (float)srcComponent + error); |
| 49 | if (SkScalarNearlyEqual(gamma, 1.0f / 2.2f)) { |
| 50 | lower = linear_to_srgb(lower / 255.f); |
| 51 | upper = linear_to_srgb(upper / 255.f); |
| 52 | } else { |
| 53 | lower = powf(lower / 255.f, gamma); |
| 54 | upper = powf(upper / 255.f, gamma); |
| 55 | } |
| 56 | SkASSERT(lower >= 0.f && lower <= 255.f); |
| 57 | SkASSERT(upper >= 0.f && upper <= 255.f); |
| 58 | uint8_t dstComponent = (dst & (0xff << (c * 8))) >> (c * 8); |
| 59 | if (dstComponent < SkScalarFloorToInt(lower * 255.f) || |
| 60 | dstComponent > SkScalarCeilToInt(upper * 255.f)) { |
| 61 | result = false; |
| 62 | } |
| 63 | uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 127.5f); |
| 64 | expectedColor |= expectedComponent << (c * 8); |
| 65 | } |
| 66 | |
| 67 | *expected = expectedColor; |
| 68 | return result; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 73 | GrContext* context = ctxInfo.grContext(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 74 | static const int kW = 10; |
| 75 | static const int kH = 10; |
| 76 | static const size_t kRowBytes = sizeof(uint32_t) * kW; |
| 77 | |
| 78 | GrSurfaceDesc baseDesc; |
| 79 | baseDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 80 | baseDesc.fWidth = kW; |
| 81 | baseDesc.fHeight = kH; |
| 82 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 83 | const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH); |
| 84 | |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 85 | SkAutoTMalloc<uint32_t> srcPixels(kW * kH); |
| 86 | for (int i = 0; i < kW * kH; ++i) { |
| 87 | srcPixels.get()[i] = i; |
| 88 | } |
| 89 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 90 | SkPixmap pm(ii, srcPixels.get(), kRowBytes); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 91 | |
| 92 | SkAutoTMalloc<uint32_t> read(kW * kH); |
| 93 | |
| 94 | // We allow more error on GPUs with lower precision shader variables. |
| 95 | float error = context->caps()->shaderCaps()->floatPrecisionVaries() ? 1.2f : 0.5f; |
| 96 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 97 | for (auto dOrigin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) { |
| 98 | for (auto gamma : { 1.0f, 1.0f / 1.8f, 1.0f / 2.2f }) { |
| 99 | sk_sp<SkImage> src(SkImage::MakeTextureFromPixmap(context, pm, SkBudgeted::kNo)); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 100 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 101 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, |
| 102 | ii, 0, dOrigin, nullptr)); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 103 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 104 | if (!src || !dst) { |
| 105 | ERRORF(reporter, "Could not create surfaces for copy surface test."); |
| 106 | continue; |
| 107 | } |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 108 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 109 | SkCanvas* dstCanvas = dst->getCanvas(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 110 | |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 111 | dstCanvas->clear(SK_ColorRED); |
| 112 | dstCanvas->flush(); |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 113 | |
robertphillips | 1e87618 | 2016-07-29 08:28:25 -0700 | [diff] [blame] | 114 | SkPaint gammaPaint; |
| 115 | gammaPaint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 116 | gammaPaint.setColorFilter(SkGammaColorFilter::Make(gamma)); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 117 | |
robertphillips | 1e87618 | 2016-07-29 08:28:25 -0700 | [diff] [blame] | 118 | dstCanvas->drawImage(src, 0, 0, &gammaPaint); |
| 119 | dstCanvas->flush(); |
robertphillips | ecf3dbe | 2016-07-28 15:17:34 -0700 | [diff] [blame] | 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, gamma, error, &expected)) { |
| 135 | ERRORF(reporter, "Expected dst %d,%d to contain 0x%08x " |
| 136 | "from src 0x%08x and gamma %f. Got %08x", |
| 137 | x, y, expected, s, gamma, r); |
| 138 | abort = true; |
| 139 | break; |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | #endif |