2 * Copyright (c) 2021 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/devel-api/rendering/renderer-devel.h>
23 #include <dali/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/integration-api/debug.h>
27 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
28 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
29 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
30 #include <dali-toolkit/internal/visuals/npatch-loader.h>
31 #include <dali-toolkit/internal/visuals/rendering-addon.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
37 #include <dali-toolkit/public-api/visuals/visual-properties.h>
52 * @brief Creates the geometry formed from the vertices and indices
54 * @param[in] vertices The vertices to generate the geometry from
55 * @param[in] indices The indices to generate the geometry from
56 * @return The geometry formed from the vertices and indices
58 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
60 Property::Map vertexFormat;
61 vertexFormat[ "aPosition" ] = Property::VECTOR2;
62 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
63 if( vertices.Size() > 0 )
65 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
68 // Create the geometry object
69 Geometry geometry = Geometry::New();
70 geometry.AddVertexBuffer( vertexBuffer );
71 if( indices.Size() > 0 )
73 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
81 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
83 * @param[out] indices The indices to add to
84 * @param[in] rowIdx The row index to start the quad
85 * @param[in] nextRowIdx The index to the next row
87 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
89 indices.PushBack( rowIdx );
90 indices.PushBack( nextRowIdx + 1 );
91 indices.PushBack( rowIdx + 1 );
93 indices.PushBack( rowIdx );
94 indices.PushBack( nextRowIdx );
95 indices.PushBack( nextRowIdx + 1 );
98 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
100 vertices.PushBack( Vector2( x, y ) );
103 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent)
105 uint16_t prevEnd = 0;
106 uint16_t prevFix = 0;
107 uint16_t prevStretch = 0;
109 for( NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
111 uint16_t start = it->GetX();
112 uint16_t end = it->GetY();
114 uint16_t fix = prevFix + start - prevEnd;
115 uint16_t stretch = prevStretch + end - start;
117 std::stringstream uniform;
118 uniform << uniformName << "[" << i << "]";
119 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
123 prevStretch = stretch;
127 prevFix += imageExtent - prevEnd;
128 std::stringstream uniform;
129 uniform << uniformName << "[" << i << "]";
130 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
134 } //unnamed namespace
136 /////////////////NPatchVisual////////////////
138 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
140 NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
141 nPatchVisual->mImageUrl = imageUrl;
142 nPatchVisual->SetProperties( properties );
143 nPatchVisual->Initialize();
147 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
149 NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
150 nPatchVisual->mImageUrl = imageUrl;
151 nPatchVisual->Initialize();
155 void NPatchVisual::LoadImages()
157 TextureManager& textureManager = mFactoryCache.GetTextureManager();
158 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
160 if( mId == NPatchData::INVALID_NPATCH_DATA_ID && mImageUrl.IsLocalResource() )
162 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
163 mId = mLoader.Load( textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading );
165 const NPatchData* data;
166 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
168 EnablePreMultipliedAlpha( data->IsPreMultiplied() );
172 if( !mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
174 // Load the auxiliary image
175 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
176 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer( mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT,
177 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
178 this, true, preMultiplyOnLoading );
182 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
187 // load now if not already loaded
188 const NPatchData* data;
189 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() != NPatchData::LoadingState::LOADING )
191 naturalSize.x = data->GetCroppedWidth();
192 naturalSize.y = data->GetCroppedHeight();
196 if( mImageUrl.IsValid() )
198 ImageDimensions dimensions = Dali::GetOriginalImageSize( mImageUrl.GetUrl() );
199 if( dimensions != ImageDimensions( 0, 0 ) )
201 naturalSize.x = dimensions.GetWidth();
202 naturalSize.y = dimensions.GetHeight();
207 if( mAuxiliaryPixelBuffer )
209 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
210 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
214 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
216 // URL is already passed in via constructor
218 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
219 if( borderOnlyValue )
221 borderOnlyValue->Get( mBorderOnly );
224 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
225 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
227 // Not a rect so try vector4
229 if( borderValue->Get( border ) )
231 mBorder.left = static_cast< int >( border.x );
232 mBorder.right = static_cast< int >( border.y );
233 mBorder.bottom = static_cast< int >( border.z );
234 mBorder.top = static_cast< int >( border.w );
238 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
242 if( auxImage->Get( url ) )
248 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
251 auxImageAlpha->Get( mAuxiliaryImageAlpha );
254 Property::Value* synchronousLoading = propertyMap.Find( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING );
255 if( synchronousLoading )
258 synchronousLoading->Get( sync );
261 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
265 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
269 Property::Value* releasePolicy = propertyMap.Find( Toolkit::ImageVisual::Property::RELEASE_POLICY, RELEASE_POLICY_NAME );
272 releasePolicy->Get( mReleasePolicy );
276 void NPatchVisual::DoSetOnScene( Actor& actor )
278 // load when first go on stage
281 const NPatchData* data;
282 if( mLoader.GetNPatchData( mId, data ) )
284 Geometry geometry = CreateGeometry();
285 Shader shader = CreateShader();
287 mImpl->mRenderer.SetGeometry(geometry);
288 mImpl->mRenderer.SetShader(shader);
290 mPlacementActor = actor;
291 if( data->GetLoadingState() != NPatchData::LoadingState::LOADING )
293 if( RenderingAddOn::Get().IsValid() )
295 RenderingAddOn::Get().SubmitRenderTask( mImpl->mRenderer, data->GetRenderingMap() );
298 ApplyTextureAndUniforms();
299 actor.AddRenderer( mImpl->mRenderer );
300 mPlacementActor.Reset();
302 // npatch loaded and ready to display
303 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
308 void NPatchVisual::DoSetOffScene( Actor& actor )
310 if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
312 mLoader.Remove(mId, this);
313 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
314 mId = NPatchData::INVALID_NPATCH_DATA_ID;
317 actor.RemoveRenderer( mImpl->mRenderer );
318 mPlacementActor.Reset();
321 void NPatchVisual::OnSetTransform()
323 if( mImpl->mRenderer )
325 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
329 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
332 bool sync = IsSynchronousLoadingRequired();
333 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync );
334 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
335 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
336 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
337 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
338 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
340 if( mAuxiliaryUrl.IsValid() )
342 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
343 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
347 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
349 if( mAuxiliaryUrl.IsValid() )
351 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
352 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
356 NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory)
357 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH),
359 mLoader(factoryCache.GetNPatchLoader()),
360 mImageVisualShaderFactory(shaderFactory),
363 mId(NPatchData::INVALID_NPATCH_DATA_ID),
366 mAuxiliaryImageAlpha(0.0f),
367 mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED)
369 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
372 NPatchVisual::~NPatchVisual()
374 if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ))
376 mLoader.Remove(mId, this);
377 mId = NPatchData::INVALID_NPATCH_DATA_ID;
381 void NPatchVisual::OnInitialize()
383 // Get basic geometry and shader
384 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
385 Shader shader = mImageVisualShaderFactory.GetShader(mFactoryCache, false, true, false);
387 mImpl->mRenderer = Renderer::New(geometry, shader);
389 //Register transform properties
390 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
393 Geometry NPatchVisual::CreateGeometry()
396 const NPatchData* data;
397 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
399 if( data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1 )
401 if( DALI_UNLIKELY( mBorderOnly ) )
403 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
407 if( data->GetRenderingMap() )
409 uint32_t elementCount[2];
410 geometry = RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), Uint16Pair(3, 3), elementCount );
411 if( mImpl->mRenderer )
413 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
418 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
422 else if( data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
424 Uint16Pair gridSize( 2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1 );
425 if( !data->GetRenderingMap() )
427 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
431 uint32_t elementCount[2];
432 geometry = !mBorderOnly ?
433 RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount ) : CreateBorderGeometry(gridSize );
434 if( mImpl->mRenderer )
436 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
443 // no N patch data so use default geometry
444 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
449 Shader NPatchVisual::CreateShader()
452 const NPatchData* data;
453 // 0 is either no data (load failed?) or no stretch regions on image
454 // for both cases we use the default shader
455 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
456 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
458 auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
459 : SHADER_NPATCH_VISUAL_SHADER_FRAG;
460 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
461 : VisualFactoryCache::NINE_PATCH_SHADER;
463 // ask loader for the regions
464 if( mLoader.GetNPatchData( mId, data ) )
466 xStretchCount = data->GetStretchPixelsX().Count();
467 yStretchCount = data->GetStretchPixelsY().Count();
470 if( DALI_LIKELY( !mImpl->mCustomShader ) )
472 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
473 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
475 shader = mFactoryCache.GetShader( shaderType );
476 if( DALI_UNLIKELY( !shader ) )
478 shader = Shader::New( SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader );
479 // Only cache vanilla 9 patch shaders
480 mFactoryCache.SaveShader( shaderType, shader );
483 else if( xStretchCount > 0 || yStretchCount > 0)
485 std::stringstream vertexShader;
486 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
487 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
488 << SHADER_NPATCH_VISUAL_SHADER_VERT;
490 shader = Shader::New( vertexShader.str(), fragmentShader );
495 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
497 if( !mImpl->mCustomShader->mFragmentShader.empty() )
499 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
501 hints = mImpl->mCustomShader->mHints;
503 /* Apply Custom Vertex Shader only if image is 9-patch */
504 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
505 ( xStretchCount == 0 && yStretchCount == 0 ) )
507 const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
509 if( !mImpl->mCustomShader->mVertexShader.empty() )
511 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
513 shader = Shader::New( vertexShader, fragmentShader, hints );
515 else if( xStretchCount > 0 || yStretchCount > 0)
517 std::stringstream vertexShader;
518 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
519 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
520 << SHADER_NPATCH_VISUAL_SHADER_VERT;
522 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
529 void NPatchVisual::ApplyTextureAndUniforms()
531 const NPatchData* data;
532 TextureSet textureSet;
534 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
536 textureSet = data->GetTextures();
538 if( data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1 )
540 //special case for 9 patch
541 Uint16Pair stretchX = data->GetStretchPixelsX()[ 0 ];
542 Uint16Pair stretchY = data->GetStretchPixelsY()[ 0 ];
544 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
545 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
547 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
548 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
549 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight ) );
550 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
554 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
555 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
557 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth() );
558 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight() );
563 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
564 textureSet = TextureSet::New();
566 Texture croppedImage = mFactoryCache.GetBrokenVisualImage();
567 textureSet.SetTexture( 0u, croppedImage );
568 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
569 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
570 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
571 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
574 if( mAuxiliaryPixelBuffer )
576 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
577 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
578 // on GL interpolation alone.
579 if( mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() &&
580 mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight() )
582 mAuxiliaryPixelBuffer.Resize( data->GetCroppedWidth(), data->GetCroppedHeight() );
585 // Note, this resets mAuxiliaryPixelBuffer handle
586 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
588 auto texture = Texture::New( TextureType::TEXTURE_2D,
589 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
590 auxiliaryPixelData.GetHeight() );
591 texture.Upload( auxiliaryPixelData );
592 textureSet.SetTexture( 1, texture );
593 mImpl->mRenderer.RegisterProperty( DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
594 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
596 mImpl->mRenderer.SetTextures( textureSet );
598 // Register transform properties
599 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
602 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
604 Geometry geometry = mFactoryCache.GetGeometry( subType );
607 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
609 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
611 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
613 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
615 mFactoryCache.SaveGeometry( subType, geometry );
620 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
622 uint16_t gridWidth = gridSize.GetWidth();
623 uint16_t gridHeight = gridSize.GetHeight();
626 Vector< Vector2 > vertices;
627 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
629 for( int y = 0; y < gridHeight + 1; ++y )
631 for( int x = 0; x < gridWidth + 1; ++x )
633 AddVertex( vertices, x, y );
638 Vector< unsigned short > indices;
639 indices.Reserve( gridWidth * gridHeight * 6 );
641 unsigned int rowIdx = 0;
642 unsigned int nextRowIdx = gridWidth + 1;
643 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
645 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
647 AddQuadIndices( indices, rowIdx, nextRowIdx );
651 return GenerateGeometry( vertices, indices );
654 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
656 uint16_t gridWidth = gridSize.GetWidth();
657 uint16_t gridHeight = gridSize.GetHeight();
660 Vector< Vector2 > vertices;
661 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
667 for( int x = 0; x < gridWidth + 1; ++x )
669 AddVertex( vertices, x, y );
673 for(; y < gridHeight - 1; ++y)
676 AddVertex( vertices, 0, y );
677 AddVertex( vertices, 1, y );
680 AddVertex( vertices, gridWidth - 1, y );
681 AddVertex( vertices, gridWidth, y );
685 for(; y < gridHeight + 1; ++y)
687 for( int x = 0; x < gridWidth + 1; ++x )
689 AddVertex( vertices, x, y );
694 Vector< unsigned short > indices;
695 indices.Reserve( gridWidth * gridHeight * 6 );
698 unsigned int rowIdx = 0 ;
699 unsigned int nextRowIdx = gridWidth + 1;
700 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
702 AddQuadIndices( indices, rowIdx, nextRowIdx );
707 rowIdx = gridWidth + 1;
708 nextRowIdx = ( gridWidth + 1 ) * 2;
710 unsigned increment = gridWidth - 1;
715 AddQuadIndices( indices, rowIdx, nextRowIdx );
717 rowIdx = gridWidth * 2;
718 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
720 AddQuadIndices( indices, rowIdx, nextRowIdx );
723 rowIdx = nextRowIdx - 2;
724 nextRowIdx = rowIdx + 4;
725 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
727 AddQuadIndices( indices, rowIdx, nextRowIdx );
732 AddQuadIndices( indices, rowIdx, nextRowIdx );
735 nextRowIdx += gridWidth - 1;
737 AddQuadIndices( indices, rowIdx, nextRowIdx );
741 rowIdx = nextRowIdx - gridWidth + 1;
742 nextRowIdx = rowIdx + gridWidth + 1;
743 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
745 AddQuadIndices( indices, rowIdx, nextRowIdx );
748 return GenerateGeometry( vertices, indices );
751 void NPatchVisual::SetResource()
753 const NPatchData* data;
754 if( mImpl->mRenderer && mLoader.GetNPatchData( mId, data ) )
756 Geometry geometry = CreateGeometry();
757 Shader shader = CreateShader();
759 mImpl->mRenderer.SetGeometry( geometry );
760 mImpl->mRenderer.SetShader( shader );
762 Actor actor = mPlacementActor.GetHandle();
765 ApplyTextureAndUniforms();
766 actor.AddRenderer( mImpl->mRenderer );
767 mPlacementActor.Reset();
769 // npatch loaded and ready to display
770 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
775 void NPatchVisual::UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied )
777 EnablePreMultipliedAlpha( preMultiplied );
780 // Image loaded and ready to display
781 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
784 if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
790 void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
792 if( loadSuccess && url.GetUrl() == mAuxiliaryUrl.GetUrl() )
794 mAuxiliaryPixelBuffer = pixelBuffer;
799 // Image loaded and ready to display
800 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
804 } // namespace Internal
806 } // namespace Toolkit