blob: a30ed909e3775e4103b940137e75f480ec6b5059 [file] [log] [blame]
Pierre Ossman236c03c2014-07-04 14:12:49 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <time.h>
5
6#include <rfb/PixelFormat.h>
7#include <rfb/PixelTransformer.h>
8
9#include "util.h"
10
11static const int tile = 64;
12static const int fbsize = 4096;
13
14static rdr::U8 *fb1, *fb2;
15
16static rfb::PixelTransformer *pt = NULL;
17
18typedef void (*testfn) (rfb::PixelFormat&, rfb::PixelFormat&, rdr::U8*, rdr::U8*);
19
20struct TestEntry {
21 const char *label;
22 testfn fn;
23};
24
25static void testMemcpy(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
26 rdr::U8 *dst, rdr::U8 *src)
27{
28 int h;
29 h = tile;
30 while (h--) {
31 memcpy(dst, src, tile * dstpf.bpp/8);
32 dst += fbsize * dstpf.bpp/8;
33 src += fbsize * dstpf.bpp/8;
34 }
35}
36
37static void testPixelTransformer(rfb::PixelFormat &dstpf,
38 rfb::PixelFormat &srcpf,
39 rdr::U8 *dst, rdr::U8 *src)
40{
41 pt->translateRect(src, fbsize, rfb::Rect(0, 0, tile, tile),
42 dst, fbsize, rfb::Point(0, 0));
43}
44
Pierre Ossman761fe242014-01-29 17:00:36 +010045static void testBuffer(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
46 rdr::U8 *dst, rdr::U8 *src)
47{
48 dstpf.bufferFromBuffer(dst, srcpf, src, tile, tile, fbsize, fbsize);
49}
50
Pierre Ossman236c03c2014-07-04 14:12:49 +020051static void testToRGB(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
52 rdr::U8 *dst, rdr::U8 *src)
53{
54 srcpf.rgbFromBuffer(dst, src, tile, fbsize, tile);
55}
56
57static void testFromRGB(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
58 rdr::U8 *dst, rdr::U8 *src)
59{
60 dstpf.bufferFromRGB(dst, src, tile, fbsize, tile);
61}
62
63static void doTest(testfn fn, rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf)
64{
65 if (!srcpf.isLittleEndian() && (fn == testPixelTransformer)) {
66 printf("NaN");
67 return;
68 }
69
70 startCpuCounter();
71
72 for (int i = 0;i < 10000;i++) {
73 int x, y;
74 rdr::U8 *dst, *src;
75 x = rand() % (fbsize - tile);
76 y = rand() % (fbsize - tile);
77 dst = fb1 + (x + y * fbsize) * dstpf.bpp/8;
78 src = fb2 + (x + y * fbsize) * srcpf.bpp/8;
79 fn(dstpf, srcpf, dst, src);
80 }
81
82 endCpuCounter();
83
84 float data, time;
85
86 data = (double)tile * tile * 10000;
87 time = getCpuCounter();
88
89 printf("%g", data / (1000.0*1000.0) / time);
90}
91
92struct TestEntry tests[] = {
93 {"memcpy", testMemcpy},
94 {"PixelTransformer", testPixelTransformer},
Pierre Ossman761fe242014-01-29 17:00:36 +010095 {"bufferFromBuffer", testBuffer},
Pierre Ossman236c03c2014-07-04 14:12:49 +020096 {"rgbFromBuffer", testToRGB},
97 {"bufferFromRGB", testFromRGB},
98};
99
100static void doTests(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf)
101{
102 int i;
103 char dstb[256], srcb[256];
104
105 dstpf.print(dstb, sizeof(dstb));
106 srcpf.print(srcb, sizeof(srcb));
107
108 if (srcpf.isLittleEndian()) {
109 delete pt;
110 pt = new rfb::PixelTransformer;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100111 pt->init(srcpf, dstpf);
Pierre Ossman236c03c2014-07-04 14:12:49 +0200112 }
113
114 printf("%s,%s", srcb, dstb);
115
116 for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++) {
117 printf(",");
118 doTest(tests[i].fn, dstpf, srcpf);
119 }
120
121 printf("\n");
122}
123
124int main(int argc, char **argv)
125{
126 int bufsize;
127
128 time_t t;
129 char datebuffer[256];
130
131 int i;
132
133 bufsize = fbsize * fbsize * 4;
134
135 fb1 = new rdr::U8[bufsize];
136 fb2 = new rdr::U8[bufsize];
137
138 for (int i = 0;i < bufsize;i++) {
139 fb1[i] = rand();
140 fb2[i] = rand();
141 }
142
143 time(&t);
144 strftime(datebuffer, sizeof(datebuffer), "%Y-%m-%d %H:%M UTC", gmtime(&t));
145
146 printf("# Pixel Conversion Test %s\n", datebuffer);
147 printf("#\n");
148 printf("# Frame buffer: %dx%d pixels\n", fbsize, fbsize);
149 printf("# Tile size: %dx%d pixels\n", tile, tile);
150 printf("#\n");
151 printf("# Note: Results are Mpixels/sec\n");
152 printf("#\n");
153
154 printf("Source format,Destination Format");
155 for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++)
156 printf(",%s", tests[i].label);
157 printf("\n");
158
159 rfb::PixelFormat dstpf, srcpf;
160
161 /* rgb888 targets */
162
Pierre Ossman00bed5c2014-07-09 14:12:39 +0200163 printf("\n");
164
Pierre Ossman236c03c2014-07-04 14:12:49 +0200165 dstpf.parse("rgb888");
166
167 srcpf.parse("rgb888");
168 doTests(dstpf, srcpf);
169
170 srcpf.parse("bgr888");
171 doTests(dstpf, srcpf);
172
173 srcpf.parse("rgb565");
174 doTests(dstpf, srcpf);
175
176 srcpf.parse("rgb232");
177 doTests(dstpf, srcpf);
178
179 /* rgb565 targets */
180
Pierre Ossman00bed5c2014-07-09 14:12:39 +0200181 printf("\n");
182
Pierre Ossman236c03c2014-07-04 14:12:49 +0200183 dstpf.parse("rgb565");
184
185 srcpf.parse("rgb888");
186 doTests(dstpf, srcpf);
187
188 srcpf.parse("bgr565");
189 doTests(dstpf, srcpf);
190
191 srcpf.parse("rgb232");
192 doTests(dstpf, srcpf);
193
Pierre Ossmane18132c2014-07-09 14:12:12 +0200194 /* rgb232 targets */
195
Pierre Ossman00bed5c2014-07-09 14:12:39 +0200196 printf("\n");
197
Pierre Ossmane18132c2014-07-09 14:12:12 +0200198 dstpf.parse("rgb232");
199
200 srcpf.parse("rgb888");
201 doTests(dstpf, srcpf);
202
203 srcpf.parse("rgb565");
204 doTests(dstpf, srcpf);
205
206 srcpf.parse("bgr232");
207 doTests(dstpf, srcpf);
208
Pierre Ossman236c03c2014-07-04 14:12:49 +0200209 /* rgb565 with endian conversion (both ways) */
210
Pierre Ossman00bed5c2014-07-09 14:12:39 +0200211 printf("\n");
212
Pierre Ossman236c03c2014-07-04 14:12:49 +0200213 dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
214 srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 8, 16);
215
216 doTests(srcpf, dstpf);
217
218 doTests(dstpf, srcpf);
219
220 dstpf = rfb::PixelFormat(16, 16, false, true, 31, 63, 31, 0, 5, 11);
221 srcpf = rfb::PixelFormat(16, 16, true, true, 31, 63, 31, 0, 5, 11);
222
223 doTests(srcpf, dstpf);
224
225 doTests(dstpf, srcpf);
226
227 return 0;
228}
229