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/adaptor-framework/image-loading.h>
23 #include <dali/devel-api/rendering/renderer-devel.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/devel-api/common/stage.h>
28 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
29 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
30 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
31 #include <dali-toolkit/internal/visuals/npatch-loader.h>
32 #include <dali-toolkit/internal/visuals/rendering-addon.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/public-api/visuals/visual-properties.h>
49 * @brief Creates the geometry formed from the vertices and indices
51 * @param[in] vertices The vertices to generate the geometry from
52 * @param[in] indices The indices to generate the geometry from
53 * @return The geometry formed from the vertices and indices
55 Geometry GenerateGeometry(const Vector<Vector2>& vertices, const Vector<unsigned short>& indices)
57 Property::Map vertexFormat;
58 vertexFormat["aPosition"] = Property::VECTOR2;
59 VertexBuffer vertexBuffer = VertexBuffer::New(vertexFormat);
60 if(vertices.Size() > 0)
62 vertexBuffer.SetData(&vertices[0], vertices.Size());
65 // Create the geometry object
66 Geometry geometry = Geometry::New();
67 geometry.AddVertexBuffer(vertexBuffer);
68 if(indices.Size() > 0)
70 geometry.SetIndexBuffer(&indices[0], indices.Size());
77 * @brief Adds the indices to form a quad composed off two triangles where the indices are organised in a grid
79 * @param[out] indices The indices to add to
80 * @param[in] rowIdx The row index to start the quad
81 * @param[in] nextRowIdx The index to the next row
83 void AddQuadIndices(Vector<unsigned short>& indices, unsigned int rowIdx, unsigned int nextRowIdx)
85 indices.PushBack(rowIdx);
86 indices.PushBack(nextRowIdx + 1);
87 indices.PushBack(rowIdx + 1);
89 indices.PushBack(rowIdx);
90 indices.PushBack(nextRowIdx);
91 indices.PushBack(nextRowIdx + 1);
94 void AddVertex(Vector<Vector2>& vertices, unsigned int x, unsigned int y)
96 vertices.PushBack(Vector2(x, y));
99 void RegisterStretchProperties(Renderer& renderer, const char* uniformName, const NPatchUtility::StretchRanges& stretchPixels, uint16_t imageExtent)
101 uint16_t prevEnd = 0;
102 uint16_t prevFix = 0;
103 uint16_t prevStretch = 0;
105 for(NPatchUtility::StretchRanges::ConstIterator it = stretchPixels.Begin(); it != stretchPixels.End(); ++it, ++i)
107 uint16_t start = it->GetX();
108 uint16_t end = it->GetY();
110 uint16_t fix = prevFix + start - prevEnd;
111 uint16_t stretch = prevStretch + end - start;
113 std::stringstream uniform;
114 uniform << uniformName << "[" << i << "]";
115 renderer.RegisterProperty(uniform.str(), Vector2(fix, stretch));
119 prevStretch = stretch;
123 prevFix += imageExtent - prevEnd;
124 std::stringstream uniform;
125 uniform << uniformName << "[" << i << "]";
126 renderer.RegisterProperty(uniform.str(), Vector2(prevFix, prevStretch));
130 } //unnamed namespace
132 /////////////////NPatchVisual////////////////
134 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl, const Property::Map& properties)
136 NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
137 nPatchVisual->mImageUrl = imageUrl;
138 nPatchVisual->SetProperties(properties);
139 nPatchVisual->Initialize();
143 NPatchVisualPtr NPatchVisual::New(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory, const VisualUrl& imageUrl)
145 NPatchVisualPtr nPatchVisual(new NPatchVisual(factoryCache, shaderFactory));
146 nPatchVisual->mImageUrl = imageUrl;
147 nPatchVisual->Initialize();
151 void NPatchVisual::LoadImages()
153 TextureManager& textureManager = mFactoryCache.GetTextureManager();
154 bool synchronousLoading = mImpl->mFlags & Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
156 if(mId == NPatchData::INVALID_NPATCH_DATA_ID && mImageUrl.IsLocalResource())
158 bool preMultiplyOnLoad = IsPreMultipliedAlphaEnabled() && !mImpl->mCustomShader ? true : false;
159 mId = mLoader.Load(textureManager, this, mImageUrl.GetUrl(), mBorder, preMultiplyOnLoad, synchronousLoading);
161 const NPatchData* data;
162 if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
164 EnablePreMultipliedAlpha(data->IsPreMultiplied());
168 if(!mAuxiliaryPixelBuffer && mAuxiliaryUrl.IsValid() && mAuxiliaryUrl.IsLocalResource())
170 // Load the auxiliary image
171 auto preMultiplyOnLoading = TextureManager::MultiplyOnLoad::LOAD_WITHOUT_MULTIPLY;
172 mAuxiliaryPixelBuffer = textureManager.LoadPixelBuffer(mAuxiliaryUrl, Dali::ImageDimensions(), FittingMode::DEFAULT, SamplingMode::BOX_THEN_LINEAR, synchronousLoading, this, true, preMultiplyOnLoading);
176 void NPatchVisual::GetNaturalSize(Vector2& naturalSize)
181 // load now if not already loaded
182 const NPatchData* data;
183 if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() != NPatchData::LoadingState::LOADING)
185 naturalSize.x = data->GetCroppedWidth();
186 naturalSize.y = data->GetCroppedHeight();
190 if(mImageUrl.IsValid())
192 ImageDimensions dimensions = Dali::GetOriginalImageSize(mImageUrl.GetUrl());
193 if(dimensions != ImageDimensions(0, 0))
195 naturalSize.x = dimensions.GetWidth();
196 naturalSize.y = dimensions.GetHeight();
201 if(mAuxiliaryPixelBuffer)
203 naturalSize.x = std::max(naturalSize.x, float(mAuxiliaryPixelBuffer.GetWidth()));
204 naturalSize.y = std::max(naturalSize.y, float(mAuxiliaryPixelBuffer.GetHeight()));
208 void NPatchVisual::DoSetProperties(const Property::Map& propertyMap)
210 // URL is already passed in via constructor
212 Property::Value* borderOnlyValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY);
215 borderOnlyValue->Get(mBorderOnly);
218 Property::Value* borderValue = propertyMap.Find(Toolkit::ImageVisual::Property::BORDER, BORDER);
219 if(borderValue && !borderValue->Get(mBorder)) // If value exists and is rect, just set mBorder
221 // Not a rect so try vector4
223 if(borderValue->Get(border))
225 mBorder.left = static_cast<int>(border.x);
226 mBorder.right = static_cast<int>(border.y);
227 mBorder.bottom = static_cast<int>(border.z);
228 mBorder.top = static_cast<int>(border.w);
232 Property::Value* auxImage = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, AUXILIARY_IMAGE_NAME);
236 if(auxImage->Get(url))
242 Property::Value* auxImageAlpha = propertyMap.Find(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, AUXILIARY_IMAGE_ALPHA_NAME);
245 auxImageAlpha->Get(mAuxiliaryImageAlpha);
248 Property::Value* synchronousLoading = propertyMap.Find(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, SYNCHRONOUS_LOADING);
249 if(synchronousLoading)
252 synchronousLoading->Get(sync);
255 mImpl->mFlags |= Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
259 mImpl->mFlags &= ~Impl::IS_SYNCHRONOUS_RESOURCE_LOADING;
263 Property::Value* releasePolicy = propertyMap.Find(Toolkit::ImageVisual::Property::RELEASE_POLICY, RELEASE_POLICY_NAME);
266 releasePolicy->Get(mReleasePolicy);
270 void NPatchVisual::DoSetOnScene(Actor& actor)
272 // load when first go on stage
275 const NPatchData* data;
276 if(mLoader.GetNPatchData(mId, data))
278 Geometry geometry = CreateGeometry();
279 Shader shader = CreateShader();
281 mImpl->mRenderer.SetGeometry(geometry);
282 mImpl->mRenderer.SetShader(shader);
284 mPlacementActor = actor;
285 if(data->GetLoadingState() != NPatchData::LoadingState::LOADING)
287 if(RenderingAddOn::Get().IsValid())
289 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
292 ApplyTextureAndUniforms();
293 actor.AddRenderer(mImpl->mRenderer);
294 mPlacementActor.Reset();
296 // npatch loaded and ready to display
297 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
302 void NPatchVisual::DoSetOffScene(Actor& actor)
304 if((mId != NPatchData::INVALID_NPATCH_DATA_ID) && mReleasePolicy == Toolkit::ImageVisual::ReleasePolicy::DETACHED)
306 mLoader.Remove(mId, this);
307 mImpl->mResourceStatus = Toolkit::Visual::ResourceStatus::PREPARING;
308 mId = NPatchData::INVALID_NPATCH_DATA_ID;
311 actor.RemoveRenderer(mImpl->mRenderer);
312 mPlacementActor.Reset();
315 void NPatchVisual::OnSetTransform()
319 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
323 void NPatchVisual::DoCreatePropertyMap(Property::Map& map) const
326 bool sync = IsSynchronousLoadingRequired();
327 map.Insert(Toolkit::ImageVisual::Property::SYNCHRONOUS_LOADING, sync);
328 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::N_PATCH);
329 map.Insert(Toolkit::ImageVisual::Property::URL, mImageUrl.GetUrl());
330 map.Insert(Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly);
331 map.Insert(Toolkit::ImageVisual::Property::BORDER, mBorder);
332 map.Insert(Toolkit::ImageVisual::Property::RELEASE_POLICY, mReleasePolicy);
334 if(mAuxiliaryUrl.IsValid())
336 map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
337 map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
341 void NPatchVisual::DoCreateInstancePropertyMap(Property::Map& map) const
343 if(mAuxiliaryUrl.IsValid())
345 map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE, mAuxiliaryUrl.GetUrl());
346 map.Insert(Toolkit::DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA, mAuxiliaryImageAlpha);
350 NPatchVisual::NPatchVisual(VisualFactoryCache& factoryCache, ImageVisualShaderFactory& shaderFactory)
351 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::N_PATCH),
353 mLoader(factoryCache.GetNPatchLoader()),
354 mImageVisualShaderFactory(shaderFactory),
357 mId(NPatchData::INVALID_NPATCH_DATA_ID),
360 mAuxiliaryImageAlpha(0.0f),
361 mReleasePolicy(Toolkit::ImageVisual::ReleasePolicy::DETACHED)
363 EnablePreMultipliedAlpha(mFactoryCache.GetPreMultiplyOnLoad());
366 NPatchVisual::~NPatchVisual()
368 if(Stage::IsInstalled() && (mId != NPatchData::INVALID_NPATCH_DATA_ID) && (mReleasePolicy != Toolkit::ImageVisual::ReleasePolicy::NEVER))
370 mLoader.Remove(mId, this);
371 mId = NPatchData::INVALID_NPATCH_DATA_ID;
375 void NPatchVisual::OnInitialize()
377 // Get basic geometry and shader
378 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
379 Shader shader = mImageVisualShaderFactory.GetShader(mFactoryCache, false, true, false);
381 mImpl->mRenderer = Renderer::New(geometry, shader);
383 //Register transform properties
384 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
387 Geometry NPatchVisual::CreateGeometry()
390 const NPatchData* data;
391 if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
393 if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
395 if(DALI_UNLIKELY(mBorderOnly))
397 geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY);
401 if(data->GetRenderingMap())
403 uint32_t elementCount[2];
404 geometry = RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), Uint16Pair(3, 3), elementCount);
407 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
412 geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
416 else if(data->GetStretchPixelsX().Size() > 0 || data->GetStretchPixelsY().Size() > 0)
418 Uint16Pair gridSize(2 * data->GetStretchPixelsX().Size() + 1, 2 * data->GetStretchPixelsY().Size() + 1);
419 if(!data->GetRenderingMap())
421 geometry = !mBorderOnly ? CreateGridGeometry(gridSize) : CreateBorderGeometry(gridSize);
425 uint32_t elementCount[2];
426 geometry = !mBorderOnly ? RenderingAddOn::Get().CreateGeometryGrid(data->GetRenderingMap(), gridSize, elementCount) : CreateBorderGeometry(gridSize);
429 RenderingAddOn::Get().SubmitRenderTask(mImpl->mRenderer, data->GetRenderingMap());
436 // no N patch data so use default geometry
437 geometry = GetNinePatchGeometry(VisualFactoryCache::NINE_PATCH_GEOMETRY);
442 Shader NPatchVisual::CreateShader()
445 const NPatchData* data;
446 // 0 is either no data (load failed?) or no stretch regions on image
447 // for both cases we use the default shader
448 NPatchUtility::StretchRanges::SizeType xStretchCount = 0;
449 NPatchUtility::StretchRanges::SizeType yStretchCount = 0;
451 auto fragmentShader = mAuxiliaryPixelBuffer ? SHADER_NPATCH_VISUAL_MASK_SHADER_FRAG
452 : SHADER_NPATCH_VISUAL_SHADER_FRAG;
453 auto shaderType = mAuxiliaryPixelBuffer ? VisualFactoryCache::NINE_PATCH_MASK_SHADER
454 : VisualFactoryCache::NINE_PATCH_SHADER;
456 // ask loader for the regions
457 if(mLoader.GetNPatchData(mId, data))
459 xStretchCount = data->GetStretchPixelsX().Count();
460 yStretchCount = data->GetStretchPixelsY().Count();
463 if(DALI_LIKELY(!mImpl->mCustomShader))
465 if(DALI_LIKELY((xStretchCount == 1 && yStretchCount == 1) ||
466 (xStretchCount == 0 && yStretchCount == 0)))
468 shader = mFactoryCache.GetShader(shaderType);
469 if(DALI_UNLIKELY(!shader))
471 shader = Shader::New(SHADER_NPATCH_VISUAL_3X3_SHADER_VERT, fragmentShader);
472 // Only cache vanilla 9 patch shaders
473 mFactoryCache.SaveShader(shaderType, shader);
476 else if(xStretchCount > 0 || yStretchCount > 0)
478 std::stringstream vertexShader;
479 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
480 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
481 << SHADER_NPATCH_VISUAL_SHADER_VERT;
483 shader = Shader::New(vertexShader.str(), fragmentShader);
488 Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
490 if(!mImpl->mCustomShader->mFragmentShader.empty())
492 fragmentShader = mImpl->mCustomShader->mFragmentShader.c_str();
494 hints = mImpl->mCustomShader->mHints;
496 /* Apply Custom Vertex Shader only if image is 9-patch */
497 if((xStretchCount == 1 && yStretchCount == 1) ||
498 (xStretchCount == 0 && yStretchCount == 0))
500 const char* vertexShader = SHADER_NPATCH_VISUAL_3X3_SHADER_VERT.data();
502 if(!mImpl->mCustomShader->mVertexShader.empty())
504 vertexShader = mImpl->mCustomShader->mVertexShader.c_str();
506 shader = Shader::New(vertexShader, fragmentShader, hints);
508 else if(xStretchCount > 0 || yStretchCount > 0)
510 std::stringstream vertexShader;
511 vertexShader << "#define FACTOR_SIZE_X " << xStretchCount + 2 << "\n"
512 << "#define FACTOR_SIZE_Y " << yStretchCount + 2 << "\n"
513 << SHADER_NPATCH_VISUAL_SHADER_VERT;
515 shader = Shader::New(vertexShader.str(), fragmentShader, hints);
522 void NPatchVisual::ApplyTextureAndUniforms()
524 const NPatchData* data;
525 TextureSet textureSet;
527 if(mLoader.GetNPatchData(mId, data) && data->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
529 textureSet = data->GetTextures();
531 if(data->GetStretchPixelsX().Size() == 1 && data->GetStretchPixelsY().Size() == 1)
533 //special case for 9 patch
534 Uint16Pair stretchX = data->GetStretchPixelsX()[0];
535 Uint16Pair stretchY = data->GetStretchPixelsY()[0];
537 uint16_t stretchWidth = (stretchX.GetY() >= stretchX.GetX()) ? stretchX.GetY() - stretchX.GetX() : 0;
538 uint16_t stretchHeight = (stretchY.GetY() >= stretchY.GetX()) ? stretchY.GetY() - stretchY.GetX() : 0;
540 mImpl->mRenderer.RegisterProperty("uFixed[0]", Vector2::ZERO);
541 mImpl->mRenderer.RegisterProperty("uFixed[1]", Vector2(stretchX.GetX(), stretchY.GetX()));
542 mImpl->mRenderer.RegisterProperty("uFixed[2]", Vector2(data->GetCroppedWidth() - stretchWidth, data->GetCroppedHeight() - stretchHeight));
543 mImpl->mRenderer.RegisterProperty("uStretchTotal", Vector2(stretchWidth, stretchHeight));
547 mImpl->mRenderer.RegisterProperty("uNinePatchFactorsX[0]", Vector2::ZERO);
548 mImpl->mRenderer.RegisterProperty("uNinePatchFactorsY[0]", Vector2::ZERO);
550 RegisterStretchProperties(mImpl->mRenderer, "uNinePatchFactorsX", data->GetStretchPixelsX(), data->GetCroppedWidth());
551 RegisterStretchProperties(mImpl->mRenderer, "uNinePatchFactorsY", data->GetStretchPixelsY(), data->GetCroppedHeight());
556 DALI_LOG_ERROR("The N patch image '%s' is not a valid N patch image\n", mImageUrl.GetUrl().c_str());
557 textureSet = TextureSet::New();
559 Texture croppedImage = mFactoryCache.GetBrokenVisualImage();
560 textureSet.SetTexture(0u, croppedImage);
561 mImpl->mRenderer.RegisterProperty("uFixed[0]", Vector2::ZERO);
562 mImpl->mRenderer.RegisterProperty("uFixed[1]", Vector2::ZERO);
563 mImpl->mRenderer.RegisterProperty("uFixed[2]", Vector2::ZERO);
564 mImpl->mRenderer.RegisterProperty("uStretchTotal", Vector2(croppedImage.GetWidth(), croppedImage.GetHeight()));
567 if(mAuxiliaryPixelBuffer)
569 // If the auxiliary image is smaller than the un-stretched NPatch, use CPU resizing to enlarge it to the
570 // same size as the unstretched NPatch. This will give slightly higher quality results than just relying
571 // on GL interpolation alone.
572 if(mAuxiliaryPixelBuffer.GetWidth() < data->GetCroppedWidth() &&
573 mAuxiliaryPixelBuffer.GetHeight() < data->GetCroppedHeight())
575 mAuxiliaryPixelBuffer.Resize(data->GetCroppedWidth(), data->GetCroppedHeight());
578 // Note, this resets mAuxiliaryPixelBuffer handle
579 auto auxiliaryPixelData = Devel::PixelBuffer::Convert(mAuxiliaryPixelBuffer);
581 auto texture = Texture::New(TextureType::TEXTURE_2D,
582 auxiliaryPixelData.GetPixelFormat(),
583 auxiliaryPixelData.GetWidth(),
584 auxiliaryPixelData.GetHeight());
585 texture.Upload(auxiliaryPixelData);
586 textureSet.SetTexture(1, texture);
587 mImpl->mRenderer.RegisterProperty(DevelImageVisual::Property::AUXILIARY_IMAGE_ALPHA,
588 AUXILIARY_IMAGE_ALPHA_NAME,
589 mAuxiliaryImageAlpha);
591 mImpl->mRenderer.SetTextures(textureSet);
593 // Register transform properties
594 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
597 Geometry NPatchVisual::GetNinePatchGeometry(VisualFactoryCache::GeometryType subType)
599 Geometry geometry = mFactoryCache.GetGeometry(subType);
602 if(DALI_LIKELY(VisualFactoryCache::NINE_PATCH_GEOMETRY == subType))
604 geometry = CreateGridGeometry(Uint16Pair(3, 3));
606 else if(VisualFactoryCache::NINE_PATCH_BORDER_GEOMETRY == subType)
608 geometry = CreateBorderGeometry(Uint16Pair(3, 3));
610 mFactoryCache.SaveGeometry(subType, geometry);
615 Geometry NPatchVisual::CreateGridGeometry(Uint16Pair gridSize)
617 uint16_t gridWidth = gridSize.GetWidth();
618 uint16_t gridHeight = gridSize.GetHeight();
621 Vector<Vector2> vertices;
622 vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
624 for(int y = 0; y < gridHeight + 1; ++y)
626 for(int x = 0; x < gridWidth + 1; ++x)
628 AddVertex(vertices, x, y);
633 Vector<unsigned short> indices;
634 indices.Reserve(gridWidth * gridHeight * 6);
636 unsigned int rowIdx = 0;
637 unsigned int nextRowIdx = gridWidth + 1;
638 for(int y = 0; y < gridHeight; ++y, ++nextRowIdx, ++rowIdx)
640 for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx)
642 AddQuadIndices(indices, rowIdx, nextRowIdx);
646 return GenerateGeometry(vertices, indices);
649 Geometry NPatchVisual::CreateBorderGeometry(Uint16Pair gridSize)
651 uint16_t gridWidth = gridSize.GetWidth();
652 uint16_t gridHeight = gridSize.GetHeight();
655 Vector<Vector2> vertices;
656 vertices.Reserve((gridWidth + 1) * (gridHeight + 1));
662 for(int x = 0; x < gridWidth + 1; ++x)
664 AddVertex(vertices, x, y);
668 for(; y < gridHeight - 1; ++y)
671 AddVertex(vertices, 0, y);
672 AddVertex(vertices, 1, y);
675 AddVertex(vertices, gridWidth - 1, y);
676 AddVertex(vertices, gridWidth, y);
680 for(; y < gridHeight + 1; ++y)
682 for(int x = 0; x < gridWidth + 1; ++x)
684 AddVertex(vertices, x, y);
689 Vector<unsigned short> indices;
690 indices.Reserve(gridWidth * gridHeight * 6);
693 unsigned int rowIdx = 0;
694 unsigned int nextRowIdx = gridWidth + 1;
695 for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx)
697 AddQuadIndices(indices, rowIdx, nextRowIdx);
702 rowIdx = gridWidth + 1;
703 nextRowIdx = (gridWidth + 1) * 2;
705 unsigned increment = gridWidth - 1;
710 AddQuadIndices(indices, rowIdx, nextRowIdx);
712 rowIdx = gridWidth * 2;
713 nextRowIdx = (gridWidth + 1) * 2 + 2;
715 AddQuadIndices(indices, rowIdx, nextRowIdx);
718 rowIdx = nextRowIdx - 2;
719 nextRowIdx = rowIdx + 4;
720 for(int y = 2; y < 2 * (gridHeight - 3); ++y, rowIdx += 2, nextRowIdx += 2)
722 AddQuadIndices(indices, rowIdx, nextRowIdx);
727 AddQuadIndices(indices, rowIdx, nextRowIdx);
730 nextRowIdx += gridWidth - 1;
732 AddQuadIndices(indices, rowIdx, nextRowIdx);
736 rowIdx = nextRowIdx - gridWidth + 1;
737 nextRowIdx = rowIdx + gridWidth + 1;
738 for(int x = 0; x < gridWidth; ++x, ++nextRowIdx, ++rowIdx)
740 AddQuadIndices(indices, rowIdx, nextRowIdx);
743 return GenerateGeometry(vertices, indices);
746 void NPatchVisual::SetResource()
748 const NPatchData* data;
749 if(mImpl->mRenderer && mLoader.GetNPatchData(mId, data))
751 Geometry geometry = CreateGeometry();
752 Shader shader = CreateShader();
754 mImpl->mRenderer.SetGeometry(geometry);
755 mImpl->mRenderer.SetShader(shader);
757 Actor actor = mPlacementActor.GetHandle();
760 ApplyTextureAndUniforms();
761 actor.AddRenderer(mImpl->mRenderer);
762 mPlacementActor.Reset();
764 // npatch loaded and ready to display
765 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
770 void NPatchVisual::UploadComplete(bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied)
772 EnablePreMultipliedAlpha(preMultiplied);
775 // Image loaded and ready to display
776 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
779 if(mAuxiliaryPixelBuffer || !mAuxiliaryUrl.IsValid())
785 void NPatchVisual::LoadComplete(bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied)
787 if(loadSuccess && url.GetUrl() == mAuxiliaryUrl.GetUrl())
789 mAuxiliaryPixelBuffer = pixelBuffer;
794 // Image loaded and ready to display
795 ResourceReady(Toolkit::Visual::ResourceStatus::FAILED);
799 } // namespace Internal
801 } // namespace Toolkit