Add NativeImageSurface
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / tizen / native-image-source-queue-impl-tizen.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/imaging/tizen/native-image-source-queue-impl-tizen.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <tbm_surface_internal.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/internal/graphics/common/egl-image-extensions.h>
29 #include <dali/internal/graphics/gles/egl-graphics.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 namespace
38 {
39 #define TBM_SURFACE_QUEUE_SIZE 3
40
41 const char* SAMPLER_TYPE    = "samplerExternalOES";
42
43 // clang-format off
44 int FORMATS_BLENDING_REQUIRED[] = {
45   TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
46   TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
47   TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
48   TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
49   TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
50   TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
51   TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
52   TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
53   TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
54 };
55 // clang-format on
56
57 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
58
59 } // namespace
60
61 NativeImageSourceQueueTizen* NativeImageSourceQueueTizen::New(uint32_t width, uint32_t height, Dali::NativeImageSourceQueue::ColorFormat colorFormat, Any nativeImageSourceQueue)
62 {
63   NativeImageSourceQueueTizen* image = new NativeImageSourceQueueTizen(width, height, colorFormat, nativeImageSourceQueue);
64   DALI_ASSERT_DEBUG(image && "NativeImageSourceQueueTizen allocation failed.");
65
66   if(image)
67   {
68     image->Initialize(colorFormat);
69   }
70
71   return image;
72 }
73
74 NativeImageSourceQueueTizen::NativeImageSourceQueueTizen(uint32_t width, uint32_t height, Dali::NativeImageSourceQueue::ColorFormat colorFormat, Any nativeImageSourceQueue)
75 : mMutex(),
76   mWidth(width),
77   mHeight(height),
78   mTbmQueue(NULL),
79   mConsumeSurface(NULL),
80   mEglImages(),
81   mBuffers(),
82   mEglGraphics(NULL),
83   mEglImageExtensions(NULL),
84   mOwnTbmQueue(false),
85   mBlendingRequired(false),
86   mIsResized(false)
87 {
88   DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
89
90   GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
91   mEglGraphics                = static_cast<EglGraphics*>(graphics);
92
93   mTbmQueue = GetSurfaceFromAny(nativeImageSourceQueue);
94
95   if(mTbmQueue != NULL)
96   {
97     mBlendingRequired = CheckBlending(tbm_surface_queue_get_format(mTbmQueue));
98     mWidth            = tbm_surface_queue_get_width(mTbmQueue);
99     mHeight           = tbm_surface_queue_get_height(mTbmQueue);
100   }
101 }
102
103 NativeImageSourceQueueTizen::~NativeImageSourceQueueTizen()
104 {
105   if(mOwnTbmQueue)
106   {
107     if(mTbmQueue != NULL)
108     {
109       tbm_surface_queue_destroy(mTbmQueue);
110     }
111   }
112 }
113
114 void NativeImageSourceQueueTizen::Initialize(Dali::NativeImageSourceQueue::ColorFormat colorFormat)
115 {
116   if(mWidth == 0 || mHeight == 0)
117   {
118     return;
119   }
120
121   if(mTbmQueue == NULL)
122   {
123     int tbmFormat = TBM_FORMAT_ARGB8888;
124
125     switch(colorFormat)
126     {
127       case Dali::NativeImageSourceQueue::ColorFormat::RGBA8888:
128       {
129         tbmFormat         = TBM_FORMAT_ARGB8888;
130         mBlendingRequired = true;
131         break;
132       }
133       case Dali::NativeImageSourceQueue::ColorFormat::RGBX8888:
134       {
135         tbmFormat         = TBM_FORMAT_XRGB8888;
136         mBlendingRequired = false;
137         break;
138       }
139       case Dali::NativeImageSourceQueue::ColorFormat::RGB888:
140       {
141         tbmFormat         = TBM_FORMAT_RGB888;
142         mBlendingRequired = false;
143         break;
144       }
145       default:
146       {
147         DALI_LOG_WARNING("Wrong color format.\n");
148         return;
149       }
150     }
151
152     mTbmQueue = tbm_surface_queue_create(TBM_SURFACE_QUEUE_SIZE, mWidth, mHeight, tbmFormat, 0);
153     if(!mTbmQueue)
154     {
155       DALI_LOG_ERROR("NativeImageSourceQueueTizen::Initialize: tbm_surface_queue_create is failed! [%p]\n", mTbmQueue);
156       return;
157     }
158
159     mOwnTbmQueue = true;
160   }
161 }
162
163 tbm_surface_queue_h NativeImageSourceQueueTizen::GetSurfaceFromAny(Any source) const
164 {
165   if(source.Empty())
166   {
167     return NULL;
168   }
169
170   if(source.GetType() == typeid(tbm_surface_queue_h))
171   {
172     return AnyCast<tbm_surface_queue_h>(source);
173   }
174   else
175   {
176     return NULL;
177   }
178 }
179
180 Any NativeImageSourceQueueTizen::GetNativeImageSourceQueue() const
181 {
182   return Any(mTbmQueue);
183 }
184
185 void NativeImageSourceQueueTizen::SetSize(uint32_t width, uint32_t height)
186 {
187   Dali::Mutex::ScopedLock lock(mMutex);
188
189   if(mWidth == width && mHeight == height)
190   {
191     return;
192   }
193
194   tbm_surface_queue_reset(mTbmQueue, width, height, tbm_surface_queue_get_format(mTbmQueue));
195
196   mWidth     = width;
197   mHeight    = height;
198   mIsResized = true;
199 }
200
201 void NativeImageSourceQueueTizen::IgnoreSourceImage()
202 {
203   Dali::Mutex::ScopedLock lock(mMutex);
204   tbm_surface_h           surface;
205
206   if(tbm_surface_queue_can_acquire(mTbmQueue, 0))
207   {
208     if(tbm_surface_queue_acquire(mTbmQueue, &surface) != TBM_SURFACE_QUEUE_ERROR_NONE)
209     {
210       DALI_LOG_ERROR("NativeImageSourceQueueTizen::IgnoreSourceImage: Failed to aquire a tbm_surface\n");
211       return;
212     }
213
214     if(tbm_surface_internal_is_valid(surface))
215     {
216       tbm_surface_queue_release(mTbmQueue, surface);
217     }
218   }
219 }
220
221 bool NativeImageSourceQueueTizen::CanDequeueBuffer()
222 {
223   Dali::Mutex::ScopedLock lock(mMutex);
224   if(tbm_surface_queue_can_dequeue(mTbmQueue, 0))
225   {
226     return true;
227   }
228   return false;
229 }
230
231 uint8_t* NativeImageSourceQueueTizen::DequeueBuffer(uint32_t& width, uint32_t& height, uint32_t& stride)
232 {
233   Dali::Mutex::ScopedLock lock(mMutex);
234   if(mTbmQueue == NULL)
235   {
236     DALI_LOG_ERROR("TbmQueue is NULL");
237     return NULL;
238   }
239
240   tbm_surface_h tbmSurface;
241   if(tbm_surface_queue_dequeue(mTbmQueue, &tbmSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
242   {
243     DALI_LOG_ERROR("Failed to dequeue a tbm_surface [%p]\n", tbmSurface);
244     return NULL;
245   }
246
247   tbm_surface_info_s info;
248   int                ret = tbm_surface_map(tbmSurface, TBM_OPTION_WRITE, &info);
249   if(ret != TBM_SURFACE_ERROR_NONE)
250   {
251     DALI_LOG_ERROR("tbm_surface_map is failed! [%d] [%p]\n", ret, tbmSurface);
252     tbm_surface_queue_cancel_dequeue(mTbmQueue, tbmSurface);
253     return NULL;
254   }
255
256   unsigned char* buffer = info.planes[0].ptr;
257   if(!buffer)
258   {
259     DALI_LOG_ERROR("tbm buffer pointer is null! [%p]\n", tbmSurface);
260     tbm_surface_unmap(tbmSurface);
261     tbm_surface_queue_cancel_dequeue(mTbmQueue, tbmSurface);
262     return NULL;
263   }
264
265   tbm_surface_internal_ref(tbmSurface);
266
267   stride = info.planes[0].stride;
268   width  = mWidth;
269   height = mHeight;
270
271   // Push the buffer
272   mBuffers.push_back(BufferPair(tbmSurface, buffer));
273   return buffer;
274 }
275
276 bool NativeImageSourceQueueTizen::EnqueueBuffer(uint8_t* buffer)
277 {
278   Dali::Mutex::ScopedLock lock(mMutex);
279   auto                    bufferInstance = std::find_if(mBuffers.begin(),
280                                      mBuffers.end(),
281                                      [buffer](BufferPair pair) { return (pair.second == buffer); });
282   if(bufferInstance != mBuffers.end())
283   {
284     tbm_surface_internal_unref((*bufferInstance).first);
285     tbm_surface_unmap((*bufferInstance).first);
286     tbm_surface_queue_enqueue(mTbmQueue, (*bufferInstance).first);
287     mBuffers.erase(bufferInstance);
288     return true;
289   }
290   return false;
291 }
292
293 bool NativeImageSourceQueueTizen::CreateResource()
294 {
295   mEglImageExtensions = mEglGraphics->GetImageExtensions();
296   DALI_ASSERT_DEBUG(mEglImageExtensions);
297
298   return true;
299 }
300
301 void NativeImageSourceQueueTizen::DestroyResource()
302 {
303   Dali::Mutex::ScopedLock lock(mMutex);
304
305   ResetEglImageList(true);
306 }
307
308 uint32_t NativeImageSourceQueueTizen::TargetTexture()
309 {
310   return 0;
311 }
312
313 void NativeImageSourceQueueTizen::PrepareTexture()
314 {
315   Dali::Mutex::ScopedLock lock(mMutex);
316
317   tbm_surface_h oldSurface = mConsumeSurface;
318
319   if(tbm_surface_queue_can_acquire(mTbmQueue, 0))
320   {
321     if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
322     {
323       DALI_LOG_ERROR("Failed to aquire a tbm_surface\n");
324       return;
325     }
326
327     if(oldSurface)
328     {
329       if(tbm_surface_internal_is_valid(oldSurface))
330       {
331         tbm_surface_queue_release(mTbmQueue, oldSurface);
332       }
333     }
334
335     if(mIsResized)
336     {
337       ResetEglImageList(false);
338       mIsResized = false;
339     }
340
341     if(mConsumeSurface)
342     {
343       bool existing = false;
344       for(auto&& iter : mEglImages)
345       {
346         if(iter.first == mConsumeSurface)
347         {
348           // Find the surface in the existing list
349           existing = true;
350           mEglImageExtensions->TargetTextureKHR(iter.second);
351           break;
352         }
353       }
354
355       if(!existing)
356       {
357         // Push the surface
358         tbm_surface_internal_ref(mConsumeSurface);
359
360         void* eglImageKHR = mEglImageExtensions->CreateImageKHR(reinterpret_cast<EGLClientBuffer>(mConsumeSurface));
361         mEglImageExtensions->TargetTextureKHR(eglImageKHR);
362
363         mEglImages.push_back(EglImagePair(mConsumeSurface, eglImageKHR));
364       }
365     }
366   }
367 }
368
369 bool NativeImageSourceQueueTizen::ApplyNativeFragmentShader(std::string& shader)
370 {
371   return mEglGraphics->ApplyNativeFragmentShader(shader, SAMPLER_TYPE);
372 }
373
374 const char* NativeImageSourceQueueTizen::GetCustomSamplerTypename() const
375 {
376   return SAMPLER_TYPE;
377 }
378
379 int NativeImageSourceQueueTizen::GetTextureTarget() const
380 {
381   return GL_TEXTURE_EXTERNAL_OES;
382 }
383
384 Any NativeImageSourceQueueTizen::GetNativeImageHandle() const
385 {
386   return nullptr;
387 }
388
389 bool NativeImageSourceQueueTizen::SourceChanged() const
390 {
391   return false;
392 }
393
394 void NativeImageSourceQueueTizen::ResetEglImageList(bool releaseConsumeSurface)
395 {
396   // When Tbm surface queue is reset(resized), the surface acquired before reset() is still valid, not the others.
397   // We can still use the acquired surface so that we will release it as the oldSurface in PrepareTexture() when the next surface is ready.
398   if(releaseConsumeSurface && mConsumeSurface)
399   {
400     if(tbm_surface_internal_is_valid(mConsumeSurface))
401     {
402       tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
403     }
404     mConsumeSurface = NULL;
405   }
406
407   for(auto&& iter : mEglImages)
408   {
409     mEglImageExtensions->DestroyImageKHR(iter.second);
410
411     tbm_surface_internal_unref(iter.first);
412   }
413   mEglImages.clear();
414 }
415
416 bool NativeImageSourceQueueTizen::CheckBlending(int format)
417 {
418   for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
419   {
420     if(format == FORMATS_BLENDING_REQUIRED[i])
421     {
422       return true;
423     }
424   }
425
426   return false;
427 }
428
429 } // namespace Adaptor
430
431 } // namespace Internal
432
433 } // namespace Dali