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/object/handle-devel.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/devel-api/adaptor-framework/image-loading.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/visuals/image-visual-properties.h>
29 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/internal/visuals/npatch-loader.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
37 #include <dali-toolkit/internal/visuals/rendering-addon.h>
50 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
51 attribute mediump vec2 aPosition;\n
52 varying mediump vec2 vTexCoord;\n
53 varying mediump vec2 vMaskTexCoord;\n
54 uniform highp mat4 uMvpMatrix;\n
55 uniform mediump vec3 uSize;\n
56 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
57 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
60 // Visual size and offset
61 uniform mediump vec2 offset;\n
62 uniform mediump vec2 size;\n
63 uniform mediump vec4 offsetSizeMode;\n
64 uniform mediump vec2 origin;\n
65 uniform mediump vec2 anchorPoint;\n
66 uniform mediump vec2 extraSize;\n
70 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
71 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
73 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
74 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
76 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
77 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
79 mediump vec4 gridPosition = vec4( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal, 0.0, 1.0 );\n
80 mediump vec4 vertexPosition = gridPosition;\n
81 vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
82 vertexPosition.xy += anchorPoint*visualSize + (visualOffset + origin)*uSize.xy;\n
83 vertexPosition = uMvpMatrix * vertexPosition;\n
85 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
86 vMaskTexCoord = gridPosition.xy / visualSize;\n
88 gl_Position = vertexPosition;\n
92 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
93 attribute mediump vec2 aPosition;\n
94 varying mediump vec2 vTexCoord;\n
95 varying mediump vec2 vMaskTexCoord;\n
96 uniform highp mat4 uMvpMatrix;\n
97 uniform mediump vec3 uSize;\n
98 uniform mediump vec2 uFixed[ 3 ];\n
99 uniform mediump vec2 uStretchTotal;\n
101 //Visual size and offset
102 uniform mediump vec2 offset;\n
103 uniform mediump vec2 size;\n
104 uniform mediump vec4 offsetSizeMode;\n
105 uniform mediump vec2 origin;\n
106 uniform mediump vec2 anchorPoint;\n
107 uniform mediump vec2 extraSize;\n
111 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
112 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
114 mediump vec2 size = visualSize.xy;\n
116 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
117 mediump vec2 stretch = floor( aPosition * 0.5 );\n
118 mediump vec2 fixedTotal = uFixed[ 2 ];\n
120 mediump vec4 gridPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
121 mediump vec4 vertexPosition = gridPosition;\n
122 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
123 vertexPosition.xy += anchorPoint*size + (visualOffset + origin)*uSize.xy;\n
125 vertexPosition = uMvpMatrix * vertexPosition;\n
127 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
129 vMaskTexCoord = gridPosition.xy / size;\n
130 gl_Position = vertexPosition;\n
134 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
135 varying mediump vec2 vTexCoord;\n
136 uniform sampler2D sTexture;\n
137 uniform lowp vec4 uColor;\n
138 uniform lowp vec3 mixColor;\n
139 uniform lowp float preMultipliedAlpha;\n
143 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n
147 const char* FRAGMENT_MASK_SHADER = DALI_COMPOSE_SHADER(
148 varying mediump vec2 vTexCoord;\n
149 varying mediump vec2 vMaskTexCoord;\n
150 uniform sampler2D sTexture;\n
151 uniform sampler2D sMask;\n
152 uniform lowp vec4 uColor;\n
153 uniform lowp vec3 mixColor;\n
154 uniform lowp float preMultipliedAlpha;\n
155 uniform mediump float auxiliaryImageAlpha;\n
159 // Where mask image is transparent, all of background image must show through.
160 // where mask image is opaque, only mask should be shown
161 // where mask is translucent, less of background should be shown.
162 // auxiliaryImageAlpha controls how much of mask is visible
164 mediump vec4 color = texture2D( sTexture, vTexCoord );\n
165 mediump vec4 mask = texture2D( sMask, vMaskTexCoord );\n
167 mediump vec3 mixedColor = color.rgb * mix( 1.0-mask.a, 1.0, 1.0-auxiliaryImageAlpha)
168 + mask.rgb*mask.a * auxiliaryImageAlpha;\n
169 gl_FragColor = vec4(mixedColor,1.0) * uColor * vec4( mixColor, 1.0 );\n
174 * @brief Creates the geometry formed from the vertices and indices
176 * @param[in] vertices The vertices to generate the geometry from
177 * @param[in] indices The indices to generate the geometry from
178 * @return The geometry formed from the vertices and indices
180 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
182 Property::Map vertexFormat;
183 vertexFormat[ "aPosition" ] = Property::VECTOR2;
184 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
185 if( vertices.Size() > 0 )
187 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
190 // Create the geometry object
191 Geometry geometry = Geometry::New();
192 geometry.AddVertexBuffer( vertexPropertyBuffer );
193 if( indices.Size() > 0 )
195 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
203 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
205 * @param[out] indices The indices to add to
206 * @param[in] rowIdx The row index to start the quad
207 * @param[in] nextRowIdx The index to the next row
209 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
211 indices.PushBack( rowIdx );
212 indices.PushBack( nextRowIdx + 1 );
213 indices.PushBack( rowIdx + 1 );
215 indices.PushBack( rowIdx );
216 indices.PushBack( nextRowIdx );
217 indices.PushBack( nextRowIdx + 1 );
220 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
222 vertices.PushBack( Vector2( x, y ) );
225 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent)
227 uint16_t prevEnd = 0;
228 uint16_t prevFix = 0;
229 uint16_t prevStretch = 0;
231 for( NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
233 uint16_t start = it->GetX();
234 uint16_t end = it->GetY();
236 uint16_t fix = prevFix + start - prevEnd;
237 uint16_t stretch = prevStretch + end - start;
239 std::stringstream uniform;
240 uniform << uniformName << "[" << i << "]";
241 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
245 prevStretch = stretch;
249 prevFix += imageExtent - prevEnd;
250 std::stringstream uniform;
251 uniform << uniformName << "[" << i << "]";
252 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
256 } //unnamed namespace
258 /////////////////NPatchVisual////////////////
260 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
262 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
263 nPatchVisual->mImageUrl = imageUrl;
264 nPatchVisual->SetProperties( properties );
269 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
271 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
272 nPatchVisual->mImageUrl = imageUrl;
277 void NPatchVisual::LoadImages()
279 TextureManager& textureManager = mFactoryCache.GetTextureManager();
280 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
282 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
284 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
285 mId = mLoader.Load( textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading );
287 const NPatchLoader::Data* data;
288 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
290 EnablePreMultipliedAlpha( preMultiplyOnLoad );
294 if( !mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
296 // Load the auxiliary image
297 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
298 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer( mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT,
299 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
300 this, true, preMultiplyOnLoading );
304 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
309 // load now if not already loaded
310 const NPatchLoader::Data* data;
311 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
313 naturalSize.x = data->croppedWidth;
314 naturalSize.y = data->croppedHeight;
318 if( mImageUrl.IsValid() )
320 ImageDimensions dimensions = Dali::GetOriginalImageSize( mImageUrl.GetUrl() );
321 if( dimensions != ImageDimensions( 0, 0 ) )
323 naturalSize.x = dimensions.GetWidth();
324 naturalSize.y = dimensions.GetHeight();
329 if( mAuxiliaryPixelBuffer )
331 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
332 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
336 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
338 // URL is already passed in via constructor
340 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
341 if( borderOnlyValue )
343 borderOnlyValue->Get( mBorderOnly );
346 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
347 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
349 // Not a rect so try vector4
351 if( borderValue->Get( border ) )
353 mBorder.left = static_cast< int >( border.x );
354 mBorder.right = static_cast< int >( border.y );
355 mBorder.bottom = static_cast< int >( border.z );
356 mBorder.top = static_cast< int >( border.w );
360 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
364 if( auxImage->Get( url ) )
370 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
373 auxImageAlpha->Get( mAuxiliaryImageAlpha );
376 Property::Value* synchronousLoading = propertyMap.Find( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING );
377 if( synchronousLoading )
380 synchronousLoading->Get( sync );
383 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
387 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
392 void NPatchVisual::DoSetOnScene( Actor& actor )
394 // load when first go on stage
397 const NPatchLoader::Data* data;
398 if( mLoader.GetNPatchData( mId, data ) )
400 Geometry geometry = CreateGeometry();
401 Shader shader = CreateShader();
403 mImpl->mRenderer = Renderer::New( geometry, shader );
405 mPlacementActor = actor;
406 if( data->loadCompleted )
408 if( RenderingAddOn::Get().IsValid() )
410 RenderingAddOn::Get().SubmitRenderTask( mImpl->mRenderer, data->renderingMap );
413 ApplyTextureAndUniforms();
414 actor.AddRenderer( mImpl->mRenderer );
415 mPlacementActor.Reset();
417 // npatch loaded and ready to display
418 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
423 void NPatchVisual::DoSetOffScene( Actor& actor )
425 actor.RemoveRenderer( mImpl->mRenderer );
426 mImpl->mRenderer.Reset();
427 mPlacementActor.Reset();
430 void NPatchVisual::OnSetTransform()
432 if( mImpl->mRenderer )
434 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
438 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
441 bool sync = IsSynchronousLoadingRequired();
442 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync );
443 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
444 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
445 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
446 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
448 if( mAuxiliaryUrl.IsValid() )
450 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
451 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
455 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
457 if( mAuxiliaryUrl.IsValid() )
459 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
460 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
464 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
465 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH ),
467 mLoader( factoryCache.GetNPatchLoader() ),
470 mId( NPatchLoader::UNINITIALIZED_ID ),
471 mBorderOnly( false ),
473 mAuxiliaryImageAlpha( 0.0f )
475 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
478 NPatchVisual::~NPatchVisual()
482 Geometry NPatchVisual::CreateGeometry()
485 const NPatchLoader::Data* data;
486 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
488 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
490 if( DALI_UNLIKELY( mBorderOnly ) )
492 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
496 if( data->renderingMap )
498 uint32_t elementCount[2];
499 geometry = RenderingAddOn::Get().CreateGeometryGrid(data->renderingMap, Uint16Pair(3, 3), elementCount );
503 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
507 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
509 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
510 if( !data->renderingMap )
512 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
516 uint32_t elementCount[2];
517 geometry = !mBorderOnly ?
518 RenderingAddOn::Get().CreateGeometryGrid(data->renderingMap, gridSize, elementCount ) : CreateBorderGeometry(gridSize );
524 // no N patch data so use default geometry
525 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
530 Shader NPatchVisual::CreateShader()
533 const NPatchLoader::Data* data;
534 // 0 is either no data (load failed?) or no stretch regions on image
535 // for both cases we use the default shader
536 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
537 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
539 auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
541 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
542 : VisualFactoryCache::NINE_PATCH_SHADER;
544 // ask loader for the regions
545 if( mLoader.GetNPatchData( mId, data ) )
547 xStretchCount = data->stretchPixelsX.Count();
548 yStretchCount = data->stretchPixelsY.Count();
551 if( DALI_LIKELY( !mImpl->mCustomShader ) )
553 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
554 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
556 shader = mFactoryCache.GetShader( shaderType );
557 if( DALI_UNLIKELY( !shader ) )
559 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader );
560 // Only cache vanilla 9 patch shaders
561 mFactoryCache.SaveShader( shaderType, shader );
564 else if( xStretchCount > 0 || yStretchCount > 0)
566 std::stringstream vertexShader;
567 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
568 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
571 shader = Shader::New( vertexShader.str(), fragmentShader );
576 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
578 if( !mImpl->mCustomShader->mFragmentShader.empty() )
580 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
582 hints = mImpl->mCustomShader->mHints;
584 /* Apply Custom Vertex Shader only if image is 9-patch */
585 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
586 ( xStretchCount == 0 && yStretchCount == 0 ) )
588 const char* vertexShader = VERTEX_SHADER_3X3;
590 if( !mImpl->mCustomShader->mVertexShader.empty() )
592 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
594 shader = Shader::New( vertexShader, fragmentShader, hints );
596 else if( xStretchCount > 0 || yStretchCount > 0)
598 std::stringstream vertexShader;
599 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
600 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
603 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
610 void NPatchVisual::ApplyTextureAndUniforms()
612 const NPatchLoader::Data* data;
613 TextureSet textureSet;
615 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
617 textureSet = data->textureSet;
619 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
621 //special case for 9 patch
622 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
623 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
625 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
626 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
628 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
629 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
630 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
631 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
635 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
636 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
638 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
639 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
644 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
645 textureSet = TextureSet::New();
647 Texture croppedImage = mFactoryCache.GetBrokenVisualImage();
648 textureSet.SetTexture( 0u, croppedImage );
649 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
650 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
651 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
652 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
655 if( mAuxiliaryPixelBuffer )
657 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
658 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
659 // on GL interpolation alone.
660 if( mAuxiliaryPixelBuffer.GetWidth() < data->croppedWidth &&
661 mAuxiliaryPixelBuffer.GetHeight() < data->croppedHeight )
663 mAuxiliaryPixelBuffer.Resize( data->croppedWidth, data->croppedHeight );
666 // Note, this resets mAuxiliaryPixelBuffer handle
667 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
669 auto texture = Texture::New( TextureType::TEXTURE_2D,
670 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
671 auxiliaryPixelData.GetHeight() );
672 texture.Upload( auxiliaryPixelData );
673 textureSet.SetTexture( 1, texture );
674 DevelHandle::RegisterProperty( mImpl->mRenderer, DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
675 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
677 mImpl->mRenderer.SetTextures( textureSet );
679 // Register transform properties
680 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
683 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
685 Geometry geometry = mFactoryCache.GetGeometry( subType );
688 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
690 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
692 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
694 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
696 mFactoryCache.SaveGeometry( subType, geometry );
701 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
703 uint16_t gridWidth = gridSize.GetWidth();
704 uint16_t gridHeight = gridSize.GetHeight();
707 Vector< Vector2 > vertices;
708 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
710 for( int y = 0; y < gridHeight + 1; ++y )
712 for( int x = 0; x < gridWidth + 1; ++x )
714 AddVertex( vertices, x, y );
719 Vector< unsigned short > indices;
720 indices.Reserve( gridWidth * gridHeight * 6 );
722 unsigned int rowIdx = 0;
723 unsigned int nextRowIdx = gridWidth + 1;
724 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
726 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
728 AddQuadIndices( indices, rowIdx, nextRowIdx );
732 return GenerateGeometry( vertices, indices );
735 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
737 uint16_t gridWidth = gridSize.GetWidth();
738 uint16_t gridHeight = gridSize.GetHeight();
741 Vector< Vector2 > vertices;
742 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
748 for( int x = 0; x < gridWidth + 1; ++x )
750 AddVertex( vertices, x, y );
754 for(; y < gridHeight - 1; ++y)
757 AddVertex( vertices, 0, y );
758 AddVertex( vertices, 1, y );
761 AddVertex( vertices, gridWidth - 1, y );
762 AddVertex( vertices, gridWidth, y );
766 for(; y < gridHeight + 1; ++y)
768 for( int x = 0; x < gridWidth + 1; ++x )
770 AddVertex( vertices, x, y );
775 Vector< unsigned short > indices;
776 indices.Reserve( gridWidth * gridHeight * 6 );
779 unsigned int rowIdx = 0 ;
780 unsigned int nextRowIdx = gridWidth + 1;
781 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
783 AddQuadIndices( indices, rowIdx, nextRowIdx );
788 rowIdx = gridWidth + 1;
789 nextRowIdx = ( gridWidth + 1 ) * 2;
791 unsigned increment = gridWidth - 1;
796 AddQuadIndices( indices, rowIdx, nextRowIdx );
798 rowIdx = gridWidth * 2;
799 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
801 AddQuadIndices( indices, rowIdx, nextRowIdx );
804 rowIdx = nextRowIdx - 2;
805 nextRowIdx = rowIdx + 4;
806 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
808 AddQuadIndices( indices, rowIdx, nextRowIdx );
813 AddQuadIndices( indices, rowIdx, nextRowIdx );
816 nextRowIdx += gridWidth - 1;
818 AddQuadIndices( indices, rowIdx, nextRowIdx );
822 rowIdx = nextRowIdx - gridWidth + 1;
823 nextRowIdx = rowIdx + gridWidth + 1;
824 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
826 AddQuadIndices( indices, rowIdx, nextRowIdx );
829 return GenerateGeometry( vertices, indices );
832 void NPatchVisual::SetResource()
834 const NPatchLoader::Data* data;
835 if( mImpl->mRenderer && mLoader.GetNPatchData( mId, data ) )
837 Geometry geometry = CreateGeometry();
838 Shader shader = CreateShader();
840 mImpl->mRenderer.SetGeometry( geometry );
841 mImpl->mRenderer.SetShader( shader );
843 Actor actor = mPlacementActor.GetHandle();
846 ApplyTextureAndUniforms();
847 actor.AddRenderer( mImpl->mRenderer );
848 mPlacementActor.Reset();
850 // npatch loaded and ready to display
851 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
856 void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
858 if( url.GetUrl() == mAuxiliaryUrl.GetUrl() )
860 mAuxiliaryPixelBuffer = pixelBuffer;
861 const NPatchLoader::Data* data;
862 if( mLoader.GetNPatchData( mId, data ) && data->loadCompleted )
871 mLoader.SetNPatchData( mId, pixelBuffer );
872 EnablePreMultipliedAlpha( preMultiplied );
875 if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
882 } // namespace Internal
884 } // namespace Toolkit