X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Fnpatch%2Fnpatch-visual.cpp;h=bca8d0376eabf49ad11a01fbd6464578f20fae50;hp=6ef02cc787512701f1653497bad89df7516fb213;hb=1d71a8f7d7abd7729aa645ad062e530958097214;hpb=b0faad981257720e1a5fb15af45647f30b669adf diff --git a/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp b/dali-toolkit/internal/visuals/npatch/npatch-visual.cpp index 6ef02cc..bca8d03 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,11 +20,12 @@ // EXTERNAL INCLUDES #include +#include #include #include -#include // INTERNAL INCLUDES +#include #include #include #include @@ -45,90 +46,9 @@ 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)); - } +const int CUSTOM_PROPERTY_COUNT(10); // 5 transform properties + fixed(3),stretch,aux } -} //unnamed namespace - /////////////////NPatchVisual//////////////// NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties) @@ -153,7 +73,7 @@ void NPatchVisual::LoadImages() TextureManager& textureManager = mFactoryCache.GetTextureManager(); bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING; - if(mId == NPatchData::INVALID_NPATCH_DATA_ID && mImageUrl.IsLocalResource()) + if(mId == NPatchData::INVALID_NPATCH_DATA_ID && (mImageUrl.IsLocalResource() || mImageUrl.IsBufferResource())) { bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false; mId = mLoader.Load(textureManager, this, mImageUrl, mBorder, preMultiplyOnLoad, synchronousLoading); @@ -165,7 +85,7 @@ void NPatchVisual::LoadImages() } } - if(!mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource()) + if(!mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && (mAuxiliaryUrl.IsLocalResource() || mAuxiliaryUrl.IsBufferResource())) { // Load the auxiliary image auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY; @@ -378,13 +298,10 @@ void NPatchVisual::OnInitialize() Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY); Shader shader = mImageVisualShaderFactory.GetShader( mFactoryCache, - TextureAtlas::DISABLED, - DefaultTextureWrapMode::APPLY, - RoundedCorner::DISABLED, - Borderline::DISABLED - ); + ImageVisualShaderFeature::FeatureBuilder()); mImpl->mRenderer = Renderer::New(geometry, shader); + mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT); //Register transform properties mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); @@ -424,12 +341,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()); @@ -456,8 +373,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)) @@ -533,41 +450,20 @@ 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 { DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str()); textureSet = TextureSet::New(); - Texture croppedImage = mFactoryCache.GetBrokenVisualImage(); - 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())); + Actor actor = mPlacementActor.GetHandle(); + Vector2 imageSize = Vector2::ZERO; + if(actor) + { + imageSize = actor.GetProperty(Actor::Property::SIZE).Get(); + } + mFactoryCache.UpdateBrokenImageRenderer(mImpl->mRenderer, imageSize); } if(mAuxiliaryPixelBuffer) @@ -607,148 +503,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; @@ -773,32 +538,34 @@ void NPatchVisual::SetResource() } } -void NPatchVisual::UploadComplete(bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied) +void NPatchVisual::LoadComplete(bool loadSuccess, TextureInformation textureInformation) { - EnablePreMultipliedAlpha(preMultiplied); - if(!loadSuccess) - { - // Image loaded and ready to display - ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); - } - - if(mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid()) + if(textureInformation.returnType == TextureUploadObserver::ReturnType::TEXTURE) { - SetResource(); - } -} + EnablePreMultipliedAlpha(textureInformation.preMultiplied); + if(!loadSuccess) + { + // Image loaded and ready to display + ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + } -void NPatchVisual::LoadComplete(bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied) -{ - if(loadSuccess && url.GetUrl() == mAuxiliaryUrl.GetUrl()) - { - mAuxiliaryPixelBuffer = pixelBuffer; - SetResource(); + if(mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid()) + { + SetResource(); + } } - else + else // for the ReturnType::PIXEL_BUFFER { - // Image loaded and ready to display - ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + if(loadSuccess && textureInformation.url == mAuxiliaryUrl.GetUrl()) + { + mAuxiliaryPixelBuffer = textureInformation.pixelBuffer; + SetResource(); + } + else + { + // Image loaded and ready to display + ResourceReady(Toolkit::Visual::ResourceStatus::FAILED); + } } }