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-impl-tizen.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <tbm_surface_internal.h>
28 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
29 #include <dali/internal/adaptor/common/adaptor-impl.h>
30 #include <dali/internal/graphics/common/egl-image-extensions.h>
31 #include <dali/internal/graphics/gles/egl-graphics.h>
41 const char* FRAGMENT_PREFIX = "#extension GL_OES_EGL_image_external:require\n";
42 const char* SAMPLER_TYPE = "samplerExternalOES";
45 tbm_format FORMATS_BLENDING_REQUIRED[] = {
46 TBM_FORMAT_ARGB4444, TBM_FORMAT_ABGR4444,
47 TBM_FORMAT_RGBA4444, TBM_FORMAT_BGRA4444,
48 TBM_FORMAT_RGBX5551, TBM_FORMAT_BGRX5551,
49 TBM_FORMAT_ARGB1555, TBM_FORMAT_ABGR1555,
50 TBM_FORMAT_RGBA5551, TBM_FORMAT_BGRA5551,
51 TBM_FORMAT_ARGB8888, TBM_FORMAT_ABGR8888,
52 TBM_FORMAT_RGBA8888, TBM_FORMAT_BGRA8888,
53 TBM_FORMAT_ARGB2101010, TBM_FORMAT_ABGR2101010,
54 TBM_FORMAT_RGBA1010102, TBM_FORMAT_BGRA1010102
58 const int NUM_FORMATS_BLENDING_REQUIRED = 18;
62 using Dali::Integration::PixelBuffer;
64 NativeImageSourceTizen* NativeImageSourceTizen::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
66 NativeImageSourceTizen* image = new NativeImageSourceTizen(width, height, depth, nativeImageSource);
67 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
77 NativeImageSourceTizen::NativeImageSourceTizen(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
80 mOwnTbmSurface(false),
83 mBlendingRequired(false),
87 mEglImageExtensions(NULL),
90 mIsBufferAcquired(false)
92 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
94 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
95 mEglGraphics = static_cast<EglGraphics*>(graphics);
97 mTbmSurface = GetSurfaceFromAny(nativeImageSource);
99 if(mTbmSurface != NULL)
101 tbm_surface_internal_ref(mTbmSurface);
102 mBlendingRequired = CheckBlending(tbm_surface_get_format(mTbmSurface));
103 mWidth = tbm_surface_get_width(mTbmSurface);
104 mHeight = tbm_surface_get_height(mTbmSurface);
108 void NativeImageSourceTizen::Initialize()
110 if(mTbmSurface != NULL || mWidth == 0 || mHeight == 0)
115 tbm_format format = TBM_FORMAT_RGB888;
120 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
122 format = TBM_FORMAT_ARGB8888;
126 case Dali::NativeImageSource::COLOR_DEPTH_8:
128 format = TBM_FORMAT_C8;
132 case Dali::NativeImageSource::COLOR_DEPTH_16:
134 format = TBM_FORMAT_RGB565;
138 case Dali::NativeImageSource::COLOR_DEPTH_24:
140 format = TBM_FORMAT_RGB888;
144 case Dali::NativeImageSource::COLOR_DEPTH_32:
146 format = TBM_FORMAT_ARGB8888;
152 DALI_LOG_WARNING("Wrong color depth.\n");
157 // set whether blending is required according to pixel format based on the depth
158 /* default pixel format is RGB888
159 If depth = 8, Pixel::A8;
160 If depth = 16, Pixel::RGB565;
161 If depth = 32, Pixel::RGBA8888 */
162 mBlendingRequired = (depth == 32 || depth == 8);
164 mTbmSurface = tbm_surface_create(mWidth, mHeight, format);
165 mOwnTbmSurface = true;
168 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny(Any source) const
175 if(source.GetType() == typeid(tbm_surface_h))
177 return AnyCast<tbm_surface_h>(source);
185 void NativeImageSourceTizen::DestroySurface()
189 if(mIsBufferAcquired)
195 if(tbm_surface_destroy(mTbmSurface) != TBM_SURFACE_ERROR_NONE)
197 DALI_LOG_ERROR("Failed to destroy tbm_surface\n");
202 tbm_surface_internal_unref(mTbmSurface);
207 NativeImageSourceTizen::~NativeImageSourceTizen()
212 Any NativeImageSourceTizen::GetNativeImageSource() const
214 return Any(mTbmSurface);
217 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
219 Dali::Mutex::ScopedLock lock(mMutex);
220 if(mTbmSurface != NULL)
222 tbm_surface_info_s surface_info;
224 if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE)
226 DALI_LOG_ERROR("Fail to map tbm_surface\n");
234 tbm_format format = surface_info.format;
235 uint32_t stride = surface_info.planes[0].stride;
236 unsigned char* ptr = surface_info.planes[0].ptr;
246 case TBM_FORMAT_RGB888:
248 lineSize = width * 3;
249 pixelFormat = Pixel::RGB888;
250 pixbuf.resize(lineSize * height);
251 unsigned char* bufptr = &pixbuf[0];
253 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
255 for(unsigned int c = 0; c < width; ++c)
258 offset = cOffset + r * stride;
259 *(bufptr + cOffset) = ptr[offset + 2];
260 *(bufptr + cOffset + 1) = ptr[offset + 1];
261 *(bufptr + cOffset + 2) = ptr[offset];
266 case TBM_FORMAT_RGBA8888:
268 lineSize = width * 4;
269 pixelFormat = Pixel::RGBA8888;
270 pixbuf.resize(lineSize * height);
271 unsigned char* bufptr = &pixbuf[0];
273 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
275 for(unsigned int c = 0; c < width; ++c)
278 offset = cOffset + r * stride;
279 *(bufptr + cOffset) = ptr[offset + 3];
280 *(bufptr + cOffset + 1) = ptr[offset + 2];
281 *(bufptr + cOffset + 2) = ptr[offset + 1];
282 *(bufptr + cOffset + 3) = ptr[offset];
287 case TBM_FORMAT_ARGB8888:
289 lineSize = width * 4;
290 pixelFormat = Pixel::RGBA8888;
291 pixbuf.resize(lineSize * height);
292 unsigned char* bufptr = &pixbuf[0];
294 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
296 for(unsigned int c = 0; c < width; ++c)
299 offset = cOffset + r * stride;
300 *(bufptr + cOffset) = ptr[offset + 2];
301 *(bufptr + cOffset + 1) = ptr[offset + 1];
302 *(bufptr + cOffset + 2) = ptr[offset];
303 *(bufptr + cOffset + 3) = ptr[offset + 3];
310 DALI_ASSERT_ALWAYS(0 && "Tbm surface has unsupported pixel format.\n");
316 if(tbm_surface_unmap(mTbmSurface) != TBM_SURFACE_ERROR_NONE)
318 DALI_LOG_ERROR("Fail to unmap tbm_surface\n");
324 DALI_LOG_WARNING("TBM surface does not exist.\n");
332 void NativeImageSourceTizen::SetSource(Any source)
334 Dali::Mutex::ScopedLock lock(mMutex);
338 mOwnTbmSurface = false;
339 mTbmSurface = GetSurfaceFromAny(source);
341 if(mTbmSurface != NULL)
344 tbm_surface_internal_ref(mTbmSurface);
345 mBlendingRequired = CheckBlending(tbm_surface_get_format(mTbmSurface));
346 mWidth = tbm_surface_get_width(mTbmSurface);
347 mHeight = tbm_surface_get_height(mTbmSurface);
351 bool NativeImageSourceTizen::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
355 tbm_format format = TBM_FORMAT_RGB888;
359 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
361 format = TBM_FORMAT_ARGB8888;
364 case Dali::NativeImageSource::COLOR_DEPTH_8:
366 format = TBM_FORMAT_C8;
369 case Dali::NativeImageSource::COLOR_DEPTH_16:
371 format = TBM_FORMAT_RGB565;
374 case Dali::NativeImageSource::COLOR_DEPTH_24:
376 format = TBM_FORMAT_RGB888;
379 case Dali::NativeImageSource::COLOR_DEPTH_32:
381 format = TBM_FORMAT_ARGB8888;
386 if(tbm_surface_query_formats(&formats, &formatNum))
388 for(unsigned int i = 0; i < formatNum; i++)
390 if(formats[i] == format)
402 bool NativeImageSourceTizen::CreateResource()
404 // If an EGL image exists, use it as it is without creating it.
405 if(mEglImageKHR != NULL)
410 // casting from an unsigned int to a void *, which should then be cast back
411 // to an unsigned int in the driver.
412 EGLClientBuffer eglBuffer = reinterpret_cast<EGLClientBuffer>(mTbmSurface);
413 if(!eglBuffer || !tbm_surface_internal_is_valid(mTbmSurface))
418 mEglImageExtensions = mEglGraphics->GetImageExtensions();
419 DALI_ASSERT_DEBUG(mEglImageExtensions);
421 // if resource of egl was not destroyed, destroy it first
424 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
426 return mEglImageKHR != NULL;
429 void NativeImageSourceTizen::DestroyResource()
431 Dali::Mutex::ScopedLock lock(mMutex);
434 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
440 uint32_t NativeImageSourceTizen::TargetTexture()
442 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
447 void NativeImageSourceTizen::PrepareTexture()
449 Dali::Mutex::ScopedLock lock(mMutex);
452 // Destroy previous eglImage because use for new one.
453 // if mEglImageKHR is not to be NULL here, it will not be updated with a new eglImage.
454 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
466 const char* NativeImageSourceTizen::GetCustomFragmentPrefix() const
468 return FRAGMENT_PREFIX;
471 const char* NativeImageSourceTizen::GetCustomSamplerTypename() const
476 int NativeImageSourceTizen::GetTextureTarget() const
478 return GL_TEXTURE_EXTERNAL_OES;
481 Any NativeImageSourceTizen::GetNativeImageHandle() const
483 return GetNativeImageSource();
486 bool NativeImageSourceTizen::SourceChanged() const
491 bool NativeImageSourceTizen::CheckBlending(tbm_format format)
493 if(mTbmFormat != format)
495 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
497 if(format == FORMATS_BLENDING_REQUIRED[i])
499 mBlendingRequired = true;
506 return mBlendingRequired;
509 uint8_t* NativeImageSourceTizen::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
511 Dali::Mutex::ScopedLock lock(mMutex);
512 if(mTbmSurface != NULL)
514 tbm_surface_info_s info;
516 if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &info) != TBM_SURFACE_ERROR_NONE)
518 DALI_LOG_ERROR("Fail to map tbm_surface\n");
525 tbm_surface_internal_ref(mTbmSurface);
526 mIsBufferAcquired = true;
528 stride = info.planes[0].stride;
532 return info.planes[0].ptr;
537 bool NativeImageSourceTizen::ReleaseBuffer()
539 Dali::Mutex::ScopedLock lock(mMutex);
541 if(mTbmSurface != NULL)
543 ret = (tbm_surface_unmap(mTbmSurface) == TBM_SURFACE_ERROR_NONE);
546 DALI_LOG_ERROR("Fail to unmap tbm_surface\n");
548 tbm_surface_internal_unref(mTbmSurface);
549 mIsBufferAcquired = false;
554 } // namespace Adaptor
556 } // namespace Internal