summaryrefslogtreecommitdiff
path: root/stream-servers/gl/glestranslator/GLcommon/GLBackgroundLoader.cpp
blob: c29c84d2d99a1df95af88b2fd2aeff73d99106d2 (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
/*
 * Copyright (C) 2017 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 "GLcommon/GLBackgroundLoader.h"

#include "GLcommon/GLEScontext.h"
#include "GLcommon/SaveableTexture.h"
#include "base/system/System.h"

#include <EGL/eglext.h>
#include <GLES2/gl2.h>

EGLContext s_context = EGL_NO_CONTEXT;
EGLSurface s_surface = EGL_NO_SURFACE;

intptr_t GLBackgroundLoader::main() {
#if SNAPSHOT_PROFILE > 1
    const auto start = get_uptime_ms();
    printf("Starting GL background loading at %" PRIu64 " ms\n", start);
#endif

    if (s_context == EGL_NO_CONTEXT) {
        if (!m_eglIface.createAndBindAuxiliaryContext(&s_context, &s_surface)) {
            return 0;
        }
    } else {
        // In unit tests, we might have torn down EGL. Check for stale
        // context and surface, and recreate them if that happened.
        if (!m_eglIface.bindAuxiliaryContext(s_context, s_surface)) {
            printf("GLBackgroundLoader::%s auxiliary context gone, create a new one\n", __func__);
            m_eglIface.createAndBindAuxiliaryContext(&s_context, &s_surface);
        }
    }

    for (const auto& it : m_textureMap) {
        if (m_interrupted.load(std::memory_order_relaxed)) break;

        // Acquire the texture loader for each load; bail
        // in case something else happened to interrupt loading.
        auto ptr = m_textureLoaderWPtr.lock();
        if (!ptr) {
            break;
        }

        const SaveableTexturePtr& saveable = it.second;
        if (saveable) {
            m_glesIface.restoreTexture(saveable.get());
            // allow other threads to run for a while
            ptr.reset();
            android::base::sleepMs(
                m_loadDelayMs.load(std::memory_order_relaxed));
        }
    }

    m_textureMap.clear();

    m_eglIface.unbindAuxiliaryContext();

#if SNAPSHOT_PROFILE > 1
    const auto end = get_uptime_ms();
    printf("Finished GL background loading at %" PRIu64 " ms (%d ms total)\n",
           end, int(end - start));
#endif

    return 0;
}

bool GLBackgroundLoader::wait(intptr_t* exitStatus) {
    m_loadDelayMs.store(0, std::memory_order_relaxed);
    return Thread::wait();
}

void GLBackgroundLoader::interrupt() {
    m_interrupted.store(true, std::memory_order_relaxed);
}