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 #include "nweb_log.h"
21
22 namespace OHOS::NWeb {
23
~NativeImageAdapterImpl()24 NativeImageAdapterImpl::~NativeImageAdapterImpl()
25 {
26 DestroyNativeImage();
27 }
28
CreateNativeImage(uint32_t textureId,uint32_t textureTarget)29 void NativeImageAdapterImpl::CreateNativeImage(uint32_t textureId, uint32_t textureTarget)
30 {
31 ohNativeImage_ = OH_NativeImage_Create(textureId, textureTarget);
32 }
33
AquireNativeWindowFromNativeImage()34 NWebNativeWindow NativeImageAdapterImpl::AquireNativeWindowFromNativeImage()
35 {
36 if (ohNativeImage_ == nullptr) {
37 return nullptr;
38 }
39 return reinterpret_cast<NWebNativeWindow>(OH_NativeImage_AcquireNativeWindow(ohNativeImage_));
40 }
41
AttachContext(uint32_t textureId)42 int32_t NativeImageAdapterImpl::AttachContext(uint32_t textureId)
43 {
44 if (ohNativeImage_ == nullptr) {
45 return SURFACE_ERROR_ERROR;
46 }
47 return OH_NativeImage_AttachContext(ohNativeImage_, textureId);
48 }
49
DetachContext()50 int32_t NativeImageAdapterImpl::DetachContext()
51 {
52 if (ohNativeImage_ == nullptr) {
53 return SURFACE_ERROR_ERROR;
54 }
55 return OH_NativeImage_DetachContext(ohNativeImage_);
56 }
57
UpdateSurfaceImage()58 int32_t NativeImageAdapterImpl::UpdateSurfaceImage()
59 {
60 if (ohNativeImage_ == nullptr) {
61 return SURFACE_ERROR_ERROR;
62 }
63 return OH_NativeImage_UpdateSurfaceImage(ohNativeImage_);
64 }
65
GetTimestamp()66 int64_t NativeImageAdapterImpl::GetTimestamp()
67 {
68 if (ohNativeImage_ == nullptr) {
69 return SURFACE_ERROR_ERROR;
70 }
71 return OH_NativeImage_GetTimestamp(ohNativeImage_);
72 }
73
GetTransformMatrix(float matrix[16])74 int32_t NativeImageAdapterImpl::GetTransformMatrix(float matrix[16])
75 {
76 if (ohNativeImage_ == nullptr) {
77 return SURFACE_ERROR_ERROR;
78 }
79 return OH_NativeImage_GetTransformMatrix(ohNativeImage_, matrix);
80 }
81
GetSurfaceId(uint64_t * surfaceId)82 int32_t NativeImageAdapterImpl::GetSurfaceId(uint64_t* surfaceId)
83 {
84 if (ohNativeImage_ == nullptr) {
85 return SURFACE_ERROR_ERROR;
86 }
87 return OH_NativeImage_GetSurfaceId(ohNativeImage_, surfaceId);
88 }
89
SetOnFrameAvailableListener(std::shared_ptr<FrameAvailableListener> listener)90 int32_t NativeImageAdapterImpl::SetOnFrameAvailableListener(std::shared_ptr<FrameAvailableListener> listener)
91 {
92 if (ohNativeImage_ == nullptr || listener == nullptr) {
93 return SURFACE_ERROR_ERROR;
94 }
95 OH_OnFrameAvailableListener callback;
96 callback.onFrameAvailable = listener->GetOnFrameAvailableCb();
97 callback.context = listener->GetContext();
98 return OH_NativeImage_SetOnFrameAvailableListener(ohNativeImage_, callback);
99 }
100
UnsetOnFrameAvailableListener()101 int32_t NativeImageAdapterImpl::UnsetOnFrameAvailableListener()
102 {
103 if (ohNativeImage_ == nullptr) {
104 return SURFACE_ERROR_ERROR;
105 }
106 return OH_NativeImage_UnsetOnFrameAvailableListener(ohNativeImage_);
107 }
108
DestroyNativeImage()109 void NativeImageAdapterImpl::DestroyNativeImage()
110 {
111 if (ohNativeImage_ == nullptr) {
112 return;
113 }
114 OH_NativeImage_Destroy(&ohNativeImage_);
115 ohNativeImage_ = nullptr;
116 }
117
118
NewNativeImage()119 void NativeImageAdapterImpl::NewNativeImage()
120 {
121 ohNativeImage_ = OH_ConsumerSurface_Create();
122 }
123
AcquireNativeWindowBuffer(void ** windowBuffer,int * acquireFenceFd)124 int32_t NativeImageAdapterImpl::AcquireNativeWindowBuffer(
125 void** windowBuffer,
126 int* acquireFenceFd)
127 {
128 if (ohNativeImage_ == nullptr) {
129 WVLOG_E("native image is null.");
130 return SURFACE_ERROR_ERROR;
131 }
132
133 OHNativeWindowBuffer* buffer = nullptr;
134 int32_t ret = OH_NativeImage_AcquireNativeWindowBuffer(ohNativeImage_, &buffer, acquireFenceFd);
135 if (ret != SURFACE_ERROR_OK || !buffer) {
136 WVLOG_E("acquireNativeWindowBuffer fail. ret = %{public}d or buffer is nullptr", ret);
137 return ret;
138 }
139 *windowBuffer = buffer;
140 return SURFACE_ERROR_OK;
141 }
142
GetNativeBuffer(void * windowBuffer,void ** nativeBuffer)143 int32_t NativeImageAdapterImpl::GetNativeBuffer(
144 void* windowBuffer,
145 void** nativeBuffer)
146 {
147 OH_NativeBuffer* buffer = nullptr;
148 int32_t ret = OH_NativeBuffer_FromNativeWindowBuffer(static_cast<OHNativeWindowBuffer*>(windowBuffer), &buffer);
149 if (ret != SURFACE_ERROR_OK || !buffer) {
150 WVLOG_E("getNativeBuffer fail. ret = %{public}d or buffer is nullptr", ret);
151 return ret;
152 }
153
154 *nativeBuffer = buffer;
155 return SURFACE_ERROR_OK;
156 }
157
ReleaseNativeWindowBuffer(void * windowBuffer,int fenceFd)158 int32_t NativeImageAdapterImpl::ReleaseNativeWindowBuffer(void* windowBuffer, int fenceFd)
159 {
160 if (ohNativeImage_ == nullptr) {
161 return SURFACE_ERROR_ERROR;
162 }
163 return OH_NativeImage_ReleaseNativeWindowBuffer(ohNativeImage_,
164 static_cast<OHNativeWindowBuffer*>(windowBuffer), fenceFd);
165 }
166
GetNativeWindowBufferSize(void * windowBuffer,uint32_t * width,uint32_t * height)167 void NativeImageAdapterImpl::GetNativeWindowBufferSize(void* windowBuffer, uint32_t* width, uint32_t* height)
168 {
169 if (windowBuffer == nullptr || width == nullptr || height == nullptr) {
170 return;
171 }
172 BufferHandle *handle = OH_NativeWindow_GetBufferHandleFromNative(static_cast<OHNativeWindowBuffer*>(windowBuffer));
173 if (handle) {
174 *height = handle->height;
175 *width = handle->width;
176 }
177 }
178 }
179