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 "display_test_utils.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayTestUtils"};
22 }
23
~DisplayTestUtils()24 DisplayTestUtils::~DisplayTestUtils()
25 {
26 if (csurface_ != nullptr) {
27 if (prevBuffer_ != nullptr) {
28 SurfaceError ret = csurface_->ReleaseBuffer(prevBuffer_, -1);
29 if (ret != SURFACE_ERROR_OK) {
30 WLOGFE("buffer release failed");
31 return;
32 }
33 WLOGI("prevBuffer_ release success");
34 }
35 csurface_->UnregisterConsumerListener();
36 }
37
38 csurface_ = nullptr;
39 psurface_ = nullptr;
40 listener_ = nullptr;
41 prevBuffer_ = nullptr;
42 bufferHandle_ = nullptr;
43 }
44
SizeEqualToDisplay(const sptr<Display> & display,const Media::Size cur)45 bool DisplayTestUtils::SizeEqualToDisplay(const sptr<Display>& display, const Media::Size cur)
46 {
47 int32_t dWidth = display->GetWidth();
48 int32_t dHeight = display->GetHeight();
49
50 bool res = ((cur.width == dWidth) && (cur.height == dHeight));
51 if (!res) {
52 WLOGFE("DisplaySize: %d %d, CurrentSize: %d %d", dWidth, dHeight, cur.width, cur.height);
53 }
54 return res;
55 }
56
SizeEqual(const Media::Size dst,const Media::Size cur)57 bool DisplayTestUtils::SizeEqual(const Media::Size dst, const Media::Size cur)
58 {
59 bool res = ((cur.width == dst.width) && (cur.height == dst.height));
60 if (!res) {
61 WLOGFE("Desired Size: %d %d, Current Size: %d %d", dst.width, dst.height, cur.width, cur.height);
62 }
63 return res;
64 }
65
CreateSurface()66 bool DisplayTestUtils::CreateSurface()
67 {
68 csurface_ = IConsumerSurface::Create();
69 if (csurface_ == nullptr) {
70 WLOGFE("csurface create failed");
71 return false;
72 }
73
74 auto producer = csurface_->GetProducer();
75 psurface_ = Surface::CreateSurfaceAsProducer(producer);
76 if (psurface_ == nullptr) {
77 WLOGFE("csurface create failed");
78 return false;
79 }
80
81 listener_ = new BufferListener(*this);
82 SurfaceError ret = csurface_->RegisterConsumerListener(listener_);
83 if (ret != SURFACE_ERROR_OK) {
84 WLOGFE("listener register failed");
85 return false;
86 }
87 return true;
88 }
89
OnVsync()90 void DisplayTestUtils::OnVsync()
91 {
92 std::lock_guard<std::mutex> lock(mutex_);
93 WLOGI("DisplayTestUtils::OnVsync");
94 sptr<SurfaceBuffer> cbuffer = nullptr;
95 int32_t fence = -1;
96 int64_t timestamp = 0;
97 OHOS::Rect damage;
98 if (csurface_ == nullptr) {
99 WLOGFE("csurface_ is null");
100 return;
101 }
102 auto sret = csurface_->AcquireBuffer(cbuffer, fence, timestamp, damage);
103 UniqueFd fenceFd(fence);
104 if (cbuffer == nullptr || sret != OHOS::SURFACE_ERROR_OK) {
105 WLOGFE("acquire buffer failed");
106 return;
107 }
108 bufferHandle_ = cbuffer->GetBufferHandle();
109 if (bufferHandle_ == nullptr) {
110 WLOGFE("get bufferHandle failed");
111 return;
112 }
113 if (defaultWidth_ == static_cast<uint32_t>(bufferHandle_->width) &&
114 defaultHeight_ == static_cast<uint32_t>(bufferHandle_->height)) {
115 successCount_++;
116 WLOGI("compareWH is successful in onVsync: %d", successCount_);
117 } else {
118 failCount_++;
119 }
120 if (cbuffer != prevBuffer_) {
121 if (prevBuffer_ != nullptr) {
122 SurfaceError ret = csurface_->ReleaseBuffer(prevBuffer_, -1);
123 if (ret != SURFACE_ERROR_OK) {
124 WLOGFE("buffer release failed");
125 return;
126 }
127 }
128 prevBuffer_ = cbuffer;
129 }
130 }
131
SetDefaultWH(const sptr<Display> & display)132 void DisplayTestUtils::SetDefaultWH(const sptr<Display>& display)
133 {
134 defaultWidth_ = display->GetWidth();
135 defaultHeight_ = display->GetHeight();
136 }
137 } // namespace ROSEN
138 } // namespace OHOS