2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <dali-toolkit/internal/controls/image-view/masked-image-view-impl.h>
32 namespace // unnamed namespace
35 const char* CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::CUSTOM_PROPERTY_COUNT ] =
44 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE =
45 "precision mediump float; \n"
46 "uniform vec2 uTargetSize; \n"
47 "uniform vec2 uSourceSize; \n"
48 "uniform vec2 uSourceOffset; \n"
49 "uniform vec2 uMaskSize; \n"
50 "uniform vec2 uMaskOffset; \n"
51 "varying vec2 vMaskTexCoord; \n"
54 " float x = uSourceSize.x*aPosition.x + uSourceOffset.x; \n"
55 " float y = uSourceSize.y*aPosition.y + uSourceOffset.y; \n"
57 " gl_Position = vec4( x/(uTargetSize.x*0.5), y/(uTargetSize.y*0.5), 0.0, 1.0 ); \n"
59 " vMaskTexCoord.x = (uMaskSize.x*0.5 + x - uMaskOffset.x) / uMaskSize.x; \n"
60 " vMaskTexCoord.y = (uMaskSize.y*0.5 + y - uMaskOffset.y) / uMaskSize.y; \n";
62 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE0 =
64 " vTexCoord = aTexCoord; \n"
67 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE90 =
69 " vTexCoord.x = aTexCoord.y; \n"
70 " vTexCoord.y = 1.0 - aTexCoord.x; \n"
73 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE180 =
75 " vTexCoord.x = 1.0 - aTexCoord.x; \n"
76 " vTexCoord.y = 1.0 - aTexCoord.y; \n"
79 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE270 =
81 " vTexCoord.x = 1.0 - aTexCoord.y; \n"
82 " vTexCoord.y = aTexCoord.x; \n"
85 const char* const MASKED_IMAGE_VIEW_FRAGMENT_SOURCE =
86 "precision mediump float; \n"
87 "varying vec2 vMaskTexCoord; \n"
90 " highp vec4 mask = texture2D(sEffect, vMaskTexCoord); \n"
91 " gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(1,1,1,mask.a); \n"
94 Vector2 EqualToConstraintVector2( const Vector2& current, const PropertyInput& property )
96 return property.GetVector2();
99 Vector2 GetSizeForAspectRatio( const Vector2& targetSize, float aspectRatio )
101 Vector2 sizeToKeepAspectRatio( targetSize );
103 float targetAspectRatio( targetSize.width / targetSize.height );
105 if( aspectRatio > targetAspectRatio )
107 sizeToKeepAspectRatio.width = sizeToKeepAspectRatio.height * aspectRatio;
109 else if ( aspectRatio < targetAspectRatio )
111 sizeToKeepAspectRatio.height = sizeToKeepAspectRatio.width / aspectRatio;
114 return sizeToKeepAspectRatio;
117 Vector2 ClampSourceSize( const Vector2& sourceSize, const Vector2& targetSize, float widthOverHeight, float maxSourceScale )
119 Vector2 clampedSize( sourceSize );
121 Vector2 minSize( targetSize );
122 if ( widthOverHeight > 0.0f )
124 minSize = GetSizeForAspectRatio( targetSize, widthOverHeight );
127 if ( clampedSize.width < minSize.width ||
128 clampedSize.height < minSize.height )
130 clampedSize = minSize;
132 else if ( clampedSize.width > minSize.width *maxSourceScale ||
133 clampedSize.height > minSize.height*maxSourceScale )
135 clampedSize = minSize * maxSourceScale;
141 Vector2 ClampSourceOffset( const Vector2& sourceOffset, const Vector2& targetSize, const Vector2& sourceSize )
145 if ( sourceSize.width > targetSize.width )
147 float offset = (sourceSize.width - targetSize.width) * 0.5f;
152 if ( sourceSize.height > targetSize.height )
154 float offset = (sourceSize.height - targetSize.height) * 0.5f;
159 return Vector2( Clamp(sourceOffset.x, min.x, max.x), Clamp(sourceOffset.y, min.y, max.y) );
162 } // unnamed namespace
164 Dali::Toolkit::MaskedImageView MaskedImageView::New( unsigned int targetWidth,
165 unsigned int targetHeight,
169 // Create the implementation
170 MaskedImageView* maskedImageView = new MaskedImageView();
172 // Pass ownership to CustomActor via derived handle
173 Dali::Toolkit::MaskedImageView handle(*maskedImageView);
175 // Second-phase init of the implementation
176 // This can only be done after the CustomActor connection has been made...
177 maskedImageView->Initialize( targetWidth, targetHeight, sourceImage, maskImage );
182 void MaskedImageView::SetSourceImage( Image sourceImage )
184 mSourceImageActor.SetImage( sourceImage );
187 Image MaskedImageView::GetSourceImage()
189 return mSourceImageActor.GetImage();
192 void MaskedImageView::SetMaskImage( Image maskImage )
194 mMaskImage = maskImage;
195 mSourceImageActor.GetShaderEffect().SetEffectImage( maskImage );
198 Image MaskedImageView::GetMaskImage()
203 Property::Index MaskedImageView::GetPropertyIndex( Dali::Toolkit::MaskedImageView::CustomProperty customProperty ) const
205 Property::Index index = Property::INVALID_INDEX;
207 switch ( customProperty )
209 case Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR:
211 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ];
215 case Dali::Toolkit::MaskedImageView::SOURCE_SIZE:
217 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ];
221 case Dali::Toolkit::MaskedImageView::SOURCE_OFFSET:
223 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ];
227 case Dali::Toolkit::MaskedImageView::MASK_SIZE:
229 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ];
233 case Dali::Toolkit::MaskedImageView::MASK_OFFSET:
235 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ];
246 void MaskedImageView::Pause()
250 mRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
254 void MaskedImageView::Resume()
258 mRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
262 bool MaskedImageView::IsPaused() const
264 return !mRenderTask; // RenderTask is discarded during Pause()
267 void MaskedImageView::SetEditMode( Dali::Toolkit::MaskedImageView::EditMode editMode )
271 mEditMode = editMode;
273 if ( Dali::Toolkit::MaskedImageView::EDIT_DISABLED == editMode )
275 if ( mPanGestureDetector )
277 mPanGestureDetector.DetachAll();
278 mPanGestureDetector.Reset();
281 if ( mPinchDetector )
283 mPinchDetector.DetachAll();
284 mPinchDetector.Reset();
289 if ( !mPanGestureDetector )
291 mPanGestureDetector = PanGestureDetector::New();
292 mPanGestureDetector.Attach( self );
293 mPanGestureDetector.DetectedSignal().Connect(this, &MaskedImageView::OnPan);
296 if ( !mPinchDetector )
298 mPinchDetector = PinchGestureDetector::New();
299 mPinchDetector.Attach( self );
300 mPinchDetector.DetectedSignal().Connect(this, &MaskedImageView::OnPinch);
303 if( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == editMode )
305 // Re-clamp values to preserve image aspect-ratio etc.
306 ClampSourceSizeAndOffset();
311 Dali::Toolkit::MaskedImageView::EditMode MaskedImageView::GetEditMode() const
316 void MaskedImageView::OnPropertySet( Property::Index index, Property::Value propertyValue )
318 // Ignore OnPropertySet if MaskedImageView is setting the properties
319 if( !mSelfPropertySetting )
321 // Synchronize with user-supplied property values...
322 if( mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ] == index )
324 // Note that clamping will take effect when edit-mode is used later
325 mSourcePosition.mStartPinchSize = propertyValue.Get<Vector2>();
326 mSourcePosition.mCurrentPinchSize = propertyValue.Get<Vector2>();
328 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ] == index )
330 // Note that clamping will take effect when edit-mode is used later
331 mSourcePosition.mPanOffset = propertyValue.Get<Vector2>();
333 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ] == index )
335 mMaskPosition.mStartPinchSize = propertyValue.Get<Vector2>();
336 mMaskPosition.mCurrentPinchSize = propertyValue.Get<Vector2>();
338 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ] == index )
340 mMaskPosition.mPanOffset = propertyValue.Get<Vector2>();
342 // else it's fine to do nothing here
346 void MaskedImageView::OnPan(Actor source, PanGesture gesture)
348 // Used to flag whether edit mode is setting properties
349 mSelfPropertySetting = true;
353 if ( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == mEditMode )
355 mSourcePosition.mPanOffset += gesture.displacement;
356 mSourcePosition.mPanOffset = ClampSourceOffset( mSourcePosition.mPanOffset, mTargetSize, mSourcePosition.mCurrentPinchSize );
358 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ), mSourcePosition.mPanOffset );
362 mMaskPosition.mPanOffset += gesture.displacement;
364 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::MASK_OFFSET ), mMaskPosition.mPanOffset );
367 // Used to flag whether edit mode is setting properties
368 mSelfPropertySetting = false;
371 void MaskedImageView::OnPinch(Actor actor, PinchGesture pinch)
373 // Used to flag whether edit mode is setting properties
374 mSelfPropertySetting = true;
378 if ( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == mEditMode )
380 if ( pinch.state == Gesture::Started )
382 mSourcePosition.mStartPinchSize = mSourcePosition.mCurrentPinchSize;
385 mSourcePosition.mCurrentPinchSize = mSourcePosition.mStartPinchSize * pinch.scale;
387 ClampSourceSizeAndOffset();
391 if ( pinch.state == Gesture::Started )
393 mMaskPosition.mStartPinchSize = mMaskPosition.mCurrentPinchSize;
396 mMaskPosition.mCurrentPinchSize = mMaskPosition.mStartPinchSize * pinch.scale;
398 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::MASK_SIZE ), mMaskPosition.mCurrentPinchSize );
401 // Used to flag whether edit mode is setting properties
402 mSelfPropertySetting = false;
405 void MaskedImageView::SetSourceAspectRatio( float widthOverHeight )
409 if ( widthOverHeight > 0.0f )
411 mWidthOverHeight = widthOverHeight;
413 ClampSourceSizeAndOffset();
417 mWidthOverHeight = 0.0f; // ignore aspect-ratio
421 float MaskedImageView::GetSourceAspectRatio() const
423 return mWidthOverHeight;
426 void MaskedImageView::SetMaximumSourceScale( float scale )
428 mMaximumSourceScale = scale;
431 float MaskedImageView::GetMaximumSourceScale() const
433 return mMaximumSourceScale;
436 void MaskedImageView::SetSourceRotation( MaskedImageView::ImageRotation newRotation )
438 if( mSourceRotation != newRotation )
440 bool oldLandscape( Dali::Toolkit::MaskedImageView::ROTATE_90 == mSourceRotation || Dali::Toolkit::MaskedImageView::ROTATE_270 == mSourceRotation );
441 bool newLandscape( Dali::Toolkit::MaskedImageView::ROTATE_90 == newRotation || Dali::Toolkit::MaskedImageView::ROTATE_270 == newRotation );
443 if ( oldLandscape != newLandscape )
445 // Changing between landscape & portraint, swap width & height
446 float temp = mSourcePosition.mCurrentPinchSize.width;
447 mSourcePosition.mCurrentPinchSize.width = mSourcePosition.mCurrentPinchSize.height;
448 mSourcePosition.mCurrentPinchSize.height = temp;
451 mSourceRotation = newRotation;
453 ApplyMaskedImageShader( newRotation );
455 ClampSourceSizeAndOffset();
459 MaskedImageView::ImageRotation MaskedImageView::GetSourceRotation() const
461 return mSourceRotation;
464 Dali::Toolkit::MaskedImageView::MaskedImageViewSignal& MaskedImageView::MaskFinishedSignal()
466 return mMaskFinishedSignal;
469 MaskedImageView::MaskedImageView()
471 mEditMode( Dali::Toolkit::MaskedImageView::EDIT_DISABLED ),
472 mSelfPropertySetting( false ),
473 mSourceRotation( Dali::Toolkit::MaskedImageView::ROTATE_0 ),
474 mWidthOverHeight( 0.0f ),
475 mMaximumSourceScale( Dali::Toolkit::MaskedImageView::DEFAULT_MAXIMUM_SOURCE_SCALE )
479 void MaskedImageView::Initialize( unsigned int targetWidth,
480 unsigned int targetHeight,
486 // Register custom properties
488 mTargetSize = Vector2( static_cast<float>(targetWidth), static_cast<float>(targetHeight) );
490 mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ]
491 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ], Color::BLACK );
493 mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ]
494 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ], mTargetSize );
496 mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ]
497 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ], Vector2::ZERO );
499 mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ]
500 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::MASK_SIZE ], mTargetSize );
502 mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ]
503 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ], Vector2::ZERO );
505 // Create destination image (FBO)
506 mDestinationImage = FrameBufferImage::New( targetWidth, targetHeight, Pixel::RGBA8888 );
508 // Create source actor for off-screen image processing
509 mSourceImageActor = ImageActor::New( sourceImage );
510 self.Add( mSourceImageActor );
511 mSourceImageActor.SetParentOrigin( ParentOrigin::CENTER );
512 mSourceImageActor.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
513 mSourceImageActor.SetInheritRotation( false );
514 mSourceImageActor.SetInheritScale( false );
515 mSourceImageActor.SetColorMode( USE_OWN_COLOR );
516 mSourceImageActor.SetSize( Vector3::ONE );
518 // Apply masking effect to source actor
519 mMaskImage = maskImage;
520 ApplyMaskedImageShader( Dali::Toolkit::MaskedImageView::ROTATE_0 );
522 // Create actor to display result of off-screen rendering
523 mDestinationImageActor = ImageActor::New( mDestinationImage );
524 self.Add( mDestinationImageActor );
525 mDestinationImageActor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
526 mDestinationImageActor.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
528 // Start the masking operation
529 mRenderTask = Stage::GetCurrent().GetRenderTaskList().CreateTask();
530 mRenderTask.SetSourceActor( mSourceImageActor );
531 mRenderTask.SetTargetFrameBuffer( mDestinationImage );
532 mRenderTask.SetInputEnabled( false );
533 mRenderTask.SetExclusive( true );
534 mRenderTask.SetClearEnabled( true );
535 mRenderTask.ApplyConstraint( Constraint::New<Vector4>( RenderTask::CLEAR_COLOR,
536 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ] ),
537 EqualToConstraint() ) );
538 mRenderTask.FinishedSignal().Connect( this, &MaskedImageView::OnRenderTaskFinished );
540 // Edit mode initialization
541 mSourcePosition.mCurrentPinchSize = Vector2( targetWidth, targetHeight );
542 mMaskPosition.mCurrentPinchSize = mSourcePosition.mCurrentPinchSize;
545 void MaskedImageView::ApplyMaskedImageShader( ImageRotation rotation )
549 // Vertex shader has different postfix for each rotation
550 std::stringstream vertexSource;
551 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE;
552 if( Dali::Toolkit::MaskedImageView::ROTATE_90 == rotation )
554 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE90;
556 else if( Dali::Toolkit::MaskedImageView::ROTATE_180 == rotation )
558 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE180;
560 else if( Dali::Toolkit::MaskedImageView::ROTATE_270 == rotation )
562 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE270;
564 else // Default to Dali::Toolkit::MaskedImageView::ROTATE_0
566 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE0;
569 ShaderEffect shader = ShaderEffect::New( vertexSource.str(),
570 MASKED_IMAGE_VIEW_FRAGMENT_SOURCE,
571 GeometryType( GEOMETRY_TYPE_IMAGE ),
572 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
574 shader.SetUniform( "uTargetSize", mTargetSize );
575 shader.SetUniform( "uSourceSize", mTargetSize );
576 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uSourceSize" ),
577 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ] ),
578 EqualToConstraintVector2 ) );
579 shader.SetUniform( "uSourceOffset", Vector2::ZERO );
580 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uSourceOffset" ),
581 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ] ),
582 EqualToConstraintVector2 ) );
583 shader.SetUniform( "uMaskSize", mTargetSize );
584 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uMaskSize" ),
585 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ] ),
586 EqualToConstraintVector2 ) );
587 shader.SetUniform( "uMaskOffset", mTargetSize );
588 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uMaskOffset" ),
589 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ] ),
590 EqualToConstraintVector2 ) );
592 shader.SetEffectImage( mMaskImage );
593 mSourceImageActor.SetShaderEffect( shader );
596 void MaskedImageView::ClampSourceSizeAndOffset()
598 float rotatedAspectRatio( mWidthOverHeight );
599 if( mWidthOverHeight > 0.0f &&
600 ( Dali::Toolkit::MaskedImageView::ROTATE_90 == mSourceRotation ||
601 Dali::Toolkit::MaskedImageView::ROTATE_270 == mSourceRotation ) )
603 rotatedAspectRatio = 1.0f / mWidthOverHeight;
608 mSourcePosition.mCurrentPinchSize = ClampSourceSize( mSourcePosition.mCurrentPinchSize, mTargetSize, rotatedAspectRatio, mMaximumSourceScale );
609 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_SIZE ), mSourcePosition.mCurrentPinchSize );
611 mSourcePosition.mPanOffset = ClampSourceOffset( mSourcePosition.mPanOffset, mTargetSize, mSourcePosition.mCurrentPinchSize );
612 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ), mSourcePosition.mPanOffset );
615 MaskedImageView::~MaskedImageView()
617 // Guard to allow handle destruction after Core has been destroyed
618 if( Stage::IsInstalled() )
620 Stage::GetCurrent().GetRenderTaskList().RemoveTask( mRenderTask );
624 void MaskedImageView::OnRenderTaskFinished( Dali::RenderTask& renderTask )
626 Toolkit::MaskedImageView handle( GetOwner() );
627 mMaskFinishedSignal.Emit( handle );
630 } // namespace Internal
632 } // namespace Toolkit