2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "npatch-visual.h"
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>
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/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>
49 const char * const BORDER_ONLY("borderOnly");
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
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
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
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
76 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
77 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
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
82 vertexPosition = uMvpMatrix * vertexPosition;\n
84 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
86 gl_Position = vertexPosition;\n
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
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
109 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
110 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
112 mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
113 mediump vec2 size = visualSize.xy * scale;\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
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;\
123 vertexPosition = uMvpMatrix * vertexPosition;\n
125 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
127 gl_Position = vertexPosition;\n
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
138 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
143 * @brief Creates the geometry formed from the vertices and indices
145 * @param[in] vertices The vertices to generate the geometry from
146 * @param[in] indices The indices to generate the geometry from
147 * @return The geometry formed from the vertices and indices
149 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
151 Property::Map vertexFormat;
152 vertexFormat[ "aPosition" ] = Property::VECTOR2;
153 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
154 if( vertices.Size() > 0 )
156 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
159 // Create the geometry object
160 Geometry geometry = Geometry::New();
161 geometry.AddVertexBuffer( vertexPropertyBuffer );
162 if( indices.Size() > 0 )
164 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
172 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
174 * @param[out] indices The indices to add to
175 * @param[in] rowIdx The row index to start the quad
176 * @param[in] nextRowIdx The index to the next row
178 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
180 indices.PushBack( rowIdx );
181 indices.PushBack( nextRowIdx + 1 );
182 indices.PushBack( rowIdx + 1 );
184 indices.PushBack( rowIdx );
185 indices.PushBack( nextRowIdx );
186 indices.PushBack( nextRowIdx + 1 );
189 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
191 vertices.PushBack( Vector2( x, y ) );
194 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
196 uint16_t prevEnd = 0;
197 uint16_t prevFix = 0;
198 uint16_t prevStretch = 0;
200 for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
202 uint16_t start = it->GetX();
203 uint16_t end = it->GetY();
205 uint16_t fix = prevFix + start - prevEnd;
206 uint16_t stretch = prevStretch + end - start;
208 std::stringstream uniform;
209 uniform << uniformName << "[" << i << "]";
210 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
214 prevStretch = stretch;
218 prevFix += imageExtent - prevEnd;
219 std::stringstream uniform;
220 uniform << uniformName << "[" << i << "]";
221 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
225 } //unnamed namespace
227 /////////////////NPatchVisual////////////////
229 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl )
231 NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
232 nPatchVisual->mImageUrl = imageUrl;
237 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
239 NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
240 nPatchVisual->mImageUrl = image.GetUrl();
245 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
249 // load now if not already loaded
250 if( NPatchLoader::UNINITIALIZED_ID == mId )
252 mId = mLoader.Load( mImageUrl );
254 const NPatchLoader::Data* data;
255 if( mLoader.GetNPatchData( mId, data ) )
257 naturalSize.x = data->croppedWidth;
258 naturalSize.y = data->croppedHeight;
262 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
264 // URL is already passed in via constructor
266 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
267 if( borderOnlyValue )
269 borderOnlyValue->Get( mBorderOnly );
273 void NPatchVisual::DoSetOnStage( Actor& actor )
275 // load when first go on stage
276 if( NPatchLoader::UNINITIALIZED_ID == mId )
278 mId = mLoader.Load( mImageUrl );
281 Geometry geometry = CreateGeometry();
282 Shader shader = CreateShader();
283 mImpl->mRenderer = Renderer::New( geometry, shader );
285 ApplyTextureAndUniforms();
287 actor.AddRenderer( mImpl->mRenderer );
290 void NPatchVisual::DoSetOffStage( Actor& actor )
292 actor.RemoveRenderer( mImpl->mRenderer );
293 mImpl->mRenderer.Reset();
296 void NPatchVisual::OnSetTransform()
298 if( mImpl->mRenderer )
300 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
304 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
307 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
308 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
309 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
312 void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
317 Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
320 return Dali::Property::Value();
323 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
324 : Visual::Base( factoryCache ),
325 mLoader( factoryCache.GetNPatchLoader() ),
327 mId( NPatchLoader::UNINITIALIZED_ID ),
332 NPatchVisual::~NPatchVisual()
336 Geometry NPatchVisual::CreateGeometry()
339 const NPatchLoader::Data* data;
340 if( mLoader.GetNPatchData( mId, data ) )
342 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
344 if( DALI_UNLIKELY( mBorderOnly ) )
346 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
350 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
353 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
355 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
356 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
361 // no N patch data so use default geometry
362 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
367 Shader NPatchVisual::CreateShader()
370 const NPatchLoader::Data* data;
371 // 0 is either no data (load failed?) or no stretch regions on image
372 // for both cases we use the default shader
373 NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
374 NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
376 // ask loader for the regions
377 if( mLoader.GetNPatchData( mId, data ) )
379 xStretchCount = data->stretchPixelsX.Count();
380 yStretchCount = data->stretchPixelsY.Count();
383 if( DALI_LIKELY( !mImpl->mCustomShader ) )
385 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
386 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
388 shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
389 if( DALI_UNLIKELY( !shader ) )
391 shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
392 mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
395 else if( xStretchCount > 0 || yStretchCount > 0)
397 std::stringstream vertexShader;
398 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
399 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
402 shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
407 const char* fragmentShader = FRAGMENT_SHADER;
408 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
410 if( !mImpl->mCustomShader->mFragmentShader.empty() )
412 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
414 hints = mImpl->mCustomShader->mHints;
416 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
417 ( xStretchCount == 0 && yStretchCount == 0 ) )
419 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
421 else if( xStretchCount > 0 || yStretchCount > 0)
423 std::stringstream vertexShader;
424 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
425 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
428 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
435 void NPatchVisual::ApplyTextureAndUniforms()
437 const NPatchLoader::Data* data;
438 if( mLoader.GetNPatchData( mId, data ) )
440 TextureSet textures( data->textureSet );
441 mImpl->mRenderer.SetTextures( textures );
442 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
444 //special case for 9 patch
445 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
446 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
448 uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
449 uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
451 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
452 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
453 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
454 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
458 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
459 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
461 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
462 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
467 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.c_str() );
468 TextureSet textureSet = TextureSet::New();
469 mImpl->mRenderer.SetTextures( textureSet );
470 Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
471 TextureSetImage( textureSet, 0u, croppedImage );
472 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
473 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
474 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
475 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
478 //Register transform properties
479 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
482 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
484 Geometry geometry = mFactoryCache.GetGeometry( subType );
487 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
489 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
491 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
493 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
495 mFactoryCache.SaveGeometry( subType, geometry );
500 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
502 uint16_t gridWidth = gridSize.GetWidth();
503 uint16_t gridHeight = gridSize.GetHeight();
506 Vector< Vector2 > vertices;
507 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
509 for( int y = 0; y < gridHeight + 1; ++y )
511 for( int x = 0; x < gridWidth + 1; ++x )
513 AddVertex( vertices, x, y );
518 Vector< unsigned short > indices;
519 indices.Reserve( gridWidth * gridHeight * 6 );
521 unsigned int rowIdx = 0;
522 unsigned int nextRowIdx = gridWidth + 1;
523 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
525 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
527 AddQuadIndices( indices, rowIdx, nextRowIdx );
531 return GenerateGeometry( vertices, indices );
534 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
536 uint16_t gridWidth = gridSize.GetWidth();
537 uint16_t gridHeight = gridSize.GetHeight();
540 Vector< Vector2 > vertices;
541 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
547 for( int x = 0; x < gridWidth + 1; ++x )
549 AddVertex( vertices, x, y );
553 for(; y < gridHeight - 1; ++y)
556 AddVertex( vertices, 0, y );
557 AddVertex( vertices, 1, y );
560 AddVertex( vertices, gridWidth - 1, y );
561 AddVertex( vertices, gridWidth, y );
565 for(; y < gridHeight + 1; ++y)
567 for( int x = 0; x < gridWidth + 1; ++x )
569 AddVertex( vertices, x, y );
574 Vector< unsigned short > indices;
575 indices.Reserve( gridWidth * gridHeight * 6 );
578 unsigned int rowIdx = 0 ;
579 unsigned int nextRowIdx = gridWidth + 1;
580 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
582 AddQuadIndices( indices, rowIdx, nextRowIdx );
587 rowIdx = gridWidth + 1;
588 nextRowIdx = ( gridWidth + 1 ) * 2;
590 unsigned increment = gridWidth - 1;
595 AddQuadIndices( indices, rowIdx, nextRowIdx );
597 rowIdx = gridWidth * 2;
598 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
600 AddQuadIndices( indices, rowIdx, nextRowIdx );
603 rowIdx = nextRowIdx - 2;
604 nextRowIdx = rowIdx + 4;
605 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
607 AddQuadIndices( indices, rowIdx, nextRowIdx );
612 AddQuadIndices( indices, rowIdx, nextRowIdx );
615 nextRowIdx += gridWidth - 1;
617 AddQuadIndices( indices, rowIdx, nextRowIdx );
621 rowIdx = nextRowIdx - gridWidth + 1;
622 nextRowIdx = rowIdx + gridWidth + 1;
623 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
625 AddQuadIndices( indices, rowIdx, nextRowIdx );
628 return GenerateGeometry( vertices, indices );
631 } // namespace Internal
633 } // namespace Toolkit