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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
// Test software OpenGL hardware accelleration using copybits.
#define LOG_TAG "copybits_test"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <ui/PixelFormat.h>
#include <cutils/log.h>
#include <cutils/native_handle.h>
#include <utils/Atomic.h>
#include <private/ui/SurfaceBuffer.h>
#include <pixelflinger/pixelflinger.h>
#include <hardware/gralloc.h>
#include <hardware/hardware.h>
#include <ui/FramebufferNativeWindow.h>
#include <ui/EGLUtils.h>
#define EGL_EGLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
using namespace android;
EGLDisplay eglDisplay;
EGLSurface eglSurface;
EGLContext eglContext;
GLuint texture;
hw_module_t const* gralloc_module;
alloc_device_t *sAllocDev;
#define FIXED_ONE 0x10000 /* 1.0 in 16.16 fixed point. */
int init_gl_surface();
void free_gl_surface();
void init_scene();
int create_physical_texture();
int readTimer();
// ===========================================================================
// Buffer an implementation of android_native_buffer_t
// ===========================================================================
class NativeBuffer;
class Buffer : public android::SurfaceBuffer
{
public:
// creates w * h buffer
Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
// return status
status_t initCheck() const;
uint32_t getWidth() const { return width; }
uint32_t getHeight() const { return height; }
uint32_t getStride() const { return stride; }
uint32_t getUsage() const { return usage; }
PixelFormat getPixelFormat() const { return format; }
android_native_buffer_t* getNativeBuffer() const;
void setPixel(int x, int y, int r, int g, int b, int a);
status_t lock(GGLSurface* surface, uint32_t usage);
void lock() {
GGLSurface s;
lock(&s, GRALLOC_USAGE_SW_WRITE_OFTEN);
mData = (void*)s.data;
}
private:
friend class LightRefBase<Buffer>;
Buffer(const Buffer& rhs);
virtual ~Buffer();
Buffer& operator = (const Buffer& rhs);
const Buffer& operator = (const Buffer& rhs) const;
status_t initSize(uint32_t w, uint32_t h);
ssize_t mInitCheck;
void* mData;
};
Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage)
: SurfaceBuffer(), mInitCheck(NO_INIT)
{
this->usage = usage;
this->format = format;
if (w>0 && h>0) {
mInitCheck = initSize(w, h);
}
}
Buffer::~Buffer()
{
if (handle) {
sAllocDev->free(sAllocDev, handle);
}
}
status_t Buffer::initCheck() const {
return mInitCheck;
}
android_native_buffer_t* Buffer::getNativeBuffer() const
{
return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this));
}
status_t Buffer::initSize(uint32_t w, uint32_t h)
{
status_t err = NO_ERROR;
err = sAllocDev->alloc(sAllocDev, w, h, format, usage, &handle, &stride);
if (err == NO_ERROR) {
if (err == NO_ERROR) {
width = w;
height = h;
}
}
return err;
}
status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
{
void* vaddr;
status_t res = SurfaceBuffer::lock(usage, &vaddr);
if (res == NO_ERROR && sur) {
sur->version = sizeof(GGLSurface);
sur->width = width;
sur->height = height;
sur->stride = stride;
sur->format = format;
sur->data = static_cast<GGLubyte*>(vaddr);
}
return res;
}
void Buffer::setPixel(int x, int y, int r, int g, int b, int a) {
if (x < 0 || (unsigned int) x >= width
|| y < 0 || (unsigned int) y >= height) {
// clipped
return;
}
int index = stride * y + x;
switch (format) {
case HAL_PIXEL_FORMAT_RGB_565: {
unsigned short val = (unsigned short) (
((0x1f & (r >> 3)) << 11)
| ((0x3f & (g >> 2)) << 5)
| (0x1f & (b >> 3)));
((unsigned short*) mData)[index]= val;
}
break;
case HAL_PIXEL_FORMAT_RGBA_8888: { // ABGR
unsigned int val = (unsigned int)
(((a & 0xff) << 24)
| ((b & 0xff) << 16)
| ((g & 0xff) << 8)
| (r & 0xff));
((unsigned int*) mData)[index] = val;
}
break;
default:
// Unsupported pixel format
break;
}
}
static void gluLookAt(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ, float upX, float upY,
float upZ)
{
// See the OpenGL GLUT documentation for gluLookAt for a description
// of the algorithm. We implement it in a straightforward way:
float fx = centerX - eyeX;
float fy = centerY - eyeY;
float fz = centerZ - eyeZ;
// Normalize f
float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz);
fx *= rlf;
fy *= rlf;
fz *= rlf;
// Normalize up
float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ);
upX *= rlup;
upY *= rlup;
upZ *= rlup;
// compute s = f x up (x means "cross product")
float sx = fy * upZ - fz * upY;
float sy = fz * upX - fx * upZ;
float sz = fx * upY - fy * upX;
// compute u = s x f
float ux = sy * fz - sz * fy;
float uy = sz * fx - sx * fz;
float uz = sx * fy - sy * fx;
float m[16] ;
m[0] = sx;
m[1] = ux;
m[2] = -fx;
m[3] = 0.0f;
m[4] = sy;
m[5] = uy;
m[6] = -fy;
m[7] = 0.0f;
m[8] = sz;
m[9] = uz;
m[10] = -fz;
m[11] = 0.0f;
m[12] = 0.0f;
m[13] = 0.0f;
m[14] = 0.0f;
m[15] = 1.0f;
glMultMatrixf(m);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
int init_gralloc() {
int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &gralloc_module);
LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
if (err == 0) {
gralloc_open(gralloc_module, &sAllocDev);
}
return err;
}
int init_gl_surface(void)
{
EGLint numConfigs = 1;
EGLConfig myConfig = {0};
EGLint attrib[] =
{
EGL_DEPTH_SIZE, 16,
EGL_NONE
};
EGLNativeWindowType window = android_createDisplaySurface();
printf("init_gl_surface\n");
if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY )
{
printf("eglGetDisplay failed\n");
return 0;
}
if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE )
{
printf("eglInitialize failed\n");
return 0;
}
if ( EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig) != 0)
{
printf("EGLUtils::selectConfigForNativeWindow failed\n");
return 0;
}
if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig,
window, 0)) == EGL_NO_SURFACE )
{
printf("eglCreateWindowSurface failed\n");
return 0;
}
if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT )
{
printf("eglCreateContext failed\n");
return 0;
}
if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE )
{
printf("eglMakeCurrent failed\n");
return 0;
}
#if EGL_ANDROID_swap_rectangle
eglSetSwapRectangleANDROID(eglDisplay, eglSurface, 0, 0, 320, 480);
#endif
return 1;
}
void free_gl_surface(void)
{
if (eglDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent( eglDisplay, EGL_NO_SURFACE,
EGL_NO_SURFACE, EGL_NO_CONTEXT );
eglDestroyContext( eglDisplay, eglContext );
eglDestroySurface( eglDisplay, eglSurface );
eglTerminate( eglDisplay );
eglDisplay = EGL_NO_DISPLAY;
}
}
void init_scene(void)
{
glDisable(GL_DITHER);
glEnable(GL_CULL_FACE);
float ratio = 320.0f / 480.0f;
glViewport(0, 0, 320, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustumf(-ratio, ratio, -1, 1, 1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0, 0, 3, // eye
0, 0, 0, // center
0, 1, 0); // up
glEnable(GL_TEXTURE_2D);
}
// #define USE_ALPHA_COLOR
#define USE_GL_REPLACE
//#define USE_GL_MODULATE
// #define USE_BLEND
#define USE_565
// #define USE_8888
// #define USE_NEAREST
#define USE_LINEAR
#define USE_SCALE
void setSmoothGradient(Buffer* bufferObject) {
int pixels = bufferObject->getHeight() * bufferObject->getWidth();
int step = 0;
for (unsigned int y = 0; y < bufferObject->getHeight(); y++) {
for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) {
int grey = step * 255 / pixels;
bufferObject->setPixel(x, y, grey, grey, grey, 255);
++step;
}
}
}
void setSmoothAlphaGradient(Buffer* bufferObject) {
int pixels = bufferObject->getHeight() * bufferObject->getWidth();
int step = 0;
for (unsigned int y = 0; y < bufferObject->getHeight(); y++) {
for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) {
int grey = step * 255 / pixels;
bufferObject->setPixel(x, y, 255, 255, 255, grey);
++step;
}
}
}
void setOrientedCheckerboard(Buffer* bufferObject) {
bufferObject->setPixel(0, 0, 0, 0, 0, 255);
for(unsigned int x = 1; x < bufferObject->getWidth() ; x++) {
bufferObject->setPixel(x, 0, 0, 255, 0, 255);
}
for (unsigned int y = 1; y < bufferObject->getHeight(); y++) {
for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) {
if ((x ^ y ) & 1) {
bufferObject->setPixel(x, y, 255, 255, 255, 255);
} else {
bufferObject->setPixel(x, y, 255, 0, 0, 255);
}
}
}
}
int create_physical_texture(unsigned int w, unsigned int h)
{
#ifdef USE_565
PixelFormat format = HAL_PIXEL_FORMAT_RGB_565;
#else
PixelFormat format = HAL_PIXEL_FORMAT_RGBA_8888;
#endif
int usage = GRALLOC_USAGE_SW_READ_OFTEN |
GRALLOC_USAGE_SW_WRITE_OFTEN |
GRALLOC_USAGE_HW_TEXTURE |
GRALLOC_USAGE_HW_2D; /* This is the key to allocating the texture in pmem. */
int32_t stride;
buffer_handle_t handle;
// Allocate the hardware buffer
Buffer* bufferObject = new Buffer(w, h, format, usage);
android_native_buffer_t* buffer = bufferObject->getNativeBuffer();
buffer->common.incRef(&buffer->common);
// create the new EGLImageKHR
EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_NONE };
EGLDisplay dpy = eglGetCurrentDisplay();
EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
(EGLClientBuffer)buffer, attrs);
if (image == EGL_NO_IMAGE_KHR) {
printf("Could not create an image %d\n", eglGetError());
return -1;
}
bufferObject->lock();
setOrientedCheckerboard(bufferObject);
// setSmoothGradient(bufferObject);
// setSmoothAlphaGradient(bufferObject);
bufferObject->unlock();
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
#ifdef USE_LINEAR
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#elif defined(USE_NEAREST)
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
#endif
#ifdef USE_GL_REPLACE
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
#elif defined(USE_GL_MODULATE)
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
#ifdef USE_ALPHA_COLOR
glColor4f(1.0f, 1.0f, 1.0f, 0.4f);
#else
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
#endif
#ifdef USE_BLEND
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#endif
return 0;
}
static const int SCALE_COUNT = 12;
int scale(int base, int factor) {
static const float kTable[SCALE_COUNT] = {
0.24f, 0.25f, 0.5f, 0.75f, 1.0f,
1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 5.0f
};
return base * kTable[factor];
}
class Timer {
struct timeval first;
double elapsedSeconds;
public:
Timer() {}
void start() {
gettimeofday(&first, NULL);
}
void stop() {
struct timeval second,
elapsed;
gettimeofday(&second, NULL);
if (first.tv_usec > second.tv_usec) {
second.tv_usec += 1000000;
second.tv_sec--;
}
elapsedSeconds = (second.tv_sec - first.tv_sec) +
(second.tv_usec - first.tv_usec) / 1000000.0;
}
double getElapsedSeconds() {
return elapsedSeconds;
}
double getElapsedMs() {
return elapsedSeconds* 1000.0f;
}
};
int testTime()
{
static const int WIDTH = 320;
static const int HEIGHT = 480;
static const int SCALE = 8;
if (create_physical_texture(WIDTH, HEIGHT) != 0) {
return -1;
}
// Need to do a dummy eglSwapBuffers first. Don't know why.
glClearColor(0.4, 1.0, 0.4, 0.4);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
eglSwapBuffers(eglDisplay, eglSurface);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
#if defined(USE_SCALE)
static const int scaleOffset = 0;
#else
static const int scaleOffset = 1;
#endif
printf("ms\n");
for(int j = 0; j < SCALE; j++) {
int w = WIDTH >> (j + scaleOffset);
int h = HEIGHT >> j;
int cropRect[4] = {0,h,w,-h}; // Left bottom width height. Width and Height can be neg to flip.
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
Timer timer;
timer.start();
int copyCount = 1000;
for (int i = 0; i < copyCount; i++) {
glDrawTexiOES(0, 0, 0, w, h);
}
timer.stop();
printf("%g\n", timer.getElapsedMs() / copyCount);
}
eglSwapBuffers(eglDisplay, eglSurface);
return 0;
}
int testStretch()
{
static const int WIDTH = 8;
static const int HEIGHT = 8;
if (create_physical_texture(WIDTH, HEIGHT) != 0) {
return -1;
}
// Need to do a dummy eglSwapBuffers first. Don't know why.
glClearColor(0.4, 1.0, 0.4, 1.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
eglSwapBuffers(eglDisplay, eglSurface);
int cropRect[4] = {0,HEIGHT,WIDTH,-HEIGHT}; // Left bottom width height. Width and Height can be neg to flip.
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
for(int frame = 0; frame < 2; frame++) {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
int baseX = 10;
for (int x = 0; x < SCALE_COUNT; x++) {
int baseY = 10;
int width = scale(WIDTH, x);
for (int y = 0; y < SCALE_COUNT; y++) {
int height = scale(HEIGHT, y);
glDrawTexxOES(baseX << 16, baseY << 16, 0, width << 16, height << 16);
baseY += height + 10;
}
baseX += width + 10;
}
eglSwapBuffers(eglDisplay, eglSurface);
LOGD("wait 1s");
usleep(1000000);
}
return 0;
}
int testRot90()
{
static const int WIDTH = 8;
static const int HEIGHT = 8;
if (create_physical_texture(WIDTH, HEIGHT) != 0) {
return -1;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, 320, 480, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Need to do a dummy eglSwapBuffers first. Don't know why.
glClearColor(0.4, 0.4, 0.4, 0.4);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
eglSwapBuffers(eglDisplay, eglSurface);
glEnable(GL_TEXTURE_2D);
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
glDisable(GL_BLEND);
glShadeModel(GL_FLAT);
glDisable(GL_DITHER);
glDisable(GL_CULL_FACE);
for(int frame = 0; frame < 2; frame++) {
LOGD("frame = %d", frame);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
int baseX = 10;
for (int x = 0; x < SCALE_COUNT; x++) {
int baseY = 10;
int width = scale(WIDTH, x);
for (int y = 0; y < SCALE_COUNT; y++) {
int height = scale(HEIGHT, y);
// Code copied from SurfaceFlinger LayerBase.cpp
const GLfixed texCoords[4][2] = {
{ 0, 0 },
{ 0, 0x10000 },
{ 0x10000, 0x10000 },
{ 0x10000, 0 }
};
GLfixed fx = baseX << 16;
GLfixed fy = baseY << 16;
GLfixed fw = width << 16;
GLfixed fh = height << 16;
/*
* Vertex pattern:
* (2)--(3)
* |\ |
* | \ |
* | \ |
* | \|
* (1)--(0)
*
*/
const GLfixed vertices[4][2] = {
{fx + fw, fy},
{fx, fy},
{fx, fy + fh},
{fx + fw, fy + fh}
};
static const bool rotate90 = true;
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FIXED, 0, vertices);
glTexCoordPointer(2, GL_FIXED, 0, texCoords);
LOGD("testRot90 %d, %d %d, %d", baseX, baseY, width, height);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
baseY += height + 10;
}
baseX += width + 10;
}
eglSwapBuffers(eglDisplay, eglSurface);
}
return 0;
}
int main(int argc, char **argv)
{
int q;
int start, end;
if (init_gralloc()) {
printf("gralloc initialization failed - exiting\n");
return 0;
}
printf("Initializing EGL...\n");
if(!init_gl_surface())
{
printf("GL initialisation failed - exiting\n");
return 0;
}
init_scene();
printf("Start test...\n");
// testTime();
testStretch();
//testRot90();
free_gl_surface();
return 0;
}
|