Merge "Changed all property & signal names to lowerCamelCase" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / npatch / npatch-renderer.cpp
1 /*
2  * Copyright (c) 2015 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-renderer.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
26 // INTERNAL IINCLUDES
27 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
28 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
29 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
30 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
31
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 const char * const RENDERER_TYPE("rendererType");
45 const char * const RENDERER_TYPE_VALUE("nPatchRenderer");
46
47 const char * const IMAGE_URL_NAME("imageUrl");
48 const char * const BORDER_ONLY("borderOnly");
49
50 std::string TEXTURE_UNIFORM_NAME = "sTexture";
51
52 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
53   attribute mediump vec2 aPosition;\n
54   varying mediump vec2 vTexCoord;\n
55   uniform mediump mat4 uMvpMatrix;\n
56   uniform mediump vec3 uSize;\n
57   uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
58   uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
59   \n
60   void main()\n
61   {\n
62     mediump vec2 fixedFactor  = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
63     mediump vec2 stretch      = vec2( uNinePatchFactorsX[ int( ( aPosition.x       ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y       ) * 0.5 ) ].y );\n
64     \n
65     mediump vec2 fixedTotal   = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
66     mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
67     \n
68     mediump vec4 vertexPosition = vec4( ( fixedFactor + ( uSize.xy - fixedTotal ) * stretch / stretchTotal ), 0.0, 1.0 );\n
69     vertexPosition.xy -= uSize.xy * vec2( 0.5, 0.5 );\n
70     vertexPosition = uMvpMatrix * vertexPosition;\n
71     \n
72     vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
73     \n
74     gl_Position = vertexPosition;\n
75   }\n
76 );
77
78 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
79     attribute mediump vec2 aPosition;\n
80     varying mediump vec2 vTexCoord;\n
81     uniform mediump mat4 uModelMatrix;\n
82     uniform mediump mat4 uMvpMatrix;\n
83     uniform mediump vec3 uSize;\n
84     uniform mediump vec2 uFixed[ 3 ];\n
85     uniform mediump vec2 uStretchTotal;\n
86     \n
87     void main()\n
88     {\n
89       mediump vec2 scale        = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
90       mediump vec2 size         = uSize.xy * scale;\n
91       \n
92       mediump vec2 fixedFactor  = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y  + 1.0 ) * 0.5 ) ].y );\n
93       mediump vec2 stretch      = floor( aPosition * 0.5 );\n
94       mediump vec2 fixedTotal   = uFixed[ 2 ];\n
95       \n
96       mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
97       vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
98       vertexPosition.xy =  vertexPosition.xy / scale;\n
99       \n
100       vertexPosition = uMvpMatrix * vertexPosition;\n
101       \n
102       vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
103       \n
104       gl_Position = vertexPosition;\n
105     }\n
106 );
107
108 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
109   varying mediump vec2 vTexCoord;\n
110   uniform sampler2D sTexture;\n
111   uniform lowp vec4 uColor;\n
112   \n
113   void main()\n
114   {\n
115     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
116   }\n
117 );
118
119 /**
120  * @brief Creates the geometry formed from the vertices and indices
121  *
122  * @param[in]  vertices             The vertices to generate the geometry from
123  * @param[in]  indices              The indices to generate the geometry from
124  * @return The geometry formed from the vertices and indices
125  */
126 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned int >& indices )
127 {
128   Property::Map vertexFormat;
129   vertexFormat[ "aPosition" ] = Property::VECTOR2;
130   PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat, vertices.Size() );
131   if( vertices.Size() > 0 )
132   {
133     vertexPropertyBuffer.SetData( &vertices[ 0 ] );
134   }
135
136   Property::Map indexFormat;
137   indexFormat[ "indices" ] = Property::INTEGER;
138   PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat, indices.Size() );
139   if( indices.Size() > 0 )
140   {
141     indexPropertyBuffer.SetData( &indices[ 0 ] );
142   }
143
144   // Create the geometry object
145   Geometry geometry = Geometry::New();
146   geometry.AddVertexBuffer( vertexPropertyBuffer );
147   geometry.SetIndexBuffer( indexPropertyBuffer );
148
149   return geometry;
150 }
151
152 /**
153  * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
154  *
155  * @param[out] indices     The indices to add to
156  * @param[in]  rowIdx      The row index to start the quad
157  * @param[in]  nextRowIdx  The index to the next row
158  */
159 void AddQuadIndices( Vector< unsigned int >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
160 {
161   indices.PushBack( rowIdx );
162   indices.PushBack( nextRowIdx + 1 );
163   indices.PushBack( rowIdx + 1 );
164
165   indices.PushBack( rowIdx );
166   indices.PushBack( nextRowIdx );
167   indices.PushBack( nextRowIdx + 1 );
168 }
169
170 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
171 {
172   vertices.PushBack( Vector2( x, y ) );
173 }
174
175 void RegisterStretchProperties( Material& material, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
176 {
177   uint16_t prevEnd = 0;
178   uint16_t prevFix = 0;
179   uint16_t prevStretch = 0;
180   unsigned int i = 1;
181   for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
182   {
183     uint16_t start = it->GetX();
184     uint16_t end = it->GetY();
185
186     uint16_t fix = prevFix + start - prevEnd;
187     uint16_t stretch = prevStretch + end - start;
188
189     std::stringstream uniform;
190     uniform << uniformName << "[" << i << "]";
191     material.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
192
193     prevEnd = end;
194     prevFix = fix;
195     prevStretch = stretch;
196   }
197
198   {
199     prevFix += imageExtent - prevEnd;
200     std::stringstream uniform;
201     uniform << uniformName << "[" << i << "]";
202     material.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
203   }
204 }
205
206 } //unnamed namespace
207
208 /////////////////NPatchRenderer////////////////
209
210 NPatchRenderer::NPatchRenderer( RendererFactoryCache& factoryCache )
211 : ControlRenderer( factoryCache ),
212   mBorderOnly( false )
213 {
214 }
215
216 NPatchRenderer::~NPatchRenderer()
217 {
218 }
219
220 void NPatchRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
221 {
222   Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
223   if( imageURLValue )
224   {
225     //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
226     Property::Value* borderOnlyValue = propertyMap.Find( BORDER_ONLY );
227     if( borderOnlyValue )
228     {
229       borderOnlyValue->Get( mBorderOnly );
230     }
231
232     if( imageURLValue->Get( mImageUrl ) )
233     {
234       NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
235       InitializeFromImage( nPatch );
236     }
237     else
238     {
239       InitializeFromBrokenImage();
240       DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
241     }
242   }
243 }
244
245 void NPatchRenderer::GetNaturalSize( Vector2& naturalSize ) const
246 {
247   if( mImage )
248   {
249     naturalSize.x = mImage.GetWidth();
250     naturalSize.y = mImage.GetHeight();
251   }
252   else if( !mImageUrl.empty() )
253   {
254     ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
255     naturalSize.x = dimentions.GetWidth();
256     naturalSize.y = dimentions.GetHeight();
257   }
258   else
259   {
260     naturalSize = Vector2::ZERO;
261   }
262 }
263
264 void NPatchRenderer::SetClipRect( const Rect<int>& clipRect )
265 {
266   ControlRenderer::SetClipRect( clipRect );
267   //ToDo: renderer responds to the clipRect change
268 }
269
270 void NPatchRenderer::SetOffset( const Vector2& offset )
271 {
272   //ToDo: renderer applies the offset
273 }
274
275 Geometry NPatchRenderer::CreateGeometry()
276 {
277   Geometry geometry;
278   if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
279   {
280     if( !mBorderOnly )
281     {
282       geometry = mFactoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY );
283       if( !geometry )
284       {
285         geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
286         mFactoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY, geometry );
287       }
288     }
289     else
290     {
291       geometry = mFactoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
292       if( !geometry )
293       {
294         geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
295         mFactoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
296       }
297     }
298   }
299   else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
300   {
301     Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1,  2 * mStretchPixelsY.Size() + 1 );
302     geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
303   }
304
305   return geometry;
306 }
307
308 Shader NPatchRenderer::CreateShader()
309 {
310   Shader shader;
311   if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
312   {
313     shader = mFactoryCache.GetShader( RendererFactoryCache::NINE_PATCH_SHADER );
314     if( !shader )
315     {
316       shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
317       mFactoryCache.SaveShader( RendererFactoryCache::NINE_PATCH_SHADER, shader );
318     }
319   }
320   else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
321   {
322     std::stringstream vertexShader;
323     vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
324                  << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
325                  << VERTEX_SHADER;
326
327     shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
328   }
329   return shader;
330 }
331
332 void NPatchRenderer::InitializeRenderer()
333 {
334   Geometry geometry = CreateGeometry();
335   Shader shader = CreateShader();
336
337   if( !geometry || !shader )
338   {
339     DALI_LOG_ERROR("The 9 patch image '%s' doesn't have any valid stretch borders and so is not a valid 9 patch image\n", mImageUrl.c_str() );
340     InitializeFromBrokenImage();
341   }
342
343   Material material = Material::New( shader );
344   mImpl->mRenderer = Renderer::New( geometry, material );
345 }
346
347
348 void NPatchRenderer::DoSetOnStage( Actor& actor )
349 {
350   if( !mCroppedImage )
351   {
352     if( !mImageUrl.empty() )
353     {
354       NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
355       InitializeFromImage( nPatch );
356     }
357     else if( mImage )
358     {
359       InitializeFromImage( mImage );
360     }
361   }
362
363   //initialize the renderer after initializing from the image since we need to know the grid size from the image before creating the geometry
364   InitializeRenderer();
365
366   if( mCroppedImage )
367   {
368     ApplyImageToSampler();
369   }
370 }
371
372 void NPatchRenderer::DoSetOffStage( Actor& actor )
373 {
374   mCroppedImage.Reset();
375 }
376
377 void NPatchRenderer::DoCreatePropertyMap( Property::Map& map ) const
378 {
379   map.Clear();
380   map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
381   if( !mImageUrl.empty() )
382   {
383     map.Insert( IMAGE_URL_NAME, mImageUrl );
384   }
385   else if( mImage )
386   {
387     map.Insert( IMAGE_URL_NAME, mImage.GetUrl() );
388   }
389   map.Insert( BORDER_ONLY, mBorderOnly );
390 }
391
392 void NPatchRenderer::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
393 {
394   //check to see if the border style has changed
395
396   bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
397   bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
398
399   if( borderOnlyChanged || gridChanged )
400   {
401     Geometry geometry = CreateGeometry();
402     if( geometry )
403     {
404       mImpl->mRenderer.SetGeometry( geometry );
405     }
406     else
407     {
408       InitializeFromBrokenImage();
409     }
410   }
411
412   if( gridChanged )
413   {
414     Shader shader = CreateShader();
415     Material material;
416     if( shader )
417     {
418       material = mImpl->mRenderer.GetMaterial();
419       if( material )
420       {
421         material.SetShader( shader );
422       }
423     }
424
425     if( !material )
426     {
427       InitializeFromBrokenImage();
428     }
429   }
430 }
431
432 void NPatchRenderer::SetImage( const std::string& imageUrl, bool borderOnly )
433 {
434   bool oldBorderOnly = mBorderOnly;
435   size_t oldGridX = mStretchPixelsX.Size();
436   size_t oldGridY = mStretchPixelsY.Size();
437
438   mBorderOnly = borderOnly;
439   mImage.Reset();
440   if( mImageUrl == imageUrl )
441   {
442     return;
443   }
444
445   mImageUrl = imageUrl;
446   if( mImpl->mRenderer )
447   {
448     NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
449     InitializeFromImage( nPatch );
450
451     ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
452
453     if( mCroppedImage )
454     {
455       ApplyImageToSampler();
456     }
457   }
458 }
459
460 void NPatchRenderer::SetImage( NinePatchImage image, bool borderOnly )
461 {
462   bool oldBorderOnly = mBorderOnly;
463   size_t oldGridX = mStretchPixelsX.Size();
464   size_t oldGridY = mStretchPixelsY.Size();
465
466   mBorderOnly = borderOnly;
467   mImageUrl.empty();
468   if( mImage == image )
469   {
470     return;
471   }
472
473   mImage = image;
474   if( mImpl->mRenderer )
475   {
476     InitializeFromImage( mImage );
477     ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
478
479     if( mCroppedImage )
480     {
481       ApplyImageToSampler();
482     }
483   }
484 }
485
486 void NPatchRenderer::InitializeFromImage( NinePatchImage nPatch )
487 {
488   mCroppedImage = nPatch.CreateCroppedBufferImage();
489   if( !mCroppedImage )
490   {
491     DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
492     InitializeFromBrokenImage();
493     return;
494   }
495
496   mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
497
498   mStretchPixelsX = nPatch.GetStretchPixelsX();
499   mStretchPixelsY = nPatch.GetStretchPixelsY();
500 }
501
502 void NPatchRenderer::InitializeFromBrokenImage()
503 {
504   mCroppedImage = RendererFactory::GetBrokenRendererImage();
505   mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
506
507   mStretchPixelsX.Clear();
508   mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
509   mStretchPixelsY.Clear();
510   mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
511 }
512
513 void NPatchRenderer::ApplyImageToSampler()
514 {
515   Material material = mImpl->mRenderer.GetMaterial();
516   if( material )
517   {
518     int index = material.GetTextureIndex( TEXTURE_UNIFORM_NAME );
519     if( index > -1 )
520     {
521       material.SetTextureImage( index, mCroppedImage );
522     }
523     else
524     {
525       material.AddTexture(  mCroppedImage, TEXTURE_UNIFORM_NAME );
526     }
527
528     if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
529     {
530       //special case for 9 patch
531       Uint16Pair stretchX = mStretchPixelsX[ 0 ];
532       Uint16Pair stretchY = mStretchPixelsY[ 0 ];
533
534       uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
535       uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
536
537       material.RegisterProperty( "uFixed[0]", Vector2::ZERO );
538       material.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
539       material.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
540       material.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
541     }
542     else
543     {
544       material.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
545       material.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
546
547       RegisterStretchProperties( material, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
548       RegisterStretchProperties( material, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
549     }
550   }
551 }
552
553 Geometry NPatchRenderer::CreateGeometry( Uint16Pair gridSize )
554 {
555   uint16_t gridWidth = gridSize.GetWidth();
556   uint16_t gridHeight = gridSize.GetHeight();
557
558   // Create vertices
559   Vector< Vector2 > vertices;
560   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
561
562   for( int y = 0; y < gridHeight + 1; ++y )
563   {
564     for( int x = 0; x < gridWidth + 1; ++x )
565     {
566       AddVertex( vertices, x, y );
567     }
568   }
569
570   // Create indices
571   //TODO: compare performance with triangle strip when Geometry supports it
572   Vector< unsigned int > indices;
573   indices.Reserve( gridWidth * gridHeight * 6 );
574
575   unsigned int rowIdx     = 0;
576   unsigned int nextRowIdx = gridWidth + 1;
577   for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
578   {
579     for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
580     {
581       AddQuadIndices( indices, rowIdx, nextRowIdx );
582     }
583   }
584
585   return GenerateGeometry( vertices, indices );
586 }
587
588 Geometry NPatchRenderer::CreateGeometryBorder( Uint16Pair gridSize )
589 {
590   uint16_t gridWidth = gridSize.GetWidth();
591   uint16_t gridHeight = gridSize.GetHeight();
592
593   // Create vertices
594   Vector< Vector2 > vertices;
595   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
596
597   //top
598   int y = 0;
599   for(; y < 2; ++y)
600   {
601     for( int x = 0; x < gridWidth + 1; ++x )
602     {
603       AddVertex( vertices, x, y );
604     }
605   }
606
607   for(; y < gridHeight - 1; ++y)
608   {
609     //left
610     AddVertex( vertices, 0, y );
611     AddVertex( vertices, 1, y );
612
613     //right
614     AddVertex( vertices, gridWidth - 1, y );
615     AddVertex( vertices, gridWidth, y );
616   }
617
618   //bottom
619   for(; y < gridHeight + 1; ++y)
620   {
621     for( int x = 0; x < gridWidth + 1; ++x )
622     {
623       AddVertex( vertices, x, y );
624     }
625   }
626
627   // Create indices
628   //TODO: compare performance with triangle strip when Geometry supports it
629   Vector< unsigned int > indices;
630   indices.Reserve( gridWidth * gridHeight * 6 );
631
632   //top
633   unsigned int rowIdx     = 0 ;
634   unsigned int nextRowIdx = gridWidth + 1;
635   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
636   {
637     AddQuadIndices( indices, rowIdx, nextRowIdx );
638   }
639
640   if(gridHeight > 2)
641   {
642     rowIdx     = gridWidth + 1;
643     nextRowIdx = ( gridWidth + 1 ) * 2;
644
645     unsigned increment = gridWidth - 1;
646     if(gridHeight > 3)
647     {
648       increment = 2;
649       //second row left
650       AddQuadIndices( indices, rowIdx, nextRowIdx );
651
652       rowIdx     = gridWidth * 2;
653       nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
654       //second row right
655       AddQuadIndices( indices, rowIdx, nextRowIdx );
656
657       //left and right
658       rowIdx     = nextRowIdx - 2;
659       nextRowIdx = rowIdx + 4;
660       for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
661       {
662         AddQuadIndices( indices, rowIdx, nextRowIdx );
663       }
664     }
665
666     //second row left
667     AddQuadIndices( indices, rowIdx, nextRowIdx );
668
669     rowIdx     += increment;
670     nextRowIdx += gridWidth - 1;
671     //second row right
672     AddQuadIndices( indices, rowIdx, nextRowIdx );
673   }
674
675   //bottom
676   rowIdx     = nextRowIdx - gridWidth + 1;
677   nextRowIdx = rowIdx + gridWidth + 1;
678   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
679   {
680     AddQuadIndices( indices, rowIdx, nextRowIdx );
681   }
682
683   return GenerateGeometry( vertices, indices );
684 }
685
686 } // namespace Internal
687
688 } // namespace Toolkit
689
690 } // namespace Dali