Merge "TextController - Update the text model." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / filters / spread-filter.cpp
index 0dab8c6..de205b6 100644 (file)
@@ -1,24 +1,28 @@
-//
-// 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) 2014 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 "spread-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>
 
 // INTERNAL INCLUDES
 
@@ -34,37 +38,29 @@ namespace Internal
 namespace
 {
 
-const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
-
 const char* const SPREAD_FRAGMENT_SOURCE =
 {
- "uniform float uSpread;\n"
+ "precision highp float;\n"
+ "varying mediump vec2 vTexCoord;\n"
+ "uniform sampler2D sTexture;\n"
+ "uniform int uSpread;\n"
  "uniform vec2 uTexScale;\n"
  "void main()\n"
  "{\n"
  "  vec4 color = texture2D( sTexture, vTexCoord);\n"
- "# ifdef DEBUG_RENDER\n"
- "  if( vTexCoord.s < 0.495 )\n"
- "  {\n"
- "# endif //def DEBUG_RENDER\n"
- "    int spread = int(uSpread);\n"
- "    for( int i = 1; i <= spread; ++i )\n"
- "    {\n"
- "      vec2 offset = uTexScale * float(i);\n"
- "      color = max( texture2D( sTexture, vTexCoord + offset), color );\n"
- "      color = max( texture2D( sTexture, vTexCoord - offset), color );\n"
- "    }\n"
- "# ifdef DEBUG_RENDER\n"
- "  }\n"
- "  else if( vTexCoord.s <= 0.505 )\n"
+ "  for( int i = 1; i <= uSpread; ++i )\n"
  "  {\n"
- "    color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
+ "    vec2 offset = uTexScale * float(i);\n"
+ "    color = max( texture2D( sTexture, vTexCoord + offset), color );\n"
+ "    color = max( texture2D( sTexture, vTexCoord - offset), color );\n"
  "  }\n"
- "# endif //def DEBUG_RENDER\n"
  "  gl_FragColor = color;\n"
  "}\n"
 };
 
+const char* const SPREAD_UNIFORM_NAME( "uSpread" );
+const char* const TEX_SCALE_UNIFORM_NAME( "uTexScale" );
+
 } // namespace
 
 
@@ -85,44 +81,35 @@ void SpreadFilter::SetSpread( float spread )
 
 void SpreadFilter::Enable()
 {
-  mCameraActor = CameraActor::New();
-  mCameraActor.SetParentOrigin(ParentOrigin::CENTER);
-
   // create actor to render input with applied emboss effect
-  mActorForInput = ImageActor::New( mInputImage );
+  mActorForInput = Toolkit::ImageView::New( mInputImage );
   mActorForInput.SetParentOrigin( ParentOrigin::CENTER );
-  mActorForInput.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
-  mActorForInput.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
+  mActorForInput.SetSize(mTargetSize);
+  // register properties as shader uniforms
+  mActorForInput.RegisterProperty( SPREAD_UNIFORM_NAME, mSpread );
+  mActorForInput.RegisterProperty( TEX_SCALE_UNIFORM_NAME, Vector2( 1.0f / mTargetSize.width, 0.0f ) );
 
   // create internal offscreen for result of horizontal pass
-  mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
-
+  mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::UNUSED );
   // create an actor to render mImageForHorz for vertical blur pass
-  mActorForHorz = ImageActor::New( mImageForHorz );
+  mActorForHorz = Toolkit::ImageView::New( mImageForHorz );
   mActorForHorz.SetParentOrigin( ParentOrigin::CENTER );
-  mActorForHorz.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
-  mActorForHorz.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
+  mActorForHorz.SetSize(mTargetSize);
+  // register properties as shader uniforms
+  mActorForHorz.RegisterProperty( SPREAD_UNIFORM_NAME, mSpread );
+  mActorForHorz.RegisterProperty( TEX_SCALE_UNIFORM_NAME, Vector2( 0.0f, 1.0f / mTargetSize.height ) );
 
-  mRootActor.Add( mActorForInput );
-  mRootActor.Add( mActorForHorz );
-  mRootActor.Add( mCameraActor );
+  Property::Map customShader;
+  customShader[ "fragmentShader" ] = SPREAD_FRAGMENT_SOURCE;
+  Property::Map rendererMap;
+  rendererMap.Insert( "shader", customShader );
 
-  std::ostringstream fragmentSource;
-  if( mDebugRender )
-  {
-    fragmentSource << "#define DEBUG_RENDER\n";
-  }
-  fragmentSource << SPREAD_FRAGMENT_SOURCE;
-
-  mShaderForHorz = ShaderEffect::New( "", fragmentSource.str() );
-  mActorForInput.SetShaderEffect( mShaderForHorz );
-  mShaderForHorz.SetUniform( "uSpread", mSpread );
-  mShaderForHorz.SetUniform( "uTexScale", Vector2( 1.0f / mTargetSize.width, 0.0f ) );
+  // set SPREAD custom shader
+  mActorForInput.SetProperty( Toolkit::ImageView::Property::IMAGE, rendererMap );
+  mActorForHorz.SetProperty( Toolkit::ImageView::Property::IMAGE, rendererMap );
 
-  mShaderForVert = ShaderEffect::New( "", fragmentSource.str() );
-  mActorForHorz.SetShaderEffect( mShaderForVert );
-  mShaderForVert.SetUniform( "uSpread", mSpread );
-  mShaderForVert.SetUniform( "uTexScale", Vector2( 0.0f, 1.0f / mTargetSize.height ) );
+  mRootActor.Add( mActorForInput );
+  mRootActor.Add( mActorForHorz );
 
   SetupCamera();
   CreateRenderTasks();
@@ -178,15 +165,17 @@ void SpreadFilter::Refresh()
   }
 }
 
-void SpreadFilter::SetupCamera()
+void SpreadFilter::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( mActorForInput )
+  {
+    mActorForInput.SetSize(mTargetSize);
+  }
+  if( mActorForHorz )
+  {
+    mActorForHorz.SetSize(mTargetSize);
+  }
 }
 
 void SpreadFilter::CreateRenderTasks()