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