1 /*
2 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
3 *
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 <vector>
31
32 #include "gl_layer_stitch_impl.h"
33
34 #define __CLASS__ "GLLayerStitchImpl"
35
36 namespace sdm {
37
38 const float kFullScreenVertices[] = {
39 -1.0f, 3.0f,
40 -1.0f, -1.0f,
41 3.0f, -1.0f
42 };
43
44 const float kFullScreenTexCoords[] = {
45 0.0f, 2.0f,
46 0.0f, 0.0f,
47 2.0f, 0.0f
48 };
49
50 const char *kVertexShader1 = ""
51 "#version 300 es \n"
52 "precision highp float; \n"
53 "layout(location = 0) in vec2 in_pos; \n"
54 "layout(location = 1) in vec2 in_uv; \n"
55 " \n"
56 "out vec2 uv; \n"
57 " \n"
58 "void main() \n"
59 "{ \n"
60 " gl_Position = vec4(in_pos, 0.0, 1.0); \n"
61 " uv = in_uv; \n"
62 "} \n";
63
64 const char *kConvertRenderRGBShader = ""
65 "precision highp float; \n"
66 " \n"
67 "layout(binding = 0) uniform sampler2D u_sTexture; \n"
68 " \n"
69 "in vec2 uv; \n"
70 "out vec4 color; \n"
71 " \n"
72 "void main() \n"
73 "{ \n"
74 " color = texture(u_sTexture, uv); \n"
75 "} \n";
76
IsValid(const GLRect & rect)77 static bool IsValid(const GLRect &rect) {
78 return ((rect.right - rect.left) && (rect.bottom - rect.top));
79 }
80
CreateContext(bool secure)81 int GLLayerStitchImpl::CreateContext(bool secure) {
82 ctx_.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
83 EGL(eglBindAPI(EGL_OPENGL_ES_API));
84
85 // Initialize current display.
86 EGL(eglInitialize(ctx_.egl_display, nullptr, nullptr));
87
88 // Get attributes corresponing to render target.
89 // Describes Framebuffer attributes like buffer depth, color space etc;
90 EGLConfig eglConfig;
91 int numConfig = 0;
92 EGLint eglConfigAttribList[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
93 EGL_RED_SIZE, 8,
94 EGL_GREEN_SIZE, 8,
95 EGL_BLUE_SIZE, 8,
96 EGL_ALPHA_SIZE, 8,
97 EGL_NONE};
98 EGL(eglChooseConfig(ctx_.egl_display, eglConfigAttribList, &eglConfig, 1, &numConfig));
99
100 // When GPU runs in protected context it can read from
101 // - Protected sources
102 // - UnProtected source
103 // and writes into Protected buffer.
104 // VS in UnProtected context it can read/write happen from/to Unprotected sources.
105 EGLint egl_contextAttribList[] = {EGL_CONTEXT_CLIENT_VERSION, 3,
106 secure ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
107 secure ? EGL_TRUE : EGL_NONE,
108 EGL_NONE};
109 ctx_.egl_context = eglCreateContext(ctx_.egl_display, eglConfig, NULL, egl_contextAttribList);
110
111 // eglCreatePbufferSurface creates an off-screen pixel buffer surface and returns its handle
112 EGLint egl_surfaceAttribList[] = {EGL_WIDTH, 1,
113 EGL_HEIGHT, 1,
114 secure ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
115 secure ? EGL_TRUE : EGL_NONE,
116 EGL_NONE};
117 ctx_.egl_surface = eglCreatePbufferSurface(ctx_.egl_display, eglConfig, egl_surfaceAttribList);
118
119 // eglMakeCurrent attaches rendering context to rendering surface.
120 MakeCurrent(&ctx_);
121
122 DLOGI("Created context = %p", (void *)(&ctx_.egl_context));
123
124 // Load Vertex and Fragment shaders.
125 const char *fragment_shaders[2] = { };
126 int count = 0;
127 const char *version = "#version 300 es\n";
128
129 fragment_shaders[count++] = version;
130
131 // ToDo: Add support to yuv_to_rgb shader.
132 fragment_shaders[count++] = kConvertRenderRGBShader;
133
134 ctx_.program_id = LoadProgram(1, &kVertexShader1, count, fragment_shaders);
135
136 SetRealTimePriority();
137 InitContext();
138
139 return 0;
140 }
141
Blit(const std::vector<StitchParams> & stitch_params,shared_ptr<Fence> * release_fence)142 int GLLayerStitchImpl::Blit(const std::vector<StitchParams> &stitch_params,
143 shared_ptr<Fence> *release_fence) {
144 DTRACE_SCOPED();
145
146 std::vector<shared_ptr<Fence>> acquire_fences;
147 std::vector<shared_ptr<Fence>> release_fences;
148 bool can_batch = !NeedsGLScissor(stitch_params);
149 for (auto &info : stitch_params) {
150 SetSourceBuffer(info.src_hnd);
151 SetDestinationBuffer(info.dst_hnd);
152 SetViewport(info.dst_rect);
153 ClearWithTransparency(info.scissor_rect);
154 glDrawArrays(GL_TRIANGLES, 0, 3);
155
156 acquire_fences.push_back(info.src_acquire_fence);
157
158 if (!can_batch) {
159 // Trigger flush and cache release fence.
160 WaitOnInputFence(acquire_fences);
161 shared_ptr<Fence> temp_release_fence = nullptr;
162 CreateOutputFence(&temp_release_fence);
163 release_fences.push_back(temp_release_fence);
164 acquire_fences = {};
165 }
166 }
167
168 if (can_batch) {
169 // Create output fence for client to wait on.
170 WaitOnInputFence(acquire_fences);
171 CreateOutputFence(release_fence);
172 } else {
173 // Merge all fd's and return one.
174 *release_fence = Fence::Merge(release_fences, false);
175 }
176
177 return 0;
178 }
179
NeedsGLScissor(const std::vector<StitchParams> & stitch_params)180 int GLLayerStitchImpl::NeedsGLScissor(const std::vector<StitchParams> &stitch_params) {
181 for (auto &info : stitch_params) {
182 if (IsValid(info.scissor_rect)) {
183 return true;
184 }
185 }
186
187 return false;
188 }
189
Init()190 int GLLayerStitchImpl::Init() {
191 return CreateContext(secure_);
192 }
193
Deinit()194 int GLLayerStitchImpl::Deinit() {
195 MakeCurrent(&ctx_);
196 DestroyContext(&ctx_);
197
198 return 0;
199 }
200
ClearWithTransparency(const GLRect & scissor_rect)201 void GLLayerStitchImpl::ClearWithTransparency(const GLRect &scissor_rect) {
202 if (!IsValid(scissor_rect)) {
203 // Disable scissor.
204 GL(glDisable(GL_SCISSOR_TEST));
205 return;
206 }
207
208 DTRACE_SCOPED();
209 // Enable scissor test.
210 GL(glEnable(GL_SCISSOR_TEST));
211 GL(glScissor(scissor_rect.left, scissor_rect.top, scissor_rect.right - scissor_rect.left,
212 scissor_rect.bottom - scissor_rect.top));
213 GL(glClearColor(0, 0, 0, 0));
214 GL(glClear(GL_COLOR_BUFFER_BIT));
215 }
216
InitContext()217 void GLLayerStitchImpl::InitContext() {
218 // eglMakeCurrent attaches rendering context to rendering surface.
219 MakeCurrent(&ctx_);
220 SetProgram(ctx_.program_id);
221 SetRealTimePriority();
222 // Set vertices.
223 glEnableVertexAttribArray(0);
224 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, kFullScreenVertices);
225 glEnableVertexAttribArray(1);
226 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, kFullScreenTexCoords);
227 }
228
~GLLayerStitchImpl()229 GLLayerStitchImpl::~GLLayerStitchImpl() {}
230
GLLayerStitchImpl(bool secure)231 GLLayerStitchImpl::GLLayerStitchImpl(bool secure) {
232 secure_ = secure;
233 }
234
235 } // namespace sdm
236
237