1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 // 2021.2.10 Increase the process used in native_view mode.
6 // Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
7
8 #include "flutter/flow/ohos_layers/texture_register.h"
9
10 #include "flutter/shell/platform/android/platform_view_android_jni.h"
11
12 namespace flutter::OHOS {
13
14 namespace {
15
CheckException(JNIEnv * env)16 bool CheckException(JNIEnv* env)
17 {
18 if (env->ExceptionCheck() == JNI_FALSE) {
19 return true;
20 }
21
22 jthrowable exception = env->ExceptionOccurred();
23 env->ExceptionClear();
24 FML_LOG(ERROR) << fml::jni::GetJavaExceptionInfo(env, exception);
25 env->DeleteLocalRef(exception);
26 return false;
27 }
28
29 static fml::jni::ScopedJavaGlobalRef<jclass>* g_layerTextureClass = nullptr;
30 static jmethodID g_setAlphaMethod = nullptr;
31
SetTextureLayerAlpha(JNIEnv * env,jobject obj,jint alpha)32 void SetTextureLayerAlpha(JNIEnv* env, jobject obj, jint alpha)
33 {
34 env->CallVoidMethod(obj, g_setAlphaMethod, alpha);
35 FML_CHECK(CheckException(env));
36 }
37
38 } // namespace
39
Register(JNIEnv * env)40 bool AceTextureLayer::Register(JNIEnv* env)
41 {
42 g_layerTextureClass = new fml::jni::ScopedJavaGlobalRef<jclass>(
43 env, env->FindClass("ohos/ace/capability/texture/AceLayerTexture"));
44 if (g_layerTextureClass->is_null()) {
45 FML_LOG(ERROR) << "Could not locate AceLayerTexture class";
46 return false;
47 }
48 g_setAlphaMethod = env->GetMethodID(
49 g_layerTextureClass->obj(), "setLayerAlpha", "(I)V");
50
51 if (g_setAlphaMethod == nullptr) {
52 FML_LOG(ERROR) << "Could not locate setLayerAlpha method";
53 return false;
54 }
55 return true;
56 }
57
setAlpha(int32_t alpha)58 void AceTextureLayer::setAlpha(int32_t alpha)
59 {
60 alpha_ = alpha;
61 JNIEnv* env = fml::jni::AttachCurrentThread();
62 fml::jni::ScopedJavaLocalRef<jobject> layer = layerTexture_.get(env);
63 if (!layer.is_null()) {
64 SetTextureLayerAlpha(env, layer.obj(), static_cast<jint>(alpha_));
65 }
66 }
67
RegisterTexture(int64_t id,long textureHandle,const fml::jni::JavaObjectWeakGlobalRef & layerTexture)68 void TextureRegistry::RegisterTexture(int64_t id, long textureHandle,
69 const fml::jni::JavaObjectWeakGlobalRef& layerTexture)
70 {
71 if (mapping_.find(id) == mapping_.end()) {
72 mapping_[id] = { id, textureHandle, layerTexture };
73 } else {
74 mapping_[id].id_ = id;
75 mapping_[id].handle_ = textureHandle;
76 mapping_[id].layerTexture_ = layerTexture;
77 }
78 }
79
RegisterNativeWindow(int64_t id,const void * nativeWindow)80 void TextureRegistry::RegisterNativeWindow(int64_t id, const void* nativeWindow)
81 {
82 if (mapping_.find(id) == mapping_.end()) {
83 mapping_[id] = {
84 .nativeWindow_ = const_cast<void*>(nativeWindow)
85 };
86 } else {
87 mapping_[id].nativeWindow_ = const_cast<void*>(nativeWindow);
88 }
89 }
90
UnregisterTexture(int64_t id)91 void TextureRegistry::UnregisterTexture(int64_t id)
92 {
93 mapping_.erase(id);
94 }
95
GetTexture(int64_t id)96 const AceTextureLayer& TextureRegistry::GetTexture(int64_t id)
97 {
98 return mapping_[id];
99 }
100
101 }
102