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