X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fnpatch%2Fnpatch-visual.cpp;h=b5c9b51ae9c219d958a68b32b6ceaf252f49b243;hb=88af55c0bf70653347b6918a83bc9a9ee5972e24;hp=4325dea15f2f73464f500eada3aac36f0a4e5e8e;hpb=3c90f2e19f0526a936f5664dc446addbaa25ade8;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp b/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp index 4325dea..b5c9b51 100644 --- a/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp +++ b/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,16 +20,16 @@ // EXTERNAL INCLUDES #include +#include #include #include -#include // INTERNAL INCLUDES +#include #include #include #include #include -#include #include #include #include @@ -44,6 +44,10 @@ namespace Toolkit { namespace Internal { +namespace +{ +const int CUSTOM_PROPERTY_COUNT(5); // fixed(3),stretch,aux +} /////////////////NPatchVisual//////////////// @@ -86,6 +90,12 @@ void NPatchVisual::LoadImages() // Load the auxiliary image auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer(mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, this, true, preMultiplyOnLoading); + + // If synchronousLoading is true, we can check the auxiliaryResource's status now. + if(synchronousLoading) + { + mAuxiliaryResourceStatus = mAuxiliaryPixelBuffer ? Toolkit::Visual::ResourceStatus::READY : Toolkit::Visual::ResourceStatus::FAILED; + } } } @@ -188,29 +198,17 @@ void NPatchVisual::DoSetOnScene(Actor& actor) // load when first go on stage LoadImages(); + // Set mPlacementActor now, because some case, LoadImages can use this information in LoadComplete API. + // at this case, we try to SetResouce to mPlaceActor twice. so, we should avoid that case. + mPlacementActor = actor; + const NPatchData* data; - if(mLoader.GetNPatchData(mId, data)) + if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING) { - Geometry geometry = CreateGeometry(); - Shader shader = CreateShader(); - - mImpl->mRenderer.SetGeometry(geometry); - mImpl->mRenderer.SetShader(shader); - - mPlacementActor = actor; - if(data->GetLoadingState() != NPatchData::LoadingState::LOADING) + // If mAuxiliaryUrl need to be loaded, we should wait it until LoadComplete called. + if(!mAuxiliaryUrl.IsValid() || mAuxiliaryResourceStatus != Toolkit::Visual::ResourceStatus::PREPARING) { - if(RenderingAddOn::Get().IsValid()) - { - RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap()); - } - - ApplyTextureAndUniforms(); - actor.AddRenderer(mImpl->mRenderer); - mPlacementActor.Reset(); - - // npatch loaded and ready to display - ResourceReady(Toolkit::Visual::ResourceStatus::READY); + SetResource(); } } } @@ -232,7 +230,7 @@ void NPatchVisual::OnSetTransform() { if(mImpl->mRenderer) { - mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); + mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } } @@ -271,6 +269,7 @@ NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFa mImageUrl(), mAuxiliaryUrl(), mId(NPatchData::INVALID_NPATCH_DATA_ID), + mAuxiliaryResourceStatus(Toolkit::Visual::ResourceStatus::PREPARING), mBorderOnly(false), mBorder(), mAuxiliaryImageAlpha(0.0f), @@ -294,13 +293,13 @@ void NPatchVisual::OnInitialize() Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY); Shader shader = mImageVisualShaderFactory.GetShader( mFactoryCache, - ImageVisualShaderFeature::FeatureBuilder() - ); + ImageVisualShaderFeature::FeatureBuilder()); - mImpl->mRenderer = Renderer::New(geometry, shader); + mImpl->mRenderer = VisualRenderer::New(geometry, shader); + mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT); //Register transform properties - mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); + mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } Geometry NPatchVisual::CreateGeometry() @@ -337,12 +336,12 @@ Geometry NPatchVisual::CreateGeometry() Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1); if(!data->GetRenderingMap()) { - geometry = !mBorderOnly ? NPatchHelper::CreateGridGeometry(gridSize) : NPatchHelper::CreateBorderGeometry(gridSize); + geometry = !mBorderOnly ? NPatchHelper::CreateGridGeometry(gridSize) : NPatchHelper::CreateBorderGeometry(gridSize); } else { uint32_t elementCount[2]; - geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : NPatchHelper::CreateBorderGeometry(gridSize); + geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : NPatchHelper::CreateBorderGeometry(gridSize); if(mImpl->mRenderer) { RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap()); @@ -369,8 +368,8 @@ Shader NPatchVisual::CreateShader() auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG : SHADER_NPATCH_VISUAL_SHADER_FRAG; - auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER - : VisualFactoryCache::NINE_PATCH_SHADER; + auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER + : VisualFactoryCache::NINE_PATCH_SHADER; // ask loader for the regions if(mLoader.GetNPatchData(mId, data)) @@ -447,55 +446,55 @@ void NPatchVisual::ApplyTextureAndUniforms() { textureSet = data->GetTextures(); NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data); + + if(mAuxiliaryPixelBuffer) + { + // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the + // same size as the unstretched NPatch. This will give slightly higher quality results than just relying + // on GL interpolation alone. + if(mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() && + mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight()) + { + mAuxiliaryPixelBuffer.Resize(data->GetCroppedWidth(), data->GetCroppedHeight()); + } + + // Note, this resets mAuxiliaryPixelBuffer handle + auto auxiliaryPixelData = Devel::PixelBuffer::Convert(mAuxiliaryPixelBuffer); + + auto texture = Texture::New(TextureType::TEXTURE_2D, + auxiliaryPixelData.GetPixelFormat(), + auxiliaryPixelData.GetWidth(), + auxiliaryPixelData.GetHeight()); + texture.Upload(auxiliaryPixelData); + + // TODO : This code exist due to the texture cache manager hold TextureSet, not Texture. + // If we call textureSet.SetTexture(1, texture) directly, the cached TextureSet also be changed. + // We should make pass utc-Dali-VisualFactory.cpp UtcDaliNPatchVisualAuxiliaryImage02(). + TextureSet tempTextureSet = TextureSet::New(); + tempTextureSet.SetTexture(0, textureSet.GetTexture(0)); + tempTextureSet.SetTexture(1, texture); + textureSet = tempTextureSet; + + mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, + AUXILIARY_IMAGE_ALPHA_NAME, + mAuxiliaryImageAlpha); + } + mImpl->mRenderer.SetTextures(textureSet); } else { DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str()); - textureSet = TextureSet::New(); - - Actor actor = mPlacementActor.GetHandle(); + Actor actor = mPlacementActor.GetHandle(); Vector2 imageSize = Vector2::ZERO; if(actor) { imageSize = actor.GetProperty(Actor::Property::SIZE).Get(); } - mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize); - Texture croppedImage = mImpl->mRenderer.GetTextures().GetTexture(0); - textureSet.SetTexture(0u, croppedImage); - mImpl->mRenderer.RegisterProperty("uFixed[0]", Vector2::ZERO); - mImpl->mRenderer.RegisterProperty("uFixed[1]", Vector2::ZERO); - mImpl->mRenderer.RegisterProperty("uFixed[2]", Vector2::ZERO); - mImpl->mRenderer.RegisterProperty("uStretchTotal", Vector2(croppedImage.GetWidth(), croppedImage.GetHeight())); - } - - if(mAuxiliaryPixelBuffer) - { - // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the - // same size as the unstretched NPatch. This will give slightly higher quality results than just relying - // on GL interpolation alone. - if(mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() && - mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight()) - { - mAuxiliaryPixelBuffer.Resize(data->GetCroppedWidth(), data->GetCroppedHeight()); - } - - // Note, this resets mAuxiliaryPixelBuffer handle - auto auxiliaryPixelData = Devel::PixelBuffer::Convert(mAuxiliaryPixelBuffer); - - auto texture = Texture::New(TextureType::TEXTURE_2D, - auxiliaryPixelData.GetPixelFormat(), - auxiliaryPixelData.GetWidth(), - auxiliaryPixelData.GetHeight()); - texture.Upload(auxiliaryPixelData); - textureSet.SetTexture(1, texture); - mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, - AUXILIARY_IMAGE_ALPHA_NAME, - mAuxiliaryImageAlpha); + mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize, false); } - mImpl->mRenderer.SetTextures(textureSet); // Register transform properties - mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); + mImpl->mTransform.SetUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType subType) @@ -527,14 +526,25 @@ void NPatchVisual::SetResource() mImpl->mRenderer.SetGeometry(geometry); mImpl->mRenderer.SetShader(shader); + if(RenderingAddOn::Get().IsValid()) + { + RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap()); + } Actor actor = mPlacementActor.GetHandle(); if(actor) { ApplyTextureAndUniforms(); actor.AddRenderer(mImpl->mRenderer); mPlacementActor.Reset(); + } - // npatch loaded and ready to display + // npatch loaded and ready to display + if(data->GetLoadingState() != NPatchData::LoadingState::LOAD_COMPLETE) + { + ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + } + else + { ResourceReady(Toolkit::Visual::ResourceStatus::READY); } } @@ -542,31 +552,42 @@ void NPatchVisual::SetResource() void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInformation) { - if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE) + if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE) // For the Url. { - EnablePreMultipliedAlpha(textureInformation.preMultiplied); - if(!loadSuccess) + if(textureInformation.textureId != TextureManager::INVALID_TEXTURE_ID) { - // Image loaded and ready to display - ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + if(mId == NPatchData::INVALID_NPATCH_DATA_ID) + { + // Special case when mLoader.Load call LoadComplete function before mId setup. + // We can overwrite mId. + mId = static_cast(textureInformation.textureId); + } } - - if(mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid()) + if(loadSuccess) { - SetResource(); + EnablePreMultipliedAlpha(textureInformation.preMultiplied); } } - else // for the ReturnType::PIXEL_BUFFER + else // For the AuxiliaryUrl : ReturnType::PIXEL_BUFFER { if(loadSuccess && textureInformation.url == mAuxiliaryUrl.GetUrl()) { - mAuxiliaryPixelBuffer = textureInformation.pixelBuffer; - SetResource(); + mAuxiliaryPixelBuffer = textureInformation.pixelBuffer; + mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::READY; } else { - // Image loaded and ready to display - ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + mAuxiliaryResourceStatus = Toolkit::Visual::ResourceStatus::FAILED; + } + } + // If auxiliaryUrl didn't required OR auxiliaryUrl load done. + if(!mAuxiliaryUrl.IsValid() || mAuxiliaryResourceStatus != Toolkit::Visual::ResourceStatus::PREPARING) + { + const NPatchData* data; + // and.. If Url loading done. + if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING) + { + SetResource(); } } }