Fix SVACE errors in various files
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-pipeline.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "gles-graphics-pipeline.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-defines.h>
23 #include <memory>
24
25 // INTERNAL INCLUDES
26 #include "egl-graphics-controller.h"
27 #include "gles-graphics-pipeline-cache.h"
28 #include "gles-graphics-program.h"
29
30 namespace Dali::Graphics::GLES
31 {
32 /**
33  * Copy of pipeline state, can be also used for internal caching
34  */
35 struct PipelineImpl::PipelineState
36 {
37   PipelineState()  = default;
38   ~PipelineState() = default;
39
40   // for maintaining correct lifecycle, the owned program
41   // wrapper must be created
42   UniquePtr<Program> program;
43
44   ColorBlendState    colorBlendState;
45   DepthStencilState  depthStencilState;
46   ProgramState       programState;
47   ViewportState      viewportState;
48   RasterizationState rasterizationState;
49   VertexInputState   vertexInputState;
50   InputAssemblyState inputAssemblyState;
51
52   PipelineCache* pipelineCache{};
53 };
54
55 PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache)
56 : mController(controller)
57 {
58   // the creation is deferred so it's needed to copy certain parts of the CreateInfo structure
59   mPipelineState = std::make_unique<PipelineImpl::PipelineState>();
60
61   // Make copies of structured pass by pointers and replace
62   // stored create info structure fields
63   CopyStateIfSet(createInfo.inputAssemblyState, mPipelineState->inputAssemblyState, &mCreateInfo.inputAssemblyState);
64   CopyStateIfSet(createInfo.vertexInputState, mPipelineState->vertexInputState, &mCreateInfo.vertexInputState);
65   CopyStateIfSet(createInfo.rasterizationState, mPipelineState->rasterizationState, &mCreateInfo.rasterizationState);
66   CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
67   CopyStateIfSet(createInfo.colorBlendState, mPipelineState->colorBlendState, &mCreateInfo.colorBlendState);
68   CopyStateIfSet(createInfo.depthStencilState, mPipelineState->depthStencilState, &mCreateInfo.depthStencilState);
69   CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
70
71   // This program doesn't need custom deleter
72   auto programImpl        = static_cast<const GLES::Program*>(createInfo.programState->program)->GetImplementation();
73   mPipelineState->program = MakeUnique<GLES::Program>(programImpl);
74
75   // To make sure the program is alive as long as the pipeline is!
76   mCreateInfo.programState->program = mPipelineState->program.get();
77
78   // Set pipeline cache
79   mPipelineState->pipelineCache = &pipelineCache;
80 }
81
82 const PipelineCreateInfo& PipelineImpl::GetCreateInfo() const
83 {
84   return mCreateInfo;
85 }
86
87 auto& PipelineImpl::GetController() const
88 {
89   return mController;
90 }
91
92 void PipelineImpl::Bind(const uint32_t glProgram) const
93 {
94   if(auto gl = GetController().GetGL())
95   {
96     gl->UseProgram(glProgram);
97   }
98 }
99
100 void PipelineImpl::Retain()
101 {
102   ++mRefCount;
103 }
104
105 void PipelineImpl::Release()
106 {
107   --mRefCount;
108 }
109
110 uint32_t PipelineImpl::GetRefCount() const
111 {
112   return mRefCount;
113 }
114
115 PipelineImpl::~PipelineImpl() = default;
116
117 // PIPELINE WRAPPER
118
119 const PipelineCreateInfo& Pipeline::GetCreateInfo() const
120 {
121   return mPipeline.GetCreateInfo();
122 }
123
124 EglGraphicsController& Pipeline::GetController() const
125 {
126   return mPipeline.GetController();
127 }
128
129 Pipeline::~Pipeline()
130 {
131   // decrease refcount
132   if(mPipeline.GetRefCount())
133   {
134     mPipeline.Release();
135   }
136 }
137
138 void Pipeline::DiscardResource()
139 {
140   // Send pipeline to discard queue if refcount is 0
141   GetController().DiscardResource(this);
142 }
143
144 } // namespace Dali::Graphics::GLES