2 * Copyright (c) 2020 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 "npatch-visual.h"
22 #include <dali/devel-api/rendering/renderer-devel.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
28 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
29 #include <dali-toolkit/public-api/visuals/visual-properties.h>
30 #include <dali-toolkit/internal/visuals/npatch-loader.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
33 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
34 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
36 #include <dali-toolkit/internal/visuals/rendering-addon.h>
49 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
50 attribute mediump vec2 aPosition;\n
51 varying mediump vec2 vTexCoord;\n
52 varying mediump vec2 vMaskTexCoord;\n
53 uniform highp mat4 uMvpMatrix;\n
54 uniform highp vec3 uSize;\n
55 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
56 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
59 // Visual size and offset
60 uniform mediump vec2 offset;\n
61 uniform highp vec2 size;\n
62 uniform mediump vec4 offsetSizeMode;\n
63 uniform mediump vec2 origin;\n
64 uniform mediump vec2 anchorPoint;\n
65 uniform mediump vec2 extraSize;\n
69 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
70 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
72 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
73 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
75 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
76 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
78 mediump vec4 gridPosition = vec4( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal, 0.0, 1.0 );\n
79 mediump vec4 vertexPosition = gridPosition;\n
80 vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
81 vertexPosition.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
82 vertexPosition = uMvpMatrix * vertexPosition;\n
84 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
85 vMaskTexCoord = gridPosition.xy / visualSize;\n
87 gl_Position = vertexPosition;\n
91 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
92 attribute mediump vec2 aPosition;\n
93 varying mediump vec2 vTexCoord;\n
94 varying mediump vec2 vMaskTexCoord;\n
95 uniform highp mat4 uMvpMatrix;\n
96 uniform highp vec3 uSize;\n
97 uniform mediump vec2 uFixed[ 3 ];\n
98 uniform mediump vec2 uStretchTotal;\n
100 //Visual size and offset
101 uniform mediump vec2 offset;\n
102 uniform highp vec2 size;\n
103 uniform mediump vec4 offsetSizeMode;\n
104 uniform mediump vec2 origin;\n
105 uniform mediump vec2 anchorPoint;\n
106 uniform mediump vec2 extraSize;\n
110 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
111 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
113 mediump vec2 size = visualSize.xy;\n
115 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
116 mediump vec2 stretch = floor( aPosition * 0.5 );\n
117 mediump vec2 fixedTotal = uFixed[ 2 ];\n
119 mediump vec4 gridPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
120 mediump vec4 vertexPosition = gridPosition;\n
121 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
122 vertexPosition.xy += anchorPoint*size + (visualOffset + origin)*uSize.xy;\n
124 vertexPosition = uMvpMatrix * vertexPosition;\n
126 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
128 vMaskTexCoord = gridPosition.xy / size;\n
129 gl_Position = vertexPosition;\n
133 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
134 varying mediump vec2 vTexCoord;\n
135 uniform sampler2D sTexture;\n
136 uniform lowp vec4 uColor;\n
137 uniform lowp vec3 mixColor;\n
138 uniform lowp float preMultipliedAlpha;\n
142 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
146 const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
147 varying mediump vec2 vTexCoord;\n
148 varying mediump vec2 vMaskTexCoord;\n
149 uniform sampler2D sTexture;\n
150 uniform sampler2D sMask;\n
151 uniform lowp vec4 uColor;\n
152 uniform lowp vec3 mixColor;\n
153 uniform lowp float preMultipliedAlpha;\n
154 uniform mediump float auxiliaryImageAlpha;\n
158 // Where mask image is transparent, all of background image must show through.
159 // where mask image is opaque, only mask should be shown
160 // where mask is translucent, less of background should be shown.
161 // auxiliaryImageAlpha controls how much of mask is visible
163 mediump vec4 color = texture2D( sTexture, vTexCoord );\n
164 mediump vec4 mask = texture2D( sMask, vMaskTexCoord );\n
166 mediump vec3 mixedColor = color.rgb * mix( 1.0-mask.a, 1.0, 1.0-auxiliaryImageAlpha)
167 + mask.rgb*mask.a * auxiliaryImageAlpha;\n
168 gl_FragColor = vec4(mixedColor,1.0) * uColor * vec4( mixColor, 1.0 );\n
173 * @brief Creates the geometry formed from the vertices and indices
175 * @param[in] vertices The vertices to generate the geometry from
176 * @param[in] indices The indices to generate the geometry from
177 * @return The geometry formed from the vertices and indices
179 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
181 Property::Map vertexFormat;
182 vertexFormat[ "aPosition" ] = Property::VECTOR2;
183 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
184 if( vertices.Size() > 0 )
186 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
189 // Create the geometry object
190 Geometry geometry = Geometry::New();
191 geometry.AddVertexBuffer( vertexBuffer );
192 if( indices.Size() > 0 )
194 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
202 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
204 * @param[out] indices The indices to add to
205 * @param[in] rowIdx The row index to start the quad
206 * @param[in] nextRowIdx The index to the next row
208 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
210 indices.PushBack( rowIdx );
211 indices.PushBack( nextRowIdx + 1 );
212 indices.PushBack( rowIdx + 1 );
214 indices.PushBack( rowIdx );
215 indices.PushBack( nextRowIdx );
216 indices.PushBack( nextRowIdx + 1 );
219 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
221 vertices.PushBack( Vector2( x, y ) );
224 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent)
226 uint16_t prevEnd = 0;
227 uint16_t prevFix = 0;
228 uint16_t prevStretch = 0;
230 for( NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
232 uint16_t start = it->GetX();
233 uint16_t end = it->GetY();
235 uint16_t fix = prevFix + start - prevEnd;
236 uint16_t stretch = prevStretch + end - start;
238 std::stringstream uniform;
239 uniform << uniformName << "[" << i << "]";
240 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
244 prevStretch = stretch;
248 prevFix += imageExtent - prevEnd;
249 std::stringstream uniform;
250 uniform << uniformName << "[" << i << "]";
251 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
255 } //unnamed namespace
257 /////////////////NPatchVisual////////////////
259 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
261 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
262 nPatchVisual->mImageUrl = imageUrl;
263 nPatchVisual->SetProperties( properties );
268 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
270 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
271 nPatchVisual->mImageUrl = imageUrl;
276 void NPatchVisual::LoadImages()
278 TextureManager& textureManager = mFactoryCache.GetTextureManager();
279 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
281 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
283 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
284 mId = mLoader.Load( textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading );
286 const NPatchLoader::Data* data;
287 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
289 EnablePreMultipliedAlpha( preMultiplyOnLoad );
293 if( !mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
295 // Load the auxiliary image
296 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
297 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer( mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT,
298 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
299 this, true, preMultiplyOnLoading );
303 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
308 // load now if not already loaded
309 const NPatchLoader::Data* data;
310 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
312 naturalSize.x = data->croppedWidth;
313 naturalSize.y = data->croppedHeight;
317 if( mImageUrl.IsValid() )
319 ImageDimensions dimensions = Dali::GetOriginalImageSize( mImageUrl.GetUrl() );
320 if( dimensions != ImageDimensions( 0, 0 ) )
322 naturalSize.x = dimensions.GetWidth();
323 naturalSize.y = dimensions.GetHeight();
328 if( mAuxiliaryPixelBuffer )
330 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
331 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
335 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
337 // URL is already passed in via constructor
339 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
340 if( borderOnlyValue )
342 borderOnlyValue->Get( mBorderOnly );
345 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
346 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
348 // Not a rect so try vector4
350 if( borderValue->Get( border ) )
352 mBorder.left = static_cast< int >( border.x );
353 mBorder.right = static_cast< int >( border.y );
354 mBorder.bottom = static_cast< int >( border.z );
355 mBorder.top = static_cast< int >( border.w );
359 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
363 if( auxImage->Get( url ) )
369 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
372 auxImageAlpha->Get( mAuxiliaryImageAlpha );
375 Property::Value* synchronousLoading = propertyMap.Find( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING );
376 if( synchronousLoading )
379 synchronousLoading->Get( sync );
382 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
386 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
391 void NPatchVisual::DoSetOnScene( Actor& actor )
393 // load when first go on stage
396 const NPatchLoader::Data* data;
397 if( mLoader.GetNPatchData( mId, data ) )
399 Geometry geometry = CreateGeometry();
400 Shader shader = CreateShader();
402 mImpl->mRenderer = Renderer::New( geometry, shader );
404 mPlacementActor = actor;
405 if( data->loadCompleted )
407 if( RenderingAddOn::Get().IsValid() )
409 RenderingAddOn::Get().SubmitRenderTask( mImpl->mRenderer, data->renderingMap );
412 ApplyTextureAndUniforms();
413 actor.AddRenderer( mImpl->mRenderer );
414 mPlacementActor.Reset();
416 // npatch loaded and ready to display
417 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
422 void NPatchVisual::DoSetOffScene( Actor& actor )
424 actor.RemoveRenderer( mImpl->mRenderer );
425 mImpl->mRenderer.Reset();
426 mPlacementActor.Reset();
429 void NPatchVisual::OnSetTransform()
431 if( mImpl->mRenderer )
433 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
437 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
440 bool sync = IsSynchronousLoadingRequired();
441 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync );
442 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
443 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
444 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
445 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
447 if( mAuxiliaryUrl.IsValid() )
449 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
450 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
454 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
456 if( mAuxiliaryUrl.IsValid() )
458 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
459 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
463 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
464 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH ),
466 mLoader( factoryCache.GetNPatchLoader() ),
469 mId( NPatchLoader::UNINITIALIZED_ID ),
470 mBorderOnly( false ),
472 mAuxiliaryImageAlpha( 0.0f )
474 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
477 NPatchVisual::~NPatchVisual()
479 TextureManager& textureManager = mFactoryCache.GetTextureManager();
480 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
481 if(mId != NPatchLoader::UNINITIALIZED_ID)
482 mLoader.Remove(mId, textureManager, this, synchronousLoading);
485 Geometry NPatchVisual::CreateGeometry()
488 const NPatchLoader::Data* data;
489 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
491 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
493 if( DALI_UNLIKELY( mBorderOnly ) )
495 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
499 if( data->renderingMap )
501 uint32_t elementCount[2];
502 geometry = RenderingAddOn::Get().CreateGeometryGrid(data->renderingMap, Uint16Pair(3, 3), elementCount );
503 if( mImpl->mRenderer )
505 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->renderingMap);
510 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
514 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
516 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
517 if( !data->renderingMap )
519 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
523 uint32_t elementCount[2];
524 geometry = !mBorderOnly ?
525 RenderingAddOn::Get().CreateGeometryGrid(data->renderingMap, gridSize, elementCount ) : CreateBorderGeometry(gridSize );
526 if( mImpl->mRenderer )
528 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->renderingMap);
535 // no N patch data so use default geometry
536 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
541 Shader NPatchVisual::CreateShader()
544 const NPatchLoader::Data* data;
545 // 0 is either no data (load failed?) or no stretch regions on image
546 // for both cases we use the default shader
547 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
548 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
550 auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
552 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
553 : VisualFactoryCache::NINE_PATCH_SHADER;
555 // ask loader for the regions
556 if( mLoader.GetNPatchData( mId, data ) )
558 xStretchCount = data->stretchPixelsX.Count();
559 yStretchCount = data->stretchPixelsY.Count();
562 if( DALI_LIKELY( !mImpl->mCustomShader ) )
564 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
565 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
567 shader = mFactoryCache.GetShader( shaderType );
568 if( DALI_UNLIKELY( !shader ) )
570 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader );
571 // Only cache vanilla 9 patch shaders
572 mFactoryCache.SaveShader( shaderType, shader );
575 else if( xStretchCount > 0 || yStretchCount > 0)
577 std::stringstream vertexShader;
578 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
579 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
582 shader = Shader::New( vertexShader.str(), fragmentShader );
587 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
589 if( !mImpl->mCustomShader->mFragmentShader.empty() )
591 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
593 hints = mImpl->mCustomShader->mHints;
595 /* Apply Custom Vertex Shader only if image is 9-patch */
596 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
597 ( xStretchCount == 0 && yStretchCount == 0 ) )
599 const char* vertexShader = VERTEX_SHADER_3X3;
601 if( !mImpl->mCustomShader->mVertexShader.empty() )
603 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
605 shader = Shader::New( vertexShader, fragmentShader, hints );
607 else if( xStretchCount > 0 || yStretchCount > 0)
609 std::stringstream vertexShader;
610 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
611 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
614 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
621 void NPatchVisual::ApplyTextureAndUniforms()
623 const NPatchLoader::Data* data;
624 TextureSet textureSet;
626 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
628 textureSet = data->textureSet;
630 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
632 //special case for 9 patch
633 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
634 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
636 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
637 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
639 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
640 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
641 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
642 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
646 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
647 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
649 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
650 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
655 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
656 textureSet = TextureSet::New();
658 Texture croppedImage = mFactoryCache.GetBrokenVisualImage();
659 textureSet.SetTexture( 0u, croppedImage );
660 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
661 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
662 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
663 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
666 if( mAuxiliaryPixelBuffer )
668 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
669 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
670 // on GL interpolation alone.
671 if( mAuxiliaryPixelBuffer.GetWidth() < data->croppedWidth &&
672 mAuxiliaryPixelBuffer.GetHeight() < data->croppedHeight )
674 mAuxiliaryPixelBuffer.Resize( data->croppedWidth, data->croppedHeight );
677 // Note, this resets mAuxiliaryPixelBuffer handle
678 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
680 auto texture = Texture::New( TextureType::TEXTURE_2D,
681 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
682 auxiliaryPixelData.GetHeight() );
683 texture.Upload( auxiliaryPixelData );
684 textureSet.SetTexture( 1, texture );
685 mImpl->mRenderer.RegisterProperty( DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
686 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
688 mImpl->mRenderer.SetTextures( textureSet );
690 // Register transform properties
691 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
694 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
696 Geometry geometry = mFactoryCache.GetGeometry( subType );
699 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
701 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
703 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
705 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
707 mFactoryCache.SaveGeometry( subType, geometry );
712 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
714 uint16_t gridWidth = gridSize.GetWidth();
715 uint16_t gridHeight = gridSize.GetHeight();
718 Vector< Vector2 > vertices;
719 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
721 for( int y = 0; y < gridHeight + 1; ++y )
723 for( int x = 0; x < gridWidth + 1; ++x )
725 AddVertex( vertices, x, y );
730 Vector< unsigned short > indices;
731 indices.Reserve( gridWidth * gridHeight * 6 );
733 unsigned int rowIdx = 0;
734 unsigned int nextRowIdx = gridWidth + 1;
735 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
737 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
739 AddQuadIndices( indices, rowIdx, nextRowIdx );
743 return GenerateGeometry( vertices, indices );
746 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
748 uint16_t gridWidth = gridSize.GetWidth();
749 uint16_t gridHeight = gridSize.GetHeight();
752 Vector< Vector2 > vertices;
753 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
759 for( int x = 0; x < gridWidth + 1; ++x )
761 AddVertex( vertices, x, y );
765 for(; y < gridHeight - 1; ++y)
768 AddVertex( vertices, 0, y );
769 AddVertex( vertices, 1, y );
772 AddVertex( vertices, gridWidth - 1, y );
773 AddVertex( vertices, gridWidth, y );
777 for(; y < gridHeight + 1; ++y)
779 for( int x = 0; x < gridWidth + 1; ++x )
781 AddVertex( vertices, x, y );
786 Vector< unsigned short > indices;
787 indices.Reserve( gridWidth * gridHeight * 6 );
790 unsigned int rowIdx = 0 ;
791 unsigned int nextRowIdx = gridWidth + 1;
792 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
794 AddQuadIndices( indices, rowIdx, nextRowIdx );
799 rowIdx = gridWidth + 1;
800 nextRowIdx = ( gridWidth + 1 ) * 2;
802 unsigned increment = gridWidth - 1;
807 AddQuadIndices( indices, rowIdx, nextRowIdx );
809 rowIdx = gridWidth * 2;
810 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
812 AddQuadIndices( indices, rowIdx, nextRowIdx );
815 rowIdx = nextRowIdx - 2;
816 nextRowIdx = rowIdx + 4;
817 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
819 AddQuadIndices( indices, rowIdx, nextRowIdx );
824 AddQuadIndices( indices, rowIdx, nextRowIdx );
827 nextRowIdx += gridWidth - 1;
829 AddQuadIndices( indices, rowIdx, nextRowIdx );
833 rowIdx = nextRowIdx - gridWidth + 1;
834 nextRowIdx = rowIdx + gridWidth + 1;
835 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
837 AddQuadIndices( indices, rowIdx, nextRowIdx );
840 return GenerateGeometry( vertices, indices );
843 void NPatchVisual::SetResource()
845 const NPatchLoader::Data* data;
846 if( mImpl->mRenderer && mLoader.GetNPatchData( mId, data ) )
848 Geometry geometry = CreateGeometry();
849 Shader shader = CreateShader();
851 mImpl->mRenderer.SetGeometry( geometry );
852 mImpl->mRenderer.SetShader( shader );
854 Actor actor = mPlacementActor.GetHandle();
857 ApplyTextureAndUniforms();
858 actor.AddRenderer( mImpl->mRenderer );
859 mPlacementActor.Reset();
861 // npatch loaded and ready to display
862 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
867 void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
869 if( url.GetUrl() == mAuxiliaryUrl.GetUrl() )
871 mAuxiliaryPixelBuffer = pixelBuffer;
872 const NPatchLoader::Data* data;
873 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
880 Devel::PixelBuffer loadedPixelBuffer;
883 loadedPixelBuffer = pixelBuffer;
887 loadedPixelBuffer = LoadImageFromFile( mFactoryCache.GetTextureManager().GetBrokenImageUrl() );
889 mLoader.SetNPatchData( mId, loadedPixelBuffer, preMultiplied );
893 void NPatchVisual::NPatchLoadComplete( bool preMultiplied )
895 EnablePreMultipliedAlpha( preMultiplied );
897 if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
903 } // namespace Internal
905 } // namespace Toolkit