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