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/gl-abstraction.h>
22 #include <dali/integration-api/gl-defines.h>
23 #include <dali/internal/graphics/gles-impl/gles-graphics-command-buffer.h>
24 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline.h>
25 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
26 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
27 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
28 #include <dali/public-api/common/dali-common.h>
29 #include "gles-graphics-program.h"
31 namespace Dali::Graphics
36 * @brief Custom deleter for all Graphics objects created
37 * with use of the Controller.
39 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
40 * doesn't destroy it directly but passes the ownership back
41 * to the Controller. The GLESDeleter is responsible for passing
42 * the object to the discard queue (by calling Resource::DiscardResource()).
47 GLESDeleter() = default;
49 void operator()(T* object)
51 // Discard resource (add it to discard queue)
52 object->DiscardResource();
57 * @brief Helper function allocating graphics object
59 * @param[in] info Create info structure
60 * @param[in] controller Controller object
61 * @param[out] out Unique pointer to the return object
63 template<class GLESType, class GfxCreateInfo, class T>
64 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
67 using Type = typename T::element_type;
68 using UPtr = Graphics::UniquePtr<Type>;
69 if(info.allocationCallbacks)
71 auto* memory = info.allocationCallbacks->allocCallback(
74 info.allocationCallbacks->userData);
75 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
77 else // Use standard allocator
79 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
83 template<class T0, class T1>
84 T0* CastObject(T1* apiObject)
86 return static_cast<T0*>(apiObject);
91 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
93 mGlAbstraction = &glAbstraction;
94 mContext = std::make_unique<GLES::Context>(*this);
97 void EglGraphicsController::Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
98 Integration::GlContextHelperAbstraction& glContextHelperAbstraction)
100 mGlSyncAbstraction = &glSyncAbstraction;
101 mGlContextHelperAbstraction = &glContextHelperAbstraction;
104 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
106 for(auto& cmdbuf : submitInfo.cmdBuffer)
108 // Push command buffers
109 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
112 // If flush bit set, flush all pending tasks
113 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
119 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
121 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
122 return *mGlAbstraction;
125 Integration::GlSyncAbstraction& EglGraphicsController::GetGlSyncAbstraction()
127 DALI_ASSERT_DEBUG(mGlSyncAbstraction && "Graphics controller not initialized");
128 return *mGlSyncAbstraction;
131 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
133 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
134 return *mGlContextHelperAbstraction;
137 Graphics::UniquePtr<CommandBuffer>
138 EglGraphicsController::CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo,
139 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
141 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
144 Graphics::UniquePtr<Texture>
145 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
147 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
150 Graphics::UniquePtr<Buffer>
151 EglGraphicsController::CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
153 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
156 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
158 // Create pipeline cache if needed
161 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
164 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
167 Graphics::UniquePtr<Program> EglGraphicsController::CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram)
169 // Create program cache if needed
172 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
175 return mPipelineCache->GetProgram(programCreateInfo, std::move(oldProgram));
178 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
180 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
183 const Graphics::Reflection& EglGraphicsController::GetProgramReflection(const Graphics::Program& program)
185 return static_cast<const Graphics::GLES::Program*>(&program)->GetReflection();
188 void EglGraphicsController::AddTexture(GLES::Texture& texture)
190 // Assuming we are on the correct context
191 mCreateTextureQueue.push(&texture);
194 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
196 // Assuming we are on the correct context
197 mCreateBufferQueue.push(&buffer);
200 void EglGraphicsController::ProcessDiscardQueues()
203 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
206 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
209 ProcessDiscardQueue<GLES::Pipeline>(mDiscardPipelineQueue);
212 ProcessDiscardQueue<GLES::Program>(mDiscardProgramQueue);
215 void EglGraphicsController::ProcessCreateQueues()
218 ProcessCreateQueue(mCreateTextureQueue);
221 ProcessCreateQueue(mCreateBufferQueue);
224 void EglGraphicsController::ProcessCommandQueues()
226 // TODO: command queue per context, sync between queues should be
229 while(!mCommandQueue.empty())
231 auto cmdBuf = mCommandQueue.front();
234 for(auto& cmd : cmdBuf->GetCommands())
239 case GLES::CommandType::FLUSH:
241 // Nothing to do here
244 case GLES::CommandType::BIND_TEXTURES:
246 mContext->BindTextures(cmd.bindTextures.textureBindings);
249 case GLES::CommandType::BIND_VERTEX_BUFFERS:
251 auto& bindings = cmd.bindVertexBuffers.vertexBufferBindings;
252 mContext->BindVertexBuffers(bindings);
255 case GLES::CommandType::BIND_INDEX_BUFFER:
257 mContext->BindIndexBuffer(cmd.bindIndexBuffer);
260 case GLES::CommandType::BIND_SAMPLERS:
264 case GLES::CommandType::BIND_PIPELINE:
266 mContext->BindPipeline(cmd.bindPipeline.pipeline);
269 case GLES::CommandType::DRAW:
271 mContext->Flush(false, cmd.draw);
274 case GLES::CommandType::DRAW_INDEXED:
276 mContext->Flush(false, cmd.draw);
279 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
281 mContext->Flush(false, cmd.draw);
289 void EglGraphicsController::ProcessTextureUpdateQueue()
291 while(!mTextureUpdateRequests.empty())
293 TextureUpdateRequest& request = mTextureUpdateRequests.front();
295 auto& info = request.first;
296 auto& source = request.second;
298 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
300 // GPU memory must be already allocated (glTexImage2D())
301 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
302 const auto& createInfo = texture->GetCreateInfo();
304 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
306 mGlAbstraction->BindTexture(GL_TEXTURE_2D, texture->GetGLTexture());
307 mGlAbstraction->TexSubImage2D(GL_TEXTURE_2D,
311 info.srcExtent2D.width,
312 info.srcExtent2D.height,
313 GLES::GLTextureFormatType(createInfo.format).format,
314 GLES::GLTextureFormatType(createInfo.format).type,
315 source.memorySource.memory);
317 // free staging memory
318 free(source.memorySource.memory);
322 // TODO: other sources
325 mTextureUpdateRequests.pop();
329 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
330 const std::vector<TextureUpdateSourceInfo>& sourceList)
333 for(auto& info : updateInfoList)
335 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
336 auto& pair = mTextureUpdateRequests.back();
337 switch(pair.second.sourceType)
339 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
341 auto& info = pair.first;
342 auto& source = pair.second;
344 // allocate staging memory and copy the data
345 // TODO: using PBO with GLES3, this is just naive
348 char* stagingBuffer = reinterpret_cast<char*>(malloc(info.srcSize));
349 std::copy(&reinterpret_cast<char*>(source.memorySource.memory)[info.srcOffset],
350 reinterpret_cast<char*>(source.memorySource.memory) + info.srcSize,
353 // store staging buffer
354 source.memorySource.memory = stagingBuffer;
357 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
359 // TODO, with PBO support
362 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
364 // TODO texture 2 texture in-GPU copy
371 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
373 // Mapping buffer requires the object to be created NOW
374 // Workaround - flush now, otherwise there will be given a staging buffer
375 // in case when the buffer is not there yet
376 ProcessCreateQueues();
378 return Graphics::UniquePtr<Memory>(new GLES::Memory(mapInfo, *this));
381 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
383 return static_cast<GLES::Program*>(&program)->GetImplementation()->GetParameter(parameterId, outData);
386 GLES::PipelineCache& EglGraphicsController::GetPipelineCache() const
388 return *mPipelineCache;
391 } // namespace Dali::Graphics