2 * Copyright (c) 2022 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/adaptor-framework/render-surface-interface.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
25 #include <dali/integration-api/graphics-sync-abstraction.h>
26 #include <dali/internal/graphics/gles-impl/egl-sync-object.h>
27 #include <dali/internal/graphics/gles-impl/gles-graphics-command-buffer.h>
28 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline.h>
29 #include <dali/internal/graphics/gles-impl/gles-graphics-program.h>
30 #include <dali/internal/graphics/gles-impl/gles-graphics-render-pass.h>
31 #include <dali/internal/graphics/gles-impl/gles-graphics-render-target.h>
32 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
33 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
34 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
35 #include <dali/internal/graphics/gles-impl/gles-sync-object.h>
36 #include <dali/internal/graphics/gles-impl/gles3-graphics-memory.h>
37 #include <dali/internal/graphics/gles/egl-sync-implementation.h>
38 #include <dali/public-api/common/dali-common.h>
40 #include <dali/internal/graphics/gles/egl-graphics.h>
44 // Uncomment the following define to turn on frame dumping
45 //#define ENABLE_COMMAND_BUFFER_FRAME_DUMP 1
46 #include <dali/internal/graphics/gles-impl/egl-graphics-controller-debug.h>
49 namespace Dali::Graphics
54 * @brief Custom deleter for all Graphics objects created
55 * with use of the Controller.
57 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
58 * doesn't destroy it directly but passes the ownership back
59 * to the Controller. The GLESDeleter is responsible for passing
60 * the object to the discard queue (by calling Resource::DiscardResource()).
65 GLESDeleter() = default;
67 void operator()(T* object)
69 // Discard resource (add it to discard queue)
70 object->DiscardResource();
75 * @brief Helper function allocating graphics object
77 * @param[in] info Create info structure
78 * @param[in] controller Controller object
79 * @param[out] out Unique pointer to the return object
81 template<class GLESType, class GfxCreateInfo, class T>
82 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
85 using Type = typename T::element_type;
86 using UPtr = Graphics::UniquePtr<Type>;
87 if(info.allocationCallbacks)
89 auto* memory = info.allocationCallbacks->allocCallback(
92 info.allocationCallbacks->userData);
93 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
95 else // Use standard allocator
97 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
101 template<class T0, class T1>
102 T0* CastObject(T1* apiObject)
104 return static_cast<T0*>(apiObject);
107 // Maximum size of texture upload buffer.
108 const uint32_t TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB = 1;
112 EglGraphicsController::EglGraphicsController()
113 : mTextureDependencyChecker(*this),
118 EglGraphicsController::~EglGraphicsController()
120 while(!mPresentationCommandBuffers.empty())
122 auto presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
123 delete presentCommandBuffer;
124 mPresentationCommandBuffers.pop();
128 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
130 DALI_LOG_RELEASE_INFO("Initializing Graphics Controller Phase 1\n");
131 mGlAbstraction = &glAbstraction;
132 mContext = std::make_unique<GLES::Context>(*this);
133 mCurrentContext = mContext.get();
136 void EglGraphicsController::Initialize(Integration::GraphicsSyncAbstraction& syncImplementation,
137 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
138 Internal::Adaptor::GraphicsInterface& graphicsInterface)
140 DALI_LOG_RELEASE_INFO("Initializing Graphics Controller Phase 2\n");
141 auto* syncImplPtr = static_cast<Internal::Adaptor::EglSyncImplementation*>(&syncImplementation);
143 mEglSyncImplementation = syncImplPtr;
144 mGlContextHelperAbstraction = &glContextHelperAbstraction;
145 mGraphics = &graphicsInterface;
148 void EglGraphicsController::FrameStart()
150 mCapacity = 0; // Reset the command buffer capacity at the start of the frame.
153 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
155 for(auto& cmdbuf : submitInfo.cmdBuffer)
157 // Push command buffers
158 auto* commandBuffer = static_cast<GLES::CommandBuffer*>(cmdbuf);
159 mCapacity += commandBuffer->GetCapacity();
160 mCommandQueue.push(commandBuffer);
163 // If flush bit set, flush all pending tasks
164 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
170 void EglGraphicsController::PresentRenderTarget(RenderTarget* renderTarget)
172 GLES::CommandBuffer* presentCommandBuffer{nullptr};
173 if(mPresentationCommandBuffers.empty())
175 CommandBufferCreateInfo info;
176 info.SetLevel(CommandBufferLevel::PRIMARY);
177 info.fixedCapacity = 1; // only one command
178 presentCommandBuffer = new GLES::CommandBuffer(info, *this);
182 presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
183 presentCommandBuffer->Reset();
184 mPresentationCommandBuffers.pop();
186 presentCommandBuffer->PresentRenderTarget(static_cast<GLES::RenderTarget*>(renderTarget));
187 SubmitInfo submitInfo;
188 submitInfo.cmdBuffer = {presentCommandBuffer};
189 submitInfo.flags = 0 | SubmitFlagBits::FLUSH;
190 SubmitCommandBuffers(submitInfo);
193 void EglGraphicsController::ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget)
195 mCurrentContext->InvalidateDepthStencilBuffers();
197 auto* rt = static_cast<GLES::RenderTarget*>(renderTarget);
198 if(rt->GetCreateInfo().surface)
200 auto* surfaceInterface = reinterpret_cast<Dali::RenderSurfaceInterface*>(rt->GetCreateInfo().surface);
201 surfaceInterface->MakeContextCurrent();
202 surfaceInterface->PostRender();
206 void EglGraphicsController::PostRender()
208 mTextureDependencyChecker.Reset();
209 mSyncPool.AgeSyncObjects();
212 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
214 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
215 return *mGlAbstraction;
218 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
220 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
221 return *mGlContextHelperAbstraction;
224 Internal::Adaptor::EglSyncImplementation& EglGraphicsController::GetEglSyncImplementation()
226 DALI_ASSERT_DEBUG(mEglSyncImplementation && "Sync implementation not initialized");
227 return *mEglSyncImplementation;
230 Graphics::UniquePtr<CommandBuffer> EglGraphicsController::CreateCommandBuffer(
231 const CommandBufferCreateInfo& commandBufferCreateInfo,
232 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
234 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
237 Graphics::UniquePtr<RenderPass> EglGraphicsController::CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass)
239 return NewObject<GLES::RenderPass>(renderPassCreateInfo, *this, std::move(oldRenderPass));
242 Graphics::UniquePtr<Texture>
243 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
245 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
248 Graphics::UniquePtr<Buffer> EglGraphicsController::CreateBuffer(
249 const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
251 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
254 Graphics::UniquePtr<Framebuffer> EglGraphicsController::CreateFramebuffer(
255 const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer)
257 return NewObject<GLES::Framebuffer>(framebufferCreateInfo, *this, std::move(oldFramebuffer));
260 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(
261 const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
263 // Create pipeline cache if needed
266 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
269 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
272 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(
273 const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
275 // Create program cache if needed
278 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
281 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
284 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
286 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
289 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
291 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
294 Graphics::UniquePtr<RenderTarget> EglGraphicsController::CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget)
296 return NewObject<GLES::RenderTarget>(renderTargetCreateInfo, *this, std::move(oldRenderTarget));
299 Graphics::UniquePtr<SyncObject> EglGraphicsController::CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
300 UniquePtr<SyncObject>&& oldSyncObject)
302 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
304 return NewObject<EGL::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
308 return NewObject<GLES::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
312 TextureProperties EglGraphicsController::GetTextureProperties(const Texture& texture)
314 const GLES::Texture* glesTexture = static_cast<const GLES::Texture*>(&texture);
315 auto createInfo = glesTexture->GetCreateInfo();
317 TextureProperties properties{};
318 properties.format = createInfo.format;
319 properties.compressed = glesTexture->IsCompressed();
320 properties.extent2D = createInfo.size;
321 properties.nativeHandle = glesTexture->GetGLTexture();
322 // TODO: Skip format1, emulated, packed, directWriteAccessEnabled of TextureProperties for now
327 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
329 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
332 void EglGraphicsController::CreateSurfaceContext(Dali::RenderSurfaceInterface* surface)
334 std::unique_ptr<GLES::Context> context = std::make_unique<GLES::Context>(*this);
335 mSurfaceContexts.push_back(std::move(std::make_pair(surface, std::move(context))));
338 void EglGraphicsController::DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface)
340 mSurfaceContexts.erase(std::remove_if(
341 mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter)
342 { return surface == iter.first; }),
343 mSurfaceContexts.end());
346 void EglGraphicsController::ActivateResourceContext()
348 mCurrentContext = mContext.get();
349 mCurrentContext->GlContextCreated();
352 auto eglGraphics = dynamic_cast<Dali::Internal::Adaptor::EglGraphics*>(mGraphics);
355 mSharedContext = eglGraphics->GetEglImplementation().GetContext();
360 void EglGraphicsController::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
362 if(surface && mGraphics->IsResourceContextSupported())
364 auto iter = std::find_if(mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter)
365 { return (iter.first == surface); });
367 if(iter != mSurfaceContexts.end())
369 mCurrentContext = iter->second.get();
370 mCurrentContext->GlContextCreated();
375 void EglGraphicsController::AddTexture(GLES::Texture& texture)
377 // Assuming we are on the correct context
378 mCreateTextureQueue.push(&texture);
381 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
383 // Assuming we are on the correct context
384 mCreateBufferQueue.push(&buffer);
387 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
389 // Assuming we are on the correct context
390 mCreateFramebufferQueue.push(&framebuffer);
393 void EglGraphicsController::ProcessDiscardQueues()
396 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
399 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
401 // Process Framebuffers
402 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
404 // Process RenderPass
405 ProcessDiscardQueue<GLES::RenderPass>(mDiscardRenderPassQueue);
407 // Process RenderTarget
408 ProcessDiscardQueue<GLES::RenderTarget>(mDiscardRenderTargetQueue);
411 ProcessDiscardQueue(mDiscardPipelineQueue);
414 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
417 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
420 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
422 // Process command buffers
423 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
426 void EglGraphicsController::ProcessCreateQueues()
429 ProcessCreateQueue(mCreateTextureQueue);
432 ProcessCreateQueue(mCreateBufferQueue);
434 // Process framebuffers
435 ProcessCreateQueue(mCreateFramebufferQueue);
438 void EglGraphicsController::ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer)
441 const auto commands = commandBuffer.GetCommands(count);
442 for(auto i = 0u; i < count; ++i)
444 auto& cmd = commands[i];
448 case GLES::CommandType::FLUSH:
450 // Nothing to do here
453 case GLES::CommandType::BIND_TEXTURES:
455 mCurrentContext->BindTextures(cmd.bindTextures.textureBindings.Ptr(), cmd.bindTextures.textureBindingsCount);
458 case GLES::CommandType::BIND_VERTEX_BUFFERS:
460 auto bindings = cmd.bindVertexBuffers.vertexBufferBindings.Ptr();
461 mCurrentContext->BindVertexBuffers(bindings, cmd.bindVertexBuffers.vertexBufferBindingsCount);
464 case GLES::CommandType::BIND_UNIFORM_BUFFER:
466 auto& bindings = cmd.bindUniformBuffers;
467 mCurrentContext->BindUniformBuffers(bindings.uniformBufferBindingsCount ? bindings.uniformBufferBindings.Ptr() : nullptr, bindings.uniformBufferBindingsCount, bindings.standaloneUniformsBufferBinding);
470 case GLES::CommandType::BIND_INDEX_BUFFER:
472 mCurrentContext->BindIndexBuffer(cmd.bindIndexBuffer);
475 case GLES::CommandType::BIND_SAMPLERS:
479 case GLES::CommandType::BIND_PIPELINE:
481 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
482 mCurrentContext->BindPipeline(pipeline);
485 case GLES::CommandType::DRAW:
487 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
490 case GLES::CommandType::DRAW_INDEXED:
492 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
495 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
497 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
500 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
502 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
505 case GLES::CommandType::SET_SCISSOR_TEST:
507 mCurrentContext->SetScissorTestEnabled(cmd.scissorTest.enable);
510 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
512 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
516 case GLES::CommandType::SET_COLOR_MASK:
518 mCurrentContext->ColorMask(cmd.colorMask.enabled);
521 case GLES::CommandType::CLEAR_STENCIL_BUFFER:
523 mCurrentContext->ClearStencilBuffer();
526 case GLES::CommandType::CLEAR_DEPTH_BUFFER:
528 mCurrentContext->ClearDepthBuffer();
532 case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
534 mCurrentContext->SetStencilTestEnable(cmd.stencilTest.enabled);
538 case GLES::CommandType::SET_STENCIL_FUNC:
540 mCurrentContext->StencilFunc(cmd.stencilFunc.compareOp,
541 cmd.stencilFunc.reference,
542 cmd.stencilFunc.compareMask);
546 case GLES::CommandType::SET_STENCIL_WRITE_MASK:
548 mCurrentContext->StencilMask(cmd.stencilWriteMask.mask);
552 case GLES::CommandType::SET_STENCIL_OP:
554 mCurrentContext->StencilOp(cmd.stencilOp.failOp,
555 cmd.stencilOp.depthFailOp,
556 cmd.stencilOp.passOp);
560 case GLES::CommandType::SET_DEPTH_COMPARE_OP:
562 mCurrentContext->SetDepthCompareOp(cmd.depth.compareOp);
565 case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
567 mCurrentContext->SetDepthTestEnable(cmd.depth.testEnabled);
570 case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
572 mCurrentContext->SetDepthWriteEnable(cmd.depth.writeEnabled);
576 case GLES::CommandType::BEGIN_RENDERPASS:
578 auto& renderTarget = *cmd.beginRenderPass.renderTarget;
579 const auto& targetInfo = renderTarget.GetCreateInfo();
581 if(targetInfo.surface)
583 // switch to surface context
584 mGraphics->ActivateSurfaceContext(static_cast<Dali::RenderSurfaceInterface*>(targetInfo.surface));
586 else if(targetInfo.framebuffer)
588 // switch to resource context
589 mGraphics->ActivateResourceContext();
592 mCurrentContext->BeginRenderPass(cmd.beginRenderPass);
596 case GLES::CommandType::END_RENDERPASS:
598 mCurrentContext->EndRenderPass(mTextureDependencyChecker);
600 // This sync object is to enable cpu to wait for rendering to complete, not gpu.
601 // It's only needed for reading the framebuffer texture in the client.
602 auto syncObject = const_cast<GLES::SyncObject*>(static_cast<const GLES::SyncObject*>(cmd.endRenderPass.syncObject));
605 syncObject->InitializeResource();
609 case GLES::CommandType::PRESENT_RENDER_TARGET:
611 ResolvePresentRenderTarget(cmd.presentRenderTarget.targetToPresent);
613 // The command buffer will be pushed into the queue of presentation command buffers
614 // for further reuse.
615 if(commandBuffer.GetCreateInfo().fixedCapacity == 1)
617 mPresentationCommandBuffers.push(&commandBuffer);
621 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
623 // Process secondary command buffers
624 // todo: check validity of the secondaries
625 // there are operations which are illigal to be done
626 // within secondaries.
627 auto buffers = cmd.executeCommandBuffers.buffers.Ptr();
628 for(auto j = 0u; j < cmd.executeCommandBuffers.buffersCount; ++j)
630 auto& buf = buffers[j];
631 ProcessCommandBuffer(*static_cast<const GLES::CommandBuffer*>(buf));
635 case GLES::CommandType::DRAW_NATIVE:
637 auto* info = &cmd.drawNative.drawNativeInfo;
639 mCurrentContext->PrepareForNativeRendering();
641 if(info->glesNativeInfo.eglSharedContextStoragePointer)
643 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
644 *anyContext = mSharedContext;
647 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
649 mCurrentContext->RestoreFromNativeRendering();
656 void EglGraphicsController::ProcessCommandQueues()
660 while(!mCommandQueue.empty())
662 auto cmdBuf = mCommandQueue.front();
664 DUMP_FRAME_COMMAND_BUFFER(cmdBuf);
665 ProcessCommandBuffer(*cmdBuf);
671 void EglGraphicsController::ProcessTextureUpdateQueue()
673 while(!mTextureUpdateRequests.empty())
675 TextureUpdateRequest& request = mTextureUpdateRequests.front();
677 auto& info = request.first;
678 auto& source = request.second;
680 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
682 // GPU memory must be already allocated.
684 // Check if it needs conversion
685 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
686 const auto& createInfo = texture->GetCreateInfo();
687 auto srcFormat = GLES::GLTextureFormatType(info.srcFormat).format;
688 auto srcType = GLES::GLTextureFormatType(info.srcFormat).type;
689 auto destInternalFormat = GLES::GLTextureFormatType(createInfo.format).internalFormat;
690 auto destFormat = GLES::GLTextureFormatType(createInfo.format).format;
692 // From render-texture.cpp
693 const bool isSubImage(info.dstOffset2D.x != 0 || info.dstOffset2D.y != 0 ||
694 info.srcExtent2D.width != (createInfo.size.width / (1 << info.level)) ||
695 info.srcExtent2D.height != (createInfo.size.height / (1 << info.level)));
697 auto* sourceBuffer = reinterpret_cast<uint8_t*>(source.memorySource.memory);
698 auto sourceStride = info.srcStride;
699 std::vector<uint8_t> tempBuffer;
701 if(mGlAbstraction->TextureRequiresConverting(srcFormat, destFormat, isSubImage))
703 // Convert RGB to RGBA if necessary.
704 if(texture->TryConvertPixelData(source.memorySource.memory, info.srcFormat, createInfo.format, info.srcSize, info.srcStride, info.srcExtent2D.width, info.srcExtent2D.height, tempBuffer))
706 sourceBuffer = &tempBuffer[0];
707 sourceStride = 0u; // Converted buffer compacted. make stride as 0.
708 srcFormat = destFormat;
709 srcType = GLES::GLTextureFormatType(createInfo.format).type;
713 // Calculate the maximum mipmap level for the texture
714 texture->SetMaxMipMapLevel(std::max(texture->GetMaxMipMapLevel(), info.level));
716 GLenum bindTarget{GL_TEXTURE_2D};
717 GLenum target{GL_TEXTURE_2D};
719 if(createInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
721 bindTarget = GL_TEXTURE_CUBE_MAP;
722 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + info.layer;
725 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
726 mGlAbstraction->PixelStorei(GL_UNPACK_ROW_LENGTH, sourceStride);
728 mCurrentContext->BindTexture(bindTarget, texture->GetTextureTypeId(), texture->GetGLTexture());
732 if(!texture->IsCompressed())
734 mGlAbstraction->TexImage2D(target,
737 info.srcExtent2D.width,
738 info.srcExtent2D.height,
746 mGlAbstraction->CompressedTexImage2D(target,
749 info.srcExtent2D.width,
750 info.srcExtent2D.height,
758 if(!texture->IsCompressed())
760 mGlAbstraction->TexSubImage2D(target,
764 info.srcExtent2D.width,
765 info.srcExtent2D.height,
772 mGlAbstraction->CompressedTexSubImage2D(target,
776 info.srcExtent2D.width,
777 info.srcExtent2D.height,
783 // free staging memory
784 free(source.memorySource.memory);
788 // TODO: other sources
791 mTextureUpdateRequests.pop();
795 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
796 const std::vector<TextureUpdateSourceInfo>& sourceList)
799 for(auto& info : updateInfoList)
801 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
802 auto& pair = mTextureUpdateRequests.back();
803 switch(pair.second.sourceType)
805 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
807 auto& info = pair.first;
808 auto& source = pair.second;
810 // allocate staging memory and copy the data
811 // TODO: using PBO with GLES3, this is just naive
814 uint8_t* stagingBuffer = reinterpret_cast<uint8_t*>(malloc(info.srcSize));
816 uint8_t* srcMemory = &reinterpret_cast<uint8_t*>(source.memorySource.memory)[info.srcOffset];
818 std::copy(srcMemory, srcMemory + info.srcSize, stagingBuffer);
820 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
822 // store staging buffer
823 source.memorySource.memory = stagingBuffer;
826 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
828 // TODO, with PBO support
831 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
833 // TODO texture 2 texture in-GPU copy
839 // If upload buffer exceeds maximum size, flush.
840 if(mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB * 1024 * 1024)
843 mTextureUploadTotalCPUMemoryUsed = 0;
847 void EglGraphicsController::ProcessTextureMipmapGenerationQueue()
849 while(!mTextureMipmapGenerationRequests.empty())
851 auto* texture = mTextureMipmapGenerationRequests.front();
853 mCurrentContext->BindTexture(texture->GetGlTarget(), texture->GetTextureTypeId(), texture->GetGLTexture());
854 mCurrentContext->GenerateMipmap(texture->GetGlTarget());
856 mTextureMipmapGenerationRequests.pop();
860 void EglGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
862 mTextureMipmapGenerationRequests.push(static_cast<const GLES::Texture*>(&texture));
865 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
867 mGraphics->ActivateResourceContext();
869 // Mapping buffer requires the object to be created NOW
870 // Workaround - flush now, otherwise there will be given a staging buffer
871 // in case when the buffer is not there yet
872 ProcessCreateQueues();
874 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
876 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
880 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
884 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
886 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
889 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
891 return *mPipelineCache;
894 } // namespace Dali::Graphics