blob: 1d19b88ecdf3072505ee6369afc955e1e56e11ee [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
45static void testToRGB(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
46 rdr::U8 *dst, rdr::U8 *src)
47{
48 srcpf.rgbFromBuffer(dst, src, tile, fbsize, tile);
49}
50
51static void testFromRGB(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf,
52 rdr::U8 *dst, rdr::U8 *src)
53{
54 dstpf.bufferFromRGB(dst, src, tile, fbsize, tile);
55}
56
57static void doTest(testfn fn, rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf)
58{
59 if (!srcpf.isLittleEndian() && (fn == testPixelTransformer)) {
60 printf("NaN");
61 return;
62 }
63
64 startCpuCounter();
65
66 for (int i = 0;i < 10000;i++) {
67 int x, y;
68 rdr::U8 *dst, *src;
69 x = rand() % (fbsize - tile);
70 y = rand() % (fbsize - tile);
71 dst = fb1 + (x + y * fbsize) * dstpf.bpp/8;
72 src = fb2 + (x + y * fbsize) * srcpf.bpp/8;
73 fn(dstpf, srcpf, dst, src);
74 }
75
76 endCpuCounter();
77
78 float data, time;
79
80 data = (double)tile * tile * 10000;
81 time = getCpuCounter();
82
83 printf("%g", data / (1000.0*1000.0) / time);
84}
85
86struct TestEntry tests[] = {
87 {"memcpy", testMemcpy},
88 {"PixelTransformer", testPixelTransformer},
89 {"rgbFromBuffer", testToRGB},
90 {"bufferFromRGB", testFromRGB},
91};
92
93static void doTests(rfb::PixelFormat &dstpf, rfb::PixelFormat &srcpf)
94{
95 int i;
96 char dstb[256], srcb[256];
97
98 dstpf.print(dstb, sizeof(dstb));
99 srcpf.print(srcb, sizeof(srcb));
100
101 if (srcpf.isLittleEndian()) {
102 delete pt;
103 pt = new rfb::PixelTransformer;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100104 pt->init(srcpf, dstpf);
Pierre Ossman236c03c2014-07-04 14:12:49 +0200105 }
106
107 printf("%s,%s", srcb, dstb);
108
109 for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++) {
110 printf(",");
111 doTest(tests[i].fn, dstpf, srcpf);
112 }
113
114 printf("\n");
115}
116
117int main(int argc, char **argv)
118{
119 int bufsize;
120
121 time_t t;
122 char datebuffer[256];
123
124 int i;
125
126 bufsize = fbsize * fbsize * 4;
127
128 fb1 = new rdr::U8[bufsize];
129 fb2 = new rdr::U8[bufsize];
130
131 for (int i = 0;i < bufsize;i++) {
132 fb1[i] = rand();
133 fb2[i] = rand();
134 }
135
136 time(&t);
137 strftime(datebuffer, sizeof(datebuffer), "%Y-%m-%d %H:%M UTC", gmtime(&t));
138
139 printf("# Pixel Conversion Test %s\n", datebuffer);
140 printf("#\n");
141 printf("# Frame buffer: %dx%d pixels\n", fbsize, fbsize);
142 printf("# Tile size: %dx%d pixels\n", tile, tile);
143 printf("#\n");
144 printf("# Note: Results are Mpixels/sec\n");
145 printf("#\n");
146
147 printf("Source format,Destination Format");
148 for (i = 0;i < sizeof(tests)/sizeof(tests[0]);i++)
149 printf(",%s", tests[i].label);
150 printf("\n");
151
152 rfb::PixelFormat dstpf, srcpf;
153
154 /* rgb888 targets */
155
156 dstpf.parse("rgb888");
157
158 srcpf.parse("rgb888");
159 doTests(dstpf, srcpf);
160
161 srcpf.parse("bgr888");
162 doTests(dstpf, srcpf);
163
164 srcpf.parse("rgb565");
165 doTests(dstpf, srcpf);
166
167 srcpf.parse("rgb232");
168 doTests(dstpf, srcpf);
169
170 /* rgb565 targets */
171
172 dstpf.parse("rgb565");
173
174 srcpf.parse("rgb888");
175 doTests(dstpf, srcpf);
176
177 srcpf.parse("bgr565");
178 doTests(dstpf, srcpf);
179
180 srcpf.parse("rgb232");
181 doTests(dstpf, srcpf);
182
183 /* rgb565 with endian conversion (both ways) */
184
185 dstpf = rfb::PixelFormat(32, 24, false, true, 255, 255, 255, 0, 8, 16);
186 srcpf = rfb::PixelFormat(32, 24, true, true, 255, 255, 255, 0, 8, 16);
187
188 doTests(srcpf, dstpf);
189
190 doTests(dstpf, srcpf);
191
192 dstpf = rfb::PixelFormat(16, 16, false, true, 31, 63, 31, 0, 5, 11);
193 srcpf = rfb::PixelFormat(16, 16, true, true, 31, 63, 31, 0, 5, 11);
194
195 doTests(srcpf, dstpf);
196
197 doTests(dstpf, srcpf);
198
199 return 0;
200}
201