1 /*
2 * Copyright (c) 2023 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 "native_image_adapter_impl.h"
17
18 #include "graphic_common_c.h"
19 #include "iconsumer_surface.h"
20
21 namespace OHOS::NWeb {
22
~NativeImageAdapterImpl()23 NativeImageAdapterImpl::~NativeImageAdapterImpl()
24 {
25 DestroyNativeImage();
26 }
27
CreateNativeImage(uint32_t textureId,uint32_t textureTarget)28 void NativeImageAdapterImpl::CreateNativeImage(uint32_t textureId, uint32_t textureTarget)
29 {
30 ohNativeImage_ = OH_NativeImage_Create(textureId, textureTarget);
31 }
32
AquireNativeWindowFromNativeImage()33 NWebNativeWindow NativeImageAdapterImpl::AquireNativeWindowFromNativeImage()
34 {
35 if (ohNativeImage_ == nullptr) {
36 return nullptr;
37 }
38 return reinterpret_cast<NWebNativeWindow>(OH_NativeImage_AcquireNativeWindow(ohNativeImage_));
39 }
40
AttachContext(uint32_t textureId)41 int32_t NativeImageAdapterImpl::AttachContext(uint32_t textureId)
42 {
43 if (ohNativeImage_ == nullptr) {
44 return SURFACE_ERROR_ERROR;
45 }
46 return OH_NativeImage_AttachContext(ohNativeImage_, textureId);
47 }
48
DetachContext()49 int32_t NativeImageAdapterImpl::DetachContext()
50 {
51 if (ohNativeImage_ == nullptr) {
52 return SURFACE_ERROR_ERROR;
53 }
54 return OH_NativeImage_DetachContext(ohNativeImage_);
55 }
56
UpdateSurfaceImage()57 int32_t NativeImageAdapterImpl::UpdateSurfaceImage()
58 {
59 if (ohNativeImage_ == nullptr) {
60 return SURFACE_ERROR_ERROR;
61 }
62 return OH_NativeImage_UpdateSurfaceImage(ohNativeImage_);
63 }
64
GetTimestamp()65 int64_t NativeImageAdapterImpl::GetTimestamp()
66 {
67 if (ohNativeImage_ == nullptr) {
68 return SURFACE_ERROR_ERROR;
69 }
70 return OH_NativeImage_GetTimestamp(ohNativeImage_);
71 }
72
GetTransformMatrix(float matrix[16])73 int32_t NativeImageAdapterImpl::GetTransformMatrix(float matrix[16])
74 {
75 if (ohNativeImage_ == nullptr) {
76 return SURFACE_ERROR_ERROR;
77 }
78 return OH_NativeImage_GetTransformMatrix(ohNativeImage_, matrix);
79 }
80
GetSurfaceId(uint64_t * surfaceId)81 int32_t NativeImageAdapterImpl::GetSurfaceId(uint64_t* surfaceId)
82 {
83 if (ohNativeImage_ == nullptr) {
84 return SURFACE_ERROR_ERROR;
85 }
86 return OH_NativeImage_GetSurfaceId(ohNativeImage_, surfaceId);
87 }
88
SetOnFrameAvailableListener(OnFrameAvailableListener * listener)89 int32_t NativeImageAdapterImpl::SetOnFrameAvailableListener(OnFrameAvailableListener* listener)
90 {
91 if (ohNativeImage_ == nullptr || listener == nullptr) {
92 return SURFACE_ERROR_ERROR;
93 }
94 OH_OnFrameAvailableListener callback;
95 callback.onFrameAvailable = listener->cb;
96 callback.context = listener->context;
97 return OH_NativeImage_SetOnFrameAvailableListener(ohNativeImage_, callback);
98 }
99
UnsetOnFrameAvailableListener()100 int32_t NativeImageAdapterImpl::UnsetOnFrameAvailableListener()
101 {
102 if (ohNativeImage_ == nullptr) {
103 return SURFACE_ERROR_ERROR;
104 }
105 return OH_NativeImage_UnsetOnFrameAvailableListener(ohNativeImage_);
106 }
107
DestroyNativeImage()108 void NativeImageAdapterImpl::DestroyNativeImage()
109 {
110 if (ohNativeImage_ == nullptr) {
111 return;
112 }
113 OH_NativeImage_Destroy(&ohNativeImage_);
114 ohNativeImage_ = nullptr;
115 }
116 }