• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "surface_utils.h"
21 
22 using namespace OHOS;
23 
24 struct OH_NativeImage {
25     OHOS::sptr<OHOS::SurfaceImage> consumer;
26     OHOS::sptr<OHOS::IBufferProducer> producer;
27     OHOS::sptr<OHOS::Surface> pSurface = nullptr;
28     struct NativeWindow* nativeWindow = nullptr;
29 };
30 
OH_NativeImage_Create(uint32_t textureId,uint32_t textureTarget)31 OH_NativeImage* OH_NativeImage_Create(uint32_t textureId, uint32_t textureTarget)
32 {
33     OHOS::sptr<OHOS::SurfaceImage> surfaceImage = new SurfaceImage(textureId, textureTarget);
34     sptr<OHOS::IBufferProducer> producer = surfaceImage->GetProducer();
35     OH_NativeImage* nativeImage = new OH_NativeImage();
36     nativeImage->consumer = surfaceImage;
37     nativeImage->producer = producer;
38     sptr<IBufferConsumerListener> listener = new SurfaceImageListener(surfaceImage);
39     nativeImage->consumer->RegisterConsumerListener(listener);
40     return nativeImage;
41 }
42 
OH_NativeImage_AcquireNativeWindow(OH_NativeImage * image)43 OHNativeWindow* OH_NativeImage_AcquireNativeWindow(OH_NativeImage* image)
44 {
45     if (image == nullptr) {
46         BLOGE("parameter error, please check input parameter");
47         return nullptr;
48     }
49 
50     if (image->nativeWindow == nullptr) {
51         if (image->pSurface == nullptr) {
52             image->pSurface = Surface::CreateSurfaceAsProducer(image->producer);
53         }
54         BLOGE_CHECK_AND_RETURN_RET(image->pSurface != nullptr, nullptr, "pSurface is null");
55         image->nativeWindow = CreateNativeWindowFromSurface(&(image->pSurface));
56     }
57 
58     return image->nativeWindow;
59 }
60 
OH_NativeImage_AttachContext(OH_NativeImage * image,uint32_t textureId)61 int32_t OH_NativeImage_AttachContext(OH_NativeImage* image, uint32_t textureId)
62 {
63     if (image == nullptr) {
64         BLOGE("parameter error, please check input parameter");
65         return SURFACE_ERROR_ERROR;
66     }
67     return image->consumer->AttachContext(textureId);
68 }
69 
OH_NativeImage_DetachContext(OH_NativeImage * image)70 int32_t OH_NativeImage_DetachContext(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->DetachContext();
77 }
78 
OH_NativeImage_UpdateSurfaceImage(OH_NativeImage * image)79 int32_t OH_NativeImage_UpdateSurfaceImage(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->UpdateSurfaceImage();
86 }
87 
OH_NativeImage_GetTimestamp(OH_NativeImage * image)88 int64_t OH_NativeImage_GetTimestamp(OH_NativeImage* image)
89 {
90     if (image == nullptr) {
91         BLOGE("parameter error, please check input parameter");
92         return SURFACE_ERROR_ERROR;
93     }
94     return image->consumer->GetTimeStamp();
95 }
96 
OH_NativeImage_GetTransformMatrix(OH_NativeImage * image,float matrix[16])97 int32_t OH_NativeImage_GetTransformMatrix(OH_NativeImage* image, float matrix[16])
98 {
99     if (image == nullptr) {
100         BLOGE("parameter error, please check input parameter");
101         return SURFACE_ERROR_ERROR;
102     }
103     return image->consumer->GetTransformMatrix(matrix);
104 }
105 
OH_NativeImage_GetSurfaceId(OH_NativeImage * image,uint64_t * surfaceId)106 int32_t OH_NativeImage_GetSurfaceId(OH_NativeImage* image, uint64_t* surfaceId)
107 {
108     if (image == nullptr || surfaceId == nullptr || image->consumer == nullptr) {
109         BLOGE("parameter error, please check input parameter");
110         return SURFACE_ERROR_ERROR;
111     }
112     *surfaceId = image->consumer->GetUniqueId();
113 
114     if (image->pSurface == nullptr) {
115         image->pSurface = Surface::CreateSurfaceAsProducer(image->producer);
116     }
117     BLOGE_CHECK_AND_RETURN_RET(image->pSurface != nullptr, SURFACE_ERROR_ERROR, "pSurface is null");
118     SurfaceUtils* utils = SurfaceUtils::GetInstance();
119     utils->Add(*surfaceId, image->pSurface);
120     return SURFACE_ERROR_OK;
121 }
122 
OH_NativeImage_SetOnFrameAvailableListener(OH_NativeImage * image,OH_OnFrameAvailableListener listener)123 int32_t OH_NativeImage_SetOnFrameAvailableListener(OH_NativeImage* image, OH_OnFrameAvailableListener listener)
124 {
125     if (image == nullptr || image->consumer == nullptr) {
126         BLOGE("parameter error, please check input parameter");
127         return SURFACE_ERROR_ERROR;
128     }
129     return image->consumer->SetOnBufferAvailableListener(listener.context, listener.onFrameAvailable);
130 }
131 
OH_NativeImage_UnsetOnFrameAvailableListener(OH_NativeImage * image)132 int32_t OH_NativeImage_UnsetOnFrameAvailableListener(OH_NativeImage* image)
133 {
134     if (image == nullptr || image->consumer == nullptr) {
135         BLOGE("parameter error, please check input parameter");
136         return SURFACE_ERROR_ERROR;
137     }
138     return image->consumer->UnsetOnBufferAvailableListener();
139 }
140 
OH_NativeImage_Destroy(OH_NativeImage ** image)141 void OH_NativeImage_Destroy(OH_NativeImage** image)
142 {
143     if (image == nullptr || *image == nullptr) {
144         BLOGE("parameter error, please check input parameter");
145         return;
146     }
147     if ((*image)->consumer != nullptr) {
148         (void)(*image)->consumer->UnsetOnBufferAvailableListener();
149         SurfaceUtils* utils = SurfaceUtils::GetInstance();
150         utils->Remove((*image)->consumer->GetUniqueId());
151     }
152 
153     if ((*image)->nativeWindow != nullptr) {
154         DestoryNativeWindow((*image)->nativeWindow);
155         (*image)->nativeWindow = nullptr;
156     }
157 
158     delete *image;
159     *image = nullptr;
160 }
161