blob: 4d4744d07ef4c3db0985ac2f294746b0608c03e0 [file] [log] [blame]
Pullakavi Srinivas17495f12019-09-16 20:25:20 +05301/*
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05302 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
Pullakavi Srinivas17495f12019-09-16 20:25:20 +05303 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "gl_common.h"
31
32#define __CLASS__ "GLCommon"
33
34namespace sdm {
35
36GLuint GLCommon::LoadProgram(int vertex_entries, const char **vertex, int fragment_entries,
37 const char **fragment) {
38 GLuint prog_id = glCreateProgram();
39
40 int vert_id = glCreateShader(GL_VERTEX_SHADER);
41 int frag_id = glCreateShader(GL_FRAGMENT_SHADER);
42
43 GL(glShaderSource(vert_id, vertex_entries, vertex, 0));
44 GL(glCompileShader(vert_id));
45 DumpShaderLog(vert_id);
46
47 GL(glShaderSource(frag_id, fragment_entries, fragment, 0));
48 GL(glCompileShader(frag_id));
49 DumpShaderLog(frag_id);
50
51 GL(glAttachShader(prog_id, vert_id));
52 GL(glAttachShader(prog_id, frag_id));
53
54 GL(glLinkProgram(prog_id));
55
56 GL(glDetachShader(prog_id, vert_id));
57 GL(glDetachShader(prog_id, frag_id));
58
59 GL(glDeleteShader(vert_id));
60 GL(glDeleteShader(frag_id));
61
62 return prog_id;
63}
64
65void GLCommon::DumpShaderLog(int shader) {
66 int success = 0;
67 GLchar infoLog[512];
68 GL(glGetShaderiv(shader, GL_COMPILE_STATUS, &success));
69 if (!success) {
70 glGetShaderInfoLog(shader, 512, NULL, infoLog);
71 DLOGI("Shader Failed to compile: %s\n", infoLog);
72 }
73}
74
75void GLCommon::MakeCurrent(const GLContext* ctx) {
76 DTRACE_SCOPED();
77 EGL(eglMakeCurrent(ctx->egl_display, ctx->egl_surface, ctx->egl_surface, ctx->egl_context));
78}
79
80void GLCommon::SetProgram(uint32_t id) {
81 DTRACE_SCOPED();
82 GL(glUseProgram(id));
83}
84
85void GLCommon::DeleteProgram(uint32_t id) {
86 DTRACE_SCOPED();
87 GL(glDeleteProgram(id));
88}
89
90void GLCommon::SetSourceBuffer(const private_handle_t *src_hnd) {
91 DTRACE_SCOPED();
92 EGLImageBuffer *src_buffer = image_wrapper_.wrap(reinterpret_cast<const void *>(src_hnd));
93
94 GL(glActiveTexture(GL_TEXTURE0));
95 if (src_buffer) {
96 GL(glBindTexture(GL_TEXTURE_2D, src_buffer->getTexture(GL_TEXTURE_2D)));
97 }
98}
99
Pullakavi Srinivas9d60fef2019-11-18 12:09:25 +0530100void GLCommon::SetDestinationBuffer(const private_handle_t *dst_hnd, const GLRect &dst_rect) {
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530101 DTRACE_SCOPED();
102 EGLImageBuffer *dst_buffer = image_wrapper_.wrap(reinterpret_cast<const void *>(dst_hnd));
103
104 if (dst_buffer) {
105 GL(glBindFramebuffer(GL_FRAMEBUFFER, dst_buffer->getFramebuffer()));
Pullakavi Srinivas9d60fef2019-11-18 12:09:25 +0530106 float width = dst_rect.right - dst_rect.left;
107 float height = dst_rect.bottom - dst_rect.top;
108 GL(glViewport(dst_rect.left, dst_rect.top, width, height));
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530109 }
110}
111
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530112int GLCommon::WaitOnInputFence(const shared_ptr<Fence> &in_fence) {
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530113 DTRACE_SCOPED();
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530114
115 int fd = Fence::Dup(in_fence);
116 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd, EGL_NONE};
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530117 EGLSyncKHR sync = eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_NATIVE_FENCE_ANDROID,
118 attribs);
119
120 if (sync == EGL_NO_SYNC_KHR) {
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530121 DLOGE("Failed to create sync from source fd: %s", Fence::GetStr(in_fence).c_str());
122 close(fd);
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530123 return -1;
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530124 }
125
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530126 EGL(eglWaitSyncKHR(eglGetCurrentDisplay(), sync, 0));
127 EGL(eglDestroySyncKHR(eglGetCurrentDisplay(), sync));
128
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530129 return 0;
130}
131
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530132int GLCommon::CreateOutputFence(shared_ptr<Fence> *out_fence) {
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530133 DTRACE_SCOPED();
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530134 EGLSyncKHR sync = eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
135
136 // Swap buffer.
137 GL(glFlush());
138
139 if (sync == EGL_NO_SYNC_KHR) {
140 DLOGE("Failed to create egl sync fence");
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530141 return -1;
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530142 }
143
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530144 int status = 0;
145 int fd = eglDupNativeFenceFDANDROID(eglGetCurrentDisplay(), sync);
146 if (fd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
147 status = -1;
148 DLOGE("Failed to dup sync");
149 } else {
Dileep Marchya58928122020-01-28 12:16:51 +0530150 *out_fence = Fence::Create(fd, "gl_out_fence");
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530151 }
152 EGL(eglDestroySyncKHR(eglGetCurrentDisplay(), sync));
153
154 return status;
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530155}
156
Pullakavi Srinivas1c87eee2019-11-05 20:08:31 +0530157void GLCommon::DestroyContext(GLContext* ctx) {
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530158 DTRACE_SCOPED();
Pullakavi Srinivas1c87eee2019-11-05 20:08:31 +0530159
160 // Clear egl image buffers.
161 image_wrapper_.Deinit();
162
163 EGL(DeleteProgram(ctx->program_id));
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530164 EGL(eglMakeCurrent(ctx->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
165 EGL(eglDestroySurface(ctx->egl_display, ctx->egl_surface));
166 EGL(eglDestroyContext(ctx->egl_display, ctx->egl_context));
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530167 EGL(eglTerminate(ctx->egl_display));
168}
169
Pullakavi Srinivasc99bd742019-12-14 18:15:17 +0530170void GLCommon::ClearCache() {
171 // Clear cached handles.
172 image_wrapper_.Deinit();
173 image_wrapper_.Init();
174}
175
Pullakavi Srinivasbf857702020-03-12 16:55:12 +0530176void GLCommon::SetRealTimePriority() {
177 // same as composer thread
178 struct sched_param param = {0};
179 param.sched_priority = 2;
180 if (sched_setscheduler(0, SCHED_FIFO | SCHED_RESET_ON_FORK, &param) != 0) {
181 DLOGE("Couldn't set SCHED_FIFO: %d", errno);
182 }
183}
184
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530185} // namespace sdm
186