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 19 /** convert 0..1 linear value to 0..1 srgb */ 20 static float linear_to_srgb(float linear) { 21 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 } 27 28 /** convert 0..1 srgb value to 0..1 linear */ 29 static 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 } 35 } 36 37 bool 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; 41 42 // Alpha should always be exactly preserved. 43 if ((src & 0xff000000) != (dst & 0xff000000)) { 44 result = false; 45 } 46 47 // 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 54 for (int c = 0; c < 3; ++c) { 55 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); 58 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 } 65 lower *= alpha; 66 upper *= alpha; 67 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); 70 if (dstComponent < SkScalarFloorToInt(lower) || 71 dstComponent > SkScalarCeilToInt(upper)) { 72 result = false; 73 } 74 uint8_t expectedComponent = SkScalarRoundToInt((lower + upper) * 0.5f); 75 expectedColor |= expectedComponent << (c * 8); 76 } 77 78 *expected = expectedColor; 79 return result; 80 } 81 82 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ApplyGamma, reporter, ctxInfo) { 83 GrContext* context = ctxInfo.grContext(); 84 static const int kW = 256; 85 static const int kH = 256; 86 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 93 const SkImageInfo ii = SkImageInfo::MakeN32Premul(kW, kH); 94 95 SkAutoTMalloc<uint32_t> srcPixels(kW * kH); 96 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 } 100 } 101 102 SkBitmap bm; 103 bm.installPixels(ii, srcPixels.get(), kRowBytes); 104 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()->halfIs32Bits() ? 0.5f : 1.2f; 109 110 for (auto toSRGB : { false, true }) { 111 sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); 112 113 if (!dst) { 114 ERRORF(reporter, "Could not create surfaces for copy surface test."); 115 continue; 116 } 117 118 SkCanvas* dstCanvas = dst->getCanvas(); 119 120 dstCanvas->clear(SK_ColorRED); 121 dstCanvas->flush(); 122 123 SkPaint gammaPaint; 124 gammaPaint.setBlendMode(SkBlendMode::kSrc); 125 gammaPaint.setColorFilter(toSRGB ? SkColorFilter::MakeLinearToSRGBGamma() 126 : SkColorFilter::MakeSRGBToLinearGamma()); 127 128 dstCanvas->drawBitmap(bm, 0, 0, &gammaPaint); 129 dstCanvas->flush(); 130 131 sk_memset32(read.get(), 0, kW * kH); 132 if (!dst->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, 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; 150 } 151 } 152 } 153 } 154 } 155 #endif 156