a39260e5eacef9e8b0004f146f60565488633354
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-pipeline.cpp
1 /*
2  * Copyright (c) 2021 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.programState, mPipelineState->programState, &mCreateInfo.programState);
70   CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
71
72   // This program doesn't need custom deleter
73   auto programImpl        = static_cast<const GLES::Program*>(createInfo.programState->program)->GetImplementation();
74   mPipelineState->program = MakeUnique<GLES::Program>(programImpl);
75
76   // To make sure the program is alive as long as the pipeline is!
77   mCreateInfo.programState->program = mPipelineState->program.get();
78
79   // Set pipeline cache
80   mPipelineState->pipelineCache = &pipelineCache;
81 }
82
83 const PipelineCreateInfo& PipelineImpl::GetCreateInfo() const
84 {
85   return mCreateInfo;
86 }
87
88 auto& PipelineImpl::GetController() const
89 {
90   return mController;
91 }
92
93 void PipelineImpl::Bind(const uint32_t glProgram) const
94 {
95   auto& gl = *GetController().GetGL();
96   gl.UseProgram(glProgram);
97 }
98
99 void PipelineImpl::Retain()
100 {
101   ++mRefCount;
102 }
103
104 void PipelineImpl::Release()
105 {
106   --mRefCount;
107 }
108
109 uint32_t PipelineImpl::GetRefCount() const
110 {
111   return mRefCount;
112 }
113
114 PipelineImpl::~PipelineImpl() = default;
115
116 // PIPELINE WRAPPER
117
118 const PipelineCreateInfo& Pipeline::GetCreateInfo() const
119 {
120   return mPipeline.GetCreateInfo();
121 }
122
123 EglGraphicsController& Pipeline::GetController() const
124 {
125   return mPipeline.GetController();
126 }
127
128 Pipeline::~Pipeline()
129 {
130   // decrease refcount
131   if(mPipeline.GetRefCount())
132   {
133     mPipeline.Release();
134   }
135 }
136
137 void Pipeline::DiscardResource()
138 {
139   // Send pipeline to discard queue if refcount is 0
140   GetController().DiscardResource(this);
141 }
142
143 } // namespace Dali::Graphics::GLES