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 New Graphics Controller #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 New Graphics Controller #2\n");
141 auto* syncImplPtr = static_cast<Internal::Adaptor::EglSyncImplementation*>(&syncImplementation);
143 mEglSyncImplementation = syncImplPtr;
144 mGlContextHelperAbstraction = &glContextHelperAbstraction;
145 mGraphics = &graphicsInterface;
148 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
150 for(auto& cmdbuf : submitInfo.cmdBuffer)
152 // Push command buffers
153 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
156 // If flush bit set, flush all pending tasks
157 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
163 void EglGraphicsController::PresentRenderTarget(RenderTarget* renderTarget)
165 GLES::CommandBuffer* presentCommandBuffer{nullptr};
166 if(mPresentationCommandBuffers.empty())
168 CommandBufferCreateInfo info;
169 info.SetLevel(CommandBufferLevel::PRIMARY);
170 info.fixedCapacity = 1; // only one command
171 presentCommandBuffer = new GLES::CommandBuffer(info, *this);
175 presentCommandBuffer = const_cast<GLES::CommandBuffer*>(mPresentationCommandBuffers.front());
176 presentCommandBuffer->Reset();
177 mPresentationCommandBuffers.pop();
179 presentCommandBuffer->PresentRenderTarget(static_cast<GLES::RenderTarget*>(renderTarget));
180 SubmitInfo submitInfo;
181 submitInfo.cmdBuffer = {presentCommandBuffer};
182 submitInfo.flags = 0 | SubmitFlagBits::FLUSH;
183 SubmitCommandBuffers(submitInfo);
186 void EglGraphicsController::ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget)
188 mCurrentContext->InvalidateDepthStencilBuffers();
190 auto* rt = static_cast<GLES::RenderTarget*>(renderTarget);
191 if(rt->GetCreateInfo().surface)
193 auto* surfaceInterface = reinterpret_cast<Dali::RenderSurfaceInterface*>(rt->GetCreateInfo().surface);
194 surfaceInterface->MakeContextCurrent();
195 surfaceInterface->PostRender();
199 void EglGraphicsController::PostRender()
201 mTextureDependencyChecker.Reset();
202 mSyncPool.AgeSyncObjects();
205 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
207 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
208 return *mGlAbstraction;
211 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
213 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
214 return *mGlContextHelperAbstraction;
217 Internal::Adaptor::EglSyncImplementation& EglGraphicsController::GetEglSyncImplementation()
219 DALI_ASSERT_DEBUG(mEglSyncImplementation && "Sync implementation not initialized");
220 return *mEglSyncImplementation;
223 Graphics::UniquePtr<CommandBuffer> EglGraphicsController::CreateCommandBuffer(
224 const CommandBufferCreateInfo& commandBufferCreateInfo,
225 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
227 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
230 Graphics::UniquePtr<RenderPass> EglGraphicsController::CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass)
232 return NewObject<GLES::RenderPass>(renderPassCreateInfo, *this, std::move(oldRenderPass));
235 Graphics::UniquePtr<Texture>
236 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
238 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
241 Graphics::UniquePtr<Buffer> EglGraphicsController::CreateBuffer(
242 const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
244 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
247 Graphics::UniquePtr<Framebuffer> EglGraphicsController::CreateFramebuffer(
248 const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer)
250 return NewObject<GLES::Framebuffer>(framebufferCreateInfo, *this, std::move(oldFramebuffer));
253 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(
254 const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
256 // Create pipeline cache if needed
259 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
262 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
265 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(
266 const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
268 // Create program cache if needed
271 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
274 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
277 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
279 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
282 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
284 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
287 Graphics::UniquePtr<RenderTarget> EglGraphicsController::CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget)
289 return NewObject<GLES::RenderTarget>(renderTargetCreateInfo, *this, std::move(oldRenderTarget));
292 Graphics::UniquePtr<SyncObject> EglGraphicsController::CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
293 UniquePtr<SyncObject>&& oldSyncObject)
295 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
297 return NewObject<EGL::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
301 return NewObject<GLES::SyncObject>(syncObjectCreateInfo, *this, std::move(oldSyncObject));
305 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
307 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
310 void EglGraphicsController::CreateSurfaceContext(Dali::RenderSurfaceInterface* surface)
312 std::unique_ptr<GLES::Context> context = std::make_unique<GLES::Context>(*this);
313 mSurfaceContexts.push_back(std::move(std::make_pair(surface, std::move(context))));
316 void EglGraphicsController::DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface)
318 mSurfaceContexts.erase(std::remove_if(
319 mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) { return surface == iter.first; }),
320 mSurfaceContexts.end());
323 void EglGraphicsController::ActivateResourceContext()
325 mCurrentContext = mContext.get();
326 mCurrentContext->GlContextCreated();
330 auto eglGraphics = dynamic_cast<Dali::Internal::Adaptor::EglGraphics*>(mGraphics);
333 mSharedContext = eglGraphics->GetEglImplementation().GetContext();
338 void EglGraphicsController::ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface)
340 if(surface && mGraphics->IsResourceContextSupported())
342 auto iter = std::find_if(mSurfaceContexts.begin(), mSurfaceContexts.end(), [surface](SurfaceContextPair& iter) {
343 return (iter.first == surface);
346 if(iter != mSurfaceContexts.end())
348 mCurrentContext = iter->second.get();
349 mCurrentContext->GlContextCreated();
354 void EglGraphicsController::AddTexture(GLES::Texture& texture)
356 // Assuming we are on the correct context
357 mCreateTextureQueue.push(&texture);
360 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
362 // Assuming we are on the correct context
363 mCreateBufferQueue.push(&buffer);
366 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
368 // Assuming we are on the correct context
369 mCreateFramebufferQueue.push(&framebuffer);
372 void EglGraphicsController::ProcessDiscardQueues()
375 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
378 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
380 // Process Framebuffers
381 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
383 // Process RenderPass
384 ProcessDiscardQueue<GLES::RenderPass>(mDiscardRenderPassQueue);
386 // Process RenderTarget
387 ProcessDiscardQueue<GLES::RenderTarget>(mDiscardRenderTargetQueue);
390 ProcessDiscardQueue(mDiscardPipelineQueue);
393 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
396 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
399 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
401 // Process command buffers
402 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
405 void EglGraphicsController::ProcessCreateQueues()
408 ProcessCreateQueue(mCreateTextureQueue);
411 ProcessCreateQueue(mCreateBufferQueue);
413 // Process framebuffers
414 ProcessCreateQueue(mCreateFramebufferQueue);
417 void EglGraphicsController::ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer)
420 const auto commands = commandBuffer.GetCommands(count);
421 for(auto i = 0u; i < count; ++i)
423 auto& cmd = commands[i];
427 case GLES::CommandType::FLUSH:
429 // Nothing to do here
432 case GLES::CommandType::BIND_TEXTURES:
434 mCurrentContext->BindTextures(cmd.bindTextures.textureBindings.Ptr(), cmd.bindTextures.textureBindingsCount);
437 case GLES::CommandType::BIND_VERTEX_BUFFERS:
439 auto bindings = cmd.bindVertexBuffers.vertexBufferBindings.Ptr();
440 mCurrentContext->BindVertexBuffers(bindings, cmd.bindVertexBuffers.vertexBufferBindingsCount);
443 case GLES::CommandType::BIND_UNIFORM_BUFFER:
445 auto& bindings = cmd.bindUniformBuffers;
446 mCurrentContext->BindUniformBuffers(bindings.uniformBufferBindingsCount ? bindings.uniformBufferBindings.Ptr() : nullptr, bindings.uniformBufferBindingsCount, bindings.standaloneUniformsBufferBinding);
449 case GLES::CommandType::BIND_INDEX_BUFFER:
451 mCurrentContext->BindIndexBuffer(cmd.bindIndexBuffer);
454 case GLES::CommandType::BIND_SAMPLERS:
458 case GLES::CommandType::BIND_PIPELINE:
460 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
461 mCurrentContext->BindPipeline(pipeline);
464 case GLES::CommandType::DRAW:
466 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
469 case GLES::CommandType::DRAW_INDEXED:
471 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
474 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
476 mCurrentContext->Flush(false, cmd.draw, mTextureDependencyChecker);
479 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
481 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
484 case GLES::CommandType::SET_SCISSOR_TEST:
486 mCurrentContext->SetScissorTestEnabled(cmd.scissorTest.enable);
489 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
491 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
495 case GLES::CommandType::SET_COLOR_MASK:
497 mCurrentContext->ColorMask(cmd.colorMask.enabled);
500 case GLES::CommandType::CLEAR_STENCIL_BUFFER:
502 mCurrentContext->ClearStencilBuffer();
505 case GLES::CommandType::CLEAR_DEPTH_BUFFER:
507 mCurrentContext->ClearDepthBuffer();
511 case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
513 mCurrentContext->SetStencilTestEnable(cmd.stencilTest.enabled);
517 case GLES::CommandType::SET_STENCIL_FUNC:
519 mCurrentContext->StencilFunc(cmd.stencilFunc.compareOp,
520 cmd.stencilFunc.reference,
521 cmd.stencilFunc.compareMask);
525 case GLES::CommandType::SET_STENCIL_WRITE_MASK:
527 mCurrentContext->StencilMask(cmd.stencilWriteMask.mask);
531 case GLES::CommandType::SET_STENCIL_OP:
533 mCurrentContext->StencilOp(cmd.stencilOp.failOp,
534 cmd.stencilOp.depthFailOp,
535 cmd.stencilOp.passOp);
539 case GLES::CommandType::SET_DEPTH_COMPARE_OP:
541 mCurrentContext->SetDepthCompareOp(cmd.depth.compareOp);
544 case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
546 mCurrentContext->SetDepthTestEnable(cmd.depth.testEnabled);
549 case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
551 mCurrentContext->SetDepthWriteEnable(cmd.depth.writeEnabled);
555 case GLES::CommandType::BEGIN_RENDERPASS:
557 auto& renderTarget = *cmd.beginRenderPass.renderTarget;
558 const auto& targetInfo = renderTarget.GetCreateInfo();
560 if(targetInfo.surface)
562 // switch to surface context
563 mGraphics->ActivateSurfaceContext(static_cast<Dali::RenderSurfaceInterface*>(targetInfo.surface));
565 else if(targetInfo.framebuffer)
567 // switch to resource context
568 mGraphics->ActivateResourceContext();
571 mCurrentContext->BeginRenderPass(cmd.beginRenderPass);
575 case GLES::CommandType::END_RENDERPASS:
577 mCurrentContext->EndRenderPass(mTextureDependencyChecker);
579 // This sync object is to enable cpu to wait for rendering to complete, not gpu.
580 // It's only needed for reading the framebuffer texture in the client.
581 auto syncObject = const_cast<GLES::SyncObject*>(static_cast<const GLES::SyncObject*>(cmd.endRenderPass.syncObject));
584 syncObject->InitializeResource();
588 case GLES::CommandType::PRESENT_RENDER_TARGET:
590 ResolvePresentRenderTarget(cmd.presentRenderTarget.targetToPresent);
592 // The command buffer will be pushed into the queue of presentation command buffers
593 // for further reuse.
594 if(commandBuffer.GetCreateInfo().fixedCapacity == 1)
596 mPresentationCommandBuffers.push(&commandBuffer);
600 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
602 // Process secondary command buffers
603 // todo: check validity of the secondaries
604 // there are operations which are illigal to be done
605 // within secondaries.
606 auto buffers = cmd.executeCommandBuffers.buffers.Ptr();
607 for(auto j = 0u; j < cmd.executeCommandBuffers.buffersCount; ++j)
609 auto& buf = buffers[j];
610 ProcessCommandBuffer(*static_cast<const GLES::CommandBuffer*>(buf));
614 case GLES::CommandType::DRAW_NATIVE:
616 auto* info = &cmd.drawNative.drawNativeInfo;
618 mCurrentContext->PrepareForNativeRendering();
620 if(info->glesNativeInfo.eglSharedContextStoragePointer)
622 auto* anyContext = reinterpret_cast<std::any*>(info->glesNativeInfo.eglSharedContextStoragePointer);
623 *anyContext = mSharedContext;
626 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
628 mCurrentContext->RestoreFromNativeRendering();
635 void EglGraphicsController::ProcessCommandQueues()
639 while(!mCommandQueue.empty())
641 auto cmdBuf = mCommandQueue.front();
643 DUMP_FRAME_COMMAND_BUFFER(cmdBuf);
644 ProcessCommandBuffer(*cmdBuf);
650 void EglGraphicsController::ProcessTextureUpdateQueue()
652 while(!mTextureUpdateRequests.empty())
654 TextureUpdateRequest& request = mTextureUpdateRequests.front();
656 auto& info = request.first;
657 auto& source = request.second;
659 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
661 // GPU memory must be already allocated.
663 // Check if it needs conversion
664 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
665 const auto& createInfo = texture->GetCreateInfo();
666 auto srcFormat = GLES::GLTextureFormatType(info.srcFormat).format;
667 auto srcType = GLES::GLTextureFormatType(info.srcFormat).type;
668 auto destInternalFormat = GLES::GLTextureFormatType(createInfo.format).internalFormat;
669 auto destFormat = GLES::GLTextureFormatType(createInfo.format).format;
671 // From render-texture.cpp
672 const bool isSubImage(info.dstOffset2D.x != 0 || info.dstOffset2D.y != 0 ||
673 info.srcExtent2D.width != (createInfo.size.width / (1 << info.level)) ||
674 info.srcExtent2D.height != (createInfo.size.height / (1 << info.level)));
676 auto* sourceBuffer = reinterpret_cast<uint8_t*>(source.memorySource.memory);
677 auto sourceStride = info.srcStride;
678 std::vector<uint8_t> tempBuffer;
680 if(mGlAbstraction->TextureRequiresConverting(srcFormat, destFormat, isSubImage))
682 // Convert RGB to RGBA if necessary.
683 if(texture->TryConvertPixelData(source.memorySource.memory, info.srcFormat, createInfo.format, info.srcSize, info.srcStride, info.srcExtent2D.width, info.srcExtent2D.height, tempBuffer))
685 sourceBuffer = &tempBuffer[0];
686 sourceStride = 0u; // Converted buffer compacted. make stride as 0.
687 srcFormat = destFormat;
688 srcType = GLES::GLTextureFormatType(createInfo.format).type;
692 // Calculate the maximum mipmap level for the texture
693 texture->SetMaxMipMapLevel(std::max(texture->GetMaxMipMapLevel(), info.level));
695 GLenum bindTarget{GL_TEXTURE_2D};
696 GLenum target{GL_TEXTURE_2D};
698 if(createInfo.textureType == Graphics::TextureType::TEXTURE_CUBEMAP)
700 bindTarget = GL_TEXTURE_CUBE_MAP;
701 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + info.layer;
704 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
705 mGlAbstraction->PixelStorei(GL_UNPACK_ROW_LENGTH, sourceStride);
707 mCurrentContext->BindTexture(bindTarget, texture->GetTextureTypeId(), texture->GetGLTexture());
711 if(!texture->IsCompressed())
713 mGlAbstraction->TexImage2D(target,
716 info.srcExtent2D.width,
717 info.srcExtent2D.height,
725 mGlAbstraction->CompressedTexImage2D(target,
728 info.srcExtent2D.width,
729 info.srcExtent2D.height,
737 if(!texture->IsCompressed())
739 mGlAbstraction->TexSubImage2D(target,
743 info.srcExtent2D.width,
744 info.srcExtent2D.height,
751 mGlAbstraction->CompressedTexSubImage2D(target,
755 info.srcExtent2D.width,
756 info.srcExtent2D.height,
762 // free staging memory
763 free(source.memorySource.memory);
767 // TODO: other sources
770 mTextureUpdateRequests.pop();
774 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
775 const std::vector<TextureUpdateSourceInfo>& sourceList)
778 for(auto& info : updateInfoList)
780 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
781 auto& pair = mTextureUpdateRequests.back();
782 switch(pair.second.sourceType)
784 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
786 auto& info = pair.first;
787 auto& source = pair.second;
789 // allocate staging memory and copy the data
790 // TODO: using PBO with GLES3, this is just naive
793 uint8_t* stagingBuffer = reinterpret_cast<uint8_t*>(malloc(info.srcSize));
795 uint8_t* srcMemory = &reinterpret_cast<uint8_t*>(source.memorySource.memory)[info.srcOffset];
797 std::copy(srcMemory, srcMemory + info.srcSize, stagingBuffer);
799 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
801 // store staging buffer
802 source.memorySource.memory = stagingBuffer;
805 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
807 // TODO, with PBO support
810 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
812 // TODO texture 2 texture in-GPU copy
818 // If upload buffer exceeds maximum size, flush.
819 if(mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB * 1024 * 1024)
822 mTextureUploadTotalCPUMemoryUsed = 0;
826 void EglGraphicsController::ProcessTextureMipmapGenerationQueue()
828 while(!mTextureMipmapGenerationRequests.empty())
830 auto* texture = mTextureMipmapGenerationRequests.front();
832 mCurrentContext->BindTexture(texture->GetGlTarget(), texture->GetTextureTypeId(), texture->GetGLTexture());
833 mCurrentContext->GenerateMipmap(texture->GetGlTarget());
835 mTextureMipmapGenerationRequests.pop();
839 void EglGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
841 mTextureMipmapGenerationRequests.push(static_cast<const GLES::Texture*>(&texture));
844 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
846 mGraphics->ActivateResourceContext();
848 // Mapping buffer requires the object to be created NOW
849 // Workaround - flush now, otherwise there will be given a staging buffer
850 // in case when the buffer is not there yet
851 ProcessCreateQueues();
853 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
855 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
859 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
863 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
865 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
868 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
870 return *mPipelineCache;
873 } // namespace Dali::Graphics