blob: 9550cbee33cef63d8100900e3a4b68b84c6a99b3 [file] [log] [blame]
Zach Reizner6cbe8832015-06-26 18:25:38 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef ANDROID_GL_WORKER_H_
18#define ANDROID_GL_WORKER_H_
19
Puneet Kumard078e4c2015-07-30 03:36:46 +000020#include <pthread.h>
21
Zach Reizner6cbe8832015-06-26 18:25:38 -070022#include <memory>
23#include <vector>
24
25#define EGL_EGLEXT_PROTOTYPES
26#define GL_GLEXT_PROTOTYPES
27
28#include <EGL/egl.h>
29#include <EGL/eglext.h>
30#include <GLES2/gl2.h>
31#include <GLES2/gl2ext.h>
32
Zach Reiznerdac5d192015-07-15 18:44:37 -070033#include <ui/GraphicBuffer.h>
34
Zach Reizner6cbe8832015-06-26 18:25:38 -070035struct hwc_layer_1;
36
37namespace android {
38
39#define AUTO_GL_TYPE(name, type, zero, deleter) \
40 struct name##Deleter { \
41 typedef type pointer; \
42 \
43 void operator()(pointer p) const { \
44 if (p != zero) { \
45 deleter; \
46 } \
47 } \
48 }; \
49 typedef std::unique_ptr<type, name##Deleter> name;
50
51AUTO_GL_TYPE(AutoGLFramebuffer, GLuint, 0, glDeleteFramebuffers(1, &p))
52AUTO_GL_TYPE(AutoGLBuffer, GLuint, 0, glDeleteBuffers(1, &p))
53AUTO_GL_TYPE(AutoGLTexture, GLuint, 0, glDeleteTextures(1, &p))
54AUTO_GL_TYPE(AutoGLShader, GLint, 0, glDeleteShader(p))
55AUTO_GL_TYPE(AutoGLProgram, GLint, 0, glDeleteProgram(p))
56
57struct EGLImageDeleter {
58 typedef EGLImageKHR pointer;
59
60 EGLDisplay egl_display_;
61
62 EGLImageDeleter(EGLDisplay egl_display) : egl_display_(egl_display) {
63 }
64
65 void operator()(EGLImageKHR p) const {
66 if (p != EGL_NO_IMAGE_KHR) {
67 eglDestroyImageKHR(egl_display_, p);
68 }
69 }
70};
71typedef std::unique_ptr<EGLImageKHR, EGLImageDeleter> AutoEGLImageKHR;
72
73struct AutoEGLImageAndGLTexture {
74 AutoEGLImageKHR image;
75 AutoGLTexture texture;
76
77 AutoEGLImageAndGLTexture(EGLDisplay egl_display)
78 : image(EGL_NO_IMAGE_KHR, EGLImageDeleter(egl_display)) {
79 }
80};
81
Zach Reiznerdac5d192015-07-15 18:44:37 -070082class GLWorkerCompositor {
83 public:
84 GLWorkerCompositor();
85 ~GLWorkerCompositor();
86
87 int Init();
88
89 int Composite(hwc_layer_1 *layers, size_t num_layers,
90 sp<GraphicBuffer> framebuffer);
Zach Reizner713a6782015-07-31 15:12:44 -070091 int CompositeAndFinish(hwc_layer_1 *layers, size_t num_layers,
92 sp<GraphicBuffer> framebuffer);
Zach Reiznerdac5d192015-07-15 18:44:37 -070093
94 private:
95 EGLDisplay egl_display_;
96 EGLContext egl_ctx_;
97
98 std::vector<AutoGLProgram> blend_programs_;
99 AutoGLBuffer vertex_buffer_;
100};
Puneet Kumard078e4c2015-07-30 03:36:46 +0000101
102class GLWorker {
103 public:
104 struct Work {
105 hwc_layer_1 *layers;
106 size_t num_layers;
107 int timeline_fd;
108 sp<GraphicBuffer> framebuffer;
109
110 Work() = default;
111 Work(const Work &rhs) = delete;
112 };
113
114 GLWorker();
115 ~GLWorker();
116
117 int Init();
118
119 int DoWork(Work *work);
120
121 private:
122 bool initialized_;
123 pthread_t thread_;
124 pthread_mutex_t lock_;
125 pthread_cond_t work_ready_cond_;
126 pthread_cond_t work_done_cond_;
127 Work *worker_work_;
128 bool work_ready_;
129 bool worker_exit_;
130 int worker_ret_;
131
132 void WorkerRoutine();
133 int DoComposition(GLWorkerCompositor &compositor, Work *work);
134
135 int SignalWorker(Work *work, bool worker_exit);
136
137 static void *StartRoutine(void *arg);
138};
Zach Reizner6cbe8832015-06-26 18:25:38 -0700139}
140
141#endif