summaryrefslogtreecommitdiff
path: root/opengl/libs/EGL/egl_object.cpp
blob: efbe6135425aa40ab27ceabc4428c13bdf642d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 ** Copyright 2007, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

#include "egl_object.h"

#include <sstream>

namespace android {

egl_object_t::egl_object_t(egl_display_t* disp) : display(disp), count(1) {
    // NOTE: this does an implicit incRef
    display->addObject(this);
}

egl_object_t::~egl_object_t() {}

void egl_object_t::terminate() {
    // this marks the object as "terminated"
    display->removeObject(this);
    if (decRef() == 1) {
        // shouldn't happen because this is called from LocalRef
        ALOGE("egl_object_t::terminate() removed the last reference!");
    }
}

void egl_object_t::destroy() {
    if (decRef() == 1) {
        delete this;
    }
}

bool egl_object_t::get(egl_display_t const* display, egl_object_t* object) {
    // used by LocalRef, this does an incRef() atomically with
    // checking that the object is valid.
    return display->getObject(object);
}

egl_surface_t::egl_surface_t(egl_display_t* dpy, EGLConfig config, EGLNativeWindowType win,
                             EGLSurface surface, EGLint colorSpace, egl_connection_t const* cnx)
      : egl_object_t(dpy),
        surface(surface),
        config(config),
        win(win),
        cnx(cnx),
        connected(true),
        colorSpace(colorSpace),
        egl_smpte2086_dirty(false),
        egl_cta861_3_dirty(false) {
    egl_smpte2086_metadata.displayPrimaryRed = {EGL_DONT_CARE, EGL_DONT_CARE};
    egl_smpte2086_metadata.displayPrimaryGreen = {EGL_DONT_CARE, EGL_DONT_CARE};
    egl_smpte2086_metadata.displayPrimaryBlue = {EGL_DONT_CARE, EGL_DONT_CARE};
    egl_smpte2086_metadata.whitePoint = {EGL_DONT_CARE, EGL_DONT_CARE};
    egl_smpte2086_metadata.maxLuminance = EGL_DONT_CARE;
    egl_smpte2086_metadata.minLuminance = EGL_DONT_CARE;
    egl_cta861_3_metadata.maxFrameAverageLightLevel = EGL_DONT_CARE;
    egl_cta861_3_metadata.maxContentLightLevel = EGL_DONT_CARE;

    if (win) {
        win->incStrong(this);
    }
}

egl_surface_t::~egl_surface_t() {
    if (win != nullptr) {
        disconnect();
        win->decStrong(this);
    }
}

void egl_surface_t::disconnect() {
    if (win != nullptr && connected) {
        // NOTE: When using Vulkan backend, the Vulkan runtime makes all the
        // native_window_* calls, so don't do them here.
        if (!cnx->useAngle) {
            native_window_set_buffers_format(win, 0);
            if (native_window_api_disconnect(win, NATIVE_WINDOW_API_EGL)) {
                ALOGW("EGLNativeWindowType %p disconnect failed", win);
            }
        }
        connected = false;
    }
}

EGLBoolean egl_surface_t::setSmpte2086Attribute(EGLint attribute, EGLint value) {
    switch (attribute) {
        case EGL_SMPTE2086_DISPLAY_PRIMARY_RX_EXT:
            egl_smpte2086_metadata.displayPrimaryRed.x = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_RY_EXT:
            egl_smpte2086_metadata.displayPrimaryRed.y = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_GX_EXT:
            egl_smpte2086_metadata.displayPrimaryGreen.x = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_GY_EXT:
            egl_smpte2086_metadata.displayPrimaryGreen.y = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_BX_EXT:
            egl_smpte2086_metadata.displayPrimaryBlue.x = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_BY_EXT:
            egl_smpte2086_metadata.displayPrimaryBlue.y = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_WHITE_POINT_X_EXT:
            egl_smpte2086_metadata.whitePoint.x = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_WHITE_POINT_Y_EXT:
            egl_smpte2086_metadata.whitePoint.y = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_MAX_LUMINANCE_EXT:
            egl_smpte2086_metadata.maxLuminance = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
        case EGL_SMPTE2086_MIN_LUMINANCE_EXT:
            egl_smpte2086_metadata.minLuminance = value;
            egl_smpte2086_dirty = true;
            return EGL_TRUE;
    }
    return EGL_FALSE;
}

EGLBoolean egl_surface_t::setCta8613Attribute(EGLint attribute, EGLint value) {
    switch (attribute) {
        case EGL_CTA861_3_MAX_CONTENT_LIGHT_LEVEL_EXT:
            egl_cta861_3_metadata.maxContentLightLevel = value;
            egl_cta861_3_dirty = true;
            return EGL_TRUE;
        case EGL_CTA861_3_MAX_FRAME_AVERAGE_LEVEL_EXT:
            egl_cta861_3_metadata.maxFrameAverageLightLevel = value;
            egl_cta861_3_dirty = true;
            return EGL_TRUE;
    }
    return EGL_FALSE;
}

EGLBoolean egl_surface_t::getSmpte2086Metadata(android_smpte2086_metadata& metadata) const {
    if (!egl_smpte2086_dirty) return EGL_FALSE;
    if (egl_smpte2086_metadata.displayPrimaryRed.x == EGL_DONT_CARE ||
        egl_smpte2086_metadata.displayPrimaryRed.y == EGL_DONT_CARE ||
        egl_smpte2086_metadata.displayPrimaryGreen.x == EGL_DONT_CARE ||
        egl_smpte2086_metadata.displayPrimaryGreen.y == EGL_DONT_CARE ||
        egl_smpte2086_metadata.displayPrimaryBlue.x == EGL_DONT_CARE ||
        egl_smpte2086_metadata.displayPrimaryBlue.y == EGL_DONT_CARE ||
        egl_smpte2086_metadata.whitePoint.x == EGL_DONT_CARE ||
        egl_smpte2086_metadata.whitePoint.y == EGL_DONT_CARE ||
        egl_smpte2086_metadata.maxLuminance == EGL_DONT_CARE ||
        egl_smpte2086_metadata.minLuminance == EGL_DONT_CARE) {
        ALOGW("egl_surface_t: incomplete SMPTE 2086 metadata!");
        return EGL_FALSE;
    }

    metadata.displayPrimaryRed.x = static_cast<float>(egl_smpte2086_metadata.displayPrimaryRed.x) /
            EGL_METADATA_SCALING_EXT;
    metadata.displayPrimaryRed.y = static_cast<float>(egl_smpte2086_metadata.displayPrimaryRed.y) /
            EGL_METADATA_SCALING_EXT;
    metadata.displayPrimaryGreen.x =
            static_cast<float>(egl_smpte2086_metadata.displayPrimaryGreen.x) /
            EGL_METADATA_SCALING_EXT;
    metadata.displayPrimaryGreen.y =
            static_cast<float>(egl_smpte2086_metadata.displayPrimaryGreen.y) /
            EGL_METADATA_SCALING_EXT;
    metadata.displayPrimaryBlue.x =
            static_cast<float>(egl_smpte2086_metadata.displayPrimaryBlue.x) /
            EGL_METADATA_SCALING_EXT;
    metadata.displayPrimaryBlue.y =
            static_cast<float>(egl_smpte2086_metadata.displayPrimaryBlue.y) /
            EGL_METADATA_SCALING_EXT;
    metadata.whitePoint.x =
            static_cast<float>(egl_smpte2086_metadata.whitePoint.x) / EGL_METADATA_SCALING_EXT;
    metadata.whitePoint.y =
            static_cast<float>(egl_smpte2086_metadata.whitePoint.y) / EGL_METADATA_SCALING_EXT;
    metadata.maxLuminance =
            static_cast<float>(egl_smpte2086_metadata.maxLuminance) / EGL_METADATA_SCALING_EXT;
    metadata.minLuminance =
            static_cast<float>(egl_smpte2086_metadata.minLuminance) / EGL_METADATA_SCALING_EXT;

    return EGL_TRUE;
}

EGLBoolean egl_surface_t::getCta8613Metadata(android_cta861_3_metadata& metadata) const {
    if (!egl_cta861_3_dirty) return EGL_FALSE;

    if (egl_cta861_3_metadata.maxContentLightLevel == EGL_DONT_CARE ||
        egl_cta861_3_metadata.maxFrameAverageLightLevel == EGL_DONT_CARE) {
        ALOGW("egl_surface_t: incomplete CTA861.3 metadata!");
        return EGL_FALSE;
    }

    metadata.maxContentLightLevel = static_cast<float>(egl_cta861_3_metadata.maxContentLightLevel) /
            EGL_METADATA_SCALING_EXT;
    metadata.maxFrameAverageLightLevel =
            static_cast<float>(egl_cta861_3_metadata.maxFrameAverageLightLevel) /
            EGL_METADATA_SCALING_EXT;

    return EGL_TRUE;
}

EGLBoolean egl_surface_t::getColorSpaceAttribute(EGLint attribute, EGLint* value) const {
    if (attribute == EGL_GL_COLORSPACE_KHR) {
        *value = colorSpace;
        return EGL_TRUE;
    }
    return EGL_FALSE;
}

EGLBoolean egl_surface_t::getSmpte2086Attribute(EGLint attribute, EGLint* value) const {
    switch (attribute) {
        case EGL_SMPTE2086_DISPLAY_PRIMARY_RX_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryRed.x;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_RY_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryRed.y;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_GX_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryGreen.x;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_GY_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryGreen.y;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_BX_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryBlue.x;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_DISPLAY_PRIMARY_BY_EXT:
            *value = egl_smpte2086_metadata.displayPrimaryBlue.y;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_WHITE_POINT_X_EXT:
            *value = egl_smpte2086_metadata.whitePoint.x;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_WHITE_POINT_Y_EXT:
            *value = egl_smpte2086_metadata.whitePoint.y;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_MAX_LUMINANCE_EXT:
            *value = egl_smpte2086_metadata.maxLuminance;
            return EGL_TRUE;
            break;
        case EGL_SMPTE2086_MIN_LUMINANCE_EXT:
            *value = egl_smpte2086_metadata.minLuminance;
            return EGL_TRUE;
            break;
    }
    return EGL_FALSE;
}

EGLBoolean egl_surface_t::getCta8613Attribute(EGLint attribute, EGLint* value) const {
    switch (attribute) {
        case EGL_CTA861_3_MAX_CONTENT_LIGHT_LEVEL_EXT:
            *value = egl_cta861_3_metadata.maxContentLightLevel;
            return EGL_TRUE;
            break;
        case EGL_CTA861_3_MAX_FRAME_AVERAGE_LEVEL_EXT:
            *value = egl_cta861_3_metadata.maxFrameAverageLightLevel;
            return EGL_TRUE;
            break;
    }
    return EGL_FALSE;
}

void egl_surface_t::terminate() {
    disconnect();
    egl_object_t::terminate();
}

egl_context_t::egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config,
                             egl_connection_t const* cnx, int version)
      : egl_object_t(get_display(dpy)),
        dpy(dpy),
        context(context),
        config(config),
        read(nullptr),
        draw(nullptr),
        cnx(cnx),
        version(version) {}

void egl_context_t::onLooseCurrent() {
    read = nullptr;
    draw = nullptr;
}

void egl_context_t::onMakeCurrent(EGLSurface draw, EGLSurface read) {
    this->read = read;
    this->draw = draw;

    /*
     * Here we cache the GL_EXTENSIONS string for this context and we
     * add the extensions always handled by the wrapper
     */
    if (!gl_extensions.empty()) return;

    // call the implementation's glGetString(GL_EXTENSIONS)
    const char* exts = (const char*)gEGLImpl.hooks[version]->gl.glGetString(GL_EXTENSIONS);
    if (!exts) return;

    // If this context is sharing with another context, and the other context was reset
    // e.g. due to robustness failure, this context might also be reset and glGetString can
    // return NULL.
    gl_extensions = exts;
    if (gl_extensions.find("GL_EXT_debug_marker") == std::string::npos) {
        gl_extensions.insert(0, "GL_EXT_debug_marker ");
        // eglGetProcAddress could return function pointers to these
        // functions while they actually don't work. Fix them now.
        __eglMustCastToProperFunctionPointerType* f;
        f = (__eglMustCastToProperFunctionPointerType*)&gEGLImpl.hooks[version]
                    ->gl.glInsertEventMarkerEXT;
        if (*f != gl_noop) *f = gl_noop;
        f = (__eglMustCastToProperFunctionPointerType*)&gEGLImpl.hooks[version]
                    ->gl.glPushGroupMarkerEXT;
        if (*f != gl_noop) *f = gl_noop;
        f = (__eglMustCastToProperFunctionPointerType*)&gEGLImpl.hooks[version]
                    ->gl.glPopGroupMarkerEXT;
        if (*f != gl_noop) *f = gl_noop;
    }

    // tokenize the supported extensions for the glGetStringi() wrapper
    std::stringstream ss;
    std::string str;
    ss << gl_extensions;
    while (ss >> str) {
        tokenized_gl_extensions.push_back(str);
    }
}

}; // namespace android