1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "render_mesh.h"
17 #include "shader_pass_on_screen.h"
18 #include "shader_pass_program.h"
19
20 namespace OHOS {
21 namespace Media {
22 constexpr const char* DEFAULT_VERTEX_SHADER_CODE = R"(attribute vec4 aPosition;
23 attribute vec4 aTextureCoord;
24 varying vec2 textureCoordinate;
25 void main()
26 {
27 gl_Position = aPosition;
28 textureCoordinate = aTextureCoord.xy;
29 }
30 )";
31
32 constexpr const char* DEFAULT_FRAGMENT_SHADER_CODE = R"(precision highp float;
33 varying vec2 textureCoordinate;
34 uniform sampler2D inputTexture;
35 void main()
36 {
37 gl_FragColor = texture2D(inputTexture, textureCoordinate);
38 }
39 )";
40
ShaderPassOnScreen(RenderContext * context)41 ShaderPassOnScreen::ShaderPassOnScreen(RenderContext* context)
42 : ShaderPass(context, DEFAULT_VERTEX_SHADER_CODE, DEFAULT_FRAGMENT_SHADER_CODE, DEFAULT_FLIP_VERTEX_DATA)
43 {
44 renderEffectData_ = std::make_shared<RenderEffectData>();
45 }
46
~ShaderPassOnScreen()47 ShaderPassOnScreen::~ShaderPassOnScreen() {}
48
GetRenderEffectData()49 RenderEffectDataPtr ShaderPassOnScreen::GetRenderEffectData()
50 {
51 return renderEffectData_;
52 }
53
Render()54 RenderTexturePtr ShaderPassOnScreen::Render()
55 {
56 if (shader_ == nullptr) {
57 shader_ = std::make_shared<ShaderPassProgram>(context_, vertexShaderCode_, fragmentShaderCode_);
58 }
59 glBindFramebuffer(GL_FRAMEBUFFER, 0);
60 glClearColor(0, 0, 0, 0);
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
62 glViewport(0, 0, renderEffectData_->outputWidth_, renderEffectData_->outputHeight_);
63 shader_->Bind();
64 renderMesh_->Bind(shader_->GetShader());
65 PreDraw();
66 glDrawArrays(renderMesh_->primitiveType_, 0, renderMesh_->vertexNum_);
67 PostDraw();
68 shader_->Unbind();
69 return nullptr;
70 }
71
PreDraw()72 void ShaderPassOnScreen::PreDraw()
73 {
74 if (renderEffectData_->inputTexture_ != nullptr) {
75 shader_->BindTexture("inputTexture", 0, renderEffectData_->inputTexture_->GetTextureId());
76 }
77 }
78
PostDraw()79 void ShaderPassOnScreen::PostDraw()
80 {
81 if (renderEffectData_->inputTexture_ != nullptr) {
82 shader_->UnBindTexture(0);
83 renderEffectData_->inputTexture_.reset();
84 }
85 }
86 }
87 }