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()
114 while(!mPresentationCommandBuffers.empty())
116 auto presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
117 delete presentCommandBuffer;
118 mPresentationCommandBuffers.pop();
122 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
124 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #1\n");
125 mGlAbstraction = &glAbstraction;
126 mContext = std::make_unique<GLES::Context>(*this);
127 mCurrentContext = mContext.get();
130 void EglGraphicsController::Initialize(Integration::GraphicsSyncAbstraction& syncImplementation,
131 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
132 Internal::Adaptor::GraphicsInterface& graphicsInterface)
134 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #2\n");
135 auto* syncImplPtr = static_cast<Internal::Adaptor::EglSyncImplementation*>(&syncImplementation);
137 mEglSyncImplementation = syncImplPtr;
138 mGlContextHelperAbstraction = &glContextHelperAbstraction;
139 mGraphics = &graphicsInterface;
142 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
144 for(auto& cmdbuf : submitInfo.cmdBuffer)
146 // Push command buffers
147 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
150 // If flush bit set, flush all pending tasks
151 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
157 void EglGraphicsController::PresentRenderTarget(RenderTarget* renderTarget)
159 GLES::CommandBuffer* presentCommandBuffer{nullptr};
160 if(mPresentationCommandBuffers.empty())
162 CommandBufferCreateInfo info;
163 info.SetLevel(CommandBufferLevel::PRIMARY);
164 info.fixedCapacity = 1; // only one command
165 presentCommandBuffer = new GLES::CommandBuffer(info, *this);
169 presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
170 presentCommandBuffer->Reset();
171 mPresentationCommandBuffers.pop();
173 presentCommandBuffer->PresentRenderTarget(static_cast<GLES::RenderTarget*>(renderTarget));
174 SubmitInfo submitInfo;
175 submitInfo.cmdBuffer = {presentCommandBuffer};
176 submitInfo.flags = 0 | SubmitFlagBits::FLUSH;
177 SubmitCommandBuffers(submitInfo);
180 void EglGraphicsController::ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget)
182 mCurrentContext->InvalidateDepthStencilBuffers();
184 auto* rt = static_cast<GLES::RenderTarget*>(renderTarget);
185 if(rt->GetCreateInfo().surface)
187 auto* surfaceInterface = reinterpret_cast<Dali::RenderSurfaceInterface*>(rt->GetCreateInfo().surface);
188 surfaceInterface->MakeContextCurrent();
189 surfaceInterface->PostRender();
193 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
195 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
196 return *mGlAbstraction;
199 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
201 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
202 return *mGlContextHelperAbstraction;
205 Internal::Adaptor::EglSyncImplementation& EglGraphicsController::GetEglSyncImplementation()
207 DALI_ASSERT_DEBUG(mEglSyncImplementation && "Sync implementation not initialized");
208 return *mEglSyncImplementation;
211 Graphics::UniquePtr<CommandBuffer> EglGraphicsController::CreateCommandBuffer(
212 const CommandBufferCreateInfo& commandBufferCreateInfo,
213 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
215 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
218 Graphics::UniquePtr<RenderPass> EglGraphicsController::CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass)
220 return NewObject<GLES::RenderPass>(renderPassCreateInfo, *this, std::move(oldRenderPass));
223 Graphics::UniquePtr<Texture>
224 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
226 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
229 Graphics::UniquePtr<Buffer> EglGraphicsController::CreateBuffer(
230 const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
232 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
235 Graphics::UniquePtr<Framebuffer> EglGraphicsController::CreateFramebuffer(
236 const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer)
238 return NewObject<GLES::Framebuffer>(framebufferCreateInfo, *this, std::move(oldFramebuffer));
241 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(
242 const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
244 // Create pipeline cache if needed
247 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
250 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
253 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(
254 const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
256 // Create program cache if needed
259 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
262 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
265 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
267 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
270 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
272 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
275 Graphics::UniquePtr<RenderTarget> EglGraphicsController::CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget)
277 return NewObject<GLES::RenderTarget>(renderTargetCreateInfo, *this, std::move(oldRenderTarget));
280 Graphics::UniquePtr<SyncObject> EglGraphicsController::CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
281 UniquePtr<SyncObject>&& oldSyncObject)
283 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
285 return NewObject<EGL::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
289 return NewObject<GLES::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
293 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
295 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
298 void EglGraphicsController::CreateSurfaceContext(Dali::RenderSurfaceInterface* surface)
300 std::unique_ptr<GLES::Context> context = std::make_unique<GLES::Context>(*this);
301 mSurfaceContexts.push_back(std::move(std::make_pair(surface, std::move(context))));
304 void EglGraphicsController::DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface)
306 mSurfaceContexts.erase(std::remove_if(
307 mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) { return surface == iter.first; }),
308 mSurfaceContexts.end());
311 void EglGraphicsController::ActivateResourceContext()
313 mCurrentContext = mContext.get();
314 mCurrentContext->GlContextCreated();
318 auto eglGraphics = dynamic_cast<Dali::Internal::Adaptor::EglGraphics*>(mGraphics);
321 mSharedContext = eglGraphics->GetEglImplementation().GetContext();
326 void EglGraphicsController::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
328 if(surface && mGraphics->IsResourceContextSupported())
330 auto iter = std::find_if(mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) {
331 return (iter.first == surface);
334 if(iter != mSurfaceContexts.end())
336 mCurrentContext = iter->second.get();
337 mCurrentContext->GlContextCreated();
342 void EglGraphicsController::AddTexture(GLES::Texture& texture)
344 // Assuming we are on the correct context
345 mCreateTextureQueue.push(&texture);
348 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
350 // Assuming we are on the correct context
351 mCreateBufferQueue.push(&buffer);
354 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
356 // Assuming we are on the correct context
357 mCreateFramebufferQueue.push(&framebuffer);
360 void EglGraphicsController::ProcessDiscardQueues()
363 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
366 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
368 // Process Framebuffers
369 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
371 // Process RenderPass
372 ProcessDiscardQueue<GLES::RenderPass>(mDiscardRenderPassQueue);
374 // Process RenderTarget
375 ProcessDiscardQueue<GLES::RenderTarget>(mDiscardRenderTargetQueue);
378 ProcessDiscardQueue(mDiscardPipelineQueue);
381 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
384 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
387 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
389 // Process command buffers
390 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
393 void EglGraphicsController::ProcessCreateQueues()
396 ProcessCreateQueue(mCreateTextureQueue);
399 ProcessCreateQueue(mCreateBufferQueue);
401 // Process framebuffers
402 ProcessCreateQueue(mCreateFramebufferQueue);
405 void EglGraphicsController::ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer)
408 const auto commands = commandBuffer.GetCommands(count);
409 for(auto i = 0u; i < count; ++i)
411 auto& cmd = commands[i];
415 case GLES::CommandType::FLUSH:
417 // Nothing to do here
420 case GLES::CommandType::BIND_TEXTURES:
422 mCurrentContext->BindTextures(cmd.bindTextures.textureBindings.Ptr(), cmd.bindTextures.textureBindingsCount);
425 case GLES::CommandType::BIND_VERTEX_BUFFERS:
427 auto bindings = cmd.bindVertexBuffers.vertexBufferBindings.Ptr();
428 mCurrentContext->BindVertexBuffers(bindings, cmd.bindVertexBuffers.vertexBufferBindingsCount);
431 case GLES::CommandType::BIND_UNIFORM_BUFFER:
433 auto& bindings = cmd.bindUniformBuffers;
434 mCurrentContext->BindUniformBuffers(bindings.uniformBufferBindingsCount ? bindings.uniformBufferBindings.Ptr() : nullptr, bindings.uniformBufferBindingsCount, bindings.standaloneUniformsBufferBinding);
437 case GLES::CommandType::BIND_INDEX_BUFFER:
439 mCurrentContext->BindIndexBuffer(cmd.bindIndexBuffer);
442 case GLES::CommandType::BIND_SAMPLERS:
446 case GLES::CommandType::BIND_PIPELINE:
448 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
449 mCurrentContext->BindPipeline(pipeline);
452 case GLES::CommandType::DRAW:
454 mCurrentContext->Flush(false, cmd.draw);
457 case GLES::CommandType::DRAW_INDEXED:
459 mCurrentContext->Flush(false, cmd.draw);
462 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
464 mCurrentContext->Flush(false, cmd.draw);
467 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
469 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
472 case GLES::CommandType::SET_SCISSOR_TEST:
474 mCurrentContext->SetScissorTestEnabled(cmd.scissorTest.enable);
477 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
479 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
483 case GLES::CommandType::SET_COLOR_MASK:
485 mCurrentContext->ColorMask(cmd.colorMask.enabled);
488 case GLES::CommandType::CLEAR_STENCIL_BUFFER:
490 mCurrentContext->ClearStencilBuffer();
493 case GLES::CommandType::CLEAR_DEPTH_BUFFER:
495 mCurrentContext->ClearDepthBuffer();
499 case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
501 mCurrentContext->SetStencilTestEnable(cmd.stencilTest.enabled);
505 case GLES::CommandType::SET_STENCIL_FUNC:
507 mCurrentContext->StencilFunc(cmd.stencilFunc.compareOp,
508 cmd.stencilFunc.reference,
509 cmd.stencilFunc.compareMask);
513 case GLES::CommandType::SET_STENCIL_WRITE_MASK:
515 mCurrentContext->StencilMask(cmd.stencilWriteMask.mask);
519 case GLES::CommandType::SET_STENCIL_OP:
521 mCurrentContext->StencilOp(cmd.stencilOp.failOp,
522 cmd.stencilOp.depthFailOp,
523 cmd.stencilOp.passOp);
527 case GLES::CommandType::SET_DEPTH_COMPARE_OP:
529 mCurrentContext->SetDepthCompareOp(cmd.depth.compareOp);
532 case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
534 mCurrentContext->SetDepthTestEnable(cmd.depth.testEnabled);
537 case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
539 mCurrentContext->SetDepthWriteEnable(cmd.depth.writeEnabled);
543 case GLES::CommandType::BEGIN_RENDERPASS:
545 auto& renderTarget = *cmd.beginRenderPass.renderTarget;
546 const auto& targetInfo = renderTarget.GetCreateInfo();
548 if(targetInfo.surface)
550 // switch to surface context
551 mGraphics->ActivateSurfaceContext(static_cast<Dali::RenderSurfaceInterface*>(targetInfo.surface));
553 else if(targetInfo.framebuffer)
555 // switch to resource context
556 mGraphics->ActivateResourceContext();
559 mCurrentContext->BeginRenderPass(cmd.beginRenderPass);
562 case GLES::CommandType::END_RENDERPASS:
564 mCurrentContext->EndRenderPass();
566 auto syncObject = const_cast<GLES::SyncObject*>(static_cast<const GLES::SyncObject*>(cmd.endRenderPass.syncObject));
569 syncObject->InitializeResource();
573 case GLES::CommandType::PRESENT_RENDER_TARGET:
575 ResolvePresentRenderTarget(cmd.presentRenderTarget.targetToPresent);
577 // The command buffer will be pushed into the queue of presentation command buffers
578 // for further reuse.
579 if(commandBuffer.GetCreateInfo().fixedCapacity == 1)
581 mPresentationCommandBuffers.push(&commandBuffer);
585 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
587 // Process secondary command buffers
588 // todo: check validity of the secondaries
589 // there are operations which are illigal to be done
590 // within secondaries.
591 auto buffers = cmd.executeCommandBuffers.buffers.Ptr();
592 for(auto j = 0u; j < cmd.executeCommandBuffers.buffersCount; ++j)
594 auto& buf = buffers[j];
595 ProcessCommandBuffer(*static_cast<const GLES::CommandBuffer*>(buf));
599 case GLES::CommandType::DRAW_NATIVE:
601 auto* info = &cmd.drawNative.drawNativeInfo;
603 mCurrentContext->PrepareForNativeRendering();
605 if(info->glesNativeInfo.eglSharedContextStoragePointer)
607 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
608 *anyContext = mSharedContext;
611 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
613 mCurrentContext->RestoreFromNativeRendering();
620 void EglGraphicsController::ProcessCommandQueues()
624 while(!mCommandQueue.empty())
626 auto cmdBuf = mCommandQueue.front();
628 DUMP_FRAME_COMMAND_BUFFER(cmdBuf);
629 ProcessCommandBuffer(*cmdBuf);
635 void EglGraphicsController::ProcessTextureUpdateQueue()
637 while(!mTextureUpdateRequests.empty())
639 TextureUpdateRequest& request = mTextureUpdateRequests.front();
641 auto& info = request.first;
642 auto& source = request.second;
644 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
646 // GPU memory must be already allocated.
648 // Check if it needs conversion
649 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
650 const auto& createInfo = texture->GetCreateInfo();
651 auto srcFormat = GLES::GLTextureFormatType(info.srcFormat).format;
652 auto srcType = GLES::GLTextureFormatType(info.srcFormat).type;
653 auto destInternalFormat = GLES::GLTextureFormatType(createInfo.format).internalFormat;
654 auto destFormat = GLES::GLTextureFormatType(createInfo.format).format;
656 // From render-texture.cpp
657 const bool isSubImage(info.dstOffset2D.x != 0 || info.dstOffset2D.y != 0 ||
658 info.srcExtent2D.width != (createInfo.size.width / (1 << info.level)) ||
659 info.srcExtent2D.height != (createInfo.size.height / (1 << info.level)));
661 auto* sourceBuffer = reinterpret_cast<uint8_t*>(source.memorySource.memory);
662 auto sourceStride = info.srcStride;
663 std::vector<uint8_t> tempBuffer;
665 if(mGlAbstraction->TextureRequiresConverting(srcFormat, destFormat, isSubImage))
667 // Convert RGB to RGBA if necessary.
668 if(texture->TryConvertPixelData(source.memorySource.memory, info.srcFormat, createInfo.format, info.srcSize, info.srcStride, info.srcExtent2D.width, info.srcExtent2D.height, tempBuffer))
670 sourceBuffer = &tempBuffer[0];
671 sourceStride = 0u; // Converted buffer compacted. make stride as 0.
672 srcFormat = destFormat;
673 srcType = GLES::GLTextureFormatType(createInfo.format).type;
677 // Calculate the maximum mipmap level for the texture
678 texture->SetMaxMipMapLevel(std::max(texture->GetMaxMipMapLevel(), info.level));
680 GLenum bindTarget{GL_TEXTURE_2D};
681 GLenum target{GL_TEXTURE_2D};
683 if(createInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
685 bindTarget = GL_TEXTURE_CUBE_MAP;
686 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + info.layer;
689 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
690 mGlAbstraction->PixelStorei(GL_UNPACK_ROW_LENGTH, sourceStride);
692 mCurrentContext->BindTexture(bindTarget, texture->GetTextureTypeId(), texture->GetGLTexture());
696 if(!texture->IsCompressed())
698 mGlAbstraction->TexImage2D(target,
701 info.srcExtent2D.width,
702 info.srcExtent2D.height,
710 mGlAbstraction->CompressedTexImage2D(target,
713 info.srcExtent2D.width,
714 info.srcExtent2D.height,
722 if(!texture->IsCompressed())
724 mGlAbstraction->TexSubImage2D(target,
728 info.srcExtent2D.width,
729 info.srcExtent2D.height,
736 mGlAbstraction->CompressedTexSubImage2D(target,
740 info.srcExtent2D.width,
741 info.srcExtent2D.height,
747 // free staging memory
748 free(source.memorySource.memory);
752 // TODO: other sources
755 mTextureUpdateRequests.pop();
759 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
760 const std::vector<TextureUpdateSourceInfo>& sourceList)
763 for(auto& info : updateInfoList)
765 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
766 auto& pair = mTextureUpdateRequests.back();
767 switch(pair.second.sourceType)
769 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
771 auto& info = pair.first;
772 auto& source = pair.second;
774 // allocate staging memory and copy the data
775 // TODO: using PBO with GLES3, this is just naive
778 uint8_t* stagingBuffer = reinterpret_cast<uint8_t*>(malloc(info.srcSize));
780 uint8_t* srcMemory = &reinterpret_cast<uint8_t*>(source.memorySource.memory)[info.srcOffset];
782 std::copy(srcMemory, srcMemory + info.srcSize, stagingBuffer);
784 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
786 // store staging buffer
787 source.memorySource.memory = stagingBuffer;
790 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
792 // TODO, with PBO support
795 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
797 // TODO texture 2 texture in-GPU copy
803 // If upload buffer exceeds maximum size, flush.
804 if(mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB * 1024 * 1024)
807 mTextureUploadTotalCPUMemoryUsed = 0;
811 void EglGraphicsController::ProcessTextureMipmapGenerationQueue()
813 while(!mTextureMipmapGenerationRequests.empty())
815 auto* texture = mTextureMipmapGenerationRequests.front();
817 mCurrentContext->BindTexture(texture->GetGlTarget(), texture->GetTextureTypeId(), texture->GetGLTexture());
818 mCurrentContext->GenerateMipmap(texture->GetGlTarget());
820 mTextureMipmapGenerationRequests.pop();
824 void EglGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
826 mTextureMipmapGenerationRequests.push(static_cast<const GLES::Texture*>(&texture));
829 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
831 mGraphics->ActivateResourceContext();
833 // Mapping buffer requires the object to be created NOW
834 // Workaround - flush now, otherwise there will be given a staging buffer
835 // in case when the buffer is not there yet
836 ProcessCreateQueues();
838 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
840 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
844 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
848 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
850 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
853 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
855 return *mPipelineCache;
858 } // namespace Dali::Graphics