• 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     return producer_->FlushBuffer(buffer->GetSeqNum(), bedataimpl, syncFence, config);
120 }
121 
InitContext(EGLContext context)122 GSError ProducerEglSurface::InitContext(EGLContext context)
123 {
124     ScopedBytrace func(__func__);
125 
126     sEglManager_ = EglManager::GetInstance();
127     if (sEglManager_ == nullptr) {
128         BLOGNE("EglManager::GetInstance Failed.");
129         return GSERROR_INTERNAL;
130     }
131 
132     if (sEglManager_->Init(context) != GSERROR_OK) {
133         BLOGNE("EglManager init failed.");
134         return GSERROR_INTERNAL;
135     }
136 
137     if (initFlag_) {
138         return GSERROR_OK;
139     }
140 
141     if (RequestBufferProc() != GSERROR_OK) {
142         BLOGNE("RequestBufferProc failed.");
143         return GSERROR_INTERNAL;
144     }
145 
146     initFlag_ = true;
147     return GSERROR_OK;
148 }
149 
GetEglDisplay() const150 EGLDisplay ProducerEglSurface::GetEglDisplay() const
151 {
152     if (initFlag_) {
153         return sEglManager_->GetEGLDisplay();
154     }
155     BLOGNE("ProducerEglSurface is not init.");
156     return EGL_NO_DISPLAY;
157 }
158 
GetEglContext() const159 EGLContext ProducerEglSurface::GetEglContext() const
160 {
161     if (initFlag_) {
162         return sEglManager_->GetEGLContext();
163     }
164     BLOGNE("ProducerEglSurface is not init.");
165     return EGL_NO_CONTEXT;
166 }
167 
GetEglSurface() const168 EGLSurface ProducerEglSurface::GetEglSurface() const
169 {
170     return EGL_NO_SURFACE;
171 }
172 
GetEglFbo() const173 GLuint ProducerEglSurface::GetEglFbo() const
174 {
175     if (initFlag_ && currentBuffer_ != nullptr) {
176         return currentBuffer_->GetEglData()->GetFrameBufferObj();
177     }
178 
179     BLOGNE("ProducerEglSurface is not init.");
180     return 0;
181 }
182 
SwapBuffers()183 GSError ProducerEglSurface::SwapBuffers()
184 {
185     ScopedBytrace func(__func__);
186     if (!initFlag_) {
187         BLOGNE("ProducerEglSurface is not init.");
188         return GSERROR_INTERNAL;
189     }
190 
191     if (FlushBufferProc() != GSERROR_OK) {
192         BLOGNE("FlushBufferProc failed.");
193     }
194 
195     if (RequestBufferProc() != GSERROR_OK) {
196         BLOGNE("RequestBufferProc failed.");
197         return GSERROR_INTERNAL;
198     }
199 
200     return GSERROR_OK;
201 }
202 
SetWidthAndHeight(int32_t width,int32_t height)203 GSError ProducerEglSurface::SetWidthAndHeight(int32_t width, int32_t height)
204 {
205     if (width <= 0 || height <= 0) {
206         return SURFACE_ERROR_INVALID_PARAM;
207     }
208 
209     std::lock_guard<std::mutex> lock(mutex_);
210     width_ = width;
211     height_ = height;
212     return GSERROR_OK;
213 }
214 
WaitForReleaseFence(int32_t fd)215 GSError ProducerEglSurface::WaitForReleaseFence(int32_t fd)
216 {
217     ScopedBytrace func(__func__);
218     GSError ret = GSERROR_OK;
219     if (fd != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
220         BLOGNI("releaseFence %{public}d.", fd);
221         EGLint attribList[] = {
222             EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
223             EGL_NONE,
224         };
225 
226         EGLSyncKHR sync = sEglManager_->EglCreateSync(EGL_SYNC_NATIVE_FENCE_ANDROID, attribList);
227         if (sync == EGL_NO_SYNC_KHR) {
228             BLOGNE("EglCreateSync failed.");
229             return GSERROR_INTERNAL;
230         }
231 
232         if (sEglManager_->EglWaitSync(sync, 0) != EGL_TRUE) {
233             BLOGNE("EglWaitSync failed.");
234             ret = GSERROR_INTERNAL;
235         }
236 
237         if (sEglManager_->EglDestroySync(sync) != EGL_TRUE) {
238             BLOGNE("EglDestroySync failed.");
239             ret = GSERROR_INTERNAL;
240         }
241     }
242     return ret;
243 }
244 
RequestBufferProc()245 GSError ProducerEglSurface::RequestBufferProc()
246 {
247     ScopedBytrace func(__func__);
248     int32_t releaseFence;
249     {
250         BufferRequestConfig rconfig;
251         {
252             std::lock_guard<std::mutex> lock(mutex_);
253             rconfig = {
254                 .width = width_,
255                 .height = height_,
256                 .strideAlignment = 0x8,
257                 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
258                 .usage = producer_->GetDefaultUsage(),
259                 .timeout = 0,
260             };
261         }
262 
263         currentBuffer_ = nullptr;
264         if (RequestBuffer(currentBuffer_, releaseFence, rconfig) != GSERROR_OK) {
265             BLOGNE("RequestBuffer failed.");
266             return GSERROR_INTERNAL;
267         }
268     }
269 
270     if (AddEglData(currentBuffer_) != GSERROR_OK) {
271         BLOGNE("AddEglData failed.");
272         return GSERROR_INTERNAL;
273     }
274 
275     if (WaitForReleaseFence(releaseFence) != GSERROR_OK) {
276         BLOGNE("WaitForReleaseFence failed.");
277         return GSERROR_INTERNAL;
278     }
279 
280     return GSERROR_OK;
281 }
282 
CreateEglFenceFd(int32_t & fd)283 GSError ProducerEglSurface::CreateEglFenceFd(int32_t &fd)
284 {
285     EGLSyncKHR sync = sEglManager_->EglCreateSync(EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
286     if (sync == EGL_NO_SYNC_KHR) {
287         BLOGNE("EglCreateSync failed.");
288         return GSERROR_INTERNAL;
289     }
290 
291     glFlush();
292 
293     fd = sEglManager_->EglDupNativeFenceFd(sync);
294     if (sEglManager_->EglDestroySync(sync) != EGL_TRUE) {
295         BLOGNE("EglDestroySync failed.");
296     }
297 
298     if (fd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
299         BLOGNE("EglDupNativeFenceFd failed.");
300         return GSERROR_INTERNAL;
301     }
302     return GSERROR_OK;
303 }
304 
FlushBufferProc()305 GSError ProducerEglSurface::FlushBufferProc()
306 {
307     ScopedBytrace func(__func__);
308     int32_t fd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
309     if (currentBuffer_ == nullptr) {
310         BLOGNE("currentBuffer_ is nullptr.");
311         return GSERROR_INTERNAL;
312     }
313 
314     if (CreateEglFenceFd(fd) != GSERROR_OK) {
315         BLOGNE("CreateEglFenceFd failed.");
316     }
317     BLOGNE("flush fence fd %{public}d.", fd);
318 
319     BufferFlushConfig fconfig = {
320         .damage = {
321             .x = 0,
322             .y = 0,
323             .w = currentBuffer_->GetWidth(),
324             .h = currentBuffer_->GetHeight(),
325         },
326     };
327     if (FlushBuffer(currentBuffer_, fd, fconfig) != GSERROR_OK) {
328         BLOGNE("FlushBuffer failed.");
329         return GSERROR_INTERNAL;
330     }
331 
332     return GSERROR_OK;
333 }
334 
IsRemote()335 bool ProducerEglSurface::IsRemote()
336 {
337     return producer_->AsObject()->IsProxyObject();
338 }
339 
AddEglData(sptr<SurfaceBuffer> & buffer)340 GSError ProducerEglSurface::AddEglData(sptr<SurfaceBuffer> &buffer)
341 {
342     ScopedBytrace func(__func__);
343     sptr<EglData> sEglData = buffer->GetEglData();
344     if (sEglData != nullptr) {
345         glBindFramebuffer(GL_FRAMEBUFFER, sEglData->GetFrameBufferObj());
346         BLOGI("buffer is reused return.");
347         return GSERROR_OK;
348     }
349 
350     sptr<EglDataImpl> sEglDataImpl = new EglDataImpl();
351     if (sEglDataImpl == nullptr) {
352         BLOGNE("new failed.");
353         return GSERROR_NO_MEM;
354     }
355     auto sret = sEglDataImpl->CreateEglData(buffer);
356     if (sret == GSERROR_OK) {
357         buffer->SetEglData(sEglDataImpl);
358         BLOGI("buffer FBO=%{public}d.", sEglDataImpl->GetFrameBufferObj());
359     }
360     return sret;
361 }
362 } // namespace OHOS
363