2 * Copyright (c) 2020 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/public-api/visuals/visual-properties.h>
29 #include <dali-toolkit/internal/visuals/npatch-loader.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>
35 #include <dali-toolkit/internal/visuals/rendering-addon.h>
36 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
51 * @brief Creates the geometry formed from the vertices and indices
53 * @param[in] vertices The vertices to generate the geometry from
54 * @param[in] indices The indices to generate the geometry from
55 * @return The geometry formed from the vertices and indices
57 Geometry GenerateGeometry( const Vector< Vector2 >& vertices, const Vector< unsigned short >& indices )
59 Property::Map vertexFormat;
60 vertexFormat[ "aPosition" ] = Property::VECTOR2;
61 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
62 if( vertices.Size() > 0 )
64 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
67 // Create the geometry object
68 Geometry geometry = Geometry::New();
69 geometry.AddVertexBuffer( vertexBuffer );
70 if( indices.Size() > 0 )
72 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
80 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
82 * @param[out] indices The indices to add to
83 * @param[in] rowIdx The row index to start the quad
84 * @param[in] nextRowIdx The index to the next row
86 void AddQuadIndices( Vector< unsigned short >& indices, unsigned int rowIdx, unsigned int nextRowIdx )
88 indices.PushBack( rowIdx );
89 indices.PushBack( nextRowIdx + 1 );
90 indices.PushBack( rowIdx + 1 );
92 indices.PushBack( rowIdx );
93 indices.PushBack( nextRowIdx );
94 indices.PushBack( nextRowIdx + 1 );
97 void AddVertex( Vector< Vector2 >& vertices, unsigned int x, unsigned int y )
99 vertices.PushBack( Vector2( x, y ) );
102 void RegisterStretchProperties( Renderer& renderer, const char * uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent)
104 uint16_t prevEnd = 0;
105 uint16_t prevFix = 0;
106 uint16_t prevStretch = 0;
108 for( NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i )
110 uint16_t start = it->GetX();
111 uint16_t end = it->GetY();
113 uint16_t fix = prevFix + start - prevEnd;
114 uint16_t stretch = prevStretch + end - start;
116 std::stringstream uniform;
117 uniform << uniformName << "[" << i << "]";
118 renderer.RegisterProperty( uniform.str(), Vector2( fix, stretch ) );
122 prevStretch = stretch;
126 prevFix += imageExtent - prevEnd;
127 std::stringstream uniform;
128 uniform << uniformName << "[" << i << "]";
129 renderer.RegisterProperty( uniform.str(), Vector2( prevFix, prevStretch ) );
133 } //unnamed namespace
135 /////////////////NPatchVisual////////////////
137 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties )
139 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
140 nPatchVisual->mImageUrl = imageUrl;
141 nPatchVisual->SetProperties( properties );
146 NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl )
148 NPatchVisualPtr nPatchVisual( new NPatchVisual( factoryCache ) );
149 nPatchVisual->mImageUrl = imageUrl;
154 void NPatchVisual::LoadImages()
156 TextureManager& textureManager = mFactoryCache.GetTextureManager();
157 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
159 if( mId == NPatchData::INVALID_NPATCH_DATA_ID && mImageUrl.IsLocalResource() )
161 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
162 mId = mLoader.Load( textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading );
164 const NPatchData* data;
165 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
167 EnablePreMultipliedAlpha( data->IsPreMultiplied() );
171 if( !mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource() )
173 // Load the auxiliary image
174 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
175 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer( mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT,
176 SamplingMode::BOX_THEN_LINEAR, synchronousLoading,
177 this, true, preMultiplyOnLoading );
181 void NPatchVisual::GetNaturalSize( Vector2& naturalSize )
186 // load now if not already loaded
187 const NPatchData* data;
188 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() != NPatchData::LoadingState::LOADING )
190 naturalSize.x = data->GetCroppedWidth();
191 naturalSize.y = data->GetCroppedHeight();
195 if( mImageUrl.IsValid() )
197 ImageDimensions dimensions = Dali::GetOriginalImageSize( mImageUrl.GetUrl() );
198 if( dimensions != ImageDimensions( 0, 0 ) )
200 naturalSize.x = dimensions.GetWidth();
201 naturalSize.y = dimensions.GetHeight();
206 if( mAuxiliaryPixelBuffer )
208 naturalSize.x = std::max( naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()) );
209 naturalSize.y = std::max( naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()) );
213 void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
215 // URL is already passed in via constructor
217 Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
218 if( borderOnlyValue )
220 borderOnlyValue->Get( mBorderOnly );
223 Property::Value* borderValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER, BORDER );
224 if( borderValue && ! borderValue->Get( mBorder ) ) // If value exists and is rect, just set mBorder
226 // Not a rect so try vector4
228 if( borderValue->Get( border ) )
230 mBorder.left = static_cast< int >( border.x );
231 mBorder.right = static_cast< int >( border.y );
232 mBorder.bottom = static_cast< int >( border.z );
233 mBorder.top = static_cast< int >( border.w );
237 Property::Value* auxImage = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME );
241 if( auxImage->Get( url ) )
247 Property::Value* auxImageAlpha = propertyMap.Find( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME );
250 auxImageAlpha->Get( mAuxiliaryImageAlpha );
253 Property::Value* synchronousLoading = propertyMap.Find( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING );
254 if( synchronousLoading )
257 synchronousLoading->Get( sync );
260 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
264 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
268 Property::Value* releasePolicy = propertyMap.Find( Toolkit::ImageVisual::Property::RELEASE_POLICY, RELEASE_POLICY_NAME );
271 releasePolicy->Get( mReleasePolicy );
275 void NPatchVisual::DoSetOnScene( Actor& actor )
277 // load when first go on stage
280 const NPatchData* data;
281 if( mLoader.GetNPatchData( mId, data ) )
283 Geometry geometry = CreateGeometry();
284 Shader shader = CreateShader();
286 mImpl->mRenderer = Renderer::New( geometry, shader );
288 mPlacementActor = actor;
289 if( data->GetLoadingState() != NPatchData::LoadingState::LOADING )
291 if( RenderingAddOn::Get().IsValid() )
293 RenderingAddOn::Get().SubmitRenderTask( mImpl->mRenderer, data->GetRenderingMap() );
296 ApplyTextureAndUniforms();
297 actor.AddRenderer( mImpl->mRenderer );
298 mPlacementActor.Reset();
300 // npatch loaded and ready to display
301 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
306 void NPatchVisual::DoSetOffScene( Actor& actor )
308 if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
310 mLoader.Remove(mId, this);
311 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
312 mId = NPatchData::INVALID_NPATCH_DATA_ID;
315 actor.RemoveRenderer( mImpl->mRenderer );
316 mImpl->mRenderer.Reset();
317 mPlacementActor.Reset();
320 void NPatchVisual::OnSetTransform()
322 if( mImpl->mRenderer )
324 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
328 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
331 bool sync = IsSynchronousLoadingRequired();
332 map.Insert( Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync );
333 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH );
334 map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl() );
335 map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
336 map.Insert( Toolkit::ImageVisual::Property::BORDER, mBorder );
337 map.Insert( Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy );
339 if( mAuxiliaryUrl.IsValid() )
341 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
342 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
346 void NPatchVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
348 if( mAuxiliaryUrl.IsValid() )
350 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl() );
351 map.Insert( Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha );
355 NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
356 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH ),
358 mLoader( factoryCache.GetNPatchLoader() ),
361 mId(NPatchData::INVALID_NPATCH_DATA_ID),
362 mBorderOnly( false ),
364 mAuxiliaryImageAlpha( 0.0f ),
365 mReleasePolicy( Toolkit::ImageVisual::ReleasePolicy::DETACHED )
367 EnablePreMultipliedAlpha( mFactoryCache.GetPreMultiplyOnLoad() );
370 NPatchVisual::~NPatchVisual()
372 if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && ( mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER ))
374 mLoader.Remove(mId, this);
375 mId = NPatchData::INVALID_NPATCH_DATA_ID;
379 Geometry NPatchVisual::CreateGeometry()
382 const NPatchData* data;
383 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
385 if( data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1 )
387 if( DALI_UNLIKELY( mBorderOnly ) )
389 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY );
393 if( data->GetRenderingMap() )
395 uint32_t elementCount[2];
396 geometry = RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), Uint16Pair(3, 3), elementCount );
397 if( mImpl->mRenderer )
399 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
404 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
408 else if( data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
410 Uint16Pair gridSize( 2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1 );
411 if( !data->GetRenderingMap() )
413 geometry = !mBorderOnly ? CreateGridGeometry( gridSize ) : CreateBorderGeometry( gridSize );
417 uint32_t elementCount[2];
418 geometry = !mBorderOnly ?
419 RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount ) : CreateBorderGeometry(gridSize );
420 if( mImpl->mRenderer )
422 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
429 // no N patch data so use default geometry
430 geometry = GetNinePatchGeometry( VisualFactoryCache::NINE_PATCH_GEOMETRY );
435 Shader NPatchVisual::CreateShader()
438 const NPatchData* data;
439 // 0 is either no data (load failed?) or no stretch regions on image
440 // for both cases we use the default shader
441 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
442 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
444 auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
445 : SHADER_NPATCH_VISUAL_SHADER_FRAG;
446 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
447 : VisualFactoryCache::NINE_PATCH_SHADER;
449 // ask loader for the regions
450 if( mLoader.GetNPatchData( mId, data ) )
452 xStretchCount = data->GetStretchPixelsX().Count();
453 yStretchCount = data->GetStretchPixelsY().Count();
456 if( DALI_LIKELY( !mImpl->mCustomShader ) )
458 if( DALI_LIKELY( ( xStretchCount == 1 && yStretchCount == 1 ) ||
459 ( xStretchCount == 0 && yStretchCount == 0 ) ) )
461 shader = mFactoryCache.GetShader( shaderType );
462 if( DALI_UNLIKELY( !shader ) )
464 shader = Shader::New( SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader );
465 // Only cache vanilla 9 patch shaders
466 mFactoryCache.SaveShader( shaderType, shader );
469 else if( xStretchCount > 0 || yStretchCount > 0)
471 std::stringstream vertexShader;
472 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
473 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
474 << SHADER_NPATCH_VISUAL_SHADER_VERT;
476 shader = Shader::New( vertexShader.str(), fragmentShader );
481 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
483 if( !mImpl->mCustomShader->mFragmentShader.empty() )
485 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
487 hints = mImpl->mCustomShader->mHints;
489 /* Apply Custom Vertex Shader only if image is 9-patch */
490 if( ( xStretchCount == 1 && yStretchCount == 1 ) ||
491 ( xStretchCount == 0 && yStretchCount == 0 ) )
493 const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
495 if( !mImpl->mCustomShader->mVertexShader.empty() )
497 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
499 shader = Shader::New( vertexShader, fragmentShader, hints );
501 else if( xStretchCount > 0 || yStretchCount > 0)
503 std::stringstream vertexShader;
504 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
505 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
506 << SHADER_NPATCH_VISUAL_SHADER_VERT;
508 shader = Shader::New( vertexShader.str(), fragmentShader, hints );
515 void NPatchVisual::ApplyTextureAndUniforms()
517 const NPatchData* data;
518 TextureSet textureSet;
520 if( mLoader.GetNPatchData( mId, data ) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE )
522 textureSet = data->GetTextures();
524 if( data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1 )
526 //special case for 9 patch
527 Uint16Pair stretchX = data->GetStretchPixelsX()[ 0 ];
528 Uint16Pair stretchY = data->GetStretchPixelsY()[ 0 ];
530 uint16_t stretchWidth = ( stretchX.GetY() >= stretchX.GetX() ) ? stretchX.GetY() - stretchX.GetX() : 0;
531 uint16_t stretchHeight = ( stretchY.GetY() >= stretchY.GetX() ) ? stretchY.GetY() - stretchY.GetX() : 0;
533 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
534 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2( stretchX.GetX(), stretchY.GetX()) );
535 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2( data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight ) );
536 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( stretchWidth, stretchHeight ) );
540 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsX[0]", Vector2::ZERO );
541 mImpl->mRenderer.RegisterProperty( "uNinePatchFactorsY[0]", Vector2::ZERO );
543 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth() );
544 RegisterStretchProperties( mImpl->mRenderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight() );
549 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str() );
550 textureSet = TextureSet::New();
552 Texture croppedImage = mFactoryCache.GetBrokenVisualImage();
553 textureSet.SetTexture( 0u, croppedImage );
554 mImpl->mRenderer.RegisterProperty( "uFixed[0]", Vector2::ZERO );
555 mImpl->mRenderer.RegisterProperty( "uFixed[1]", Vector2::ZERO );
556 mImpl->mRenderer.RegisterProperty( "uFixed[2]", Vector2::ZERO );
557 mImpl->mRenderer.RegisterProperty( "uStretchTotal", Vector2( croppedImage.GetWidth(), croppedImage.GetHeight() ) );
560 if( mAuxiliaryPixelBuffer )
562 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
563 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
564 // on GL interpolation alone.
565 if( mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() &&
566 mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight() )
568 mAuxiliaryPixelBuffer.Resize( data->GetCroppedWidth(), data->GetCroppedHeight() );
571 // Note, this resets mAuxiliaryPixelBuffer handle
572 auto auxiliaryPixelData = Devel::PixelBuffer::Convert( mAuxiliaryPixelBuffer );
574 auto texture = Texture::New( TextureType::TEXTURE_2D,
575 auxiliaryPixelData.GetPixelFormat(), auxiliaryPixelData.GetWidth(),
576 auxiliaryPixelData.GetHeight() );
577 texture.Upload( auxiliaryPixelData );
578 textureSet.SetTexture( 1, texture );
579 mImpl->mRenderer.RegisterProperty( DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
580 AUXILIARY_IMAGE_ALPHA_NAME, mAuxiliaryImageAlpha );
582 mImpl->mRenderer.SetTextures( textureSet );
584 // Register transform properties
585 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
588 Geometry NPatchVisual::GetNinePatchGeometry( VisualFactoryCache::GeometryType subType )
590 Geometry geometry = mFactoryCache.GetGeometry( subType );
593 if( DALI_LIKELY( VisualFactoryCache::NINE_PATCH_GEOMETRY == subType ) )
595 geometry = CreateGridGeometry( Uint16Pair( 3, 3 ) );
597 else if( VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType )
599 geometry = CreateBorderGeometry( Uint16Pair( 3, 3 ) );
601 mFactoryCache.SaveGeometry( subType, geometry );
606 Geometry NPatchVisual::CreateGridGeometry( Uint16Pair gridSize )
608 uint16_t gridWidth = gridSize.GetWidth();
609 uint16_t gridHeight = gridSize.GetHeight();
612 Vector< Vector2 > vertices;
613 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
615 for( int y = 0; y < gridHeight + 1; ++y )
617 for( int x = 0; x < gridWidth + 1; ++x )
619 AddVertex( vertices, x, y );
624 Vector< unsigned short > indices;
625 indices.Reserve( gridWidth * gridHeight * 6 );
627 unsigned int rowIdx = 0;
628 unsigned int nextRowIdx = gridWidth + 1;
629 for( int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx )
631 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
633 AddQuadIndices( indices, rowIdx, nextRowIdx );
637 return GenerateGeometry( vertices, indices );
640 Geometry NPatchVisual::CreateBorderGeometry( Uint16Pair gridSize )
642 uint16_t gridWidth = gridSize.GetWidth();
643 uint16_t gridHeight = gridSize.GetHeight();
646 Vector< Vector2 > vertices;
647 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
653 for( int x = 0; x < gridWidth + 1; ++x )
655 AddVertex( vertices, x, y );
659 for(; y < gridHeight - 1; ++y)
662 AddVertex( vertices, 0, y );
663 AddVertex( vertices, 1, y );
666 AddVertex( vertices, gridWidth - 1, y );
667 AddVertex( vertices, gridWidth, y );
671 for(; y < gridHeight + 1; ++y)
673 for( int x = 0; x < gridWidth + 1; ++x )
675 AddVertex( vertices, x, y );
680 Vector< unsigned short > indices;
681 indices.Reserve( gridWidth * gridHeight * 6 );
684 unsigned int rowIdx = 0 ;
685 unsigned int nextRowIdx = gridWidth + 1;
686 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
688 AddQuadIndices( indices, rowIdx, nextRowIdx );
693 rowIdx = gridWidth + 1;
694 nextRowIdx = ( gridWidth + 1 ) * 2;
696 unsigned increment = gridWidth - 1;
701 AddQuadIndices( indices, rowIdx, nextRowIdx );
703 rowIdx = gridWidth * 2;
704 nextRowIdx = ( gridWidth + 1 ) * 2 + 2;
706 AddQuadIndices( indices, rowIdx, nextRowIdx );
709 rowIdx = nextRowIdx - 2;
710 nextRowIdx = rowIdx + 4;
711 for(int y = 2; y < 2*(gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
713 AddQuadIndices( indices, rowIdx, nextRowIdx );
718 AddQuadIndices( indices, rowIdx, nextRowIdx );
721 nextRowIdx += gridWidth - 1;
723 AddQuadIndices( indices, rowIdx, nextRowIdx );
727 rowIdx = nextRowIdx - gridWidth + 1;
728 nextRowIdx = rowIdx + gridWidth + 1;
729 for( int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx )
731 AddQuadIndices( indices, rowIdx, nextRowIdx );
734 return GenerateGeometry( vertices, indices );
737 void NPatchVisual::SetResource()
739 const NPatchData* data;
740 if( mImpl->mRenderer && mLoader.GetNPatchData( mId, data ) )
742 Geometry geometry = CreateGeometry();
743 Shader shader = CreateShader();
745 mImpl->mRenderer.SetGeometry( geometry );
746 mImpl->mRenderer.SetShader( shader );
748 Actor actor = mPlacementActor.GetHandle();
751 ApplyTextureAndUniforms();
752 actor.AddRenderer( mImpl->mRenderer );
753 mPlacementActor.Reset();
755 // npatch loaded and ready to display
756 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
761 void NPatchVisual::UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied )
763 EnablePreMultipliedAlpha( preMultiplied );
766 // Image loaded and ready to display
767 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
770 if( mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid() )
776 void NPatchVisual::LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied )
778 if( loadSuccess && url.GetUrl() == mAuxiliaryUrl.GetUrl() )
780 mAuxiliaryPixelBuffer = pixelBuffer;
785 // Image loaded and ready to display
786 ResourceReady( Toolkit::Visual::ResourceStatus::FAILED );
790 } // namespace Internal
792 } // namespace Toolkit