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 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, " -port <portNum> - listening TCP port number\n"); 38 fprintf(stderr, " -x <num> - render subwindow x position\n"); 39 fprintf(stderr, " -y <num> - render subwindow y position\n"); 40 fprintf(stderr, " -width <num> - render subwindow width\n"); 41 fprintf(stderr, " -height <num> - render subwindow height\n"); 42 exit(-1); 43 } 44 45 int main(int argc, char *argv[]) 46 { 47 int portNum = CODEC_SERVER_PORT; 48 int winX = 0; 49 int winY = 0; 50 int winWidth = 320; 51 int winHeight = 480; 52 FBNativeWindowType windowId = NULL; 53 int iWindowId = 0; 54 55 // 56 // Parse command line arguments 57 // 58 for (int i=1; i<argc; i++) { 59 if (!strcmp(argv[i], "-windowid")) { 60 if (++i >= argc || sscanf(argv[i],"%d", &iWindowId) != 1) { 61 printUsage(argv[0]); 62 } 63 } 64 else if (!strncmp(argv[i], "-port", 5)) { 65 if (++i >= argc || sscanf(argv[i],"%d", &portNum) != 1) { 66 printUsage(argv[0]); 67 } 68 } 69 else if (!strncmp(argv[i], "-x", 2)) { 70 if (++i >= argc || sscanf(argv[i],"%d", &winX) != 1) { 71 printUsage(argv[0]); 72 } 73 } 74 else if (!strncmp(argv[i], "-y", 2)) { 75 if (++i >= argc || sscanf(argv[i],"%d", &winY) != 1) { 76 printUsage(argv[0]); 77 } 78 } 79 else if (!strncmp(argv[i], "-width", 6)) { 80 if (++i >= argc || sscanf(argv[i],"%d", &winWidth) != 1) { 81 printUsage(argv[0]); 82 } 83 } 84 else if (!strncmp(argv[i], "-height", 7)) { 85 if (++i >= argc || sscanf(argv[i],"%d", &winHeight) != 1) { 86 printUsage(argv[0]); 87 } 88 } 89 } 90 91 windowId = (FBNativeWindowType)iWindowId; 92 if (!windowId) { 93 // window id must be provided 94 printUsage(argv[0]); 95 } 96 97 #if 0 //Enable to attach gdb to renderer on startup 98 fprintf(stderr, "renderer pid %d , press any key to continue...\n", getpid()); 99 getchar(); 100 #else 101 fprintf(stderr, "renderer pid %d \n", getpid()); 102 #endif 103 104 #ifdef _WIN32 105 WSADATA wsaData; 106 int rc = WSAStartup( MAKEWORD(2,2), &wsaData); 107 if (rc != 0) { 108 printf( "could not initialize Winsock\n" ); 109 } 110 #endif 111 112 #ifdef __linux__ 113 // some OpenGL implementations may call X functions 114 // it is safer to synchronize all X calls made by all the 115 // rendering threads. (although the calls we do are locked 116 // in the FrameBuffer singleton object). 117 XInitThreads(); 118 #endif 119 120 // 121 // initialize Framebuffer 122 // 123 bool inited = FrameBuffer::initialize(winWidth, winHeight, NULL, NULL); 124 if (!inited) { 125 fprintf(stderr,"Failed to initialize Framebuffer\n"); 126 return -1; 127 } 128 129 inited = FrameBuffer::setupSubWindow(windowId, 130 winX, winY, winWidth, winHeight, 0.0); 131 if (!inited) { 132 fprintf(stderr,"Failed to create subwindow Framebuffer\n"); 133 return -1; 134 } 135 136 // 137 // Create and run a render server listening to the given port number 138 // 139 RenderServer *server = RenderServer::create(portNum); 140 if (!server) { 141 fprintf(stderr,"Cannot initialize render server\n"); 142 return -1; 143 } 144 145 #ifndef _WIN32 146 // 147 // run the server listener loop 148 // 149 server->Main(); 150 #else 151 // 152 // on windows we need to handle messages for the 153 // created subwindow. So we run the server on a seperate 154 // thread and running the windows message pump loop 155 // in this main thread. 156 // 157 server->start(); 158 159 // 160 // Dispatch events for the subwindow 161 // During termination of the render server, the FrameBuffer 162 // will be finalized, the Framebuffer subwindow will 163 // get destroyed and the following loop will exit. 164 // 165 MSG msg; 166 HWND hWnd = FrameBuffer::getFB()->getSubWindow(); 167 while( GetMessage(&msg, hWnd, 0, 0) > 0 ) { 168 TranslateMessage(&msg); 169 DispatchMessage(&msg); 170 } 171 #endif 172 173 return 0; 174 } 175