2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/imaging/tizen/native-image-source-queue-impl-tizen.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <tbm_surface_internal.h>
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>
39 #define TBM_SURFACE_QUEUE_SIZE 3
41 const char* SAMPLER_TYPE = "samplerExternalOES";
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
57 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
61 NativeImageSourceQueueTizen* NativeImageSourceQueueTizen::New(uint32_t width, uint32_t height, Dali::NativeImageSourceQueue::ColorFormat colorFormat, Any nativeImageSourceQueue)
63 NativeImageSourceQueueTizen* image = new NativeImageSourceQueueTizen(width, height, colorFormat, nativeImageSourceQueue);
64 DALI_ASSERT_DEBUG(image && "NativeImageSourceQueueTizen allocation failed.");
68 image->Initialize(colorFormat);
74 NativeImageSourceQueueTizen::NativeImageSourceQueueTizen(uint32_t width, uint32_t height, Dali::NativeImageSourceQueue::ColorFormat colorFormat, Any nativeImageSourceQueue)
79 mConsumeSurface(NULL),
83 mEglImageExtensions(NULL),
85 mBlendingRequired(false),
88 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
90 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
91 mEglGraphics = static_cast<EglGraphics*>(graphics);
93 mTbmQueue = GetSurfaceFromAny(nativeImageSourceQueue);
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);
103 NativeImageSourceQueueTizen::~NativeImageSourceQueueTizen()
107 if(mTbmQueue != NULL)
109 tbm_surface_queue_destroy(mTbmQueue);
114 void NativeImageSourceQueueTizen::Initialize(Dali::NativeImageSourceQueue::ColorFormat colorFormat)
116 if(mWidth == 0 || mHeight == 0)
121 if(mTbmQueue == NULL)
123 int tbmFormat = TBM_FORMAT_ARGB8888;
127 case Dali::NativeImageSourceQueue::ColorFormat::RGBA8888:
129 tbmFormat = TBM_FORMAT_ARGB8888;
130 mBlendingRequired = true;
133 case Dali::NativeImageSourceQueue::ColorFormat::RGBX8888:
135 tbmFormat = TBM_FORMAT_XRGB8888;
136 mBlendingRequired = false;
139 case Dali::NativeImageSourceQueue::ColorFormat::RGB888:
141 tbmFormat = TBM_FORMAT_RGB888;
142 mBlendingRequired = false;
147 DALI_LOG_WARNING("Wrong color format.\n");
152 mTbmQueue = tbm_surface_queue_create(TBM_SURFACE_QUEUE_SIZE, mWidth, mHeight, tbmFormat, 0);
155 DALI_LOG_ERROR("NativeImageSourceQueueTizen::Initialize: tbm_surface_queue_create is failed! [%p]\n", mTbmQueue);
163 tbm_surface_queue_h NativeImageSourceQueueTizen::GetSurfaceFromAny(Any source) const
170 if(source.GetType() == typeid(tbm_surface_queue_h))
172 return AnyCast<tbm_surface_queue_h>(source);
180 Any NativeImageSourceQueueTizen::GetNativeImageSourceQueue() const
182 return Any(mTbmQueue);
185 void NativeImageSourceQueueTizen::SetSize(uint32_t width, uint32_t height)
187 Dali::Mutex::ScopedLock lock(mMutex);
189 if(mWidth == width && mHeight == height)
194 tbm_surface_queue_reset(mTbmQueue, width, height, tbm_surface_queue_get_format(mTbmQueue));
201 void NativeImageSourceQueueTizen::IgnoreSourceImage()
203 Dali::Mutex::ScopedLock lock(mMutex);
204 tbm_surface_h surface;
206 if(tbm_surface_queue_can_acquire(mTbmQueue, 0))
208 if(tbm_surface_queue_acquire(mTbmQueue, &surface) != TBM_SURFACE_QUEUE_ERROR_NONE)
210 DALI_LOG_ERROR("NativeImageSourceQueueTizen::IgnoreSourceImage: Failed to aquire a tbm_surface\n");
214 if(tbm_surface_internal_is_valid(surface))
216 tbm_surface_queue_release(mTbmQueue, surface);
221 bool NativeImageSourceQueueTizen::CanDequeueBuffer()
223 Dali::Mutex::ScopedLock lock(mMutex);
224 if(tbm_surface_queue_can_dequeue(mTbmQueue, 0))
231 uint8_t* NativeImageSourceQueueTizen::DequeueBuffer(uint32_t& width, uint32_t& height, uint32_t& stride)
233 Dali::Mutex::ScopedLock lock(mMutex);
234 if(mTbmQueue == NULL)
236 DALI_LOG_ERROR("TbmQueue is NULL");
240 tbm_surface_h tbmSurface;
241 if(tbm_surface_queue_dequeue(mTbmQueue, &tbmSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
243 DALI_LOG_ERROR("Failed to dequeue a tbm_surface [%p]\n", tbmSurface);
247 tbm_surface_info_s info;
248 int ret = tbm_surface_map(tbmSurface, TBM_OPTION_WRITE, &info);
249 if(ret != TBM_SURFACE_ERROR_NONE)
251 DALI_LOG_ERROR("tbm_surface_map is failed! [%d] [%p]\n", ret, tbmSurface);
252 tbm_surface_queue_cancel_dequeue(mTbmQueue, tbmSurface);
256 unsigned char* buffer = info.planes[0].ptr;
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);
265 tbm_surface_internal_ref(tbmSurface);
267 stride = info.planes[0].stride;
272 mBuffers.push_back(BufferPair(tbmSurface, buffer));
276 bool NativeImageSourceQueueTizen::EnqueueBuffer(uint8_t* buffer)
278 Dali::Mutex::ScopedLock lock(mMutex);
279 auto bufferInstance = std::find_if(mBuffers.begin(),
281 [buffer](BufferPair pair) { return (pair.second == buffer); });
282 if(bufferInstance != mBuffers.end())
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);
293 bool NativeImageSourceQueueTizen::CreateResource()
295 mEglImageExtensions = mEglGraphics->GetImageExtensions();
296 DALI_ASSERT_DEBUG(mEglImageExtensions);
301 void NativeImageSourceQueueTizen::DestroyResource()
303 Dali::Mutex::ScopedLock lock(mMutex);
305 ResetEglImageList(true);
308 uint32_t NativeImageSourceQueueTizen::TargetTexture()
313 void NativeImageSourceQueueTizen::PrepareTexture()
315 Dali::Mutex::ScopedLock lock(mMutex);
317 tbm_surface_h oldSurface = mConsumeSurface;
319 if(tbm_surface_queue_can_acquire(mTbmQueue, 0))
321 if(tbm_surface_queue_acquire(mTbmQueue, &mConsumeSurface) != TBM_SURFACE_QUEUE_ERROR_NONE)
323 DALI_LOG_ERROR("Failed to aquire a tbm_surface\n");
329 if(tbm_surface_internal_is_valid(oldSurface))
331 tbm_surface_queue_release(mTbmQueue, oldSurface);
337 ResetEglImageList(false);
343 bool existing = false;
344 for(auto&& iter : mEglImages)
346 if(iter.first == mConsumeSurface)
348 // Find the surface in the existing list
350 mEglImageExtensions->TargetTextureKHR(iter.second);
358 tbm_surface_internal_ref(mConsumeSurface);
360 void* eglImageKHR = mEglImageExtensions->CreateImageKHR(reinterpret_cast<EGLClientBuffer>(mConsumeSurface));
361 mEglImageExtensions->TargetTextureKHR(eglImageKHR);
363 mEglImages.push_back(EglImagePair(mConsumeSurface, eglImageKHR));
369 bool NativeImageSourceQueueTizen::ApplyNativeFragmentShader(std::string& shader)
371 return mEglGraphics->ApplyNativeFragmentShader(shader, SAMPLER_TYPE);
374 const char* NativeImageSourceQueueTizen::GetCustomSamplerTypename() const
379 int NativeImageSourceQueueTizen::GetTextureTarget() const
381 return GL_TEXTURE_EXTERNAL_OES;
384 Any NativeImageSourceQueueTizen::GetNativeImageHandle() const
389 bool NativeImageSourceQueueTizen::SourceChanged() const
394 void NativeImageSourceQueueTizen::ResetEglImageList(bool releaseConsumeSurface)
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)
400 if(tbm_surface_internal_is_valid(mConsumeSurface))
402 tbm_surface_queue_release(mTbmQueue, mConsumeSurface);
404 mConsumeSurface = NULL;
407 for(auto&& iter : mEglImages)
409 mEglImageExtensions->DestroyImageKHR(iter.second);
411 tbm_surface_internal_unref(iter.first);
416 bool NativeImageSourceQueueTizen::CheckBlending(int format)
418 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
420 if(format == FORMATS_BLENDING_REQUIRED[i])
429 } // namespace Adaptor
431 } // namespace Internal