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 "rs_interfaces_test_utils.h"
17
18 #include "platform/common/rs_log.h"
19
20 namespace OHOS {
21 namespace Rosen {
~RSInterfacesTestUtils()22 RSInterfacesTestUtils::~RSInterfacesTestUtils()
23 {
24 if (cSurface_ != nullptr) {
25 ROSEN_LOGE("cSurface_ is null");
26 return;
27 }
28
29 if (prevBuffer_ != nullptr) {
30 SurfaceError ret = cSurface_->ReleaseBuffer(prevBuffer_, -1);
31 if (ret != SURFACE_ERROR_OK) {
32 ROSEN_LOGE("buffer release failed");
33 return;
34 }
35 ROSEN_LOGI("prevBuffer_ release success");
36 }
37
38 cSurface_->UnregisterConsumerListener();
39 cSurface_ = nullptr;
40 pSurface_ = nullptr;
41 listener_ = nullptr;
42 prevBuffer_ = nullptr;
43 bufferHandle_ = nullptr;
44 }
45
CreateSurface()46 bool RSInterfacesTestUtils::CreateSurface()
47 {
48 cSurface_ = IConsumerSurface::Create();
49 if (cSurface_ == nullptr) {
50 ROSEN_LOGE("csurface create failed");
51 return false;
52 }
53
54 auto producer = cSurface_->GetProducer();
55 pSurface_ = Surface::CreateSurfaceAsProducer(producer);
56 if (pSurface_ == nullptr) {
57 ROSEN_LOGE("csurface create failed");
58 return false;
59 }
60
61 listener_ = new BufferListener(*this);
62 SurfaceError ret = cSurface_->RegisterConsumerListener(listener_);
63 if (ret != SURFACE_ERROR_OK) {
64 ROSEN_LOGE("listener register failed");
65 return false;
66 }
67 return true;
68 }
69
OnVsync()70 void RSInterfacesTestUtils::OnVsync()
71 {
72 std::lock_guard<std::mutex> lock(mutex_);
73 ROSEN_LOGI("RSInterfacesTestUtils::OnVsync");
74 sptr<SurfaceBuffer> cbuffer = nullptr;
75 int32_t fence = -1;
76 int64_t timestamp = 0;
77 OHOS::Rect damage;
78 if (cSurface_ == nullptr) {
79 ROSEN_LOGE("cSurface_ is null");
80 return;
81 }
82 auto sret = cSurface_->AcquireBuffer(cbuffer, fence, timestamp, damage);
83 UniqueFd fenceFd(fence);
84 if (cbuffer == nullptr || sret != OHOS::SURFACE_ERROR_OK) {
85 ROSEN_LOGE("acquire buffer failed");
86 return;
87 }
88 bufferHandle_ = cbuffer->GetBufferHandle();
89 if (bufferHandle_ == nullptr) {
90 ROSEN_LOGE("get bufferHandle failed");
91 return;
92 }
93 if (defaultWidth_ == static_cast<uint32_t>(bufferHandle_->width) &&
94 defaultHeight_ == static_cast<uint32_t>(bufferHandle_->height)) {
95 successCount_++;
96 ROSEN_LOGI("compareWH is successful in onVsync: %{public}d", successCount_);
97 } else {
98 failCount_++;
99 }
100 if (cbuffer != prevBuffer_) {
101 if (prevBuffer_ != nullptr) {
102 SurfaceError ret = cSurface_->ReleaseBuffer(prevBuffer_, -1);
103 if (ret != SURFACE_ERROR_OK) {
104 ROSEN_LOGE("buffer release failed");
105 return;
106 }
107 }
108 prevBuffer_ = cbuffer;
109 }
110 }
111
RootNodeInit(std::shared_ptr<RSUIDirector> rsUiDirector,int width,int height)112 void RSInterfacesTestUtils::RootNodeInit(std::shared_ptr<RSUIDirector> rsUiDirector, int width, int height)
113 {
114 std::cout << "rs app demo Init Rosen Backend!" << std::endl;
115 rootNode_ = RSRootNode::Create();
116 rootNode_->SetBounds(0, 0, width, height);
117 rootNode_->SetFrame(0, 0, width, height);
118 rootNode_->SetBackgroundColor(SK_ColorRED);
119 rsUiDirector->SetRoot(rootNode_->GetId());
120 }
121
CreateWindowByDisplayParam(DisplayId displayId,std::string name,Rect rect)122 sptr<Window> RSInterfacesTestUtils::CreateWindowByDisplayParam(DisplayId displayId, std::string name,
123 Rect rect)
124 {
125 sptr<WindowOption> option = new WindowOption();
126 if (option == nullptr) {
127 return nullptr;
128 }
129 Rect displayRect = {rect.posX_, rect.posY_, rect.width_, rect.height_};
130 option->SetDisplayId(displayId);
131 option->SetWindowRect(displayRect);
132 option->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
133 option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
134 option->SetWindowName(name);
135 sptr<Window> window = Window::Create(option->GetWindowName(), option);
136 return window;
137 }
138 } // namespace ROSEN
139 } // namespace OHOS