1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <array>
#include <memory>
#include <gtest/gtest.h>
#include "GL/osmesa.h"
typedef std::array<GLenum, 2> Params;
class OSMesaRenderTestFixture : public testing::TestWithParam<Params> {};
std::string
name_params(const testing::TestParamInfo<Params> params) {
auto p = params.param;
std::string first, second;
switch (p[0]) {
case OSMESA_RGBA:
first = "rgba";
break;
case OSMESA_BGRA:
first = "bgra";
break;
case OSMESA_RGB:
first = "rgb";
break;
case OSMESA_RGB_565:
first = "rgb_565";
break;
case OSMESA_ARGB:
first = "argb";
break;
}
switch (p[1]) {
case GL_UNSIGNED_SHORT:
second = "unsigned_short";
break;
case GL_UNSIGNED_BYTE:
second = "unsigned_byte";
break;
case GL_FLOAT:
second = "float";
break;
case GL_UNSIGNED_SHORT_5_6_5:
second = "unsigned_short_565";
break;
}
return first + "_" + second;
};
TEST_P(OSMesaRenderTestFixture, Render)
{
auto params = GetParam();
const int w = 2, h = 2;
uint8_t pixels[w * h * 4] = { 0 };
uint32_t expected; // This should be green for the given color model
std::unique_ptr<osmesa_context, decltype(&OSMesaDestroyContext)> ctx{
OSMesaCreateContext(params[0], NULL), &OSMesaDestroyContext};
ASSERT_TRUE(ctx);
auto ret = OSMesaMakeCurrent(ctx.get(), &pixels, params[1], w, h);
ASSERT_EQ(ret, GL_TRUE);
int bpp = 4;
switch (params[0]) {
case OSMESA_RGB:
bpp = 3;
break;
case OSMESA_RGB_565:
bpp = 2;
break;
}
switch (params[0]) {
case OSMESA_RGBA:
case OSMESA_BGRA:
case OSMESA_RGB:
expected = 0xff << 8;
glClearColor(0, 1, 0, 0);
break;
case OSMESA_RGB_565:
expected = 0x3f << 5;
glClearColor(0, 1, 0, 0);
break;
case OSMESA_ARGB:
expected = 0xff << 24;
glClearColor(0, 0, 1, 0);
break;
}
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
for (unsigned i = 0; i < w * h; i++) {
uint32_t color = 0;
memcpy(&color, &pixels[i * bpp], bpp);
ASSERT_EQ(expected, color);
}
}
INSTANTIATE_TEST_CASE_P(
OSMesaRenderTest,
OSMesaRenderTestFixture,
testing::Values(
Params{ OSMESA_RGBA, GL_UNSIGNED_BYTE },
Params{ OSMESA_BGRA, GL_UNSIGNED_BYTE },
Params{ OSMESA_ARGB, GL_UNSIGNED_BYTE },
Params{ OSMESA_RGB, GL_UNSIGNED_BYTE }
),
name_params
);
|