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/internal/visuals/visual-factory-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
47 const char * const BORDER_ONLY("borderOnly");
49 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
50 attribute mediump vec2 aPosition;\n
51 varying mediump vec2 vTexCoord;\n
52 uniform mediump mat4 uMvpMatrix;\n
53 uniform mediump vec3 uSize;\n
54 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
55 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
59 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
60 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
62 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
63 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
65 mediump vec4 vertexPosition = vec4( ( fixedFactor + ( uSize.xy - fixedTotal ) * stretch / stretchTotal ), 0.0, 1.0 );\n
66 vertexPosition.xy -= uSize.xy * vec2( 0.5, 0.5 );\n
67 vertexPosition = uMvpMatrix * vertexPosition;\n
69 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
71 gl_Position = vertexPosition;\n
75 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
76 attribute mediump vec2 aPosition;\n
77 varying mediump vec2 vTexCoord;\n
78 uniform mediump mat4 uModelMatrix;\n
79 uniform mediump mat4 uMvpMatrix;\n
80 uniform mediump vec3 uSize;\n
81 uniform mediump vec2 uFixed[ 3 ];\n
82 uniform mediump vec2 uStretchTotal;\n
86 mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
87 mediump vec2 size = uSize.xy * scale;\n
89 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
90 mediump vec2 stretch = floor( aPosition * 0.5 );\n
91 mediump vec2 fixedTotal = uFixed[ 2 ];\n
93 mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
94 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
95 vertexPosition.xy = vertexPosition.xy / scale;\n
97 vertexPosition = uMvpMatrix * vertexPosition;\n
99 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
101 gl_Position = vertexPosition;\n
105 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
106 varying mediump vec2 vTexCoord;\n
107 uniform sampler2D sTexture;\n
108 uniform lowp vec4 uColor;\n
112 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
117 * @brief Creates the geometry formed from the vertices and indices
119 * @param[in] vertices The vertices to generate the geometry from
120 * @param[in] indices The indices to generate the geometry from
121 * @return The geometry formed from the vertices and indices
123 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
125 Property::Map vertexFormat;
126 vertexFormat[ "aPosition" ] = Property::VECTOR2;
127 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
128 if( vertices.Size() > 0 )
130 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
133 // Create the geometry object
134 Geometry geometry = Geometry::New();
135 geometry.AddVertexBuffer( vertexPropertyBuffer );
136 if( indices.Size() > 0 )
138 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
146 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
148 * @param[out] indices The indices to add to
149 * @param[in] rowIdx The row index to start the quad
150 * @param[in] nextRowIdx The index to the next row
152 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
154 indices.PushBack( rowIdx );
155 indices.PushBack( nextRowIdx + 1 );
156 indices.PushBack( rowIdx + 1 );
158 indices.PushBack( rowIdx );
159 indices.PushBack( nextRowIdx );
160 indices.PushBack( nextRowIdx + 1 );
163 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
165 vertices.PushBack( Vector2( x, y ) );
168 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
170 uint16_t prevEnd = 0;
171 uint16_t prevFix = 0;
172 uint16_t prevStretch = 0;
174 for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
176 uint16_t start = it->GetX();
177 uint16_t end = it->GetY();
179 uint16_t fix = prevFix + start - prevEnd;
180 uint16_t stretch = prevStretch + end - start;
182 std::stringstream uniform;
183 uniform << uniformName << "[" << i << "]";
184 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
188 prevStretch = stretch;
192 prevFix += imageExtent - prevEnd;
193 std::stringstream uniform;
194 uniform << uniformName << "[" << i << "]";
195 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
199 } //unnamed namespace
201 /////////////////NPatchVisual////////////////
203 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
204 : Visual::Base( factoryCache ),
209 NPatchVisual::~NPatchVisual()
213 void NPatchVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
215 Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
218 //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
219 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
220 if( borderOnlyValue )
222 borderOnlyValue->Get( mBorderOnly );
225 if( imageURLValue->Get( mImageUrl ) )
227 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
228 InitializeFromImage( nPatch );
232 InitializeFromBrokenImage();
233 DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
238 void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
242 naturalSize.x = mImage.GetWidth();
243 naturalSize.y = mImage.GetHeight();
245 else if( !mImageUrl.empty() )
247 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
248 naturalSize.x = dimentions.GetWidth();
249 naturalSize.y = dimentions.GetHeight();
253 naturalSize = Vector2::ZERO;
257 Geometry NPatchVisual::CreateGeometry()
260 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
264 geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
267 geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
268 mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry );
273 geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
276 geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
277 mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
281 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
283 Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1, 2 * mStretchPixelsY.Size() + 1 );
284 geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
290 Shader NPatchVisual::CreateShader()
293 if( !mImpl->mCustomShader )
295 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
297 shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
300 shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
301 mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
304 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
306 std::stringstream vertexShader;
307 vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
308 << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
311 shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
316 const char* fragmentShader = FRAGMENT_SHADER;
317 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
319 if( !mImpl->mCustomShader->mFragmentShader.empty() )
321 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
323 hints = mImpl->mCustomShader->mHints;
325 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
327 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
329 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
331 std::stringstream vertexShader;
332 vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
333 << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
336 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
343 void NPatchVisual::InitializeRenderer()
345 Geometry geometry = CreateGeometry();
346 Shader shader = CreateShader();
348 if( !geometry || !shader )
350 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() );
351 InitializeFromBrokenImage();
354 TextureSet textureSet = TextureSet::New();
355 mImpl->mRenderer = Renderer::New( geometry, shader );
356 mImpl->mRenderer.SetTextures( textureSet );
360 void NPatchVisual::DoSetOnStage( Actor& actor )
364 if( !mImageUrl.empty() )
366 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
367 InitializeFromImage( nPatch );
371 InitializeFromImage( mImage );
375 //initialize the renderer after initializing from the image since we need to know the grid size from the image before creating the geometry
376 InitializeRenderer();
380 ApplyImageToSampler();
383 actor.AddRenderer( mImpl->mRenderer );
386 void NPatchVisual::DoSetOffStage( Actor& actor )
388 mCroppedImage.Reset();
389 actor.RemoveRenderer( mImpl->mRenderer );
390 mImpl->mRenderer.Reset();
393 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
396 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE );
397 if( !mImageUrl.empty() )
399 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
403 map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
405 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
408 void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
413 Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
416 return Dali::Property::Value();
419 void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
421 //check to see if the border style has changed
423 bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
424 bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
426 if( borderOnlyChanged || gridChanged )
428 Geometry geometry = CreateGeometry();
431 mImpl->mRenderer.SetGeometry( geometry );
435 InitializeFromBrokenImage();
441 Shader shader = CreateShader();
442 TextureSet textureSet;
445 textureSet = mImpl->mRenderer.GetTextures();
448 InitializeFromBrokenImage();
450 mImpl->mRenderer.SetShader( shader );
455 void NPatchVisual::SetImage( const std::string& imageUrl, bool borderOnly )
457 bool oldBorderOnly = mBorderOnly;
458 size_t oldGridX = mStretchPixelsX.Size();
459 size_t oldGridY = mStretchPixelsY.Size();
461 mBorderOnly = borderOnly;
463 if( mImageUrl == imageUrl )
468 mImageUrl = imageUrl;
469 if( mImpl->mRenderer )
471 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
472 InitializeFromImage( nPatch );
474 ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
478 ApplyImageToSampler();
483 void NPatchVisual::SetImage( NinePatchImage image, bool borderOnly )
485 bool oldBorderOnly = mBorderOnly;
486 size_t oldGridX = mStretchPixelsX.Size();
487 size_t oldGridY = mStretchPixelsY.Size();
489 mBorderOnly = borderOnly;
491 if( mImage == image )
497 if( mImpl->mRenderer )
499 InitializeFromImage( mImage );
500 ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
504 ApplyImageToSampler();
509 void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
511 mCroppedImage = nPatch.CreateCroppedBufferImage();
514 DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
515 InitializeFromBrokenImage();
519 mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
521 mStretchPixelsX = nPatch.GetStretchPixelsX();
522 mStretchPixelsY = nPatch.GetStretchPixelsY();
525 void NPatchVisual::InitializeFromBrokenImage()
527 mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
528 mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
530 mStretchPixelsX.Clear();
531 mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
532 mStretchPixelsY.Clear();
533 mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
536 void NPatchVisual::ApplyImageToSampler()
538 TextureSet textureSet = mImpl->mRenderer.GetTextures();
541 TextureSetImage( textureSet, 0u, mCroppedImage );
543 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
545 //special case for 9 patch
546 Uint16Pair stretchX = mStretchPixelsX[ 0 ];
547 Uint16Pair stretchY = mStretchPixelsY[ 0 ];
549 uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
550 uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
552 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
553 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
554 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
555 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
559 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
560 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
562 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
563 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
568 Geometry NPatchVisual::CreateGeometry( Uint16Pair gridSize )
570 uint16_t gridWidth = gridSize.GetWidth();
571 uint16_t gridHeight = gridSize.GetHeight();
574 Vector< Vector2 > vertices;
575 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
577 for( int y = 0; y < gridHeight + 1; ++y )
579 for( int x = 0; x < gridWidth + 1; ++x )
581 AddVertex( vertices, x, y );
586 //TODO: compare performance with triangle strip when Geometry supports it
587 Vector< unsigned short > indices;
588 indices.Reserve( gridWidth * gridHeight * 6 );
590 unsigned int rowIdx = 0;
591 unsigned int nextRowIdx = gridWidth + 1;
592 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
594 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
596 AddQuadIndices( indices, rowIdx, nextRowIdx );
600 return GenerateGeometry( vertices, indices );
603 Geometry NPatchVisual::CreateGeometryBorder( Uint16Pair gridSize )
605 uint16_t gridWidth = gridSize.GetWidth();
606 uint16_t gridHeight = gridSize.GetHeight();
609 Vector< Vector2 > vertices;
610 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
616 for( int x = 0; x < gridWidth + 1; ++x )
618 AddVertex( vertices, x, y );
622 for(; y < gridHeight - 1; ++y)
625 AddVertex( vertices, 0, y );
626 AddVertex( vertices, 1, y );
629 AddVertex( vertices, gridWidth - 1, y );
630 AddVertex( vertices, gridWidth, y );
634 for(; y < gridHeight + 1; ++y)
636 for( int x = 0; x < gridWidth + 1; ++x )
638 AddVertex( vertices, x, y );
643 //TODO: compare performance with triangle strip when Geometry supports it
644 Vector< unsigned short > indices;
645 indices.Reserve( gridWidth * gridHeight * 6 );
648 unsigned int rowIdx = 0 ;
649 unsigned int nextRowIdx = gridWidth + 1;
650 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
652 AddQuadIndices( indices, rowIdx, nextRowIdx );
657 rowIdx = gridWidth + 1;
658 nextRowIdx = ( gridWidth + 1 ) * 2;
660 unsigned increment = gridWidth - 1;
665 AddQuadIndices( indices, rowIdx, nextRowIdx );
667 rowIdx = gridWidth * 2;
668 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
670 AddQuadIndices( indices, rowIdx, nextRowIdx );
673 rowIdx = nextRowIdx - 2;
674 nextRowIdx = rowIdx + 4;
675 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
677 AddQuadIndices( indices, rowIdx, nextRowIdx );
682 AddQuadIndices( indices, rowIdx, nextRowIdx );
685 nextRowIdx += gridWidth - 1;
687 AddQuadIndices( indices, rowIdx, nextRowIdx );
691 rowIdx = nextRowIdx - gridWidth + 1;
692 nextRowIdx = rowIdx + gridWidth + 1;
693 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
695 AddQuadIndices( indices, rowIdx, nextRowIdx );
698 return GenerateGeometry( vertices, indices );
701 } // namespace Internal
703 } // namespace Toolkit