2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_GL_WORKER_H_
18 #define ANDROID_GL_WORKER_H_
25 #define EGL_EGLEXT_PROTOTYPES
26 #define GL_GLEXT_PROTOTYPES
29 #include <EGL/eglext.h>
30 #include <GLES2/gl2.h>
31 #include <GLES2/gl2ext.h>
33 #include <ui/GraphicBuffer.h>
39 #define AUTO_GL_TYPE(name, type, zero, deleter) \
40 struct name##Deleter { \
41 typedef type pointer; \
43 void operator()(pointer p) const { \
49 typedef std::unique_ptr<type, name##Deleter> name;
51 AUTO_GL_TYPE(AutoGLFramebuffer, GLuint, 0, glDeleteFramebuffers(1, &p))
52 AUTO_GL_TYPE(AutoGLBuffer, GLuint, 0, glDeleteBuffers(1, &p))
53 AUTO_GL_TYPE(AutoGLTexture, GLuint, 0, glDeleteTextures(1, &p))
54 AUTO_GL_TYPE(AutoGLShader, GLint, 0, glDeleteShader(p))
55 AUTO_GL_TYPE(AutoGLProgram, GLint, 0, glDeleteProgram(p))
57 struct EGLImageDeleter {
58 typedef EGLImageKHR pointer;
60 EGLDisplay egl_display_;
62 EGLImageDeleter(EGLDisplay egl_display) : egl_display_(egl_display) {
65 void operator()(EGLImageKHR p) const {
66 if (p != EGL_NO_IMAGE_KHR) {
67 eglDestroyImageKHR(egl_display_, p);
71 typedef std::unique_ptr<EGLImageKHR, EGLImageDeleter> AutoEGLImageKHR;
73 struct AutoEGLImageAndGLTexture {
74 AutoEGLImageKHR image;
75 AutoGLTexture texture;
77 AutoEGLImageAndGLTexture(EGLDisplay egl_display)
78 : image(EGL_NO_IMAGE_KHR, EGLImageDeleter(egl_display)) {
82 class GLWorkerCompositor {
85 ~GLWorkerCompositor();
89 int Composite(hwc_layer_1 *layers, size_t num_layers,
90 sp<GraphicBuffer> framebuffer);
91 int CompositeAndFinish(hwc_layer_1 *layers, size_t num_layers,
92 sp<GraphicBuffer> framebuffer);
95 EGLDisplay egl_display_;
98 std::vector<AutoGLProgram> blend_programs_;
99 AutoGLBuffer vertex_buffer_;
108 sp<GraphicBuffer> framebuffer;
111 Work(const Work &rhs) = delete;
119 int DoWork(Work *work);
124 pthread_mutex_t lock_;
125 pthread_cond_t work_ready_cond_;
126 pthread_cond_t work_done_cond_;
132 void WorkerRoutine();
133 int DoComposition(GLWorkerCompositor &compositor, Work *work);
135 int SignalWorker(Work *work, bool worker_exit);
137 static void *StartRoutine(void *arg);