Home | History | Annotate | Download | only in ut_renderer
      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 "RendererSurface.h"
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 
     20 #include "NativeWindowing.h"
     21 
     22 #define MAX_ATTRIB 100
     23 
     24 
     25 EGLConfig RendererSurface::getEglConfig(EGLDisplay eglDisplay, SurfaceConfig config)
     26 {
     27     EGLConfig eglConfig;
     28     int nConfigs;
     29 
     30     EGLint attrib[MAX_ATTRIB];
     31     int pos =0;
     32 
     33     attrib[pos++] = EGL_SURFACE_TYPE; attrib[pos++] = EGL_WINDOW_BIT;
     34     if (config & CONFIG_DEPTH) {attrib[pos++] = EGL_DEPTH_SIZE; attrib[pos++] = 1;}
     35     attrib[pos++] = EGL_NONE;
     36 
     37     if (!eglChooseConfig(eglDisplay, attrib, &eglConfig, 1, &nConfigs)) {
     38         return 0;
     39     }
     40     /***/
     41     int ibuf;
     42     if (eglGetConfigAttrib(eglDisplay, eglConfig, EGL_BUFFER_SIZE, &ibuf)) {
     43         fprintf(stderr, "EGL COLOR Buffer size: %d\n", ibuf);
     44     } else {
     45         fprintf(stderr, "eglGetConfigAttrib error: %d\n", eglGetError());
     46     }
     47     if (eglGetConfigAttrib(eglDisplay, eglConfig, EGL_DEPTH_SIZE, &ibuf)) {
     48         fprintf(stderr, "EGL DEPTH Buffer size: %d\n", ibuf);
     49     } else {
     50         fprintf(stderr, "eglGetConfigAttrib error: %d\n", eglGetError());
     51     }
     52     /***/
     53 
     54 
     55     if (nConfigs != 1) {
     56         return 0;
     57     }
     58     return eglConfig;
     59 }
     60 
     61 RendererSurface * RendererSurface::create(EGLDisplay eglDisplay, SurfaceConfig config, NativeWindowing *nw)
     62 {
     63     int width = 0, height = 0;
     64     const char* env;
     65 
     66     env = getenv("ANDROID_WINDOW_WIDTH");
     67     if (env && *env) {
     68         width = atoi(env);
     69     }
     70     env = getenv("ANDROID_WINDOW_HEIGHT");
     71     if (env && *env) {
     72         height = atoi(env);
     73     }
     74     if (width <= 160)
     75         width = DEFAULT_WIDTH;
     76     if (height <= 160)
     77         height = DEFAULT_HEIGHT;
     78 
     79     printf("%s: Using width=%d height=%d\n", __FUNCTION__, width, height);
     80 
     81     EGLConfig eglConfig = getEglConfig(eglDisplay, config);
     82     if (eglConfig == 0) {
     83         return NULL;
     84     }
     85 
     86     NativeWindowType window = nw->createNativeWindow(nw->getNativeDisplay(), width, height);
     87     if (window == 0) {
     88         return NULL;
     89     }
     90 
     91     EGLSurface eglSurface = eglCreateWindowSurface(eglDisplay,
     92                                                    eglConfig,
     93                                                    window, NULL);
     94 
     95     if (eglGetError() != EGL_SUCCESS) {
     96         return NULL;
     97     }
     98 
     99     return new RendererSurface(eglDisplay, window, eglSurface, eglConfig);
    100 }
    101 
    102 int RendererSurface::destroy(NativeWindowing *nw)
    103 {
    104     eglDestroySurface(m_eglDisplay, m_eglSurface);
    105     nw->destroyNativeWindow(nw->getNativeDisplay(), m_window);
    106     return 1;
    107 }
    108 
    109