1 /*
2 * Copyright (c) 2023 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 "surface_adapter_impl.h"
17
18 #include "drivers/peripheral/display/interfaces/include/display_type.h"
19 #include "nweb_log.h"
20
21 namespace OHOS::NWeb {
22 namespace {
IsSupportFormat(int32_t format)23 bool IsSupportFormat(int32_t format)
24 {
25 switch (format) {
26 case PixelFormat::PIXEL_FMT_RGBA_8888:
27 case PixelFormat::PIXEL_FMT_YCBCR_420_SP:
28 return true;
29 default:
30 return false;
31 }
32 }
33 } // namespace
34
35 const int32_t GSErrorCode::GSERROR_OK = GSError::GSERROR_OK;
36
37 const int32_t PixelFormatAdapter::PIXEL_FMT_RGBA_8888 = PixelFormat::PIXEL_FMT_RGBA_8888;
38
39 const int32_t PixelFormatAdapter::PIXEL_FMT_YCBCR_420_SP = PixelFormat::PIXEL_FMT_YCBCR_420_SP;
40
SurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer)41 SurfaceBufferAdapterImpl::SurfaceBufferAdapterImpl(sptr<SurfaceBuffer> buffer) : buffer_(buffer) {}
42
GetFileDescriptor() const43 int32_t SurfaceBufferAdapterImpl::GetFileDescriptor() const
44 {
45 if (!buffer_) {
46 WVLOG_E("buffer_ is nullptr");
47 return -1;
48 }
49 return buffer_->GetFileDescriptor();
50 }
51
GetWidth() const52 int32_t SurfaceBufferAdapterImpl::GetWidth() const
53 {
54 if (!buffer_) {
55 WVLOG_E("buffer_ is nullptr");
56 return -1;
57 }
58 return buffer_->GetWidth();
59 }
60
GetHeight() const61 int32_t SurfaceBufferAdapterImpl::GetHeight() const
62 {
63 if (!buffer_) {
64 WVLOG_E("buffer_ is nullptr");
65 return -1;
66 }
67 return buffer_->GetHeight();
68 }
69
GetStride() const70 int32_t SurfaceBufferAdapterImpl::GetStride() const
71 {
72 if (!buffer_) {
73 WVLOG_E("buffer_ is nullptr");
74 return -1;
75 }
76 return buffer_->GetStride();
77 }
78
GetFormat() const79 int32_t SurfaceBufferAdapterImpl::GetFormat() const
80 {
81 if (!buffer_) {
82 WVLOG_E("buffer_ is nullptr");
83 return -1;
84 }
85 return buffer_->GetFormat();
86 }
87
GetSize() const88 uint32_t SurfaceBufferAdapterImpl::GetSize() const
89 {
90 if (!buffer_) {
91 WVLOG_E("buffer_ is nullptr");
92 return 0;
93 }
94 return buffer_->GetSize();
95 }
96
GetVirAddr() const97 void* SurfaceBufferAdapterImpl::GetVirAddr() const
98 {
99 if (!buffer_) {
100 WVLOG_E("buffer_ is nullptr");
101 return nullptr;
102 }
103 return buffer_->GetVirAddr();
104 }
105
GetBuffer()106 sptr<SurfaceBuffer>& SurfaceBufferAdapterImpl::GetBuffer()
107 {
108 return buffer_;
109 }
110
BufferConsumerListenerImpl(wptr<IConsumerSurface> surface,std::unique_ptr<IBufferConsumerListenerAdapter> listener)111 BufferConsumerListenerImpl::BufferConsumerListenerImpl(
112 wptr<IConsumerSurface> surface, std::unique_ptr<IBufferConsumerListenerAdapter> listener)
113 : cSurface_(surface), listener_(std::move(listener))
114 {}
115
OnBufferAvailable()116 void BufferConsumerListenerImpl::OnBufferAvailable()
117 {
118 if (cSurface_ == nullptr || listener_ == nullptr) {
119 WVLOG_E("cSurface_ or listener_ is nullptr");
120 return;
121 }
122 auto surfaceTemp = cSurface_.promote();
123 if (surfaceTemp == nullptr) {
124 WVLOG_E("surface is nullptr");
125 return;
126 }
127 sptr<SurfaceBuffer> buffer;
128 int32_t fence;
129 int64_t timestamp;
130 Rect damage;
131 GSError ret = surfaceTemp->AcquireBuffer(buffer, fence, timestamp, damage);
132 if (ret != GSERROR_OK) {
133 WVLOG_E("acquire buffer failed, ret=%{public}d", ret);
134 return;
135 }
136 int32_t format = buffer->GetFormat();
137 if (!IsSupportFormat(format)) {
138 WVLOG_E("unsupport format for:%{public}d", format);
139 surfaceTemp->ReleaseBuffer(buffer, -1);
140 return;
141 }
142 auto bufferAdapter = std::make_unique<SurfaceBufferAdapterImpl>(buffer);
143 listener_->OnBufferAvailable(std::move(bufferAdapter));
144 }
145
ConsumerSurfaceAdapterImpl()146 ConsumerSurfaceAdapterImpl::ConsumerSurfaceAdapterImpl() : cSurface_(IConsumerSurface::Create()) {}
147
RegisterConsumerListener(std::unique_ptr<IBufferConsumerListenerAdapter> listenerAdapter)148 int32_t ConsumerSurfaceAdapterImpl::RegisterConsumerListener(
149 std::unique_ptr<IBufferConsumerListenerAdapter> listenerAdapter)
150 {
151 if (!cSurface_ || !listenerAdapter) {
152 WVLOG_E("cSurface_ or listener_ is nullptr");
153 return -1;
154 }
155 sptr<IBufferConsumerListener> listener =
156 new (std::nothrow) BufferConsumerListenerImpl(cSurface_, std::move(listenerAdapter));
157 if (!listener) {
158 WVLOG_E("listener create failed");
159 return -1;
160 }
161 return cSurface_->RegisterConsumerListener(listener);
162 }
163
ReleaseBuffer(std::unique_ptr<SurfaceBufferAdapter> bufferAdapter,int32_t fence)164 int32_t ConsumerSurfaceAdapterImpl::ReleaseBuffer(std::unique_ptr<SurfaceBufferAdapter> bufferAdapter, int32_t fence)
165 {
166 if (!cSurface_ || !bufferAdapter) {
167 WVLOG_E("cSurface_ or bufferAdapter is nullptr");
168 return -1;
169 }
170 auto bufferImpl = static_cast<SurfaceBufferAdapterImpl*>(bufferAdapter.get());
171 return cSurface_->ReleaseBuffer(bufferImpl->GetBuffer(), fence);
172 }
173
SetUserData(const std::string & key,const std::string & val)174 int32_t ConsumerSurfaceAdapterImpl::SetUserData(const std::string& key, const std::string& val)
175 {
176 if (!cSurface_) {
177 WVLOG_E("cSurface_ is nullptr");
178 return -1;
179 }
180 return cSurface_->SetUserData(key, val);
181 }
182
SetQueueSize(uint32_t queueSize)183 int32_t ConsumerSurfaceAdapterImpl::SetQueueSize(uint32_t queueSize)
184 {
185 if (!cSurface_) {
186 WVLOG_E("cSurface_ is nullptr");
187 return -1;
188 }
189 return cSurface_->SetQueueSize(queueSize);
190 }
191
GetConsumerSurface()192 sptr<IConsumerSurface>& ConsumerSurfaceAdapterImpl::GetConsumerSurface()
193 {
194 return cSurface_;
195 }
196 } // namespace OHOS::NWeb
197