1518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian/*
2518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Copyright 2007, The Android Open Source Project
3518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
4518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
5518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** you may not use this file except in compliance with the License.
6518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** You may obtain a copy of the License at
7518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
8518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
9518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian **
10518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** Unless required by applicable law or agreed to in writing, software
11518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
12518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** See the License for the specific language governing permissions and
14518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian ** limitations under the License.
15518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian */
16518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
17518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <ctype.h>
18518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <stdint.h>
19518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <stdlib.h>
20518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
21518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/egl.h>
22518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <EGL/eglext.h>
23518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/gl.h>
24518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <GLES/glext.h>
25518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
26518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include <utils/threads.h>
27518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
28518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian#include "egl_object.h"
29518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
30518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
31518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopiannamespace android {
32518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
33518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
34518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopianegl_object_t::egl_object_t(egl_display_t* disp) :
355b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    display(disp), count(1) {
365b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // NOTE: this does an implicit incRef
37518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian    display->addObject(this);
38518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
39518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
405b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopianegl_object_t::~egl_object_t() {
415b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian}
425b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian
435b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopianvoid egl_object_t::terminate() {
445b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // this marks the object as "terminated"
455b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    display->removeObject(this);
465b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (decRef() == 1) {
475b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        // shouldn't happen because this is called from LocalRef
48e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("egl_object_t::terminate() removed the last reference!");
495b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    }
50518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
51518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
525b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopianvoid egl_object_t::destroy() {
535b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    if (decRef() == 1) {
545b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian        delete this;
555b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    }
565b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian}
575b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian
58f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopianbool egl_object_t::get(egl_display_t const* display, egl_object_t* object) {
595b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // used by LocalRef, this does an incRef() atomically with
605b287a6ea8dfac7ab3e03ae1e98f9e2214cbae09Mathias Agopian    // checking that the object is valid.
61f0480de37492597a5c5cf1e6f8346f1467e3a552Mathias Agopian    return display->getObject(object);
62518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}
63518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian
64518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
6548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
66258385978c517a47626161b1e644c48bcee28de1Jesse Hallegl_surface_t::egl_surface_t(egl_display_t* dpy, EGLConfig config,
67258385978c517a47626161b1e644c48bcee28de1Jesse Hall        EGLNativeWindowType win, EGLSurface surface,
68258385978c517a47626161b1e644c48bcee28de1Jesse Hall        egl_connection_t const* cnx) :
69258385978c517a47626161b1e644c48bcee28de1Jesse Hall    egl_object_t(dpy), surface(surface), config(config), win(win), cnx(cnx)
70258385978c517a47626161b1e644c48bcee28de1Jesse Hall{
71258385978c517a47626161b1e644c48bcee28de1Jesse Hall    if (win) {
72258385978c517a47626161b1e644c48bcee28de1Jesse Hall        getDisplay()->onWindowSurfaceCreated();
73258385978c517a47626161b1e644c48bcee28de1Jesse Hall    }
74258385978c517a47626161b1e644c48bcee28de1Jesse Hall}
75258385978c517a47626161b1e644c48bcee28de1Jesse Hall
76258385978c517a47626161b1e644c48bcee28de1Jesse Hallegl_surface_t::~egl_surface_t() {
77258385978c517a47626161b1e644c48bcee28de1Jesse Hall    ANativeWindow* const window = win.get();
78258385978c517a47626161b1e644c48bcee28de1Jesse Hall    if (window != NULL) {
79258385978c517a47626161b1e644c48bcee28de1Jesse Hall        native_window_set_buffers_format(window, 0);
80258385978c517a47626161b1e644c48bcee28de1Jesse Hall        if (native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL)) {
81258385978c517a47626161b1e644c48bcee28de1Jesse Hall            ALOGW("EGLNativeWindowType %p disconnect failed", window);
82258385978c517a47626161b1e644c48bcee28de1Jesse Hall        }
83258385978c517a47626161b1e644c48bcee28de1Jesse Hall        getDisplay()->onWindowSurfaceDestroyed();
84258385978c517a47626161b1e644c48bcee28de1Jesse Hall    }
85258385978c517a47626161b1e644c48bcee28de1Jesse Hall}
86258385978c517a47626161b1e644c48bcee28de1Jesse Hall
87258385978c517a47626161b1e644c48bcee28de1Jesse Hall// ----------------------------------------------------------------------------
88258385978c517a47626161b1e644c48bcee28de1Jesse Hall
8948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopianegl_context_t::egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
90ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        egl_connection_t const* cnx, int version) :
91b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall    egl_object_t(get_display_nowake(dpy)), dpy(dpy), context(context),
92b29e5e8c2682ae145e8c56d9afb061f8da7f854cJesse Hall            config(config), read(0), draw(0), cnx(cnx), version(version) {
9348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian}
9448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
9548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopianvoid egl_context_t::onLooseCurrent() {
9648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    read = NULL;
9748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    draw = NULL;
9848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian}
9948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
10048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopianvoid egl_context_t::onMakeCurrent(EGLSurface draw, EGLSurface read) {
10148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    this->read = read;
10248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    this->draw = draw;
10348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
10448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    /*
10548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian     * Here we cache the GL_EXTENSIONS string for this context and we
10648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian     * add the extensions always handled by the wrapper
10748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian     */
10848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
10948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    if (gl_extensions.isEmpty()) {
11048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian        // call the implementation's glGetString(GL_EXTENSIONS)
111ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        const char* exts = (const char *)gEGLImpl.hooks[version]->gl.glGetString(GL_EXTENSIONS);
11248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian        gl_extensions.setTo(exts);
11348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian        if (gl_extensions.find("GL_EXT_debug_marker") < 0) {
11448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            String8 temp("GL_EXT_debug_marker ");
11548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            temp.append(gl_extensions);
11648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            gl_extensions.setTo(temp);
11748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian        }
11848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian    }
11948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian}
12048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
12148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian// ----------------------------------------------------------------------------
122518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian}; // namespace android
123518ec112f468eb67bf681b3eec896d7bfb4ff98dMathias Agopian// ----------------------------------------------------------------------------
124