1 /*
2 * Copyright (c) 2022 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.h"
17 #include "surface.h"
18 #include "surface_image.h"
19 #include "window.h"
20
21 using namespace OHOS;
22
23 struct OH_NativeImage {
24 OHOS::sptr<OHOS::SurfaceImage> consumer;
25 OHOS::sptr<OHOS::IBufferProducer> producer;
26 };
27
OH_NativeImage_Create(uint32_t textureId,uint32_t textureTarget)28 OH_NativeImage* OH_NativeImage_Create(uint32_t textureId, uint32_t textureTarget)
29 {
30 OHOS::sptr<OHOS::SurfaceImage> surfaceImage = new SurfaceImage(textureId, textureTarget);
31 sptr<OHOS::IBufferProducer> producer = surfaceImage->GetProducer();
32 OH_NativeImage* nativeImage = new OH_NativeImage();
33 nativeImage->consumer = surfaceImage;
34 nativeImage->producer = producer;
35 sptr<IBufferConsumerListener> listener = new SurfaceImageListener(surfaceImage);
36 nativeImage->consumer->RegisterConsumerListener(listener);
37 return nativeImage;
38 }
39
OH_NativeImage_AcquireNativeWindow(OH_NativeImage * image)40 OHNativeWindow* OH_NativeImage_AcquireNativeWindow(OH_NativeImage* image)
41 {
42 if (image == nullptr) {
43 BLOGE("parameter error, please check input parameter");
44 return nullptr;
45 }
46 sptr<OHOS::IBufferProducer> producer = image->producer;
47 sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
48 struct NativeWindow* nativeWindow = CreateNativeWindowFromSurface(&pSurface);
49 return nativeWindow;
50 }
51
OH_NativeImage_AttachContext(OH_NativeImage * image,uint32_t textureId)52 int32_t OH_NativeImage_AttachContext(OH_NativeImage* image, uint32_t textureId)
53 {
54 if (image == nullptr) {
55 BLOGE("parameter error, please check input parameter");
56 return SURFACE_ERROR_ERROR;
57 }
58 return image->consumer->AttachContext(textureId);
59 }
60
OH_NativeImage_DetachContext(OH_NativeImage * image)61 int32_t OH_NativeImage_DetachContext(OH_NativeImage* image)
62 {
63 if (image == nullptr) {
64 BLOGE("parameter error, please check input parameter");
65 return SURFACE_ERROR_ERROR;
66 }
67 return image->consumer->DetachContext();
68 }
69
OH_NativeImage_UpdateSurfaceImage(OH_NativeImage * image)70 int32_t OH_NativeImage_UpdateSurfaceImage(OH_NativeImage* image)
71 {
72 if (image == nullptr) {
73 BLOGE("parameter error, please check input parameter");
74 return SURFACE_ERROR_ERROR;
75 }
76 return image->consumer->UpdateSurfaceImage();
77 }
78
OH_NativeImage_GetTimestamp(OH_NativeImage * image)79 int64_t OH_NativeImage_GetTimestamp(OH_NativeImage* image)
80 {
81 if (image == nullptr) {
82 BLOGE("parameter error, please check input parameter");
83 return SURFACE_ERROR_ERROR;
84 }
85 return image->consumer->GetTimeStamp();
86 }
87
OH_NativeImage_GetTransformMatrix(OH_NativeImage * image,float matrix[16])88 int32_t OH_NativeImage_GetTransformMatrix(OH_NativeImage* image, float matrix[16])
89 {
90 if (image == nullptr) {
91 BLOGE("parameter error, please check input parameter");
92 return SURFACE_ERROR_ERROR;
93 }
94 return image->consumer->GetTransformMatrix(matrix);
95 }
96
OH_NativeImage_Destroy(OH_NativeImage ** image)97 void OH_NativeImage_Destroy(OH_NativeImage** image)
98 {
99 if (image == nullptr) {
100 BLOGE("parameter error, please check input parameter");
101 return;
102 }
103 delete *image;
104 *image = nullptr;
105 }
106
107