-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2017 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
// CLASS HEADER
#include "emboss-filter.h"
// EXTERNAL INCLUDES
#include <sstream>
+#include <dali/public-api/animation/constraints.h>
+#include <dali/public-api/common/stage.h>
+#include <dali/public-api/object/property-map.h>
+#include <dali/public-api/render-tasks/render-task-list.h>
+#include <dali/public-api/rendering/renderer.h>
// INTERNAL INCLUDES
+#include <dali-toolkit/internal/controls/control/control-renderers.h>
namespace Dali
{
namespace
{
-const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
-
-const char* EMBOSS_FRAGMENT_SOURCE1 =
+const char* EMBOSS_FRAGMENT_SOURCE =
{
+ "precision highp float;\n"
+ "varying mediump vec2 vTexCoord;\n"
+ "uniform sampler2D sTexture;\n"
"uniform vec2 uTexScale;\n"
+ "uniform vec3 uCoefficient;\n"
"\n"
"void main()\n"
"{\n"
- " vec4 color;\n"
- "# ifdef DEBUG_RENDER\n"
- " if( vTexCoord.s < 0.495 )\n"
- " {\n"
- "# endif //def DEBUG_RENDER\n"
- " color = 2.0 * texture2D( sTexture, vTexCoord + vec2(0.0, -uTexScale.y) );\n"
- " color += -1.0 * texture2D( sTexture, vTexCoord );\n"
- " color += -1.0 * texture2D( sTexture, vTexCoord + vec2(0.0, uTexScale.y) );\n"
- "# ifdef DEBUG_RENDER\n"
- " }\n"
- " else if( vTexCoord.s > 0.505 )\n"
- " {\n"
- " color = texture2D( sTexture, vTexCoord );\n"
- " }\n"
- " else\n"
- " {\n"
- " color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
- " }\n"
- "# endif //def DEBUG_RENDER\n"
- " gl_FragColor = uColor * color;\n"
+ " vec4 color = uCoefficient.x * texture2D( sTexture, vTexCoord + vec2(0.0, -uTexScale.y) );\n"
+ " color += uCoefficient.y * texture2D( sTexture, vTexCoord );\n"
+ " color += uCoefficient.z * texture2D( sTexture, vTexCoord + vec2(0.0, uTexScale.y) );\n"
+ " gl_FragColor = color;\n"
"}\n"
};
-const char* EMBOSS_FRAGMENT_SOURCE2 =
+const char* const COMPOSITE_FRAGMENT_SOURCE =
{
- "uniform vec2 uTexScale;\n"
- "\n"
- "void main()\n"
- "{\n"
- " vec4 color;\n"
- "# ifdef DEBUG_RENDER\n"
- " if( vTexCoord.s < 0.495 )\n"
- " {\n"
- "# endif //def DEBUG_RENDER\n"
- " color = -1.0 * texture2D( sTexture, vTexCoord + vec2(0.0, -uTexScale.y) );\n"
- " color += -1.0 * texture2D( sTexture, vTexCoord );\n"
- " color += 2.0 * texture2D( sTexture, vTexCoord + vec2(0.0, uTexScale.y) );\n"
- "# ifdef DEBUG_RENDER\n"
- " }\n"
- " else if( vTexCoord.s > 0.505 )\n"
- " {\n"
- " color = texture2D( sTexture, vTexCoord );\n"
- " }\n"
- " else\n"
- " {\n"
- " color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
- " }\n"
- "# endif //def DEBUG_RENDER\n"
- " gl_FragColor = uColor * color;\n"
- "}\n"
+ "varying mediump vec2 vTexCoord;\n"
+ "uniform sampler2D sTexture;\n"
+ "uniform lowp vec4 uEffectColor;\n"
+ "void main()\n"
+ "{\n"
+ " gl_FragColor = uEffectColor;\n"
+ " gl_FragColor.a *= texture2D( sTexture, vTexCoord).a;\n"
+ "}\n"
};
-const char* const COMPOSITE_FRAGMENT_SOURCE =
- "void main()\n"
- "{\n"
- " gl_FragColor = uColor;\n"
- " gl_FragColor.a *= texture2D( sTexture, vTexCoord).a;\n"
- "}\n";
+const char* const TEX_SCALE_UNIFORM_NAME( "uTexScale" );
+const char* const COEFFICIENT_UNIFORM_NAME( "uCoefficient" );
+const char* const COLOR_UNIFORM_NAME( "uEffectColor" );
} // namespace
void EmbossFilter::Enable()
{
- // Create camera
- mCameraActor = CameraActor::New();
- mCameraActor.SetParentOrigin(ParentOrigin::CENTER);
+ mFrameBufferForEmboss1 = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
+ Texture texture1 = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
+ mFrameBufferForEmboss1.AttachColorTexture( texture1 );
- mImageForEmboss1 = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
- mImageForEmboss2 = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
+ mFrameBufferForEmboss2 = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
+ Texture texture2 = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
+ mFrameBufferForEmboss2.AttachColorTexture( texture2 );
// create actor to render input with applied emboss effect
- mActorForInput1 = ImageActor::New( mInputImage );
+ mActorForInput1 = Actor::New();
mActorForInput1.SetParentOrigin( ParentOrigin::CENTER );
- mActorForInput1.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
- mActorForInput1.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
- mActorForInput1.SetColor( Color::WHITE );
+ mActorForInput1.SetSize(mTargetSize);
+ Vector2 textureScale( 1.5f/mTargetSize.width, 1.5f/mTargetSize.height);
+ mActorForInput1.RegisterProperty( TEX_SCALE_UNIFORM_NAME, textureScale );
+ mActorForInput1.RegisterProperty( COEFFICIENT_UNIFORM_NAME, Vector3( 2.f, -1.f, -1.f ) );
+ // set EMBOSS custom shader
+ Renderer renderer1 = CreateRenderer( BASIC_VERTEX_SOURCE, EMBOSS_FRAGMENT_SOURCE );
+ SetRendererTexture( renderer1, mInputTexture );
+ mActorForInput1.AddRenderer( renderer1 );
+ mRootActor.Add( mActorForInput1 );
- mActorForInput2 = ImageActor::New( mInputImage );
+ mActorForInput2 = Actor::New();
mActorForInput2.SetParentOrigin( ParentOrigin::CENTER );
- mActorForInput2.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
- mActorForInput2.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
- mActorForInput2.SetColor( Color::WHITE );
-
- mActorForEmboss1 = ImageActor::New( mImageForEmboss1 );
- mActorForEmboss1.SetParentOrigin( ParentOrigin::CENTER );
- mActorForEmboss1.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
- mActorForEmboss1.SetColor( Color::BLACK );
- mActorForEmboss1.SetShaderEffect( ShaderEffect::New( "", COMPOSITE_FRAGMENT_SOURCE ) );
-
- mActorForEmboss2 = ImageActor::New( mImageForEmboss2 );
- mActorForEmboss2.SetParentOrigin( ParentOrigin::CENTER );
- mActorForEmboss2.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
- mActorForEmboss2.SetColor( Color::WHITE );
- mActorForEmboss2.SetShaderEffect( ShaderEffect::New( "", COMPOSITE_FRAGMENT_SOURCE ) );
+ mActorForInput2.SetSize(mTargetSize);
+ mActorForInput2.RegisterProperty( TEX_SCALE_UNIFORM_NAME, textureScale );
+ mActorForInput2.RegisterProperty( COEFFICIENT_UNIFORM_NAME, Vector3( -1.f, -1.f, 2.f ) );
+ // set EMBOSS custom shader
+ Renderer renderer2 = CreateRenderer( BASIC_VERTEX_SOURCE, EMBOSS_FRAGMENT_SOURCE );
+ SetRendererTexture( renderer2, mInputTexture );
+ mActorForInput2.AddRenderer( renderer2 );
+ mRootActor.Add( mActorForInput2 );
mActorForComposite = Actor::New();
mActorForComposite.SetParentOrigin( ParentOrigin::CENTER );
- mActorForComposite.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
- mActorForComposite.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
+ mActorForComposite.SetSize(mTargetSize);
+ mActorForComposite.SetColor( Color::BLACK );
- // create custom shader effect
- std::ostringstream embossFragmentSource1;
- if( mDebugRender )
- {
- embossFragmentSource1 << "#define DEBUG_RENDER\n";
- }
- embossFragmentSource1 << EMBOSS_FRAGMENT_SOURCE1;
- ShaderEffect effect1 = ShaderEffect::New( "", embossFragmentSource1.str() );
- mActorForInput1.SetShaderEffect( effect1 );
- effect1.SetUniform( "uTexScale", Vector2( 1.0f/mTargetSize.width, 1.0f/mTargetSize.height) * 1.5f );
+ mRootActor.Add( mActorForComposite );
- std::ostringstream embossFragmentSource2;
- if( mDebugRender )
- {
- embossFragmentSource2 << "#define DEBUG_RENDER\n";
- }
- embossFragmentSource2 << EMBOSS_FRAGMENT_SOURCE2;
- ShaderEffect effect2 = ShaderEffect::New( "", embossFragmentSource2.str() );
- mActorForInput2.SetShaderEffect( effect2 );
- effect2.SetUniform( "uTexScale", Vector2( 1.0f/mTargetSize.width, 1.0f/mTargetSize.height) * 1.5f );
+ mRendererForEmboss1 = CreateRenderer( BASIC_VERTEX_SOURCE, COMPOSITE_FRAGMENT_SOURCE );
+ SetRendererTexture( mRendererForEmboss1, mFrameBufferForEmboss1 );
+ mRendererForEmboss1.RegisterProperty( COLOR_UNIFORM_NAME, Color::BLACK );
+ mActorForComposite.AddRenderer( mRendererForEmboss1 );
+
+ mRendererForEmboss2 = CreateRenderer( BASIC_VERTEX_SOURCE, COMPOSITE_FRAGMENT_SOURCE );
+ SetRendererTexture( mRendererForEmboss2, mFrameBufferForEmboss2 );
+ mRendererForEmboss2.RegisterProperty( COLOR_UNIFORM_NAME, Color::WHITE );
+ mActorForComposite.AddRenderer( mRendererForEmboss2 );
SetupCamera();
CreateRenderTasks();
-
- mRootActor.Add( mActorForInput1 );
- mRootActor.Add( mActorForInput2 );
- mRootActor.Add( mActorForComposite );
- mActorForComposite.Add( mActorForEmboss1 );
- mActorForComposite.Add( mActorForEmboss2 );
- mRootActor.Add( mCameraActor );
}
void EmbossFilter::Disable()
if( mActorForComposite )
{
+ mActorForComposite.RemoveRenderer( mRendererForEmboss1 );
+ mRendererForEmboss1.Reset();
+
+ mActorForComposite.RemoveRenderer( mRendererForEmboss2 );
+ mRendererForEmboss2.Reset();
+
mRootActor.Remove( mActorForComposite );
mActorForComposite.Reset();
- mActorForEmboss1.Reset();
- mActorForEmboss2.Reset();
}
RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
taskList.RemoveTask(mRenderTaskForEmboss2);
}
+ if( mRenderTaskForOutput )
+ {
+ taskList.RemoveTask( mRenderTaskForOutput );
+ }
+
mRootActor.Reset();
}
}
}
}
-void EmbossFilter::SetupCamera()
+void EmbossFilter::SetSize( const Vector2& size )
{
- // Create and place a camera for the embossing render, corresponding to its render target size
- mCameraActor.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
- mCameraActor.SetNearClippingPlane(1.0f);
- mCameraActor.SetAspectRatio(mTargetSize.width / mTargetSize.height);
- mCameraActor.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
- mCameraActor.SetRotation(Quaternion(M_PI, Vector3::YAXIS));
- mCameraActor.SetPosition(0.0f, 0.0f, ((mTargetSize.height * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
+ mTargetSize = size;
+ if( mActorForInput1 )
+ {
+ mActorForInput1.SetSize(mTargetSize);
+ }
+ if( mActorForInput2 )
+ {
+ mActorForInput2.SetSize(mTargetSize);
+ }
+ if( mActorForComposite )
+ {
+ mActorForComposite.SetSize(mTargetSize);
+ }
}
void EmbossFilter::CreateRenderTasks()
mRenderTaskForEmboss1.SetInputEnabled( false );
mRenderTaskForEmboss1.SetClearColor( Vector4( 0.0f, 0.0f, 0.0f, 0.0f ) );
mRenderTaskForEmboss1.SetClearEnabled( true );
- mRenderTaskForEmboss1.SetTargetFrameBuffer( mImageForEmboss1 );
+ mRenderTaskForEmboss1.SetFrameBuffer( mFrameBufferForEmboss1 );
mRenderTaskForEmboss1.SetCameraActor( mCameraActor );
mRenderTaskForEmboss2 = taskList.CreateTask();
mRenderTaskForEmboss2.SetInputEnabled( false );
mRenderTaskForEmboss2.SetClearColor( Vector4( 1.0f, 1.0f, 1.0f, 0.0f ) );
mRenderTaskForEmboss2.SetClearEnabled( true );
- mRenderTaskForEmboss2.SetTargetFrameBuffer( mImageForEmboss2 );
+ mRenderTaskForEmboss2.SetFrameBuffer( mFrameBufferForEmboss2 );
mRenderTaskForEmboss2.SetCameraActor( mCameraActor );
mRenderTaskForOutput = taskList.CreateTask();
mRenderTaskForOutput.SetInputEnabled( false );
mRenderTaskForOutput.SetClearColor( Vector4( 0.5f, 0.5f, 0.5f, 0.0f ) );
mRenderTaskForOutput.SetClearEnabled( true );
- mRenderTaskForOutput.SetTargetFrameBuffer( mOutputImage );
+ mRenderTaskForOutput.SetFrameBuffer( mOutputFrameBuffer );
mRenderTaskForOutput.SetCameraActor( mCameraActor );
}