[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / bloom-view / bloom-view-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BLOOM_VIEW_H
2 #define DALI_TOOLKIT_INTERNAL_BLOOM_VIEW_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/public-api/actors/camera-actor.h>
23 #include <dali/public-api/render-tasks/render-task.h>
24 #include <dali/public-api/rendering/frame-buffer.h>
25 #include <cmath>
26 #include <sstream>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/controls/bloom-view/bloom-view.h>
30 #include <dali-toolkit/devel-api/controls/gaussian-blur-view/gaussian-blur-view.h>
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 class BloomView;
39
40 namespace Internal
41 {
42 /**
43  * BloomEffect implementation class
44  */
45 class BloomView : public Control
46 {
47 public:
48   /**
49    * @copydoc Dali::Toolkit::BloomView::BloomView
50    */
51   BloomView();
52
53   /**
54    * @copydoc Dali::Toolkit::BloomView::BloomView
55    */
56   BloomView(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat, const float downsampleWidthScale, const float downsampleHeightScale);
57
58   /**
59    * @copydoc Dali::Toolkit::BloomView::~BloomView
60    */
61   virtual ~BloomView();
62
63   /**
64    * @copydoc Dali::Toolkit::BloomView::New
65    */
66   static Dali::Toolkit::BloomView New();
67   static Dali::Toolkit::BloomView New(const unsigned int numSamples, const float blurBellCurveWidth, const Pixel::Format renderTargetPixelFormat, const float downsampleWidthScale, const float downsampleHeightScale);
68   void                            Activate();
69   void                            Deactivate();
70
71   Property::Index GetBloomThresholdPropertyIndex() const
72   {
73     return mBloomThresholdPropertyIndex;
74   }
75   Property::Index GetBlurStrengthPropertyIndex() const
76   {
77     return mBlurStrengthPropertyIndex;
78   }
79   Property::Index GetBloomIntensityPropertyIndex() const
80   {
81     return mBloomIntensityPropertyIndex;
82   }
83   Property::Index GetBloomSaturationPropertyIndex() const
84   {
85     return mBloomSaturationPropertyIndex;
86   }
87   Property::Index GetImageIntensityPropertyIndex() const
88   {
89     return mImageIntensityPropertyIndex;
90   }
91   Property::Index GetImageSaturationPropertyIndex() const
92   {
93     return mImageSaturationPropertyIndex;
94   }
95
96 private:
97   void OnInitialize() override;
98   void OnSizeSet(const Vector3& targetSize) override;
99
100   /**
101    * @copydoc Control::OnChildAdd()
102    */
103   void OnChildAdd(Actor& child) override;
104
105   /**
106    * @copydoc Control::OnChildRemove()
107    */
108   void OnChildRemove(Actor& child) override;
109
110   void AllocateResources();
111   void CreateRenderTasks();
112   void RemoveRenderTasks();
113
114   void SetupProperties();
115
116   /////////////////////////////////////////////////////////////
117   unsigned int  mBlurNumSamples;     // number of blur samples in each of horiz/vert directions
118   float         mBlurBellCurveWidth; // constant used when calculating the gaussian weights
119   Pixel::Format mPixelFormat;        // pixel format used by render targets
120
121   /////////////////////////////////////////////////////////////
122   // downsampling is used for the separated blur passes to get increased blur with the same number of samples and also to make rendering quicker
123   float mDownsampleWidthScale;
124   float mDownsampleHeightScale;
125   float mDownsampledWidth;
126   float mDownsampledHeight;
127
128   /////////////////////////////////////////////////////////////
129   // for checking if we need to reallocate render targets
130   Vector2 mTargetSize;
131   Vector2 mLastSize;
132
133   /////////////////////////////////////////////////////////////
134   // for creating a subtree for all user added child actors, so that we can have them exclusive to the mRenderChildrenTask and our other actors exclusive to our other tasks
135   Actor mChildrenRoot;
136   // for creating a subtree for the internal actors
137   Actor mInternalRoot;
138
139   /////////////////////////////////////////////////////////////
140   // for mapping offscreen renders to render target sizes
141   CameraActor mRenderFullSizeCamera;
142   CameraActor mRenderDownsampledCamera;
143
144   /////////////////////////////////////////////////////////////
145   // for rendering all user added children to offscreen target
146   FrameBuffer mRenderTargetForRenderingChildren;
147   RenderTask  mRenderChildrenTask;
148
149   /////////////////////////////////////////////////////////////
150   // for extracting bright parts of image to an offscreen target
151   FrameBuffer mBloomExtractTarget; // for rendering bright parts of image into separate texture, also used as target for gaussian blur
152   RenderTask  mBloomExtractTask;
153   Actor       mBloomExtractActor;
154
155   /////////////////////////////////////////////////////////////
156   // for blurring extracted bloom
157   Dali::Toolkit::GaussianBlurView mGaussianBlurView;
158
159   /////////////////////////////////////////////////////////////
160   // for compositing bloom and children renders to offscreen target
161   RenderTask mCompositeTask;
162
163   Actor mCompositeActor;
164
165   /////////////////////////////////////////////////////////////
166   // for holding blurred result
167   FrameBuffer mOutputRenderTarget;
168   Actor       mTargetActor;
169
170   /////////////////////////////////////////////////////////////
171   // Properties for setting by user, e.g. by animations
172   Property::Index mBloomThresholdPropertyIndex;
173   Property::Index mBlurStrengthPropertyIndex;
174   Property::Index mBloomIntensityPropertyIndex;
175   Property::Index mBloomSaturationPropertyIndex;
176   Property::Index mImageIntensityPropertyIndex;
177   Property::Index mImageSaturationPropertyIndex;
178
179   bool mActivated : 1;
180
181 private:
182   // Undefined copy constructor.
183   BloomView(const BloomView&);
184
185   // Undefined assignment operator.
186   BloomView& operator=(const BloomView&);
187 };
188
189 } // namespace Internal
190
191 // Helpers for public-api forwarding methods
192 inline Toolkit::Internal::BloomView& GetImpl(Toolkit::BloomView& obj)
193 {
194   DALI_ASSERT_ALWAYS(obj);
195   Dali::RefObject& handle = obj.GetImplementation();
196   return static_cast<Toolkit::Internal::BloomView&>(handle);
197 }
198
199 inline const Toolkit::Internal::BloomView& GetImpl(const Toolkit::BloomView& obj)
200 {
201   DALI_ASSERT_ALWAYS(obj);
202   const Dali::RefObject& handle = obj.GetImplementation();
203   return static_cast<const Toolkit::Internal::BloomView&>(handle);
204 }
205
206 } // namespace Toolkit
207
208 } // namespace Dali
209
210 #endif // DALI_TOOLKIT_INTERNAL_BLOOM_VIEW_H