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/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>
48 const char * const BORDER_ONLY("borderOnly");
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
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
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
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
75 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
76 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
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
81 vertexPosition = uMvpMatrix * vertexPosition;\n
83 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
85 gl_Position = vertexPosition;\n
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
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
108 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
109 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
111 mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
112 mediump vec2 size = visualSize.xy * scale;\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
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;\
122 vertexPosition = uMvpMatrix * vertexPosition;\n
124 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
126 gl_Position = vertexPosition;\n
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
137 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
142 * @brief Creates the geometry formed from the vertices and indices
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
148 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
150 Property::Map vertexFormat;
151 vertexFormat[ "aPosition" ] = Property::VECTOR2;
152 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
153 if( vertices.Size() > 0 )
155 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
158 // Create the geometry object
159 Geometry geometry = Geometry::New();
160 geometry.AddVertexBuffer( vertexPropertyBuffer );
161 if( indices.Size() > 0 )
163 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
171 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
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
177 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
179 indices.PushBack( rowIdx );
180 indices.PushBack( nextRowIdx + 1 );
181 indices.PushBack( rowIdx + 1 );
183 indices.PushBack( rowIdx );
184 indices.PushBack( nextRowIdx );
185 indices.PushBack( nextRowIdx + 1 );
188 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
190 vertices.PushBack( Vector2( x, y ) );
193 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
195 uint16_t prevEnd = 0;
196 uint16_t prevFix = 0;
197 uint16_t prevStretch = 0;
199 for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
201 uint16_t start = it->GetX();
202 uint16_t end = it->GetY();
204 uint16_t fix = prevFix + start - prevEnd;
205 uint16_t stretch = prevStretch + end - start;
207 std::stringstream uniform;
208 uniform << uniformName << "[" << i << "]";
209 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
213 prevStretch = stretch;
217 prevFix += imageExtent - prevEnd;
218 std::stringstream uniform;
219 uniform << uniformName << "[" << i << "]";
220 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
224 } //unnamed namespace
226 /////////////////NPatchVisual////////////////
228 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl )
230 NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
231 nPatchVisual->mImageUrl = imageUrl;
233 NinePatchImage image = NinePatchImage::New( imageUrl );
234 nPatchVisual->InitializeFromImage( image );
239 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image )
241 NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache );
242 nPatchVisual->mImage = image;
244 nPatchVisual->InitializeFromImage( image );
249 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
250 : Visual::Base( factoryCache ),
261 NPatchVisual::~NPatchVisual()
265 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
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 )
272 borderOnlyValue->Get( mBorderOnly );
276 void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
280 naturalSize.x = mImage.GetWidth();
281 naturalSize.y = mImage.GetHeight();
283 else if( !mImageUrl.empty() )
285 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
286 naturalSize.x = dimentions.GetWidth();
287 naturalSize.y = dimentions.GetHeight();
291 naturalSize = Vector2::ZERO;
295 Geometry NPatchVisual::CreateGeometry()
298 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
302 geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
305 geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
306 mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry );
311 geometry = mFactoryCache.GetGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
314 geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
315 mFactoryCache.SaveGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
319 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
321 Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1, 2 * mStretchPixelsY.Size() + 1 );
322 geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
328 Shader NPatchVisual::CreateShader()
331 if( !mImpl->mCustomShader )
333 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
335 shader = mFactoryCache.GetShader( VisualFactoryCache::NINE_PATCH_SHADER );
338 shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
339 mFactoryCache.SaveShader( VisualFactoryCache::NINE_PATCH_SHADER, shader );
342 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
344 std::stringstream vertexShader;
345 vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
346 << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
349 shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
354 const char* fragmentShader = FRAGMENT_SHADER;
355 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
357 if( !mImpl->mCustomShader->mFragmentShader.empty() )
359 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
361 hints = mImpl->mCustomShader->mHints;
363 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
365 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
367 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
369 std::stringstream vertexShader;
370 vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
371 << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
374 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
381 void NPatchVisual::InitializeRenderer()
383 Geometry geometry = CreateGeometry();
384 Shader shader = CreateShader();
386 if( !geometry || !shader )
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();
392 TextureSet textureSet = TextureSet::New();
393 mImpl->mRenderer = Renderer::New( geometry, shader );
394 mImpl->mRenderer.SetTextures( textureSet );
396 //Register transform properties
397 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
401 void NPatchVisual::DoSetOnStage( Actor& actor )
405 if( !mImageUrl.empty() )
407 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
408 InitializeFromImage( nPatch );
412 InitializeFromImage( mImage );
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();
421 ApplyImageToSampler();
424 actor.AddRenderer( mImpl->mRenderer );
427 void NPatchVisual::DoSetOffStage( Actor& actor )
429 mCroppedImage.Reset();
430 actor.RemoveRenderer( mImpl->mRenderer );
431 mImpl->mRenderer.Reset();
434 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
437 map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
438 if( !mImageUrl.empty() )
440 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
444 map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
446 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
449 void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
454 Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
457 return Dali::Property::Value();
460 void NPatchVisual::OnSetTransform()
462 if( mImpl->mRenderer )
464 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
469 void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
471 //check to see if the border style has changed
473 bool borderOnlyChanged = oldBorderOnly != mBorderOnly;
474 bool gridChanged = oldGridX != mStretchPixelsX.Size() || oldGridY != mStretchPixelsY.Size();
476 if( borderOnlyChanged || gridChanged )
478 Geometry geometry = CreateGeometry();
481 mImpl->mRenderer.SetGeometry( geometry );
485 InitializeFromBrokenImage();
491 Shader shader = CreateShader();
492 TextureSet textureSet;
495 textureSet = mImpl->mRenderer.GetTextures();
498 InitializeFromBrokenImage();
500 mImpl->mRenderer.SetShader( shader );
505 void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
507 mCroppedImage = nPatch.CreateCroppedBufferImage();
510 DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
511 InitializeFromBrokenImage();
515 mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
517 mStretchPixelsX = nPatch.GetStretchPixelsX();
518 mStretchPixelsY = nPatch.GetStretchPixelsY();
521 void NPatchVisual::InitializeFromBrokenImage()
523 mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
524 mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
526 mStretchPixelsX.Clear();
527 mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
528 mStretchPixelsY.Clear();
529 mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
532 void NPatchVisual::ApplyImageToSampler()
534 TextureSet textureSet = mImpl->mRenderer.GetTextures();
537 TextureSetImage( textureSet, 0u, mCroppedImage );
539 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
541 //special case for 9 patch
542 Uint16Pair stretchX = mStretchPixelsX[ 0 ];
543 Uint16Pair stretchY = mStretchPixelsY[ 0 ];
545 uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
546 uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
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 ) );
555 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
556 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
558 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
559 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
564 Geometry NPatchVisual::CreateGeometry( Uint16Pair gridSize )
566 uint16_t gridWidth = gridSize.GetWidth();
567 uint16_t gridHeight = gridSize.GetHeight();
570 Vector< Vector2 > vertices;
571 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
573 for( int y = 0; y < gridHeight + 1; ++y )
575 for( int x = 0; x < gridWidth + 1; ++x )
577 AddVertex( vertices, x, y );
582 //TODO: compare performance with triangle strip when Geometry supports it
583 Vector< unsigned short > indices;
584 indices.Reserve( gridWidth * gridHeight * 6 );
586 unsigned int rowIdx = 0;
587 unsigned int nextRowIdx = gridWidth + 1;
588 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
590 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
592 AddQuadIndices( indices, rowIdx, nextRowIdx );
596 return GenerateGeometry( vertices, indices );
599 Geometry NPatchVisual::CreateGeometryBorder( Uint16Pair gridSize )
601 uint16_t gridWidth = gridSize.GetWidth();
602 uint16_t gridHeight = gridSize.GetHeight();
605 Vector< Vector2 > vertices;
606 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
612 for( int x = 0; x < gridWidth + 1; ++x )
614 AddVertex( vertices, x, y );
618 for(; y < gridHeight - 1; ++y)
621 AddVertex( vertices, 0, y );
622 AddVertex( vertices, 1, y );
625 AddVertex( vertices, gridWidth - 1, y );
626 AddVertex( vertices, gridWidth, y );
630 for(; y < gridHeight + 1; ++y)
632 for( int x = 0; x < gridWidth + 1; ++x )
634 AddVertex( vertices, x, y );
639 //TODO: compare performance with triangle strip when Geometry supports it
640 Vector< unsigned short > indices;
641 indices.Reserve( gridWidth * gridHeight * 6 );
644 unsigned int rowIdx = 0 ;
645 unsigned int nextRowIdx = gridWidth + 1;
646 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
648 AddQuadIndices( indices, rowIdx, nextRowIdx );
653 rowIdx = gridWidth + 1;
654 nextRowIdx = ( gridWidth + 1 ) * 2;
656 unsigned increment = gridWidth - 1;
661 AddQuadIndices( indices, rowIdx, nextRowIdx );
663 rowIdx = gridWidth * 2;
664 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
666 AddQuadIndices( indices, rowIdx, nextRowIdx );
669 rowIdx = nextRowIdx - 2;
670 nextRowIdx = rowIdx + 4;
671 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
673 AddQuadIndices( indices, rowIdx, nextRowIdx );
678 AddQuadIndices( indices, rowIdx, nextRowIdx );
681 nextRowIdx += gridWidth - 1;
683 AddQuadIndices( indices, rowIdx, nextRowIdx );
687 rowIdx = nextRowIdx - gridWidth + 1;
688 nextRowIdx = rowIdx + gridWidth + 1;
689 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
691 AddQuadIndices( indices, rowIdx, nextRowIdx );
694 return GenerateGeometry( vertices, indices );
697 } // namespace Internal
699 } // namespace Toolkit