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