2 * Copyright (c) 2018 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
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 );\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
110 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\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 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
184 if( vertices.Size() > 0 )
186 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
189 // Create the geometry object
190 Geometry geometry = Geometry::New();
191 geometry.AddVertexBuffer( vertexPropertyBuffer );
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 NPatchLoader::StretchRanges& stretchPixels, uint16_t imageExtent)
226 uint16_t prevEnd = 0;
227 uint16_t prevFix = 0;
228 uint16_t prevStretch = 0;
230 for( NPatchLoader::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 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
278 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
279 VisualUrl visualUrl( image.GetUrl() );
280 nPatchVisual->mImageUrl = visualUrl;
284 void NPatchVisual::LoadImages()
286 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
288 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
290 mId = mLoader.Load( mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad );
292 EnablePreMultipliedAlpha( preMultiplyOnLoad );
295 if( ! mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
297 // Load the auxiliary image synchronously
298 mAuxiliaryPixelBuffer = Dali::LoadImageFromFile( mAuxiliaryUrl.GetUrl(), ImageDimensions(),
299 FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, true );
303 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
308 // load now if not already loaded
311 const NPatchLoader::Data* data;
312 if( mLoader.GetNPatchData( mId, data ) )
314 naturalSize.x = data->croppedWidth;
315 naturalSize.y = data->croppedHeight;
318 if( mAuxiliaryPixelBuffer )
320 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
321 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
325 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
327 // URL is already passed in via constructor
329 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
330 if( borderOnlyValue )
332 borderOnlyValue->Get( mBorderOnly );
335 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
336 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
338 // Not a rect so try vector4
340 if( borderValue->Get( border ) )
342 mBorder.left = static_cast< int >( border.x );
343 mBorder.right = static_cast< int >( border.y );
344 mBorder.bottom = static_cast< int >( border.z );
345 mBorder.top = static_cast< int >( border.w );
349 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
353 if( auxImage->Get( url ) )
359 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
362 auxImageAlpha->Get( mAuxiliaryImageAlpha );
366 void NPatchVisual::DoSetOnStage( Actor& actor )
368 // load when first go on stage
371 Geometry geometry = CreateGeometry();
372 Shader shader = CreateShader();
373 mImpl->mRenderer = Renderer::New( geometry, shader );
375 ApplyTextureAndUniforms();
377 actor.AddRenderer( mImpl->mRenderer );
379 // npatch loaded and ready to display
380 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
383 void NPatchVisual::DoSetOffStage( Actor& actor )
385 actor.RemoveRenderer( mImpl->mRenderer );
386 mImpl->mRenderer.Reset();
389 void NPatchVisual::OnSetTransform()
391 if( mImpl->mRenderer )
393 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
397 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
400 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
401 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
402 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
403 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
405 if( mAuxiliaryUrl.IsValid() )
407 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
408 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
412 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
414 if( mAuxiliaryUrl.IsValid() )
416 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
417 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
421 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
422 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
423 mLoader( factoryCache.GetNPatchLoader() ),
426 mId( NPatchLoader::UNINITIALIZED_ID ),
427 mBorderOnly( false ),
429 mAuxiliaryImageAlpha( 0.0f )
431 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
434 NPatchVisual::~NPatchVisual()
438 Geometry NPatchVisual::CreateGeometry()
441 const NPatchLoader::Data* data;
442 if( mLoader.GetNPatchData( mId, data ) )
444 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
446 if( DALI_UNLIKELY( mBorderOnly ) )
448 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
452 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
455 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
457 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
458 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
463 // no N patch data so use default geometry
464 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
469 Shader NPatchVisual::CreateShader()
472 const NPatchLoader::Data* data;
473 // 0 is either no data (load failed?) or no stretch regions on image
474 // for both cases we use the default shader
475 NPatchLoader::StretchRanges::SizeType xStretchCount = 0;
476 NPatchLoader::StretchRanges::SizeType yStretchCount = 0;
478 auto fragmentShader = mAuxiliaryPixelBuffer ? FRAGMENT_MASK_SHADER
480 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
481 : VisualFactoryCache::NINE_PATCH_SHADER;
483 // ask loader for the regions
484 if( mLoader.GetNPatchData( mId, data ) )
486 xStretchCount = data->stretchPixelsX.Count();
487 yStretchCount = data->stretchPixelsY.Count();
490 if( DALI_LIKELY( !mImpl->mCustomShader ) )
492 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
493 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
495 shader = mFactoryCache.GetShader( shaderType );
496 if( DALI_UNLIKELY( !shader ) )
498 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader );
499 // Only cache vanilla 9 patch shaders
500 mFactoryCache.SaveShader( shaderType, shader );
503 else if( xStretchCount > 0 || yStretchCount > 0)
505 std::stringstream vertexShader;
506 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
507 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
510 shader = Shader::New( vertexShader.str(), fragmentShader );
515 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
517 if( !mImpl->mCustomShader->mFragmentShader.empty() )
519 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
521 hints = mImpl->mCustomShader->mHints;
523 /* Apply Custom Vertex Shader only if image is 9-patch */
524 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
525 ( xStretchCount == 0 && yStretchCount == 0 ) )
527 const char* vertexShader = VERTEX_SHADER_3X3;
529 if( !mImpl->mCustomShader->mVertexShader.empty() )
531 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
533 shader = Shader::New( vertexShader, fragmentShader, hints );
535 else if( xStretchCount > 0 || yStretchCount > 0)
537 std::stringstream vertexShader;
538 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
539 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
542 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
549 void NPatchVisual::ApplyTextureAndUniforms()
551 const NPatchLoader::Data* data;
552 TextureSet textureSet;
554 if( mLoader.GetNPatchData( mId, data ) )
556 textureSet = data->textureSet;
558 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
560 //special case for 9 patch
561 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
562 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
564 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
565 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
567 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
568 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
569 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
570 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
574 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
575 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
577 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
578 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
583 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
584 textureSet = TextureSet::New();
586 Image croppedImage = mFactoryCache.GetBrokenVisualImage();
587 TextureSetImage( textureSet, 0u, croppedImage );
588 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
589 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
590 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
591 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
594 if( mAuxiliaryPixelBuffer )
596 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
597 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
598 // on GL interpolation alone.
599 if( mAuxiliaryPixelBuffer.GetWidth() < data->croppedWidth &&
600 mAuxiliaryPixelBuffer.GetHeight() < data->croppedHeight )
602 mAuxiliaryPixelBuffer.Resize( data->croppedWidth, data->croppedHeight );
605 // Note, this resets mAuxiliaryPixelBuffer handle
606 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
608 auto texture = Texture::New( TextureType::TEXTURE_2D,
609 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
610 auxiliaryPixelData.GetHeight() );
611 texture.Upload( auxiliaryPixelData );
612 textureSet.SetTexture( 1, texture );
613 DevelHandle::RegisterProperty( mImpl->mRenderer, DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
614 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
616 mImpl->mRenderer.SetTextures( textureSet );
618 // Register transform properties
619 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
622 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
624 Geometry geometry = mFactoryCache.GetGeometry( subType );
627 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
629 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
631 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
633 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
635 mFactoryCache.SaveGeometry( subType, geometry );
640 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
642 uint16_t gridWidth = gridSize.GetWidth();
643 uint16_t gridHeight = gridSize.GetHeight();
646 Vector< Vector2 > vertices;
647 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
649 for( int y = 0; y < gridHeight + 1; ++y )
651 for( int x = 0; x < gridWidth + 1; ++x )
653 AddVertex( vertices, x, y );
658 Vector< unsigned short > indices;
659 indices.Reserve( gridWidth * gridHeight * 6 );
661 unsigned int rowIdx = 0;
662 unsigned int nextRowIdx = gridWidth + 1;
663 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
665 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
667 AddQuadIndices( indices, rowIdx, nextRowIdx );
671 return GenerateGeometry( vertices, indices );
674 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
676 uint16_t gridWidth = gridSize.GetWidth();
677 uint16_t gridHeight = gridSize.GetHeight();
680 Vector< Vector2 > vertices;
681 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
687 for( int x = 0; x < gridWidth + 1; ++x )
689 AddVertex( vertices, x, y );
693 for(; y < gridHeight - 1; ++y)
696 AddVertex( vertices, 0, y );
697 AddVertex( vertices, 1, y );
700 AddVertex( vertices, gridWidth - 1, y );
701 AddVertex( vertices, gridWidth, y );
705 for(; y < gridHeight + 1; ++y)
707 for( int x = 0; x < gridWidth + 1; ++x )
709 AddVertex( vertices, x, y );
714 Vector< unsigned short > indices;
715 indices.Reserve( gridWidth * gridHeight * 6 );
718 unsigned int rowIdx = 0 ;
719 unsigned int nextRowIdx = gridWidth + 1;
720 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
722 AddQuadIndices( indices, rowIdx, nextRowIdx );
727 rowIdx = gridWidth + 1;
728 nextRowIdx = ( gridWidth + 1 ) * 2;
730 unsigned increment = gridWidth - 1;
735 AddQuadIndices( indices, rowIdx, nextRowIdx );
737 rowIdx = gridWidth * 2;
738 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
740 AddQuadIndices( indices, rowIdx, nextRowIdx );
743 rowIdx = nextRowIdx - 2;
744 nextRowIdx = rowIdx + 4;
745 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
747 AddQuadIndices( indices, rowIdx, nextRowIdx );
752 AddQuadIndices( indices, rowIdx, nextRowIdx );
755 nextRowIdx += gridWidth - 1;
757 AddQuadIndices( indices, rowIdx, nextRowIdx );
761 rowIdx = nextRowIdx - gridWidth + 1;
762 nextRowIdx = rowIdx + gridWidth + 1;
763 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
765 AddQuadIndices( indices, rowIdx, nextRowIdx );
768 return GenerateGeometry( vertices, indices );
771 } // namespace Internal
773 } // namespace Toolkit