Add null check for mTextureSet.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / filters / spread-filter.cpp
index 0dab8c6..5eb8a82 100644 (file)
@@ -1,26 +1,31 @@
-//
-// 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) 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.
+ * 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/devel-api/common/stage.h>
+#include <dali/public-api/object/property-map.h>
+#include <dali/public-api/render-tasks/render-task-list.h>
 
 // INTERNAL INCLUDES
+#include <dali-toolkit/internal/controls/control/control-renderers.h>
 
 namespace Dali
 {
@@ -34,37 +39,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 +82,36 @@ 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.SetParentOrigin( ParentOrigin::CENTER );
-  mActorForInput.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
-  mActorForInput.ScaleBy( Vector3(1.0f, -1.0f, 1.0f) );
+  mActorForInput = Actor::New();
+  mActorForInput.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+  mActorForInput.SetProperty( Actor::Property::SIZE, 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 ) );
+
+  Renderer rendererForInput = CreateRenderer( BASIC_VERTEX_SOURCE, SPREAD_FRAGMENT_SOURCE );
+  SetRendererTexture( rendererForInput, mInputTexture );
+  mActorForInput.AddRenderer( rendererForInput );
 
   // create internal offscreen for result of horizontal pass
-  mImageForHorz = FrameBufferImage::New( mTargetSize.width, mTargetSize.height, mPixelFormat, Image::Unused );
+  mFrameBufferForHorz = FrameBuffer::New( mTargetSize.width, mTargetSize.height, FrameBuffer::Attachment::NONE );
+  Texture textureForHorz = Texture::New( TextureType::TEXTURE_2D, mPixelFormat, unsigned(mTargetSize.width), unsigned(mTargetSize.height) );
+  mFrameBufferForHorz.AttachColorTexture( textureForHorz );
 
   // create an actor to render mImageForHorz for vertical blur pass
-  mActorForHorz = ImageActor::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 = Actor::New();
+  mActorForHorz.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
+  mActorForHorz.SetProperty( Actor::Property::SIZE, 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 ) );
+  Renderer rendererForHorz = CreateRenderer( BASIC_VERTEX_SOURCE, SPREAD_FRAGMENT_SOURCE );
+  SetRendererTexture( rendererForHorz, textureForHorz );
+  mActorForHorz.AddRenderer( rendererForHorz );
 
   mRootActor.Add( mActorForInput );
   mRootActor.Add( mActorForHorz );
-  mRootActor.Add( mCameraActor );
-
-  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 ) );
-
-  mShaderForVert = ShaderEffect::New( "", fragmentSource.str() );
-  mActorForHorz.SetShaderEffect( mShaderForVert );
-  mShaderForVert.SetUniform( "uSpread", mSpread );
-  mShaderForVert.SetUniform( "uTexScale", Vector2( 0.0f, 1.0f / mTargetSize.height ) );
 
   SetupCamera();
   CreateRenderTasks();
@@ -178,15 +167,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.SetProperty( Actor::Property::SIZE, mTargetSize);
+  }
+  if( mActorForHorz )
+  {
+    mActorForHorz.SetProperty( Actor::Property::SIZE, mTargetSize);
+  }
 }
 
 void SpreadFilter::CreateRenderTasks()
@@ -201,7 +192,7 @@ void SpreadFilter::CreateRenderTasks()
   mRenderTaskForHorz.SetInputEnabled( false );
   mRenderTaskForHorz.SetClearEnabled( true );
   mRenderTaskForHorz.SetClearColor( mBackgroundColor );
-  mRenderTaskForHorz.SetTargetFrameBuffer( mImageForHorz );
+  mRenderTaskForHorz.SetFrameBuffer( mFrameBufferForHorz );
   mRenderTaskForHorz.SetCameraActor( mCameraActor );
 
   // use the internal buffer and perform a horizontal blur targeting the output buffer
@@ -212,7 +203,7 @@ void SpreadFilter::CreateRenderTasks()
   mRenderTaskForVert.SetInputEnabled( false );
   mRenderTaskForVert.SetClearEnabled( true );
   mRenderTaskForVert.SetClearColor( mBackgroundColor );
-  mRenderTaskForVert.SetTargetFrameBuffer( mOutputImage );
+  mRenderTaskForVert.SetFrameBuffer( mOutputFrameBuffer );
   mRenderTaskForVert.SetCameraActor( mCameraActor );
 }