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