• 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 <EGL/egl.h>
17 #include <GLES3/gl3.h>
18 #include <EGL/eglext.h>
19 #include <GLES2/gl2ext.h>
20 #include "image_effect_render.h"
21 #include "native_buffer.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 constexpr char SET_INPUT_TEXTURE[] = "INPUT_TEX";
30 constexpr char SET_OUTPUT_TEXTURE[] = "OUT_TEX";
31 constexpr char SET_TEXTURE_WIDTH[] = "INPUT_WIDTH";
32 constexpr char SET_TEXTURE_HEIGHT[] = "INPUT_HEIGHT";
33 
34 #ifdef IMAGE_EFFECT_SUPPORT
ImageEffectRender(const std::shared_ptr<OH_ImageEffect> & imageEffect)35 ImageEffectRender::ImageEffectRender(const std::shared_ptr<OH_ImageEffect>& imageEffect)
36     : RenderObject(), imageEffect_(imageEffect)
37 {
38     MEDIA_LOGD("construct.");
39     auto initResult = Init();
40     if (initResult == false) {
41         MEDIA_LOGE("init image effect render failed.");
42     }
43     SetReady(initResult);
44     MEDIA_LOGD("construct finish.");
45 }
46 #else
ImageEffectRender()47 ImageEffectRender::ImageEffectRender()
48     : RenderObject()
49 {
50 }
51 #endif
52 
~ImageEffectRender()53 ImageEffectRender::~ImageEffectRender()
54 {
55     MEDIA_LOGD("destruct.");
56     Release();
57     MEDIA_LOGD("destruct finish.");
58 }
59 
Init()60 bool ImageEffectRender::Init()
61 {
62     auto initEffectFilterResult = InitEffectFilter();
63     if (initEffectFilterResult != VEFError::ERR_OK) {
64         MEDIA_LOGE("init effect filter failed with error: %{public}d.", initEffectFilterResult);
65         return false;
66     }
67     return true;
68 }
69 
Release()70 bool ImageEffectRender::Release()
71 {
72     SetReady(false);
73 
74     return true;
75 }
76 
InitEffectFilter()77 VEFError ImageEffectRender::InitEffectFilter()
78 {
79 #ifdef IMAGE_EFFECT_SUPPORT
80     if (effectFilter_ == nullptr) {
81         int32_t filterCount = OH_ImageEffect_GetFilterCount(imageEffect_.get());
82         if (filterCount <= 0) {
83             MEDIA_LOGE("no filter.");
84             return VEFError::ERR_INTERNAL_ERROR;
85         }
86         effectFilter_ = OH_ImageEffect_GetFilter(imageEffect_.get(), 0);
87         if (effectFilter_ == nullptr) {
88             MEDIA_LOGE("cannot get filter.");
89             return VEFError::ERR_INTERNAL_ERROR;
90         }
91     }
92     return VEFError::ERR_OK;
93 #else
94     return VEFError::ERR_INTERNAL_ERROR;
95 #endif
96 }
97 
Render(RenderContext * context,RenderSurface * surface,const RenderTexturePtr & inputRenderTexture,const int32_t colorRange)98 RenderTexturePtr ImageEffectRender::Render(
99     RenderContext *context,
100     RenderSurface* surface,
101     const RenderTexturePtr& inputRenderTexture,
102     const int32_t colorRange)
103 {
104 #ifdef IMAGE_EFFECT_SUPPORT
105     if (!IsReady()) {
106         MEDIA_LOGE("image effect render is not ready.");
107         return inputRenderTexture;
108     }
109     if (inputRenderTexture == nullptr) {
110         MEDIA_LOGE("inputRenderTexture is nullptr.");
111         return inputRenderTexture;
112     }
113     GLsizei width = inputRenderTexture->Width();
114     GLsizei height = inputRenderTexture->Height();
115     colorRange_ = colorRange;
116 
117     auto tex = std::make_shared<RenderTexture>(context, width, height, GL_RGBA8);
118     if (!tex->Init()) {
119         MEDIA_LOGE("init RenderTexture object failed.");
120         return inputRenderTexture;
121     }
122     SetImageValue((int32_t) inputRenderTexture->GetTextureId(), SET_INPUT_TEXTURE);
123     SetImageValue((int32_t) tex->GetTextureId(), SET_OUTPUT_TEXTURE);
124     SetImageValue((int32_t) width, SET_TEXTURE_WIDTH);
125     SetImageValue((int32_t) height, SET_TEXTURE_HEIGHT);
126 
127     OH_EffectFilter_Render(effectFilter_, nullptr, nullptr);
128     return tex;
129 #else
130     return inputRenderTexture;
131 #endif
132 }
133 
SetImageValue(const int32_t value,const char valueName[])134 void ImageEffectRender::SetImageValue(const int32_t value, const char valueName[])
135 {
136 #ifdef IMAGE_EFFECT_SUPPORT
137     ImageEffect_DataValue dataValue = { .int32Value = static_cast<int32_t>(value) };
138     ImageEffect_Any val = {
139         .dataType = ImageEffect_DataType::EFFECT_DATA_TYPE_INT32,
140         .dataValue = dataValue
141     };
142     OH_EffectFilter_SetValue(effectFilter_, valueName, &val);
143 #endif
144 }
145 }
146 }