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 "shader_pass_rotate.h"
17 #include "shader_pass.h"
18 #include "shader_pass_surface.h"
19 #include "render/graphics/graphics_render_engine.h"
20 #include "shader_pass_program.h"
21 #include "render_mesh.h"
22 #include "media_log.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_VIDEOEDITOR, "VideoEditorRender"};
28 }
29
30 constexpr const char* SURFACE_VERTEX_SHADER_CODE = R"(uniform mat4 uTexMatrix;
31 attribute vec4 aPosition;
32 attribute vec4 aTextureCoord;
33 varying vec2 vTextureCoord;
34 void main() {
35 gl_Position = aPosition;
36 vTextureCoord = (uTexMatrix * vec4(aTextureCoord.xy, 0.0, 1.0)).xy;
37 }
38 )";
39
40 constexpr const char* SURFACE_ROTATE_FRAGMENT_SHADER_CODE = R"(
41 precision mediump float;
42 varying vec2 vTextureCoord;
43 uniform sampler2D sTexture;
44 void main() {
45 gl_FragColor = texture2D(sTexture, vTextureCoord);
46 }
47 )";
48
ShaderPassRotate(RenderContext * context)49 ShaderPassRotate::ShaderPassRotate(RenderContext* context)
50 : ShaderPass(context, SURFACE_VERTEX_SHADER_CODE, SURFACE_ROTATE_FRAGMENT_SHADER_CODE, DEFAULT_VERTEX_DATA)
51 {
52 renderEffectData_ = std::make_shared<SurfaceData>();
53 }
54
~ShaderPassRotate()55 ShaderPassRotate::~ShaderPassRotate() {}
56
GetRenderEffectData()57 SurfaceDataPtr ShaderPassRotate::GetRenderEffectData()
58 {
59 return std::static_pointer_cast<SurfaceData>(renderEffectData_);
60 }
61
PreDraw()62 void ShaderPassRotate::PreDraw()
63 {
64 SurfaceDataPtr surfaceData = std::static_pointer_cast<SurfaceData>(renderEffectData_);
65 if (shader_ == nullptr || surfaceData == nullptr) {
66 MEDIA_LOGE("ShaderPassRotate::PreDraw failed because the shader or surfaceData is nullptr.");
67 return;
68 }
69 if (surfaceData->inputTexture_ != nullptr) {
70 shader_->BindTexture("sTexture", 0, surfaceData->inputTexture_->GetTextureId());
71 }
72 shader_->SetMat4("uTexMatrix", surfaceData->uTexMatrix);
73 }
74
PostDraw()75 void ShaderPassRotate::PostDraw()
76 {
77 SurfaceDataPtr surfaceData = std::static_pointer_cast<SurfaceData>(renderEffectData_);
78 if (surfaceData->inputTexture_ != nullptr) {
79 shader_->UnBindTexture(0);
80 surfaceData->inputTexture_.reset();
81 }
82 }
83 }
84 }