[3.0] Resource ready signal for Controls (for ImageLoading)
[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;\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;\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 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
204 : Visual::Base( factoryCache ),
205   mBorderOnly( false )
206 {
207 }
208
209 NPatchVisual::~NPatchVisual()
210 {
211 }
212
213 void NPatchVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
214 {
215   Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
216   if( imageURLValue )
217   {
218     //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
219     Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
220     if( borderOnlyValue )
221     {
222       borderOnlyValue->Get( mBorderOnly );
223     }
224
225     if( imageURLValue->Get( mImageUrl ) )
226     {
227       NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
228       InitializeFromImage( nPatch );
229     }
230     else
231     {
232       InitializeFromBrokenImage();
233       DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
234     }
235   }
236 }
237
238 void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
239 {
240   if( mImage )
241   {
242     naturalSize.x = mImage.GetWidth();
243     naturalSize.y = mImage.GetHeight();
244   }
245   else if( !mImageUrl.empty() )
246   {
247     ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
248     naturalSize.x = dimentions.GetWidth();
249     naturalSize.y = dimentions.GetHeight();
250   }
251   else
252   {
253     naturalSize = Vector2::ZERO;
254   }
255 }
256
257 void NPatchVisual::SetClipRect( const Rect<int>& clipRect )
258 {
259   Visual::Base::SetClipRect( clipRect );
260   //ToDo: renderer responds to the clipRect change
261 }
262
263 void NPatchVisual::SetOffset( const Vector2& offset )
264 {
265   //ToDo: renderer applies the offset
266 }
267
268 Geometry NPatchVisual::CreateGeometry()
269 {
270   Geometry geometry;
271   if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
272   {
273     if( !mBorderOnly )
274     {
275       geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
276       if( !geometry )
277       {
278         geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
279         mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry );
280       }
281     }
282     else
283     {
284       geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
285       if( !geometry )
286       {
287         geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
288         mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
289       }
290     }
291   }
292   else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
293   {
294     Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1,  2 * mStretchPixelsY.Size() + 1 );
295     geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
296   }
297
298   return geometry;
299 }
300
301 Shader NPatchVisual::CreateShader()
302 {
303   Shader shader;
304   if( !mImpl->mCustomShader )
305   {
306     if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
307     {
308       shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
309       if( !shader )
310       {
311         shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
312         mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
313       }
314     }
315     else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
316     {
317       std::stringstream vertexShader;
318       vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
319                    << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
320                    << VERTEX_SHADER;
321
322       shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
323     }
324   }
325   else
326   {
327     const char* fragmentShader = FRAGMENT_SHADER;
328     Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
329
330     if( !mImpl->mCustomShader->mFragmentShader.empty() )
331     {
332       fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
333     }
334     hints = mImpl->mCustomShader->mHints;
335
336     if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
337     {
338       shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
339     }
340     else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
341     {
342       std::stringstream vertexShader;
343       vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
344                    << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
345                    << VERTEX_SHADER;
346
347       shader = Shader::New( vertexShader.str(), fragmentShader, hints );
348     }
349   }
350
351   return shader;
352 }
353
354 void NPatchVisual::InitializeRenderer()
355 {
356   Geometry geometry = CreateGeometry();
357   Shader shader = CreateShader();
358
359   if( !geometry || !shader )
360   {
361     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() );
362     InitializeFromBrokenImage();
363   }
364
365   TextureSet textureSet = TextureSet::New();
366   mImpl->mRenderer = Renderer::New( geometry, shader );
367   mImpl->mRenderer.SetTextures( textureSet );
368 }
369
370
371 void NPatchVisual::DoSetOnStage( Actor& actor )
372 {
373   if( !mCroppedImage )
374   {
375     if( !mImageUrl.empty() )
376     {
377       NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
378       InitializeFromImage( nPatch );
379     }
380     else if( mImage )
381     {
382       InitializeFromImage( mImage );
383     }
384   }
385
386   //initialize the renderer after initializing from the image since we need to know the grid size from the image before creating the geometry
387   InitializeRenderer();
388
389   if( mCroppedImage )
390   {
391     ApplyImageToSampler();
392   }
393
394   actor.AddRenderer( mImpl->mRenderer );
395
396   // npatch loaded and ready to display
397   ResourceReady();
398 }
399
400 void NPatchVisual::DoSetOffStage( Actor& actor )
401 {
402   mCroppedImage.Reset();
403   actor.RemoveRenderer( mImpl->mRenderer );
404   mImpl->mRenderer.Reset();
405 }
406
407 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
408 {
409   map.Clear();
410   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
411   if( !mImageUrl.empty() )
412   {
413     map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
414   }
415   else if( mImage )
416   {
417     map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
418   }
419   map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
420 }
421
422 void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
423 {
424   //check to see if the border style has changed
425
426   bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
427   bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
428
429   if( borderOnlyChanged || gridChanged )
430   {
431     Geometry geometry = CreateGeometry();
432     if( geometry )
433     {
434       mImpl->mRenderer.SetGeometry( geometry );
435     }
436     else
437     {
438       InitializeFromBrokenImage();
439     }
440   }
441
442   if( gridChanged )
443   {
444     Shader shader = CreateShader();
445     TextureSet textureSet;
446     if( shader )
447     {
448       textureSet = mImpl->mRenderer.GetTextures();
449       if( !textureSet )
450       {
451         InitializeFromBrokenImage();
452       }
453       mImpl->mRenderer.SetShader( shader );
454     }
455   }
456 }
457
458 void NPatchVisual::SetImage( const std::string& imageUrl, bool borderOnly )
459 {
460   bool oldBorderOnly = mBorderOnly;
461   size_t oldGridX = mStretchPixelsX.Size();
462   size_t oldGridY = mStretchPixelsY.Size();
463
464   mBorderOnly = borderOnly;
465   mImage.Reset();
466   if( mImageUrl == imageUrl )
467   {
468     return;
469   }
470
471   mImageUrl = imageUrl;
472   if( mImpl->mRenderer )
473   {
474     NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
475     InitializeFromImage( nPatch );
476
477     ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
478
479     if( mCroppedImage )
480     {
481       ApplyImageToSampler();
482     }
483   }
484 }
485
486 void NPatchVisual::SetImage( NinePatchImage image, bool borderOnly )
487 {
488   bool oldBorderOnly = mBorderOnly;
489   size_t oldGridX = mStretchPixelsX.Size();
490   size_t oldGridY = mStretchPixelsY.Size();
491
492   mBorderOnly = borderOnly;
493   mImageUrl.empty();
494   if( mImage == image )
495   {
496     return;
497   }
498
499   mImage = image;
500   if( mImpl->mRenderer )
501   {
502     InitializeFromImage( mImage );
503     ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
504
505     if( mCroppedImage )
506     {
507       ApplyImageToSampler();
508     }
509   }
510 }
511
512 void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
513 {
514   mCroppedImage = nPatch.CreateCroppedBufferImage();
515   if( !mCroppedImage )
516   {
517     DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
518     InitializeFromBrokenImage();
519     return;
520   }
521
522   mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
523
524   mStretchPixelsX = nPatch.GetStretchPixelsX();
525   mStretchPixelsY = nPatch.GetStretchPixelsY();
526 }
527
528 void NPatchVisual::InitializeFromBrokenImage()
529 {
530   mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
531   mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
532
533   mStretchPixelsX.Clear();
534   mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
535   mStretchPixelsY.Clear();
536   mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
537 }
538
539 void NPatchVisual::ApplyImageToSampler()
540 {
541   TextureSet textureSet = mImpl->mRenderer.GetTextures();
542   if( textureSet )
543   {
544     TextureSetImage( textureSet, 0u, mCroppedImage );
545
546     if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
547     {
548       //special case for 9 patch
549       Uint16Pair stretchX = mStretchPixelsX[ 0 ];
550       Uint16Pair stretchY = mStretchPixelsY[ 0 ];
551
552       uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
553       uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
554
555       mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
556       mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
557       mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
558       mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
559     }
560     else
561     {
562       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
563       mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
564
565       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
566       RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
567     }
568   }
569 }
570
571 Geometry NPatchVisual::CreateGeometry( Uint16Pair gridSize )
572 {
573   uint16_t gridWidth = gridSize.GetWidth();
574   uint16_t gridHeight = gridSize.GetHeight();
575
576   // Create vertices
577   Vector< Vector2 > vertices;
578   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
579
580   for( int y = 0; y < gridHeight + 1; ++y )
581   {
582     for( int x = 0; x < gridWidth + 1; ++x )
583     {
584       AddVertex( vertices, x, y );
585     }
586   }
587
588   // Create indices
589   //TODO: compare performance with triangle strip when Geometry supports it
590   Vector< unsigned short > indices;
591   indices.Reserve( gridWidth * gridHeight * 6 );
592
593   unsigned int rowIdx     = 0;
594   unsigned int nextRowIdx = gridWidth + 1;
595   for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
596   {
597     for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
598     {
599       AddQuadIndices( indices, rowIdx, nextRowIdx );
600     }
601   }
602
603   return GenerateGeometry( vertices, indices );
604 }
605
606 Geometry NPatchVisual::CreateGeometryBorder( Uint16Pair gridSize )
607 {
608   uint16_t gridWidth = gridSize.GetWidth();
609   uint16_t gridHeight = gridSize.GetHeight();
610
611   // Create vertices
612   Vector< Vector2 > vertices;
613   vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
614
615   //top
616   int y = 0;
617   for(; y < 2; ++y)
618   {
619     for( int x = 0; x < gridWidth + 1; ++x )
620     {
621       AddVertex( vertices, x, y );
622     }
623   }
624
625   for(; y < gridHeight - 1; ++y)
626   {
627     //left
628     AddVertex( vertices, 0, y );
629     AddVertex( vertices, 1, y );
630
631     //right
632     AddVertex( vertices, gridWidth - 1, y );
633     AddVertex( vertices, gridWidth, y );
634   }
635
636   //bottom
637   for(; y < gridHeight + 1; ++y)
638   {
639     for( int x = 0; x < gridWidth + 1; ++x )
640     {
641       AddVertex( vertices, x, y );
642     }
643   }
644
645   // Create indices
646   //TODO: compare performance with triangle strip when Geometry supports it
647   Vector< unsigned short > indices;
648   indices.Reserve( gridWidth * gridHeight * 6 );
649
650   //top
651   unsigned int rowIdx     = 0 ;
652   unsigned int nextRowIdx = gridWidth + 1;
653   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
654   {
655     AddQuadIndices( indices, rowIdx, nextRowIdx );
656   }
657
658   if(gridHeight > 2)
659   {
660     rowIdx     = gridWidth + 1;
661     nextRowIdx = ( gridWidth + 1 ) * 2;
662
663     unsigned increment = gridWidth - 1;
664     if(gridHeight > 3)
665     {
666       increment = 2;
667       //second row left
668       AddQuadIndices( indices, rowIdx, nextRowIdx );
669
670       rowIdx     = gridWidth * 2;
671       nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
672       //second row right
673       AddQuadIndices( indices, rowIdx, nextRowIdx );
674
675       //left and right
676       rowIdx     = nextRowIdx - 2;
677       nextRowIdx = rowIdx + 4;
678       for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
679       {
680         AddQuadIndices( indices, rowIdx, nextRowIdx );
681       }
682     }
683
684     //second row left
685     AddQuadIndices( indices, rowIdx, nextRowIdx );
686
687     rowIdx     += increment;
688     nextRowIdx += gridWidth - 1;
689     //second row right
690     AddQuadIndices( indices, rowIdx, nextRowIdx );
691   }
692
693   //bottom
694   rowIdx     = nextRowIdx - gridWidth + 1;
695   nextRowIdx = rowIdx + gridWidth + 1;
696   for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
697   {
698     AddQuadIndices( indices, rowIdx, nextRowIdx );
699   }
700
701   return GenerateGeometry( vertices, indices );
702 }
703
704 } // namespace Internal
705
706 } // namespace Toolkit
707
708 } // namespace Dali