egl_object.h revision 1a5c4deed3e33efd7f286a570cf88fbd2d861010
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#ifndef ANDROID_EGL_OBJECT_H
18#define ANDROID_EGL_OBJECT_H
19
20
21#include <ctype.h>
22#include <stdint.h>
23#include <stdlib.h>
24
25#include <EGL/egl.h>
26#include <EGL/eglext.h>
27
28#include <utils/threads.h>
29#include <utils/String8.h>
30#include <utils/Vector.h>
31
32#include <system/window.h>
33
34#include "egl_display.h"
35
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
40class egl_display_t;
41
42class egl_object_t {
43    egl_display_t *display;
44    mutable volatile int32_t count;
45
46protected:
47    virtual ~egl_object_t();
48
49public:
50    egl_object_t(egl_display_t* display);
51    void destroy();
52
53    inline int32_t incRef() { return android_atomic_inc(&count); }
54    inline int32_t decRef() { return android_atomic_dec(&count); }
55    inline egl_display_t* getDisplay() const { return display; }
56
57private:
58    void terminate();
59    static bool get(egl_display_t const* display, egl_object_t* object);
60
61public:
62    template <typename N, typename T>
63    class LocalRef {
64        egl_object_t* ref;
65        LocalRef();
66        LocalRef(const LocalRef* rhs);
67    public:
68        ~LocalRef();
69        explicit LocalRef(egl_object_t* rhs);
70        explicit LocalRef(egl_display_t const* display, T o) : ref(0) {
71            egl_object_t* native = reinterpret_cast<N*>(o);
72            if (o && egl_object_t::get(display, native)) {
73                ref = native;
74            }
75        }
76        inline N* get() {
77            return static_cast<N*>(ref);
78        }
79        void acquire() const;
80        void release() const;
81        void terminate();
82    };
83    template <typename N, typename T>
84    friend class LocalRef;
85};
86
87template<typename N, typename T>
88egl_object_t::LocalRef<N, T>::LocalRef(egl_object_t* rhs) : ref(rhs) {
89    if (ref) {
90        ref->incRef();
91    }
92}
93
94template <typename N, typename T>
95egl_object_t::LocalRef<N,T>::~LocalRef() {
96    if (ref) {
97        ref->destroy();
98    }
99}
100
101template <typename N, typename T>
102void egl_object_t::LocalRef<N,T>::acquire() const {
103    if (ref) {
104        ref->incRef();
105    }
106}
107
108template <typename N, typename T>
109void egl_object_t::LocalRef<N,T>::release() const {
110    if (ref) {
111        if (ref->decRef() == 1) {
112            // shouldn't happen because this is called from LocalRef
113            ALOGE("LocalRef::release() removed the last reference!");
114        }
115    }
116}
117
118template <typename N, typename T>
119void egl_object_t::LocalRef<N,T>::terminate() {
120    if (ref) {
121        ref->terminate();
122    }
123}
124
125// ----------------------------------------------------------------------------
126
127class egl_surface_t : public egl_object_t {
128protected:
129    ~egl_surface_t();
130public:
131    typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref;
132
133    egl_surface_t(egl_display_t* dpy, EGLConfig config,
134            EGLNativeWindowType win, EGLSurface surface,
135            egl_connection_t const* cnx);
136
137    EGLSurface surface;
138    EGLConfig config;
139    sp<ANativeWindow> win;
140    egl_connection_t const* cnx;
141};
142
143class egl_context_t: public egl_object_t {
144protected:
145    ~egl_context_t() {}
146public:
147    typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref;
148
149    egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
150            egl_connection_t const* cnx, int version);
151
152    void onLooseCurrent();
153    void onMakeCurrent(EGLSurface draw, EGLSurface read);
154
155    EGLDisplay dpy;
156    EGLContext context;
157    EGLConfig config;
158    EGLSurface read;
159    EGLSurface draw;
160    egl_connection_t const* cnx;
161    int version;
162    String8 gl_extensions;
163    Vector<String8> tokenized_gl_extensions;
164};
165
166// ----------------------------------------------------------------------------
167
168typedef egl_surface_t::Ref  SurfaceRef;
169typedef egl_context_t::Ref  ContextRef;
170
171// ----------------------------------------------------------------------------
172
173template<typename NATIVE, typename EGL>
174static inline NATIVE* egl_to_native_cast(EGL arg) {
175    return reinterpret_cast<NATIVE*>(arg);
176}
177
178static inline
179egl_surface_t* get_surface(EGLSurface surface) {
180    return egl_to_native_cast<egl_surface_t>(surface);
181}
182
183static inline
184egl_context_t* get_context(EGLContext context) {
185    return egl_to_native_cast<egl_context_t>(context);
186}
187
188// ----------------------------------------------------------------------------
189}; // namespace android
190// ----------------------------------------------------------------------------
191
192#endif // ANDROID_EGL_OBJECT_H
193