2 * Copyright (c) 2022 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>
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gTextureFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TEXTURE");
39 * Converts DALi pixel format to Graphics::Format
43 constexpr Graphics::Format ConvertPixelFormat(Pixel::Format format)
48 return Graphics::Format::UNDEFINED;
50 return Graphics::Format::R8_UNORM;
53 return Graphics::Format::L8;
55 return Graphics::Format::L8A8;
57 return Graphics::Format::R5G6B5_UNORM_PACK16;
59 return Graphics::Format::B5G6R5_UNORM_PACK16;
61 return Graphics::Format::R4G4B4A4_UNORM_PACK16;
64 return Graphics::Format::B4G4R4A4_UNORM_PACK16;
66 return Graphics::Format::R5G5B5A1_UNORM_PACK16;
68 return Graphics::Format::B5G5R5A1_UNORM_PACK16;
70 return Graphics::Format::R8G8B8_UNORM;
72 return Graphics::Format::R8G8B8A8_UNORM;
74 return Graphics::Format::B8G8R8A8_UNORM;
76 return Graphics::Format::R8G8B8A8_UNORM;
78 return Graphics::Format::B8G8R8A8_UNORM;
80 case Pixel::DEPTH_UNSIGNED_INT:
81 return Graphics::Format::D16_UNORM;
82 case Pixel::DEPTH_FLOAT:
83 return Graphics::Format::D32_SFLOAT;
84 case Pixel::DEPTH_STENCIL:
85 return Graphics::Format::D24_UNORM_S8_UINT;
88 case Pixel::COMPRESSED_R11_EAC:
89 return Graphics::Format::EAC_R11_UNORM_BLOCK;
90 case Pixel::COMPRESSED_SIGNED_R11_EAC:
91 return Graphics::Format::EAC_R11_SNORM_BLOCK;
92 case Pixel::COMPRESSED_RG11_EAC:
93 return Graphics::Format::EAC_R11G11_UNORM_BLOCK;
94 case Pixel::COMPRESSED_SIGNED_RG11_EAC:
95 return Graphics::Format::EAC_R11G11_SNORM_BLOCK;
98 case Pixel::COMPRESSED_RGB8_ETC2:
99 return Graphics::Format::ETC2_R8G8B8_UNORM_BLOCK;
100 case Pixel::COMPRESSED_SRGB8_ETC2:
101 return Graphics::Format::ETC2_R8G8B8_SRGB_BLOCK;
103 case Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
104 return Graphics::Format::ETC2_R8G8B8A1_UNORM_BLOCK; // no 'punchthrough' format
106 case Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
107 return Graphics::Format::ETC2_R8G8B8A1_SRGB_BLOCK; // no 'punchthrough' format
109 case Pixel::COMPRESSED_RGBA8_ETC2_EAC:
110 return Graphics::Format::ETC2_R8G8B8A8_UNORM_BLOCK;
112 case Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
113 return Graphics::Format::ETC2_R8G8B8A8_SRGB_BLOCK;
115 case Pixel::COMPRESSED_RGB8_ETC1:
116 return Graphics::Format::ETC2_R8G8B8_UNORM_BLOCK; // doesn't seem to be supported at all
118 case Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
119 return Graphics::Format::PVRTC1_4BPP_UNORM_BLOCK_IMG; // or SRGB?
122 case Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
123 return Graphics::Format::ASTC_4x4_UNORM_BLOCK; // or SRGB?
124 case Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
125 return Graphics::Format::ASTC_5x4_UNORM_BLOCK;
126 case Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
127 return Graphics::Format::ASTC_5x5_UNORM_BLOCK;
128 case Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
129 return Graphics::Format::ASTC_6x5_UNORM_BLOCK;
130 case Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
131 return Graphics::Format::ASTC_6x6_UNORM_BLOCK;
132 case Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
133 return Graphics::Format::ASTC_8x5_UNORM_BLOCK;
134 case Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
135 return Graphics::Format::ASTC_8x6_UNORM_BLOCK;
136 case Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
137 return Graphics::Format::ASTC_8x8_UNORM_BLOCK;
138 case Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
139 return Graphics::Format::ASTC_10x5_UNORM_BLOCK;
140 case Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
141 return Graphics::Format::ASTC_10x6_UNORM_BLOCK;
142 case Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
143 return Graphics::Format::ASTC_10x8_UNORM_BLOCK;
144 case Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
145 return Graphics::Format::ASTC_10x10_UNORM_BLOCK;
146 case Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
147 return Graphics::Format::ASTC_12x10_UNORM_BLOCK;
148 case Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
149 return Graphics::Format::ASTC_12x12_UNORM_BLOCK;
150 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
151 return Graphics::Format::ASTC_4x4_SRGB_BLOCK; // not type with alpha, but likely to use SRGB
152 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
153 return Graphics::Format::ASTC_5x4_SRGB_BLOCK;
154 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
155 return Graphics::Format::ASTC_5x5_SRGB_BLOCK;
156 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
157 return Graphics::Format::ASTC_6x5_SRGB_BLOCK;
158 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
159 return Graphics::Format::ASTC_6x6_SRGB_BLOCK;
160 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
161 return Graphics::Format::ASTC_8x5_SRGB_BLOCK;
162 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
163 return Graphics::Format::ASTC_8x6_UNORM_BLOCK;
164 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
165 return Graphics::Format::ASTC_8x8_SRGB_BLOCK;
166 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
167 return Graphics::Format::ASTC_10x5_SRGB_BLOCK;
168 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
169 return Graphics::Format::ASTC_10x6_SRGB_BLOCK;
170 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
171 return Graphics::Format::ASTC_10x8_SRGB_BLOCK;
172 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
173 return Graphics::Format::ASTC_10x10_SRGB_BLOCK;
174 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
175 return Graphics::Format::ASTC_12x10_SRGB_BLOCK;
176 case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
177 return Graphics::Format::ASTC_12x12_SRGB_BLOCK;
180 return Graphics::Format::R16G16B16_SFLOAT;
182 return Graphics::Format::R32G32B32_SFLOAT;
183 case Pixel::R11G11B10F:
184 return Graphics::Format::R11G11B10_UFLOAT_PACK32;
186 case Pixel::CHROMINANCE_U:
187 return Graphics::Format::L8;
188 case Pixel::CHROMINANCE_V:
189 return Graphics::Format::L8;
191 return Graphics::Format::UNDEFINED;
194 constexpr Graphics::TextureType ConvertType(Texture::Type type)
198 case TextureType::TEXTURE_2D:
199 return Graphics::TextureType::TEXTURE_2D;
200 case TextureType::TEXTURE_CUBE:
201 return Graphics::TextureType::TEXTURE_CUBEMAP;
203 return Graphics::TextureType::TEXTURE_2D;
206 } //Unnamed namespace
208 Texture::Texture(Type type, Pixel::Format format, ImageDimensions size)
209 : mGraphicsController(nullptr),
210 mGraphicsTexture(nullptr),
213 mPixelFormat(format),
214 mWidth(size.GetWidth()),
215 mHeight(size.GetHeight()),
217 mHasAlpha(HasAlpha(format))
221 Texture::Texture(NativeImageInterfacePtr nativeImageInterface)
222 : mGraphicsController(nullptr),
223 mGraphicsTexture(nullptr),
224 mNativeImage(nativeImageInterface),
226 mPixelFormat(Pixel::RGBA8888),
227 mWidth(static_cast<uint16_t>(nativeImageInterface->GetWidth())), // ignoring overflow, not happening in practice
228 mHeight(static_cast<uint16_t>(nativeImageInterface->GetHeight())), // ignoring overflow, not happening in practice
229 mType(TextureType::TEXTURE_2D),
230 mHasAlpha(nativeImageInterface->RequiresBlending())
234 Texture::~Texture() = default;
236 void Texture::Initialize(Graphics::Controller& graphicsController)
238 mGraphicsController = &graphicsController;
241 Create(static_cast<uint32_t>(Graphics::TextureUsageFlagBits::SAMPLE));
245 void Texture::Destroy()
247 mGraphicsTexture.reset();
250 Graphics::Texture* Texture::GetGraphicsObject() const
252 DALI_LOG_INFO(gTextureFilter, Debug::General, "SC::Texture(%p)::GetGraphicsObject() = %p\n", this, mGraphicsTexture.get());
254 return mGraphicsTexture.get();
257 void Texture::Create(Graphics::TextureUsageFlags usage)
259 CreateWithData(usage, nullptr, 0u);
262 void Texture::CreateWithData(Graphics::TextureUsageFlags usage, uint8_t* data, uint32_t size)
264 auto createInfo = Graphics::TextureCreateInfo();
266 .SetTextureType(ConvertType(mType))
267 .SetUsageFlags(usage)
268 .SetFormat(ConvertPixelFormat(mPixelFormat))
269 .SetSize({mWidth, mHeight})
270 .SetLayout(Graphics::TextureLayout::LINEAR)
273 .SetNativeImage(mNativeImage)
274 .SetMipMapFlag(Graphics::TextureMipMapFlag::DISABLED);
276 mGraphicsTexture = mGraphicsController->CreateTexture(createInfo, std::move(mGraphicsTexture));
279 void Texture::Upload(PixelDataPtr pixelData, const Internal::Texture::UploadParams& params)
281 DALI_ASSERT_ALWAYS(!mNativeImage);
283 if(!mGraphicsTexture)
285 Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE));
288 Graphics::TextureUpdateInfo info{};
290 const uint32_t bytePerPixel = Pixel::GetBytesPerPixel(pixelData->GetPixelFormat());
291 const uint32_t srcStride = pixelData->GetStride();
292 uint32_t srcOffset = 0u;
293 uint32_t srcSize = pixelData->GetBufferSize();
295 const bool requiredSubPixelData = (!Pixel::IsCompressed(pixelData->GetPixelFormat())) &&
296 ((params.dataXOffset != 0) ||
297 (params.dataYOffset != 0) ||
298 (params.dataWidth != pixelData->GetWidth()) ||
299 (params.dataHeight != pixelData->GetHeight()));
301 if(requiredSubPixelData)
304 * TextureUpdateInfo use byte scaled offset / size.
306 * To make we only use sub-data of inputed PixelData, make srcOffset as 'start of SubPixelData.
308 * |---- dataStrideByte -----|
309 * |-----| <-- dataXOffsetByte
310 * ...........................
311 * ......A-----------+........
314 * ......+-----------+C.......
315 * ......B....................
317 * A) Start of SubPixelData. offsetByte = dataStrideByte * dataYOffset + dataXOffsetByte.
318 * B) offsetByte = A).offsetByte + dataStrideByte * dataHeight. Note, It can be out of original PixelData boundary.
319 * C) End of SubPixelData. offsetByte = B).offsetByte - dataStrideByte + dataWidthByte.
321 * srcOffset = A).offsetByte;
322 * srcSize = ( C).offsetByte - A).offsetByte );
324 const uint32_t dataStrideByte = (srcStride ? srcStride : static_cast<uint32_t>(params.dataWidth)) * bytePerPixel;
325 const uint32_t dataXOffsetByte = params.dataXOffset * bytePerPixel;
326 const uint32_t dataWidthByte = static_cast<uint32_t>(params.dataWidth) * bytePerPixel;
328 srcOffset = params.dataYOffset * dataStrideByte + dataXOffsetByte;
329 srcSize = static_cast<uint32_t>(params.dataHeight) * dataStrideByte - (dataStrideByte - dataWidthByte);
332 info.dstTexture = mGraphicsTexture.get();
333 info.dstOffset2D = {params.xOffset, params.yOffset};
334 info.layer = params.layer;
335 info.level = params.mipmap;
336 info.srcReference = 0;
337 info.srcExtent2D = {params.dataWidth, params.dataHeight};
338 info.srcOffset = srcOffset;
339 info.srcSize = srcSize;
340 info.srcStride = srcStride;
341 info.srcFormat = ConvertPixelFormat(pixelData->GetPixelFormat());
343 Graphics::TextureUpdateSourceInfo updateSourceInfo{};
344 updateSourceInfo.sourceType = Graphics::TextureUpdateSourceInfo::Type::MEMORY;
345 updateSourceInfo.memorySource.memory = pixelData->GetBuffer();
347 mGraphicsController->UpdateTextures({info}, {updateSourceInfo});
350 bool Texture::HasAlphaChannel() const
352 bool alpha = mHasAlpha;
355 alpha = mNativeImage->RequiresBlending();
360 void Texture::GenerateMipmaps()
362 if(!mGraphicsTexture)
364 Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE));
367 mGraphicsController->GenerateTextureMipmaps(*mGraphicsTexture.get());
370 } // namespace Render
372 } // namespace Internal