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