2 * Copyright (c) 2021 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/debug.h>
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/gl-defines.h>
24 #include <dali/internal/graphics/gles-impl/gles-graphics-command-buffer.h>
25 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline.h>
26 #include <dali/internal/graphics/gles-impl/gles-graphics-render-pass.h>
27 #include <dali/internal/graphics/gles-impl/gles-graphics-render-target.h>
28 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
29 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
30 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
31 #include <dali/internal/graphics/gles-impl/gles3-graphics-memory.h>
32 #include <dali/public-api/common/dali-common.h>
33 #include "gles-graphics-program.h"
35 namespace Dali::Graphics
40 * @brief Custom deleter for all Graphics objects created
41 * with use of the Controller.
43 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
44 * doesn't destroy it directly but passes the ownership back
45 * to the Controller. The GLESDeleter is responsible for passing
46 * the object to the discard queue (by calling Resource::DiscardResource()).
51 GLESDeleter() = default;
53 void operator()(T* object)
55 // Discard resource (add it to discard queue)
56 object->DiscardResource();
61 * @brief Helper function allocating graphics object
63 * @param[in] info Create info structure
64 * @param[in] controller Controller object
65 * @param[out] out Unique pointer to the return object
67 template<class GLESType, class GfxCreateInfo, class T>
68 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
71 using Type = typename T::element_type;
72 using UPtr = Graphics::UniquePtr<Type>;
73 if(info.allocationCallbacks)
75 auto* memory = info.allocationCallbacks->allocCallback(
78 info.allocationCallbacks->userData);
79 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
81 else // Use standard allocator
83 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
87 template<class T0, class T1>
88 T0* CastObject(T1* apiObject)
90 return static_cast<T0*>(apiObject);
95 EglGraphicsController::~EglGraphicsController() = default;
97 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
99 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #1\n");
100 mGlAbstraction = &glAbstraction;
101 mContext = std::make_unique<GLES::Context>(*this);
104 void EglGraphicsController::Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
105 Integration::GlContextHelperAbstraction& glContextHelperAbstraction)
107 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #2\n");
108 mGlSyncAbstraction = &glSyncAbstraction;
109 mGlContextHelperAbstraction = &glContextHelperAbstraction;
112 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
114 for(auto& cmdbuf : submitInfo.cmdBuffer)
116 // Push command buffers
117 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
120 // If flush bit set, flush all pending tasks
121 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
127 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
129 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
130 return *mGlAbstraction;
133 Integration::GlSyncAbstraction& EglGraphicsController::GetGlSyncAbstraction()
135 DALI_ASSERT_DEBUG(mGlSyncAbstraction && "Graphics controller not initialized");
136 return *mGlSyncAbstraction;
139 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
141 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
142 return *mGlContextHelperAbstraction;
145 Graphics::UniquePtr<CommandBuffer> EglGraphicsController::CreateCommandBuffer(
146 const CommandBufferCreateInfo& commandBufferCreateInfo,
147 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
149 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
152 Graphics::UniquePtr<RenderPass> EglGraphicsController::CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass)
154 return NewObject<GLES::RenderPass>(renderPassCreateInfo, *this, std::move(oldRenderPass));
157 Graphics::UniquePtr<Texture>
158 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
160 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
163 Graphics::UniquePtr<Buffer> EglGraphicsController::CreateBuffer(
164 const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
166 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
169 Graphics::UniquePtr<Framebuffer> EglGraphicsController::CreateFramebuffer(
170 const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer)
172 return NewObject<GLES::Framebuffer>(framebufferCreateInfo, *this, std::move(oldFramebuffer));
175 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(
176 const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
178 // Create pipeline cache if needed
181 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
184 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
187 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(
188 const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
190 // Create program cache if needed
193 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
196 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
199 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
201 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
204 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
206 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
209 Graphics::UniquePtr<RenderTarget> EglGraphicsController::CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget)
211 return NewObject<GLES::RenderTarget>(renderTargetCreateInfo, *this, std::move(oldRenderTarget));
214 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
216 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
219 void EglGraphicsController::AddTexture(GLES::Texture& texture)
221 // Assuming we are on the correct context
222 mCreateTextureQueue.push(&texture);
225 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
227 // Assuming we are on the correct context
228 mCreateBufferQueue.push(&buffer);
231 void EglGraphicsController::AddFramebuffer(GLES::Framebuffer& framebuffer)
233 // Assuming we are on the correct context
234 mCreateFramebufferQueue.push(&framebuffer);
237 void EglGraphicsController::ProcessDiscardQueues()
240 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
243 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
245 // Process Framebuffers
246 ProcessDiscardQueue<GLES::Framebuffer>(mDiscardFramebufferQueue);
249 ProcessDiscardQueue<GLES::Pipeline>(mDiscardPipelineQueue);
252 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
255 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
258 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
260 // Process command buffers
261 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
264 void EglGraphicsController::ProcessCreateQueues()
267 ProcessCreateQueue(mCreateTextureQueue);
270 ProcessCreateQueue(mCreateBufferQueue);
272 // Process framebuffers
273 ProcessCreateQueue(mCreateFramebufferQueue);
276 void EglGraphicsController::ProcessCommandBuffer(GLES::CommandBuffer& commandBuffer)
278 for(auto& cmd : commandBuffer.GetCommands())
283 case GLES::CommandType::FLUSH:
285 // Nothing to do here
288 case GLES::CommandType::BIND_TEXTURES:
290 mContext->BindTextures(cmd.bindTextures.textureBindings);
293 case GLES::CommandType::BIND_VERTEX_BUFFERS:
295 auto& bindings = cmd.bindVertexBuffers.vertexBufferBindings;
296 mContext->BindVertexBuffers(bindings);
299 case GLES::CommandType::BIND_UNIFORM_BUFFER:
301 auto& bindings = cmd.bindUniformBuffers;
302 mContext->BindUniformBuffers(bindings.uniformBufferBindings, bindings.standaloneUniformsBufferBinding);
305 case GLES::CommandType::BIND_INDEX_BUFFER:
307 mContext->BindIndexBuffer(cmd.bindIndexBuffer);
310 case GLES::CommandType::BIND_SAMPLERS:
314 case GLES::CommandType::BIND_PIPELINE:
316 auto pipeline = static_cast<const GLES::Pipeline*>(cmd.bindPipeline.pipeline);
317 mContext->BindPipeline(pipeline);
320 case GLES::CommandType::DRAW:
322 mContext->Flush(false, cmd.draw);
325 case GLES::CommandType::DRAW_INDEXED:
327 mContext->Flush(false, cmd.draw);
330 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
332 mContext->Flush(false, cmd.draw);
335 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
337 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
340 case GLES::CommandType::SET_SCISSOR_TEST:
342 if(cmd.scissorTest.enable)
344 mGlAbstraction->Enable(GL_SCISSOR_TEST);
348 mGlAbstraction->Disable(GL_SCISSOR_TEST);
352 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
354 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
357 case GLES::CommandType::BEGIN_RENDERPASS:
359 mContext->BeginRenderPass(cmd.beginRenderPass);
362 case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
364 // Process secondary command buffers
365 // todo: check validity of the secondaries
366 // there are operations which are illigal to be done
367 // within secondaries.
368 for(auto& buf : cmd.executeCommandBuffers.buffers)
370 ProcessCommandBuffer(*static_cast<GLES::CommandBuffer*>(buf));
378 void EglGraphicsController::ProcessCommandQueues()
380 // TODO: command queue per context, sync between queues should be
382 currentFramebuffer = nullptr;
384 while(!mCommandQueue.empty())
386 auto cmdBuf = mCommandQueue.front();
388 ProcessCommandBuffer(*cmdBuf);
392 void EglGraphicsController::ProcessTextureUpdateQueue()
394 while(!mTextureUpdateRequests.empty())
396 TextureUpdateRequest& request = mTextureUpdateRequests.front();
398 auto& info = request.first;
399 auto& source = request.second;
401 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
403 // GPU memory must be already allocated (glTexImage2D())
404 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
405 const auto& createInfo = texture->GetCreateInfo();
407 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
409 mGlAbstraction->BindTexture(GL_TEXTURE_2D, texture->GetGLTexture());
410 mGlAbstraction->TexSubImage2D(GL_TEXTURE_2D,
414 info.srcExtent2D.width,
415 info.srcExtent2D.height,
416 GLES::GLTextureFormatType(createInfo.format).format,
417 GLES::GLTextureFormatType(createInfo.format).type,
418 source.memorySource.memory);
420 // free staging memory
421 free(source.memorySource.memory);
425 // TODO: other sources
428 mTextureUpdateRequests.pop();
432 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
433 const std::vector<TextureUpdateSourceInfo>& sourceList)
436 for(auto& info : updateInfoList)
438 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
439 auto& pair = mTextureUpdateRequests.back();
440 switch(pair.second.sourceType)
442 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
444 auto& info = pair.first;
445 auto& source = pair.second;
447 // allocate staging memory and copy the data
448 // TODO: using PBO with GLES3, this is just naive
451 char* stagingBuffer = reinterpret_cast<char*>(malloc(info.srcSize));
452 std::copy(&reinterpret_cast<char*>(source.memorySource.memory)[info.srcOffset],
453 reinterpret_cast<char*>(source.memorySource.memory) + info.srcSize,
456 // store staging buffer
457 source.memorySource.memory = stagingBuffer;
460 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
462 // TODO, with PBO support
465 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
467 // TODO texture 2 texture in-GPU copy
474 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
476 // Mapping buffer requires the object to be created NOW
477 // Workaround - flush now, otherwise there will be given a staging buffer
478 // in case when the buffer is not there yet
479 ProcessCreateQueues();
481 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
483 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
487 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
491 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
493 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
496 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
498 return *mPipelineCache;
501 } // namespace Dali::Graphics