xref: /aosp_15_r20/external/skia/tests/FloatingPointTextureTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/GpuTypes.h"
20 #include "include/gpu/ganesh/GrDirectContext.h"
21 #include "include/gpu/ganesh/GrTypes.h"
22 #include "include/private/base/SkTDArray.h"
23 #include "include/private/gpu/ganesh/GrTypesPriv.h"
24 #include "src/base/SkHalf.h"
25 #include "src/gpu/ganesh/GrDirectContextPriv.h"
26 #include "src/gpu/ganesh/GrImageInfo.h"
27 #include "src/gpu/ganesh/GrPixmap.h"
28 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
29 #include "src/gpu/ganesh/SurfaceContext.h"
30 #include "tests/CtsEnforcement.h"
31 #include "tests/Test.h"
32 #include "tools/gpu/ProxyUtils.h"
33 
34 #include <cstring>
35 #include <initializer_list>
36 #include <memory>
37 #include <utility>
38 
39 struct GrContextOptions;
40 
41 static const int DEV_W = 100, DEV_H = 100;
42 
43 template <typename T>
runFPTest(skiatest::Reporter * reporter,GrDirectContext * dContext,T min,T max,T epsilon,T maxInt,int arraySize,GrColorType colorType)44 void runFPTest(skiatest::Reporter* reporter, GrDirectContext* dContext,
45                T min, T max, T epsilon, T maxInt,
46                int arraySize, GrColorType colorType) {
47     if (0 != arraySize % 4) {
48         REPORT_FAILURE(reporter, "(0 != arraySize % 4)",
49                        SkString("arraySize must be divisible by 4."));
50         return;
51     }
52 
53     SkTDArray<T> controlPixelData, readBuffer;
54     controlPixelData.resize(arraySize);
55     readBuffer.resize(arraySize);
56 
57     for (int i = 0; i < arraySize; i += 4) {
58         controlPixelData[i + 0] = min;
59         controlPixelData[i + 1] = max;
60         controlPixelData[i + 2] = epsilon;
61         controlPixelData[i + 3] = maxInt;
62     }
63 
64     for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
65         GrImageInfo info(colorType, kPremul_SkAlphaType, nullptr, {DEV_W, DEV_H});
66         GrCPixmap controlPixmap(info, controlPixelData.begin(), info.minRowBytes());
67         auto fpView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
68                                                                 GrRenderable::kYes,
69                                                                 origin,
70                                                                 controlPixmap);
71         // Floating point textures are NOT supported everywhere
72         if (!fpView) {
73             continue;
74         }
75 
76         auto sc = dContext->priv().makeSC(std::move(fpView), info.colorInfo());
77         REPORTER_ASSERT(reporter, sc);
78 
79         GrPixmap readPixmap(info, readBuffer.begin(), info.minRowBytes());
80         bool result = sc->readPixels(dContext, readPixmap, {0, 0});
81         REPORTER_ASSERT(reporter, result);
82         REPORTER_ASSERT(reporter,
83             !memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.size_bytes()));
84     }
85 }
86 
87 static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
88 static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800;  // 2 ^ 11
89 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)90 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,
91                                        reporter,
92                                        ctxInfo,
93                                        CtsEnforcement::kApiLevel_T) {
94     auto direct = ctxInfo.directContext();
95 
96     runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
97                       kMaxIntegerRepresentableInHalfFloatingPoint, HALF_ALPHA_CONTROL_ARRAY_SIZE,
98                       GrColorType::kAlpha_F16);
99 }
100 
101 static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
102 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)103 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,
104                                        reporter,
105                                        ctxInfo,
106                                        CtsEnforcement::kApiLevel_T) {
107     auto direct = ctxInfo.directContext();
108 
109     runFPTest<SkHalf>(reporter, direct, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
110                       kMaxIntegerRepresentableInHalfFloatingPoint, HALF_RGBA_CONTROL_ARRAY_SIZE,
111                       GrColorType::kRGBA_F16);
112 }
113