test/putsurface: more cleanup, remove #ifdef ANDROID from putsurface_common.c
authorAustin Yuan <shengquan.yuan@gmail.com>
Mon, 20 Jun 2011 08:03:41 +0000 (01:03 -0700)
committerAustin Yuan <shengquan.yuan@gmail.com>
Mon, 20 Jun 2011 08:03:41 +0000 (01:03 -0700)
Signed-off-by: Austin Yuan <shengquan.yuan@gmail.com>
test/android_winsys.cpp
test/encode/h264encode_common.c
test/putsurface/putsurface_android.cpp
test/putsurface/putsurface_common.c
test/putsurface/putsurface_x11.c

index 6f84e38..4452edd 100644 (file)
@@ -9,7 +9,7 @@ namespace android {
 };
 
 #define SURFACE_CREATE(client,surface_ctrl,android_surface, android_isurface, win_width, win_height) \
-{\
+do {\
     client = new SurfaceComposerClient();\
     surface_ctrl = client->createSurface(getpid(), 0, win_width, win_height, PIXEL_FORMAT_RGB_565, ISurfaceComposer::ePushBuffers);\
     android_surface = surface_ctrl->getSurface();\
@@ -27,6 +27,6 @@ namespace android {
     surface_ctrl->setLayer(0x100000);\
     client->closeTransaction();\
 \
-}\
+} while (0)                                     \
 
 
index a1e24d6..7e8c1ad 100644 (file)
@@ -77,7 +77,6 @@ static int display_surface(int frame_id, int *exit_encode);
 
 static int upload_source_YUV_once_for_all()
 {
-    VAImage surface_image;
     void *surface_p=NULL, *U_start,*V_start;
     VAStatus va_status;
     int box_width=8;
index e168727..8d388ee 100644 (file)
@@ -36,9 +36,7 @@
 #include <assert.h>
 #include <pthread.h>
 
-#define Display unsigned int
-static  int win_thread0 = 0, win_thread1 = 0;
-static  int multi_thread = 0;
+static  int android_display=0;
 
 using namespace android;
 #include "../android_winsys.cpp"
@@ -53,8 +51,25 @@ sp<Surface> android_surface1;
 sp<ISurface> android_isurface1;
 sp<SurfaceControl> surface_ctrl1;
 
+static void *open_display(void);
+static void close_display(void *win_display);
+static int create_window(void *win_display, int width, int height);
+static int check_window_event(void *x11_display, void *win, int *width, int *height, int *quit);
 
-static int create_window(int width, int height)
+#define CAST_DRAWABLE(a)  static_cast<ISurface*>((void *)(*(unsigned int *)a))
+#include "putsurface_common.c"
+
+static void *open_display()
+{
+    return &android_display;
+}
+
+static void close_display(void *win_display)
+{
+    return;
+}
+
+static int create_window(void *win_display, int width, int height)
 {
     sp<ProcessState> proc(ProcessState::self());
     ProcessState::self()->startThreadPool();
@@ -62,16 +77,21 @@ static int create_window(int width, int height)
     printf("Create window0 for thread0\n");
     SURFACE_CREATE(client,surface_ctrl,android_surface, android_isurface, width, height);
 
-    win_thread0 = 1;
+    drawable_thread0 = static_cast<void*>(&android_isurface);
     if (multi_thread == 0)
         return 0;
 
     printf("Create window1 for thread1\n");
     /* need to modify here jgl*/
     SURFACE_CREATE(client1,surface_ctrl1,android_surface1, android_isurface1, width, height);
+    drawable_thread1 = static_cast<void *>(&android_isurface);
+    
+    return 0;
+}
 
-    win_thread1 = 2;
+int check_window_event(void *win_display, void *drawble, int *width, int *height, int *quit)
+{
     return 0;
 }
 
-#include "putsurface_common.c"
+
index 0186e18..1a28d21 100644 (file)
@@ -22,8 +22,6 @@
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-
-
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-
-#include <va/va.h>
-#ifdef ANDROID
-#include <va/va_android.h>
-#else
-#include <va/va_x11.h>
-#endif
 #include <assert.h>
 #include <pthread.h>
 
@@ -64,25 +55,25 @@ if (va_status != VA_STATUS_SUCCESS) {                                   \
 }
 #include "../loadsurface.h"
 
-#define SURFACE_NUM 5
+#define SURFACE_NUM 16
+
+static  void *win_display;
+static  VADisplay va_dpy;
 static  VASurfaceID surface_id[SURFACE_NUM];
+static  pthread_mutex_t surface_mutex[SURFACE_NUM];
+
+static  void *drawable_thread0, *drawable_thread1;
 static  int surface_width = 352, surface_height = 288;
 static  int win_width=352, win_height=288;
-static  Display *x11_display;
-static  VADisplay va_dpy;
+static  unsigned long long frame_num_total = ~0;
 static  int check_event = 1;
 static  int put_pixmap = 0;
 static  int test_clip = 0;
 static  int display_field = VA_FRAME_PICTURE;
-static  int verbose = 0;
-static  pthread_mutex_t surface_mutex[SURFACE_NUM];
-static pthread_mutex_t gmutex;
+static  pthread_mutex_t gmutex;
 static  int box_width = 32;
-
-#ifndef ANDROID
-static Pixmap create_pixmap(int width, int height);
-#endif
-static int create_window(int width, int height);
+static  int multi_thread = 0;
+static  int verbose = 0;
 
 static VASurfaceID get_next_free_surface(int *index)
 {
@@ -91,6 +82,16 @@ static VASurfaceID get_next_free_surface(int *index)
 
     assert(index);
 
+    if (multi_thread == 0) {
+        i = *index;
+        i++;
+        if (i == SURFACE_NUM)
+            i = 0;
+        *index = i;
+
+        return surface_id[i];
+    }
+    
     for (i=0; i<SURFACE_NUM; i++) {
         surface_status = (VASurfaceStatus)0;
         vaQuerySurfaceStatus(va_dpy, surface_id[i], &surface_status);
@@ -110,6 +111,27 @@ static VASurfaceID get_next_free_surface(int *index)
         return surface_id[i];
 }
 
+static int upload_source_YUV_once_for_all()
+{
+    VAImage surface_image;
+    void *surface_p=NULL, *U_start,*V_start;
+    VAStatus va_status;
+    int box_width_loc=8;
+    int row_shift_loc=0;
+    int i;
+    
+    for (i=0; i<SURFACE_NUM; i++) {
+        printf("\rLoading data into surface %d.....", i);
+        upload_surface(va_dpy, surface_id[i], box_width_loc, row_shift_loc, 0);
+        
+        row_shift_loc++;
+        if (row_shift_loc==(2*box_width_loc)) row_shift_loc= 0;
+    }
+    printf("\n");
+
+    return 0;
+}
+
 /*
  * Helper function for profiling purposes
  */
@@ -124,54 +146,19 @@ static unsigned long get_tick_count(void)
 static void* putsurface_thread(void *data)
 {
     int width=win_width, height=win_height;
-#ifdef ANDROID
-    int win = (int)data;
-#else 
-    Window win = (Window)data;
-    Pixmap pixmap = 0;
-    GC context = NULL;
-    Bool is_event; 
-    XEvent event;
-    Drawable draw;
-#endif
+    void *drawable = data;
     int quit = 0;
     VAStatus vaStatus;
     int row_shift = 0;
     int index = 0;
     unsigned int frame_num=0, start_time, putsurface_time;
     VARectangle cliprects[2]; /* client supplied clip list */
-#ifdef ANDROID
-    sp<ISurface> win_isurface;
-    if (win == win_thread0) {
-        printf("Enter into thread0\n\n");
-        win_isurface = android_isurface;    
-    }
     
-    if (win == win_thread1) {
-        printf("Enter into thread1\n\n");
-        win_isurface = android_isurface1;  
-    }   
-#else
-    if (win == win_thread0) {
+    if (drawable == drawable_thread0)
         printf("Enter into thread0\n\n");
-        pixmap = pixmap_thread0;
-        context = context_thread0;
-    }
-    
-    if (win == win_thread1) {
+    if (drawable == drawable_thread1)
         printf("Enter into thread1\n\n");
-        pixmap = pixmap_thread1;
-        context = context_thread1;
-    }
-
-    if (put_pixmap)
-        draw = pixmap;
-    else
-        draw = win;
-#endif
-    printf("vaPutSurface into a Window directly\n\n");
-
+    
     putsurface_time = 0;
     while (!quit) {
         VASurfaceID surface_id = VA_INVALID_SURFACE;
@@ -179,38 +166,29 @@ static void* putsurface_thread(void *data)
         while (surface_id == VA_INVALID_SURFACE)
             surface_id = get_next_free_surface(&index);
 
-        if (verbose) printf("Thread %x Display surface 0x%p,\n", (unsigned int)win, (void *)surface_id);
+        if (verbose) printf("Thread %x Display surface 0x%p,\n", (unsigned int)drawable, (void *)surface_id);
 
-        upload_surface(va_dpy, surface_id, box_width, row_shift, display_field);
+        if (multi_thread)
+            upload_surface(va_dpy, surface_id, box_width, row_shift, display_field);
 
         start_time = get_tick_count();
-
-#ifdef ANDROID
-            vaStatus = vaPutSurface(va_dpy, surface_id, win_isurface,
-                                    0,0,surface_width,surface_height,
-                                    0,0,width,height,
-                                    (test_clip==0)?NULL:&cliprects[0],
-                                    (test_clip==0)?0:2,
-                                    display_field);
-#else
-        if (check_event) {
+        
+        if (check_event)
             pthread_mutex_lock(&gmutex);
-        }
-        vaStatus = vaPutSurface(va_dpy, surface_id, draw,
+        vaStatus = vaPutSurface(va_dpy, surface_id, CAST_DRAWABLE(drawable),
                                 0,0,surface_width,surface_height,
                                 0,0,width,height,
                                 (test_clip==0)?NULL:&cliprects[0],
                                 (test_clip==0)?0:2,
                                 display_field);
-        if (check_event) {
+        if (check_event)
             pthread_mutex_unlock(&gmutex);
-        }
-#endif 
-
+        
+        pthread_mutex_unlock(&surface_mutex[index]); /* locked in get_next_free_surface */
+        
         CHECK_VASTATUS(vaStatus,"vaPutSurface");
 
         putsurface_time += (get_tick_count() - start_time);
-        
         if ((frame_num % 0xff) == 0) {
             fprintf(stderr, "%.2f FPS             \r", 256000.0 / (float)putsurface_time);
             putsurface_time = 0;
@@ -232,42 +210,21 @@ static void* putsurface_thread(void *data)
                        cliprects[1].x, cliprects[1].y, cliprects[1].width, cliprects[1].height);
             }
         }
+        if (check_event)
+            check_window_event(win_display, drawable, &width, &height, &quit);
 
-
-        pthread_mutex_unlock(&surface_mutex[index]);
-#ifndef ANDROID
-        if (check_event) {
-            pthread_mutex_lock(&gmutex);
-            is_event = XCheckWindowEvent(x11_display, win, StructureNotifyMask|KeyPressMask,&event);
-            pthread_mutex_unlock(&gmutex);
-            if (is_event) {
-                /* bail on any focused key press */
-                if(event.type == KeyPress) {  
-                    quit = 1;
-                    break;
-                }
-#if 0
-                /* rescale the video to fit the window */
-                if(event.type == ConfigureNotify) { 
-                    width = event.xconfigure.width;
-                    height = event.xconfigure.height;
-                    printf("Scale window to %dx%d\n", width, height);
-                }      
-#endif
-            }
-        }
-#endif
         row_shift++;
         if (row_shift==(2*box_width)) row_shift= 0;
 
         frame_num++;
 
-        if( frame_num == 0x200 )
+        if (frame_num >= frame_num_total)
             quit = 1;
-
     }
-    if(win == win_thread1)    
+    
+    if (drawable == drawable_thread1)    
         pthread_exit(NULL);
+    
     return 0;
 }
 
@@ -285,6 +242,7 @@ int main(int argc,char **argv)
         switch (c) {
             case '?':
                 printf("putsurface <options>\n");
+                printf("           -w/-h the window width/height\n");
                 printf("           -d the dimension of black/write square box, default is 32\n");
                 printf("           -t multi-threads\n");
                 printf("           -c test clipbox\n");
@@ -298,6 +256,9 @@ int main(int argc,char **argv)
             case 'h':
                 win_height = atoi(optarg);
                 break;
+            case 'n':
+                frame_num_total = atoi(optarg);
+                break;
             case 'd':
                 box_width = atoi(optarg);
                 break;
@@ -305,14 +266,12 @@ int main(int argc,char **argv)
                 multi_thread = 1;
                 printf("Two threads to do vaPutSurface\n");
                 break;
-#ifndef ANDROID
             case 'e':
                 check_event = 0;
                 break;
             case 'p':
                 put_pixmap = 1;
                 break;
-#endif 
             case 'c':
                 test_clip = 1;
                 break;
@@ -333,21 +292,14 @@ int main(int argc,char **argv)
         }
     }
 
-#ifdef ANDROID
-    x11_display = (Display*)malloc(sizeof(Display));
-    *(x11_display) = 0x18c34078;
-#else
-    x11_display = XOpenDisplay(":0.0");
-#endif
-
-    if (x11_display == NULL) {
-        fprintf(stderr, "Can't connect X server!\n");
+    win_display = (void *)open_display();
+    if (win_display == NULL) {
+        fprintf(stderr, "Can't open the connection of display!\n");
         exit(-1);
     }
+    create_window(win_display, win_width, win_height);
 
-    create_window(win_width, win_height);
-
-    va_dpy = vaGetDisplay(x11_display);
+    va_dpy = vaGetDisplay(win_display);
     va_status = vaInitialize(va_dpy, &major_ver, &minor_ver);
     CHECK_VASTATUS(va_status, "vaInitialize");
 
@@ -356,7 +308,9 @@ int main(int argc,char **argv)
     va_status = vaCreateSurfaces(va_dpy,surface_width, surface_height,
                                 VA_RT_FORMAT_YUV420, SURFACE_NUM, &surface_id[0]);
     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
-
+    if (multi_thread == 0) /* upload the content for all surfaces */
+        upload_source_YUV_once_for_all();
+    
     if (check_event)
         pthread_mutex_init(&gmutex, NULL);
    
@@ -364,15 +318,18 @@ int main(int argc,char **argv)
         pthread_mutex_init(&surface_mutex[i], NULL);
     
     if (multi_thread == 1) 
-        ret = pthread_create(&thread1, NULL, putsurface_thread, (void*)win_thread1);
+        ret = pthread_create(&thread1, NULL, putsurface_thread, (void*)drawable_thread1);
 
-    putsurface_thread((void *)win_thread0);
+    putsurface_thread((void *)drawable_thread0);
 
     if (multi_thread == 1) 
         pthread_join(thread1, (void **)&ret);
-     printf("thread1 is free\n");
+    printf("thread1 is free\n");
+    
     vaDestroySurfaces(va_dpy,&surface_id[0],SURFACE_NUM);    
     vaTerminate(va_dpy);
+
+    close_display(win_display);
     
     return 0;
 }
index 3968ad3..3912d1a 100644 (file)
 #include <X11/Xutil.h>
 #include <va/va_x11.h>
 
-static  Window win_thread0, win_thread1;
-static  int multi_thread = 0;
-static  Pixmap pixmap_thread0, pixmap_thread1;
+static  Window window_thread0, window_thread1;
 static  GC context_thread0, context_thread1;
-static pthread_mutex_t gmutex;
+static  pthread_mutex_t gmutex;
+
+static void *open_display(void);
+static void close_display(void *win_display);
+static int create_window(void *win_display, int width, int height);
+static int check_window_event(void *x11_display, void *drawable, int *width, int *height, int *quit);
+
+#define CAST_DRAWABLE(a)  (Drawable)(a)
 
 #include "putsurface_common.c"
 
-static Pixmap create_pixmap(int width, int height)
+static void *open_display(void)
+{
+    return XOpenDisplay(":0.0");
+}
+
+static void close_display(void *win_display)
+{
+    XCloseDisplay(win_display);
+}
+
+static Pixmap create_pixmap(void *win_display, int width, int height)
 {
+    Display *x11_display = (Display *)win_display;
     int screen = DefaultScreen(x11_display);
     Window root;
     Pixmap pixmap;
@@ -52,17 +68,18 @@ static Pixmap create_pixmap(int width, int height)
     return pixmap;
 }
 
-static int create_window(int width, int height)
+static int create_window(void *win_display, int width, int height)
 {
+    Display *x11_display = (Display *)win_display;
     int screen = DefaultScreen(x11_display);
     Window root, win;
 
     root = RootWindow(x11_display, screen);
 
     printf("Create window0 for thread0\n");
-    win_thread0 = win = XCreateSimpleWindow(x11_display, root, 0, 0, width, height,
-                                            0, 0, WhitePixel(x11_display, 0));
-    if (win) {
+    drawable_thread0 = (void *)XCreateSimpleWindow(x11_display, root, 0, 0, width, height,
+                                           0, 0, WhitePixel(x11_display, 0));
+    if (drawable_thread0) {
         XSizeHints sizehints;
         sizehints.width  = width;
         sizehints.height = height;
@@ -77,17 +94,19 @@ static int create_window(int width, int height)
     XSelectInput(x11_display, win, KeyPressMask | StructureNotifyMask);
     XSync(x11_display, False);
 
-    if (put_pixmap)
-        pixmap_thread0 = create_pixmap(width, height);
+    if (put_pixmap) {
+        window_thread0 = (Window)drawable_thread0;
+        drawable_thread0 = (void *)create_pixmap(x11_display, width, height);
+    }
     
     if (multi_thread == 0)
         return 0;
 
     printf("Create window1 for thread1\n");
     
-    win_thread1 = win = XCreateSimpleWindow(x11_display, root, width, 0, width, height,
+    drawable_thread1 = (void *)XCreateSimpleWindow(x11_display, root, width, 0, width, height,
                                             0, 0, WhitePixel(x11_display, 0));
-    if (win) {
+    if (drawable_thread1) {
         XSizeHints sizehints;
         sizehints.width  = width;
         sizehints.height = height;
@@ -98,8 +117,10 @@ static int create_window(int width, int height)
 
         XMapWindow(x11_display, win);
     }
-    if (put_pixmap)
-        pixmap_thread1 = create_pixmap(width, height);
+    if (put_pixmap) {
+        window_thread1 = (Window)drawable_thread1;
+        drawable_thread1 = (void *)create_pixmap(x11_display, width, height);
+    }
 
     context_thread1 = XCreateGC(x11_display, win, 0, 0);
     XSelectInput(x11_display, win, KeyPressMask | StructureNotifyMask);
@@ -107,3 +128,40 @@ static int create_window(int width, int height)
     
     return 0;
 }
+
+static int check_window_event(void *win_display, void *drawable, int *width, int *height, int *quit)
+{
+    int is_event = 0;
+    XEvent event;
+    Window win = (Window)drawable;
+    Display *x11_display = (Display *)win_display;
+    
+    
+    if (check_event == 0)
+        return 0;
+
+    pthread_mutex_lock(&gmutex);
+    is_event = XCheckWindowEvent(x11_display, win, StructureNotifyMask|KeyPressMask,&event);
+    pthread_mutex_unlock(&gmutex);
+    
+    if (is_event == 0)
+        return 0;
+
+    /* bail on any focused key press */
+    if(event.type == KeyPress) {  
+        *quit = 1;
+        return 0;
+    }
+    
+#if 0
+    /* rescale the video to fit the window */
+    if(event.type == ConfigureNotify) { 
+        *width = event.xconfigure.width;
+        *height = event.xconfigure.height;
+        printf("Scale window to %dx%d\n", width, height);
+    }
+#endif
+}
+
+
+