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