1 /*
2  * Copyright (c) 2021 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 <iostream>
17 #include "external_window.h"
18 #include "surface.h"
19 
20 using namespace OHOS;
21 
22 namespace {
23 class BufferConsumerListenerTest : public ::OHOS::IBufferConsumerListener {
24 public:
OnBufferAvailable()25     void OnBufferAvailable() override
26     {
27     }
28 };
29 
CreateNativeWindow()30 OHNativeWindow* CreateNativeWindow()
31 {
32     sptr<OHOS::IConsumerSurface> cSurface = IConsumerSurface::Create();
33     sptr<IBufferConsumerListener> listener = new BufferConsumerListenerTest();
34     cSurface->RegisterConsumerListener(listener);
35     sptr<OHOS::IBufferProducer> producer = cSurface->GetProducer();
36     sptr<OHOS::Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
37 
38     OHNativeWindow* nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
39 
40     int code = SET_USAGE;
41     int32_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
42     int32_t ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, usage);
43     if (ret != OHOS::GSERROR_OK) {
44         std::cout << "OH_NativeWindow_NativeWindowHandleOpt SET_USAGE faile" << std::endl;
45     }
46     code = SET_BUFFER_GEOMETRY;
47     int32_t width = 0x100;
48     int32_t height = 0x100;
49     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height);
50     if (ret != OHOS::GSERROR_OK) {
51         std::cout << "OH_NativeWindow_NativeWindowHandleOpt SET_BUFFER_GEOMETRY failed" << std::endl;
52     }
53     code = SET_STRIDE;
54     int32_t stride = 0x8;
55     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride);
56     if (ret != OHOS::GSERROR_OK) {
57         std::cout << "OH_NativeWindow_NativeWindowHandleOpt SET_STRIDE failed" << std::endl;
58     }
59     code = SET_FORMAT;
60     int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
61     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format);
62     if (ret != OHOS::GSERROR_OK) {
63         std::cout << "OH_NativeWindow_NativeWindowHandleOpt SET_FORMAT failed" << std::endl;
64     }
65     return nativeWindow;
66 }
67 
CreateNativeWindowBuffer(OHNativeWindow * nativeWindow,NativeWindowBuffer ** nativeWindowBuffer,int * fenceFd)68 void CreateNativeWindowBuffer(OHNativeWindow* nativeWindow, NativeWindowBuffer** nativeWindowBuffer, int* fenceFd)
69 {
70     NativeWindowBuffer* windowBuffer = nullptr;
71     int fence = -1;
72     ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &windowBuffer, &fence);
73     if (ret != OHOS::GSERROR_OK) {
74         std::cout << "OH_NativeWindow_NativeWindowRequestBuffer failed" << std::endl;
75     }
76 
77     if (OH_NativeWindow_GetBufferHandleFromNative(windowBuffer) == nullptr) {
78         std::cout << "OH_NativeWindow_GetBufferHandleFromNative failed" << std::endl;
79     }
80 
81     ret = OH_NativeWindow_NativeObjectReference(windowBuffer);
82     if (ret != OHOS::GSERROR_OK) {
83         std::cout << "OH_NativeWindow_NativeObjectReference failed" << std::endl;
84     }
85 
86     ret = OH_NativeWindow_NativeObjectUnreference(windowBuffer);
87     if (ret != OHOS::GSERROR_OK) {
88         std::cout << "OH_NativeWindow_NativeObjectUnreference failed" << std::endl;
89     }
90     *nativeWindowBuffer = windowBuffer;
91     *fenceFd = fence;
92 }
93 }
94 
main(int32_t argc,const char * argv[])95 int32_t main(int32_t argc, const char *argv[])
96 {
97     std::cout << "sample start" << std::endl;
98     OHNativeWindow* nativeWindow = CreateNativeWindow();
99     NativeWindowBuffer* nativeWindowBuffer = nullptr;
100     int fenceFd = -1;
101     CreateNativeWindowBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
102 
103     struct Region *region = new Region();
104     struct Region::Rect *rect = new Region::Rect();
105     rect->x = 0x100;
106     rect->y = 0x100;
107     rect->w = 0x100;
108     rect->h = 0x100;
109     region->rects = rect;
110     region->rectNumber = 1;
111 
112     ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, fenceFd, *region);
113     if (ret != OHOS::GSERROR_OK) {
114         std::cout << "OH_NativeWindow_NativeWindowFlushBuffer failed" << std::endl;
115     }
116     delete region;
117     delete rect;
118 
119     int32_t sequence = 0;
120     OHScalingMode scalingMode = OHScalingMode::OH_SCALING_MODE_SCALE_TO_WINDOW;
121     ret = OH_NativeWindow_NativeWindowSetScalingMode(nativeWindow, sequence, scalingMode);
122     if (ret != OHOS::GSERROR_OK) {
123         std::cout << "OH_NativeWindow_NativeWindowSetScalingMode failed" << std::endl;
124     }
125 
126     int32_t size = 1;
127     const OHHDRMetaData metaData[] = {{OH_METAKEY_RED_PRIMARY_X, 0}};
128     ret = OH_NativeWindow_NativeWindowSetMetaData(nativeWindow, sequence, size, metaData);
129     if (ret != OHOS::GSERROR_OK) {
130         std::cout << "OH_NativeWindow_NativeWindowSetMetaData failed" << std::endl;
131     }
132 
133     OHHDRMetadataKey key = OHHDRMetadataKey::OH_METAKEY_HDR10_PLUS;
134     const uint8_t metaDataSet[] = {0};
135     ret = OH_NativeWindow_NativeWindowSetMetaDataSet(nativeWindow, sequence, key, size, metaDataSet);
136     if (ret != OHOS::GSERROR_OK) {
137         std::cout << "OH_NativeWindow_NativeWindowSetMetaDataSet failed" << std::endl;
138     }
139 
140     OHExtDataHandle *handle = new OHExtDataHandle();
141     handle->fd = -1;
142     handle->reserveInts = 1;
143     handle->reserve[0] = 1;
144     ret = OH_NativeWindow_NativeWindowSetTunnelHandle(nativeWindow, handle);
145     if (ret != OHOS::GSERROR_OK) {
146         std::cout << "OH_NativeWindow_NativeWindowSetTunnelHandle failed" << std::endl;
147     }
148     delete handle;
149     std::cout << "sample end" << std::endl;
150     return 0;
151 }