Merge "C# Bindings for Transition Policy properties" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
1 /*
2  * Copyright (c) 2016 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/integration-api/platform-abstraction.h>
23 #include <dali/public-api/images/buffer-image.h>
24 #include <dali/public-api/images/resource-image.h>
25 #include <dali/devel-api/images/texture-set-image.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 std::string& 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 std::string& 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   nPatchVisual->mImageUrl = image.GetUrl();
258
259   return nPatchVisual;
260 }
261
262 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
263 {
264   naturalSize.x = 0u;
265   naturalSize.y = 0u;
266   // load now if not already loaded
267   if( NPatchLoader::UNINITIALIZED_ID == mId )
268   {
269     mId = mLoader.Load( mImageUrl, 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 )
308   {
309     mId = mLoader.Load( mImageUrl, 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
321 void NPatchVisual::DoSetOffStage( Actor& actor )
322 {
323   actor.RemoveRenderer( mImpl->mRenderer );
324   mImpl->mRenderer.Reset();
325 }
326
327 void NPatchVisual::OnSetTransform()
328 {
329   if( mImpl->mRenderer )
330   {
331     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
332   }
333 }
334
335 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
336 {
337   map.Clear();
338   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::N_PATCH );
339   map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
340   map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
341   map.Insert( Toolkit::DevelImageVisual::Property::BORDER, mBorder );
342 }
343
344 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
345 {
346   // Do nothing
347 }
348
349 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
350 : Visual::Base( factoryCache ),
351   mLoader( factoryCache.GetNPatchLoader() ),
352   mImageUrl(),
353   mId( NPatchLoader::UNINITIALIZED_ID ),
354   mBorderOnly( false ),
355   mBorder()
356 {
357 }
358
359 NPatchVisual::~NPatchVisual()
360 {
361 }
362
363 Geometry NPatchVisual::CreateGeometry()
364 {
365   Geometry geometry;
366   const NPatchLoader::Data* data;
367   if( mLoader.GetNPatchData( mId, data ) )
368   {
369     if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
370     {
371       if( DALI_UNLIKELY( mBorderOnly ) )
372       {
373         geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
374       }
375       else
376       {
377         geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
378       }
379     }
380     else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
381     {
382       Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1,  2 * data->stretchPixelsY.Size() + 1 );
383       geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
384     }
385   }
386   else
387   {
388     // no N patch data so use default geometry
389     geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
390   }
391   return geometry;
392 }
393
394 Shader NPatchVisual::CreateShader()
395 {
396   Shader shader;
397   const NPatchLoader::Data* data;
398   // 0 is either no data (load failed?) or no stretch regions on image
399   // for both cases we use the default shader
400   NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
401   NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
402
403   // ask loader for the regions
404   if( mLoader.GetNPatchData( mId, data ) )
405   {
406     xStretchCount = data->stretchPixelsX.Count();
407     yStretchCount = data->stretchPixelsY.Count();
408   }
409
410   if( DALI_LIKELY( !mImpl->mCustomShader ) )
411   {
412     if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
413                      ( xStretchCount == 0 && yStretchCount == 0 ) ) )
414     {
415       shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
416       if( DALI_UNLIKELY( !shader ) )
417       {
418         shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
419         mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
420       }
421     }
422     else if( xStretchCount > 0 || yStretchCount > 0)
423     {
424       std::stringstream vertexShader;
425       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
426                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
427                    << VERTEX_SHADER;
428
429       shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
430     }
431   }
432   else
433   {
434     const char* fragmentShader = FRAGMENT_SHADER;
435     Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
436
437     if( !mImpl->mCustomShader->mFragmentShader.empty() )
438     {
439       fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
440     }
441     hints = mImpl->mCustomShader->mHints;
442
443     if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
444         ( xStretchCount == 0 && yStretchCount == 0 ) )
445     {
446       shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
447     }
448     else if( xStretchCount > 0 || yStretchCount > 0)
449     {
450       std::stringstream vertexShader;
451       vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
452                    << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
453                    << VERTEX_SHADER;
454
455       shader = Shader::New( vertexShader.str(), fragmentShader, hints );
456     }
457   }
458
459   return shader;
460 }
461
462 void NPatchVisual::ApplyTextureAndUniforms()
463 {
464   const NPatchLoader::Data* data;
465   if( mLoader.GetNPatchData( mId, data ) )
466   {
467     TextureSet textures( data->textureSet );
468     mImpl->mRenderer.SetTextures( textures );
469     if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
470     {
471       //special case for 9 patch
472       Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
473       Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
474
475       uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
476       uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
477
478       mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
479       mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
480       mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
481       mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
482     }
483     else
484     {
485       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
486       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
487
488       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
489       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
490     }
491   }
492   else
493   {
494     DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.c_str() );
495     TextureSet textureSet = TextureSet::New();
496     mImpl->mRenderer.SetTextures( textureSet );
497     Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
498     TextureSetImage( textureSet, 0u, croppedImage );
499     mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
500     mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
501     mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
502     mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
503   }
504
505   //Register transform properties
506   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
507 }
508
509 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
510 {
511   Geometry geometry = mFactoryCache.GetGeometry( subType );
512   if( !geometry )
513   {
514     if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
515     {
516       geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
517     }
518     else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
519     {
520       geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
521     }
522     mFactoryCache.SaveGeometry( subType, geometry );
523   }
524   return geometry;
525 }
526
527 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
528 {
529   uint16_t gridWidth = gridSize.GetWidth();
530   uint16_t gridHeight = gridSize.GetHeight();
531
532   // Create vertices
533   Vector< Vector2 > vertices;
534   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
535
536   for( int y = 0; y < gridHeight + 1; ++y )
537   {
538     for( int x = 0; x < gridWidth + 1; ++x )
539     {
540       AddVertex( vertices, x, y );
541     }
542   }
543
544   // Create indices
545   Vector< unsigned short > indices;
546   indices.Reserve( gridWidth * gridHeight * 6 );
547
548   unsigned int rowIdx     = 0;
549   unsigned int nextRowIdx = gridWidth + 1;
550   for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
551   {
552     for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
553     {
554       AddQuadIndices( indices, rowIdx, nextRowIdx );
555     }
556   }
557
558   return GenerateGeometry( vertices, indices );
559 }
560
561 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
562 {
563   uint16_t gridWidth = gridSize.GetWidth();
564   uint16_t gridHeight = gridSize.GetHeight();
565
566   // Create vertices
567   Vector< Vector2 > vertices;
568   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
569
570   //top
571   int y = 0;
572   for(; y < 2; ++y)
573   {
574     for( int x = 0; x < gridWidth + 1; ++x )
575     {
576       AddVertex( vertices, x, y );
577     }
578   }
579
580   for(; y < gridHeight - 1; ++y)
581   {
582     //left
583     AddVertex( vertices, 0, y );
584     AddVertex( vertices, 1, y );
585
586     //right
587     AddVertex( vertices, gridWidth - 1, y );
588     AddVertex( vertices, gridWidth, y );
589   }
590
591   //bottom
592   for(; y < gridHeight + 1; ++y)
593   {
594     for( int x = 0; x < gridWidth + 1; ++x )
595     {
596       AddVertex( vertices, x, y );
597     }
598   }
599
600   // Create indices
601   Vector< unsigned short > indices;
602   indices.Reserve( gridWidth * gridHeight * 6 );
603
604   //top
605   unsigned int rowIdx     = 0 ;
606   unsigned int nextRowIdx = gridWidth + 1;
607   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
608   {
609     AddQuadIndices( indices, rowIdx, nextRowIdx );
610   }
611
612   if(gridHeight > 2)
613   {
614     rowIdx     = gridWidth + 1;
615     nextRowIdx = ( gridWidth + 1 ) * 2;
616
617     unsigned increment = gridWidth - 1;
618     if(gridHeight > 3)
619     {
620       increment = 2;
621       //second row left
622       AddQuadIndices( indices, rowIdx, nextRowIdx );
623
624       rowIdx     = gridWidth * 2;
625       nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
626       //second row right
627       AddQuadIndices( indices, rowIdx, nextRowIdx );
628
629       //left and right
630       rowIdx     = nextRowIdx - 2;
631       nextRowIdx = rowIdx + 4;
632       for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
633       {
634         AddQuadIndices( indices, rowIdx, nextRowIdx );
635       }
636     }
637
638     //second row left
639     AddQuadIndices( indices, rowIdx, nextRowIdx );
640
641     rowIdx     += increment;
642     nextRowIdx += gridWidth - 1;
643     //second row right
644     AddQuadIndices( indices, rowIdx, nextRowIdx );
645   }
646
647   //bottom
648   rowIdx     = nextRowIdx - gridWidth + 1;
649   nextRowIdx = rowIdx + gridWidth + 1;
650   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
651   {
652     AddQuadIndices( indices, rowIdx, nextRowIdx );
653   }
654
655   return GenerateGeometry( vertices, indices );
656 }
657
658 } // namespace Internal
659
660 } // namespace Toolkit
661
662 } // namespace Dali