[AT-SPI] Remove SetAccessibilityConstructor()
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / shadow-view / shadow-view-impl.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 "shadow-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/render-tasks/render-task-list.h>
28 #include <dali/public-api/rendering/shader.h>
29 #include <iomanip>
30 #include <sstream>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/devel-api/controls/control-devel.h>
34 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
35 #include <dali-toolkit/internal/controls/control/control-renderers.h>
36 #include <dali-toolkit/internal/controls/shadow-view/shadow-view-impl.h>
37 #include <dali-toolkit/internal/filters/blur-two-pass-filter.h>
38 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
39 #include <dali-toolkit/public-api/visuals/visual-properties.h>
40
41 // TODO:
42 // pixel format / size - set from JSON
43 // aspect ratio property needs to be able to be constrained also for cameras. (now do-able)
44 // default near clip value
45
46 /////////////////////////////////////////////////////////
47 // IMPLEMENTATION NOTES
48
49 // As the ShadowView actor changes size, the amount of pixels we need to blur changes. Therefore we need some way of doing this. However:-
50 // OnSetSize() does not get called when ShadowView object size is modified using a Constraint.
51 // OnSizeAnimation() only gets called once per AnimateTo/By() and if an Animation has N such calls then only the final one will end up being used. Therefore we can't use
52 // OnSizeAnimation() to alter render target sizes.
53 // To get around the above problems, we use fixed sized render targets, from the last SetSize() call (which calls OnSetSize()), then we adjust the internal cameras / actors
54 // to take account of the changed ShadowView object size, projecting to the unchanged render target sizes. This is done relative to the fixed render target / actor sizes
55 // by using constraints relative to the ShadowView actor size.
56
57 namespace Dali
58 {
59 namespace Toolkit
60 {
61 namespace Internal
62 {
63 namespace
64 {
65 using namespace Dali;
66
67 BaseHandle Create()
68 {
69   return Toolkit::ShadowView::New();
70 }
71
72 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::ShadowView, Toolkit::Control, Create)
73 DALI_TYPE_REGISTRATION_END()
74
75 const float BLUR_STRENGTH_DEFAULT = 1.0f;
76
77 const Vector3 DEFAULT_LIGHT_POSITION(300.0f, 250.0f, 600.0f);
78 const float   DEFAULT_FIELD_OF_VIEW_RADIANS = Math::PI / 4.0f; // 45 degrees
79
80 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.8f);
81
82 const char* const SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME = "uLightCameraProjectionMatrix";
83 const char* const SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME       = "uLightCameraViewMatrix";
84 const char* const SHADER_SHADOW_COLOR_PROPERTY_NAME                   = "uShadowColor";
85 const char* const BLUR_STRENGTH_PROPERTY_NAME                         = "BlurStrengthProperty";
86 const char* const SHADOW_COLOR_PROPERTY_NAME                          = "ShadowColorProperty";
87
88 } // namespace
89
90 ShadowView::ShadowView(float downsampleWidthScale, float downsampleHeightScale)
91 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
92   mChildrenRoot(Actor::New()),
93   mCachedShadowColor(DEFAULT_SHADOW_COLOR),
94   mCachedBackgroundColor(DEFAULT_SHADOW_COLOR.r, DEFAULT_SHADOW_COLOR.g, DEFAULT_SHADOW_COLOR.b, 0.0f),
95   mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
96   mShadowColorPropertyIndex(Property::INVALID_INDEX),
97   mDownsampleWidthScale(downsampleWidthScale),
98   mDownsampleHeightScale(downsampleHeightScale)
99 {
100 }
101
102 ShadowView::~ShadowView()
103 {
104 }
105
106 Toolkit::ShadowView ShadowView::New(float downsampleWidthScale, float downsampleHeightScale)
107 {
108   ShadowView* impl = new ShadowView(downsampleWidthScale, downsampleHeightScale);
109
110   Dali::Toolkit::ShadowView handle = Dali::Toolkit::ShadowView(*impl);
111
112   // Second-phase init of the implementation
113   // This can only be done after the CustomActor connection has been made...
114   impl->Initialize();
115
116   return handle;
117 }
118
119 void ShadowView::SetShadowPlaneBackground(Actor shadowPlaneBackground)
120 {
121   mShadowPlaneBg = shadowPlaneBackground;
122
123   mShadowPlane = Actor::New();
124   mShadowPlane.SetProperty(Actor::Property::NAME, "SHADOW_PLANE");
125   mShadowPlane.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
126   mShadowPlane.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
127   Renderer   shadowRenderer = CreateRenderer(SHADER_SHADOW_VIEW_RENDER_SHADER_VERT, SHADER_SHADOW_VIEW_RENDER_SHADER_FRAG, Shader::Hint::OUTPUT_IS_TRANSPARENT, Uint16Pair(20, 20));
128   TextureSet textureSet     = shadowRenderer.GetTextures();
129   textureSet.SetTexture(0u, mOutputFrameBuffer.GetColorTexture());
130   mShadowPlane.AddRenderer(shadowRenderer);
131
132   SetShaderConstants();
133
134   // Rather than parent the shadow plane drawable and have constraints to move it to the same
135   // position, instead parent the shadow plane drawable on the shadow plane passed in.
136   mShadowPlaneBg.Add(mShadowPlane);
137   mShadowPlane.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
138   mShadowPlane.SetProperty(Actor::Property::POSITION_Z, 1.0f);
139
140   ConstrainCamera();
141
142   mShadowPlane.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
143
144   mBlurRootActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
145 }
146
147 void ShadowView::SetPointLight(Actor pointLight)
148 {
149   mPointLight = pointLight;
150
151   ConstrainCamera();
152 }
153
154 void ShadowView::SetPointLightFieldOfView(float fieldOfView)
155 {
156   mCameraActor.SetFieldOfView(fieldOfView);
157 }
158
159 void ShadowView::SetShadowColor(Vector4 color)
160 {
161   mCachedShadowColor       = color;
162   mCachedBackgroundColor.r = color.r;
163   mCachedBackgroundColor.g = color.g;
164   mCachedBackgroundColor.b = color.b;
165
166   if(mShadowPlane)
167   {
168     mShadowPlane.SetProperty(mShadowColorPropertyIndex, mCachedShadowColor);
169   }
170   if(mRenderSceneTask)
171   {
172     mRenderSceneTask.SetClearColor(mCachedBackgroundColor);
173   }
174 }
175
176 void ShadowView::Activate()
177 {
178   DALI_ASSERT_ALWAYS(Self().GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE) && "ShadowView should be on stage before calling Activate()\n");
179
180   // make sure resources are allocated and start the render tasks processing
181   CreateRenderTasks();
182 }
183
184 void ShadowView::Deactivate()
185 {
186   DALI_ASSERT_ALWAYS(Self().GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE) && "ShadowView should be on stage before calling Deactivate()\n")
187
188   // stop render tasks processing
189   // Note: render target resources are automatically freed since we set the Image::Unused flag
190   RemoveRenderTasks();
191 }
192
193 ///////////////////////////////////////////////////////////
194 //
195 // Private methods
196 //
197
198 void ShadowView::OnInitialize()
199 {
200   // root actor to parent all user added actors. Used as source actor for shadow render task.
201   mChildrenRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
202   mChildrenRoot.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
203
204   Vector2 stageSize = Stage::GetCurrent().GetSize();
205   mCameraActor      = CameraActor::New(stageSize);
206
207   mCameraActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
208
209   // Target is constrained to point at the shadow plane origin
210   mCameraActor.SetNearClippingPlane(1.0f);
211   mCameraActor.SetType(Dali::Camera::FREE_LOOK); // Camera orientation constrained to point at shadow plane world position
212   mCameraActor.SetProperty(Actor::Property::ORIENTATION, Quaternion(Radian(Degree(180)), Vector3::YAXIS));
213   mCameraActor.SetProperty(Actor::Property::POSITION, DEFAULT_LIGHT_POSITION);
214
215   // Create render targets needed for rendering from light's point of view
216   mSceneFromLightRenderTarget = FrameBuffer::New(stageSize.width, stageSize.height, FrameBuffer::Attachment::NONE);
217   Texture textureFromLight    = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(stageSize.width), unsigned(stageSize.height));
218   mSceneFromLightRenderTarget.AttachColorTexture(textureFromLight);
219
220   mOutputFrameBuffer    = FrameBuffer::New(stageSize.width * 0.5f, stageSize.height * 0.5f, FrameBuffer::Attachment::NONE);
221   Texture outputTexture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(stageSize.width * 0.5f), unsigned(stageSize.height * 0.5f));
222   mOutputFrameBuffer.AttachColorTexture(outputTexture);
223
224   //////////////////////////////////////////////////////
225   // Connect to actor tree
226
227   Self().Add(mChildrenRoot);
228   Stage::GetCurrent().Add(mCameraActor);
229
230   mBlurFilter.SetRefreshOnDemand(false);
231   mBlurFilter.SetInputTexture(mSceneFromLightRenderTarget.GetColorTexture());
232   mBlurFilter.SetOutputFrameBuffer(mOutputFrameBuffer);
233   mBlurFilter.SetSize(stageSize * 0.5f);
234   mBlurFilter.SetPixelFormat(Pixel::RGBA8888);
235
236   mBlurRootActor = Actor::New();
237   mBlurRootActor.SetProperty(Actor::Property::NAME, "BLUR_ROOT_ACTOR");
238
239   // Turn off inheritance to ensure filter renders properly
240   mBlurRootActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
241   mBlurRootActor.SetProperty(Actor::Property::INHERIT_POSITION, false);
242   mBlurRootActor.SetProperty(Actor::Property::INHERIT_ORIENTATION, false);
243   mBlurRootActor.SetProperty(Actor::Property::INHERIT_SCALE, false);
244   mBlurRootActor.SetProperty(Actor::Property::COLOR_MODE, USE_OWN_COLOR);
245
246   Self().Add(mBlurRootActor);
247
248   mBlurFilter.SetRootActor(mBlurRootActor);
249   mBlurFilter.SetBackgroundColor(Vector4::ZERO);
250
251   CustomActor self = Self();
252   // Register a property that the user can use to control the blur in the internal object
253   mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);
254
255   Constraint blurStrengthConstraint = Constraint::New<float>(mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint());
256   blurStrengthConstraint.AddSource(Source(self, mBlurStrengthPropertyIndex));
257   blurStrengthConstraint.Apply();
258
259   Self().SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::FILLER);
260 }
261
262 void ShadowView::OnChildAdd(Actor& child)
263 {
264   if(child != mChildrenRoot && child != mBlurRootActor)
265   {
266     mChildrenRoot.Add(child);
267   }
268
269   Control::OnChildAdd(child);
270 }
271
272 void ShadowView::OnChildRemove(Actor& child)
273 {
274   mChildrenRoot.Remove(child);
275
276   Control::OnChildRemove(child);
277 }
278
279 void ShadowView::ConstrainCamera()
280 {
281   if(mPointLight && mShadowPlane)
282   {
283     // Constrain camera to look directly at center of shadow plane. (mPointLight position
284     // is under control of application, can't use transform inheritance)
285
286     Constraint cameraOrientationConstraint = Constraint::New<Quaternion>(mCameraActor, Actor::Property::ORIENTATION, &LookAt);
287     cameraOrientationConstraint.AddSource(Source(mShadowPlane, Actor::Property::WORLD_POSITION));
288     cameraOrientationConstraint.AddSource(Source(mPointLight, Actor::Property::WORLD_POSITION));
289     cameraOrientationConstraint.AddSource(Source(mShadowPlane, Actor::Property::WORLD_ORIENTATION));
290     cameraOrientationConstraint.Apply();
291
292     Constraint pointLightPositionConstraint = Constraint::New<Vector3>(mCameraActor, Actor::Property::POSITION, EqualToConstraint());
293     pointLightPositionConstraint.AddSource(Source(mPointLight, Actor::Property::WORLD_POSITION));
294     pointLightPositionConstraint.Apply();
295   }
296 }
297
298 void ShadowView::CreateRenderTasks()
299 {
300   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
301
302   // We want the first task to render the scene from the light
303   mRenderSceneTask = taskList.CreateTask();
304
305   mRenderSceneTask.SetCameraActor(mCameraActor);
306   mRenderSceneTask.SetSourceActor(mChildrenRoot);
307   mRenderSceneTask.SetFrameBuffer(mSceneFromLightRenderTarget);
308   mRenderSceneTask.SetInputEnabled(false);
309   mRenderSceneTask.SetClearEnabled(true);
310
311   // background color for render task should be the shadow color, but with alpha 0
312   // we don't want to blend the edges of the content with a BLACK at alpha 0, but
313   // the same shadow color at alpha 0.
314   mRenderSceneTask.SetClearColor(mCachedBackgroundColor);
315
316   mBlurFilter.Enable();
317 }
318
319 void ShadowView::RemoveRenderTasks()
320 {
321   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
322
323   taskList.RemoveTask(mRenderSceneTask);
324   mRenderSceneTask.Reset();
325
326   mBlurFilter.Disable();
327 }
328
329 void ShadowView::SetShaderConstants()
330 {
331   Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowPlane.RegisterProperty(SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY);
332   Constraint      projectionMatrixConstraint               = Constraint::New<Dali::Matrix>(mShadowPlane, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint());
333   projectionMatrixConstraint.AddSource(Source(mCameraActor, CameraActor::Property::PROJECTION_MATRIX));
334   projectionMatrixConstraint.Apply();
335
336   Property::Index lightCameraViewMatrixPropertyIndex = mShadowPlane.RegisterProperty(SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY);
337   Constraint      viewMatrixConstraint               = Constraint::New<Dali::Matrix>(mShadowPlane, lightCameraViewMatrixPropertyIndex, EqualToConstraint());
338   viewMatrixConstraint.AddSource(Source(mCameraActor, CameraActor::Property::VIEW_MATRIX));
339   viewMatrixConstraint.Apply();
340
341   mShadowColorPropertyIndex = mShadowPlane.RegisterProperty(SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor);
342 }
343
344 } // namespace Internal
345
346 } // namespace Toolkit
347
348 } // namespace Dali