• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_surface.h"
17 #include "shader_pass_program.h"
18 
19 namespace OHOS {
20 namespace Media {
21 #ifndef GL_TEXTURE_EXTERNAL_OES
22 #define GL_TEXTURE_EXTERNAL_OES 0x8D65
23 #endif
24 
25 constexpr const char* SURFACE_VERTEX_SHADER_CODE = R"(uniform mat4 uTexMatrix;
26     attribute vec4 aPosition;
27     attribute vec4 aTextureCoord;
28     varying vec2 vTextureCoord;
29     void main() {
30         gl_Position = aPosition;
31         vTextureCoord = (uTexMatrix * vec4(aTextureCoord.xy, 0.0, 1.0)).xy;
32     }
33     )";
34 
35 constexpr const char* SURFACE_FRAGMENT_SHADER_CODE = R"(#extension GL_OES_EGL_image_external : require
36     precision mediump float;
37     varying vec2 vTextureCoord;
38     uniform samplerExternalOES sTexture;
39     void main() {
40         gl_FragColor = texture2D(sTexture, vTextureCoord);
41     }
42     )";
43 
ShaderPassSurface(RenderContext * context)44 ShaderPassSurface::ShaderPassSurface(RenderContext* context)
45     : ShaderPass(context, SURFACE_VERTEX_SHADER_CODE, SURFACE_FRAGMENT_SHADER_CODE, DEFAULT_VERTEX_DATA)
46 {
47     renderEffectData_ = std::make_shared<SurfaceData>();
48 }
49 
~ShaderPassSurface()50 ShaderPassSurface::~ShaderPassSurface() {}
51 
GetRenderEffectData()52 SurfaceDataPtr ShaderPassSurface::GetRenderEffectData()
53 {
54     return std::static_pointer_cast<SurfaceData>(renderEffectData_);
55 }
56 
PreDraw()57 void ShaderPassSurface::PreDraw()
58 {
59     SurfaceDataPtr surfaceData = std::static_pointer_cast<SurfaceData>(renderEffectData_);
60     if (shader_) {
61         shader_->BindTexture("sTexture", 0, surfaceData->nativeTexId, GL_TEXTURE_EXTERNAL_OES);
62         shader_->SetMat4("uTexMatrix", surfaceData->uTexMatrix);
63     }
64 }
65 
PostDraw()66 void ShaderPassSurface::PostDraw()
67 {
68     SurfaceDataPtr surfaceData = std::static_pointer_cast<SurfaceData>(renderEffectData_);
69     if (surfaceData->inputTexture_ != nullptr) {
70         shader_->UnBindTexture(0);
71         surfaceData->inputTexture_.reset();
72     }
73 }
74 }
75 }