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/public-api/images/buffer-image.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/devel-api/object/handle-devel.h>
25 #include <dali/devel-api/images/texture-set-image.h>
26 #include <dali/devel-api/adaptor-framework/image-loading.h>
27 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
31 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33 #include <dali-toolkit/internal/visuals/npatch-loader.h>
34 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
36 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
37 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
38 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
51 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
52 attribute mediump vec2 aPosition;\n
53 varying mediump vec2 vTexCoord;\n
54 varying mediump vec2 vMaskTexCoord;\n
55 uniform highp mat4 uMvpMatrix;\n
56 uniform mediump vec3 uSize;\n
57 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
58 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
61 // Visual size and offset
62 uniform mediump vec2 offset;\n
63 uniform mediump vec2 size;\n
64 uniform mediump vec4 offsetSizeMode;\n
65 uniform mediump vec2 origin;\n
66 uniform mediump vec2 anchorPoint;\n
67 uniform mediump vec2 extraSize;\n
71 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
72 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
74 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
75 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
77 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
78 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
80 mediump vec4 gridPosition = vec4( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal, 0.0, 1.0 );\n
81 mediump vec4 vertexPosition = gridPosition;\n
82 vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
83 vertexPosition.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
84 vertexPosition = uMvpMatrix * vertexPosition;\n
86 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
87 vMaskTexCoord = gridPosition.xy / visualSize;\n
89 gl_Position = vertexPosition;\n
93 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
94 attribute mediump vec2 aPosition;\n
95 varying mediump vec2 vTexCoord;\n
96 varying mediump vec2 vMaskTexCoord;\n
97 uniform highp mat4 uMvpMatrix;\n
98 uniform mediump vec3 uSize;\n
99 uniform mediump vec2 uFixed[ 3 ];\n
100 uniform mediump vec2 uStretchTotal;\n
102 //Visual size and offset
103 uniform mediump vec2 offset;\n
104 uniform mediump vec2 size;\n
105 uniform mediump vec4 offsetSizeMode;\n
106 uniform mediump vec2 origin;\n
107 uniform mediump vec2 anchorPoint;\n
108 uniform mediump vec2 extraSize;\n
112 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
113 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
115 mediump vec2 size = visualSize.xy;\n
117 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
118 mediump vec2 stretch = floor( aPosition * 0.5 );\n
119 mediump vec2 fixedTotal = uFixed[ 2 ];\n
121 mediump vec4 gridPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
122 mediump vec4 vertexPosition = gridPosition;\n
123 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
124 vertexPosition.xy += anchorPoint*size + (visualOffset + origin)*uSize.xy;\n
126 vertexPosition = uMvpMatrix * vertexPosition;\n
128 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
130 vMaskTexCoord = gridPosition.xy / size;\n
131 gl_Position = vertexPosition;\n
135 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
136 varying mediump vec2 vTexCoord;\n
137 uniform sampler2D sTexture;\n
138 uniform lowp vec4 uColor;\n
139 uniform lowp vec3 mixColor;\n
140 uniform lowp float preMultipliedAlpha;\n
144 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
148 const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
149 varying mediump vec2 vTexCoord;\n
150 varying mediump vec2 vMaskTexCoord;\n
151 uniform sampler2D sTexture;\n
152 uniform sampler2D sMask;\n
153 uniform lowp vec4 uColor;\n
154 uniform lowp vec3 mixColor;\n
155 uniform lowp float preMultipliedAlpha;\n
156 uniform mediump float auxiliaryImageAlpha;\n
160 // Where mask image is transparent, all of background image must show through.
161 // where mask image is opaque, only mask should be shown
162 // where mask is translucent, less of background should be shown.
163 // auxiliaryImageAlpha controls how much of mask is visible
165 mediump vec4 color = texture2D( sTexture, vTexCoord );\n
166 mediump vec4 mask = texture2D( sMask, vMaskTexCoord );\n
168 mediump vec3 mixedColor = color.rgb * mix( 1.0-mask.a, 1.0, 1.0-auxiliaryImageAlpha)
169 + mask.rgb*mask.a * auxiliaryImageAlpha;\n
170 gl_FragColor = vec4(mixedColor,1.0) * uColor * vec4( mixColor, 1.0 );\n
175 * @brief Creates the geometry formed from the vertices and indices
177 * @param[in] vertices The vertices to generate the geometry from
178 * @param[in] indices The indices to generate the geometry from
179 * @return The geometry formed from the vertices and indices
181 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
183 Property::Map vertexFormat;
184 vertexFormat[ "aPosition" ] = Property::VECTOR2;
185 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
186 if( vertices.Size() > 0 )
188 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
191 // Create the geometry object
192 Geometry geometry = Geometry::New();
193 geometry.AddVertexBuffer( vertexPropertyBuffer );
194 if( indices.Size() > 0 )
196 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
204 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
206 * @param[out] indices The indices to add to
207 * @param[in] rowIdx The row index to start the quad
208 * @param[in] nextRowIdx The index to the next row
210 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
212 indices.PushBack( rowIdx );
213 indices.PushBack( nextRowIdx + 1 );
214 indices.PushBack( rowIdx + 1 );
216 indices.PushBack( rowIdx );
217 indices.PushBack( nextRowIdx );
218 indices.PushBack( nextRowIdx + 1 );
221 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
223 vertices.PushBack( Vector2( x, y ) );
226 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchLoader::StretchRanges& stretchPixels, uint16_t imageExtent)
228 uint16_t prevEnd = 0;
229 uint16_t prevFix = 0;
230 uint16_t prevStretch = 0;
232 for( NPatchLoader::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
234 uint16_t start = it->GetX();
235 uint16_t end = it->GetY();
237 uint16_t fix = prevFix + start - prevEnd;
238 uint16_t stretch = prevStretch + end - start;
240 std::stringstream uniform;
241 uniform << uniformName << "[" << i << "]";
242 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
246 prevStretch = stretch;
250 prevFix += imageExtent - prevEnd;
251 std::stringstream uniform;
252 uniform << uniformName << "[" << i << "]";
253 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
257 } //unnamed namespace
259 /////////////////NPatchVisual////////////////
261 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
263 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
264 nPatchVisual->mImageUrl = imageUrl;
265 nPatchVisual->SetProperties( properties );
270 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
272 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
273 nPatchVisual->mImageUrl = imageUrl;
278 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
280 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
281 VisualUrl visualUrl( image.GetUrl() );
282 nPatchVisual->mImageUrl = visualUrl;
286 void NPatchVisual::LoadImages()
288 TextureManager& textureManager = mFactoryCache.GetTextureManager();
289 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
291 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
293 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
294 mId = mLoader.Load( textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading );
296 const NPatchLoader::Data* data;
297 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
299 EnablePreMultipliedAlpha( preMultiplyOnLoad );
303 if( !mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
305 // Load the auxiliary image
306 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
307 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer( mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT,
308 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
309 this, true, preMultiplyOnLoading );
313 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
318 // load now if not already loaded
319 const NPatchLoader::Data* data;
320 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
322 naturalSize.x = data->croppedWidth;
323 naturalSize.y = data->croppedHeight;
327 if( mImageUrl.IsValid() )
329 ImageDimensions dimensions = Dali::GetOriginalImageSize( mImageUrl.GetUrl() );
330 if( dimensions != ImageDimensions( 0, 0 ) )
332 naturalSize.x = dimensions.GetWidth();
333 naturalSize.y = dimensions.GetHeight();
338 if( mAuxiliaryPixelBuffer )
340 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
341 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
345 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
347 // URL is already passed in via constructor
349 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
350 if( borderOnlyValue )
352 borderOnlyValue->Get( mBorderOnly );
355 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
356 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
358 // Not a rect so try vector4
360 if( borderValue->Get( border ) )
362 mBorder.left = static_cast< int >( border.x );
363 mBorder.right = static_cast< int >( border.y );
364 mBorder.bottom = static_cast< int >( border.z );
365 mBorder.top = static_cast< int >( border.w );
369 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
373 if( auxImage->Get( url ) )
379 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
382 auxImageAlpha->Get( mAuxiliaryImageAlpha );
385 Property::Value* synchronousLoading = propertyMap.Find( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING );
386 if( synchronousLoading )
389 synchronousLoading->Get( sync );
392 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
396 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
401 void NPatchVisual::DoSetOnStage( Actor& actor )
403 // load when first go on stage
406 const NPatchLoader::Data* data;
407 if( mLoader.GetNPatchData( mId, data ) )
409 Geometry geometry = CreateGeometry();
410 Shader shader = CreateShader();
412 mImpl->mRenderer = Renderer::New( geometry, shader );
414 mPlacementActor = actor;
415 if( data->loadCompleted )
417 ApplyTextureAndUniforms();
418 actor.AddRenderer( mImpl->mRenderer );
419 mPlacementActor.Reset();
421 // npatch loaded and ready to display
422 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
427 void NPatchVisual::DoSetOffStage( Actor& actor )
429 actor.RemoveRenderer( mImpl->mRenderer );
430 mImpl->mRenderer.Reset();
431 mPlacementActor.Reset();
434 void NPatchVisual::OnSetTransform()
436 if( mImpl->mRenderer )
438 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
442 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
445 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
446 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
447 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
448 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
450 if( mAuxiliaryUrl.IsValid() )
452 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
453 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
457 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
459 if( mAuxiliaryUrl.IsValid() )
461 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
462 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
466 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
467 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
469 mLoader( factoryCache.GetNPatchLoader() ),
472 mId( NPatchLoader::UNINITIALIZED_ID ),
473 mBorderOnly( false ),
475 mAuxiliaryImageAlpha( 0.0f )
477 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
480 NPatchVisual::~NPatchVisual()
484 Geometry NPatchVisual::CreateGeometry()
487 const NPatchLoader::Data* data;
488 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
490 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
492 if( DALI_UNLIKELY( mBorderOnly ) )
494 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
498 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
501 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
503 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
504 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
509 // no N patch data so use default geometry
510 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
515 Shader NPatchVisual::CreateShader()
518 const NPatchLoader::Data* data;
519 // 0 is either no data (load failed?) or no stretch regions on image
520 // for both cases we use the default shader
521 NPatchLoader::StretchRanges::SizeType xStretchCount = 0;
522 NPatchLoader::StretchRanges::SizeType yStretchCount = 0;
524 auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
526 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
527 : VisualFactoryCache::NINE_PATCH_SHADER;
529 // ask loader for the regions
530 if( mLoader.GetNPatchData( mId, data ) )
532 xStretchCount = data->stretchPixelsX.Count();
533 yStretchCount = data->stretchPixelsY.Count();
536 if( DALI_LIKELY( !mImpl->mCustomShader ) )
538 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
539 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
541 shader = mFactoryCache.GetShader( shaderType );
542 if( DALI_UNLIKELY( !shader ) )
544 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader );
545 // Only cache vanilla 9 patch shaders
546 mFactoryCache.SaveShader( shaderType, shader );
549 else if( xStretchCount > 0 || yStretchCount > 0)
551 std::stringstream vertexShader;
552 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
553 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
556 shader = Shader::New( vertexShader.str(), fragmentShader );
561 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
563 if( !mImpl->mCustomShader->mFragmentShader.empty() )
565 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
567 hints = mImpl->mCustomShader->mHints;
569 /* Apply Custom Vertex Shader only if image is 9-patch */
570 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
571 ( xStretchCount == 0 && yStretchCount == 0 ) )
573 const char* vertexShader = VERTEX_SHADER_3X3;
575 if( !mImpl->mCustomShader->mVertexShader.empty() )
577 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
579 shader = Shader::New( vertexShader, fragmentShader, hints );
581 else if( xStretchCount > 0 || yStretchCount > 0)
583 std::stringstream vertexShader;
584 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
585 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
588 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
595 void NPatchVisual::ApplyTextureAndUniforms()
597 const NPatchLoader::Data* data;
598 TextureSet textureSet;
600 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
602 textureSet = data->textureSet;
604 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
606 //special case for 9 patch
607 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
608 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
610 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
611 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
613 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
614 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
615 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
616 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
620 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
621 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
623 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
624 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
629 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
630 textureSet = TextureSet::New();
632 Image croppedImage = mFactoryCache.GetBrokenVisualImage();
633 TextureSetImage( textureSet, 0u, croppedImage );
634 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
635 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
636 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
637 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
640 if( mAuxiliaryPixelBuffer )
642 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
643 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
644 // on GL interpolation alone.
645 if( mAuxiliaryPixelBuffer.GetWidth() < data->croppedWidth &&
646 mAuxiliaryPixelBuffer.GetHeight() < data->croppedHeight )
648 mAuxiliaryPixelBuffer.Resize( data->croppedWidth, data->croppedHeight );
651 // Note, this resets mAuxiliaryPixelBuffer handle
652 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
654 auto texture = Texture::New( TextureType::TEXTURE_2D,
655 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
656 auxiliaryPixelData.GetHeight() );
657 texture.Upload( auxiliaryPixelData );
658 textureSet.SetTexture( 1, texture );
659 DevelHandle::RegisterProperty( mImpl->mRenderer, DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
660 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
662 mImpl->mRenderer.SetTextures( textureSet );
664 // Register transform properties
665 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
668 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
670 Geometry geometry = mFactoryCache.GetGeometry( subType );
673 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
675 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
677 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
679 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
681 mFactoryCache.SaveGeometry( subType, geometry );
686 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
688 uint16_t gridWidth = gridSize.GetWidth();
689 uint16_t gridHeight = gridSize.GetHeight();
692 Vector< Vector2 > vertices;
693 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
695 for( int y = 0; y < gridHeight + 1; ++y )
697 for( int x = 0; x < gridWidth + 1; ++x )
699 AddVertex( vertices, x, y );
704 Vector< unsigned short > indices;
705 indices.Reserve( gridWidth * gridHeight * 6 );
707 unsigned int rowIdx = 0;
708 unsigned int nextRowIdx = gridWidth + 1;
709 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
711 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
713 AddQuadIndices( indices, rowIdx, nextRowIdx );
717 return GenerateGeometry( vertices, indices );
720 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
722 uint16_t gridWidth = gridSize.GetWidth();
723 uint16_t gridHeight = gridSize.GetHeight();
726 Vector< Vector2 > vertices;
727 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
733 for( int x = 0; x < gridWidth + 1; ++x )
735 AddVertex( vertices, x, y );
739 for(; y < gridHeight - 1; ++y)
742 AddVertex( vertices, 0, y );
743 AddVertex( vertices, 1, y );
746 AddVertex( vertices, gridWidth - 1, y );
747 AddVertex( vertices, gridWidth, y );
751 for(; y < gridHeight + 1; ++y)
753 for( int x = 0; x < gridWidth + 1; ++x )
755 AddVertex( vertices, x, y );
760 Vector< unsigned short > indices;
761 indices.Reserve( gridWidth * gridHeight * 6 );
764 unsigned int rowIdx = 0 ;
765 unsigned int nextRowIdx = gridWidth + 1;
766 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
768 AddQuadIndices( indices, rowIdx, nextRowIdx );
773 rowIdx = gridWidth + 1;
774 nextRowIdx = ( gridWidth + 1 ) * 2;
776 unsigned increment = gridWidth - 1;
781 AddQuadIndices( indices, rowIdx, nextRowIdx );
783 rowIdx = gridWidth * 2;
784 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
786 AddQuadIndices( indices, rowIdx, nextRowIdx );
789 rowIdx = nextRowIdx - 2;
790 nextRowIdx = rowIdx + 4;
791 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
793 AddQuadIndices( indices, rowIdx, nextRowIdx );
798 AddQuadIndices( indices, rowIdx, nextRowIdx );
801 nextRowIdx += gridWidth - 1;
803 AddQuadIndices( indices, rowIdx, nextRowIdx );
807 rowIdx = nextRowIdx - gridWidth + 1;
808 nextRowIdx = rowIdx + gridWidth + 1;
809 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
811 AddQuadIndices( indices, rowIdx, nextRowIdx );
814 return GenerateGeometry( vertices, indices );
817 void NPatchVisual::SetResource()
819 const NPatchLoader::Data* data;
820 if( mImpl->mRenderer && mLoader.GetNPatchData( mId, data ) )
822 Geometry geometry = CreateGeometry();
823 Shader shader = CreateShader();
825 mImpl->mRenderer.SetGeometry( geometry );
826 mImpl->mRenderer.SetShader( shader );
828 Actor actor = mPlacementActor.GetHandle();
831 ApplyTextureAndUniforms();
832 actor.AddRenderer( mImpl->mRenderer );
833 mPlacementActor.Reset();
835 // npatch loaded and ready to display
836 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
841 void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
843 if( url.GetUrl() == mAuxiliaryUrl.GetUrl() )
845 mAuxiliaryPixelBuffer = pixelBuffer;
846 const NPatchLoader::Data* data;
847 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
856 mLoader.SetNPatchData( mId, pixelBuffer );
857 EnablePreMultipliedAlpha( preMultiplied );
860 if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
867 } // namespace Internal
869 } // namespace Toolkit