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* SAMPLER_TYPE = "samplerExternalOES";
44 tbm_format 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 using Dali::Integration::PixelBuffer;
63 NativeImageSourceTizen* NativeImageSourceTizen::New(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
65 NativeImageSourceTizen* image = new NativeImageSourceTizen(width, height, depth, nativeImageSource);
66 DALI_ASSERT_DEBUG(image && "NativeImageSource allocation failed.");
76 NativeImageSourceTizen::NativeImageSourceTizen(uint32_t width, uint32_t height, Dali::NativeImageSource::ColorDepth depth, Any nativeImageSource)
79 mOwnTbmSurface(false),
82 mBlendingRequired(false),
86 mEglImageExtensions(NULL),
89 mIsBufferAcquired(false)
91 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
93 GraphicsInterface* graphics = &(Adaptor::GetImplementation(Adaptor::Get()).GetGraphicsInterface());
94 mEglGraphics = static_cast<EglGraphics*>(graphics);
96 mTbmSurface = GetSurfaceFromAny(nativeImageSource);
98 if(mTbmSurface != NULL)
100 tbm_surface_internal_ref(mTbmSurface);
101 mBlendingRequired = CheckBlending(tbm_surface_get_format(mTbmSurface));
102 mWidth = tbm_surface_get_width(mTbmSurface);
103 mHeight = tbm_surface_get_height(mTbmSurface);
107 void NativeImageSourceTizen::Initialize()
109 if(mTbmSurface != NULL || mWidth == 0 || mHeight == 0)
114 tbm_format format = TBM_FORMAT_RGB888;
119 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
121 format = TBM_FORMAT_ARGB8888;
125 case Dali::NativeImageSource::COLOR_DEPTH_8:
127 format = TBM_FORMAT_C8;
131 case Dali::NativeImageSource::COLOR_DEPTH_16:
133 format = TBM_FORMAT_RGB565;
137 case Dali::NativeImageSource::COLOR_DEPTH_24:
139 format = TBM_FORMAT_RGB888;
143 case Dali::NativeImageSource::COLOR_DEPTH_32:
145 format = TBM_FORMAT_ARGB8888;
151 DALI_LOG_WARNING("Wrong color depth.\n");
156 // set whether blending is required according to pixel format based on the depth
157 /* default pixel format is RGB888
158 If depth = 8, Pixel::A8;
159 If depth = 16, Pixel::RGB565;
160 If depth = 32, Pixel::RGBA8888 */
161 mBlendingRequired = (depth == 32 || depth == 8);
163 mTbmSurface = tbm_surface_create(mWidth, mHeight, format);
164 mOwnTbmSurface = true;
167 tbm_surface_h NativeImageSourceTizen::GetSurfaceFromAny(Any source) const
174 if(source.GetType() == typeid(tbm_surface_h))
176 return AnyCast<tbm_surface_h>(source);
184 void NativeImageSourceTizen::DestroySurface()
188 if(mIsBufferAcquired)
194 if(tbm_surface_destroy(mTbmSurface) != TBM_SURFACE_ERROR_NONE)
196 DALI_LOG_ERROR("Failed to destroy tbm_surface\n");
201 tbm_surface_internal_unref(mTbmSurface);
206 NativeImageSourceTizen::~NativeImageSourceTizen()
211 Any NativeImageSourceTizen::GetNativeImageSource() const
213 return Any(mTbmSurface);
216 bool NativeImageSourceTizen::GetPixels(std::vector<unsigned char>& pixbuf, unsigned& width, unsigned& height, Pixel::Format& pixelFormat) const
218 Dali::Mutex::ScopedLock lock(mMutex);
219 if(mTbmSurface != NULL)
221 tbm_surface_info_s surface_info;
223 if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ, &surface_info) != TBM_SURFACE_ERROR_NONE)
225 DALI_LOG_ERROR("Fail to map tbm_surface\n");
233 tbm_format format = surface_info.format;
234 uint32_t stride = surface_info.planes[0].stride;
235 unsigned char* ptr = surface_info.planes[0].ptr;
245 case TBM_FORMAT_RGB888:
247 lineSize = width * 3;
248 pixelFormat = Pixel::RGB888;
249 pixbuf.resize(lineSize * height);
250 unsigned char* bufptr = &pixbuf[0];
252 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
254 for(unsigned int c = 0; c < width; ++c)
257 offset = cOffset + r * stride;
258 *(bufptr + cOffset) = ptr[offset + 2];
259 *(bufptr + cOffset + 1) = ptr[offset + 1];
260 *(bufptr + cOffset + 2) = ptr[offset];
265 case TBM_FORMAT_RGBA8888:
267 lineSize = width * 4;
268 pixelFormat = Pixel::RGBA8888;
269 pixbuf.resize(lineSize * height);
270 unsigned char* bufptr = &pixbuf[0];
272 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
274 for(unsigned int c = 0; c < width; ++c)
277 offset = cOffset + r * stride;
278 *(bufptr + cOffset) = ptr[offset + 3];
279 *(bufptr + cOffset + 1) = ptr[offset + 2];
280 *(bufptr + cOffset + 2) = ptr[offset + 1];
281 *(bufptr + cOffset + 3) = ptr[offset];
286 case TBM_FORMAT_ARGB8888:
288 lineSize = width * 4;
289 pixelFormat = Pixel::RGBA8888;
290 pixbuf.resize(lineSize * height);
291 unsigned char* bufptr = &pixbuf[0];
293 for(unsigned int r = 0; r < height; ++r, bufptr += lineSize)
295 for(unsigned int c = 0; c < width; ++c)
298 offset = cOffset + r * stride;
299 *(bufptr + cOffset) = ptr[offset + 2];
300 *(bufptr + cOffset + 1) = ptr[offset + 1];
301 *(bufptr + cOffset + 2) = ptr[offset];
302 *(bufptr + cOffset + 3) = ptr[offset + 3];
309 DALI_ASSERT_ALWAYS(0 && "Tbm surface has unsupported pixel format.\n");
315 if(tbm_surface_unmap(mTbmSurface) != TBM_SURFACE_ERROR_NONE)
317 DALI_LOG_ERROR("Fail to unmap tbm_surface\n");
323 DALI_LOG_WARNING("TBM surface does not exist.\n");
331 void NativeImageSourceTizen::SetSource(Any source)
333 Dali::Mutex::ScopedLock lock(mMutex);
337 mOwnTbmSurface = false;
338 mTbmSurface = GetSurfaceFromAny(source);
340 if(mTbmSurface != NULL)
343 tbm_surface_internal_ref(mTbmSurface);
344 mBlendingRequired = CheckBlending(tbm_surface_get_format(mTbmSurface));
345 mWidth = tbm_surface_get_width(mTbmSurface);
346 mHeight = tbm_surface_get_height(mTbmSurface);
350 bool NativeImageSourceTizen::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
354 tbm_format format = TBM_FORMAT_RGB888;
358 case Dali::NativeImageSource::COLOR_DEPTH_DEFAULT:
360 format = TBM_FORMAT_ARGB8888;
363 case Dali::NativeImageSource::COLOR_DEPTH_8:
365 format = TBM_FORMAT_C8;
368 case Dali::NativeImageSource::COLOR_DEPTH_16:
370 format = TBM_FORMAT_RGB565;
373 case Dali::NativeImageSource::COLOR_DEPTH_24:
375 format = TBM_FORMAT_RGB888;
378 case Dali::NativeImageSource::COLOR_DEPTH_32:
380 format = TBM_FORMAT_ARGB8888;
385 if(tbm_surface_query_formats(&formats, &formatNum))
387 for(unsigned int i = 0; i < formatNum; i++)
389 if(formats[i] == format)
401 bool NativeImageSourceTizen::CreateResource()
403 // If an EGL image exists, use it as it is without creating it.
404 if(mEglImageKHR != NULL)
409 // casting from an unsigned int to a void *, which should then be cast back
410 // to an unsigned int in the driver.
411 EGLClientBuffer eglBuffer = reinterpret_cast<EGLClientBuffer>(mTbmSurface);
412 if(!eglBuffer || !tbm_surface_internal_is_valid(mTbmSurface))
417 mEglImageExtensions = mEglGraphics->GetImageExtensions();
418 DALI_ASSERT_DEBUG(mEglImageExtensions);
420 mEglImageKHR = mEglImageExtensions->CreateImageKHR(eglBuffer);
422 return mEglImageKHR != NULL;
425 void NativeImageSourceTizen::DestroyResource()
427 Dali::Mutex::ScopedLock lock(mMutex);
430 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
436 uint32_t NativeImageSourceTizen::TargetTexture()
438 mEglImageExtensions->TargetTextureKHR(mEglImageKHR);
443 void NativeImageSourceTizen::PrepareTexture()
445 Dali::Mutex::ScopedLock lock(mMutex);
448 // Destroy previous eglImage because use for new one.
449 // if mEglImageKHR is not to be NULL here, it will not be updated with a new eglImage.
450 mEglImageExtensions->DestroyImageKHR(mEglImageKHR);
462 bool NativeImageSourceTizen::ApplyNativeFragmentShader(std::string& shader)
464 return mEglGraphics->ApplyNativeFragmentShader(shader, SAMPLER_TYPE);
467 const char* NativeImageSourceTizen::GetCustomSamplerTypename() const
472 int NativeImageSourceTizen::GetTextureTarget() const
474 return GL_TEXTURE_EXTERNAL_OES;
477 Any NativeImageSourceTizen::GetNativeImageHandle() const
479 return GetNativeImageSource();
482 bool NativeImageSourceTizen::SourceChanged() const
487 bool NativeImageSourceTizen::CheckBlending(tbm_format format)
489 if(mTbmFormat != format)
491 for(int i = 0; i < NUM_FORMATS_BLENDING_REQUIRED; ++i)
493 if(format == FORMATS_BLENDING_REQUIRED[i])
495 mBlendingRequired = true;
502 return mBlendingRequired;
505 uint8_t* NativeImageSourceTizen::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
507 Dali::Mutex::ScopedLock lock(mMutex);
508 if(mTbmSurface != NULL)
510 tbm_surface_info_s info;
512 if(tbm_surface_map(mTbmSurface, TBM_SURF_OPTION_READ | TBM_SURF_OPTION_WRITE, &info) != TBM_SURFACE_ERROR_NONE)
514 DALI_LOG_ERROR("Fail to map tbm_surface\n");
521 tbm_surface_internal_ref(mTbmSurface);
522 mIsBufferAcquired = true;
524 stride = info.planes[0].stride;
528 return info.planes[0].ptr;
533 bool NativeImageSourceTizen::ReleaseBuffer()
535 Dali::Mutex::ScopedLock lock(mMutex);
537 if(mTbmSurface != NULL)
539 ret = (tbm_surface_unmap(mTbmSurface) == TBM_SURFACE_ERROR_NONE);
542 DALI_LOG_ERROR("Fail to unmap tbm_surface\n");
544 tbm_surface_internal_unref(mTbmSurface);
545 mIsBufferAcquired = false;
550 } // namespace Adaptor
552 } // namespace Internal