Multi-level context caching
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-pipeline.h
1 #ifndef DALI_GRAPHICS_GLES_PIPELINE_H
2 #define DALI_GRAPHICS_GLES_PIPELINE_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-pipeline-create-info.h>
23 #include <dali/graphics-api/graphics-pipeline.h>
24 #include <string.h>
25
26 // INTERNAL INCLUDES
27 #include "gles-graphics-reflection.h"
28 #include "gles-graphics-resource.h"
29
30 namespace Dali::Graphics::GLES
31 {
32 class PipelineCache;
33 class Program;
34
35 /**
36  * @brief PipelineImpl is the implementation of Pipeline
37  *
38  * PipelineImpl is owned by the pipeline cache. The client-side
39  * will receive Graphics::Pipeline objects which are only
40  * wrappers for this implementation. The lifecycle of
41  * PipelineImpl is managed by the PipelineCache.
42  */
43 class PipelineImpl
44 {
45 public:
46   /**
47    * @brief Constructor
48    * @param[in] createInfo valid TextureCreateInfo structure
49    * @param[in] controller Reference to the Controller
50    * @param[in] pipelineCache Reference to valid pipeline cache
51    */
52   PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller, PipelineCache& pipelineCache);
53
54   /**
55    * @brief Destructor
56    */
57   ~PipelineImpl();
58
59   /**
60    * @brief Binds pipeline
61    *
62    * Binds Pipeline by binding GL program and flushing state.
63    *
64    * @param[in] glProgram The GL program to be bound
65    */
66   void Bind(const uint32_t glProgram) const;
67
68   /**
69    * @brief Increases ref count
70    */
71   void Retain();
72
73   /**
74    * @brief Decreases ref count
75    */
76   void Release();
77
78   /**
79    * @brief Retrieves ref count
80    * @return Refcount value
81    */
82   [[nodiscard]] uint32_t GetRefCount() const;
83
84   /**
85    * @brief Returns PipelineCreateInfo structure
86    *
87    * @return PipelineCreateInfo structure
88    */
89   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
90
91   /**
92    * @brief Returns controller
93    *
94    * @return Reference to the Controller
95    */
96   [[nodiscard]] auto& GetController() const;
97
98 private:
99   /**
100    * @brief Helper function. Copies state if pointer is set
101    */
102   template<class T>
103   void CopyStateIfSet(const T* sourceState, T& copyState, T** destState)
104   {
105     *destState = nullptr;
106     if(sourceState)
107     {
108       copyState  = *sourceState;
109       *destState = &copyState;
110     }
111   }
112
113   /**
114    * @brief Helper function. Copies const state if pointer is set
115    */
116   template<class T>
117   void CopyStateIfSet(const T* sourceState, T& copyState, const T** destState)
118   {
119     *destState = nullptr;
120     if(sourceState)
121     {
122       copyState  = *sourceState;
123       *destState = &copyState;
124     }
125   }
126
127   // Pipeline state is store locally so any assigned pointers on a
128   // client-side may go safely out of scope.
129   struct PipelineState;
130   std::unique_ptr<PipelineState> mPipelineState;
131
132   EglGraphicsController& mController;
133   PipelineCreateInfo     mCreateInfo;
134
135   uint32_t mRefCount{0u};
136 };
137
138 /**
139  * @brief Pipeline class wraps the PipelineImpl
140  */
141 class Pipeline : public Graphics::Pipeline
142 {
143 public:
144   Pipeline() = delete;
145
146   /**
147    * @brief Constructor
148    * @param pipeline Pipeline implementation
149    */
150   explicit Pipeline(GLES::PipelineImpl& pipeline)
151   : mPipeline(pipeline)
152   {
153     // increase refcount
154     mPipeline.Retain();
155   }
156
157   /**
158    * @brief Destructor
159    */
160   ~Pipeline() override;
161
162   /**
163    * @brief Returns pipeline implementation
164    *
165    * @return Valid pipeline implementation
166    */
167   [[nodiscard]] auto& GetPipeline() const
168   {
169     return mPipeline;
170   }
171
172   /**
173    * @brief Returns create info structure
174    *
175    * @return Valid create info structure
176    */
177   [[nodiscard]] const PipelineCreateInfo& GetCreateInfo() const;
178
179   /**
180    * @brief Returns controller
181    *
182    * @return reference to Controller
183    */
184   [[nodiscard]] EglGraphicsController& GetController() const;
185
186   bool operator==(const PipelineImpl* impl) const
187   {
188     return &mPipeline == impl;
189   }
190
191   /**
192    * @brief Run by UniquePtr to discard resource
193    */
194   void DiscardResource();
195
196   /**
197    * @brief Destroy resource
198    *
199    * Despite this class doesn't inherit Resource it must provide
200    * (so it won't duplicate same data) same set of functions
201    * so it can work with resource management functions of Controller.
202    */
203   void DestroyResource()
204   {
205     // Nothing to do here
206   }
207
208 private:
209   GLES::PipelineImpl& mPipeline;
210 };
211
212 } // namespace Dali::Graphics::GLES
213 #endif