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/control-renderer-impl.h>
30 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
44 const char * const RENDERER_TYPE("renderer-type");
45 const char * const RENDERER_TYPE_VALUE("n-patch-renderer");
47 const char * const IMAGE_URL_NAME("image-url");
48 const char * const BORDER_ONLY("border-only");
50 std::string TEXTURE_UNIFORM_NAME = "sTexture";
52 const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
53 attribute mediump vec2 aPosition;\n
54 varying mediump vec2 vTexCoord;\n
55 uniform mediump mat4 uModelMatrix;\n
56 uniform mediump mat4 uMvpMatrix;\n
57 uniform mediump vec3 uSize;\n
58 uniform mediump vec2 uFixed[ 3 ];\n
59 uniform mediump vec2 uStretchTotal;\n
63 mediump vec2 scale = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
64 mediump vec2 size = uSize.xy * scale;\n
66 mediump vec2 fixedFactor = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].y );\n
67 mediump vec2 stretch = floor( aPosition * 0.5 );\n
68 mediump vec2 fixedTotal = uFixed[ 2 ];\n
70 mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
71 vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
72 vertexPosition.xy = vertexPosition.xy / scale;\n
74 vertexPosition = uMvpMatrix * vertexPosition;\n
76 vTexCoord = ( fixedFactor + stretch * uStretchTotal ) / ( fixedTotal + uStretchTotal );\n
78 gl_Position = vertexPosition;\n
82 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
83 varying mediump vec2 vTexCoord;\n
84 uniform sampler2D sTexture;\n
85 uniform lowp vec4 uColor;\n
89 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
94 * @brief Creates the geometry formed from the vertices and indices
96 * @param[in] vertices The vertices to generate the geometry from
97 * @param[in] indices The indices to generate the geometry from
98 * @return The geometry formed from the vertices and indices
100 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned int >& indices )
102 Property::Map vertexFormat;
103 vertexFormat[ "aPosition" ] = Property::VECTOR2;
104 PropertyBuffer vertexPropertyBuffer = PropertyBuffer::New( vertexFormat, vertices.Size() );
105 if( vertices.Size() > 0 )
107 vertexPropertyBuffer.SetData( &vertices[ 0 ] );
110 Property::Map indexFormat;
111 indexFormat[ "indices" ] = Property::INTEGER;
112 PropertyBuffer indexPropertyBuffer = PropertyBuffer::New( indexFormat, indices.Size() );
113 if( indices.Size() > 0 )
115 indexPropertyBuffer.SetData( &indices[ 0 ] );
118 // Create the geometry object
119 Geometry geometry = Geometry::New();
120 geometry.AddVertexBuffer( vertexPropertyBuffer );
121 geometry.SetIndexBuffer( indexPropertyBuffer );
127 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
129 * @param[out] indices The indices to add to
130 * @param[in] rowIdx The row index to start the quad
131 * @param[in] nextRowIdx The index to the next row
133 void AddQuadIndices( Vector< unsigned int >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
135 indices.PushBack( rowIdx );
136 indices.PushBack( nextRowIdx + 1 );
137 indices.PushBack( rowIdx + 1 );
139 indices.PushBack( rowIdx );
140 indices.PushBack( nextRowIdx );
141 indices.PushBack( nextRowIdx + 1 );
144 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
146 vertices.PushBack( Vector2( x, y ) );
149 } //unnamed namespace
151 /////////////////NPatchRenderer////////////////
153 NPatchRenderer::NPatchRenderer()
159 NPatchRenderer::~NPatchRenderer()
163 void NPatchRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
165 Initialize(factoryCache);
167 Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
170 //Read the border-only property first since InitialiseFromImage relies on mBorderOnly to be properly set
171 Property::Value* borderOnlyValue = propertyMap.Find( BORDER_ONLY );
172 if( borderOnlyValue )
174 borderOnlyValue->Get( mBorderOnly );
177 if( imageURLValue->Get( mImageUrl ) )
179 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
180 InitialiseFromImage( nPatch );
185 DALI_LOG_ERROR( "The property '%s' is not a string\n", IMAGE_URL_NAME );
190 void NPatchRenderer::SetClipRect( const Rect<int>& clipRect )
192 ControlRenderer::SetClipRect( clipRect );
193 //ToDo: renderer responds to the clipRect change
196 void NPatchRenderer::SetOffset( const Vector2& offset )
198 //ToDo: renderer applies the offset
201 void NPatchRenderer::DoSetOnStage( Actor& actor )
205 if( !mImageUrl.empty() )
207 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
208 InitialiseFromImage( nPatch );
212 InitialiseFromImage( mImage );
218 ApplyImageToSampler();
222 void NPatchRenderer::DoSetOffStage( Actor& actor )
224 mCroppedImage.Reset();
227 void NPatchRenderer::CreatePropertyMap( Property::Map& map ) const
230 map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
231 if( !mImageUrl.empty() )
233 map.Insert( IMAGE_URL_NAME, mImageUrl );
237 map.Insert( IMAGE_URL_NAME, mImage.GetUrl() );
239 map.Insert( BORDER_ONLY, mBorderOnly );
242 void NPatchRenderer::Initialize( RendererFactoryCache& factoryCache )
244 mNinePatchGeometry = factoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY );
245 if( !(mNinePatchGeometry) )
247 mNinePatchGeometry = CreateGeometry( Uint16Pair( 3, 3 ) );
248 factoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_GEOMETRY, mNinePatchGeometry );
251 mNinePatchBorderGeometry = factoryCache.GetGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
252 if( !(mNinePatchBorderGeometry) )
254 mNinePatchBorderGeometry = CreateGeometryBorder( Uint16Pair( 3, 3 ) );
255 factoryCache.SaveGeometry( RendererFactoryCache::NINE_PATCH_BORDER_GEOMETRY, mNinePatchBorderGeometry );
258 mNinePatchShader = factoryCache.GetShader( RendererFactoryCache::NINE_PATCH_SHADER );
259 if( !mNinePatchShader )
261 mNinePatchShader = Shader::New( VERTEX_SHADER_3X3, FRAGMENT_SHADER );
262 factoryCache.SaveShader( RendererFactoryCache::NINE_PATCH_SHADER, mNinePatchShader );
265 mImpl->mGeometry = mNinePatchGeometry;
266 mImpl->mShader = mNinePatchShader;
271 void NPatchRenderer::SetImage( const std::string& imageUrl, bool borderOnly )
273 mBorderOnly = borderOnly;
275 if( mImageUrl == imageUrl )
280 mImageUrl = imageUrl;
281 NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
282 InitialiseFromImage( nPatch );
284 if( mCroppedImage && mImpl->mIsOnStage )
286 ApplyImageToSampler();
290 void NPatchRenderer::SetImage( NinePatchImage image, bool borderOnly )
292 mBorderOnly = borderOnly;
294 if( mImage == image )
300 InitialiseFromImage( mImage );
302 if( mCroppedImage && mImpl->mIsOnStage )
304 ApplyImageToSampler();
308 void NPatchRenderer::InitialiseFromImage( NinePatchImage nPatch )
310 mCroppedImage = nPatch.CreateCroppedBufferImage();
313 DALI_LOG_ERROR("'%s' specify a valid 9 patch image\n", mImageUrl.c_str() );
318 mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
320 mStretchPixelsX = nPatch.GetStretchPixelsX();
321 mStretchPixelsY = nPatch.GetStretchPixelsY();
323 if( mStretchPixelsX.Size() > 0 && mStretchPixelsY.Size() > 0 )
325 //only 9 patch supported for now
326 mImpl->mGeometry = !mBorderOnly ? mNinePatchGeometry : mNinePatchBorderGeometry;
327 mImpl->mShader = mNinePatchShader;
331 void NPatchRenderer::CreateErrorImage()
333 mImageSize = ImageDimensions( 1, 1 );
335 BufferImage bufferImage = BufferImage::New( mImageSize.GetWidth(), mImageSize.GetHeight(), Pixel::RGBA8888 );
336 mCroppedImage = bufferImage;
337 PixelBuffer* pixbuf = bufferImage.GetBuffer();
339 for( size_t i = 0; i < mImageSize.GetWidth() * mImageSize.GetHeight() * 4u; )
347 mStretchPixelsX.Clear();
348 mStretchPixelsX.PushBack( Uint16Pair( 0, mImageSize.GetWidth() ) );
349 mStretchPixelsY.Clear();
350 mStretchPixelsY.PushBack( Uint16Pair( 0, mImageSize.GetHeight() ) );
352 mImpl->mGeometry = mNinePatchGeometry;
353 mImpl->mShader = mNinePatchShader;
356 void NPatchRenderer::ApplyImageToSampler()
358 Material material = mImpl->mRenderer.GetMaterial();
362 for( std::size_t i = 0; i < material.GetNumberOfSamplers(); ++i )
364 sampler = material.GetSamplerAt( i );
365 if( sampler.GetUniformName() == TEXTURE_UNIFORM_NAME )
367 sampler.SetImage( mCroppedImage );
373 sampler = Sampler::New( mCroppedImage, TEXTURE_UNIFORM_NAME );
374 material.AddSampler( sampler );
377 if( mStretchPixelsX.Size() > 0 && mStretchPixelsY.Size() > 0 )
379 //only 9 patch supported for now
380 Uint16Pair stretchX = mStretchPixelsX[ 0 ];
381 Uint16Pair stretchY = mStretchPixelsY[ 0 ];
383 uint16_t stretchWidth = stretchX.GetY() - stretchX.GetX();
384 uint16_t stretchHeight = stretchY.GetY() - stretchY.GetX();
386 sampler.RegisterProperty( "uFixed[0]", Vector2::ZERO );
387 sampler.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
388 sampler.RegisterProperty( "uFixed[2]", Vector2( mImageSize.GetWidth() - stretchWidth, mImageSize.GetHeight() - stretchHeight ) );
389 sampler.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
394 Geometry NPatchRenderer::CreateGeometry( Uint16Pair gridSize )
396 uint16_t gridWidth = gridSize.GetWidth();
397 uint16_t gridHeight = gridSize.GetHeight();
400 Vector< Vector2 > vertices;
401 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
403 for( int y = 0; y < gridHeight + 1; ++y )
405 for( int x = 0; x < gridWidth + 1; ++x )
407 AddVertex( vertices, x, y );
412 //TODO: compare performance with triangle strip when Geometry supports it
413 Vector< unsigned int > indices;
414 indices.Reserve( gridWidth * gridHeight * 6 );
416 unsigned int rowIdx = 0;
417 unsigned int nextRowIdx = gridWidth + 1;
418 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
420 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
422 AddQuadIndices( indices, rowIdx, nextRowIdx );
426 return GenerateGeometry( vertices, indices );
429 Geometry NPatchRenderer::CreateGeometryBorder( Uint16Pair gridSize )
431 uint16_t gridWidth = gridSize.GetWidth();
432 uint16_t gridHeight = gridSize.GetHeight();
435 Vector< Vector2 > vertices;
436 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
442 for( int x = 0; x < gridWidth + 1; ++x )
444 AddVertex( vertices, x, y );
448 for(; y < gridHeight - 1; ++y)
451 AddVertex( vertices, 0, y );
452 AddVertex( vertices, 1, y );
455 AddVertex( vertices, gridWidth - 1, y );
456 AddVertex( vertices, gridWidth, y );
460 for(; y < gridHeight + 1; ++y)
462 for( int x = 0; x < gridWidth + 1; ++x )
464 AddVertex( vertices, x, y );
469 //TODO: compare performance with triangle strip when Geometry supports it
470 Vector< unsigned int > indices;
471 indices.Reserve( gridWidth * gridHeight * 6 );
474 unsigned int rowIdx = 0 ;
475 unsigned int nextRowIdx = gridWidth + 1;
476 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
478 AddQuadIndices( indices, rowIdx, nextRowIdx );
483 rowIdx = gridWidth + 1;
484 nextRowIdx = ( gridWidth + 1 ) * 2;
486 unsigned increment = gridWidth - 1;
491 AddQuadIndices( indices, rowIdx, nextRowIdx );
493 rowIdx = gridWidth * 2;
494 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
496 AddQuadIndices( indices, rowIdx, nextRowIdx );
499 rowIdx = nextRowIdx - 2;
500 nextRowIdx = rowIdx + 4;
501 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
503 AddQuadIndices( indices, rowIdx, nextRowIdx );
508 AddQuadIndices( indices, rowIdx, nextRowIdx );
511 nextRowIdx += gridWidth - 1;
513 AddQuadIndices( indices, rowIdx, nextRowIdx );
517 rowIdx = nextRowIdx - gridWidth + 1;
518 nextRowIdx = rowIdx + gridWidth + 1;
519 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
521 AddQuadIndices( indices, rowIdx, nextRowIdx );
524 return GenerateGeometry( vertices, indices );
527 } // namespace Internal
529 } // namespace Toolkit