df9b2ff74e541eecf9f945c95f42d6f95a484dd2
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "npatch-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/buffer-image.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/devel-api/images/texture-set-image.h>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/visuals/image-visual-properties.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
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 namespace
47 {
48 const char * const BORDER_ONLY( "borderOnly" );
49 const char * const BORDER( "border" );
50
51 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
52   attribute mediump vec2 aPosition;\n
53   varying mediump vec2 vTexCoord;\n
54   uniform mediump 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
58   \n
59
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
67   void main()\n
68   {\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
71     \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
74     \n
75
76     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
77     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
78
79     mediump vec4 vertexPosition = vec4( ( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal ) +  anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
80     vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
81
82     vertexPosition = uMvpMatrix * vertexPosition;\n
83     \n
84     vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
85     \n
86     gl_Position = vertexPosition;\n
87   }\n
88 );
89
90 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
91     attribute mediump vec2 aPosition;\n
92     varying mediump vec2 vTexCoord;\n
93     uniform mediump mat4 uModelMatrix;\n
94     uniform mediump mat4 uMvpMatrix;\n
95     uniform mediump vec3 uSize;\n
96     uniform mediump vec2 uFixed[ 3 ];\n
97     uniform mediump vec2 uStretchTotal;\n
98     \n
99
100     //Visual size and offset
101     uniform mediump vec2 offset;\n
102     uniform mediump vec2 size;\n
103     uniform mediump vec4 offsetSizeMode;\n
104     uniform mediump vec2 origin;\n
105     uniform mediump vec2 anchorPoint;\n
106
107     void main()\n
108     {\n
109       vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
110       vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
111
112       mediump vec2 size         = visualSize.xy;\n
113       \n
114       mediump vec2 fixedFactor  = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y  + 1.0 ) * 0.5 ) ].y );\n
115       mediump vec2 stretch      = floor( aPosition * 0.5 );\n
116       mediump vec2 fixedTotal   = uFixed[ 2 ];\n
117       \n
118       mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );
119       vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
120       vertexPosition.xy =  vertexPosition.xy + anchorPoint*size + (visualOffset + origin)*uSize.xy;\
121       \n
122       vertexPosition = uMvpMatrix * vertexPosition;\n
123       \n
124       vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
125       \n
126       gl_Position = vertexPosition;\n
127     }\n
128 );
129
130 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
131   varying mediump vec2 vTexCoord;\n
132   uniform sampler2D sTexture;\n
133   uniform lowp vec4 uColor;\n
134   uniform lowp vec3 mixColor;\n
135   uniform lowp float opacity;\n
136   uniform lowp float preMultipliedAlpha;\n
137   lowp vec4 visualMixColor()\n
138   {\n
139     return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
140   }\n
141   void main()\n
142   {\n
143     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
144   }\n
145 );
146
147 /**
148  * @brief Creates the geometry formed from the vertices and indices
149  *
150  * @param[in]  vertices             The vertices to generate the geometry from
151  * @param[in]  indices              The indices to generate the geometry from
152  * @return The geometry formed from the vertices and indices
153  */
154 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
155 {
156   Property::Map vertexFormat;
157   vertexFormat[ "aPosition" ] = Property::VECTOR2;
158   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
159   if( vertices.Size() > 0 )
160   {
161     vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
162   }
163
164   // Create the geometry object
165   Geometry geometry = Geometry::New();
166   geometry.AddVertexBuffer( vertexPropertyBuffer );
167   if( indices.Size() > 0 )
168   {
169     geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
170   }
171
172
173   return geometry;
174 }
175
176 /**
177  * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
178  *
179  * @param[out] indices     The indices to add to
180  * @param[in]  rowIdx      The row index to start the quad
181  * @param[in]  nextRowIdx  The index to the next row
182  */
183 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
184 {
185   indices.PushBack( rowIdx );
186   indices.PushBack( nextRowIdx + 1 );
187   indices.PushBack( rowIdx + 1 );
188
189   indices.PushBack( rowIdx );
190   indices.PushBack( nextRowIdx );
191   indices.PushBack( nextRowIdx + 1 );
192 }
193
194 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
195 {
196   vertices.PushBack( Vector2( x, y ) );
197 }
198
199 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
200 {
201   uint16_t prevEnd = 0;
202   uint16_t prevFix = 0;
203   uint16_t prevStretch = 0;
204   unsigned int i = 1;
205   for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
206   {
207     uint16_t start = it->GetX();
208     uint16_t end = it->GetY();
209
210     uint16_t fix = prevFix + start - prevEnd;
211     uint16_t stretch = prevStretch + end - start;
212
213     std::stringstream uniform;
214     uniform << uniformName << "[" << i << "]";
215     renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
216
217     prevEnd = end;
218     prevFix = fix;
219     prevStretch = stretch;
220   }
221
222   {
223     prevFix += imageExtent - prevEnd;
224     std::stringstream uniform;
225     uniform << uniformName << "[" << i << "]";
226     renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
227   }
228 }
229
230 } //unnamed namespace
231
232 /////////////////NPatchVisual////////////////
233
234 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
235 {
236   NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
237   nPatchVisual->mImageUrl = imageUrl;
238   nPatchVisual->SetProperties( properties );
239
240   return nPatchVisual;
241 }
242
243 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
244 {
245   NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
246   nPatchVisual->mImageUrl = imageUrl;
247
248   return nPatchVisual;
249 }
250
251 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
252 {
253   NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
254   VisualUrl visualUrl( image.GetUrl() );
255   nPatchVisual->mImageUrl = visualUrl;
256   return nPatchVisual;
257 }
258
259 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
260 {
261   naturalSize.x = 0u;
262   naturalSize.y = 0u;
263
264   // load now if not already loaded
265   if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
266   {
267     mId = mLoader.Load( mImageUrl.GetUrl(), mBorder );
268   }
269   const NPatchLoader::Data* data;
270   if( mLoader.GetNPatchData( mId, data ) )
271   {
272     naturalSize.x = data->croppedWidth;
273     naturalSize.y = data->croppedHeight;
274   }
275 }
276
277 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
278 {
279   // URL is already passed in via constructor
280
281   Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
282   if( borderOnlyValue )
283   {
284     borderOnlyValue->Get( mBorderOnly );
285   }
286
287   Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
288   if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
289   {
290     // Not a rect so try vector4
291     Vector4 border;
292     if( borderValue->Get( border ) )
293     {
294       mBorder.left = static_cast< int >( border.x );
295       mBorder.right = static_cast< int >( border.y );
296       mBorder.bottom = static_cast< int >( border.z );
297       mBorder.top = static_cast< int >( border.w );
298     }
299   }
300 }
301
302 void NPatchVisual::DoSetOnStage( Actor& actor )
303 {
304   // load when first go on stage
305   if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
306   {
307     mId = mLoader.Load( mImageUrl.GetUrl(), mBorder );
308   }
309
310   Geometry geometry = CreateGeometry();
311   Shader shader = CreateShader();
312   mImpl->mRenderer = Renderer::New( geometry, shader );
313
314   ApplyTextureAndUniforms();
315
316   actor.AddRenderer( mImpl->mRenderer );
317
318   // npatch loaded and ready to display
319   ResourceReady();
320 }
321
322 void NPatchVisual::DoSetOffStage( Actor& actor )
323 {
324   actor.RemoveRenderer( mImpl->mRenderer );
325   mImpl->mRenderer.Reset();
326 }
327
328 void NPatchVisual::OnSetTransform()
329 {
330   if( mImpl->mRenderer )
331   {
332     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
333   }
334 }
335
336 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
337 {
338   map.Clear();
339   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
340   map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
341   map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
342   map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
343 }
344
345 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
346 {
347   // Do nothing
348 }
349
350 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
351 : Visual::Base( factoryCache ),
352   mLoader( factoryCache.GetNPatchLoader() ),
353   mImageUrl(),
354   mId( NPatchLoader::UNINITIALIZED_ID ),
355   mBorderOnly( false ),
356   mBorder()
357 {
358 }
359
360 NPatchVisual::~NPatchVisual()
361 {
362 }
363
364 Geometry NPatchVisual::CreateGeometry()
365 {
366   Geometry geometry;
367   const NPatchLoader::Data* data;
368   if( mLoader.GetNPatchData( mId, data ) )
369   {
370     if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
371     {
372       if( DALI_UNLIKELY( mBorderOnly ) )
373       {
374         geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
375       }
376       else
377       {
378         geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
379       }
380     }
381     else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
382     {
383       Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1,  2 * data->stretchPixelsY.Size() + 1 );
384       geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
385     }
386   }
387   else
388   {
389     // no N patch data so use default geometry
390     geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
391   }
392   return geometry;
393 }
394
395 Shader NPatchVisual::CreateShader()
396 {
397   Shader shader;
398   const NPatchLoader::Data* data;
399   // 0 is either no data (load failed?) or no stretch regions on image
400   // for both cases we use the default shader
401   NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
402   NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
403
404   // ask loader for the regions
405   if( mLoader.GetNPatchData( mId, data ) )
406   {
407     xStretchCount = data->stretchPixelsX.Count();
408     yStretchCount = data->stretchPixelsY.Count();
409   }
410
411   if( DALI_LIKELY( !mImpl->mCustomShader ) )
412   {
413     if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
414                      ( xStretchCount == 0 && yStretchCount == 0 ) ) )
415     {
416       shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
417       if( DALI_UNLIKELY( !shader ) )
418       {
419         shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
420         mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
421       }
422     }
423     else if( xStretchCount > 0 || yStretchCount > 0)
424     {
425       std::stringstream vertexShader;
426       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
427                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
428                    << VERTEX_SHADER;
429
430       shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
431     }
432   }
433   else
434   {
435     const char* fragmentShader = FRAGMENT_SHADER;
436     Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
437
438     if( !mImpl->mCustomShader->mFragmentShader.empty() )
439     {
440       fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
441     }
442     hints = mImpl->mCustomShader->mHints;
443
444     /* Apply Custom Vertex Shader only if image is 9-patch */
445     if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
446         ( xStretchCount == 0 && yStretchCount == 0 ) )
447     {
448       const char* vertexShader = VERTEX_SHADER_3X3;
449
450       if( !mImpl->mCustomShader->mVertexShader.empty() )
451       {
452         vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
453       }
454       shader = Shader::New( vertexShader, fragmentShader, hints );
455     }
456     else if( xStretchCount > 0 || yStretchCount > 0)
457     {
458       std::stringstream vertexShader;
459       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
460                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
461                    << VERTEX_SHADER;
462
463       shader = Shader::New( vertexShader.str(), fragmentShader, hints );
464     }
465   }
466
467   return shader;
468 }
469
470 void NPatchVisual::ApplyTextureAndUniforms()
471 {
472   const NPatchLoader::Data* data;
473   if( mLoader.GetNPatchData( mId, data ) )
474   {
475     TextureSet textures( data->textureSet );
476     mImpl->mRenderer.SetTextures( textures );
477     if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
478     {
479       //special case for 9 patch
480       Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
481       Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
482
483       uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
484       uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
485
486       mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
487       mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
488       mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
489       mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
490     }
491     else
492     {
493       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
494       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
495
496       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
497       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
498     }
499   }
500   else
501   {
502     DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
503     TextureSet textureSet = TextureSet::New();
504     mImpl->mRenderer.SetTextures( textureSet );
505     Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
506     TextureSetImage( textureSet, 0u, croppedImage );
507     mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
508     mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
509     mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
510     mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
511   }
512
513   //Register transform properties
514   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
515 }
516
517 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
518 {
519   Geometry geometry = mFactoryCache.GetGeometry( subType );
520   if( !geometry )
521   {
522     if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
523     {
524       geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
525     }
526     else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
527     {
528       geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
529     }
530     mFactoryCache.SaveGeometry( subType, geometry );
531   }
532   return geometry;
533 }
534
535 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
536 {
537   uint16_t gridWidth = gridSize.GetWidth();
538   uint16_t gridHeight = gridSize.GetHeight();
539
540   // Create vertices
541   Vector< Vector2 > vertices;
542   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
543
544   for( int y = 0; y < gridHeight + 1; ++y )
545   {
546     for( int x = 0; x < gridWidth + 1; ++x )
547     {
548       AddVertex( vertices, x, y );
549     }
550   }
551
552   // Create indices
553   Vector< unsigned short > indices;
554   indices.Reserve( gridWidth * gridHeight * 6 );
555
556   unsigned int rowIdx     = 0;
557   unsigned int nextRowIdx = gridWidth + 1;
558   for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
559   {
560     for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
561     {
562       AddQuadIndices( indices, rowIdx, nextRowIdx );
563     }
564   }
565
566   return GenerateGeometry( vertices, indices );
567 }
568
569 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
570 {
571   uint16_t gridWidth = gridSize.GetWidth();
572   uint16_t gridHeight = gridSize.GetHeight();
573
574   // Create vertices
575   Vector< Vector2 > vertices;
576   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
577
578   //top
579   int y = 0;
580   for(; y < 2; ++y)
581   {
582     for( int x = 0; x < gridWidth + 1; ++x )
583     {
584       AddVertex( vertices, x, y );
585     }
586   }
587
588   for(; y < gridHeight - 1; ++y)
589   {
590     //left
591     AddVertex( vertices, 0, y );
592     AddVertex( vertices, 1, y );
593
594     //right
595     AddVertex( vertices, gridWidth - 1, y );
596     AddVertex( vertices, gridWidth, y );
597   }
598
599   //bottom
600   for(; y < gridHeight + 1; ++y)
601   {
602     for( int x = 0; x < gridWidth + 1; ++x )
603     {
604       AddVertex( vertices, x, y );
605     }
606   }
607
608   // Create indices
609   Vector< unsigned short > indices;
610   indices.Reserve( gridWidth * gridHeight * 6 );
611
612   //top
613   unsigned int rowIdx     = 0 ;
614   unsigned int nextRowIdx = gridWidth + 1;
615   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
616   {
617     AddQuadIndices( indices, rowIdx, nextRowIdx );
618   }
619
620   if(gridHeight > 2)
621   {
622     rowIdx     = gridWidth + 1;
623     nextRowIdx = ( gridWidth + 1 ) * 2;
624
625     unsigned increment = gridWidth - 1;
626     if(gridHeight > 3)
627     {
628       increment = 2;
629       //second row left
630       AddQuadIndices( indices, rowIdx, nextRowIdx );
631
632       rowIdx     = gridWidth * 2;
633       nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
634       //second row right
635       AddQuadIndices( indices, rowIdx, nextRowIdx );
636
637       //left and right
638       rowIdx     = nextRowIdx - 2;
639       nextRowIdx = rowIdx + 4;
640       for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
641       {
642         AddQuadIndices( indices, rowIdx, nextRowIdx );
643       }
644     }
645
646     //second row left
647     AddQuadIndices( indices, rowIdx, nextRowIdx );
648
649     rowIdx     += increment;
650     nextRowIdx += gridWidth - 1;
651     //second row right
652     AddQuadIndices( indices, rowIdx, nextRowIdx );
653   }
654
655   //bottom
656   rowIdx     = nextRowIdx - gridWidth + 1;
657   nextRowIdx = rowIdx + gridWidth + 1;
658   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
659   {
660     AddQuadIndices( indices, rowIdx, nextRowIdx );
661   }
662
663   return GenerateGeometry( vertices, indices );
664 }
665
666 } // namespace Internal
667
668 } // namespace Toolkit
669
670 } // namespace Dali