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-shader.h>
27 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
28 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
29 #include <dali/internal/graphics/gles-impl/gles3-graphics-memory.h>
30 #include <dali/public-api/common/dali-common.h>
31 #include "gles-graphics-program.h"
33 namespace Dali::Graphics
38 * @brief Custom deleter for all Graphics objects created
39 * with use of the Controller.
41 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
42 * doesn't destroy it directly but passes the ownership back
43 * to the Controller. The GLESDeleter is responsible for passing
44 * the object to the discard queue (by calling Resource::DiscardResource()).
49 GLESDeleter() = default;
51 void operator()(T* object)
53 // Discard resource (add it to discard queue)
54 object->DiscardResource();
59 * @brief Helper function allocating graphics object
61 * @param[in] info Create info structure
62 * @param[in] controller Controller object
63 * @param[out] out Unique pointer to the return object
65 template<class GLESType, class GfxCreateInfo, class T>
66 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
69 using Type = typename T::element_type;
70 using UPtr = Graphics::UniquePtr<Type>;
71 if(info.allocationCallbacks)
73 auto* memory = info.allocationCallbacks->allocCallback(
76 info.allocationCallbacks->userData);
77 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
79 else // Use standard allocator
81 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
85 template<class T0, class T1>
86 T0* CastObject(T1* apiObject)
88 return static_cast<T0*>(apiObject);
91 // Maximum size of texture upload buffer.
92 const uint32_t TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB = 1;
96 EglGraphicsController::~EglGraphicsController() = default;
98 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
100 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #1\n");
101 mGlAbstraction = &glAbstraction;
102 mContext = std::make_unique<GLES::Context>(*this);
105 void EglGraphicsController::Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
106 Integration::GlContextHelperAbstraction& glContextHelperAbstraction)
108 DALI_LOG_RELEASE_INFO("Initializing New Graphics Controller #2\n");
109 mGlSyncAbstraction = &glSyncAbstraction;
110 mGlContextHelperAbstraction = &glContextHelperAbstraction;
113 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
115 for(auto& cmdbuf : submitInfo.cmdBuffer)
117 // Push command buffers
118 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
121 // If flush bit set, flush all pending tasks
122 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
128 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
130 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
131 return *mGlAbstraction;
134 Integration::GlSyncAbstraction& EglGraphicsController::GetGlSyncAbstraction()
136 DALI_ASSERT_DEBUG(mGlSyncAbstraction && "Graphics controller not initialized");
137 return *mGlSyncAbstraction;
140 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
142 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
143 return *mGlContextHelperAbstraction;
146 Graphics::UniquePtr<CommandBuffer>
147 EglGraphicsController::CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo,
148 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
150 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
153 Graphics::UniquePtr<Texture>
154 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
156 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
159 Graphics::UniquePtr<Buffer>
160 EglGraphicsController::CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
162 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
165 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
167 // Create pipeline cache if needed
170 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
173 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
176 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
178 // Create program cache if needed
181 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
184 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
187 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
189 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
192 Graphics::UniquePtr<Sampler> EglGraphicsController::CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler)
194 return NewObject<GLES::Sampler>(samplerCreateInfo, *this, std::move(oldSampler));
197 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
199 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
202 void EglGraphicsController::AddTexture(GLES::Texture& texture)
204 // Assuming we are on the correct context
205 mCreateTextureQueue.push(&texture);
208 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
210 // Assuming we are on the correct context
211 mCreateBufferQueue.push(&buffer);
214 void EglGraphicsController::ProcessDiscardQueues()
217 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
220 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
223 ProcessDiscardQueue<GLES::Pipeline>(mDiscardPipelineQueue);
226 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
229 ProcessDiscardQueue<GLES::Shader>(mDiscardShaderQueue);
232 ProcessDiscardQueue<GLES::Sampler>(mDiscardSamplerQueue);
234 // Process command buffers
235 ProcessDiscardQueue<GLES::CommandBuffer>(mDiscardCommandBufferQueue);
238 void EglGraphicsController::ProcessCreateQueues()
241 ProcessCreateQueue(mCreateTextureQueue);
244 ProcessCreateQueue(mCreateBufferQueue);
247 void EglGraphicsController::ProcessCommandQueues()
249 // TODO: command queue per context, sync between queues should be
252 while(!mCommandQueue.empty())
254 auto cmdBuf = mCommandQueue.front();
257 for(auto& cmd : cmdBuf->GetCommands())
262 case GLES::CommandType::FLUSH:
264 // Nothing to do here
267 case GLES::CommandType::BIND_TEXTURES:
269 mContext->BindTextures(cmd.bindTextures.textureBindings);
272 case GLES::CommandType::BIND_VERTEX_BUFFERS:
274 auto& bindings = cmd.bindVertexBuffers.vertexBufferBindings;
275 mContext->BindVertexBuffers(bindings);
278 case GLES::CommandType::BIND_UNIFORM_BUFFER:
280 auto& bindings = cmd.bindUniformBuffers;
281 mContext->BindUniformBuffers(bindings.uniformBufferBindings, bindings.standaloneUniformsBufferBinding);
284 case GLES::CommandType::BIND_INDEX_BUFFER:
286 mContext->BindIndexBuffer(cmd.bindIndexBuffer);
289 case GLES::CommandType::BIND_SAMPLERS:
293 case GLES::CommandType::BIND_PIPELINE:
295 mContext->BindPipeline(cmd.bindPipeline.pipeline);
298 case GLES::CommandType::DRAW:
300 mContext->Flush(false, cmd.draw);
303 case GLES::CommandType::DRAW_INDEXED:
305 mContext->Flush(false, cmd.draw);
308 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
310 mContext->Flush(false, cmd.draw);
313 case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
315 mGlAbstraction->Scissor(cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
318 case GLES::CommandType::SET_SCISSOR_TEST:
320 if(cmd.scissorTest.enable)
322 mGlAbstraction->Enable(GL_SCISSOR_TEST);
326 mGlAbstraction->Disable(GL_SCISSOR_TEST);
330 case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
332 mGlAbstraction->Viewport(cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
340 void EglGraphicsController::ProcessTextureUpdateQueue()
342 while(!mTextureUpdateRequests.empty())
344 TextureUpdateRequest& request = mTextureUpdateRequests.front();
346 auto& info = request.first;
347 auto& source = request.second;
349 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
351 // GPU memory must be already allocated (glTexImage2D())
352 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
353 const auto& createInfo = texture->GetCreateInfo();
355 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
357 mGlAbstraction->BindTexture(GL_TEXTURE_2D, texture->GetGLTexture());
358 mGlAbstraction->TexSubImage2D(GL_TEXTURE_2D,
362 info.srcExtent2D.width,
363 info.srcExtent2D.height,
364 GLES::GLTextureFormatType(createInfo.format).format,
365 GLES::GLTextureFormatType(createInfo.format).type,
366 source.memorySource.memory);
368 // free staging memory
369 free(source.memorySource.memory);
373 // TODO: other sources
376 mTextureUpdateRequests.pop();
380 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
381 const std::vector<TextureUpdateSourceInfo>& sourceList)
384 for(auto& info : updateInfoList)
386 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
387 auto& pair = mTextureUpdateRequests.back();
388 switch(pair.second.sourceType)
390 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
392 auto& info = pair.first;
393 auto& source = pair.second;
395 // allocate staging memory and copy the data
396 // TODO: using PBO with GLES3, this is just naive
399 char* stagingBuffer = reinterpret_cast<char*>(malloc(info.srcSize));
400 std::copy(&reinterpret_cast<char*>(source.memorySource.memory)[info.srcOffset],
401 reinterpret_cast<char*>(source.memorySource.memory) + info.srcSize,
404 mTextureUploadTotalCPUMemoryUsed += info.srcSize;
406 // store staging buffer
407 source.memorySource.memory = stagingBuffer;
410 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
412 // TODO, with PBO support
415 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
417 // TODO texture 2 texture in-GPU copy
423 // If upload buffer exceeds maximum size, flush.
424 if( mTextureUploadTotalCPUMemoryUsed > TEXTURE_UPLOAD_MAX_BUFER_SIZE_MB*1024 )
427 mTextureUploadTotalCPUMemoryUsed = 0;
431 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
433 // Mapping buffer requires the object to be created NOW
434 // Workaround - flush now, otherwise there will be given a staging buffer
435 // in case when the buffer is not there yet
436 ProcessCreateQueues();
438 if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
440 return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
444 return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
448 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
450 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
453 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
455 return *mPipelineCache;
458 } // namespace Dali::Graphics