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