1/*
2 ** Copyright 2007, 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
17#include "egl_object.h"
18
19#include <sstream>
20
21
22// ----------------------------------------------------------------------------
23namespace android {
24// ----------------------------------------------------------------------------
25
26egl_object_t::egl_object_t(egl_display_t* disp) :
27    display(disp), count(1) {
28    // NOTE: this does an implicit incRef
29    display->addObject(this);
30}
31
32egl_object_t::~egl_object_t() {
33}
34
35void egl_object_t::terminate() {
36    // this marks the object as "terminated"
37    display->removeObject(this);
38    if (decRef() == 1) {
39        // shouldn't happen because this is called from LocalRef
40        ALOGE("egl_object_t::terminate() removed the last reference!");
41    }
42}
43
44void egl_object_t::destroy() {
45    if (decRef() == 1) {
46        delete this;
47    }
48}
49
50bool egl_object_t::get(egl_display_t const* display, egl_object_t* object) {
51    // used by LocalRef, this does an incRef() atomically with
52    // checking that the object is valid.
53    return display->getObject(object);
54}
55
56// ----------------------------------------------------------------------------
57
58egl_surface_t::egl_surface_t(egl_display_t* dpy, EGLConfig config,
59        EGLNativeWindowType win, EGLSurface surface,
60        egl_connection_t const* cnx) :
61    egl_object_t(dpy), surface(surface), config(config), win(win), cnx(cnx),
62    connected(true)
63{
64    if (win) {
65        win->incStrong(this);
66    }
67}
68
69egl_surface_t::~egl_surface_t() {
70    if (win != NULL) {
71        disconnect();
72        win->decStrong(this);
73    }
74}
75
76void egl_surface_t::disconnect() {
77    if (win != NULL && connected) {
78        native_window_set_buffers_format(win, 0);
79        if (native_window_api_disconnect(win, NATIVE_WINDOW_API_EGL)) {
80            ALOGW("EGLNativeWindowType %p disconnect failed", win);
81        }
82        connected = false;
83    }
84}
85
86void egl_surface_t::terminate() {
87    disconnect();
88    egl_object_t::terminate();
89}
90
91// ----------------------------------------------------------------------------
92
93egl_context_t::egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
94        egl_connection_t const* cnx, int version) :
95    egl_object_t(get_display_nowake(dpy)), dpy(dpy), context(context),
96            config(config), read(0), draw(0), cnx(cnx), version(version) {
97}
98
99void egl_context_t::onLooseCurrent() {
100    read = NULL;
101    draw = NULL;
102}
103
104void egl_context_t::onMakeCurrent(EGLSurface draw, EGLSurface read) {
105    this->read = read;
106    this->draw = draw;
107
108    /*
109     * Here we cache the GL_EXTENSIONS string for this context and we
110     * add the extensions always handled by the wrapper
111     */
112
113    if (gl_extensions.empty()) {
114        // call the implementation's glGetString(GL_EXTENSIONS)
115        const char* exts = (const char *)gEGLImpl.hooks[version]->gl.glGetString(GL_EXTENSIONS);
116        gl_extensions = exts;
117        if (gl_extensions.find("GL_EXT_debug_marker") == std::string::npos) {
118            gl_extensions.insert(0, "GL_EXT_debug_marker ");
119        }
120
121        // tokenize the supported extensions for the glGetStringi() wrapper
122        std::stringstream ss;
123        std::string str;
124        ss << gl_extensions;
125        while (ss >> str) {
126            tokenized_gl_extensions.push_back(str);
127        }
128    }
129}
130
131// ----------------------------------------------------------------------------
132}; // namespace android
133// ----------------------------------------------------------------------------
134