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