2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "shadow-view-impl.h"
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/integration-api/debug.h>
32 #include <dali-toolkit/internal/controls/shadow-view/shadow-view-impl.h>
33 #include <dali-toolkit/internal/filters/blur-two-pass-filter.h>
36 // pixel format / size - set from JSON
37 // aspect ratio property needs to be able to be constrained also for cameras. (now do-able)
38 // default near clip value
39 // mChildrenRoot Add()/Remove() overloads - better solution
42 /////////////////////////////////////////////////////////
43 // IMPLEMENTATION NOTES
45 // As the ShadowView actor changes size, the amount of pixels we need to blur changes. Therefore we need some way of doing this. However:-
46 // OnSetSize() does not get called when ShadowView object size is modified using a Constraint.
47 // 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
48 // OnSizeAnimation() to alter render target sizes.
49 // 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
50 // 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
51 // by using constraints relative to the ShadowView actor size.
69 return Toolkit::ShadowView::New();
72 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ShadowView, Toolkit::Control, Create )
73 DALI_TYPE_REGISTRATION_END()
75 const float BLUR_STRENGTH_DEFAULT = 1.0f;
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
80 const Vector4 DEFAULT_SHADOW_COLOR = Vector4(0.2f, 0.2f, 0.2f, 0.8f);
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";
88 const char* const RENDER_SHADOW_VERTEX_SOURCE =
89 " uniform mediump mat4 uLightCameraProjectionMatrix;\n"
90 " uniform mediump mat4 uLightCameraViewMatrix;\n"
94 " gl_Position = uProjection * uModelView * vec4(aPosition,1.0);\n"
95 " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vec4(aPosition,1.0);\n"
96 " vTexCoord = 0.5 + 0.5 * (textureCoords.xy/textureCoords.w);\n"
99 const char* const RENDER_SHADOW_FRAGMENT_SOURCE =
100 "uniform lowp vec4 uShadowColor;\n"
103 " lowp float alpha;\n"
104 " alpha = texture2D(sTexture, vec2(vTexCoord.x, vTexCoord.y)).a;\n"
105 " gl_FragColor = vec4(uShadowColor.rgb, uShadowColor.a * alpha);\n"
110 ShadowView::ShadowView( float downsampleWidthScale, float downsampleHeightScale )
111 : Control( CONTROL_BEHAVIOUR_NONE ),
112 mChildrenRoot(Actor::New()),
113 mCachedShadowColor(DEFAULT_SHADOW_COLOR),
114 mCachedBackgroundColor(DEFAULT_SHADOW_COLOR.r, DEFAULT_SHADOW_COLOR.g, DEFAULT_SHADOW_COLOR.b, 0.0f),
115 mBlurStrengthPropertyIndex(Property::INVALID_INDEX),
116 mShadowColorPropertyIndex(Property::INVALID_INDEX),
117 mDownsampleWidthScale(downsampleWidthScale),
118 mDownsampleHeightScale(downsampleHeightScale)
122 ShadowView::~ShadowView()
126 Toolkit::ShadowView ShadowView::New(float downsampleWidthScale, float downsampleHeightScale)
128 ShadowView* impl = new ShadowView(downsampleWidthScale, downsampleHeightScale);
130 Dali::Toolkit::ShadowView handle = Dali::Toolkit::ShadowView( *impl );
132 // Second-phase init of the implementation
133 // This can only be done after the CustomActor connection has been made...
139 /////////////////////////////////////////////////////////////
140 // for creating a subtree for all user added child actors.
141 // TODO: overloading Actor::Add()/Remove() not nice since breaks polymorphism. Need another method to pass ownership of added child actors to our internal actor root.
142 void ShadowView::Add(Actor child)
144 mChildrenRoot.Add(child);
147 void ShadowView::Remove(Actor child)
149 mChildrenRoot.Remove(child);
152 void ShadowView::SetShadowPlane(Actor shadowPlane)
154 mShadowPlaneBg = shadowPlane;
156 mShadowPlane = ImageActor::New();
157 mShadowPlane.SetName( "SHADOW_PLANE" );
158 mShadowPlane.SetParentOrigin(ParentOrigin::CENTER);
159 mShadowPlane.SetAnchorPoint(AnchorPoint::CENTER);
161 mShadowPlane.SetImage(mOutputImage);
162 mShadowPlane.SetShaderEffect(mShadowRenderShader);
164 // Rather than parent the shadow plane drawable and have constraints to move it to the same
165 // position, instead parent the shadow plane drawable on the shadow plane passed in.
166 mShadowPlaneBg.Add(mShadowPlane);
167 mShadowPlane.SetParentOrigin(ParentOrigin::CENTER);
168 mShadowPlane.SetZ(1.0f);
172 mShadowPlane.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
174 mBlurRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
177 void ShadowView::SetPointLight(Actor pointLight)
179 mPointLight = pointLight;
184 void ShadowView::SetPointLightFieldOfView(float fieldOfView)
186 mCameraActor.SetFieldOfView(fieldOfView);
189 void ShadowView::SetShadowColor(Vector4 color)
191 mCachedShadowColor = color;
192 mCachedBackgroundColor.r = color.r;
193 mCachedBackgroundColor.g = color.g;
194 mCachedBackgroundColor.b = color.b;
196 Self().SetProperty( mShadowColorPropertyIndex, mCachedShadowColor );
199 mRenderSceneTask.SetClearColor( mCachedBackgroundColor );
203 void ShadowView::Activate()
205 DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Activate()\n" );
207 // make sure resources are allocated and start the render tasks processing
211 void ShadowView::Deactivate()
213 DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Deactivate()\n" )
215 // stop render tasks processing
216 // Note: render target resources are automatically freed since we set the Image::Unused flag
220 ///////////////////////////////////////////////////////////
225 void ShadowView::OnInitialize()
227 // root actor to parent all user added actors. Used as source actor for shadow render task.
228 mChildrenRoot.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
229 mChildrenRoot.SetRelayoutEnabled( true );
230 mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
232 Vector2 stageSize = Stage::GetCurrent().GetSize();
233 mCameraActor = CameraActor::New(stageSize);
235 mCameraActor.SetParentOrigin( ParentOrigin::CENTER );
237 // Target is constrained to point at the shadow plane origin
238 mCameraActor.SetNearClippingPlane( 1.0f );
239 mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position
240 mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS);
241 mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION);
243 mShadowRenderShader = ShaderEffect::New( RENDER_SHADOW_VERTEX_SOURCE, RENDER_SHADOW_FRAGMENT_SOURCE,
244 Dali::GeometryType( GEOMETRY_TYPE_IMAGE ),
245 ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING ));
247 // Create render targets needed for rendering from light's point of view
248 mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 );
250 mOutputImage = FrameBufferImage::New( stageSize.width * 0.5f, stageSize.height * 0.5f, Pixel::RGBA8888 );
252 //////////////////////////////////////////////////////
253 // Connect to actor tree
255 Self().Add( mChildrenRoot );
256 Stage::GetCurrent().Add( mCameraActor );
258 mBlurFilter.SetRefreshOnDemand(false);
259 mBlurFilter.SetInputImage(mSceneFromLightRenderTarget);
260 mBlurFilter.SetOutputImage(mOutputImage);
261 mBlurFilter.SetSize(stageSize * 0.5f);
262 mBlurFilter.SetPixelFormat(Pixel::RGBA8888);
264 mBlurRootActor = Actor::New();
265 mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" );
266 mBlurRootActor.SetRelayoutEnabled( true );
268 // Turn off inheritance to ensure filter renders properly
269 mBlurRootActor.SetPositionInheritanceMode(USE_PARENT_POSITION);
270 mBlurRootActor.SetInheritOrientation(false);
271 mBlurRootActor.SetInheritScale(false);
272 mBlurRootActor.SetColorMode(USE_OWN_COLOR);
274 Self().Add(mBlurRootActor);
276 mBlurFilter.SetRootActor(mBlurRootActor);
277 mBlurFilter.SetBackgroundColor(Vector4::ZERO);
279 SetShaderConstants();
282 void ShadowView::OnSizeSet(const Vector3& targetSize)
286 void ShadowView::OnStageConnection()
288 // TODO: can't call this here, since SetImage() calls fail to connect images to stage, since parent chain not fully on stage yet
289 // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate()
293 void ShadowView::OnStageDisconnection()
295 // TODO: can't call this here, since SetImage() calls fails similarly to above
296 // Need to fix the stage connection so this callback can be used arbitrarily. At that point we can simplify the API by removing the need for Activate() / Deactivate()
300 void ShadowView::ConstrainCamera()
302 if( mPointLight && mShadowPlane )
304 // Constrain camera to look directly at center of shadow plane. (mPointLight position
305 // is under control of application, can't use transform inheritance)
307 Constraint cameraOrientationConstraint = Constraint::New<Quaternion> ( mCameraActor, Actor::Property::ORIENTATION, &LookAt );
308 cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_POSITION ) );
309 cameraOrientationConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) );
310 cameraOrientationConstraint.AddSource( Source( mShadowPlane, Actor::Property::WORLD_ORIENTATION ) );
311 cameraOrientationConstraint.Apply();
313 Constraint pointLightPositionConstraint = Constraint::New<Vector3>( mCameraActor, Actor::Property::POSITION, EqualToConstraint() );
314 pointLightPositionConstraint.AddSource( Source( mPointLight, Actor::Property::WORLD_POSITION ) );
315 pointLightPositionConstraint.Apply();
319 void ShadowView::CreateRenderTasks()
321 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
323 // We want the first task to render the scene from the light
324 mRenderSceneTask = taskList.CreateTask();
326 mRenderSceneTask.SetCameraActor( mCameraActor );
327 mRenderSceneTask.SetSourceActor( mChildrenRoot );
328 mRenderSceneTask.SetTargetFrameBuffer( mSceneFromLightRenderTarget );
329 mRenderSceneTask.SetInputEnabled( false );
330 mRenderSceneTask.SetClearEnabled( true );
332 // background color for render task should be the shadow color, but with alpha 0
333 // we don't want to blend the edges of the content with a BLACK at alpha 0, but
334 // the same shadow color at alpha 0.
335 mRenderSceneTask.SetClearColor( mCachedBackgroundColor );
337 mBlurFilter.Enable();
340 void ShadowView::RemoveRenderTasks()
342 RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
344 taskList.RemoveTask(mRenderSceneTask);
345 mRenderSceneTask.Reset();
347 mBlurFilter.Disable();
350 void ShadowView::SetShaderConstants()
352 CustomActor self = Self();
354 mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
355 mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY );
356 mShadowRenderShader.SetUniform( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor );
358 Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME);
359 Property::Index lightCameraViewMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME);
361 Constraint projectionMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowRenderShader, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() );
362 projectionMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::PROJECTION_MATRIX ) );
364 Constraint viewMatrixConstraint = Constraint::New<Dali::Matrix>( mShadowRenderShader, lightCameraViewMatrixPropertyIndex, EqualToConstraint() );
365 viewMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::VIEW_MATRIX ) );
367 projectionMatrixConstraint.Apply();
368 viewMatrixConstraint.Apply();
370 // Register a property that the user can use to control the blur in the internal object
371 mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT);
373 Constraint blurStrengthConstraint = Constraint::New<float>( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() );
374 blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) );
375 blurStrengthConstraint.Apply();
377 // Register a property that the user can use to control the color of the shadow.
378 Property::Index index = mShadowRenderShader.GetPropertyIndex(SHADER_SHADOW_COLOR_PROPERTY_NAME);
379 mShadowColorPropertyIndex = self.RegisterProperty(SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor);
381 Constraint shadowRenderShaderConstraint = Constraint::New<Dali::Vector4>( mShadowRenderShader, index, EqualToConstraint() );
382 shadowRenderShaderConstraint.AddSource( Source( self, mShadowColorPropertyIndex ) );
383 shadowRenderShaderConstraint.Apply();
386 } // namespace Internal
388 } // namespace Toolkit