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