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>
30 namespace Dali::Graphics
35 * @brief Custom deleter for all Graphics objects created
36 * with use of the Controller.
38 * When Graphics object dies the unique pointer (Graphics::UniquePtr)
39 * doesn't destroy it directly but passes the ownership back
40 * to the Controller. The GLESDeleter is responsible for passing
41 * the object to the discard queue (by calling Resource::DiscardResource()).
46 GLESDeleter() = default;
48 void operator()(T* object)
50 // Discard resource (add it to discard queue)
51 object->DiscardResource();
56 * @brief Helper function allocating graphics object
58 * @param[in] info Create info structure
59 * @param[in] controller Controller object
60 * @param[out] out Unique pointer to the return object
62 template<class GLESType, class GfxCreateInfo, class T>
63 auto NewObject(const GfxCreateInfo& info, EglGraphicsController& controller, T&& oldObject)
66 using Type = typename T::element_type;
67 using UPtr = Graphics::UniquePtr<Type>;
68 if(info.allocationCallbacks)
70 auto* memory = info.allocationCallbacks->allocCallback(
73 info.allocationCallbacks->userData);
74 return UPtr(new(memory) GLESType(info, controller), GLESDeleter<GLESType>());
76 else // Use standard allocator
78 return UPtr(new GLESType(info, controller), GLESDeleter<GLESType>());
82 template<class T0, class T1>
83 T0* CastObject(T1* apiObject)
85 return static_cast<T0*>(apiObject);
90 void EglGraphicsController::InitializeGLES(Integration::GlAbstraction& glAbstraction)
92 mGlAbstraction = &glAbstraction;
93 mContext = std::make_unique<GLES::Context>(*this);
96 void EglGraphicsController::Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
97 Integration::GlContextHelperAbstraction& glContextHelperAbstraction)
99 mGlSyncAbstraction = &glSyncAbstraction;
100 mGlContextHelperAbstraction = &glContextHelperAbstraction;
103 void EglGraphicsController::SubmitCommandBuffers(const SubmitInfo& submitInfo)
105 for(auto& cmdbuf : submitInfo.cmdBuffer)
107 // Push command buffers
108 mCommandQueue.push(static_cast<GLES::CommandBuffer*>(cmdbuf));
111 // If flush bit set, flush all pending tasks
112 if(submitInfo.flags & (0 | SubmitFlagBits::FLUSH))
118 Integration::GlAbstraction& EglGraphicsController::GetGlAbstraction()
120 DALI_ASSERT_DEBUG(mGlAbstraction && "Graphics controller not initialized");
121 return *mGlAbstraction;
124 Integration::GlSyncAbstraction& EglGraphicsController::GetGlSyncAbstraction()
126 DALI_ASSERT_DEBUG(mGlSyncAbstraction && "Graphics controller not initialized");
127 return *mGlSyncAbstraction;
130 Integration::GlContextHelperAbstraction& EglGraphicsController::GetGlContextHelperAbstraction()
132 DALI_ASSERT_DEBUG(mGlContextHelperAbstraction && "Graphics controller not initialized");
133 return *mGlContextHelperAbstraction;
136 Graphics::UniquePtr<CommandBuffer>
137 EglGraphicsController::CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo,
138 Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer)
140 return NewObject<GLES::CommandBuffer>(commandBufferCreateInfo, *this, std::move(oldCommandBuffer));
143 Graphics::UniquePtr<Texture>
144 EglGraphicsController::CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture)
146 return NewObject<GLES::Texture>(textureCreateInfo, *this, std::move(oldTexture));
149 Graphics::UniquePtr<Buffer>
150 EglGraphicsController::CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer)
152 return NewObject<GLES::Buffer>(bufferCreateInfo, *this, std::move(oldBuffer));
155 Graphics::UniquePtr<Shader> EglGraphicsController::CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader)
157 return NewObject<GLES::Shader>(shaderCreateInfo, *this, std::move(oldShader));
160 Graphics::UniquePtr<Pipeline> EglGraphicsController::CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
162 // Create pipeline cache if needed
165 mPipelineCache = std::make_unique<GLES::PipelineCache>(*this);
168 return mPipelineCache->GetPipeline(pipelineCreateInfo, std::move(oldPipeline));
171 void EglGraphicsController::AddTexture(GLES::Texture& texture)
173 // Assuming we are on the correct context
174 mCreateTextureQueue.push(&texture);
177 void EglGraphicsController::AddBuffer(GLES::Buffer& buffer)
179 // Assuming we are on the correct context
180 mCreateBufferQueue.push(&buffer);
183 void EglGraphicsController::ProcessDiscardQueues()
186 ProcessDiscardQueue<GLES::Texture>(mDiscardTextureQueue);
189 ProcessDiscardQueue<GLES::Buffer>(mDiscardBufferQueue);
192 void EglGraphicsController::ProcessCreateQueues()
195 ProcessCreateQueue(mCreateTextureQueue);
198 ProcessCreateQueue(mCreateBufferQueue);
201 void EglGraphicsController::ProcessCommandQueues()
203 // TODO: command queue per context, sync between queues should be
206 while(!mCommandQueue.empty())
208 auto cmdBuf = mCommandQueue.front();
211 for(auto& cmd : cmdBuf->GetCommands())
216 case GLES::CommandType::FLUSH:
218 // Nothing to do here
221 case GLES::CommandType::BIND_TEXTURES:
223 mContext->BindTextures(cmd.bindTextures.textureBindings);
226 case GLES::CommandType::BIND_VERTEX_BUFFERS:
228 auto& bindings = cmd.bindVertexBuffers.vertexBufferBindings;
229 mContext->BindVertexBuffers(bindings);
232 case GLES::CommandType::BIND_INDEX_BUFFER:
234 mContext->BindIndexBuffer(cmd.bindIndexBuffer);
237 case GLES::CommandType::BIND_SAMPLERS:
241 case GLES::CommandType::BIND_PIPELINE:
243 mContext->BindPipeline(cmd.bindPipeline.pipeline);
246 case GLES::CommandType::DRAW:
248 mContext->Flush(false, cmd.draw);
251 case GLES::CommandType::DRAW_INDEXED:
253 mContext->Flush(false, cmd.draw);
256 case GLES::CommandType::DRAW_INDEXED_INDIRECT:
258 mContext->Flush(false, cmd.draw);
266 void EglGraphicsController::ProcessTextureUpdateQueue()
268 while(!mTextureUpdateRequests.empty())
270 TextureUpdateRequest& request = mTextureUpdateRequests.front();
272 auto& info = request.first;
273 auto& source = request.second;
275 if(source.sourceType == Graphics::TextureUpdateSourceInfo::Type::MEMORY)
277 // GPU memory must be already allocated (glTexImage2D())
278 auto* texture = static_cast<GLES::Texture*>(info.dstTexture);
279 const auto& createInfo = texture->GetCreateInfo();
281 mGlAbstraction->PixelStorei(GL_UNPACK_ALIGNMENT, 1);
283 mGlAbstraction->BindTexture(GL_TEXTURE_2D, texture->GetGLTexture());
284 mGlAbstraction->TexSubImage2D(GL_TEXTURE_2D,
288 info.srcExtent2D.width,
289 info.srcExtent2D.height,
290 GLES::GLTextureFormatType(createInfo.format).format,
291 GLES::GLTextureFormatType(createInfo.format).type,
292 source.memorySource.memory);
294 // free staging memory
295 free(source.memorySource.memory);
299 // TODO: other sources
302 mTextureUpdateRequests.pop();
306 void EglGraphicsController::UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
307 const std::vector<TextureUpdateSourceInfo>& sourceList)
310 for(auto& info : updateInfoList)
312 mTextureUpdateRequests.push(std::make_pair(info, sourceList[info.srcReference]));
313 auto& pair = mTextureUpdateRequests.back();
314 switch(pair.second.sourceType)
316 case Graphics::TextureUpdateSourceInfo::Type::MEMORY:
318 auto& info = pair.first;
319 auto& source = pair.second;
321 // allocate staging memory and copy the data
322 // TODO: using PBO with GLES3, this is just naive
325 char* stagingBuffer = reinterpret_cast<char*>(malloc(info.srcSize));
326 std::copy(&reinterpret_cast<char*>(source.memorySource.memory)[info.srcOffset],
327 reinterpret_cast<char*>(source.memorySource.memory) + info.srcSize,
330 // store staging buffer
331 source.memorySource.memory = stagingBuffer;
334 case Graphics::TextureUpdateSourceInfo::Type::BUFFER:
336 // TODO, with PBO support
339 case Graphics::TextureUpdateSourceInfo::Type::TEXTURE:
341 // TODO texture 2 texture in-GPU copy
348 Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBufferInfo& mapInfo)
350 // Mapping buffer requires the object to be created NOW
351 // Workaround - flush now, otherwise there will be given a staging buffer
352 // in case when the buffer is not there yet
353 ProcessCreateQueues();
355 return Graphics::UniquePtr<Memory>(new GLES::Memory(mapInfo, *this));
358 } // namespace Dali::Graphics