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 <float.h>
16 #include "Test.h"
17 
18 #if SK_SUPPORT_GPU
19 #include "GrContext.h"
20 #include "GrTexture.h"
21 #include "SkHalf.h"
22 
23 static const int DEV_W = 100, DEV_H = 100;
24 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
25 
26 template <typename T>
runFPTest(skiatest::Reporter * reporter,GrContext * context,T min,T max,T epsilon,T maxInt,int arraySize,GrPixelConfig config)27 void runFPTest(skiatest::Reporter* reporter, GrContext* context,
28                T min, T max, T epsilon, T maxInt, int arraySize, GrPixelConfig config) {
29     SkTDArray<T> controlPixelData, readBuffer;
30     controlPixelData.setCount(arraySize);
31     readBuffer.setCount(arraySize);
32 
33     for (int i = 0; i < arraySize; i += 4) {
34         controlPixelData[i + 0] = min;
35         controlPixelData[i + 1] = max;
36         controlPixelData[i + 2] = epsilon;
37         controlPixelData[i + 3] = maxInt;
38     }
39 
40     for (int origin = 0; origin < 2; ++origin) {
41         GrSurfaceDesc desc;
42         desc.fFlags = kRenderTarget_GrSurfaceFlag;
43         desc.fWidth = DEV_W;
44         desc.fHeight = DEV_H;
45         desc.fConfig = config;
46         desc.fOrigin = 0 == origin ?
47             kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
48         SkAutoTUnref<GrTexture> fpTexture(context->textureProvider()->createTexture(
49             desc, SkBudgeted::kNo, controlPixelData.begin(), 0));
50         // Floating point textures are NOT supported everywhere
51         if (nullptr == fpTexture) {
52             continue;
53         }
54         fpTexture->readPixels(0, 0, DEV_W, DEV_H, desc.fConfig, readBuffer.begin(), 0);
55         REPORTER_ASSERT(reporter,
56                         0 == memcmp(readBuffer.begin(), controlPixelData.begin(), readBuffer.bytes()));
57     }
58 }
59 
60 static const int FP_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4/*RGBA*/;
61 static const float kMaxIntegerRepresentableInSPFloatingPoint = 16777216;  // 2 ^ 24
62 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest,reporter,context)63 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(FloatingPointTextureTest, reporter, context) {
64     runFPTest<float>(reporter, context, FLT_MIN, FLT_MAX, FLT_EPSILON,
65                      kMaxIntegerRepresentableInSPFloatingPoint,
66                      FP_CONTROL_ARRAY_SIZE, kRGBA_float_GrPixelConfig);
67 }
68 
69 static const int HALF_ALPHA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 1 /*alpha-only*/;
70 static const SkHalf kMaxIntegerRepresentableInHalfFloatingPoint = 0x6800;  // 2 ^ 11
71 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest,reporter,context)72 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatAlphaTextureTest, reporter, context) {
73     runFPTest<SkHalf>(reporter, context, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
74         kMaxIntegerRepresentableInHalfFloatingPoint,
75         HALF_ALPHA_CONTROL_ARRAY_SIZE, kAlpha_half_GrPixelConfig);
76 }
77 
78 static const int HALF_RGBA_CONTROL_ARRAY_SIZE = DEV_W * DEV_H * 4 /*RGBA*/;
79 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest,reporter,context)80 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(HalfFloatRGBATextureTest, reporter, context) {
81     runFPTest<SkHalf>(reporter, context, SK_HalfMin, SK_HalfMax, SK_HalfEpsilon,
82         kMaxIntegerRepresentableInHalfFloatingPoint,
83         HALF_RGBA_CONTROL_ARRAY_SIZE, kRGBA_half_GrPixelConfig);
84 }
85 
86 #endif
87