2 * Copyright (c) 2017 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/public-api/images/buffer-image.h>
23 #include <dali/public-api/images/resource-image.h>
24 #include <dali/devel-api/images/texture-set-image.h>
25 #include <dali/integration-api/debug.h>
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/devel-api/visuals/image-visual-properties-devel.h>
31 #include <dali-toolkit/internal/visuals/npatch-loader.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
50 const char * const BORDER_ONLY( "borderOnly" );
51 const char * const BORDER( "border" );
53 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
54 attribute mediump vec2 aPosition;\n
55 varying mediump vec2 vTexCoord;\n
56 uniform mediump mat4 uMvpMatrix;\n
57 uniform mediump vec3 uSize;\n
58 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
59 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
62 //Visual size and offset
63 uniform mediump vec2 offset;\n
64 uniform mediump vec2 size;\n
65 uniform mediump vec4 offsetSizeMode;\n
66 uniform mediump vec2 origin;\n
67 uniform mediump vec2 anchorPoint;\n
71 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
72 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
74 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
75 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
78 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
79 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
81 mediump vec4 vertexPosition = vec4( ( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal ) + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
82 vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
84 vertexPosition = uMvpMatrix * vertexPosition;\n
86 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
88 gl_Position = vertexPosition;\n
92 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
93 attribute mediump vec2 aPosition;\n
94 varying mediump vec2 vTexCoord;\n
95 uniform mediump mat4 uModelMatrix;\n
96 uniform mediump mat4 uMvpMatrix;\n
97 uniform mediump vec3 uSize;\n
98 uniform mediump vec2 uFixed[ 3 ];\n
99 uniform mediump vec2 uStretchTotal;\n
102 //Visual size and offset
103 uniform mediump vec2 offset;\n
104 uniform mediump vec2 size;\n
105 uniform mediump vec4 offsetSizeMode;\n
106 uniform mediump vec2 origin;\n
107 uniform mediump vec2 anchorPoint;\n
111 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
112 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
114 mediump vec2 size = visualSize.xy;\n
116 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
117 mediump vec2 stretch = floor( aPosition * 0.5 );\n
118 mediump vec2 fixedTotal = uFixed[ 2 ];\n
120 mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );
121 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
122 vertexPosition.xy = vertexPosition.xy + anchorPoint*size + (visualOffset + origin)*uSize.xy;\
124 vertexPosition = uMvpMatrix * vertexPosition;\n
126 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
128 gl_Position = vertexPosition;\n
132 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
133 varying mediump vec2 vTexCoord;\n
134 uniform sampler2D sTexture;\n
135 uniform lowp vec4 uColor;\n
136 uniform lowp vec3 mixColor;\n
137 uniform lowp float opacity;\n
138 uniform lowp float preMultipliedAlpha;\n
139 lowp vec4 visualMixColor()\n
141 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
145 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * visualMixColor();\n
150 * @brief Creates the geometry formed from the vertices and indices
152 * @param[in] vertices The vertices to generate the geometry from
153 * @param[in] indices The indices to generate the geometry from
154 * @return The geometry formed from the vertices and indices
156 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
158 Property::Map vertexFormat;
159 vertexFormat[ "aPosition" ] = Property::VECTOR2;
160 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
161 if( vertices.Size() > 0 )
163 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
166 // Create the geometry object
167 Geometry geometry = Geometry::New();
168 geometry.AddVertexBuffer( vertexPropertyBuffer );
169 if( indices.Size() > 0 )
171 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
179 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
181 * @param[out] indices The indices to add to
182 * @param[in] rowIdx The row index to start the quad
183 * @param[in] nextRowIdx The index to the next row
185 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
187 indices.PushBack( rowIdx );
188 indices.PushBack( nextRowIdx + 1 );
189 indices.PushBack( rowIdx + 1 );
191 indices.PushBack( rowIdx );
192 indices.PushBack( nextRowIdx );
193 indices.PushBack( nextRowIdx + 1 );
196 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
198 vertices.PushBack( Vector2( x, y ) );
201 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
203 uint16_t prevEnd = 0;
204 uint16_t prevFix = 0;
205 uint16_t prevStretch = 0;
207 for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
209 uint16_t start = it->GetX();
210 uint16_t end = it->GetY();
212 uint16_t fix = prevFix + start - prevEnd;
213 uint16_t stretch = prevStretch + end - start;
215 std::stringstream uniform;
216 uniform << uniformName << "[" << i << "]";
217 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
221 prevStretch = stretch;
225 prevFix += imageExtent - prevEnd;
226 std::stringstream uniform;
227 uniform << uniformName << "[" << i << "]";
228 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
232 } //unnamed namespace
234 /////////////////NPatchVisual////////////////
236 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
238 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
239 nPatchVisual->mImageUrl = imageUrl;
240 nPatchVisual->SetProperties( properties );
245 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
247 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
248 nPatchVisual->mImageUrl = imageUrl;
253 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
255 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
256 VisualUrl visualUrl( image.GetUrl() );
257 nPatchVisual->mImageUrl = visualUrl;
261 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
266 // load now if not already loaded
267 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
269 mId = mLoader.Load( mImageUrl.GetUrl(), mBorder );
271 const NPatchLoader::Data* data;
272 if( mLoader.GetNPatchData( mId, data ) )
274 naturalSize.x = data->croppedWidth;
275 naturalSize.y = data->croppedHeight;
279 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
281 // URL is already passed in via constructor
283 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
284 if( borderOnlyValue )
286 borderOnlyValue->Get( mBorderOnly );
289 Property::Value* borderValue = propertyMap.Find( Toolkit::DevelImageVisual::Property::BORDER, BORDER );
290 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
292 // Not a rect so try vector4
294 if( borderValue->Get( border ) )
296 mBorder.left = static_cast< int >( border.x );
297 mBorder.right = static_cast< int >( border.y );
298 mBorder.bottom = static_cast< int >( border.z );
299 mBorder.top = static_cast< int >( border.w );
304 void NPatchVisual::DoSetOnStage( Actor& actor )
306 // load when first go on stage
307 if( NPatchLoader::UNINITIALIZED_ID == mId && mImageUrl.IsLocalResource() )
309 mId = mLoader.Load( mImageUrl.GetUrl(), mBorder );
312 Geometry geometry = CreateGeometry();
313 Shader shader = CreateShader();
314 mImpl->mRenderer = Renderer::New( geometry, shader );
316 ApplyTextureAndUniforms();
318 actor.AddRenderer( mImpl->mRenderer );
320 // npatch loaded and ready to display
324 void NPatchVisual::DoSetOffStage( Actor& actor )
326 actor.RemoveRenderer( mImpl->mRenderer );
327 mImpl->mRenderer.Reset();
330 void NPatchVisual::OnSetTransform()
332 if( mImpl->mRenderer )
334 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
338 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
341 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::DevelVisual::N_PATCH );
342 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
343 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
344 map.Insert( Toolkit::DevelImageVisual::Property::BORDER, mBorder );
347 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
352 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
353 : Visual::Base( factoryCache ),
354 mLoader( factoryCache.GetNPatchLoader() ),
356 mId( NPatchLoader::UNINITIALIZED_ID ),
357 mBorderOnly( false ),
362 NPatchVisual::~NPatchVisual()
366 Geometry NPatchVisual::CreateGeometry()
369 const NPatchLoader::Data* data;
370 if( mLoader.GetNPatchData( mId, data ) )
372 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
374 if( DALI_UNLIKELY( mBorderOnly ) )
376 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
380 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
383 else if( data->stretchPixelsX.Size() > 0 || data->stretchPixelsY.Size() > 0)
385 Uint16Pair gridSize( 2 * data->stretchPixelsX.Size() + 1, 2 * data->stretchPixelsY.Size() + 1 );
386 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
391 // no N patch data so use default geometry
392 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
397 Shader NPatchVisual::CreateShader()
400 const NPatchLoader::Data* data;
401 // 0 is either no data (load failed?) or no stretch regions on image
402 // for both cases we use the default shader
403 NinePatchImage::StretchRanges::SizeType xStretchCount = 0;
404 NinePatchImage::StretchRanges::SizeType yStretchCount = 0;
406 // ask loader for the regions
407 if( mLoader.GetNPatchData( mId, data ) )
409 xStretchCount = data->stretchPixelsX.Count();
410 yStretchCount = data->stretchPixelsY.Count();
413 if( DALI_LIKELY( !mImpl->mCustomShader ) )
415 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
416 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
418 shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
419 if( DALI_UNLIKELY( !shader ) )
421 shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
422 mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
425 else if( xStretchCount > 0 || yStretchCount > 0)
427 std::stringstream vertexShader;
428 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
429 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
432 shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
437 const char* fragmentShader = FRAGMENT_SHADER;
438 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
440 if( !mImpl->mCustomShader->mFragmentShader.empty() )
442 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
444 hints = mImpl->mCustomShader->mHints;
446 /* Apply Custom Vertex Shader only if image is 9-patch */
447 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
448 ( xStretchCount == 0 && yStretchCount == 0 ) )
450 const char* vertexShader = VERTEX_SHADER_3X3;
452 if( !mImpl->mCustomShader->mVertexShader.empty() )
454 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
456 shader = Shader::New( vertexShader, fragmentShader, hints );
458 else if( xStretchCount > 0 || yStretchCount > 0)
460 std::stringstream vertexShader;
461 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
462 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
465 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
472 void NPatchVisual::ApplyTextureAndUniforms()
474 const NPatchLoader::Data* data;
475 if( mLoader.GetNPatchData( mId, data ) )
477 TextureSet textures( data->textureSet );
478 mImpl->mRenderer.SetTextures( textures );
479 if( data->stretchPixelsX.Size() == 1 && data->stretchPixelsY.Size() == 1 )
481 //special case for 9 patch
482 Uint16Pair stretchX = data->stretchPixelsX[ 0 ];
483 Uint16Pair stretchY = data->stretchPixelsY[ 0 ];
485 uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
486 uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
488 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
489 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
490 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->croppedWidth - stretchWidth, data->croppedHeight - stretchHeight ) );
491 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
495 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
496 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
498 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->stretchPixelsX, data->croppedWidth );
499 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->stretchPixelsY, data->croppedHeight );
504 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
505 TextureSet textureSet = TextureSet::New();
506 mImpl->mRenderer.SetTextures( textureSet );
507 Image croppedImage = VisualFactoryCache::GetBrokenVisualImage();
508 TextureSetImage( textureSet, 0u, croppedImage );
509 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
510 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
511 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
512 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
515 //Register transform properties
516 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
519 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
521 Geometry geometry = mFactoryCache.GetGeometry( subType );
524 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
526 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
528 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
530 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
532 mFactoryCache.SaveGeometry( subType, geometry );
537 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
539 uint16_t gridWidth = gridSize.GetWidth();
540 uint16_t gridHeight = gridSize.GetHeight();
543 Vector< Vector2 > vertices;
544 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
546 for( int y = 0; y < gridHeight + 1; ++y )
548 for( int x = 0; x < gridWidth + 1; ++x )
550 AddVertex( vertices, x, y );
555 Vector< unsigned short > indices;
556 indices.Reserve( gridWidth * gridHeight * 6 );
558 unsigned int rowIdx = 0;
559 unsigned int nextRowIdx = gridWidth + 1;
560 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
562 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
564 AddQuadIndices( indices, rowIdx, nextRowIdx );
568 return GenerateGeometry( vertices, indices );
571 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
573 uint16_t gridWidth = gridSize.GetWidth();
574 uint16_t gridHeight = gridSize.GetHeight();
577 Vector< Vector2 > vertices;
578 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
584 for( int x = 0; x < gridWidth + 1; ++x )
586 AddVertex( vertices, x, y );
590 for(; y < gridHeight - 1; ++y)
593 AddVertex( vertices, 0, y );
594 AddVertex( vertices, 1, y );
597 AddVertex( vertices, gridWidth - 1, y );
598 AddVertex( vertices, gridWidth, y );
602 for(; y < gridHeight + 1; ++y)
604 for( int x = 0; x < gridWidth + 1; ++x )
606 AddVertex( vertices, x, y );
611 Vector< unsigned short > indices;
612 indices.Reserve( gridWidth * gridHeight * 6 );
615 unsigned int rowIdx = 0 ;
616 unsigned int nextRowIdx = gridWidth + 1;
617 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
619 AddQuadIndices( indices, rowIdx, nextRowIdx );
624 rowIdx = gridWidth + 1;
625 nextRowIdx = ( gridWidth + 1 ) * 2;
627 unsigned increment = gridWidth - 1;
632 AddQuadIndices( indices, rowIdx, nextRowIdx );
634 rowIdx = gridWidth * 2;
635 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
637 AddQuadIndices( indices, rowIdx, nextRowIdx );
640 rowIdx = nextRowIdx - 2;
641 nextRowIdx = rowIdx + 4;
642 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
644 AddQuadIndices( indices, rowIdx, nextRowIdx );
649 AddQuadIndices( indices, rowIdx, nextRowIdx );
652 nextRowIdx += gridWidth - 1;
654 AddQuadIndices( indices, rowIdx, nextRowIdx );
658 rowIdx = nextRowIdx - gridWidth + 1;
659 nextRowIdx = rowIdx + gridWidth + 1;
660 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
662 AddQuadIndices( indices, rowIdx, nextRowIdx );
665 return GenerateGeometry( vertices, indices );
668 } // namespace Internal
670 } // namespace Toolkit