summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/tests/BufferGenerator.cpp
blob: d74bd559875a9555e1ebb70264d96471d228570f (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright 2018 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.
 */

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"

#include <gui/BufferItemConsumer.h>
#include <gui/Surface.h>

#include <GLES3/gl3.h>
#include <math/vec2.h>
#include <math/vec3.h>
#include <math/vec4.h>

#include "BufferGenerator.h"
#include "BufferGeneratorShader.h"

namespace android {

/* Used to receive the surfaces and fences from egl. The egl buffers are thrown
 * away. The fences are sent to the requester via a callback */
class SurfaceManager {
public:
    /* Returns a fence from egl */
    using BufferCallback = std::function<void(const sp<GraphicBuffer>& buffer, int32_t fence)>;

    /* Listens for a new frame, detaches the buffer and returns the fence
     * through saved callback. */
    class BufferListener : public ConsumerBase::FrameAvailableListener {
    public:
        BufferListener(sp<IGraphicBufferConsumer> consumer, BufferCallback callback)
              : mConsumer(consumer), mCallback(callback) {}

        void onFrameAvailable(const BufferItem& /*item*/) {
            BufferItem item;

            if (mConsumer->acquireBuffer(&item, 0)) return;
            if (mConsumer->detachBuffer(item.mSlot)) return;

            mCallback(item.mGraphicBuffer, item.mFence->dup());
        }

    private:
        sp<IGraphicBufferConsumer> mConsumer;
        BufferCallback mCallback;
    };

    /* Creates a buffer listener that waits on a new frame from the buffer
     * queue. */
    void initialize(uint32_t width, uint32_t height, android_pixel_format_t format,
                    BufferCallback callback) {
        sp<IGraphicBufferProducer> producer;
        sp<IGraphicBufferConsumer> consumer;
        BufferQueue::createBufferQueue(&producer, &consumer);

        consumer->setDefaultBufferSize(width, height);
        consumer->setDefaultBufferFormat(format);

        mBufferItemConsumer =
                sp<BufferItemConsumer>::make(consumer, GraphicBuffer::USAGE_HW_TEXTURE);

        mListener = sp<BufferListener>::make(consumer, callback);
        mBufferItemConsumer->setFrameAvailableListener(mListener);

        mSurface = sp<Surface>::make(producer, true);
    }

    /* Used by Egl manager. The surface is never displayed. */
    sp<Surface> getSurface() const { return mSurface; }

private:
    sp<BufferItemConsumer> mBufferItemConsumer;
    sp<BufferListener> mListener;
    /* Used by Egl manager. The surface is never displayed */
    sp<Surface> mSurface;
};

/* Used to generate valid fences. It is not possible to create a placeholder sync
 * fence for testing. Egl can generate buffers along with a valid fence.
 * The buffer cannot be guaranteed to be the same format across all devices so
 * a CPU filled buffer is used instead. The Egl fence is used along with the
 * CPU filled buffer. */
class EglManager {
public:
    EglManager()
          : mEglDisplay(EGL_NO_DISPLAY), mEglSurface(EGL_NO_SURFACE), mEglContext(EGL_NO_CONTEXT) {}

    ~EglManager() { cleanup(); }

    int initialize(sp<Surface> surface) {
        mSurface = surface;

        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
        if (mEglDisplay == EGL_NO_DISPLAY) return false;

        EGLint major;
        EGLint minor;
        if (!eglInitialize(mEglDisplay, &major, &minor)) {
            ALOGW("Could not initialize EGL");
            return false;
        }

        /* We're going to use a 1x1 pbuffer surface later on
         * The configuration distance doesn't really matter for what we're
         * trying to do */
        EGLint configAttrs[] = {EGL_RENDERABLE_TYPE,
                                EGL_OPENGL_ES2_BIT,
                                EGL_RED_SIZE,
                                8,
                                EGL_GREEN_SIZE,
                                8,
                                EGL_BLUE_SIZE,
                                8,
                                EGL_ALPHA_SIZE,
                                0,
                                EGL_DEPTH_SIZE,
                                24,
                                EGL_STENCIL_SIZE,
                                0,
                                EGL_NONE};

        EGLConfig configs[1];
        EGLint configCnt;
        if (!eglChooseConfig(mEglDisplay, configAttrs, configs, 1, &configCnt)) {
            ALOGW("Could not select EGL configuration");
            eglReleaseThread();
            eglTerminate(mEglDisplay);
            return false;
        }

        if (configCnt <= 0) {
            ALOGW("Could not find EGL configuration");
            eglReleaseThread();
            eglTerminate(mEglDisplay);
            return false;
        }

        /* These objects are initialized below but the default "null" values are
         * used to cleanup properly at any point in the initialization sequence */
        EGLint attrs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
        mEglContext = eglCreateContext(mEglDisplay, configs[0], EGL_NO_CONTEXT, attrs);
        if (mEglContext == EGL_NO_CONTEXT) {
            ALOGW("Could not create EGL context");
            cleanup();
            return false;
        }

        EGLint majorVersion;
        if (!eglQueryContext(mEglDisplay, mEglContext, EGL_CONTEXT_CLIENT_VERSION, &majorVersion)) {
            ALOGW("Could not query EGL version");
            cleanup();
            return false;
        }

        if (majorVersion != 3) {
            ALOGW("Unsupported EGL version");
            cleanup();
            return false;
        }

        EGLint surfaceAttrs[] = {EGL_NONE};
        mEglSurface = eglCreateWindowSurface(mEglDisplay, configs[0], mSurface.get(), surfaceAttrs);
        if (mEglSurface == EGL_NO_SURFACE) {
            ALOGW("Could not create EGL surface");
            cleanup();
            return false;
        }

        if (!eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
            ALOGW("Could not change current EGL context");
            cleanup();
            return false;
        }

        return true;
    }

    void makeCurrent() const { eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext); }

    void present() const { eglSwapBuffers(mEglDisplay, mEglSurface); }

private:
    void cleanup() {
        if (mEglDisplay == EGL_NO_DISPLAY) return;
        if (mEglSurface != EGL_NO_SURFACE) eglDestroySurface(mEglDisplay, mEglSurface);
        if (mEglContext != EGL_NO_CONTEXT) eglDestroyContext(mEglDisplay, mEglContext);

        eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
        eglReleaseThread();
        eglTerminate(mEglDisplay);
    }

    sp<Surface> mSurface;
    EGLDisplay mEglDisplay;
    EGLSurface mEglSurface;
    EGLContext mEglContext;
};

class Program {
public:
    ~Program() {
        if (mInitialized) {
            glDetachShader(mProgram, mVertexShader);
            glDetachShader(mProgram, mFragmentShader);

            glDeleteShader(mVertexShader);
            glDeleteShader(mFragmentShader);

            glDeleteProgram(mProgram);
        }
    }

    bool initialize(const char* vertex, const char* fragment) {
        mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
        if (!mVertexShader) {
            return false;
        }

        mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
        if (!mFragmentShader) {
            return false;
        }

        mProgram = glCreateProgram();
        glAttachShader(mProgram, mVertexShader);
        glAttachShader(mProgram, mFragmentShader);

        glLinkProgram(mProgram);

        GLint status;
        glGetProgramiv(mProgram, GL_LINK_STATUS, &status);
        if (status != GL_TRUE) {
            GLint length = 0;
            glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &length);
            if (length > 1) {
                GLchar log[length];
                glGetProgramInfoLog(mProgram, length, nullptr, &log[0]);
                ALOGE("%s", log);
            }
            ALOGE("Error while linking shaders");
            return false;
        }
        mInitialized = true;
        return true;
    }

    void use() const { glUseProgram(mProgram); }

    void bindVec4(GLint location, vec4 v) const { glUniform4f(location, v.x, v.y, v.z, v.w); }

    void bindVec3(GLint location, const vec3* v, uint32_t count) const {
        glUniform3fv(location, count, &(v->x));
    }

    void bindFloat(GLint location, float v) { glUniform1f(location, v); }

private:
    GLuint buildShader(const char* source, GLenum type) const {
        GLuint shader = glCreateShader(type);
        glShaderSource(shader, 1, &source, nullptr);
        glCompileShader(shader);

        GLint status;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
        if (status != GL_TRUE) {
            ALOGE("Error while compiling shader of type 0x%x:\n===\n%s\n===", type, source);
            // Some drivers return wrong values for GL_INFO_LOG_LENGTH
            // use a fixed size instead
            GLchar log[512];
            glGetShaderInfoLog(shader, sizeof(log), nullptr, &log[0]);
            ALOGE("Shader info log: %s", log);
            return 0;
        }

        return shader;
    }

    GLuint mProgram = 0;
    GLuint mVertexShader = 0;
    GLuint mFragmentShader = 0;
    bool mInitialized = false;
};

BufferGenerator::BufferGenerator()
      : mSurfaceManager(new SurfaceManager), mEglManager(new EglManager), mProgram(new Program) {
    mBufferSize.set(1000.0, 1000.0);

    auto setBufferWithContext =
            std::bind(setBuffer, std::placeholders::_1, std::placeholders::_2, this);
    mSurfaceManager->initialize(mBufferSize.width, mBufferSize.height, HAL_PIXEL_FORMAT_RGBA_8888,
                                setBufferWithContext);

    if (!mEglManager->initialize(mSurfaceManager->getSurface())) return;

    mEglManager->makeCurrent();

    if (!mProgram->initialize(VERTEX_SHADER, FRAGMENT_SHADER)) return;
    mProgram->use();
    mProgram->bindVec4(0,
                       vec4{mBufferSize.width, mBufferSize.height, 1.0f / mBufferSize.width,
                            1.0f / mBufferSize.height});
    mProgram->bindVec3(2, &SPHERICAL_HARMONICS[0], 4);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, &TRIANGLE[0]);

    mInitialized = true;
}

BufferGenerator::~BufferGenerator() {
    mEglManager->makeCurrent();
}

status_t BufferGenerator::get(sp<GraphicBuffer>* outBuffer, sp<Fence>* outFence) {
    // mMutex is used to protect get() from getting called by multiple threads at the same time
    static std::mutex mMutex;
    std::lock_guard lock(mMutex);

    if (!mInitialized) {
        if (outBuffer) {
            *outBuffer = nullptr;
        }
        if (*outFence) {
            *outFence = nullptr;
        }
        return -EINVAL;
    }

    // Generate a buffer and fence. They will be returned through the setBuffer callback
    mEglManager->makeCurrent();

    glClear(GL_COLOR_BUFFER_BIT);

    const std::chrono::duration<float> time = std::chrono::steady_clock::now() - mEpoch;
    mProgram->bindFloat(1, time.count());

    glDrawArrays(GL_TRIANGLES, 0, 3);

    mPending = true;
    mEglManager->present();

    // Wait for the setBuffer callback
    if (!mConditionVariable.wait_for(mMutex, std::chrono::seconds(2),
                                     [this] { return !mPending; })) {
        ALOGE("failed to set buffer and fence");
        return -ETIME;
    }

    // Return buffer and fence
    if (outBuffer) {
        *outBuffer = mGraphicBuffer;
    }
    if (outFence) {
        *outFence = sp<Fence>::make(mFence);
    } else {
        close(mFence);
    }
    mGraphicBuffer = nullptr;
    mFence = -1;

    return NO_ERROR;
}

ui::Size BufferGenerator::getSize() {
    return mBufferSize;
}

// static
void BufferGenerator::setBuffer(const sp<GraphicBuffer>& buffer, int32_t fence,
                                void* bufferGenerator) {
    BufferGenerator* generator = static_cast<BufferGenerator*>(bufferGenerator);
    generator->mGraphicBuffer = buffer;
    generator->mFence = fence;
    generator->mPending = false;
    generator->mConditionVariable.notify_all();
}

} // namespace android

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic pop // ignored "-Wconversion"