Merge branch 'devel/master' into devel/graphics
[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-resource.h"
28
29 namespace Dali::Graphics::GLES
30 {
31 using PipelineResource = Resource<Graphics::Pipeline, Graphics::PipelineCreateInfo>;
32
33 class Pipeline : public PipelineResource
34 {
35 public:
36   /**
37    * @brief Constructor
38    * @param[in] createInfo valid TextureCreateInfo structure
39    * @param[in] controller Reference to the Controller
40    */
41   Pipeline(const Graphics::PipelineCreateInfo& createInfo, Graphics::EglGraphicsController& controller);
42
43   /**
44    * @brief Destroys all the low-level resources used by the class
45    */
46   void DestroyResource() override;
47
48   /**
49    * @brief Initializes low-level resources
50    *
51    * @return Tron success
52    */
53   bool InitializeResource() override;
54
55   /**
56    * @brief Discards object
57    */
58   void DiscardResource() override;
59
60   /**
61    * @brief returns GL program id
62    * @return GL program id
63    */
64   [[nodiscard]] uint32_t GetGLProgram() const;
65
66   /**
67    * @brief Binds pipeline
68    *
69    * Binds Pipeline by binding GL program and flushing state.
70    *
71    * If previous pipeline specified, it will be used in order to
72    * avoid redundant state swiches.
73    *
74    * @param[in] prevPipeline previous pipeline
75    */
76   void Bind(GLES::Pipeline* prevPipeline);
77
78   /**
79    * Executes state change function if condition met
80    */
81   template<typename FUNC, typename STATE>
82   void ExecuteStateChange(FUNC& func, const STATE* prevPipelineState, const STATE* thisPipelineState)
83   {
84     if(!prevPipelineState)
85     {
86       func();
87     }
88     else
89     {
90       // binary test and execute when different
91       if(memcmp(prevPipelineState, thisPipelineState, sizeof(STATE)) != 0)
92       {
93         func();
94       }
95     }
96   }
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 stored as a copy of create info
128   // data.
129   struct PipelineState;
130   std::unique_ptr<PipelineState> mPipelineState{};
131
132   uint32_t mGlProgram{0u};
133 };
134
135 } // namespace Dali::Graphics::GLES
136 #endif