Merge "Remove the replacing surface code" into devel/master
[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   FramebufferState   framebufferState;
49   RasterizationState rasterizationState;
50   VertexInputState   vertexInputState;
51   InputAssemblyState inputAssemblyState;
52
53   PipelineCache* pipelineCache{};
54 };
55
56 PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache)
57 : mController(controller)
58 {
59   // the creation is deferred so it's needed to copy certain parts of the CreateInfo structure
60   mPipelineState = std::make_unique<PipelineImpl::PipelineState>();
61
62   // Make copies of structured pass by pointers and replace
63   // stored create info structure fields
64   CopyStateIfSet(createInfo.inputAssemblyState, mPipelineState->inputAssemblyState, &mCreateInfo.inputAssemblyState);
65   CopyStateIfSet(createInfo.vertexInputState, mPipelineState->vertexInputState, &mCreateInfo.vertexInputState);
66   CopyStateIfSet(createInfo.rasterizationState, mPipelineState->rasterizationState, &mCreateInfo.rasterizationState);
67   CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
68   CopyStateIfSet(createInfo.framebufferState, mPipelineState->framebufferState, &mCreateInfo.framebufferState);
69   CopyStateIfSet(createInfo.colorBlendState, mPipelineState->colorBlendState, &mCreateInfo.colorBlendState);
70   CopyStateIfSet(createInfo.depthStencilState, mPipelineState->depthStencilState, &mCreateInfo.depthStencilState);
71   CopyStateIfSet(createInfo.programState, mPipelineState->programState, &mCreateInfo.programState);
72   CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
73
74   // This program doesn't need custom deleter
75   auto programImpl        = static_cast<const GLES::Program*>(createInfo.programState->program)->GetImplementation();
76   mPipelineState->program = MakeUnique<GLES::Program>(programImpl);
77
78   // To make sure the program is alive as long as the pipeline is!
79   mCreateInfo.programState->program = mPipelineState->program.get();
80
81   // Set pipeline cache
82   mPipelineState->pipelineCache = &pipelineCache;
83 }
84
85 const PipelineCreateInfo& PipelineImpl::GetCreateInfo() const
86 {
87   return mCreateInfo;
88 }
89
90 auto& PipelineImpl::GetController() const
91 {
92   return mController;
93 }
94
95 void PipelineImpl::Bind(GLES::PipelineImpl* prevPipeline)
96 {
97   auto& gl        = *GetController().GetGL();
98   auto  glProgram = static_cast<const GLES::Program*>(GetCreateInfo().programState->program)->GetImplementation()->GetGlProgram();
99   gl.UseProgram(glProgram);
100 }
101
102 void PipelineImpl::Retain()
103 {
104   ++mRefCount;
105 }
106
107 void PipelineImpl::Release()
108 {
109   --mRefCount;
110 }
111
112 uint32_t PipelineImpl::GetRefCount() const
113 {
114   return mRefCount;
115 }
116
117 PipelineImpl::~PipelineImpl() = default;
118
119 // PIPELINE WRAPPER
120
121 const PipelineCreateInfo& Pipeline::GetCreateInfo() const
122 {
123   return mPipeline.GetCreateInfo();
124 }
125
126 EglGraphicsController& Pipeline::GetController() const
127 {
128   return mPipeline.GetController();
129 }
130
131 Pipeline::~Pipeline()
132 {
133   // decrease refcount
134   if(mPipeline.GetRefCount())
135   {
136     mPipeline.Release();
137   }
138 }
139
140 void Pipeline::DiscardResource()
141 {
142   // Send pipeline to discard queue if refcount is 0
143   GetController().DiscardResource(this);
144 }
145
146 } // namespace Dali::Graphics::GLES