File: MultipleContexts.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (266 lines) | stat: -rw-r--r-- 9,001 bytes parent folder | download | duplicates (22)
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
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Demonstrates GLES usage with two contexts, one uploading texture data.

#include "SampleApplication.h"
#include "util/random_utils.h"
#include "util/shader_utils.h"
#include "util/test_utils.h"

#include <array>
#include <mutex>
#include <queue>
#include <thread>

struct TextureAndFence
{
    GLuint textureID;
    GLsync fenceSync;
};

constexpr GLuint64 kTimeout         = 10000000;
static constexpr GLint kTextureSize = 2;
constexpr size_t kWindowWidth       = 400;
constexpr size_t kWindowHeight      = 300;

void UpdateThreadLoop(EGLDisplay display,
                      EGLConfig config,
                      EGLContext shareContext,
                      std::mutex *updateThreadMutex,
                      std::queue<TextureAndFence> *updateThreadQueue,
                      std::mutex *mainThreadMutex,
                      std::queue<TextureAndFence> *mainThreadQueue)
{
    angle::RNG rng;

    EGLint contextAttribs[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE};
    EGLContext context      = eglCreateContext(display, config, shareContext, contextAttribs);

    EGLint surfaceAttribs[] = {EGL_NONE};
    EGLSurface surface      = eglCreatePbufferSurface(display, config, surfaceAttribs);

    eglMakeCurrent(display, surface, surface, context);

    for (;;)
    {
        bool hasUpdate = false;
        TextureAndFence textureAndFence;

        {
            std::lock_guard<std::mutex> lock(*updateThreadMutex);
            if (!updateThreadQueue->empty())
            {
                textureAndFence = updateThreadQueue->back();
                hasUpdate       = true;
                updateThreadQueue->pop();
            }
        }

        if (hasUpdate)
        {
            if (textureAndFence.textureID == 0)
            {
                // Signal from the main thread to stop execution.
                break;
            }

            glClientWaitSync(textureAndFence.fenceSync, 0, kTimeout);
            glDeleteSync(textureAndFence.fenceSync);
            glBindTexture(GL_TEXTURE_2D, textureAndFence.textureID);

            std::vector<uint8_t> bytes(3, 0);
            FillVectorWithRandomUBytes(&rng, &bytes);
            bytes.push_back(255);

            std::vector<uint8_t> textureData;
            for (GLint x = 0; x < kTextureSize; ++x)
            {
                for (GLint y = 0; y < kTextureSize; ++y)
                {
                    textureData.insert(textureData.end(), bytes.begin(), bytes.end());
                }
            }

            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, kTextureSize, kTextureSize, GL_RGBA,
                            GL_UNSIGNED_BYTE, textureData.data());

            GLsync mainThreadSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);

            {
                std::lock_guard<std::mutex> lock(*mainThreadMutex);
                mainThreadQueue->push({textureAndFence.textureID, mainThreadSync});
            }
        }

        angle::Sleep(200);
    }

    eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroySurface(display, surface);
    eglDestroyContext(display, context);
}

class MultipleContextsSample : public SampleApplication
{
  public:
    MultipleContextsSample(int argc, char **argv)
        : SampleApplication("MultipleContexts",
                            argc,
                            argv,
                            ClientType::ES3_0,
                            kWindowWidth,
                            kWindowHeight)
    {}

    bool initialize() override
    {
        // Initialize some textures and send them to the update thread.
        glGenTextures(kNumTextures, mTextures.data());

        for (GLuint texture : mTextures)
        {
            glBindTexture(GL_TEXTURE_2D, texture);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureSize, kTextureSize, 0, GL_RGBA,
                         GL_UNSIGNED_BYTE, nullptr);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glBindTexture(GL_TEXTURE_2D, 0);
            GLsync fenceSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);

            mUpdateThreadQueue.push({texture, fenceSync});
        }

        mUpdateThread.reset(new std::thread(UpdateThreadLoop, getDisplay(), getConfig(),
                                            getContext(), &mUpdateThreadMutex, &mUpdateThreadQueue,
                                            &mMainThreadMutex, &mMainThreadQueue));

        mProgram = CompileProgram(angle::essl1_shaders::vs::Texture2D(),
                                  angle::essl1_shaders::fs::Texture2D());
        glUseProgram(mProgram);
        glEnableVertexAttribArray(0);

        glGenBuffers(1, &mVertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, nullptr, GL_DYNAMIC_DRAW);

        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);

        return true;
    }

    void destroy() override
    {
        {
            // Signal the worker thread to stop execution.
            std::lock_guard<std::mutex> lock(mUpdateThreadMutex);
            mUpdateThreadQueue.push({0, 0});
        }

        for (;;)
        {
            {
                std::lock_guard<std::mutex> mainLock(mMainThreadMutex);
                if (mMainThreadQueue.empty())
                {
                    std::lock_guard<std::mutex> updateLock(mUpdateThreadMutex);
                    if (mUpdateThreadQueue.empty())
                    {
                        break;
                    }
                }
                else
                {
                    TextureAndFence textureAndFence = mMainThreadQueue.back();
                    mMainThreadQueue.pop();
                    glClientWaitSync(textureAndFence.fenceSync, 0, kTimeout);
                    glDeleteSync(textureAndFence.fenceSync);
                }
            }
        }

        glDeleteTextures(kNumTextures, mTextures.data());
        glDeleteProgram(mProgram);
        glDeleteBuffers(1, &mVertexBuffer);
    }

    void draw() override
    {
        bool hasUpdate = false;
        TextureAndFence textureAndFence;
        while (!hasUpdate)
        {
            {
                std::lock_guard<std::mutex> lock(mMainThreadMutex);
                if (!mMainThreadQueue.empty())
                {
                    hasUpdate       = true;
                    textureAndFence = mMainThreadQueue.back();
                    mMainThreadQueue.pop();
                }
            }
        }

        glClientWaitSync(textureAndFence.fenceSync, 0, kTimeout);
        glDeleteSync(textureAndFence.fenceSync);
        glBindTexture(GL_TEXTURE_2D, textureAndFence.textureID);

        constexpr size_t kNumRows    = 3;
        constexpr size_t kNumCols    = 4;
        constexpr size_t kTileHeight = kWindowHeight / kNumRows;
        constexpr size_t kTileWidth  = kWindowWidth / kNumCols;

        size_t tileX = mDrawCount % kNumCols;
        size_t tileY = (mDrawCount / kNumCols) % kNumRows;

        mDrawCount++;

        GLfloat tileX0 = static_cast<float>(tileX) / static_cast<float>(kNumCols);
        GLfloat tileY0 = static_cast<float>(tileY) / static_cast<float>(kNumRows);
        GLfloat tileX1 = tileX0 + static_cast<float>(kTileWidth) / static_cast<float>(kWindowWidth);
        GLfloat tileY1 =
            tileY0 + static_cast<float>(kTileHeight) / static_cast<float>(kWindowHeight);

        tileX0 = tileX0 * 2.0f - 1.0f;
        tileX1 = tileX1 * 2.0f - 1.0f;
        tileY0 = tileY0 * 2.0f - 1.0f;
        tileY1 = tileY1 * 2.0f - 1.0f;

        std::vector<GLfloat> vertices = {tileX0, tileY0, tileX0, tileY1, tileX1, tileY0,
                                         tileX1, tileY0, tileX1, tileY1, tileX0, tileY1};

        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices[0]) * vertices.size(), vertices.data());

        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 6);

        GLsync drawSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
        {
            std::lock_guard<std::mutex> lock(mUpdateThreadMutex);
            mUpdateThreadQueue.push({textureAndFence.textureID, drawSync});
        }
    }

  private:
    std::unique_ptr<std::thread> mUpdateThread;

    static constexpr GLuint kNumTextures = 4;
    std::array<GLuint, kNumTextures> mTextures;

    GLuint mProgram      = 0;
    GLuint mVertexBuffer = 0;

    std::mutex mUpdateThreadMutex;
    std::queue<TextureAndFence> mUpdateThreadQueue;
    std::mutex mMainThreadMutex;
    std::queue<TextureAndFence> mMainThreadQueue;
    size_t mDrawCount = 0;
};

int main(int argc, char **argv)
{
    MultipleContextsSample app(argc, argv);
    return app.run();
}