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 "gl_layer_stitch_impl.h"
31
32 #define __CLASS__ "GLLayerStitchImpl"
33
34 namespace sdm {
35
36 const float kFullScreenVertices[] = {
37 -1.0f, 3.0f,
38 -1.0f, -1.0f,
39 3.0f, -1.0f
40 };
41
42 const float kFullScreenTexCoords[] = {
43 0.0f, 2.0f,
44 0.0f, 0.0f,
45 2.0f, 0.0f
46 };
47
48 const char *kVertexShader1 = ""
49 "#version 300 es \n"
50 "precision highp float; \n"
51 "layout(location = 0) in vec2 in_pos; \n"
52 "layout(location = 1) in vec2 in_uv; \n"
53 " \n"
54 "out vec2 uv; \n"
55 " \n"
56 "void main() \n"
57 "{ \n"
58 " gl_Position = vec4(in_pos, 0.0, 1.0); \n"
59 " uv = in_uv; \n"
60 "} \n";
61
62 const char *kConvertRenderRGBShader = ""
63 "precision highp float; \n"
64 " \n"
65 "layout(binding = 0) uniform sampler2D u_sTexture; \n"
66 " \n"
67 "in vec2 uv; \n"
68 "out vec4 color; \n"
69 " \n"
70 "void main() \n"
71 "{ \n"
72 " color = texture(u_sTexture, uv); \n"
73 "} \n";
74
CreateContext(bool secure)75 int GLLayerStitchImpl::CreateContext(bool secure) {
76 ctx_.egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
77 EGL(eglBindAPI(EGL_OPENGL_ES_API));
78
79 // Initialize current display.
80 EGL(eglInitialize(ctx_.egl_display, nullptr, nullptr));
81
82 // Get attributes corresponing to render target.
83 // Describes Framebuffer attributes like buffer depth, color space etc;
84 EGLConfig eglConfig;
85 int numConfig = 0;
86 EGLint eglConfigAttribList[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
87 EGL_RED_SIZE, 8,
88 EGL_GREEN_SIZE, 8,
89 EGL_BLUE_SIZE, 8,
90 EGL_ALPHA_SIZE, 8,
91 EGL_NONE};
92 EGL(eglChooseConfig(ctx_.egl_display, eglConfigAttribList, &eglConfig, 1, &numConfig));
93
94 // When GPU runs in protected context it can read from
95 // - Protected sources
96 // - UnProtected source
97 // and writes into Protected buffer.
98 // VS in UnProtected context it can read/write happen from/to Unprotected sources.
99 EGLint egl_contextAttribList[] = {EGL_CONTEXT_CLIENT_VERSION, 3,
100 secure ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
101 secure ? EGL_TRUE : EGL_NONE,
102 EGL_NONE};
103 ctx_.egl_context = eglCreateContext(ctx_.egl_display, eglConfig, NULL, egl_contextAttribList);
104
105 // eglCreatePbufferSurface creates an off-screen pixel buffer surface and returns its handle
106 EGLint egl_surfaceAttribList[] = {EGL_WIDTH, 1,
107 EGL_HEIGHT, 1,
108 secure ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
109 secure ? EGL_TRUE : EGL_NONE,
110 EGL_NONE};
111 ctx_.egl_surface = eglCreatePbufferSurface(ctx_.egl_display, eglConfig, egl_surfaceAttribList);
112
113 // eglMakeCurrent attaches rendering context to rendering surface.
114 MakeCurrent(&ctx_);
115
116 DLOGI("Created context = %p", (void *)(&ctx_.egl_context));
117
118 // Load Vertex and Fragment shaders.
119 const char *fragment_shaders[2] = { };
120 int count = 0;
121 const char *version = "#version 300 es\n";
122
123 fragment_shaders[count++] = version;
124
125 // ToDo: Add support to yuv_to_rgb shader.
126 fragment_shaders[count++] = kConvertRenderRGBShader;
127
128 ctx_.program_id = LoadProgram(1, &kVertexShader1, count, fragment_shaders);
129
130 SetRealTimePriority();
131
132 return 0;
133 }
134
Blit(const private_handle_t * src_hnd,const private_handle_t * dst_hnd,const GLRect & src_rect,const GLRect & dst_rect,const GLRect & scissor_rect,const shared_ptr<Fence> & src_acquire_fence,const shared_ptr<Fence> & dst_acquire_fence,shared_ptr<Fence> * release_fence)135 int GLLayerStitchImpl::Blit(const private_handle_t *src_hnd, const private_handle_t *dst_hnd,
136 const GLRect &src_rect, const GLRect &dst_rect,
137 const GLRect &scissor_rect,
138 const shared_ptr<Fence> &src_acquire_fence,
139 const shared_ptr<Fence> &dst_acquire_fence,
140 shared_ptr<Fence> *release_fence) {
141 DTRACE_SCOPED();
142 // eglMakeCurrent attaches rendering context to rendering surface.
143 MakeCurrent(&ctx_);
144
145 SetProgram(ctx_.program_id);
146
147 ClearWithTransparency(scissor_rect);
148
149 SetSourceBuffer(src_hnd);
150 SetDestinationBuffer(dst_hnd, dst_rect);
151
152 glEnableVertexAttribArray(0);
153 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, kFullScreenVertices);
154 glEnableVertexAttribArray(1);
155 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, kFullScreenTexCoords);
156 glDrawArrays(GL_TRIANGLES, 0, 3);
157
158 // Dst. is always guaranteed to be signaled.
159 WaitOnInputFence(src_acquire_fence);
160
161 // Create output fence for client to wait on.
162 CreateOutputFence(release_fence);
163
164 return 0;
165 }
166
Init()167 int GLLayerStitchImpl::Init() {
168 return CreateContext(secure_);
169 }
170
Deinit()171 int GLLayerStitchImpl::Deinit() {
172 MakeCurrent(&ctx_);
173 DestroyContext(&ctx_);
174
175 return 0;
176 }
177
ClearWithTransparency(const GLRect & scissor_rect)178 void GLLayerStitchImpl::ClearWithTransparency(const GLRect &scissor_rect) {
179 DTRACE_SCOPED();
180 GL(glScissor(scissor_rect.left, scissor_rect.top, scissor_rect.right - scissor_rect.left,
181 scissor_rect.bottom - scissor_rect.top));
182 GL(glEnable(GL_SCISSOR_TEST));
183 GL(glClearColor(0, 0, 0, 0));
184 GL(glClear(GL_COLOR_BUFFER_BIT));
185 }
186
~GLLayerStitchImpl()187 GLLayerStitchImpl::~GLLayerStitchImpl() {}
188
GLLayerStitchImpl(bool secure)189 GLLayerStitchImpl::GLLayerStitchImpl(bool secure) {
190 secure_ = secure;
191 }
192
193 } // namespace sdm
194
195