• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "producer_egl_surface.h"
17 
18 #include <mutex>
19 
20 #include <scoped_bytrace.h>
21 
22 #include "buffer_log.h"
23 #include "buffer_extra_data_impl.h"
24 #include "sync_fence.h"
25 
26 namespace OHOS {
ProducerEglSurface(sptr<IBufferProducer> & producer)27 ProducerEglSurface::ProducerEglSurface(sptr<IBufferProducer>& producer)
28 {
29     producer_ = producer;
30     width_ = producer_->GetDefaultWidth();
31     height_ = producer_->GetDefaultHeight();
32     auto sret = producer_->GetName(name_);
33     if (sret != GSERROR_OK) {
34         BLOGNE("GetName failed, %{public}s", GSErrorStr(sret).c_str());
35     }
36     BLOGND("ctor");
37 }
38 
~ProducerEglSurface()39 ProducerEglSurface::~ProducerEglSurface()
40 {
41     BLOGND("dtor");
42     initFlag_ = false;
43     if (IsRemote()) {
44         for (auto it = bufferProducerCache_.begin(); it != bufferProducerCache_.end(); it++) {
45             if (it->second->GetVirAddr() != nullptr) {
46                 BufferManager::GetInstance()->Unmap(it->second);
47                 it->second->SetEglData(nullptr);
48             }
49         }
50     }
51 
52     if (currentBuffer_ != nullptr) {
53         const sptr<BufferExtraData>& bedataimpl = currentBuffer_->GetExtraData();
54         producer_->CancelBuffer(currentBuffer_->GetSeqNum(), bedataimpl);
55         currentBuffer_ = nullptr;
56     }
57     sEglManager_ = nullptr;
58     producer_ = nullptr;
59 }
60 
RequestBuffer(sptr<SurfaceBuffer> & buffer,int32_t & fence,BufferRequestConfig & config)61 GSError ProducerEglSurface::RequestBuffer(sptr<SurfaceBuffer> &buffer,
62     int32_t& fence, BufferRequestConfig &config)
63 {
64     IBufferProducer::RequestBufferReturnValue retval;
65     sptr<BufferExtraData> bedataimpl = new BufferExtraDataImpl;
66     retval.fence = SyncFence::INVALID_FENCE;
67     GSError ret = producer_->RequestBuffer(config, bedataimpl, retval);
68     if (ret != GSERROR_OK) {
69         BLOGN_FAILURE("Producer report %{public}s", GSErrorStr(ret).c_str());
70         return ret;
71     }
72 
73     // add cache
74     if (retval.buffer != nullptr && IsRemote()) {
75         ret = BufferManager::GetInstance()->Map(retval.buffer);
76         if (ret != GSERROR_OK) {
77             BLOGN_FAILURE_ID(retval.sequence, "Map failed");
78         } else {
79             BLOGN_SUCCESS_ID(retval.sequence, "Map");
80         }
81     }
82 
83     if (retval.buffer != nullptr) {
84         bufferProducerCache_[retval.sequence] = retval.buffer;
85     } else {
86         retval.buffer = bufferProducerCache_[retval.sequence];
87     }
88     buffer = retval.buffer;
89 
90     ret = BufferManager::GetInstance()->InvalidateCache(buffer);
91     if (ret != GSERROR_OK) {
92         BLOGNW("Warning [%{public}d], InvalidateCache failed", retval.sequence);
93     }
94 
95     if (buffer != nullptr) {
96         buffer->SetExtraData(bedataimpl);
97     }
98 
99     for (auto it = retval.deletingBuffers.begin(); it != retval.deletingBuffers.end(); it++) {
100         if (IsRemote() && bufferProducerCache_[*it]->GetVirAddr() != nullptr) {
101             bufferProducerCache_[*it]->SetEglData(nullptr);
102             BufferManager::GetInstance()->Unmap(bufferProducerCache_[*it]);
103         }
104         bufferProducerCache_.erase(*it);
105     }
106 
107     fence = retval.fence->Get();
108     return GSERROR_OK;
109 }
110 
FlushBuffer(sptr<SurfaceBuffer> & buffer,int32_t fence,BufferFlushConfig & config)111 GSError ProducerEglSurface::FlushBuffer(sptr<SurfaceBuffer> &buffer,
112     int32_t fence, BufferFlushConfig &config)
113 {
114     if (buffer == nullptr) {
115         return GSERROR_INVALID_ARGUMENTS;
116     }
117     sptr<SyncFence> syncFence = new SyncFence(fence);
118     const sptr<BufferExtraData>& bedataimpl = buffer->GetExtraData();
119     BufferFlushConfigWithDamages damagesFlushConfig;
120     damagesFlushConfig.damages.push_back(config.damage);
121     damagesFlushConfig.timestamp = config.timestamp;
122 
123     return producer_->FlushBuffer(buffer->GetSeqNum(), bedataimpl, syncFence, damagesFlushConfig);
124 }
125 
InitContext(EGLContext context)126 GSError ProducerEglSurface::InitContext(EGLContext context)
127 {
128     ScopedBytrace func(__func__);
129 
130     sEglManager_ = EglManager::GetInstance();
131     if (sEglManager_ == nullptr) {
132         BLOGNE("EglManager::GetInstance Failed.");
133         return GSERROR_INTERNAL;
134     }
135 
136     if (sEglManager_->Init(context) != GSERROR_OK) {
137         BLOGNE("EglManager init failed.");
138         return GSERROR_INTERNAL;
139     }
140 
141     if (initFlag_) {
142         return GSERROR_OK;
143     }
144 
145     if (RequestBufferProc() != GSERROR_OK) {
146         BLOGNE("RequestBufferProc failed.");
147         return GSERROR_INTERNAL;
148     }
149 
150     initFlag_ = true;
151     return GSERROR_OK;
152 }
153 
GetEglDisplay() const154 EGLDisplay ProducerEglSurface::GetEglDisplay() const
155 {
156     if (initFlag_) {
157         return sEglManager_->GetEGLDisplay();
158     }
159     BLOGNE("ProducerEglSurface is not init.");
160     return EGL_NO_DISPLAY;
161 }
162 
GetEglContext() const163 EGLContext ProducerEglSurface::GetEglContext() const
164 {
165     if (initFlag_) {
166         return sEglManager_->GetEGLContext();
167     }
168     BLOGNE("ProducerEglSurface is not init.");
169     return EGL_NO_CONTEXT;
170 }
171 
GetEglSurface() const172 EGLSurface ProducerEglSurface::GetEglSurface() const
173 {
174     return EGL_NO_SURFACE;
175 }
176 
GetEglFbo() const177 GLuint ProducerEglSurface::GetEglFbo() const
178 {
179     if (initFlag_ && currentBuffer_ != nullptr) {
180         return currentBuffer_->GetEglData()->GetFrameBufferObj();
181     }
182 
183     BLOGNE("ProducerEglSurface is not init.");
184     return 0;
185 }
186 
SwapBuffers()187 GSError ProducerEglSurface::SwapBuffers()
188 {
189     ScopedBytrace func(__func__);
190     if (!initFlag_) {
191         BLOGNE("ProducerEglSurface is not init.");
192         return GSERROR_INTERNAL;
193     }
194 
195     if (FlushBufferProc() != GSERROR_OK) {
196         BLOGNE("FlushBufferProc failed.");
197     }
198 
199     if (RequestBufferProc() != GSERROR_OK) {
200         BLOGNE("RequestBufferProc failed.");
201         return GSERROR_INTERNAL;
202     }
203 
204     return GSERROR_OK;
205 }
206 
SetWidthAndHeight(int32_t width,int32_t height)207 GSError ProducerEglSurface::SetWidthAndHeight(int32_t width, int32_t height)
208 {
209     if (width <= 0 || height <= 0) {
210         return SURFACE_ERROR_INVALID_PARAM;
211     }
212 
213     std::lock_guard<std::mutex> lock(mutex_);
214     width_ = width;
215     height_ = height;
216     return GSERROR_OK;
217 }
218 
WaitForReleaseFence(int32_t fd)219 GSError ProducerEglSurface::WaitForReleaseFence(int32_t fd)
220 {
221     ScopedBytrace func(__func__);
222     GSError ret = GSERROR_OK;
223     if (fd != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
224         BLOGNI("releaseFence %{public}d.", fd);
225         EGLint attribList[] = {
226             EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
227             EGL_NONE,
228         };
229 
230         EGLSyncKHR sync = sEglManager_->EglCreateSync(EGL_SYNC_NATIVE_FENCE_ANDROID, attribList);
231         if (sync == EGL_NO_SYNC_KHR) {
232             BLOGNE("EglCreateSync failed.");
233             return GSERROR_INTERNAL;
234         }
235 
236         if (sEglManager_->EglWaitSync(sync, 0) != EGL_TRUE) {
237             BLOGNE("EglWaitSync failed.");
238             ret = GSERROR_INTERNAL;
239         }
240 
241         if (sEglManager_->EglDestroySync(sync) != EGL_TRUE) {
242             BLOGNE("EglDestroySync failed.");
243             ret = GSERROR_INTERNAL;
244         }
245     }
246     return ret;
247 }
248 
RequestBufferProc()249 GSError ProducerEglSurface::RequestBufferProc()
250 {
251     ScopedBytrace func(__func__);
252     int32_t releaseFence;
253     {
254         BufferRequestConfig rconfig;
255         {
256             std::lock_guard<std::mutex> lock(mutex_);
257             rconfig = {
258                 .width = width_,
259                 .height = height_,
260                 .strideAlignment = 0x8,
261                 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
262                 .usage = producer_->GetDefaultUsage(),
263                 .timeout = 0,
264             };
265         }
266 
267         currentBuffer_ = nullptr;
268         if (RequestBuffer(currentBuffer_, releaseFence, rconfig) != GSERROR_OK) {
269             BLOGNE("RequestBuffer failed.");
270             return GSERROR_INTERNAL;
271         }
272     }
273 
274     if (AddEglData(currentBuffer_) != GSERROR_OK) {
275         BLOGNE("AddEglData failed.");
276         return GSERROR_INTERNAL;
277     }
278 
279     if (WaitForReleaseFence(releaseFence) != GSERROR_OK) {
280         BLOGNE("WaitForReleaseFence failed.");
281         return GSERROR_INTERNAL;
282     }
283 
284     return GSERROR_OK;
285 }
286 
CreateEglFenceFd(int32_t & fd)287 GSError ProducerEglSurface::CreateEglFenceFd(int32_t &fd)
288 {
289     EGLSyncKHR sync = sEglManager_->EglCreateSync(EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
290     if (sync == EGL_NO_SYNC_KHR) {
291         BLOGNE("EglCreateSync failed.");
292         return GSERROR_INTERNAL;
293     }
294 
295     glFlush();
296 
297     fd = sEglManager_->EglDupNativeFenceFd(sync);
298     if (sEglManager_->EglDestroySync(sync) != EGL_TRUE) {
299         BLOGNE("EglDestroySync failed.");
300     }
301 
302     if (fd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
303         BLOGNE("EglDupNativeFenceFd failed.");
304         return GSERROR_INTERNAL;
305     }
306     return GSERROR_OK;
307 }
308 
FlushBufferProc()309 GSError ProducerEglSurface::FlushBufferProc()
310 {
311     ScopedBytrace func(__func__);
312     int32_t fd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
313     if (currentBuffer_ == nullptr) {
314         BLOGNE("currentBuffer_ is nullptr.");
315         return GSERROR_INTERNAL;
316     }
317 
318     if (CreateEglFenceFd(fd) != GSERROR_OK) {
319         BLOGNE("CreateEglFenceFd failed.");
320     }
321     BLOGNE("flush fence fd %{public}d.", fd);
322 
323     BufferFlushConfig fconfig = {
324         .damage = {
325             .x = 0,
326             .y = 0,
327             .w = currentBuffer_->GetWidth(),
328             .h = currentBuffer_->GetHeight(),
329         },
330     };
331     if (FlushBuffer(currentBuffer_, fd, fconfig) != GSERROR_OK) {
332         BLOGNE("FlushBuffer failed.");
333         return GSERROR_INTERNAL;
334     }
335 
336     return GSERROR_OK;
337 }
338 
IsRemote()339 bool ProducerEglSurface::IsRemote()
340 {
341     return producer_->AsObject()->IsProxyObject();
342 }
343 
AddEglData(sptr<SurfaceBuffer> & buffer)344 GSError ProducerEglSurface::AddEglData(sptr<SurfaceBuffer> &buffer)
345 {
346     ScopedBytrace func(__func__);
347     sptr<EglData> sEglData = buffer->GetEglData();
348     if (sEglData != nullptr) {
349         glBindFramebuffer(GL_FRAMEBUFFER, sEglData->GetFrameBufferObj());
350         BLOGI("buffer is reused return.");
351         return GSERROR_OK;
352     }
353 
354     sptr<EglDataImpl> sEglDataImpl = new EglDataImpl();
355     if (sEglDataImpl == nullptr) {
356         BLOGNE("new failed.");
357         return GSERROR_NO_MEM;
358     }
359     auto sret = sEglDataImpl->CreateEglData(buffer);
360     if (sret == GSERROR_OK) {
361         buffer->SetEglData(sEglDataImpl);
362         BLOGI("buffer FBO=%{public}d.", sEglDataImpl->GetFrameBufferObj());
363     }
364     return sret;
365 }
366 } // namespace OHOS
367