2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/image-view/masked-image-view-impl.h>
23 #include <dali/public-api/animation/active-constraint.h>
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/render-tasks/render-task-list.h>
28 #include <dali/public-api/shader-effects/shader-effect.h>
39 namespace // unnamed namespace
42 const char* CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::CUSTOM_PROPERTY_COUNT ] =
51 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE =
52 "precision mediump float; \n"
53 "uniform vec2 uTargetSize; \n"
54 "uniform vec2 uSourceSize; \n"
55 "uniform vec2 uSourceOffset; \n"
56 "uniform vec2 uMaskSize; \n"
57 "uniform vec2 uMaskOffset; \n"
58 "varying vec2 vMaskTexCoord; \n"
61 " float x = uSourceSize.x*aPosition.x + uSourceOffset.x; \n"
62 " float y = uSourceSize.y*aPosition.y + uSourceOffset.y; \n"
64 " gl_Position = vec4( x/(uTargetSize.x*0.5), y/(uTargetSize.y*0.5), 0.0, 1.0 ); \n"
66 " vMaskTexCoord.x = (uMaskSize.x*0.5 + x - uMaskOffset.x) / uMaskSize.x; \n"
67 " vMaskTexCoord.y = (uMaskSize.y*0.5 + y - uMaskOffset.y) / uMaskSize.y; \n";
69 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE0 =
71 " vTexCoord = aTexCoord; \n"
74 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE90 =
76 " vTexCoord.x = aTexCoord.y; \n"
77 " vTexCoord.y = 1.0 - aTexCoord.x; \n"
80 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE180 =
82 " vTexCoord.x = 1.0 - aTexCoord.x; \n"
83 " vTexCoord.y = 1.0 - aTexCoord.y; \n"
86 const char* const MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE270 =
88 " vTexCoord.x = 1.0 - aTexCoord.y; \n"
89 " vTexCoord.y = aTexCoord.x; \n"
92 const char* const MASKED_IMAGE_VIEW_FRAGMENT_SOURCE =
93 "precision mediump float; \n"
94 "varying vec2 vMaskTexCoord; \n"
97 " highp vec4 mask = texture2D(sEffect, vMaskTexCoord); \n"
98 " gl_FragColor = texture2D(sTexture, vTexCoord) * vec4(1,1,1,mask.a); \n"
101 Vector2 EqualToConstraintVector2( const Vector2& current, const PropertyInput& property )
103 return property.GetVector2();
106 Vector2 GetSizeForAspectRatio( const Vector2& targetSize, float aspectRatio )
108 Vector2 sizeToKeepAspectRatio( targetSize );
110 float targetAspectRatio( targetSize.width / targetSize.height );
112 if( aspectRatio > targetAspectRatio )
114 sizeToKeepAspectRatio.width = sizeToKeepAspectRatio.height * aspectRatio;
116 else if ( aspectRatio < targetAspectRatio )
118 sizeToKeepAspectRatio.height = sizeToKeepAspectRatio.width / aspectRatio;
121 return sizeToKeepAspectRatio;
124 Vector2 ClampSourceSize( const Vector2& sourceSize, const Vector2& targetSize, float widthOverHeight, float maxSourceScale )
126 Vector2 clampedSize( sourceSize );
128 Vector2 minSize( targetSize );
129 if ( widthOverHeight > 0.0f )
131 minSize = GetSizeForAspectRatio( targetSize, widthOverHeight );
134 if ( clampedSize.width < minSize.width ||
135 clampedSize.height < minSize.height )
137 clampedSize = minSize;
139 else if ( clampedSize.width > minSize.width *maxSourceScale ||
140 clampedSize.height > minSize.height*maxSourceScale )
142 clampedSize = minSize * maxSourceScale;
148 Vector2 ClampSourceOffset( const Vector2& sourceOffset, const Vector2& targetSize, const Vector2& sourceSize )
152 if ( sourceSize.width > targetSize.width )
154 float offset = (sourceSize.width - targetSize.width) * 0.5f;
159 if ( sourceSize.height > targetSize.height )
161 float offset = (sourceSize.height - targetSize.height) * 0.5f;
166 return Vector2( Clamp(sourceOffset.x, min.x, max.x), Clamp(sourceOffset.y, min.y, max.y) );
169 } // unnamed namespace
171 Dali::Toolkit::MaskedImageView MaskedImageView::New( unsigned int targetWidth,
172 unsigned int targetHeight,
176 // Create the implementation
177 MaskedImageView* maskedImageView = new MaskedImageView();
179 // Pass ownership to CustomActor via derived handle
180 Dali::Toolkit::MaskedImageView handle(*maskedImageView);
182 // Second-phase init of the implementation
183 // This can only be done after the CustomActor connection has been made...
184 maskedImageView->Initialize( targetWidth, targetHeight, sourceImage, maskImage );
189 void MaskedImageView::SetSourceImage( Image sourceImage )
191 mSourceImageActor.SetImage( sourceImage );
194 Image MaskedImageView::GetSourceImage()
196 return mSourceImageActor.GetImage();
199 void MaskedImageView::SetMaskImage( Image maskImage )
201 mMaskImage = maskImage;
202 mSourceImageActor.GetShaderEffect().SetEffectImage( maskImage );
205 Image MaskedImageView::GetMaskImage()
210 Property::Index MaskedImageView::GetPropertyIndex( Dali::Toolkit::MaskedImageView::CustomProperty customProperty ) const
212 Property::Index index = Property::INVALID_INDEX;
214 switch ( customProperty )
216 case Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR:
218 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ];
222 case Dali::Toolkit::MaskedImageView::SOURCE_SIZE:
224 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ];
228 case Dali::Toolkit::MaskedImageView::SOURCE_OFFSET:
230 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ];
234 case Dali::Toolkit::MaskedImageView::MASK_SIZE:
236 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ];
240 case Dali::Toolkit::MaskedImageView::MASK_OFFSET:
242 index = mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ];
253 void MaskedImageView::Pause()
257 mRenderTask.SetRefreshRate( RenderTask::REFRESH_ONCE );
261 void MaskedImageView::Resume()
265 mRenderTask.SetRefreshRate( RenderTask::REFRESH_ALWAYS );
269 bool MaskedImageView::IsPaused() const
271 if( mRenderTask.GetRefreshRate() ) // REFRESH_ALWAYS
281 void MaskedImageView::SetEditMode( Dali::Toolkit::MaskedImageView::EditMode editMode )
285 mEditMode = editMode;
287 if ( Dali::Toolkit::MaskedImageView::EDIT_DISABLED == editMode )
289 if ( mPanGestureDetector )
291 mPanGestureDetector.DetachAll();
292 mPanGestureDetector.Reset();
295 if ( mPinchDetector )
297 mPinchDetector.DetachAll();
298 mPinchDetector.Reset();
303 if ( !mPanGestureDetector )
305 mPanGestureDetector = PanGestureDetector::New();
306 mPanGestureDetector.Attach( self );
307 mPanGestureDetector.DetectedSignal().Connect(this, &MaskedImageView::OnPan);
310 if ( !mPinchDetector )
312 mPinchDetector = PinchGestureDetector::New();
313 mPinchDetector.Attach( self );
314 mPinchDetector.DetectedSignal().Connect(this, &MaskedImageView::OnPinch);
317 if( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == editMode )
319 // Re-clamp values to preserve image aspect-ratio etc.
320 ClampSourceSizeAndOffset();
325 Dali::Toolkit::MaskedImageView::EditMode MaskedImageView::GetEditMode() const
330 void MaskedImageView::OnPropertySet( Property::Index index, Property::Value propertyValue )
332 // Ignore OnPropertySet if MaskedImageView is setting the properties
333 if( !mSelfPropertySetting )
335 // Synchronize with user-supplied property values...
336 if( mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ] == index )
338 // Note that clamping will take effect when edit-mode is used later
339 mSourcePosition.mStartPinchSize = propertyValue.Get<Vector2>();
340 mSourcePosition.mCurrentPinchSize = propertyValue.Get<Vector2>();
342 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ] == index )
344 // Note that clamping will take effect when edit-mode is used later
345 mSourcePosition.mPanOffset = propertyValue.Get<Vector2>();
347 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ] == index )
349 mMaskPosition.mStartPinchSize = propertyValue.Get<Vector2>();
350 mMaskPosition.mCurrentPinchSize = propertyValue.Get<Vector2>();
352 else if( mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ] == index )
354 mMaskPosition.mPanOffset = propertyValue.Get<Vector2>();
356 // else it's fine to do nothing here
360 void MaskedImageView::OnPan(Actor source, const PanGesture& gesture)
362 // Used to flag whether edit mode is setting properties
363 mSelfPropertySetting = true;
367 if ( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == mEditMode )
369 mSourcePosition.mPanOffset += gesture.displacement;
370 mSourcePosition.mPanOffset = ClampSourceOffset( mSourcePosition.mPanOffset, mTargetSize, mSourcePosition.mCurrentPinchSize );
372 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ), mSourcePosition.mPanOffset );
376 mMaskPosition.mPanOffset += gesture.displacement;
378 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::MASK_OFFSET ), mMaskPosition.mPanOffset );
381 // Used to flag whether edit mode is setting properties
382 mSelfPropertySetting = false;
385 void MaskedImageView::OnPinch(Actor actor, const PinchGesture& pinch)
387 // Used to flag whether edit mode is setting properties
388 mSelfPropertySetting = true;
392 if ( Dali::Toolkit::MaskedImageView::EDIT_SOURCE == mEditMode )
394 if ( pinch.state == Gesture::Started )
396 mSourcePosition.mStartPinchSize = mSourcePosition.mCurrentPinchSize;
399 mSourcePosition.mCurrentPinchSize = mSourcePosition.mStartPinchSize * pinch.scale;
401 ClampSourceSizeAndOffset();
405 if ( pinch.state == Gesture::Started )
407 mMaskPosition.mStartPinchSize = mMaskPosition.mCurrentPinchSize;
410 mMaskPosition.mCurrentPinchSize = mMaskPosition.mStartPinchSize * pinch.scale;
412 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::MASK_SIZE ), mMaskPosition.mCurrentPinchSize );
415 // Used to flag whether edit mode is setting properties
416 mSelfPropertySetting = false;
419 void MaskedImageView::SetSourceAspectRatio( float widthOverHeight )
423 if ( widthOverHeight > 0.0f )
425 mWidthOverHeight = widthOverHeight;
427 ClampSourceSizeAndOffset();
431 mWidthOverHeight = 0.0f; // ignore aspect-ratio
435 float MaskedImageView::GetSourceAspectRatio() const
437 return mWidthOverHeight;
440 void MaskedImageView::SetMaximumSourceScale( float scale )
442 mMaximumSourceScale = scale;
445 float MaskedImageView::GetMaximumSourceScale() const
447 return mMaximumSourceScale;
450 void MaskedImageView::SetSourceRotation( MaskedImageView::ImageRotation newRotation )
452 if( mSourceRotation != newRotation )
454 bool oldLandscape( Dali::Toolkit::MaskedImageView::ROTATE_90 == mSourceRotation || Dali::Toolkit::MaskedImageView::ROTATE_270 == mSourceRotation );
455 bool newLandscape( Dali::Toolkit::MaskedImageView::ROTATE_90 == newRotation || Dali::Toolkit::MaskedImageView::ROTATE_270 == newRotation );
457 if ( oldLandscape != newLandscape )
459 // Changing between landscape & portraint, swap width & height
460 float temp = mSourcePosition.mCurrentPinchSize.width;
461 mSourcePosition.mCurrentPinchSize.width = mSourcePosition.mCurrentPinchSize.height;
462 mSourcePosition.mCurrentPinchSize.height = temp;
465 mSourceRotation = newRotation;
467 ApplyMaskedImageShader( newRotation );
469 ClampSourceSizeAndOffset();
473 MaskedImageView::ImageRotation MaskedImageView::GetSourceRotation() const
475 return mSourceRotation;
478 Dali::Toolkit::MaskedImageView::MaskedImageViewSignal& MaskedImageView::MaskFinishedSignal()
480 return mMaskFinishedSignal;
483 MaskedImageView::MaskedImageView()
484 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
485 mEditMode( Dali::Toolkit::MaskedImageView::EDIT_DISABLED ),
486 mSelfPropertySetting( false ),
487 mSourceRotation( Dali::Toolkit::MaskedImageView::ROTATE_0 ),
488 mWidthOverHeight( 0.0f ),
489 mMaximumSourceScale( Dali::Toolkit::MaskedImageView::DEFAULT_MAXIMUM_SOURCE_SCALE )
493 void MaskedImageView::Initialize( unsigned int targetWidth,
494 unsigned int targetHeight,
500 // Register custom properties
502 mTargetSize = Vector2( static_cast<float>(targetWidth), static_cast<float>(targetHeight) );
504 mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ]
505 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ], Color::BLACK );
507 mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ]
508 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ], mTargetSize );
510 mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ]
511 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ], Vector2::ZERO );
513 mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ]
514 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::MASK_SIZE ], mTargetSize );
516 mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ]
517 = self.RegisterProperty( CUSTOM_PROPERTY_NAMES[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ], Vector2::ZERO );
519 // Create destination image (FBO)
520 mDestinationImage = FrameBufferImage::New( targetWidth, targetHeight, Pixel::RGBA8888 );
522 // Create source actor for off-screen image processing
523 mSourceImageActor = ImageActor::New( sourceImage );
524 self.Add( mSourceImageActor );
525 mSourceImageActor.SetParentOrigin( ParentOrigin::CENTER );
526 mSourceImageActor.SetPositionInheritanceMode( DONT_INHERIT_POSITION );
527 mSourceImageActor.SetInheritRotation( false );
528 mSourceImageActor.SetInheritScale( false );
529 mSourceImageActor.SetColorMode( USE_OWN_COLOR );
530 mSourceImageActor.SetSize( Vector3::ONE );
532 // Apply masking effect to source actor
533 mMaskImage = maskImage;
534 ApplyMaskedImageShader( Dali::Toolkit::MaskedImageView::ROTATE_0 );
536 // Create actor to display result of off-screen rendering
537 mDestinationImageActor = ImageActor::New( mDestinationImage );
538 self.Add( mDestinationImageActor );
539 mDestinationImageActor.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
541 // Start the masking operation
542 mRenderTask = Stage::GetCurrent().GetRenderTaskList().CreateTask();
543 mRenderTask.SetSourceActor( mSourceImageActor );
544 mRenderTask.SetTargetFrameBuffer( mDestinationImage );
545 mRenderTask.SetInputEnabled( false );
546 mRenderTask.SetExclusive( true );
547 mRenderTask.SetClearEnabled( true );
548 mRenderTask.ApplyConstraint( Constraint::New<Vector4>( RenderTask::CLEAR_COLOR,
549 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::BACKGROUND_COLOR ] ),
550 EqualToConstraint() ) );
551 mRenderTask.FinishedSignal().Connect( this, &MaskedImageView::OnRenderTaskFinished );
553 // Edit mode initialization
554 mSourcePosition.mCurrentPinchSize = Vector2( targetWidth, targetHeight );
555 mMaskPosition.mCurrentPinchSize = mSourcePosition.mCurrentPinchSize;
558 void MaskedImageView::ApplyMaskedImageShader( ImageRotation rotation )
562 // Vertex shader has different postfix for each rotation
563 std::stringstream vertexSource;
564 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE;
565 if( Dali::Toolkit::MaskedImageView::ROTATE_90 == rotation )
567 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE90;
569 else if( Dali::Toolkit::MaskedImageView::ROTATE_180 == rotation )
571 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE180;
573 else if( Dali::Toolkit::MaskedImageView::ROTATE_270 == rotation )
575 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE270;
577 else // Default to Dali::Toolkit::MaskedImageView::ROTATE_0
579 vertexSource << MASKED_IMAGE_VIEW_VERTEX_SOURCE_ROTATE0;
582 ShaderEffect shader = ShaderEffect::New( vertexSource.str(),
583 MASKED_IMAGE_VIEW_FRAGMENT_SOURCE,
584 GeometryType( GEOMETRY_TYPE_IMAGE ),
585 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
587 shader.SetUniform( "uTargetSize", mTargetSize );
588 shader.SetUniform( "uSourceSize", mTargetSize );
589 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uSourceSize" ),
590 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_SIZE ] ),
591 EqualToConstraintVector2 ) );
592 shader.SetUniform( "uSourceOffset", Vector2::ZERO );
593 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uSourceOffset" ),
594 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ] ),
595 EqualToConstraintVector2 ) );
596 shader.SetUniform( "uMaskSize", mTargetSize );
597 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uMaskSize" ),
598 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_SIZE ] ),
599 EqualToConstraintVector2 ) );
600 shader.SetUniform( "uMaskOffset", mTargetSize );
601 shader.ApplyConstraint( Constraint::New<Vector2>( shader.GetPropertyIndex( "uMaskOffset" ),
602 Source( self, mCustomProperties[ Dali::Toolkit::MaskedImageView::MASK_OFFSET ] ),
603 EqualToConstraintVector2 ) );
605 shader.SetEffectImage( mMaskImage );
606 mSourceImageActor.SetShaderEffect( shader );
609 void MaskedImageView::ClampSourceSizeAndOffset()
611 float rotatedAspectRatio( mWidthOverHeight );
612 if( mWidthOverHeight > 0.0f &&
613 ( Dali::Toolkit::MaskedImageView::ROTATE_90 == mSourceRotation ||
614 Dali::Toolkit::MaskedImageView::ROTATE_270 == mSourceRotation ) )
616 rotatedAspectRatio = 1.0f / mWidthOverHeight;
621 mSourcePosition.mCurrentPinchSize = ClampSourceSize( mSourcePosition.mCurrentPinchSize, mTargetSize, rotatedAspectRatio, mMaximumSourceScale );
622 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_SIZE ), mSourcePosition.mCurrentPinchSize );
624 mSourcePosition.mPanOffset = ClampSourceOffset( mSourcePosition.mPanOffset, mTargetSize, mSourcePosition.mCurrentPinchSize );
625 self.SetProperty( GetPropertyIndex( Dali::Toolkit::MaskedImageView::SOURCE_OFFSET ), mSourcePosition.mPanOffset );
628 MaskedImageView::~MaskedImageView()
630 // Guard to allow handle destruction after Core has been destroyed
631 if( Stage::IsInstalled() )
633 Stage::GetCurrent().GetRenderTaskList().RemoveTask( mRenderTask );
637 void MaskedImageView::OnControlSizeSet( const Vector3& targetSize )
639 mDestinationImageActor.SetSize(targetSize);
642 void MaskedImageView::OnRenderTaskFinished( Dali::RenderTask& renderTask )
644 Toolkit::MaskedImageView handle( GetOwner() );
645 mMaskFinishedSignal.Emit( handle );
648 } // namespace Internal
650 } // namespace Toolkit