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