1 /*
2 * Copyright (c) 2021-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 <iostream>
17 #include "securec.h"
18 #include "surface.h"
19 #include "window.h"
20
21 #include "transaction/rs_transaction.h"
22 #include "ui/rs_surface_node.h"
23
24 using namespace OHOS;
25 using namespace OHOS::Rosen;
26
27 namespace Test {
GetWindowSurface(uint32_t w,uint32_t h)28 sptr<OHOS::Surface> GetWindowSurface(uint32_t w, uint32_t h)
29 {
30 sptr<WindowOption> option = new WindowOption();
31 option->SetWindowRect( {0, 0, w, h} );
32 option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_LAUNCHING);
33 option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
34 sptr<OHOS::Rosen::Window> previewWindow = Rosen::Window::Create("xcomponent_window", option);
35 if (previewWindow == nullptr || previewWindow->GetSurfaceNode() == nullptr) {
36 std::cout << "previewWindow is nullptr" << std::endl;
37 return nullptr;
38 }
39 previewWindow->Show();
40 auto surfaceNode = previewWindow->GetSurfaceNode();
41 surfaceNode->SetFrameGravity(Rosen::Gravity::RESIZE);
42 Rosen::RSTransaction::FlushImplicitTransaction();
43 return surfaceNode->GetSurface();
44 }
45 }
46
main()47 int main()
48 {
49 std::cout << "Test Begin" << std::endl;
50 // 700 500 : surface width and height
51 sptr<OHOS::Surface> surface = Test::GetWindowSurface(700, 500);
52 if (surface == nullptr) {
53 return 0;
54 }
55 std::cout << "GetWindowSurface Success" << std::endl;
56 sptr<OHOS::SurfaceBuffer> buffer;
57 int32_t releaseFence;
58 OHOS::BufferRequestConfig config = {
59 .width = 700, // width
60 .height = 500, // height
61 .strideAlignment = 0x8,
62 .format = GRAPHIC_PIXEL_FMT_YCBCR_422_P,
63 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
64 };
65
66 OHOS::SurfaceError ret = surface->RequestBuffer(buffer, releaseFence, config);
67 if (ret != SURFACE_ERROR_OK) {
68 std::cout << "request buffer failed" << std::endl;
69 return 0;
70 }
71
72 if (buffer == nullptr) {
73 std::cout << "request buffer failed: buffer is nullptr" << std::endl;
74 return 0;
75 }
76 if (buffer->GetVirAddr() == nullptr) {
77 std::cout << "get virAddr failed: virAddr is nullptr"<< std::endl;
78 return 0;
79 }
80 std::cout << "RequestBuffer Success" << std::endl;
81 OHOS::BufferFlushConfig flushConfig = {
82 .damage = {
83 .w = buffer->GetWidth(),
84 .h = buffer->GetHeight(),
85 },
86 };
87 ret = surface->FlushBuffer(buffer, -1, flushConfig);
88 if (ret != SURFACE_ERROR_OK) {
89 std::cout << "flush buffer failed" << std::endl;
90 return 0;
91 }
92 std::cout << "FlushBuffer Success" << std::endl;
93 sleep(1000); // wait 1000s
94 }