• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <chrono>
16 #include <thread>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <iservice_registry.h>
20 #include <display_type.h>
21 #include <native_window.h>
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Rosen {
27 class NativeWindowBufferTest : public testing::Test,  public IBufferConsumerListenerClazz {
28 public:
29     static void SetUpTestCase();
30     virtual void OnBufferAvailable() override;
31     pid_t ChildProcessMain();
32 
33     static inline sptr<OHOS::Surface> cSurface = nullptr;
34     static inline int32_t pipeFd[2] = {};
35     static inline int32_t ipcSystemAbilityID = 34156;
36 };
37 
SetUpTestCase()38 void NativeWindowBufferTest::SetUpTestCase() {}
39 
OnBufferAvailable()40 void NativeWindowBufferTest::OnBufferAvailable() {}
41 
ChildProcessMain()42 pid_t NativeWindowBufferTest::ChildProcessMain()
43 {
44     pipe(pipeFd);
45     pid_t pid = fork();
46     if (pid != 0) {
47         return pid;
48     }
49 
50     int64_t data;
51     read(pipeFd[0], &data, sizeof(data));
52 
53     sptr<IRemoteObject> robj = nullptr;
54     while (true) {
55         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56         robj = sam->GetSystemAbility(ipcSystemAbilityID);
57         if (robj != nullptr) {
58             break;
59         }
60         sleep(0);
61     }
62 
63     auto producer = iface_cast<IBufferProducer>(robj);
64     sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
65 
66     struct NativeWindow *nativeWindow = CreateNativeWindowFromSurface(&pSurface);
67     struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
68 
69     int code = SET_USAGE;
70     int32_t usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA;
71     NativeWindowHandleOpt(nativeWindow, code, usage);
72 
73     code = SET_BUFFER_GEOMETRY;
74     int32_t height = 0x100;
75     int32_t width = 0x100;
76     NativeWindowHandleOpt(nativeWindow, code, height, width);
77 
78     code = SET_FORMAT;
79     int32_t format = PIXEL_FMT_RGBA_8888;
80     NativeWindowHandleOpt(nativeWindow, code, format);
81 
82     code = SET_STRIDE;
83     int32_t stride = 0x8;
84     NativeWindowHandleOpt(nativeWindow, code, stride);
85 
86     int32_t fenceFd = -1;
87     auto ret = NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
88     if (ret != OHOS::GSERROR_OK) {
89         data = ret;
90         write(pipeFd[1], &data, sizeof(data));
91         exit(0);
92         return -1;
93     }
94     nativeWindowBuffer->sfbuffer->ExtraSet("123", 0x123);
95     nativeWindowBuffer->sfbuffer->ExtraSet("345", (int64_t)0x345);
96     nativeWindowBuffer->sfbuffer->ExtraSet("567", "567");
97 
98     struct Region *region = new Region();
99     struct Region::Rect *rect = new Region::Rect();
100     rect->w = 0x100;
101     rect->h = 0x100;
102     region->rects = rect;
103     ret = NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, -1, *region);
104     if (ret != OHOS::GSERROR_OK) {
105         data = ret;
106         write(pipeFd[1], &data, sizeof(data));
107         exit(0);
108         return -1;
109     }
110     data = ret;
111     write(pipeFd[1], &data, sizeof(data));
112     sleep(0);
113     read(pipeFd[0], &data, sizeof(data));
114     close(pipeFd[0]);
115     close(pipeFd[1]);
116     exit(0);
117     return 0;
118 }
119 
120 /*
121 * Function: produce and consumer surface of nativewindow
122 * Type: Function
123 * Rank: Important(2)
124 * EnvConditions: N/A
125 * CaseDescription: 1. produce surface by nativewindow interface, fill buffer
126 *                  2. consume surface and check buffer
127  */
128 HWTEST_F(NativeWindowBufferTest, Surface001, Function | MediumTest | Level2)
129 {
130     auto pid = ChildProcessMain();
131     ASSERT_GE(pid, 0);
132 
133     cSurface = Surface::CreateSurfaceAsConsumer("test");
134     cSurface->RegisterConsumerListener(this);
135     auto producer = cSurface->GetProducer();
136     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
137     sam->AddSystemAbility(ipcSystemAbilityID, producer->AsObject());
138 
139     int64_t data = 0;
140     write(pipeFd[1], &data, sizeof(data));
141     sleep(0);
142     read(pipeFd[0], &data, sizeof(data));
143     EXPECT_EQ(data, OHOS::GSERROR_OK);
144 
145     OHOS::sptr<SurfaceBuffer> buffer = nullptr;
146     int32_t fence = -1;
147     int64_t timestamp;
148     Rect damage;
149     auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
150     EXPECT_EQ(ret, OHOS::GSERROR_OK);
151     EXPECT_NE(buffer, nullptr);
152     if (buffer != nullptr) {
153         int32_t int32;
154         int64_t int64;
155         std::string str;
156         buffer->ExtraGet("123", int32);
157         buffer->ExtraGet("345", int64);
158         buffer->ExtraGet("567", str);
159 
160         EXPECT_EQ(int32, 0x123);
161         EXPECT_EQ(int64, 0x345);
162         EXPECT_EQ(str, "567");
163     }
164 
165     ret = cSurface->ReleaseBuffer(buffer, -1);
166     EXPECT_EQ(ret, OHOS::GSERROR_OK);
167 
168     write(pipeFd[1], &data, sizeof(data));
169     close(pipeFd[0]);
170     close(pipeFd[1]);
171     sam->RemoveSystemAbility(ipcSystemAbilityID);
172     waitpid(pid, nullptr, NULL);
173 }
174 }
175