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);
319 mSharedContext = eglGraphics->GetEglImplementation().GetContext();
323 void EglGraphicsController::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
325 if(surface && mGraphics->IsResourceContextSupported())
327 auto iter = std::find_if(mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) {
328 return (iter.first == surface);
331 if(iter != mSurfaceContexts.end())
333 mCurrentContext = iter->second.get();
334 mCurrentContext->GlContextCreated();
339 void EglGraphicsController::AddTexture(GLES::Texture& texture)
341 // Assuming we are on the correct context
342 mCreateTextureQueue.push(&texture);
345 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
347 // Assuming we are on the correct context
348 mCreateBufferQueue.push(&buffer);
351 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
353 // Assuming we are on the correct context
354 mCreateFramebufferQueue.push(&framebuffer);
357 void EglGraphicsController::ProcessDiscardQueues()
360 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
363 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
365 // Process Framebuffers
366 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
368 // Process RenderPass
369 ProcessDiscardQueue<GLES::RenderPass>(mDiscardRenderPassQueue);
371 // Process RenderTarget
372 ProcessDiscardQueue<GLES::RenderTarget>(mDiscardRenderTargetQueue);
375 ProcessDiscardQueue(mDiscardPipelineQueue);
378 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
381 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
384 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
386 // Process command buffers
387 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
390 void EglGraphicsController::ProcessCreateQueues()
393 ProcessCreateQueue(mCreateTextureQueue);
396 ProcessCreateQueue(mCreateBufferQueue);
398 // Process framebuffers
399 ProcessCreateQueue(mCreateFramebufferQueue);
402 void EglGraphicsController::ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer)
405 const auto commands = commandBuffer.GetCommands(count);
406 for(auto i = 0u; i < count; ++i)
408 auto& cmd = commands[i];
412 case GLES::CommandType::FLUSH:
414 // Nothing to do here
417 case GLES::CommandType::BIND_TEXTURES:
419 mCurrentContext->BindTextures(cmd.bindTextures.textureBindings.Ptr(), cmd.bindTextures.textureBindingsCount);
422 case GLES::CommandType::BIND_VERTEX_BUFFERS:
424 auto bindings = cmd.bindVertexBuffers.vertexBufferBindings.Ptr();
425 mCurrentContext->BindVertexBuffers(bindings, cmd.bindVertexBuffers.vertexBufferBindingsCount);
428 case GLES::CommandType::BIND_UNIFORM_BUFFER:
430 auto& bindings = cmd.bindUniformBuffers;
431 mCurrentContext->BindUniformBuffers(bindings.uniformBufferBindingsCount ? bindings.uniformBufferBindings.Ptr() : nullptr, bindings.uniformBufferBindingsCount, bindings.standaloneUniformsBufferBinding);
434 case GLES::CommandType::BIND_INDEX_BUFFER:
436 mCurrentContext->BindIndexBuffer(cmd.bindIndexBuffer);
439 case GLES::CommandType::BIND_SAMPLERS:
443 case GLES::CommandType::BIND_PIPELINE:
445 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
446 mCurrentContext->BindPipeline(pipeline);
449 case GLES::CommandType::DRAW:
451 mCurrentContext->Flush(false, cmd.draw);
454 case GLES::CommandType::DRAW_INDEXED:
456 mCurrentContext->Flush(false, cmd.draw);
459 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
461 mCurrentContext->Flush(false, cmd.draw);
464 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
466 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
469 case GLES::CommandType::SET_SCISSOR_TEST:
471 mCurrentContext->SetScissorTestEnabled(cmd.scissorTest.enable);
474 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
476 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
480 case GLES::CommandType::SET_COLOR_MASK:
482 mCurrentContext->ColorMask(cmd.colorMask.enabled);
485 case GLES::CommandType::CLEAR_STENCIL_BUFFER:
487 mCurrentContext->ClearStencilBuffer();
490 case GLES::CommandType::CLEAR_DEPTH_BUFFER:
492 mCurrentContext->ClearDepthBuffer();
496 case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
498 mCurrentContext->SetStencilTestEnable(cmd.stencilTest.enabled);
502 case GLES::CommandType::SET_STENCIL_FUNC:
504 mCurrentContext->StencilFunc(cmd.stencilFunc.compareOp,
505 cmd.stencilFunc.reference,
506 cmd.stencilFunc.compareMask);
510 case GLES::CommandType::SET_STENCIL_WRITE_MASK:
512 mCurrentContext->StencilMask(cmd.stencilWriteMask.mask);
516 case GLES::CommandType::SET_STENCIL_OP:
518 mCurrentContext->StencilOp(cmd.stencilOp.failOp,
519 cmd.stencilOp.depthFailOp,
520 cmd.stencilOp.passOp);
524 case GLES::CommandType::SET_DEPTH_COMPARE_OP:
526 mCurrentContext->SetDepthCompareOp(cmd.depth.compareOp);
529 case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
531 mCurrentContext->SetDepthTestEnable(cmd.depth.testEnabled);
534 case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
536 mCurrentContext->SetDepthWriteEnable(cmd.depth.writeEnabled);
540 case GLES::CommandType::BEGIN_RENDERPASS:
542 auto& renderTarget = *cmd.beginRenderPass.renderTarget;
543 const auto& targetInfo = renderTarget.GetCreateInfo();
545 if(targetInfo.surface)
547 // switch to surface context
548 mGraphics->ActivateSurfaceContext(static_cast<Dali::RenderSurfaceInterface*>(targetInfo.surface));
550 else if(targetInfo.framebuffer)
552 // switch to resource context
553 mGraphics->ActivateResourceContext();
556 mCurrentContext->BeginRenderPass(cmd.beginRenderPass);
559 case GLES::CommandType::END_RENDERPASS:
561 mCurrentContext->EndRenderPass();
563 auto syncObject = const_cast<GLES::SyncObject*>(static_cast<const GLES::SyncObject*>(cmd.endRenderPass.syncObject));
566 syncObject->InitializeResource();
570 case GLES::CommandType::PRESENT_RENDER_TARGET:
572 ResolvePresentRenderTarget(cmd.presentRenderTarget.targetToPresent);
574 // The command buffer will be pushed into the queue of presentation command buffers
575 // for further reuse.
576 if(commandBuffer.GetCreateInfo().fixedCapacity == 1)
578 mPresentationCommandBuffers.push(&commandBuffer);
582 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
584 // Process secondary command buffers
585 // todo: check validity of the secondaries
586 // there are operations which are illigal to be done
587 // within secondaries.
588 auto buffers = cmd.executeCommandBuffers.buffers.Ptr();
589 for(auto j = 0u; j < cmd.executeCommandBuffers.buffersCount; ++j)
591 auto& buf = buffers[j];
592 ProcessCommandBuffer(*static_cast<const GLES::CommandBuffer*>(buf));
596 case GLES::CommandType::DRAW_NATIVE:
598 auto* info = &cmd.drawNative.drawNativeInfo;
600 mCurrentContext->PrepareForNativeRendering();
602 if(info->glesNativeInfo.eglSharedContextStoragePointer)
604 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
605 *anyContext = mSharedContext;
608 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
610 mCurrentContext->RestoreFromNativeRendering();
617 void EglGraphicsController::ProcessCommandQueues()
621 while(!mCommandQueue.empty())
623 auto cmdBuf = mCommandQueue.front();
625 DUMP_FRAME_COMMAND_BUFFER(cmdBuf);
626 ProcessCommandBuffer(*cmdBuf);
632 void EglGraphicsController::ProcessTextureUpdateQueue()
634 while(!mTextureUpdateRequests.empty())
636 TextureUpdateRequest& request = mTextureUpdateRequests.front();
638 auto& info = request.first;
639 auto& source = request.second;
641 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
643 // GPU memory must be already allocated.
645 // Check if it needs conversion
646 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
647 const auto& createInfo = texture->GetCreateInfo();
648 auto srcFormat = GLES::GLTextureFormatType(info.srcFormat).format;
649 auto srcType = GLES::GLTextureFormatType(info.srcFormat).type;
650 auto destInternalFormat = GLES::GLTextureFormatType(createInfo.format).internalFormat;
651 auto destFormat = GLES::GLTextureFormatType(createInfo.format).format;
653 // From render-texture.cpp
654 const bool isSubImage(info.dstOffset2D.x != 0 || info.dstOffset2D.y != 0 ||
655 info.srcExtent2D.width != (createInfo.size.width / (1 << info.level)) ||
656 info.srcExtent2D.height != (createInfo.size.height / (1 << info.level)));
658 auto* sourceBuffer = reinterpret_cast<uint8_t*>(source.memorySource.memory);
659 std::vector<uint8_t> tempBuffer;
660 if(mGlAbstraction->TextureRequiresConverting(srcFormat, destFormat, isSubImage))
662 // Convert RGB to RGBA if necessary.
663 texture->TryConvertPixelData(source.memorySource.memory, info.srcFormat, createInfo.format, info.srcSize, info.srcExtent2D.width, info.srcExtent2D.height, tempBuffer);
664 sourceBuffer = &tempBuffer[0];
665 srcFormat = destFormat;
666 srcType = GLES::GLTextureFormatType(createInfo.format).type;
669 // Calculate the maximum mipmap level for the texture
670 texture->SetMaxMipMapLevel(std::max(texture->GetMaxMipMapLevel(), info.level));
672 GLenum bindTarget{GL_TEXTURE_2D};
673 GLenum target{GL_TEXTURE_2D};
675 if(createInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
677 bindTarget = GL_TEXTURE_CUBE_MAP;
678 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + info.layer;
681 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
682 mGlAbstraction->PixelStorei(GL_UNPACK_ROW_LENGTH, info.srcStride);
684 mCurrentContext->BindTexture(bindTarget, texture->GetTextureTypeId(), texture->GetGLTexture());
688 if(!texture->IsCompressed())
690 mGlAbstraction->TexImage2D(target,
693 info.srcExtent2D.width,
694 info.srcExtent2D.height,
702 mGlAbstraction->CompressedTexImage2D(target,
705 info.srcExtent2D.width,
706 info.srcExtent2D.height,
714 if(!texture->IsCompressed())
716 mGlAbstraction->TexSubImage2D(target,
720 info.srcExtent2D.width,
721 info.srcExtent2D.height,
728 mGlAbstraction->CompressedTexSubImage2D(target,
732 info.srcExtent2D.width,
733 info.srcExtent2D.height,
739 // free staging memory
740 free(source.memorySource.memory);
744 // TODO: other sources
747 mTextureUpdateRequests.pop();
751 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
752 const std::vector<TextureUpdateSourceInfo>& sourceList)
755 for(auto& info : updateInfoList)
757 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
758 auto& pair = mTextureUpdateRequests.back();
759 switch(pair.second.sourceType)
761 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
763 auto& info = pair.first;
764 auto& source = pair.second;
766 // allocate staging memory and copy the data
767 // TODO: using PBO with GLES3, this is just naive
770 char* stagingBuffer = reinterpret_cast<char*>(malloc(info.srcSize));
771 std::copy(&reinterpret_cast<char*>(source.memorySource.memory)[info.srcOffset],
772 reinterpret_cast<char*>(source.memorySource.memory) + info.srcSize,
775 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
777 // store staging buffer
778 source.memorySource.memory = stagingBuffer;
781 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
783 // TODO, with PBO support
786 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
788 // TODO texture 2 texture in-GPU copy
794 // If upload buffer exceeds maximum size, flush.
795 if(mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB * 1024 * 1024)
798 mTextureUploadTotalCPUMemoryUsed = 0;
802 void EglGraphicsController::ProcessTextureMipmapGenerationQueue()
804 while(!mTextureMipmapGenerationRequests.empty())
806 auto* texture = mTextureMipmapGenerationRequests.front();
808 mCurrentContext->BindTexture(texture->GetGlTarget(), texture->GetTextureTypeId(), texture->GetGLTexture());
809 mCurrentContext->GenerateMipmap(texture->GetGlTarget());
811 mTextureMipmapGenerationRequests.pop();
815 void EglGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
817 mTextureMipmapGenerationRequests.push(static_cast<const GLES::Texture*>(&texture));
820 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
822 mGraphics->ActivateResourceContext();
824 // Mapping buffer requires the object to be created NOW
825 // Workaround - flush now, otherwise there will be given a staging buffer
826 // in case when the buffer is not there yet
827 ProcessCreateQueues();
829 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
831 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
835 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
839 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
841 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
844 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
846 return *mPipelineCache;
849 } // namespace Dali::Graphics