X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fshadow-view%2Fshadow-view-impl.cpp;h=217101b795a082a8f1bdd7ece1da454640ffb0dd;hp=67b5262839aab4d4344dd02e2a1d96adfe8f9611;hb=b1e8521ad77e7b4e62b59613b2edef64429130e9;hpb=402784aceba9171e57ffca8769ca46ef8aebcd2f diff --git a/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp b/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp index 67b5262..217101b 100644 --- a/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp +++ b/dali-toolkit/internal/controls/shadow-view/shadow-view-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,13 +22,16 @@ #include #include #include -#include +#include #include #include #include +#include #include // INTERNAL INCLUDES +#include +#include #include #include @@ -36,7 +39,6 @@ // pixel format / size - set from JSON // aspect ratio property needs to be able to be constrained also for cameras. (now do-able) // default near clip value -// mChildrenRoot Add()/Remove() overloads - better solution ///////////////////////////////////////////////////////// @@ -86,18 +88,30 @@ const char* const BLUR_STRENGTH_PROPERTY_NAME = "BlurStrengthProperty"; const char* const SHADOW_COLOR_PROPERTY_NAME = "ShadowColorProperty"; const char* const RENDER_SHADOW_VERTEX_SOURCE = + + " attribute mediump vec2 aPosition;\n" + " uniform mediump mat4 uMvpMatrix;\n" + " uniform mediump mat4 uModelMatrix;\n" + " uniform vec3 uSize;\n" + " varying vec2 vTexCoord;\n" + " uniform mediump mat4 uLightCameraProjectionMatrix;\n" " uniform mediump mat4 uLightCameraViewMatrix;\n" "\n" "void main()\n" "{\n" - " gl_Position = uProjection * uModelView * vec4(aPosition,1.0);\n" - " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vec4(aPosition,1.0);\n" + " mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n" + " vertexPosition.xyz *= uSize;\n" + " gl_Position = uMvpMatrix * vertexPosition;\n" + " vec4 textureCoords = uLightCameraProjectionMatrix * uLightCameraViewMatrix * uModelMatrix * vertexPosition;\n" " vTexCoord = 0.5 + 0.5 * (textureCoords.xy/textureCoords.w);\n" "}\n"; const char* const RENDER_SHADOW_FRAGMENT_SOURCE = + "varying mediump vec2 vTexCoord;\n" "uniform lowp vec4 uShadowColor;\n" + "uniform sampler2D sTexture;\n" + "void main()\n" "{\n" " lowp float alpha;\n" @@ -108,7 +122,7 @@ const char* const RENDER_SHADOW_FRAGMENT_SOURCE = } // namespace ShadowView::ShadowView( float downsampleWidthScale, float downsampleHeightScale ) -: Control( CONTROL_BEHAVIOUR_NONE ), +: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ), mChildrenRoot(Actor::New()), mCachedShadowColor(DEFAULT_SHADOW_COLOR), mCachedBackgroundColor(DEFAULT_SHADOW_COLOR.r, DEFAULT_SHADOW_COLOR.g, DEFAULT_SHADOW_COLOR.b, 0.0f), @@ -136,42 +150,32 @@ Toolkit::ShadowView ShadowView::New(float downsampleWidthScale, float downsample return handle; } -///////////////////////////////////////////////////////////// -// for creating a subtree for all user added child actors. -// 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. -void ShadowView::Add(Actor child) +void ShadowView::SetShadowPlaneBackground(Actor shadowPlaneBackground) { - mChildrenRoot.Add(child); -} + mShadowPlaneBg = shadowPlaneBackground; -void ShadowView::Remove(Actor child) -{ - mChildrenRoot.Remove(child); -} + mShadowPlane = Actor::New(); + mShadowPlane.SetProperty( Actor::Property::NAME, "SHADOW_PLANE" ); + mShadowPlane.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + mShadowPlane.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER ); + Renderer shadowRenderer = CreateRenderer( RENDER_SHADOW_VERTEX_SOURCE, RENDER_SHADOW_FRAGMENT_SOURCE, Shader::Hint::OUTPUT_IS_TRANSPARENT, Uint16Pair(20,20) ); + TextureSet textureSet = shadowRenderer.GetTextures(); + textureSet.SetTexture( 0u, mOutputFrameBuffer.GetColorTexture() ); + mShadowPlane.AddRenderer( shadowRenderer ); -void ShadowView::SetShadowPlane(Actor shadowPlane) -{ - mShadowPlaneBg = shadowPlane; - - mShadowPlane = ImageActor::New(); - mShadowPlane.SetName( "SHADOW_PLANE" ); - mShadowPlane.SetParentOrigin(ParentOrigin::CENTER); - mShadowPlane.SetAnchorPoint(AnchorPoint::CENTER); - - mShadowPlane.SetImage(mOutputImage); - mShadowPlane.SetShaderEffect(mShadowRenderShader); + SetShaderConstants(); // Rather than parent the shadow plane drawable and have constraints to move it to the same // position, instead parent the shadow plane drawable on the shadow plane passed in. - mShadowPlaneBg.Add(mShadowPlane); - mShadowPlane.SetParentOrigin(ParentOrigin::CENTER); - mShadowPlane.SetZ(1.0f); + mShadowPlaneBg.Add( mShadowPlane ); + mShadowPlane.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + mShadowPlane.SetProperty( Actor::Property::POSITION_Z, 1.0f ); ConstrainCamera(); - mShadowPlane.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS ); + mShadowPlane.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); - mBlurRootActor.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS ); + mBlurRootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); } void ShadowView::SetPointLight(Actor pointLight) @@ -193,7 +197,10 @@ void ShadowView::SetShadowColor(Vector4 color) mCachedBackgroundColor.g = color.g; mCachedBackgroundColor.b = color.b; - Self().SetProperty( mShadowColorPropertyIndex, mCachedShadowColor ); + if( mShadowPlane ) + { + mShadowPlane.SetProperty( mShadowColorPropertyIndex, mCachedShadowColor ); + } if(mRenderSceneTask) { mRenderSceneTask.SetClearColor( mCachedBackgroundColor ); @@ -202,7 +209,7 @@ void ShadowView::SetShadowColor(Vector4 color) void ShadowView::Activate() { - DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Activate()\n" ); + DALI_ASSERT_ALWAYS( Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) && "ShadowView should be on stage before calling Activate()\n" ); // make sure resources are allocated and start the render tasks processing CreateRenderTasks(); @@ -210,7 +217,7 @@ void ShadowView::Activate() void ShadowView::Deactivate() { - DALI_ASSERT_ALWAYS( Self().OnStage() && "ShadowView should be on stage before calling Deactivate()\n" ) + DALI_ASSERT_ALWAYS( Self().GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) && "ShadowView should be on stage before calling Deactivate()\n" ) // stop render tasks processing // Note: render target resources are automatically freed since we set the Image::Unused flag @@ -225,29 +232,28 @@ void ShadowView::Deactivate() void ShadowView::OnInitialize() { // root actor to parent all user added actors. Used as source actor for shadow render task. - mChildrenRoot.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION ); - mChildrenRoot.SetRelayoutEnabled( true ); - mChildrenRoot.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS ); + mChildrenRoot.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + mChildrenRoot.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); Vector2 stageSize = Stage::GetCurrent().GetSize(); mCameraActor = CameraActor::New(stageSize); - mCameraActor.SetParentOrigin( ParentOrigin::CENTER ); + mCameraActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); // Target is constrained to point at the shadow plane origin mCameraActor.SetNearClippingPlane( 1.0f ); mCameraActor.SetType( Dali::Camera::FREE_LOOK ); // Camera orientation constrained to point at shadow plane world position - mCameraActor.SetOrientation(Radian(Degree(180)), Vector3::YAXIS); - mCameraActor.SetPosition(DEFAULT_LIGHT_POSITION); - - mShadowRenderShader = ShaderEffect::New( RENDER_SHADOW_VERTEX_SOURCE, RENDER_SHADOW_FRAGMENT_SOURCE, - Dali::GeometryType( GEOMETRY_TYPE_IMAGE ), - ShaderEffect::GeometryHints( ShaderEffect::HINT_GRID | ShaderEffect::HINT_BLENDING )); + mCameraActor.SetProperty( Actor::Property::ORIENTATION, Quaternion(Radian(Degree(180)), Vector3::YAXIS) ); + mCameraActor.SetProperty( Actor::Property::POSITION, DEFAULT_LIGHT_POSITION ); // Create render targets needed for rendering from light's point of view - mSceneFromLightRenderTarget = FrameBufferImage::New( stageSize.width, stageSize.height, Pixel::RGBA8888 ); + mSceneFromLightRenderTarget = FrameBuffer::New( stageSize.width, stageSize.height, FrameBuffer::Attachment::NONE ); + Texture textureFromLight = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(stageSize.width), unsigned(stageSize.height) ); + mSceneFromLightRenderTarget.AttachColorTexture( textureFromLight ); - mOutputImage = FrameBufferImage::New( stageSize.width * 0.5f, stageSize.height * 0.5f, Pixel::RGBA8888 ); + mOutputFrameBuffer = FrameBuffer::New( stageSize.width * 0.5f, stageSize.height * 0.5f, FrameBuffer::Attachment::NONE ); + Texture outputTexture = Texture::New( TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(stageSize.width * 0.5f), unsigned(stageSize.height * 0.5f) ); + mOutputFrameBuffer.AttachColorTexture( outputTexture ); ////////////////////////////////////////////////////// // Connect to actor tree @@ -255,46 +261,51 @@ void ShadowView::OnInitialize() Self().Add( mChildrenRoot ); Stage::GetCurrent().Add( mCameraActor ); - mBlurFilter.SetRefreshOnDemand(false); - mBlurFilter.SetInputImage(mSceneFromLightRenderTarget); - mBlurFilter.SetOutputImage(mOutputImage); - mBlurFilter.SetSize(stageSize * 0.5f); - mBlurFilter.SetPixelFormat(Pixel::RGBA8888); + mBlurFilter.SetRefreshOnDemand( false ); + mBlurFilter.SetInputTexture( mSceneFromLightRenderTarget.GetColorTexture() ); + mBlurFilter.SetOutputFrameBuffer( mOutputFrameBuffer ); + mBlurFilter.SetSize( stageSize * 0.5f ); + mBlurFilter.SetPixelFormat( Pixel::RGBA8888 ); mBlurRootActor = Actor::New(); - mBlurRootActor.SetName( "BLUR_ROOT_ACTOR" ); - mBlurRootActor.SetRelayoutEnabled( true ); + mBlurRootActor.SetProperty( Actor::Property::NAME, "BLUR_ROOT_ACTOR" ); // Turn off inheritance to ensure filter renders properly - mBlurRootActor.SetPositionInheritanceMode(USE_PARENT_POSITION); - mBlurRootActor.SetInheritOrientation(false); - mBlurRootActor.SetInheritScale(false); - mBlurRootActor.SetColorMode(USE_OWN_COLOR); + mBlurRootActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + mBlurRootActor.SetProperty( Actor::Property::INHERIT_POSITION, false ); + mBlurRootActor.SetProperty( Actor::Property::INHERIT_ORIENTATION, false ); + mBlurRootActor.SetProperty( Actor::Property::INHERIT_SCALE, false ); + mBlurRootActor.SetProperty( Actor::Property::COLOR_MODE, USE_OWN_COLOR ); - Self().Add(mBlurRootActor); + Self().Add( mBlurRootActor ); mBlurFilter.SetRootActor(mBlurRootActor); mBlurFilter.SetBackgroundColor(Vector4::ZERO); - SetShaderConstants(); -} + CustomActor self = Self(); + // Register a property that the user can use to control the blur in the internal object + mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT); -void ShadowView::OnSizeSet(const Vector3& targetSize) -{ + Constraint blurStrengthConstraint = Constraint::New( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() ); + blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) ); + blurStrengthConstraint.Apply(); } -void ShadowView::OnStageConnection() +void ShadowView::OnChildAdd( Actor& child ) { - // TODO: can't call this here, since SetImage() calls fail to connect images to stage, since parent chain not fully on stage yet - // 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() - //Activate(); + if( child != mChildrenRoot && child != mBlurRootActor) + { + mChildrenRoot.Add( child ); + } + + Control::OnChildAdd( child ); } -void ShadowView::OnStageDisconnection() +void ShadowView::OnChildRemove( Actor& child ) { - // TODO: can't call this here, since SetImage() calls fails similarly to above - // 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() - //Deactivate(); + mChildrenRoot.Remove( child ); + + Control::OnChildRemove( child ); } void ShadowView::ConstrainCamera() @@ -325,7 +336,7 @@ void ShadowView::CreateRenderTasks() mRenderSceneTask.SetCameraActor( mCameraActor ); mRenderSceneTask.SetSourceActor( mChildrenRoot ); - mRenderSceneTask.SetTargetFrameBuffer( mSceneFromLightRenderTarget ); + mRenderSceneTask.SetFrameBuffer( mSceneFromLightRenderTarget ); mRenderSceneTask.SetInputEnabled( false ); mRenderSceneTask.SetClearEnabled( true ); @@ -349,38 +360,17 @@ void ShadowView::RemoveRenderTasks() void ShadowView::SetShaderConstants() { - CustomActor self = Self(); - - mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); - mShadowRenderShader.SetUniform( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); - mShadowRenderShader.SetUniform( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor ); - - Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME); - Property::Index lightCameraViewMatrixPropertyIndex = mShadowRenderShader.GetPropertyIndex(SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME); - - Constraint projectionMatrixConstraint = Constraint::New( mShadowRenderShader, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() ); + Property::Index lightCameraProjectionMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_PROJECTION_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); + Constraint projectionMatrixConstraint = Constraint::New( mShadowPlane, lightCameraProjectionMatrixPropertyIndex, EqualToConstraint() ); projectionMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::PROJECTION_MATRIX ) ); + projectionMatrixConstraint.Apply(); - Constraint viewMatrixConstraint = Constraint::New( mShadowRenderShader, lightCameraViewMatrixPropertyIndex, EqualToConstraint() ); + Property::Index lightCameraViewMatrixPropertyIndex = mShadowPlane.RegisterProperty( SHADER_LIGHT_CAMERA_VIEW_MATRIX_PROPERTY_NAME, Matrix::IDENTITY ); + Constraint viewMatrixConstraint = Constraint::New( mShadowPlane, lightCameraViewMatrixPropertyIndex, EqualToConstraint() ); viewMatrixConstraint.AddSource( Source( mCameraActor, CameraActor::Property::VIEW_MATRIX ) ); - - projectionMatrixConstraint.Apply(); viewMatrixConstraint.Apply(); - // Register a property that the user can use to control the blur in the internal object - mBlurStrengthPropertyIndex = self.RegisterProperty(BLUR_STRENGTH_PROPERTY_NAME, BLUR_STRENGTH_DEFAULT); - - Constraint blurStrengthConstraint = Constraint::New( mBlurFilter.GetHandleForAnimateBlurStrength(), mBlurFilter.GetBlurStrengthPropertyIndex(), EqualToConstraint() ); - blurStrengthConstraint.AddSource( Source( self, mBlurStrengthPropertyIndex) ); - blurStrengthConstraint.Apply(); - - // Register a property that the user can use to control the color of the shadow. - Property::Index index = mShadowRenderShader.GetPropertyIndex(SHADER_SHADOW_COLOR_PROPERTY_NAME); - mShadowColorPropertyIndex = self.RegisterProperty(SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor); - - Constraint shadowRenderShaderConstraint = Constraint::New( mShadowRenderShader, index, EqualToConstraint() ); - shadowRenderShaderConstraint.AddSource( Source( self, mShadowColorPropertyIndex ) ); - shadowRenderShaderConstraint.Apply(); + mShadowColorPropertyIndex = mShadowPlane.RegisterProperty( SHADER_SHADOW_COLOR_PROPERTY_NAME, mCachedShadowColor ); } } // namespace Internal