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 "surface_reader.h"
17 #include "window_manager_hilog.h"
18 #include "unique_fd.h"
19
20 #include <securec.h>
21
22 using namespace OHOS::Media;
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SurfaceReader"};
28 } // namespace
29 const int BPP = 4; // bytes per pixel
30
SurfaceReader()31 SurfaceReader::SurfaceReader()
32 {
33 }
34
~SurfaceReader()35 SurfaceReader::~SurfaceReader()
36 {
37 if (csurface_ != nullptr) {
38 csurface_->UnregisterConsumerListener();
39 }
40 psurface_ = nullptr;
41 csurface_ = nullptr;
42 }
43
Init()44 bool SurfaceReader::Init()
45 {
46 csurface_ = Surface::CreateSurfaceAsConsumer();
47 if (csurface_ == nullptr) {
48 return false;
49 }
50
51 auto producer = csurface_->GetProducer();
52 psurface_ = Surface::CreateSurfaceAsProducer(producer);
53 if (psurface_ == nullptr) {
54 return false;
55 }
56
57 listener_ = new BufferListener(*this);
58 SurfaceError ret = csurface_->RegisterConsumerListener(listener_);
59 if (ret != SURFACE_ERROR_OK) {
60 return false;
61 }
62 return true;
63 }
64
OnVsync()65 void SurfaceReader::OnVsync()
66 {
67 WLOGFI("SurfaceReader::OnVsync");
68
69 sptr<SurfaceBuffer> cbuffer = nullptr;
70 int32_t fence = -1;
71 int64_t timestamp = 0;
72 Rect damage;
73 auto sret = csurface_->AcquireBuffer(cbuffer, fence, timestamp, damage);
74 if (cbuffer == nullptr || sret != OHOS::SURFACE_ERROR_OK) {
75 WLOGFE("SurfaceReader::OnVsync: surface buffer is null");
76 return;
77 }
78
79 if (!ProcessBuffer(cbuffer)) {
80 WLOGFE("SurfaceReader::OnVsync: ProcessBuffer failed");
81 return;
82 }
83
84 if (cbuffer != prevBuffer_) {
85 if (prevBuffer_ != nullptr) {
86 SurfaceError ret = csurface_->ReleaseBuffer(prevBuffer_, -1);
87 if (ret != SURFACE_ERROR_OK) {
88 WLOGFE("SurfaceReader::OnVsync: release buffer error");
89 return;
90 }
91 }
92
93 prevBuffer_ = cbuffer;
94 }
95 }
96
GetSurface() const97 sptr<Surface> SurfaceReader::GetSurface() const
98 {
99 return psurface_;
100 }
101
SetHandler(sptr<SurfaceReaderHandler> handler)102 void SurfaceReader::SetHandler(sptr<SurfaceReaderHandler> handler)
103 {
104 handler_ = handler;
105 }
106
ProcessBuffer(const sptr<SurfaceBuffer> & buf)107 bool SurfaceReader::ProcessBuffer(const sptr<SurfaceBuffer> &buf)
108 {
109 if (handler_ == nullptr) {
110 WLOGFE("SurfaceReaderHandler not set");
111 return false;
112 }
113
114 BufferHandle *bufferHandle = buf->GetBufferHandle();
115 if (bufferHandle == nullptr) {
116 WLOGFE("bufferHandle nullptr");
117 return false;
118 }
119
120 uint32_t width = static_cast<uint32_t>(bufferHandle->width);
121 uint32_t height = static_cast<uint32_t>(bufferHandle->height);
122 uint32_t stride = static_cast<uint32_t>(bufferHandle->stride);
123 uint8_t *addr = (uint8_t *)buf->GetVirAddr();
124
125 auto data = (uint8_t *)malloc(width * height * BPP);
126 if (data == nullptr) {
127 WLOGFE("data malloc failed");
128 return false;
129 }
130 for (uint32_t i = 0; i < height; i++) {
131 errno_t ret = memcpy_s(data + width * i * BPP, width * BPP, addr + stride * i, width * BPP);
132 if (ret != EOK) {
133 WLOGFE("memcpy failed");
134 return false;
135 }
136 }
137
138 sptr<PixelMap> pixelMap = new PixelMap();
139 ImageInfo info;
140 info.size.width = static_cast<int32_t>(width);
141 info.size.height = static_cast<int32_t>(height);
142 info.pixelFormat = PixelFormat::RGBA_8888;
143 info.colorSpace = ColorSpace::SRGB;
144 pixelMap->SetImageInfo(info);
145
146 pixelMap->SetPixelsAddr(data, nullptr, width * height, AllocatorType::HEAP_ALLOC, nullptr);
147
148 handler_->OnImageAvalible(pixelMap);
149 return true;
150 }
151 }
152 }