• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "RenderServer.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "FrameBuffer.h"
21 
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <codec_defs.h>
25 #ifdef _WIN32
26 #include <winsock2.h>
27 #endif
28 
29 #ifdef __linux__
30 #include <X11/Xlib.h>
31 #endif
32 
printUsage(const char * progName)33 static void printUsage(const char *progName)
34 {
35     fprintf(stderr, "Usage: %s -windowid <windowid> [options]\n", progName);
36     fprintf(stderr, "    -windowid <windowid>   - window id to render into\n");
37     fprintf(stderr, "    -x <num>               - render subwindow x position\n");
38     fprintf(stderr, "    -y <num>               - render subwindow y position\n");
39     fprintf(stderr, "    -width <num>           - render subwindow width\n");
40     fprintf(stderr, "    -height <num>          - render subwindow height\n");
41     exit(-1);
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46     int winX = 0;
47     int winY = 0;
48     int winWidth = 320;
49     int winHeight = 480;
50     FBNativeWindowType windowId = NULL;
51     int iWindowId  = 0;
52 
53     //
54     // Parse command line arguments
55     //
56     for (int i=1; i<argc; i++) {
57         if (!strcmp(argv[i], "-windowid")) {
58             if (++i >= argc || sscanf(argv[i],"%d", &iWindowId) != 1) {
59                 printUsage(argv[0]);
60             }
61         }
62         else if (!strncmp(argv[i], "-x", 2)) {
63             if (++i >= argc || sscanf(argv[i],"%d", &winX) != 1) {
64                 printUsage(argv[0]);
65             }
66         }
67         else if (!strncmp(argv[i], "-y", 2)) {
68             if (++i >= argc || sscanf(argv[i],"%d", &winY) != 1) {
69                 printUsage(argv[0]);
70             }
71         }
72         else if (!strncmp(argv[i], "-width", 6)) {
73             if (++i >= argc || sscanf(argv[i],"%d", &winWidth) != 1) {
74                 printUsage(argv[0]);
75             }
76         }
77         else if (!strncmp(argv[i], "-height", 7)) {
78             if (++i >= argc || sscanf(argv[i],"%d", &winHeight) != 1) {
79                 printUsage(argv[0]);
80             }
81         }
82     }
83 
84     windowId = (FBNativeWindowType)iWindowId;
85     if (!windowId) {
86         // window id must be provided
87         printUsage(argv[0]);
88     }
89 
90 #if 0 //Enable to attach gdb to renderer on startup
91     fprintf(stderr, "renderer pid %d , press any key to continue...\n", getpid());
92     getchar();
93 #else
94     fprintf(stderr, "renderer pid %d \n", getpid());
95 #endif
96 
97 #ifdef _WIN32
98     WSADATA  wsaData;
99     int      rc = WSAStartup( MAKEWORD(2,2), &wsaData);
100     if (rc != 0) {
101             printf( "could not initialize Winsock\n" );
102     }
103 #endif
104 
105 #ifdef __linux__
106     // some OpenGL implementations may call X functions
107     // it is safer to synchronize all X calls made by all the
108     // rendering threads. (although the calls we do are locked
109     // in the FrameBuffer singleton object).
110     XInitThreads();
111 #endif
112 
113     //
114     // initialize Framebuffer
115     //
116     bool inited = FrameBuffer::initialize(winWidth, winHeight);
117     if (!inited) {
118         fprintf(stderr,"Failed to initialize Framebuffer\n");
119         return -1;
120     }
121 
122     inited = FrameBuffer::setupSubWindow(windowId,
123                                          winX, winY, winWidth, winHeight, 0.0);
124     if (!inited) {
125         fprintf(stderr,"Failed to create subwindow Framebuffer\n");
126         return -1;
127     }
128 
129     //
130     // Create and run a render server listening to the given port number
131     //
132     char addr[256];
133     RenderServer *server = RenderServer::create(addr, sizeof(addr));
134     if (!server) {
135         fprintf(stderr,"Cannot initialize render server\n");
136         return -1;
137     }
138     printf("render server listening at '%s'\n", addr);
139 
140 #ifndef _WIN32
141     //
142     // run the server listener loop
143     //
144     server->Main();
145 #else
146     //
147     // on windows we need to handle messages for the
148     // created subwindow. So we run the server on a seperate
149     // thread and running the windows message pump loop
150     // in this main thread.
151     //
152     server->start();
153 
154     //
155     // Dispatch events for the subwindow
156     // During termination of the render server, the FrameBuffer
157     // will be finalized, the Framebuffer subwindow will
158     // get destroyed and the following loop will exit.
159     //
160     MSG msg;
161     HWND hWnd = FrameBuffer::getFB()->getSubWindow();
162     while( GetMessage(&msg, hWnd, 0, 0) > 0 ) {
163         TranslateMessage(&msg);
164         DispatchMessage(&msg);
165     }
166 #endif
167 
168     return 0;
169 }
170