2 * Copyright (c) 2015 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-renderer.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>
27 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
28 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
29 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
30 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
31 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
45 const char * const BORDER_ONLY("borderOnly");
47 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
48 attribute mediump vec2 aPosition;\n
49 varying mediump vec2 vTexCoord;\n
50 uniform mediump mat4 uMvpMatrix;\n
51 uniform mediump vec3 uSize;\n
52 uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
53 uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
57 mediump vec2 fixedFactor = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
58 mediump vec2 stretch = vec2( uNinePatchFactorsX[ int( ( aPosition.x ) * 0.5 ) ].y, uNinePatchFactorsY[ int( ( aPosition.y ) * 0.5 ) ].y );\n
60 mediump vec2 fixedTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
61 mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
63 mediump vec4 vertexPosition = vec4( ( fixedFactor + ( uSize.xy - fixedTotal ) * stretch / stretchTotal ), 0.0, 1.0 );\n
64 vertexPosition.xy -= uSize.xy * vec2( 0.5, 0.5 );\n
65 vertexPosition = uMvpMatrix * vertexPosition;\n
67 vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
69 gl_Position = vertexPosition;\n
73 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
74 attribute mediump vec2 aPosition;\n
75 varying mediump vec2 vTexCoord;\n
76 uniform mediump mat4 uModelMatrix;\n
77 uniform mediump mat4 uMvpMatrix;\n
78 uniform mediump vec3 uSize;\n
79 uniform mediump vec2 uFixed[ 3 ];\n
80 uniform mediump vec2 uStretchTotal;\n
84 mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
85 mediump vec2 size = uSize.xy * scale;\n
87 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
88 mediump vec2 stretch = floor( aPosition * 0.5 );\n
89 mediump vec2 fixedTotal = uFixed[ 2 ];\n
91 mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
92 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
93 vertexPosition.xy = vertexPosition.xy / scale;\n
95 vertexPosition = uMvpMatrix * vertexPosition;\n
97 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
99 gl_Position = vertexPosition;\n
103 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
104 varying mediump vec2 vTexCoord;\n
105 uniform sampler2D sTexture;\n
106 uniform lowp vec4 uColor;\n
110 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
115 * @brief Creates the geometry formed from the vertices and indices
117 * @param[in] vertices The vertices to generate the geometry from
118 * @param[in] indices The indices to generate the geometry from
119 * @return The geometry formed from the vertices and indices
121 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned int >& indices )
123 Property::Map vertexFormat;
124 vertexFormat[ "aPosition" ] = Property::VECTOR2;
125 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
126 if( vertices.Size() > 0 )
128 vertexPropertyBuffer.SetData( &vertices[ 0 ], vertices.Size() );
131 Property::Map indexFormat;
132 indexFormat[ "indices" ] = Property::INTEGER;
133 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat );
134 if( indices.Size() > 0 )
136 indexPropertyBuffer.SetData( &indices[ 0 ], indices.Size() );
139 // Create the geometry object
140 Geometry geometry = Geometry::New();
141 geometry.AddVertexBuffer( vertexPropertyBuffer );
142 geometry.SetIndexBuffer( indexPropertyBuffer );
148 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
150 * @param[out] indices The indices to add to
151 * @param[in] rowIdx The row index to start the quad
152 * @param[in] nextRowIdx The index to the next row
154 void AddQuadIndices( Vector< unsigned int >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
156 indices.PushBack( rowIdx );
157 indices.PushBack( nextRowIdx + 1 );
158 indices.PushBack( rowIdx + 1 );
160 indices.PushBack( rowIdx );
161 indices.PushBack( nextRowIdx );
162 indices.PushBack( nextRowIdx + 1 );
165 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
167 vertices.PushBack( Vector2( x, y ) );
170 void RegisterStretchProperties( TextureSet& textureSet, const char * uniformName, const NinePatchImage::StretchRanges& stretchPixels, uint16_t imageExtent)
172 uint16_t prevEnd = 0;
173 uint16_t prevFix = 0;
174 uint16_t prevStretch = 0;
176 for( NinePatchImage::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
178 uint16_t start = it->GetX();
179 uint16_t end = it->GetY();
181 uint16_t fix = prevFix + start - prevEnd;
182 uint16_t stretch = prevStretch + end - start;
184 std::stringstream uniform;
185 uniform << uniformName << "[" << i << "]";
186 textureSet.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
190 prevStretch = stretch;
194 prevFix += imageExtent - prevEnd;
195 std::stringstream uniform;
196 uniform << uniformName << "[" << i << "]";
197 textureSet.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
201 } //unnamed namespace
203 /////////////////NPatchRenderer////////////////
205 NPatchRenderer::NPatchRenderer( RendererFactoryCache& factoryCache )
206 : ControlRenderer( factoryCache ),
211 NPatchRenderer::~NPatchRenderer()
215 void NPatchRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
217 Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
220 //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
221 Property::Value* borderOnlyValue = propertyMap.Find( BORDER_ONLY );
222 if( borderOnlyValue )
224 borderOnlyValue->Get( mBorderOnly );
227 if( imageURLValue->Get( mImageUrl ) )
229 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
230 InitializeFromImage( nPatch );
234 InitializeFromBrokenImage();
235 DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
240 void NPatchRenderer::GetNaturalSize( Vector2& naturalSize ) const
244 naturalSize.x = mImage.GetWidth();
245 naturalSize.y = mImage.GetHeight();
247 else if( !mImageUrl.empty() )
249 ImageDimensions dimentions = ResourceImage::GetImageSize( mImageUrl );
250 naturalSize.x = dimentions.GetWidth();
251 naturalSize.y = dimentions.GetHeight();
255 naturalSize = Vector2::ZERO;
259 void NPatchRenderer::SetClipRect( const Rect<int>& clipRect )
261 ControlRenderer::SetClipRect( clipRect );
262 //ToDo: renderer responds to the clipRect change
265 void NPatchRenderer::SetOffset( const Vector2& offset )
267 //ToDo: renderer applies the offset
270 Geometry NPatchRenderer::CreateGeometry()
273 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
277 geometry = mFactoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY );
280 geometry = CreateGeometry( Uint16Pair( 3, 3 ) );
281 mFactoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY, geometry );
286 geometry = mFactoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
289 geometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
290 mFactoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY, geometry );
294 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
296 Uint16Pair gridSize( 2 * mStretchPixelsX.Size() + 1, 2 * mStretchPixelsY.Size() + 1 );
297 geometry = !mBorderOnly ? CreateGeometry( gridSize ) : CreateGeometryBorder( gridSize );
303 Shader NPatchRenderer::CreateShader()
306 if( !mImpl->mCustomShader )
308 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
310 shader = mFactoryCache.GetShader( RendererFactoryCache::NINE_PATCH_SHADER );
313 shader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
314 mFactoryCache.SaveShader( RendererFactoryCache::NINE_PATCH_SHADER, shader );
317 else if( mStretchPixelsX.Size() > 0 || mStretchPixelsY.Size() > 0)
319 std::stringstream vertexShader;
320 vertexShader << "#define FACTOR_SIZE_X " << mStretchPixelsX.Size() + 2 << "\n"
321 << "#define FACTOR_SIZE_Y " << mStretchPixelsY.Size() + 2 << "\n"
324 shader = Shader::New( vertexShader.str(), FRAGMENT_SHADER );
329 const char* fragmentShader = FRAGMENT_SHADER;
330 Dali::Shader::ShaderHints hints = Dali::Shader::HINT_NONE;
332 if( !mImpl->mCustomShader->mFragmentShader.empty() )
334 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
336 hints = mImpl->mCustomShader->mHints;
338 if( mStretchPixelsX.Size() == 1 && mStretchPixelsY.Size() == 1 )
340 shader = Shader::New( VERTEX_SHADER_3X3, fragmentShader, hints );
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(), fragmentShader, hints );
356 void NPatchRenderer::InitializeRenderer()
358 Geometry geometry = CreateGeometry();
359 Shader shader = CreateShader();
361 if( !geometry || !shader )
363 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() );
364 InitializeFromBrokenImage();
367 TextureSet textureSet = TextureSet::New();
368 mImpl->mRenderer = Renderer::New( geometry, shader );
369 mImpl->mRenderer.SetTextures( textureSet );
373 void NPatchRenderer::DoSetOnStage( Actor& actor )
377 if( !mImageUrl.empty() )
379 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
380 InitializeFromImage( nPatch );
384 InitializeFromImage( mImage );
388 //initialize the renderer after initializing from the image since we need to know the grid size from the image before creating the geometry
389 InitializeRenderer();
393 ApplyImageToSampler();
397 void NPatchRenderer::DoSetOffStage( Actor& actor )
399 mCroppedImage.Reset();
400 actor.RemoveRenderer( mImpl->mRenderer );
401 mImpl->mRenderer.Reset();
404 void NPatchRenderer::DoCreatePropertyMap( Property::Map& map ) const
407 map.Insert( RENDERER_TYPE, IMAGE_RENDERER );
408 if( !mImageUrl.empty() )
410 map.Insert( IMAGE_URL_NAME, mImageUrl );
414 map.Insert( IMAGE_URL_NAME, mImage.GetUrl() );
416 map.Insert( BORDER_ONLY, mBorderOnly );
419 void NPatchRenderer::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 NPatchRenderer::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 NPatchRenderer::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 NPatchRenderer::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 NPatchRenderer::InitializeFromBrokenImage()
527 mCroppedImage = RendererFactory::GetBrokenRendererImage();
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 NPatchRenderer::ApplyImageToSampler()
538 TextureSet textureSet = mImpl->mRenderer.GetTextures();
541 textureSet.SetImage( 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 textureSet.RegisterProperty( "uFixed[0]", Vector2::ZERO );
553 textureSet.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
554 textureSet.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
555 textureSet.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
559 textureSet.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
560 textureSet.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
562 RegisterStretchProperties( textureSet, "uNinePatchFactorsX", mStretchPixelsX, mImageSize.GetWidth() );
563 RegisterStretchProperties( textureSet, "uNinePatchFactorsY", mStretchPixelsY, mImageSize.GetHeight() );
568 Geometry NPatchRenderer::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 int > 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 NPatchRenderer::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 int > 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