1 /*
2 * Copyright (c) 2021 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 "core/components/video/resource/texture.h"
17
18 #include <sstream>
19
20 #include "base/log/log.h"
21
22 namespace OHOS::Ace {
23
24 const char TEXTURE_ERRORCODE_CREATEFAIL[] = "error_video_000001";
25 const char TEXTURE_ERRORMSG_CREATEFAIL[] = "Unable to initialize video player.";
26
27 const char TEXTURE_METHOD_REFRESH[] = "markTextureFrameAvailable";
28
29 const char SET_TEXTURE_SIZE[] = "setTextureSize";
30 const char TEXTURE_HEIGHT[] = "textureHeight";
31 const char TEXTURE_ID[] = "textureId";
32 const char TEXTURE_WIDTH[] = "textureWidth";
33
~Texture()34 Texture::~Texture()
35 {
36 auto context = context_.Upgrade();
37 if (!context) {
38 LOGE("fail to create texture due to context is null");
39 return;
40 }
41
42 auto resRegister = context->GetPlatformResRegister();
43 if (resRegister == nullptr) {
44 return;
45 }
46 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
47 if (platformTaskExecutor.IsRunOnCurrentThread()) {
48 resRegister->UnregisterEvent(MakeEventHash(TEXTURE_METHOD_REFRESH));
49 } else {
50 auto weak = AceType::WeakClaim(AceType::RawPtr(resRegister));
51 platformTaskExecutor.PostTask([eventHash = MakeEventHash(TEXTURE_METHOD_REFRESH), weak] {
52 auto resRegister = weak.Upgrade();
53 if (resRegister == nullptr) {
54 LOGE("resRegister is nullptr");
55 return;
56 }
57 resRegister->UnregisterEvent(eventHash);
58 }, "ArkUIVideoTextureUnregisterEvent");
59 }
60 }
61
Create(const std::function<void (int64_t)> & onCreate)62 void Texture::Create(const std::function<void(int64_t)>& onCreate)
63 {
64 auto context = context_.Upgrade();
65 if (!context) {
66 LOGE("fail to create texture due to context is null");
67 return;
68 }
69
70 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
71
72 platformTaskExecutor.PostTask([weak = WeakClaim(this), onCreate] {
73 auto texture = weak.Upgrade();
74 if (texture) {
75 texture->CreateTexture(onCreate);
76 }
77 }, "ArkUIVideoCreateTexture");
78 }
79
CreateTexture(const std::function<void (int64_t)> & onCreate)80 void Texture::CreateTexture(const std::function<void(int64_t)>& onCreate)
81 {
82 auto context = context_.Upgrade();
83 auto resRegister = context->GetPlatformResRegister();
84 if (resRegister == nullptr) {
85 return;
86 }
87 id_ = resRegister->CreateResource(type_, PARAM_NONE);
88 if (id_ == INVALID_ID) {
89 if (onError_) {
90 onError_(TEXTURE_ERRORCODE_CREATEFAIL, TEXTURE_ERRORMSG_CREATEFAIL);
91 }
92 return;
93 }
94 hash_ = MakeResourceHash();
95 resRegister->RegisterEvent(
96 MakeEventHash(TEXTURE_METHOD_REFRESH), [weak = WeakClaim(this)](const std::string& param) {
97 auto texture = weak.Upgrade();
98 if (texture) {
99 texture->OnRefresh(param);
100 }
101 });
102
103 if (onCreate) {
104 onCreate(id_);
105 }
106 }
107
OnRefresh(const std::string & param)108 void Texture::OnRefresh(const std::string& param)
109 {
110 if (onRefreshListener_) {
111 onRefreshListener_();
112 }
113 }
114
OnSize(int64_t textureId,int32_t textureWidth,int32_t textureHeight)115 void Texture::OnSize(int64_t textureId, int32_t textureWidth, int32_t textureHeight)
116 {
117 std::stringstream paramStream;
118 paramStream << TEXTURE_ID << PARAM_EQUALS << textureId << PARAM_AND << TEXTURE_WIDTH << PARAM_EQUALS << textureWidth
119 << PARAM_AND << TEXTURE_HEIGHT << PARAM_EQUALS << textureHeight;
120 std::string param = paramStream.str();
121 CallResRegisterMethod(MakeMethodHash(SET_TEXTURE_SIZE), param);
122 }
123
124 } // namespace OHOS::Ace