• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 /*
9  * This is a straightforward test of floating point textures, which are
10  * supported on some platforms.  As of right now, this test only supports
11  * 32 bit floating point textures, and indeed floating point test values
12  * have been selected to require 32 bits of precision and full IEEE conformance
13  */
14 
15 #include "include/core/SkAlphaType.h"
16 #include "include/core/SkColorSpace.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkString.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "include/gpu/GrTypes.h"
21 #include "include/private/base/SkTDArray.h"
22 #include "include/private/gpu/ganesh/GrTypesPriv.h"
23 #include "src/base/SkHalf.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrImageInfo.h"
26 #include "src/gpu/ganesh/GrPixmap.h"
27 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
28 #include "src/gpu/ganesh/SurfaceContext.h"
29 #include "tests/CtsEnforcement.h"
30 #include "tests/Test.h"
31 #include "tools/gpu/ProxyUtils.h"
32 
33 #include <cstring>
34 #include <initializer_list>
35 #include <memory>
36 #include <utility>
37 
38 struct GrContextOptions;
39 
40 static const int DEV_W = 100, DEV_H = 100;
41 
42 template <typename T>
runFPTest(skiatest::Reporter * reporter,GrDirectContext * dContext,T min,T max,T epsilon,T maxInt,int arraySize,GrColorType colorType)43 void runFPTest(skiatest::Reporter* reporter, GrDirectContext* dContext,
44                T min, T max, T epsilon, T maxInt,
45                int arraySize, GrColorType colorType) {
46     if (0 != arraySize % 4) {
47         REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
48                        SkString("arraySize must be divisible by 4."));
49         return;
50     }
51 
52     SkTDArray<T> controlPixelData, readBuffer;
53     controlPixelData.resize(arraySize);
54     readBuffer.resize(arraySize);
55 
56     for (int i = 0; i < arraySize; i += 4) {
57         controlPixelData[i + 0] = min;
58         controlPixelData[i + 1] = max;
59         controlPixelData[i + 2] = epsilon;
60         controlPixelData[i + 3] = maxInt;
61     }
62 
63     for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
64         GrImageInfo info(colorType, kPremul_SkAlphaType, nullptr, {DEV_W, DEV_H});
65         GrCPixmap controlPixmap(info, controlPixelData.begin(), info.minRowBytes());
66         auto fpView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
67                                                                 GrRenderable::kYes,
68                                                                 origin,
69                                                                 controlPixmap);
70         // Floating point textures are NOT supported everywhere
71         if (!fpView) {
72             continue;
73         }
74 
75         auto sc = dContext->priv().makeSC(std::move(fpView), info.colorInfo());
76         REPORTER_ASSERT(reporter, sc);
77 
78         GrPixmap readPixmap(info, readBuffer.begin(), info.minRowBytes());
79         bool result = sc->readPixels(dContext, readPixmap, {0, 0});
80         REPORTER_ASSERT(reporter, result);
81         REPORTER_ASSERT(reporter,
82             !memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.size_bytes()));
83     }
84 }
85 
86 static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
87 static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800;  // 2 ^ 11
88 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)89 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,
90                                        reporter,
91                                        ctxInfo,
92                                        CtsEnforcement::kApiLevel_T) {
93     auto direct = ctxInfo.directContext();
94 
95     runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
96                       kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
97                       GrColorType::kAlpha_F16);
98 }
99 
100 static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
101 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)102 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,
103                                        reporter,
104                                        ctxInfo,
105                                        CtsEnforcement::kApiLevel_T) {
106     auto direct = ctxInfo.directContext();
107 
108     runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
109                       kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
110                       GrColorType::kRGBA_F16);
111 }
112