1ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall/*
2ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Copyright (C) 2011 The Android Open Source Project
3ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall*
4ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Licensed under the Apache License, Version 2.0 (the "License");
5ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* you may not use this file except in compliance with the License.
6ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* You may obtain a copy of the License at
7ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall*
8ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* http://www.apache.org/licenses/LICENSE-2.0
9ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall*
10ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* Unless required by applicable law or agreed to in writing, software
11ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* distributed under the License is distributed on an "AS IS" BASIS,
12ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* See the License for the specific language governing permissions and
14ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall* limitations under the License.
15ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall*/
16ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include "RendererSurface.h"
17ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include <stdio.h>
18ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include <stdlib.h>
19ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
20ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#include "NativeWindowing.h"
21ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
22ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall#define MAX_ATTRIB 100
23ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
24ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
25ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse HallEGLConfig RendererSurface::getEglConfig(EGLDisplay eglDisplay, SurfaceConfig config)
26ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall{
27ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    EGLConfig eglConfig;
28ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    int nConfigs;
29ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
30ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    EGLint attrib[MAX_ATTRIB];
31ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    int pos =0;
32ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
33ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    attrib[pos++] = EGL_SURFACE_TYPE; attrib[pos++] = EGL_WINDOW_BIT;
34ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (config & CONFIG_DEPTH) {attrib[pos++] = EGL_DEPTH_SIZE; attrib[pos++] = 1;}
35ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    attrib[pos++] = EGL_NONE;
36ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
37ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (!eglChooseConfig(eglDisplay, attrib, &eglConfig, 1, &nConfigs)) {
38ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        return 0;
39ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
40ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    /***/
41ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    int ibuf;
42ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (eglGetConfigAttrib(eglDisplay, eglConfig, EGL_BUFFER_SIZE, &ibuf)) {
43ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        fprintf(stderr, "EGL COLOR Buffer size: %d\n", ibuf);
44ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    } else {
45ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        fprintf(stderr, "eglGetConfigAttrib error: %d\n", eglGetError());
46ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
47ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (eglGetConfigAttrib(eglDisplay, eglConfig, EGL_DEPTH_SIZE, &ibuf)) {
48ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        fprintf(stderr, "EGL DEPTH Buffer size: %d\n", ibuf);
49ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    } else {
50ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        fprintf(stderr, "eglGetConfigAttrib error: %d\n", eglGetError());
51ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
52ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    /***/
53ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
54ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
55ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (nConfigs != 1) {
56ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        return 0;
57ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
58ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    return eglConfig;
59ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall}
60ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
61ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse HallRendererSurface * RendererSurface::create(EGLDisplay eglDisplay, SurfaceConfig config, NativeWindowing *nw)
62ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall{
63ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    int width = 0, height = 0;
64ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    const char* env;
65ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
66ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    env = getenv("ANDROID_WINDOW_WIDTH");
67ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (env && *env) {
68ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        width = atoi(env);
69ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
70ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    env = getenv("ANDROID_WINDOW_HEIGHT");
71ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (env && *env) {
72ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        height = atoi(env);
73ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
74ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (width <= 160)
75ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        width = DEFAULT_WIDTH;
76ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (height <= 160)
77ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        height = DEFAULT_HEIGHT;
78ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
79ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    printf("%s: Using width=%d height=%d\n", __FUNCTION__, width, height);
80ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
81ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    EGLConfig eglConfig = getEglConfig(eglDisplay, config);
82ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (eglConfig == 0) {
83ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        return NULL;
84ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
85ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
86ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    NativeWindowType window = nw->createNativeWindow(nw->getNativeDisplay(), width, height);
87ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (window == 0) {
88ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        return NULL;
89ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
90ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
91ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    EGLSurface eglSurface = eglCreateWindowSurface(eglDisplay,
92ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall                                                   eglConfig,
93ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall                                                   window, NULL);
94ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
95ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    if (eglGetError() != EGL_SUCCESS) {
96ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall        return NULL;
97ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    }
98ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
99ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    return new RendererSurface(eglDisplay, window, eglSurface, eglConfig);
100ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall}
101ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
102ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hallint RendererSurface::destroy(NativeWindowing *nw)
103ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall{
104ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    eglDestroySurface(m_eglDisplay, m_eglSurface);
105ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    nw->destroyNativeWindow(nw->getNativeDisplay(), m_window);
106ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall    return 1;
107ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall}
108ce6c3389061fb9fcdefc94fab2044a8e11600b52Jesse Hall
109