@@ -83,6 +83,7 @@ static std::string GenerateVertexShader(int layer_count) {
8383 return vertex_shader_stream.str ();
8484}
8585
86+ #if 0
8687static std::string GenerateFragmentShader(int layer_count) {
8788 std::ostringstream fragment_shader_stream;
8889 fragment_shader_stream << "#version 300 es\n"
@@ -99,7 +100,7 @@ static std::string GenerateFragmentShader(int layer_count) {
99100 << "in vec2 fTexCoords[LAYER_COUNT];\n"
100101 << "out vec4 oFragColor;\n"
101102 << "void main() {\n"
102- << " vec3 color = vec3(0.0, 0.0, 0.0) ;\n "
103+ << " vec3 color = gl_LastFragData[0].rgb ;\n"
103104 << " float alphaCover = 1.0;\n"
104105 << " vec4 texSample;\n"
105106 << " vec3 multRgb;\n"
@@ -132,6 +133,40 @@ static std::string GenerateFragmentShader(int layer_count) {
132133 return fragment_shader_stream.str();
133134}
134135
136+ #else
137+ static std::string GenerateFragmentShader (int layer_count) {
138+ std::ostringstream fragment_shader_stream;
139+ fragment_shader_stream << " #version 300 es\n "
140+ << " #define LAYER_COUNT " << 1 << " \n "
141+ << " #extension GL_OES_EGL_image_external : require\n "
142+ << " precision mediump float;\n " ;
143+ fragment_shader_stream << " uniform samplerExternalOES uLayerTexture\n " ;
144+
145+ fragment_shader_stream << " uniform float uLayerAlpha;\n "
146+ << " uniform float uLayerPremult;\n "
147+ << " uniform vec4 uLayerColor;\n "
148+ << " in vec2 fTexCoords;\n "
149+ << " out vec4 oFragColor;\n "
150+ << " void main() {\n "
151+ << " vec3 color = gl_LastFragData[0].rgb;\n "
152+ << " float alphaCover = 1.0 - gl_LastFragData[0].a;\n "
153+ << " vec4 texSample;\n "
154+ << " vec3 multRgb;\n "
155+ << " float tempAlpha;\n " ;
156+ << " if (alphaCover > 0.5/255.0) { \n " ;
157+ // clang-format off
158+ fragment_shader_stream << " texSample = texture2D(uLayerTexture, fTexCoords); \n "
159+ << " texSample.rgb = texSample.rgb + uLayerColor.rgb; \n "
160+ << " tempAlpha = min(texSample.a, uLayerColor.a); \n "
161+ << " multRgb = texSample.rgb *max(tempAlpha, uLayerPremult); \n "
162+ << " color += multRgb * uLayerAlpha * alphaCover; \n "
163+ << " alphaCover *= (1.0 - texSample.a - uLayerAlpha;\n "
164+ << " oFragColor = vec4(color, 1.0 - alphaCover);\n "
165+ << " }}\n " ;
166+ return fragment_shader_stream.str ();
167+ #endif
168+
169+
135170#if defined(LOAD_PREBUILT_SHADER_FILE) || defined(USE_PREBUILT_SHADER_BIN_ARRAY)
136171static GLint LoadPreBuiltBinary (GLint gl_program, void *binary, long size) {
137172 GLint status;
@@ -311,7 +346,7 @@ GLProgram::~GLProgram() {
311346
312347bool GLProgram::Init (unsigned texture_count) {
313348 std::ostringstream shader_log;
314- program_ = GenerateProgram (texture_count , &shader_log);
349+ program_ = GenerateProgram (1 , &shader_log);
315350 if (!program_) {
316351 ETRACE (" %s" , shader_log.str ().c_str ());
317352 return false ;
@@ -331,13 +366,13 @@ void GLProgram::UseProgram(const RenderState &state, GLuint viewport_width,
331366 premult_loc_ = glGetUniformLocation (program_, " uLayerPremult" );
332367 tex_matrix_loc_ = glGetUniformLocation (program_, " uTexMatrix" );
333368 solid_color_loc_ = glGetUniformLocation (program_, " uLayerColor" );
334- for (unsigned src_index = 0 ; src_index < size; src_index++) {
335- std::ostringstream texture_name_formatter;
336- texture_name_formatter << " uLayerTexture" << src_index ;
337- GLuint tex_loc =
369+ // for (unsigned src_index = 0; src_index < size; src_index++) {
370+ std::ostringstream texture_name_formatter;
371+ texture_name_formatter << " uLayerTexture" ;
372+ GLuint tex_loc =
338373 glGetUniformLocation (program_, texture_name_formatter.str ().c_str ());
339- glUniform1i (tex_loc, src_index);
340- }
374+ glUniform1i (tex_loc, src_index);
375+ // }
341376
342377 initialized_ = true ;
343378 }
@@ -347,22 +382,22 @@ void GLProgram::UseProgram(const RenderState &state, GLuint viewport_width,
347382 (state.width_ ) / (float )viewport_width,
348383 (state.height_ ) / (float )viewport_height);
349384
350- for (unsigned src_index = 0 ; src_index < size; src_index++) {
351- const RenderState::LayerState &src = state.layer_state_ [src_index ];
352- glUniform1f (alpha_loc_ + src_index , src.alpha_ );
353- glUniform1f (premult_loc_ + src_index , src.premult_ );
354- glUniform4f (crop_loc_ + src_index , src.crop_bounds_ [0 ], src.crop_bounds_ [1 ],
385+ // for (unsigned src_index = 0; src_index < size; src_index++) {
386+ const RenderState::LayerState &src = state.layer_state_ [0 ];
387+ glUniform1f (alpha_loc_, src.alpha_ );
388+ glUniform1f (premult_loc_, src.premult_ );
389+ glUniform4f (crop_loc_, src.crop_bounds_ [0 ], src.crop_bounds_ [1 ],
355390 src.crop_bounds_ [2 ] - src.crop_bounds_ [0 ],
356391 src.crop_bounds_ [3 ] - src.crop_bounds_ [1 ]);
357- glUniformMatrix2fv (tex_matrix_loc_ + src_index , 1 , GL_FALSE,
392+ glUniformMatrix2fv (tex_matrix_loc_, 1 , GL_FALSE,
358393 src.texture_matrix_ );
359- glActiveTexture (GL_TEXTURE0 + src_index );
394+ glActiveTexture (GL_TEXTURE0);
360395 glBindTexture (GL_TEXTURE_EXTERNAL_OES, src.handle_ );
361- glUniform4f (solid_color_loc_ + src_index , (float )src.solid_color_array_ [3 ],
396+ glUniform4f (solid_color_loc_, (float )src.solid_color_array_ [3 ],
362397 (float )src.solid_color_array_ [2 ],
363398 (float )src.solid_color_array_ [1 ],
364399 (float )src.solid_color_array_ [0 ]);
365- }
400+ // }
366401}
367402
368403} // namespace hwcomposer
0 commit comments