From 3c90f2e19f0526a936f5664dc446addbaa25ade8 Mon Sep 17 00:00:00 2001 From: tscholb Date: Tue, 23 Nov 2021 16:10:10 +0900 Subject: [PATCH] Refactorying code related to broken image - Use default broken image when the url of custom broken is invalid - Create a npatch-helper for merging npatch code Change-Id: I21f33392f864996e8419715b40e66a5d16eccb6a --- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 53 ++++ dali-toolkit/devel-api/file.list | 2 + dali-toolkit/devel-api/utility/npatch-helper.cpp | 286 +++++++++++++++++++++ dali-toolkit/devel-api/utility/npatch-helper.h | 97 +++++++ .../internal/visuals/npatch/npatch-visual.cpp | 249 +----------------- .../internal/visuals/visual-factory-cache.cpp | 209 ++++----------- .../internal/visuals/visual-factory-cache.h | 22 +- .../internal/visuals/visual-factory-impl.cpp | 18 +- 8 files changed, 497 insertions(+), 439 deletions(-) create mode 100644 dali-toolkit/devel-api/utility/npatch-helper.cpp create mode 100644 dali-toolkit/devel-api/utility/npatch-helper.h diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index 96f4536..7c0e975 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -3001,6 +3001,59 @@ int UtcDaliImageViewImageLoadFailure03(void) END_TEST; } +int UtcDaliImageViewImageLoadFailure04(void) +{ + ToolkitTestApplication application; + + ImageView imageView = ImageView::New("invalidUrl.png"); + imageView.SetProperty( Actor::Property::SIZE, Vector2( 100.f, 100.f ) ); + application.GetScene().Add( imageView ); + application.SendNotification(); + application.Render(16); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + + + Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get(); + DevelStyleManager::SetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::SMALL, TEST_BROKEN_IMAGE_S); + DevelStyleManager::SetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::NORMAL, "invalidBroken.png"); + DevelStyleManager::SetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::LARGE, TEST_BROKEN_IMAGE_L); + + ImageView imageView2 = ImageView::New("invalidUrl.png"); + imageView2.SetProperty( Actor::Property::SIZE, Vector2( 100.f, 100.f ) ); + application.GetScene().Add( imageView2 ); + + std::string brokenUrl; + brokenUrl = DevelStyleManager::GetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::SMALL); + DALI_TEST_EQUALS( TEST_BROKEN_IMAGE_S, brokenUrl, TEST_LOCATION); + + brokenUrl = DevelStyleManager::GetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::LARGE); + DALI_TEST_EQUALS( TEST_BROKEN_IMAGE_L, brokenUrl, TEST_LOCATION); + + application.SendNotification(); + application.Render(16); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + + + DevelStyleManager::SetBrokenImageUrl(styleManager, DevelStyleManager::BrokenImageType::NORMAL, "invalidBroken.9.png"); + + ImageView imageView3 = ImageView::New("invalidUrl.png"); + imageView3.SetProperty( Actor::Property::SIZE, Vector2( 100.f, 100.f ) ); + application.GetScene().Add( imageView3 ); + + application.SendNotification(); + application.Render(16); + + // loading started, this waits for the loader thread + DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION ); + + END_TEST; +} + + namespace { diff --git a/dali-toolkit/devel-api/file.list b/dali-toolkit/devel-api/file.list index cc5abef..0a000bc 100755 --- a/dali-toolkit/devel-api/file.list +++ b/dali-toolkit/devel-api/file.list @@ -67,6 +67,7 @@ SET( devel_api_src_files ${devel_api_src_dir}/transition-effects/cube-transition-fold-effect.cpp ${devel_api_src_dir}/transition-effects/cube-transition-wave-effect.cpp ${devel_api_src_dir}/utility/npatch-utilities.cpp + ${devel_api_src_dir}/utility/npatch-helper.cpp ${devel_api_src_dir}/visual-factory/transition-data.cpp ${devel_api_src_dir}/visual-factory/visual-factory.cpp ${devel_api_src_dir}/visual-factory/visual-base.cpp @@ -271,6 +272,7 @@ SET( devel_api_drag_and_drop_detector_header_files SET( devel_api_utility_header_files ${devel_api_src_dir}/utility/npatch-utilities.h + ${devel_api_src_dir}/utility/npatch-helper.h ) SET( SOURCES ${SOURCES} diff --git a/dali-toolkit/devel-api/utility/npatch-helper.cpp b/dali-toolkit/devel-api/utility/npatch-helper.cpp new file mode 100644 index 0000000..53c6d4d --- /dev/null +++ b/dali-toolkit/devel-api/utility/npatch-helper.cpp @@ -0,0 +1,286 @@ +/* +* Copyright (c) 2021 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +*/ + +// CLASS HEADER +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace NPatchHelper +{ +namespace +{ +/** + * @brief Creates the geometry formed from the vertices and indices + * + * @param[in] vertices The vertices to generate the geometry from + * @param[in] indices The indices to generate the geometry from + * @return The geometry formed from the vertices and indices + */ +Geometry GenerateGeometry(const Vector& vertices, const Vector& indices) +{ + Property::Map vertexFormat; + vertexFormat["aPosition"] = Property::VECTOR2; + VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat); + if(vertices.Size() > 0) + { + vertexBuffer.SetData(&vertices[0], vertices.Size()); + } + + // Create the geometry object + Geometry geometry = Geometry::New(); + geometry.AddVertexBuffer(vertexBuffer); + if(indices.Size() > 0) + { + geometry.SetIndexBuffer(&indices[0], indices.Size()); + } + + return geometry; +} + +/** + * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid + * + * @param[out] indices The indices to add to + * @param[in] rowIdx The row index to start the quad + * @param[in] nextRowIdx The index to the next row + */ +void AddQuadIndices(Vector& indices, unsigned int rowIdx, unsigned int nextRowIdx) +{ + indices.PushBack(rowIdx); + indices.PushBack(nextRowIdx + 1); + indices.PushBack(rowIdx + 1); + + indices.PushBack(rowIdx); + indices.PushBack(nextRowIdx); + indices.PushBack(nextRowIdx + 1); +} + +/** + * @brief Adds the vertices to create for npatch + * @param[out] vertices The vertices to add to + * @param[in] x The x value of vector + * @param[in] y The y value of vector + */ +void AddVertex(Vector& vertices, unsigned int x, unsigned int y) +{ + vertices.PushBack(Vector2(x, y)); +} + +} // unnamed namespace + +Geometry CreateGridGeometry(Uint16Pair gridSize) +{ + uint16_t gridWidth = gridSize.GetWidth(); + uint16_t gridHeight = gridSize.GetHeight(); + + // Create vertices + Vector vertices; + vertices.Reserve((gridWidth + 1) * (gridHeight + 1)); + + for(int y = 0; y < gridHeight + 1; ++y) + { + for(int x = 0; x < gridWidth + 1; ++x) + { + AddVertex(vertices, x, y); + } + } + + // Create indices + Vector indices; + indices.Reserve(gridWidth * gridHeight * 6); + + unsigned int rowIdx = 0; + unsigned int nextRowIdx = gridWidth + 1; + for(int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx) + { + for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) + { + AddQuadIndices(indices, rowIdx, nextRowIdx); + } + } + + return GenerateGeometry(vertices, indices); +} + +Geometry CreateBorderGeometry(Uint16Pair gridSize) +{ + uint16_t gridWidth = gridSize.GetWidth(); + uint16_t gridHeight = gridSize.GetHeight(); + + // Create vertices + Vector vertices; + vertices.Reserve((gridWidth + 1) * (gridHeight + 1)); + + //top + int y = 0; + for(; y < 2; ++y) + { + for(int x = 0; x < gridWidth + 1; ++x) + { + AddVertex(vertices, x, y); + } + } + + for(; y < gridHeight - 1; ++y) + { + //left + AddVertex(vertices, 0, y); + AddVertex(vertices, 1, y); + + //right + AddVertex(vertices, gridWidth - 1, y); + AddVertex(vertices, gridWidth, y); + } + + //bottom + for(; y < gridHeight + 1; ++y) + { + for(int x = 0; x < gridWidth + 1; ++x) + { + AddVertex(vertices, x, y); + } + } + + // Create indices + Vector indices; + indices.Reserve(gridWidth * gridHeight * 6); + + //top + unsigned int rowIdx = 0; + unsigned int nextRowIdx = gridWidth + 1; + for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) + { + AddQuadIndices(indices, rowIdx, nextRowIdx); + } + + if(gridHeight > 2) + { + rowIdx = gridWidth + 1; + nextRowIdx = (gridWidth + 1) * 2; + + unsigned increment = gridWidth - 1; + if(gridHeight > 3) + { + increment = 2; + //second row left + AddQuadIndices(indices, rowIdx, nextRowIdx); + + rowIdx = gridWidth * 2; + nextRowIdx = (gridWidth + 1) * 2 + 2; + //second row right + AddQuadIndices(indices, rowIdx, nextRowIdx); + + //left and right + rowIdx = nextRowIdx - 2; + nextRowIdx = rowIdx + 4; + for(int y = 2; y < 2 * (gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2) + { + AddQuadIndices(indices, rowIdx, nextRowIdx); + } + } + + //second row left + AddQuadIndices(indices, rowIdx, nextRowIdx); + + rowIdx += increment; + nextRowIdx += gridWidth - 1; + //second row right + AddQuadIndices(indices, rowIdx, nextRowIdx); + } + + //bottom + rowIdx = nextRowIdx - gridWidth + 1; + nextRowIdx = rowIdx + gridWidth + 1; + for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) + { + AddQuadIndices(indices, rowIdx, nextRowIdx); + } + + return GenerateGeometry(vertices, indices); +} + +void RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent) +{ + uint16_t prevEnd = 0; + uint16_t prevFix = 0; + uint16_t prevStretch = 0; + unsigned int i = 1; + for(NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i) + { + uint16_t start = it->GetX(); + uint16_t end = it->GetY(); + + uint16_t fix = prevFix + start - prevEnd; + uint16_t stretch = prevStretch + end - start; + + std::stringstream uniform; + uniform << uniformName << "[" << i << "]"; + renderer.RegisterProperty(uniform.str(), Vector2(fix, stretch)); + + prevEnd = end; + prevFix = fix; + prevStretch = stretch; + } + + { + prevFix += imageExtent - prevEnd; + std::stringstream uniform; + uniform << uniformName << "[" << i << "]"; + renderer.RegisterProperty(uniform.str(), Vector2(prevFix, prevStretch)); + } +} + +void ApplyTextureAndUniforms(Renderer& renderer, const Internal::NPatchData* data) +{ + TextureSet textureSet; + textureSet = data->GetTextures(); + + if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1) + { + //special case for 9 patch + Uint16Pair stretchX = data->GetStretchPixelsX()[0]; + Uint16Pair stretchY = data->GetStretchPixelsY()[0]; + + uint16_t stretchWidth = (stretchX.GetY() >= stretchX.GetX()) ? stretchX.GetY() - stretchX.GetX() : 0; + uint16_t stretchHeight = (stretchY.GetY() >= stretchY.GetX()) ? stretchY.GetY() - stretchY.GetX() : 0; + + renderer.RegisterProperty("uFixed[0]", Vector2::ZERO); + renderer.RegisterProperty("uFixed[1]", Vector2(stretchX.GetX(), stretchY.GetX())); + renderer.RegisterProperty("uFixed[2]", Vector2(data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight)); + renderer.RegisterProperty("uStretchTotal", Vector2(stretchWidth, stretchHeight)); + } + else + { + renderer.RegisterProperty("uNinePatchFactorsX[0]", Vector2::ZERO); + renderer.RegisterProperty("uNinePatchFactorsY[0]", Vector2::ZERO); + + RegisterStretchProperties(renderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth()); + RegisterStretchProperties(renderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight()); + } +} + +} // namespace NPatchHelper + +} // namespace Toolkit + +} // namespace Dali diff --git a/dali-toolkit/devel-api/utility/npatch-helper.h b/dali-toolkit/devel-api/utility/npatch-helper.h new file mode 100644 index 0000000..7a198fe --- /dev/null +++ b/dali-toolkit/devel-api/utility/npatch-helper.h @@ -0,0 +1,97 @@ +#ifndef DALI_TOOLKIT_NPATCH_HELPER_H +#define DALI_TOOLKIT_NPATCH_HELPER_H + +/* + * Copyright (c) 2021 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include +#include + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace NPatchHelper +{ +/** + * The list that includes stretch pixel ranges + */ +using StretchRanges = Dali::Vector; + +/** + * @brief Creates a Npatch Geometry object + * + * @param[in] gridSize The gridSize for creating a geometry + * @return The Geometry for NPatch + */ +DALI_TOOLKIT_API Geometry CreateGridGeometry(Uint16Pair gridSize); + +/** + * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders + * e.g. a 5x4 grid would create a geometry that would look like: + * + * --------------------- + * | /| /| /| /| /| + * |/ |/ |/ |/ |/ | + * --------------------- + * | /| | /| + * |/ | |/ | + * ----- ----- + * | /| | /| + * |/ | |/ | + * --------------------- + * | /| /| /| /| /| + * |/ |/ |/ |/ |/ | + * --------------------- + * + * @param[in] gridSize The grid size of the solid geometry to create + * @return Returns the created geometry for the grid size + */ +DALI_TOOLKIT_API Geometry CreateBorderGeometry(Uint16Pair gridSize); + + +/** + * @brief Registers a properties for Stretch Ranges + * + * @param[in,out] renderer The renderer for broken image + * @param[in] uniformName The name of the uniform + * @param[in] stretchPixels The stretchable pixels in the cropped image space + * @param[in] imageExtent The imageExtent + */ +void RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent); + +/** + * @brief Apply a texture and uniforms + * + * @param[in,out] renderer The renderer for broken image + * @param[in] data The pointer of npatch-data + */ +void ApplyTextureAndUniforms(Renderer& renderer, const Internal::NPatchData* data); + +} // namespace NPatchUtility + +} // namespace Toolkit + +} // namespace Dali + +#endif // DALI_TOOLKIT_NPATCH_HELPER_H diff --git a/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp b/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp index b9e046a..4325dea 100644 --- a/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp +++ b/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -43,91 +44,6 @@ namespace Toolkit { namespace Internal { -namespace -{ -/** - * @brief Creates the geometry formed from the vertices and indices - * - * @param[in] vertices The vertices to generate the geometry from - * @param[in] indices The indices to generate the geometry from - * @return The geometry formed from the vertices and indices - */ -Geometry GenerateGeometry(const Vector& vertices, const Vector& indices) -{ - Property::Map vertexFormat; - vertexFormat["aPosition"] = Property::VECTOR2; - VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat); - if(vertices.Size() > 0) - { - vertexBuffer.SetData(&vertices[0], vertices.Size()); - } - - // Create the geometry object - Geometry geometry = Geometry::New(); - geometry.AddVertexBuffer(vertexBuffer); - if(indices.Size() > 0) - { - geometry.SetIndexBuffer(&indices[0], indices.Size()); - } - - return geometry; -} - -/** - * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid - * - * @param[out] indices The indices to add to - * @param[in] rowIdx The row index to start the quad - * @param[in] nextRowIdx The index to the next row - */ -void AddQuadIndices(Vector& indices, unsigned int rowIdx, unsigned int nextRowIdx) -{ - indices.PushBack(rowIdx); - indices.PushBack(nextRowIdx + 1); - indices.PushBack(rowIdx + 1); - - indices.PushBack(rowIdx); - indices.PushBack(nextRowIdx); - indices.PushBack(nextRowIdx + 1); -} - -void AddVertex(Vector& vertices, unsigned int x, unsigned int y) -{ - vertices.PushBack(Vector2(x, y)); -} - -void RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent) -{ - uint16_t prevEnd = 0; - uint16_t prevFix = 0; - uint16_t prevStretch = 0; - unsigned int i = 1; - for(NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i) - { - uint16_t start = it->GetX(); - uint16_t end = it->GetY(); - - uint16_t fix = prevFix + start - prevEnd; - uint16_t stretch = prevStretch + end - start; - - std::stringstream uniform; - uniform << uniformName << "[" << i << "]"; - renderer.RegisterProperty(uniform.str(), Vector2(fix, stretch)); - - prevEnd = end; - prevFix = fix; - prevStretch = stretch; - } - - { - prevFix += imageExtent - prevEnd; - std::stringstream uniform; - uniform << uniformName << "[" << i << "]"; - renderer.RegisterProperty(uniform.str(), Vector2(prevFix, prevStretch)); - } -} - -} //unnamed namespace /////////////////NPatchVisual//////////////// @@ -421,12 +337,12 @@ Geometry NPatchVisual::CreateGeometry() Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1); if(!data->GetRenderingMap()) { - geometry = !mBorderOnly ? CreateGridGeometry(gridSize) : CreateBorderGeometry(gridSize); + geometry = !mBorderOnly ? NPatchHelper::CreateGridGeometry(gridSize) : NPatchHelper::CreateBorderGeometry(gridSize); } else { uint32_t elementCount[2]; - geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : CreateBorderGeometry(gridSize); + geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : NPatchHelper::CreateBorderGeometry(gridSize); if(mImpl->mRenderer) { RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap()); @@ -530,29 +446,7 @@ void NPatchVisual::ApplyTextureAndUniforms() if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE) { textureSet = data->GetTextures(); - - if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1) - { - //special case for 9 patch - Uint16Pair stretchX = data->GetStretchPixelsX()[0]; - Uint16Pair stretchY = data->GetStretchPixelsY()[0]; - - uint16_t stretchWidth = (stretchX.GetY() >= stretchX.GetX()) ? stretchX.GetY() - stretchX.GetX() : 0; - uint16_t stretchHeight = (stretchY.GetY() >= stretchY.GetX()) ? stretchY.GetY() - stretchY.GetX() : 0; - - mImpl->mRenderer.RegisterProperty("uFixed[0]", Vector2::ZERO); - mImpl->mRenderer.RegisterProperty("uFixed[1]", Vector2(stretchX.GetX(), stretchY.GetX())); - mImpl->mRenderer.RegisterProperty("uFixed[2]", Vector2(data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight)); - mImpl->mRenderer.RegisterProperty("uStretchTotal", Vector2(stretchWidth, stretchHeight)); - } - else - { - mImpl->mRenderer.RegisterProperty("uNinePatchFactorsX[0]", Vector2::ZERO); - mImpl->mRenderer.RegisterProperty("uNinePatchFactorsY[0]", Vector2::ZERO); - - RegisterStretchProperties(mImpl->mRenderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth()); - RegisterStretchProperties(mImpl->mRenderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight()); - } + NPatchHelper::ApplyTextureAndUniforms(mImpl->mRenderer, data); } else { @@ -611,148 +505,17 @@ Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType sub { if(DALI_LIKELY(VisualFactoryCache::NINE_PATCH_GEOMETRY == subType)) { - geometry = CreateGridGeometry(Uint16Pair(3, 3)); + geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3, 3)); } else if(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType) { - geometry = CreateBorderGeometry(Uint16Pair(3, 3)); + geometry = NPatchHelper::CreateBorderGeometry(Uint16Pair(3, 3)); } mFactoryCache.SaveGeometry(subType, geometry); } return geometry; } -Geometry NPatchVisual::CreateGridGeometry(Uint16Pair gridSize) -{ - uint16_t gridWidth = gridSize.GetWidth(); - uint16_t gridHeight = gridSize.GetHeight(); - - // Create vertices - Vector vertices; - vertices.Reserve((gridWidth + 1) * (gridHeight + 1)); - - for(int y = 0; y < gridHeight + 1; ++y) - { - for(int x = 0; x < gridWidth + 1; ++x) - { - AddVertex(vertices, x, y); - } - } - - // Create indices - Vector indices; - indices.Reserve(gridWidth * gridHeight * 6); - - unsigned int rowIdx = 0; - unsigned int nextRowIdx = gridWidth + 1; - for(int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx) - { - for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) - { - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - } - - return GenerateGeometry(vertices, indices); -} - -Geometry NPatchVisual::CreateBorderGeometry(Uint16Pair gridSize) -{ - uint16_t gridWidth = gridSize.GetWidth(); - uint16_t gridHeight = gridSize.GetHeight(); - - // Create vertices - Vector vertices; - vertices.Reserve((gridWidth + 1) * (gridHeight + 1)); - - //top - int y = 0; - for(; y < 2; ++y) - { - for(int x = 0; x < gridWidth + 1; ++x) - { - AddVertex(vertices, x, y); - } - } - - for(; y < gridHeight - 1; ++y) - { - //left - AddVertex(vertices, 0, y); - AddVertex(vertices, 1, y); - - //right - AddVertex(vertices, gridWidth - 1, y); - AddVertex(vertices, gridWidth, y); - } - - //bottom - for(; y < gridHeight + 1; ++y) - { - for(int x = 0; x < gridWidth + 1; ++x) - { - AddVertex(vertices, x, y); - } - } - - // Create indices - Vector indices; - indices.Reserve(gridWidth * gridHeight * 6); - - //top - unsigned int rowIdx = 0; - unsigned int nextRowIdx = gridWidth + 1; - for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) - { - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - - if(gridHeight > 2) - { - rowIdx = gridWidth + 1; - nextRowIdx = (gridWidth + 1) * 2; - - unsigned increment = gridWidth - 1; - if(gridHeight > 3) - { - increment = 2; - //second row left - AddQuadIndices(indices, rowIdx, nextRowIdx); - - rowIdx = gridWidth * 2; - nextRowIdx = (gridWidth + 1) * 2 + 2; - //second row right - AddQuadIndices(indices, rowIdx, nextRowIdx); - - //left and right - rowIdx = nextRowIdx - 2; - nextRowIdx = rowIdx + 4; - for(int y = 2; y < 2 * (gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2) - { - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - } - - //second row left - AddQuadIndices(indices, rowIdx, nextRowIdx); - - rowIdx += increment; - nextRowIdx += gridWidth - 1; - //second row right - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - - //bottom - rowIdx = nextRowIdx - gridWidth + 1; - nextRowIdx = rowIdx + gridWidth + 1; - for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) - { - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - - return GenerateGeometry(vertices, indices); -} - void NPatchVisual::SetResource() { const NPatchData* data; diff --git a/dali-toolkit/internal/visuals/visual-factory-cache.cpp b/dali-toolkit/internal/visuals/visual-factory-cache.cpp index febd285..a65c779 100644 --- a/dali-toolkit/internal/visuals/visual-factory-cache.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-cache.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include namespace Dali @@ -35,73 +36,14 @@ namespace Toolkit { namespace Internal { -namespace -{ - -/** - * @brief Creates the geometry formed from the vertices and indices - * - * @param[in] vertices The vertices to generate the geometry from - * @param[in] indices The indices to generate the geometry from - * @return The geometry formed from the vertices and indices - */ -Geometry GenerateGeometry(const Vector& vertices, const Vector& indices) -{ - Property::Map vertexFormat; - vertexFormat["aPosition"] = Property::VECTOR2; - VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat); - if(vertices.Size() > 0) - { - vertexBuffer.SetData(&vertices[0], vertices.Size()); - } - - // Create the geometry object - Geometry geometry = Geometry::New(); - geometry.AddVertexBuffer(vertexBuffer); - if(indices.Size() > 0) - { - geometry.SetIndexBuffer(&indices[0], indices.Size()); - } - - return geometry; -} - -/** - * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid - * - * @param[out] indices The indices to add to - * @param[in] rowIdx The row index to start the quad - * @param[in] nextRowIdx The index to the next row - */ -void AddQuadIndices(Vector& indices, unsigned int rowIdx, unsigned int nextRowIdx) -{ - indices.PushBack(rowIdx); - indices.PushBack(nextRowIdx + 1); - indices.PushBack(rowIdx + 1); - - indices.PushBack(rowIdx); - indices.PushBack(nextRowIdx); - indices.PushBack(nextRowIdx + 1); -} - -/** - * @brief Adds the vertices to create for npatch - * @param[out] vertices The vertices to add to - * @param[in] x The x value of vector - * @param[in] y The y value of vector - */ -void AddVertex(Vector& vertices, unsigned int x, unsigned int y) -{ - vertices.PushBack(Vector2(x, y)); -} - -} //unnamed namespace VisualFactoryCache::VisualFactoryCache(bool preMultiplyOnLoad) : mSvgRasterizeThread(NULL), mVectorAnimationManager(), mPreMultiplyOnLoad(preMultiplyOnLoad), - mBrokenImageInfoContainer() + mBrokenImageInfoContainer(), + mDefaultBrokenImageUrl(""), + mUseDefaultBrokenImageOnly(true) { } @@ -168,10 +110,7 @@ ImageAtlasManagerPtr VisualFactoryCache::GetAtlasManager() if(!mAtlasManager) { mAtlasManager = new ImageAtlasManager(); - if(!mBrokenImageInfoContainer.empty()) - { - mAtlasManager->SetBrokenImage(mBrokenImageInfoContainer[0].url); - } + mAtlasManager->SetBrokenImage(mDefaultBrokenImageUrl); } return mAtlasManager; @@ -302,14 +241,17 @@ bool VisualFactoryCache::GetPreMultiplyOnLoad() return mPreMultiplyOnLoad; } -void VisualFactoryCache::SetBrokenImageUrl(const std::vector& brokenImageUrlList) +void VisualFactoryCache::SetBrokenImageUrl(std::string& defaultBrokenUrl, const std::vector& brokenImageUrlList) { + mUseDefaultBrokenImageOnly = false; mBrokenImageInfoContainer.clear(); mBrokenImageInfoContainer.assign(brokenImageUrlList.size(), BrokenImageInfo()); for(unsigned int i = 0; i < brokenImageUrlList.size(); i++) { mBrokenImageInfoContainer[i].url = brokenImageUrlList[i]; } + + mDefaultBrokenImageUrl = defaultBrokenUrl; } VisualUrl::Type VisualFactoryCache::GetBrokenImageVisualType(int index) @@ -317,40 +259,6 @@ VisualUrl::Type VisualFactoryCache::GetBrokenImageVisualType(int index) return mBrokenImageInfoContainer[index].visualType; } -Geometry VisualFactoryCache::CreateNPatchGeometry(Uint16Pair gridSize) -{ - uint16_t gridWidth = gridSize.GetWidth(); - uint16_t gridHeight = gridSize.GetHeight(); - - // Create vertices - Vector vertices; - vertices.Reserve((gridWidth + 1) * (gridHeight + 1)); - - for(int y = 0; y < gridHeight + 1; ++y) - { - for(int x = 0; x < gridWidth + 1; ++x) - { - AddVertex(vertices, x, y); - } - } - - // Create indices - Vector indices; - indices.Reserve(gridWidth * gridHeight * 6); - - unsigned int rowIdx = 0; - unsigned int nextRowIdx = gridWidth + 1; - for(int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx) - { - for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx) - { - AddQuadIndices(indices, rowIdx, nextRowIdx); - } - } - - return GenerateGeometry(vertices, indices); -} - Geometry VisualFactoryCache::GetNPatchGeometry(int index) { Geometry geometry; @@ -362,14 +270,14 @@ Geometry VisualFactoryCache::GetNPatchGeometry(int index) geometry = GetGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY); if(!geometry) { - geometry = CreateNPatchGeometry(Uint16Pair(3,3)); + geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3,3)); SaveGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry); } } else if(data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0) { Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1); - geometry = CreateNPatchGeometry(gridSize); + geometry = NPatchHelper::CreateGridGeometry(gridSize); } } else @@ -378,7 +286,7 @@ Geometry VisualFactoryCache::GetNPatchGeometry(int index) geometry = GetGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY); if(!geometry) { - geometry = CreateNPatchGeometry(Uint16Pair(3,3)); + geometry = NPatchHelper::CreateGridGeometry(Uint16Pair(3,3)); SaveGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY, geometry); } } @@ -401,7 +309,7 @@ Shader VisualFactoryCache::GetNPatchShader(int index) yStretchCount = data->GetStretchPixelsY().Count(); } - if(DALI_LIKELY((xStretchCount == 0 && yStretchCount == 0))) + if(DALI_LIKELY((xStretchCount == 0 && yStretchCount == 0) || (xStretchCount == 1 && yStretchCount == 1))) { shader = GetShader(VisualFactoryCache::NINE_PATCH_SHADER); if(DALI_UNLIKELY(!shader)) @@ -423,37 +331,6 @@ Shader VisualFactoryCache::GetNPatchShader(int index) return shader; } -void VisualFactoryCache::RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent) -{ - uint16_t prevEnd = 0; - uint16_t prevFix = 0; - uint16_t prevStretch = 0; - unsigned int i = 1; - for(NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i) - { - uint16_t start = it->GetX(); - uint16_t end = it->GetY(); - - uint16_t fix = prevFix + start - prevEnd; - uint16_t stretch = prevStretch + end - start; - - std::stringstream uniform; - uniform << uniformName << "[" << i << "]"; - renderer.RegisterProperty(uniform.str(), Vector2(fix, stretch)); - - prevEnd = end; - prevFix = fix; - prevStretch = stretch; - } - - { - prevFix += imageExtent - prevEnd; - std::stringstream uniform; - uniform << uniformName << "[" << i << "]"; - renderer.RegisterProperty(uniform.str(), Vector2(prevFix, prevStretch)); - } -} - void VisualFactoryCache::ApplyTextureAndUniforms(Renderer& renderer, int index) { const NPatchData* data; @@ -461,38 +338,23 @@ void VisualFactoryCache::ApplyTextureAndUniforms(Renderer& renderer, int index) if(mNPatchLoader.GetNPatchData(mBrokenImageInfoContainer[index].npatchId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE) { textureSet = data->GetTextures(); - mBrokenImageInfoContainer[index].texture = data->GetTextures().GetTexture(0); - - if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1) - { - //special case for 9 patch - Uint16Pair stretchX = data->GetStretchPixelsX()[0]; - Uint16Pair stretchY = data->GetStretchPixelsY()[0]; - - uint16_t stretchWidth = (stretchX.GetY() >= stretchX.GetX()) ? stretchX.GetY() - stretchX.GetX() : 0; - uint16_t stretchHeight = (stretchY.GetY() >= stretchY.GetX()) ? stretchY.GetY() - stretchY.GetX() : 0; - - renderer.RegisterProperty("uFixed[0]", Vector2::ZERO); - renderer.RegisterProperty("uFixed[1]", Vector2(stretchX.GetX(), stretchY.GetX())); - renderer.RegisterProperty("uFixed[2]", Vector2(data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight)); - renderer.RegisterProperty("uStretchTotal", Vector2(stretchWidth, stretchHeight)); - } - else - { - renderer.RegisterProperty("uNinePatchFactorsX[0]", Vector2::ZERO); - renderer.RegisterProperty("uNinePatchFactorsY[0]", Vector2::ZERO); - - RegisterStretchProperties(renderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth()); - RegisterStretchProperties(renderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight()); - } + mBrokenImageInfoContainer[index].texture = textureSet.GetTexture(0); + NPatchHelper::ApplyTextureAndUniforms(renderer, data); renderer.SetTextures(textureSet); } } void VisualFactoryCache::UpdateBrokenImageRenderer(Renderer& renderer, const Vector2& size) { + + bool useDefaultBrokenImage = false; + if(mBrokenImageInfoContainer.size() == 0) + { + useDefaultBrokenImage = true; + } + // Load Information for broken image - for(uint32_t index = 0; index < mBrokenImageInfoContainer.size(); index++) + for(uint32_t index = 0; (index < mBrokenImageInfoContainer.size()) && !useDefaultBrokenImage; index++) { if(mBrokenImageInfoContainer[index].width == 0 && mBrokenImageInfoContainer[index].height == 0) { @@ -512,17 +374,36 @@ void VisualFactoryCache::UpdateBrokenImageRenderer(Renderer& renderer, const Vec } else { - DALI_LOG_ERROR("Can't update renderer for broken image. maybe image loading is failed [path:%s] \n",mBrokenImageInfoContainer[index].url.c_str()); + DALI_LOG_ERROR("Can't update renderer for broken image. maybe image loading is failed [index:%d] [path:%s] \n",index, mBrokenImageInfoContainer[index].url.c_str()); + useDefaultBrokenImage = true; } } else { - GetBrokenVisualImage(index); + if(!GetBrokenVisualImage(index)) + { + DALI_LOG_ERROR("Can't update renderer for broken image. maybe image loading is failed [index:%d] [path:%s] \n",index, mBrokenImageInfoContainer[index].url.c_str()); + useDefaultBrokenImage = true; + } } } } } + if(!mUseDefaultBrokenImageOnly && useDefaultBrokenImage) + { + // Clear broken info + mBrokenImageInfoContainer.clear(); + + // assign for broken image + const int defaultBrokenIndex = 0; + mBrokenImageInfoContainer.assign(1, BrokenImageInfo()); + mBrokenImageInfoContainer[defaultBrokenIndex].url = mDefaultBrokenImageUrl; + VisualUrl visualUrl(mBrokenImageInfoContainer[defaultBrokenIndex].url); + mBrokenImageInfoContainer[defaultBrokenIndex].visualType = visualUrl.GetType(); + mUseDefaultBrokenImageOnly = true; + } + // Set Texutre to renderer int brokenIndex = GetProperBrokenImageIndex(size); if(GetBrokenImageVisualType(brokenIndex) == VisualUrl::N_PATCH) @@ -547,7 +428,7 @@ int32_t VisualFactoryCache::GetProperBrokenImageIndex(const Vector2& size) { // Sets the default broken type int32_t returnIndex = 0; - if((size.width == 0 || size.height == 0)) + if((size.width == 0 || size.height == 0) || mUseDefaultBrokenImageOnly ) { // To do : Need to add observer about size return returnIndex; diff --git a/dali-toolkit/internal/visuals/visual-factory-cache.h b/dali-toolkit/internal/visuals/visual-factory-cache.h index deae0b0..b19bad4 100644 --- a/dali-toolkit/internal/visuals/visual-factory-cache.h +++ b/dali-toolkit/internal/visuals/visual-factory-cache.h @@ -195,7 +195,7 @@ public: * @brief Set an image to be used when a visual has failed to correctly render * @param[in] brokenImageUrlList The broken image url list */ - void SetBrokenImageUrl(const std::vector& brokenImageUrlList); + void SetBrokenImageUrl(std::string& defaultBrokenUrl, const std::vector& brokenImageUrlList); /** * @brief Update the broken image Renderer object @@ -272,14 +272,6 @@ private: void ApplyTextureAndUniforms(Renderer& renderer, int index); /** - * @brief Creates a Npatch Geometry object - * - * @param[in] gridSize The gridSize for creating a geometry - * @return The Geometry for NPatch - */ - Geometry CreateNPatchGeometry(Uint16Pair gridSize); - - /** * @brief Gets a geometry for npatch image * * @param[in] index the index of broken image @@ -296,16 +288,6 @@ private: Shader GetNPatchShader(int index); /** - * @brief Registers a properties for Stretch Ranges - * - * @param[in,out] renderer The renderer for broken image - * @param[in] uniformName The name of the uniform - * @param[in] stretchPixels The stretchable pixels in the cropped image space - * @param[in] imageExtent The imageExtent - */ - void RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent); - - /** * @brief Returns a broken image type * @param[in] index BrokenImage index * @return The broken image type. @@ -349,6 +331,8 @@ private: std::unique_ptr mVectorAnimationManager; bool mPreMultiplyOnLoad; std::vector mBrokenImageInfoContainer; + std::string mDefaultBrokenImageUrl; + bool mUseDefaultBrokenImageOnly; }; } // namespace Internal diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.cpp b/dali-toolkit/internal/visuals/visual-factory-impl.cpp index 112fdd3..4220b00 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-impl.cpp @@ -377,20 +377,12 @@ void VisualFactory::SetBrokenImageUrl(Toolkit::StyleManager& styleManager) if(styleManager) { customBrokenImageUrlList = Toolkit::DevelStyleManager::GetBrokenImageUrlList(styleManager); - if(customBrokenImageUrlList.size() == 0) - { - Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager); - config["brokenImageUrl"].Get(brokenImageUrl); - customBrokenImageUrlList.push_back(brokenImageUrl); - } - mFactoryCache->SetBrokenImageUrl(customBrokenImageUrlList); - } - else - { - // Set default image - customBrokenImageUrlList.push_back(brokenImageUrl); - mFactoryCache->SetBrokenImageUrl(customBrokenImageUrlList); + Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager); + config["brokenImageUrl"].Get(brokenImageUrl); } + + // Add default image + mFactoryCache->SetBrokenImageUrl(brokenImageUrl, customBrokenImageUrlList); } Internal::VisualFactoryCache& VisualFactory::GetFactoryCache() -- 2.7.4