2 * Copyright (c) 2023 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.
18 #include <dali/internal/render/renderers/render-texture.h>
21 #include <math.h> //floor, log2
24 #include <dali/integration-api/debug.h>
25 #include <dali/internal/common/memory-pool-object-allocator.h>
26 #include <dali/internal/render/common/render-manager.h>
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gTextureFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TEXTURE");
40 // Memory pool used to allocate new textures. Memory used by this pool will be released when shutting down DALi
41 MemoryPoolObjectAllocator<Texture>& GetTextureMemoryPool()
43 static MemoryPoolObjectAllocator<Texture> gTextureMemoryPool;
44 return gTextureMemoryPool;
49 TextureKey Texture::NewKey(Type type, Pixel::Format format, ImageDimensions size)
51 void* ptr = GetTextureMemoryPool().AllocateRawThreadSafe();
52 auto key = GetTextureMemoryPool().GetKeyFromPtr(static_cast<Texture*>(ptr));
53 new(ptr) Texture(type, format, size);
55 return TextureKey(key);
58 TextureKey Texture::NewKey(NativeImageInterfacePtr nativeImageInterface)
60 void* ptr = GetTextureMemoryPool().AllocateRawThreadSafe();
61 auto key = GetTextureMemoryPool().GetKeyFromPtr(static_cast<Texture*>(ptr));
62 new(ptr) Texture(nativeImageInterface);
64 return TextureKey(key);
67 TextureKey Texture::NewKey(Type type, uint32_t resourceId)
69 void* ptr = GetTextureMemoryPool().AllocateRawThreadSafe();
70 auto key = GetTextureMemoryPool().GetKeyFromPtr(static_cast<Texture*>(ptr));
71 new(ptr) Texture(type, resourceId);
73 return TextureKey(key);
76 Texture::Texture(Type type, Pixel::Format format, ImageDimensions size)
77 : mGraphicsController(nullptr),
78 mRenderManager(nullptr),
79 mGraphicsTexture(nullptr),
83 mWidth(size.GetWidth()),
84 mHeight(size.GetHeight()),
87 mHasAlpha(HasAlpha(format)),
89 mUseUploadedParameter(mWidth == 0u && mHeight == 0u && mPixelFormat == Pixel::INVALID)
93 Texture::Texture(NativeImageInterfacePtr nativeImageInterface)
94 : mGraphicsController(nullptr),
95 mRenderManager(nullptr),
96 mGraphicsTexture(nullptr),
97 mNativeImage(nativeImageInterface),
99 mPixelFormat(Pixel::RGBA8888),
100 mWidth(static_cast<uint16_t>(nativeImageInterface->GetWidth())), // ignoring overflow, not happening in practice
101 mHeight(static_cast<uint16_t>(nativeImageInterface->GetHeight())), // ignoring overflow, not happening in practice
103 mType(TextureType::TEXTURE_2D),
104 mHasAlpha(nativeImageInterface->RequiresBlending()),
106 mUseUploadedParameter(false)
110 Texture::Texture(Type type, uint32_t resourceId)
111 : mGraphicsController(nullptr),
112 mRenderManager(nullptr),
113 mGraphicsTexture(nullptr),
116 mPixelFormat(Pixel::INVALID),
119 mResourceId(resourceId),
121 mHasAlpha(true), ///< Since we don't assume what kind of texture will be uploaded in this case.
123 mUseUploadedParameter(true)
127 Texture::~Texture() = default;
129 void Texture::operator delete(void* ptr)
131 GetTextureMemoryPool().FreeThreadSafe(static_cast<Texture*>(ptr));
134 Render::Texture* Texture::Get(TextureKey::KeyType key)
136 return GetTextureMemoryPool().GetPtrFromKey(key);
139 void Texture::Initialize(Graphics::Controller& graphicsController, SceneGraph::RenderManager& renderManager)
141 mGraphicsController = &graphicsController;
142 mRenderManager = &renderManager;
145 Create(static_cast<uint32_t>(Graphics::TextureUsageFlagBits::SAMPLE));
148 if(mResourceId != 0u && mGraphicsController->GetTextureFromResourceId(mResourceId))
150 // Take ownership from graphics controller
151 mGraphicsTexture = std::move(mGraphicsController->ReleaseTextureFromResourceId(mResourceId));
152 // Now we take on the Graphics::Texture ownership. We don't need to keep mResourceId anymore.
157 void Texture::Destroy()
159 if(mResourceId != 0u && mGraphicsTexture.get() == nullptr)
161 // We still don't take texture ownership from controller before.
162 // Discard graphics object now.
163 mGraphicsController->DiscardTextureFromResourceId(mResourceId);
167 mGraphicsTexture.reset();
170 Graphics::Texture* Texture::GetGraphicsObject()
172 if(mResourceId != 0u && mGraphicsTexture.get() == nullptr)
174 // The ownership of Graphics::Texture is on Graphics::Controller. Ask to controller.
175 Graphics::Texture* graphicsTexturePtr = nullptr;
176 if(mGraphicsController)
178 graphicsTexturePtr = mGraphicsController->GetTextureFromResourceId(mResourceId);
180 if(graphicsTexturePtr)
182 // Take ownership from graphics controller
183 mGraphicsTexture = std::move(mGraphicsController->ReleaseTextureFromResourceId(mResourceId));
184 // (Partial update) Do not make mResourceId as 0 until we update mLatestUsedGraphicsTexture.
185 NotifyTextureUpdated();
188 DALI_LOG_INFO(gTextureFilter, Debug::General, "Render::Texture(%p)::GetGraphicsObject() = %p [with resource id : %u]\n", this, graphicsTexturePtr, mResourceId);
190 return graphicsTexturePtr;
194 DALI_LOG_INFO(gTextureFilter, Debug::General, "Render::Texture(%p)::GetGraphicsObject() = %p\n", this, mGraphicsTexture.get());
196 return mGraphicsTexture.get();
200 void Texture::Create(Graphics::TextureUsageFlags usage)
202 DALI_ASSERT_ALWAYS(mResourceId == 0u);
204 Create(usage, Graphics::TextureAllocationPolicy::CREATION);
207 void Texture::Create(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy)
209 CreateWithData(usage, allocationPolicy, nullptr, 0u);
212 void Texture::CreateWithData(Graphics::TextureUsageFlags usage, Graphics::TextureAllocationPolicy allocationPolicy, uint8_t* data, uint32_t size)
214 auto createInfo = Graphics::TextureCreateInfo();
216 .SetTextureType(Dali::Graphics::ConvertTextureType(mType))
217 .SetUsageFlags(usage)
218 .SetFormat(Dali::Graphics::ConvertPixelFormat(mPixelFormat))
219 .SetSize({mWidth, mHeight})
220 .SetLayout(Graphics::TextureLayout::LINEAR)
221 .SetAllocationPolicy(allocationPolicy)
224 .SetNativeImage(mNativeImage)
225 .SetMipMapFlag(Graphics::TextureMipMapFlag::DISABLED);
227 mGraphicsTexture = mGraphicsController->CreateTexture(createInfo, std::move(mGraphicsTexture));
230 void Texture::Upload(PixelDataPtr pixelData, const Graphics::UploadParams& params)
232 DALI_ASSERT_ALWAYS(!mNativeImage);
233 DALI_ASSERT_ALWAYS(mResourceId == 0u);
235 const uint32_t srcStride = pixelData->GetStride();
236 uint32_t srcOffset = 0u;
237 uint32_t srcSize = pixelData->GetBufferSize();
239 // Cache uploaded data
240 const auto uploadedDataWidth = params.dataWidth;
241 const auto uploadedDataHeight = params.dataHeight;
242 const auto uploadedPixelFormat = pixelData->GetPixelFormat();
244 const bool requiredSubPixelData = (!Pixel::IsCompressed(uploadedPixelFormat)) &&
245 ((params.dataXOffset != 0) ||
246 (params.dataYOffset != 0) ||
247 (uploadedDataWidth != pixelData->GetWidth()) ||
248 (uploadedDataHeight != pixelData->GetHeight()));
250 if(requiredSubPixelData)
253 * TextureUpdateInfo use byte scaled offset / size.
255 * To make we only use sub-data of inputed PixelData, make srcOffset as 'start of SubPixelData.
257 * |---- dataStrideByte -----|
258 * |-----| <-- dataXOffsetByte
259 * ...........................
260 * ......A-----------+........
263 * ......+-----------+C.......
264 * ......B....................
266 * A) Start of SubPixelData. offsetByte = dataStrideByte * dataYOffset + dataXOffsetByte.
267 * B) offsetByte = A).offsetByte + dataStrideByte * dataHeight. Note, It can be out of original PixelData boundary.
268 * C) End of SubPixelData. offsetByte = B).offsetByte - dataStrideByte + dataWidthByte.
270 * srcOffset = A).offsetByte;
271 * srcSize = ( C).offsetByte - A).offsetByte );
273 const uint32_t bytePerPixel = Pixel::GetBytesPerPixel(uploadedPixelFormat);
274 const uint32_t dataStrideByte = (srcStride ? srcStride : pixelData->GetWidth()) * bytePerPixel;
275 const uint32_t dataXOffsetByte = params.dataXOffset * bytePerPixel;
276 const uint32_t dataWidthByte = static_cast<uint32_t>(uploadedDataWidth) * bytePerPixel;
278 srcOffset = params.dataYOffset * dataStrideByte + dataXOffsetByte;
279 srcSize = static_cast<uint32_t>(uploadedDataHeight) * dataStrideByte - (dataStrideByte - dataWidthByte);
282 // Update render texture value as uploaded data.
283 if(mUseUploadedParameter)
285 mWidth = params.xOffset + uploadedDataWidth;
286 mHeight = params.yOffset + uploadedDataHeight;
287 mPixelFormat = uploadedPixelFormat;
290 // Always create new graphics texture object if we use uploaded parameter as texture.
291 if(!mGraphicsTexture || mUseUploadedParameter)
293 const bool isSubImage(params.xOffset != 0u || params.yOffset != 0u ||
294 uploadedDataWidth != (mWidth >> params.mipmap) ||
295 uploadedDataHeight != (mHeight >> params.mipmap));
296 Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE), isSubImage ? Graphics::TextureAllocationPolicy::CREATION : Graphics::TextureAllocationPolicy::UPLOAD);
299 Graphics::TextureUpdateInfo info{};
301 info.dstTexture = mGraphicsTexture.get();
302 info.dstOffset2D = {params.xOffset, params.yOffset};
303 info.layer = params.layer;
304 info.level = params.mipmap;
305 info.srcReference = 0;
306 info.srcExtent2D = {uploadedDataWidth, uploadedDataHeight};
307 info.srcOffset = srcOffset;
308 info.srcSize = srcSize;
309 info.srcStride = srcStride;
310 info.srcFormat = Dali::Graphics::ConvertPixelFormat(uploadedPixelFormat);
312 mUpdatedArea = Rect<uint16_t>(params.xOffset, params.yOffset, params.width, params.height);
314 Graphics::TextureUpdateSourceInfo updateSourceInfo{};
315 updateSourceInfo.sourceType = Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA;
316 updateSourceInfo.pixelDataSource.pixelData = Dali::PixelData(pixelData.Get());
318 mGraphicsController->UpdateTextures({info}, {updateSourceInfo});
323 bool Texture::HasAlphaChannel() const
325 bool alpha = mHasAlpha;
328 alpha = mNativeImage->RequiresBlending();
333 bool Texture::IsGraphicsObjectChanged()
335 return mLatestUsedGraphicsTexture != GetGraphicsObject();
338 void Texture::NotifyTextureUpdated()
342 mRenderManager->SetTextureUpdated(TextureKey(GetTextureMemoryPool().GetKeyFromPtr(this)));
346 void Texture::GenerateMipmaps()
348 DALI_ASSERT_ALWAYS(mResourceId == 0u);
350 // Compressed pixel doesn't support mipmap generation.
351 if(Pixel::IsCompressed(mPixelFormat))
356 if(!mGraphicsTexture)
358 Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE));
361 mGraphicsController->GenerateTextureMipmaps(*mGraphicsTexture.get());
364 void Texture::OnRenderFinished()
368 if(mResourceId != 0u)
370 mLatestUsedGraphicsTexture = GetGraphicsObject();
371 if(mLatestUsedGraphicsTexture != nullptr)
373 // We don't need to keep mResourceId anymore.
378 mUpdatedArea = Rect<uint16_t>{0, 0, 0, 0};
381 Rect<uint16_t> Texture::GetUpdatedArea()
385 Rect<uint32_t> rect = mNativeImage->GetUpdatedArea();
386 mUpdatedArea.x = static_cast<uint16_t>(rect.x);
387 mUpdatedArea.y = static_cast<uint16_t>(rect.y);
388 mUpdatedArea.width = static_cast<uint16_t>(rect.width);
389 mUpdatedArea.height = static_cast<uint16_t>(rect.height);
394 } // namespace Render
396 } // namespace Internal