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