/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
END_TEST;
}
+int UtcDaliTextureSetRemoveTextureAndGetTextureCount(void)
+{
+ TestApplication application;
+
+ TextureSet textureSet = CreateTextureSet();
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 0u, TEST_LOCATION);
+
+ Texture image0 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
+ Texture image1 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
+ Texture image2 = CreateTexture(TextureType::TEXTURE_2D, Pixel::RGBA8888, 64, 64);
+
+ textureSet.SetTexture(0u, image0);
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+
+ textureSet.SetTexture(1u, image1);
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 2u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
+
+ // Set empty texture so we can remove it.
+ textureSet.SetTexture(1u, Texture());
+
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+
+ application.SendNotification();
+ application.Render();
+
+ textureSet.SetTexture(2u, image2);
+
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
+
+ textureSet.SetTexture(1u, image1);
+
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(1u), image1, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
+
+ // Set empty texture middle of textureset.
+ textureSet.SetTexture(1u, Texture());
+
+ application.SendNotification();
+ application.Render();
+
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 3u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(1u), Texture(), TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(2u), image2, TEST_LOCATION);
+
+ // Set empty texture end of textureset.
+ textureSet.SetTexture(2u, Texture());
+
+ DALI_TEST_EQUALS(textureSet.GetTextureCount(), 1u, TEST_LOCATION);
+ DALI_TEST_EQUALS(textureSet.GetTexture(0u), image0, TEST_LOCATION);
+
+ application.SendNotification();
+ application.Render();
+
+ END_TEST;
+}
int UtcDaliTextureSetSetSamplerNegative(void)
{
TestApplication application;
void TextureSet::SetTexture(uint32_t index, TexturePtr texture)
{
uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
- if(index >= textureCount)
- {
- mTextures.resize(index + 1);
-
- bool samplerExist = true;
- if(mSamplers.size() < index + 1)
- {
- mSamplers.resize(index + 1);
- samplerExist = false;
- }
-
- for(uint32_t i(textureCount); i <= index; ++i)
- {
- mTextures[i] = nullptr;
- if(!samplerExist)
- {
- mSamplers[i] = nullptr;
- }
- }
+ if(textureCount < index + 1)
+ {
+ SetTextureCount(index + 1);
}
mTextures[index] = texture;
}
SceneGraph::SetTextureMessage(mEventThreadServices, *mSceneObject, index, renderTexture);
+
+ if(!texture)
+ {
+ // Check wheter we need to pop back textures
+ TrimContainers();
+ }
}
Texture* TextureSet::GetTexture(uint32_t index) const
uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
if(samplerCount < index + 1)
{
- mSamplers.resize(index + 1);
- for(uint32_t i = samplerCount; i <= index; ++i)
- {
- mSamplers[i] = nullptr;
- }
+ SetSamplerCount(index + 1);
}
mSamplers[index] = sampler;
}
SceneGraph::SetSamplerMessage(mEventThreadServices, *mSceneObject, index, renderSampler);
+
+ if(!sampler)
+ {
+ // Check wheter we need to pop back sampler
+ TrimContainers();
+ }
}
Sampler* TextureSet::GetSampler(uint32_t index) const
return mSceneObject;
}
+void TextureSet::SetTextureCount(uint32_t count)
+{
+ uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
+ if(textureCount != count)
+ {
+ mTextures.resize(count, nullptr);
+ }
+}
+
+void TextureSet::SetSamplerCount(uint32_t count)
+{
+ uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
+ if(samplerCount != count)
+ {
+ mSamplers.resize(count, nullptr);
+ }
+}
+
+void TextureSet::TrimContainers()
+{
+ uint32_t textureCount = static_cast<uint32_t>(mTextures.size());
+ uint32_t samplerCount = static_cast<uint32_t>(mSamplers.size());
+
+ while(textureCount > 0u)
+ {
+ if(mTextures[textureCount - 1u])
+ {
+ break;
+ }
+ --textureCount;
+ }
+
+ while(samplerCount > 0u)
+ {
+ if(mSamplers[samplerCount - 1u])
+ {
+ break;
+ }
+ --samplerCount;
+ }
+
+ SetTextureCount(textureCount);
+ SetSamplerCount(samplerCount);
+}
+
TextureSet::TextureSet()
: mEventThreadServices(EventThreadServices::Get()),
mSceneObject(nullptr)
#define DALI_INTERNAL_TEXTURE_SET_H
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
*/
void Initialize();
+ /**
+ * @brief Change the count of texture and sampler. It will increase count automatically if we need more textures.
+ *
+ * @param[in] count The number of textures what this texture set want to hold
+ */
+ void SetTextureCount(uint32_t count);
+
+ /**
+ * @brief Change the count of sampler. It will increase count automatically if we need more samplers.
+ *
+ * @param[in] count The number of samplers what this texture set want to hold
+ */
+ void SetSamplerCount(uint32_t count);
+
+ /**
+ * @brief Remove empty textures and samplers at the back of container, and resize.
+ */
+ void TrimContainers();
+
protected:
/**
* A reference counted object may only be deleted by calling Unreference()
// if it's default, delete the graphics object
// otherwise re-initialize it if dirty
- const Graphics::Sampler* graphicsSampler = samplers ? ((*samplers)[i] ? (*samplers)[i]->GetGraphicsObject()
- : nullptr)
+ const Graphics::Sampler* graphicsSampler = samplers ? ((i < (*samplers).Size() && (*samplers)[i]) ? (*samplers)[i]->GetGraphicsObject()
+ : nullptr)
: nullptr;
const Graphics::TextureBinding textureBinding{graphicsTexture, graphicsSampler, textureUnit};
TextureSet::TextureSet()
: mSamplers(),
+ mTextures(),
mHasAlpha(false)
{
}
const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
if(samplerCount < index + 1)
{
- mSamplers.Resize(index + 1);
- for(uint32_t i(samplerCount); i <= index; ++i)
- {
- mSamplers[i] = nullptr;
- }
+ SetSamplerCount(index + 1);
}
mSamplers[index] = sampler;
// Construct message in the render queue memory; note that delete should not be called on the return value
new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, mTextures[index]);
}
+
+ if(!sampler)
+ {
+ // Check wheter we need to pop back sampler
+ TrimContainers();
+ }
}
void TextureSet::SetTexture(uint32_t index, const Render::TextureKey& texture)
const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
if(textureCount < index + 1)
{
- mTextures.Resize(index + 1);
-
- bool samplerExist = true;
- if(mSamplers.Size() < index + 1)
- {
- mSamplers.Resize(index + 1);
- samplerExist = false;
- }
-
- for(uint32_t i(textureCount); i <= index; ++i)
- {
- mTextures[i] = Render::TextureKey{};
-
- if(!samplerExist)
- {
- mSamplers[i] = nullptr;
- }
- }
+ SetTextureCount(index + 1);
}
mTextures[index] = texture;
// Construct message in the render queue memory; note that delete should not be called on the return value
new(slot) DerivedType(&mRenderMessageDispatcher->GetRenderManager(), &RenderManager::SetTextureUpdated, texture);
}
+ else
+ {
+ // Check wheter we need to pop back textures
+ TrimContainers();
+ }
+}
+
+void TextureSet::SetTextureCount(uint32_t count)
+{
+ const uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
+
+ if(textureCount != count)
+ {
+ mTextures.Resize(count, Render::TextureKey{});
+ }
+}
+
+void TextureSet::SetSamplerCount(uint32_t count)
+{
+ const uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
+
+ if(samplerCount != count)
+ {
+ mSamplers.Resize(count, nullptr);
+ }
+}
+
+void TextureSet::TrimContainers()
+{
+ uint32_t textureCount = static_cast<uint32_t>(mTextures.Size());
+ uint32_t samplerCount = static_cast<uint32_t>(mSamplers.Size());
+
+ while(textureCount > 0u)
+ {
+ if(mTextures[textureCount - 1u])
+ {
+ break;
+ }
+ --textureCount;
+ }
+
+ while(samplerCount > 0u)
+ {
+ if(mSamplers[samplerCount - 1u])
+ {
+ break;
+ }
+ --samplerCount;
+ }
+
+ SetTextureCount(textureCount);
+ SetSamplerCount(samplerCount);
}
bool TextureSet::HasAlpha() const
TextureSet();
private:
+ /**
+ * @brief Change the count of texture set. It will increase count automatically if we need more textures.
+ *
+ * @param[in] count The number of textures what this texture set want to hold
+ */
+ void SetTextureCount(uint32_t count);
+
+ /**
+ * @brief Change the count of sampler set. It will increase count automatically if we need more samplers.
+ *
+ * @param[in] count The number of samplers what this texture set want to hold
+ */
+ void SetSamplerCount(uint32_t count);
+
+ /**
+ * @brief Remove empty textures and samplers at the back of container, and resize.
+ */
+ void TrimContainers();
+
+private:
Vector<Render::Sampler*> mSamplers; ///< List of samplers used by each texture. Not owned
Vector<Render::TextureKey> mTextures; ///< List of Textures. Not owned
RenderMessageDispatcher* mRenderMessageDispatcher{nullptr}; ///< for sending messages to render thread. Not owned