2 * Copyright (c) 2024 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.
18 #include <dali/internal/graphics/gles-impl/egl-graphics-controller.h>
21 #include <dali/integration-api/trace.h>
22 #include <dali/public-api/common/dali-common.h>
25 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/gl-abstraction.h>
28 #include <dali/integration-api/gl-defines.h>
29 #include <dali/integration-api/graphics-sync-abstraction.h>
30 #include <dali/integration-api/pixel-data-integ.h>
31 #include <dali/internal/graphics/gles-impl/egl-sync-object.h>
32 #include <dali/internal/graphics/gles-impl/gles-graphics-command-buffer.h>
33 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline.h>
34 #include <dali/internal/graphics/gles-impl/gles-graphics-program.h>
35 #include <dali/internal/graphics/gles-impl/gles-graphics-render-pass.h>
36 #include <dali/internal/graphics/gles-impl/gles-graphics-render-target.h>
37 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
38 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
39 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
40 #include <dali/internal/graphics/gles-impl/gles-sync-object.h>
41 #include <dali/internal/graphics/gles-impl/gles3-graphics-memory.h>
42 #include <dali/internal/graphics/gles/egl-sync-implementation.h>
44 #include <dali/internal/graphics/gles/egl-graphics.h>
48 // Uncomment the following define to turn on frame dumping
49 //#define ENABLE_COMMAND_BUFFER_FRAME_DUMP 1
50 #include <dali/internal/graphics/gles-impl/egl-graphics-controller-debug.h>
53 namespace Dali::Graphics
58 * @brief Custom deleter for all Graphics objects created
59 * with use of the Controller.
61 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
62 * doesn't destroy it directly but passes the ownership back
63 * to the Controller. The GLESDeleter is responsible for passing
64 * the object to the discard queue (by calling Resource::DiscardResource()).
69 GLESDeleter() = default;
71 void operator()(T* object)
73 // Discard resource (add it to discard queue)
74 object->DiscardResource();
79 * @brief Helper function allocating graphics object
81 * @param[in] info Create info structure
82 * @param[in] controller Controller object
83 * @param[out] out Unique pointer to the return object
85 template<class GLESType, class GfxCreateInfo, class T>
86 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
89 using Type = typename T::element_type;
90 using UPtr = Graphics::UniquePtr<Type>;
91 if(info.allocationCallbacks)
93 auto* memory = info.allocationCallbacks->allocCallback(
96 info.allocationCallbacks->userData);
97 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
99 else // Use standard allocator
101 // We are given all object for recycling
104 auto reusedObject = oldObject.release();
105 // If succeeded, attach the object to the unique_ptr and return it back
106 if(static_cast<GLESType*>(reusedObject)->TryRecycle(info, controller))
108 return UPtr(reusedObject, GLESDeleter<GLESType>());
112 // can't reuse so kill object by giving it back to original
114 oldObject.reset(reusedObject);
118 // Create brand new object
119 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
123 template<class T0, class T1>
124 T0* CastObject(T1* apiObject)
126 return static_cast<T0*>(apiObject);
129 // Maximum size of texture upload buffer.
130 const uint32_t TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB = 1;
132 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_EGL, false);
135 EglGraphicsController::EglGraphicsController()
136 : mTextureDependencyChecker(*this),
141 EglGraphicsController::~EglGraphicsController()
143 while(!mPresentationCommandBuffers.empty())
145 auto presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
146 delete presentCommandBuffer;
147 mPresentationCommandBuffers.pop();
151 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
153 DALI_LOG_RELEASE_INFO("Initializing Graphics Controller Phase 1\n");
154 mGlAbstraction = &glAbstraction;
155 mContext = std::make_unique<GLES::Context>(*this, mGlAbstraction);
156 mCurrentContext = mContext.get();
159 void EglGraphicsController::Initialize(Integration::GraphicsSyncAbstraction& syncImplementation,
160 Internal::Adaptor::GraphicsInterface& graphicsInterface)
162 DALI_LOG_RELEASE_INFO("Initializing Graphics Controller Phase 2\n");
163 auto* syncImplPtr = static_cast<Internal::Adaptor::EglSyncImplementation*>(&syncImplementation);
165 mEglSyncImplementation = syncImplPtr;
166 mGraphics = &graphicsInterface;
169 void EglGraphicsController::FrameStart()
171 mCapacity = 0; // Reset the command buffer capacity at the start of the frame.
174 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
176 for(auto& cmdbuf : submitInfo.cmdBuffer)
178 // Push command buffers
179 auto* commandBuffer = static_cast<GLES::CommandBuffer*>(cmdbuf);
180 mCapacity += commandBuffer->GetCapacity();
181 mCommandQueue.push(commandBuffer);
184 // If flush bit set, flush all pending tasks
185 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
191 void EglGraphicsController::WaitIdle()
196 void EglGraphicsController::PresentRenderTarget(RenderTarget* renderTarget)
198 GLES::CommandBuffer* presentCommandBuffer{nullptr};
199 if(mPresentationCommandBuffers.empty())
201 CommandBufferCreateInfo info;
202 info.SetLevel(CommandBufferLevel::PRIMARY);
203 info.fixedCapacity = 1; // only one command
204 presentCommandBuffer = new GLES::CommandBuffer(info, *this);
208 presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
209 presentCommandBuffer->Reset();
210 mPresentationCommandBuffers.pop();
212 presentCommandBuffer->PresentRenderTarget(static_cast<GLES::RenderTarget*>(renderTarget));
213 SubmitInfo submitInfo;
214 submitInfo.cmdBuffer = {presentCommandBuffer};
215 submitInfo.flags = 0 | SubmitFlagBits::FLUSH;
216 SubmitCommandBuffers(submitInfo);
219 void EglGraphicsController::ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget)
221 mCurrentContext->InvalidateDepthStencilBuffers();
223 auto* rt = static_cast<GLES::RenderTarget*>(renderTarget);
224 if(rt->GetCreateInfo().surface)
226 auto* surfaceInterface = reinterpret_cast<Dali::RenderSurfaceInterface*>(rt->GetCreateInfo().surface);
227 surfaceInterface->MakeContextCurrent();
228 surfaceInterface->PostRender();
232 void EglGraphicsController::PostRender()
234 mTextureDependencyChecker.Reset();
235 mSyncPool.AgeSyncObjects();
238 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
240 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
241 return *mGlAbstraction;
244 Integration::GraphicsConfig& EglGraphicsController::GetGraphicsConfig()
246 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
247 return *mGlAbstraction;
250 Internal::Adaptor::EglSyncImplementation& EglGraphicsController::GetEglSyncImplementation()
252 DALI_ASSERT_DEBUG(mEglSyncImplementation && "Sync implementation not initialized");
253 return *mEglSyncImplementation;
256 Graphics::UniquePtr<CommandBuffer> EglGraphicsController::CreateCommandBuffer(
257 const CommandBufferCreateInfo& commandBufferCreateInfo,
258 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
260 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
263 Graphics::UniquePtr<RenderPass> EglGraphicsController::CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass)
265 return NewObject<GLES::RenderPass>(renderPassCreateInfo, *this, std::move(oldRenderPass));
268 Graphics::UniquePtr<Texture>
269 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
271 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
274 Graphics::UniquePtr<Buffer> EglGraphicsController::CreateBuffer(
275 const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
277 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
280 Graphics::UniquePtr<Framebuffer> EglGraphicsController::CreateFramebuffer(
281 const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer)
283 return NewObject<GLES::Framebuffer>(framebufferCreateInfo, *this, std::move(oldFramebuffer));
286 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(
287 const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
289 // Create pipeline cache if needed
292 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
295 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
298 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(
299 const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
301 // Create pipeline cache if needed
304 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
307 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
310 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
312 // Create pipeline cache if needed
315 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
317 return mPipelineCache->GetShader(shaderCreateInfo, std::move(oldShader));
320 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
322 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
325 Graphics::UniquePtr<RenderTarget> EglGraphicsController::CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget)
327 return NewObject<GLES::RenderTarget>(renderTargetCreateInfo, *this, std::move(oldRenderTarget));
330 Graphics::UniquePtr<SyncObject> EglGraphicsController::CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
331 UniquePtr<SyncObject>&& oldSyncObject)
333 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
335 return NewObject<EGL::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
339 return NewObject<GLES::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
343 MemoryRequirements EglGraphicsController::GetBufferMemoryRequirements(Buffer& buffer) const
345 MemoryRequirements requirements{};
351 gl->GetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &align);
352 requirements.alignment = align;
357 TextureProperties EglGraphicsController::GetTextureProperties(const Texture& texture)
359 const GLES::Texture* glesTexture = static_cast<const GLES::Texture*>(&texture);
360 const auto& createInfo = glesTexture->GetCreateInfo();
362 TextureProperties properties{};
363 properties.format = createInfo.format;
364 properties.compressed = glesTexture->IsCompressed();
365 properties.extent2D = createInfo.size;
366 properties.nativeHandle = glesTexture->GetGLTexture();
367 // TODO: Skip format1, emulated, packed, directWriteAccessEnabled of TextureProperties for now
372 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
374 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
377 void EglGraphicsController::CreateSurfaceContext(Dali::RenderSurfaceInterface* surface)
379 std::unique_ptr<GLES::Context> context = std::make_unique<GLES::Context>(*this, mGlAbstraction);
380 mSurfaceContexts.push_back(std::move(std::make_pair(surface, std::move(context))));
383 void EglGraphicsController::DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface)
385 mSurfaceContexts.erase(std::remove_if(
386 mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) { return surface == iter.first; }),
387 mSurfaceContexts.end());
390 void EglGraphicsController::ActivateResourceContext()
392 mCurrentContext = mContext.get();
393 mCurrentContext->GlContextCreated();
396 auto eglGraphics = dynamic_cast<Dali::Internal::Adaptor::EglGraphics*>(mGraphics);
399 mSharedContext = eglGraphics->GetEglImplementation().GetContext();
404 void EglGraphicsController::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
406 if(surface && mGraphics->IsResourceContextSupported())
408 auto iter = std::find_if(mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) { return (iter.first == surface); });
410 if(iter != mSurfaceContexts.end())
412 mCurrentContext = iter->second.get();
413 mCurrentContext->GlContextCreated();
418 void EglGraphicsController::AddTexture(GLES::Texture& texture)
420 // Assuming we are on the correct context
421 mCreateTextureQueue.push(&texture);
424 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
426 // Assuming we are on the correct context
427 mCreateBufferQueue.push(&buffer);
430 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
432 // Assuming we are on the correct context
433 mCreateFramebufferQueue.push(&framebuffer);
436 void EglGraphicsController::ProcessDiscardQueues()
438 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EGL_CONTROLLER_DISCARD_QUEUE");
441 ProcessDiscardSet<GLES::Texture>(mDiscardTextureSet);
444 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
446 // Process Framebuffers
447 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
449 // Process RenderPass
450 ProcessDiscardQueue<GLES::RenderPass>(mDiscardRenderPassQueue);
452 // Process RenderTarget
453 ProcessDiscardQueue<GLES::RenderTarget>(mDiscardRenderTargetQueue);
456 if(mPipelineCache && !mDiscardPipelineQueue.empty())
458 mPipelineCache->MarkPipelineCacheFlushRequired();
460 ProcessDiscardQueue(mDiscardPipelineQueue);
463 if(mPipelineCache && !mDiscardProgramQueue.empty())
465 mPipelineCache->MarkProgramCacheFlushRequired();
467 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
470 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
473 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
475 // Process command buffers
476 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
479 void EglGraphicsController::ProcessCreateQueues()
481 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EGL_CONTROLLER_CREATE_QUEUE");
483 ProcessCreateQueue(mCreateTextureQueue);
486 ProcessCreateQueue(mCreateBufferQueue);
488 // Process framebuffers
489 ProcessCreateQueue(mCreateFramebufferQueue);
492 void EglGraphicsController::ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer)
495 const auto commands = commandBuffer.GetCommands(count);
497 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EGL_CONTROLLER_PROCESS", [&](std::ostringstream& oss) {
498 oss << "[commandCount:" << count << "]";
501 for(auto i = 0u; i < count; ++i)
503 auto& cmd = commands[i];
507 case GLES::CommandType::FLUSH:
509 // Nothing to do here
512 case GLES::CommandType::BIND_TEXTURES:
514 mCurrentContext->BindTextures(cmd.bindTextures.textureBindings.Ptr(), cmd.bindTextures.textureBindingsCount);
517 case GLES::CommandType::BIND_VERTEX_BUFFERS:
519 auto bindings = cmd.bindVertexBuffers.vertexBufferBindings.Ptr();
520 mCurrentContext->BindVertexBuffers(bindings, cmd.bindVertexBuffers.vertexBufferBindingsCount);
523 case GLES::CommandType::BIND_UNIFORM_BUFFER:
525 auto& bindings = cmd.bindUniformBuffers;
526 mCurrentContext->BindUniformBuffers(bindings.uniformBufferBindingsCount ? bindings.uniformBufferBindings.Ptr() : nullptr, bindings.uniformBufferBindingsCount, bindings.standaloneUniformsBufferBinding);
529 case GLES::CommandType::BIND_INDEX_BUFFER:
531 mCurrentContext->BindIndexBuffer(cmd.bindIndexBuffer);
534 case GLES::CommandType::BIND_SAMPLERS:
538 case GLES::CommandType::BIND_PIPELINE:
540 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
541 mCurrentContext->BindPipeline(pipeline);
544 case GLES::CommandType::DRAW:
546 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
549 case GLES::CommandType::DRAW_INDEXED:
551 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
554 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
556 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
559 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
561 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
564 case GLES::CommandType::SET_SCISSOR_TEST:
566 mCurrentContext->SetScissorTestEnabled(cmd.scissorTest.enable);
569 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
571 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
575 case GLES::CommandType::SET_COLOR_MASK:
577 mCurrentContext->ColorMask(cmd.colorMask.enabled);
580 case GLES::CommandType::CLEAR_STENCIL_BUFFER:
582 mCurrentContext->ClearStencilBuffer();
585 case GLES::CommandType::CLEAR_DEPTH_BUFFER:
587 mCurrentContext->ClearDepthBuffer();
591 case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
593 mCurrentContext->SetStencilTestEnable(cmd.stencilTest.enabled);
597 case GLES::CommandType::SET_STENCIL_FUNC:
599 mCurrentContext->StencilFunc(cmd.stencilFunc.compareOp,
600 cmd.stencilFunc.reference,
601 cmd.stencilFunc.compareMask);
605 case GLES::CommandType::SET_STENCIL_WRITE_MASK:
607 mCurrentContext->StencilMask(cmd.stencilWriteMask.mask);
611 case GLES::CommandType::SET_STENCIL_OP:
613 mCurrentContext->StencilOp(cmd.stencilOp.failOp,
614 cmd.stencilOp.depthFailOp,
615 cmd.stencilOp.passOp);
619 case GLES::CommandType::SET_DEPTH_COMPARE_OP:
621 mCurrentContext->SetDepthCompareOp(cmd.depth.compareOp);
624 case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
626 mCurrentContext->SetDepthTestEnable(cmd.depth.testEnabled);
629 case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
631 mCurrentContext->SetDepthWriteEnable(cmd.depth.writeEnabled);
635 case GLES::CommandType::BEGIN_RENDERPASS:
637 auto& renderTarget = *cmd.beginRenderPass.renderTarget;
638 const auto& targetInfo = renderTarget.GetCreateInfo();
640 if(targetInfo.surface)
642 // switch to surface context
643 mGraphics->ActivateSurfaceContext(static_cast<Dali::RenderSurfaceInterface*>(targetInfo.surface));
645 else if(targetInfo.framebuffer)
647 // switch to resource context
648 mGraphics->ActivateResourceContext();
651 mCurrentContext->BeginRenderPass(cmd.beginRenderPass);
655 case GLES::CommandType::END_RENDERPASS:
657 mCurrentContext->EndRenderPass(mTextureDependencyChecker);
659 // This sync object is to enable cpu to wait for rendering to complete, not gpu.
660 // It's only needed for reading the framebuffer texture in the client.
661 auto syncObject = const_cast<GLES::SyncObject*>(static_cast<const GLES::SyncObject*>(cmd.endRenderPass.syncObject));
664 syncObject->InitializeResource();
668 case GLES::CommandType::PRESENT_RENDER_TARGET:
670 ResolvePresentRenderTarget(cmd.presentRenderTarget.targetToPresent);
672 // The command buffer will be pushed into the queue of presentation command buffers
673 // for further reuse.
674 if(commandBuffer.GetCreateInfo().fixedCapacity == 1)
676 mPresentationCommandBuffers.push(&commandBuffer);
680 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
682 // Process secondary command buffers
683 // todo: check validity of the secondaries
684 // there are operations which are illigal to be done
685 // within secondaries.
686 auto buffers = cmd.executeCommandBuffers.buffers.Ptr();
687 for(auto j = 0u; j < cmd.executeCommandBuffers.buffersCount; ++j)
689 auto& buf = buffers[j];
690 ProcessCommandBuffer(*static_cast<const GLES::CommandBuffer*>(buf));
694 case GLES::CommandType::DRAW_NATIVE:
696 auto* info = &cmd.drawNative.drawNativeInfo;
698 mCurrentContext->PrepareForNativeRendering();
700 if(info->glesNativeInfo.eglSharedContextStoragePointer)
702 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
703 *anyContext = mSharedContext;
706 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
708 mCurrentContext->RestoreFromNativeRendering();
713 DALI_TRACE_END(gTraceFilter, "DALI_EGL_CONTROLLER_PROCESS");
716 void EglGraphicsController::ProcessCommandQueues()
720 while(!mCommandQueue.empty())
722 auto cmdBuf = mCommandQueue.front();
724 DUMP_FRAME_COMMAND_BUFFER(cmdBuf);
725 ProcessCommandBuffer(*cmdBuf);
731 void EglGraphicsController::ProcessTextureUpdateQueue()
733 if(mTextureUpdateRequests.empty())
737 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EGL_CONTROLLER_TEXTURE_UPDATE");
738 while(!mTextureUpdateRequests.empty())
740 TextureUpdateRequest& request = mTextureUpdateRequests.front();
742 auto& info = request.first;
743 auto& source = request.second;
745 switch(source.sourceType)
747 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
748 case Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA:
750 // GPU memory must be already allocated.
752 // Check if it needs conversion
753 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
754 const auto& createInfo = texture->GetCreateInfo();
755 auto srcFormat = GLES::GLTextureFormatType(info.srcFormat).format;
756 auto srcType = GLES::GLTextureFormatType(info.srcFormat).type;
757 auto destInternalFormat = GLES::GLTextureFormatType(createInfo.format).internalFormat;
758 auto destFormat = GLES::GLTextureFormatType(createInfo.format).format;
760 // From render-texture.cpp
761 const bool isSubImage(info.dstOffset2D.x != 0 || info.dstOffset2D.y != 0 ||
762 info.srcExtent2D.width != (createInfo.size.width / (1 << info.level)) ||
763 info.srcExtent2D.height != (createInfo.size.height / (1 << info.level)));
765 uint8_t* sourceBuffer = nullptr;
766 bool sourceBufferReleaseRequired = false;
767 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
769 sourceBuffer = reinterpret_cast<uint8_t*>(source.memorySource.memory);
770 sourceBufferReleaseRequired = true;
774 Dali::Integration::PixelDataBuffer pixelBufferData = Dali::Integration::GetPixelDataBuffer(source.pixelDataSource.pixelData);
776 sourceBuffer = pixelBufferData.buffer + info.srcOffset;
777 sourceBufferReleaseRequired = Dali::Integration::IsPixelDataReleaseAfterUpload(source.pixelDataSource.pixelData) && info.srcOffset == 0u;
780 // Skip texture upload if given texture is already discarded for this render loop.
781 if(mDiscardTextureSet.find(texture) == mDiscardTextureSet.end())
783 auto sourceStride = info.srcStride;
784 std::vector<uint8_t> tempBuffer;
786 if(mGlAbstraction->TextureRequiresConverting(srcFormat, destFormat, isSubImage))
788 // Convert RGB to RGBA if necessary.
789 if(texture->TryConvertPixelData(sourceBuffer, info.srcFormat, createInfo.format, info.srcSize, info.srcStride, info.srcExtent2D.width, info.srcExtent2D.height, tempBuffer))
791 sourceBuffer = &tempBuffer[0];
792 sourceStride = 0u; // Converted buffer compacted. make stride as 0.
793 srcFormat = destFormat;
794 srcType = GLES::GLTextureFormatType(createInfo.format).type;
798 // Calculate the maximum mipmap level for the texture
799 texture->SetMaxMipMapLevel(std::max(texture->GetMaxMipMapLevel(), info.level));
801 GLenum bindTarget{GL_TEXTURE_2D};
802 GLenum target{GL_TEXTURE_2D};
804 if(createInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
806 bindTarget = GL_TEXTURE_CUBE_MAP;
807 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + info.layer;
810 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
811 mGlAbstraction->PixelStorei(GL_UNPACK_ROW_LENGTH, sourceStride);
813 mCurrentContext->BindTexture(bindTarget, texture->GetTextureTypeId(), texture->GetGLTexture());
817 if(!texture->IsCompressed())
819 mGlAbstraction->TexImage2D(target,
822 info.srcExtent2D.width,
823 info.srcExtent2D.height,
831 mGlAbstraction->CompressedTexImage2D(target,
834 info.srcExtent2D.width,
835 info.srcExtent2D.height,
843 if(!texture->IsCompressed())
845 mGlAbstraction->TexSubImage2D(target,
849 info.srcExtent2D.width,
850 info.srcExtent2D.height,
857 mGlAbstraction->CompressedTexSubImage2D(target,
861 info.srcExtent2D.width,
862 info.srcExtent2D.height,
870 if(sourceBufferReleaseRequired && sourceBuffer != nullptr)
872 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
874 free(reinterpret_cast<void*>(sourceBuffer));
878 Dali::Integration::ReleasePixelDataBuffer(source.pixelDataSource.pixelData);
885 // TODO: other sources
890 mTextureUpdateRequests.pop();
894 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
895 const std::vector<TextureUpdateSourceInfo>& sourceList)
898 for(auto& info : updateInfoList)
900 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
901 auto& pair = mTextureUpdateRequests.back();
902 switch(pair.second.sourceType)
904 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
906 auto& info = pair.first;
907 auto& source = pair.second;
909 // allocate staging memory and copy the data
910 // TODO: using PBO with GLES3, this is just naive
913 uint8_t* stagingBuffer = reinterpret_cast<uint8_t*>(malloc(info.srcSize));
915 uint8_t* srcMemory = &reinterpret_cast<uint8_t*>(source.memorySource.memory)[info.srcOffset];
917 std::copy(srcMemory, srcMemory + info.srcSize, stagingBuffer);
919 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
921 // store staging buffer
922 source.memorySource.memory = stagingBuffer;
925 case Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA:
927 // Increase CPU memory usage since ownership of PixelData is now on mTextureUpdateRequests.
928 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
931 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
933 // TODO, with PBO support
936 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
938 // TODO texture 2 texture in-GPU copy
944 // If upload buffer exceeds maximum size, flush.
945 if(mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB * 1024 * 1024)
948 mTextureUploadTotalCPUMemoryUsed = 0;
952 void EglGraphicsController::ProcessTextureMipmapGenerationQueue()
954 if(mTextureMipmapGenerationRequests.empty())
958 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EGL_CONTROLLER_TEXTURE_MIPMAP");
959 while(!mTextureMipmapGenerationRequests.empty())
961 auto* texture = mTextureMipmapGenerationRequests.front();
963 mCurrentContext->BindTexture(texture->GetGlTarget(), texture->GetTextureTypeId(), texture->GetGLTexture());
964 mCurrentContext->GenerateMipmap(texture->GetGlTarget());
966 mTextureMipmapGenerationRequests.pop();
970 void EglGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
972 mTextureMipmapGenerationRequests.push(static_cast<const GLES::Texture*>(&texture));
975 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
977 // Mapping buffer requires the object to be created NOW
978 // Workaround - flush now, otherwise there will be given a staging buffer
979 // in case when the buffer is not there yet
980 if(!mCreateBufferQueue.empty())
982 mGraphics->ActivateResourceContext();
983 ProcessCreateQueues();
986 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
988 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
992 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
996 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
998 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
1001 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
1003 return *mPipelineCache;
1006 Graphics::Texture* EglGraphicsController::CreateTextureByResourceId(uint32_t resourceId, const Graphics::TextureCreateInfo& createInfo)
1008 Graphics::Texture* ret = nullptr;
1009 Graphics::UniquePtr<Graphics::Texture> texture;
1011 auto iter = mExternalTextureResources.find(resourceId);
1012 DALI_ASSERT_ALWAYS(iter == mExternalTextureResources.end());
1014 texture = CreateTexture(createInfo, std::move(texture));
1016 ret = texture.get();
1018 mExternalTextureResources.insert(std::make_pair(resourceId, std::move(texture)));
1023 void EglGraphicsController::DiscardTextureFromResourceId(uint32_t resourceId)
1025 auto iter = mExternalTextureResources.find(resourceId);
1026 if(iter != mExternalTextureResources.end())
1028 mExternalTextureResources.erase(iter);
1032 Graphics::Texture* EglGraphicsController::GetTextureFromResourceId(uint32_t resourceId)
1034 Graphics::Texture* ret = nullptr;
1036 auto iter = mExternalTextureResources.find(resourceId);
1037 if(iter != mExternalTextureResources.end())
1039 ret = iter->second.get();
1045 Graphics::UniquePtr<Graphics::Texture> EglGraphicsController::ReleaseTextureFromResourceId(uint32_t resourceId)
1047 Graphics::UniquePtr<Graphics::Texture> texture;
1049 auto iter = mExternalTextureResources.find(resourceId);
1050 if(iter != mExternalTextureResources.end())
1052 texture = std::move(iter->second);
1053 mExternalTextureResources.erase(iter);
1059 } // namespace Dali::Graphics