0fb1cecc43bb311b53742a7ca6ac03125c9730d8
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali/internal/render/renderers/render-texture.h>
19
20 // EXTERNAL INCLUDES
21 #include <math.h> //floor, log2
22
23 // INTERNAL INCLUDES
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace Render
31 {
32 namespace
33 {
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gTextureFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TEXTURE");
36 #endif
37
38 /**
39  * Converts DALi pixel format to Graphics::Format
40  * @param format
41  * @return
42  */
43 constexpr Graphics::Format ConvertPixelFormat(Pixel::Format format)
44 {
45   switch(format)
46   {
47     case Pixel::INVALID:
48       return Graphics::Format::UNDEFINED;
49     case Pixel::A8:
50       return Graphics::Format::R8_UNORM;
51
52     case Pixel::L8:
53       return Graphics::Format::L8;
54     case Pixel::LA88:
55       return Graphics::Format::L8A8;
56     case Pixel::RGB565:
57       return Graphics::Format::R5G6B5_UNORM_PACK16;
58     case Pixel::BGR565:
59       return Graphics::Format::B5G6R5_UNORM_PACK16;
60     case Pixel::RGBA4444:
61       return Graphics::Format::R4G4B4A4_UNORM_PACK16;
62
63     case Pixel::BGRA4444:
64       return Graphics::Format::B4G4R4A4_UNORM_PACK16;
65     case Pixel::RGBA5551:
66       return Graphics::Format::R5G5B5A1_UNORM_PACK16;
67     case Pixel::BGRA5551:
68       return Graphics::Format::B5G5R5A1_UNORM_PACK16;
69     case Pixel::RGB888:
70       return Graphics::Format::R8G8B8_UNORM;
71     case Pixel::RGB8888:
72       return Graphics::Format::R8G8B8A8_UNORM;
73     case Pixel::BGR8888:
74       return Graphics::Format::B8G8R8A8_UNORM;
75     case Pixel::RGBA8888:
76       return Graphics::Format::R8G8B8A8_UNORM;
77     case Pixel::BGRA8888:
78       return Graphics::Format::B8G8R8A8_UNORM;
79
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;
86
87     // EAC
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;
96
97     // ETC
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;
102
103     case Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
104       return Graphics::Format::ETC2_R8G8B8A1_UNORM_BLOCK; // no 'punchthrough' format
105
106     case Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
107       return Graphics::Format::ETC2_R8G8B8A1_SRGB_BLOCK; // no 'punchthrough' format
108
109     case Pixel::COMPRESSED_RGBA8_ETC2_EAC:
110       return Graphics::Format::ETC2_R8G8B8A8_UNORM_BLOCK;
111
112     case Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
113       return Graphics::Format::ETC2_R8G8B8A8_SRGB_BLOCK;
114
115     case Pixel::COMPRESSED_RGB8_ETC1:
116       return Graphics::Format::ETC2_R8G8B8_UNORM_BLOCK; // doesn't seem to be supported at all
117
118     case Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
119       return Graphics::Format::PVRTC1_4BPP_UNORM_BLOCK_IMG; // or SRGB?
120
121     // ASTC
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
152       return Graphics::Format::ASTC_4x4_SRGB_BLOCK; // not type with alpha, but likely to use SRGB
153     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
154       return Graphics::Format::ASTC_5x4_SRGB_BLOCK;
155     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
156       return Graphics::Format::ASTC_5x5_SRGB_BLOCK;
157     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
158       return Graphics::Format::ASTC_6x5_SRGB_BLOCK;
159     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
160       return Graphics::Format::ASTC_6x6_SRGB_BLOCK;
161     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
162       return Graphics::Format::ASTC_8x5_SRGB_BLOCK;
163     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
164       return Graphics::Format::ASTC_8x6_UNORM_BLOCK;
165     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
166       return Graphics::Format::ASTC_8x8_SRGB_BLOCK;
167     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
168       return Graphics::Format::ASTC_10x5_SRGB_BLOCK;
169     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
170       return Graphics::Format::ASTC_10x6_SRGB_BLOCK;
171     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
172       return Graphics::Format::ASTC_10x8_SRGB_BLOCK;
173     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
174       return Graphics::Format::ASTC_10x10_SRGB_BLOCK;
175     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
176       return Graphics::Format::ASTC_12x10_SRGB_BLOCK;
177     case Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
178       return Graphics::Format::ASTC_12x12_SRGB_BLOCK;
179     case Pixel::RGB16F:
180       return Graphics::Format::R16G16B16_SFLOAT;
181     case Pixel::RGB32F:
182       return Graphics::Format::R32G32B32_SFLOAT;
183     case Pixel::R11G11B10F:
184       return Graphics::Format::R11G11B10_UFLOAT_PACK32;
185   }
186   return Graphics::Format::UNDEFINED;
187 }
188
189 constexpr Graphics::TextureType ConvertType(Texture::Type type)
190 {
191   switch(type)
192   {
193     case TextureType::TEXTURE_2D:
194       return Graphics::TextureType::TEXTURE_2D;
195     case TextureType::TEXTURE_CUBE:
196       return Graphics::TextureType::TEXTURE_CUBEMAP;
197   }
198   return Graphics::TextureType::TEXTURE_2D;
199 }
200
201 } //Unnamed namespace
202
203 Texture::Texture(Type type, Pixel::Format format, ImageDimensions size)
204 : mGraphicsController(nullptr),
205   mGraphicsTexture(nullptr),
206   mNativeImage(),
207   mSampler(),
208   mPixelFormat(format),
209   mWidth(size.GetWidth()),
210   mHeight(size.GetHeight()),
211   mType(type),
212   mHasAlpha(HasAlpha(format))
213 {
214 }
215
216 Texture::Texture(NativeImageInterfacePtr nativeImageInterface)
217 : mGraphicsController(nullptr),
218   mGraphicsTexture(nullptr),
219   mNativeImage(nativeImageInterface),
220   mSampler(),
221   mPixelFormat(Pixel::RGBA8888),
222   mWidth(static_cast<uint16_t>(nativeImageInterface->GetWidth())),   // ignoring overflow, not happening in practice
223   mHeight(static_cast<uint16_t>(nativeImageInterface->GetHeight())), // ignoring overflow, not happening in practice
224   mType(TextureType::TEXTURE_2D),
225   mHasAlpha(nativeImageInterface->RequiresBlending())
226 {
227 }
228
229 Texture::~Texture() = default;
230
231 void Texture::Initialize(Graphics::Controller& graphicsController)
232 {
233   mGraphicsController = &graphicsController;
234   if(mNativeImage)
235   {
236     Create(static_cast<uint32_t>(Graphics::TextureUsageFlagBits::SAMPLE));
237   }
238 }
239
240 void Texture::Destroy()
241 {
242   mGraphicsTexture.reset();
243 }
244
245 Graphics::Texture* Texture::GetGraphicsObject() const
246 {
247   DALI_LOG_INFO(gTextureFilter, Debug::General, "SC::Texture(%p)::GetGraphicsObject() = %p\n", this, mGraphicsTexture.get());
248
249   return mGraphicsTexture.get();
250 }
251
252 void Texture::Create(Graphics::TextureUsageFlags usage)
253 {
254   CreateWithData(usage, nullptr, 0u);
255 }
256
257 void Texture::CreateWithData(Graphics::TextureUsageFlags usage, uint8_t* data, uint32_t size)
258 {
259   auto createInfo = Graphics::TextureCreateInfo();
260   createInfo
261     .SetTextureType(ConvertType(mType))
262     .SetUsageFlags(usage)
263     .SetFormat(ConvertPixelFormat(mPixelFormat))
264     .SetSize({mWidth, mHeight})
265     .SetLayout(Graphics::TextureLayout::LINEAR)
266     .SetData(data)
267     .SetDataSize(size)
268     .SetNativeImage(mNativeImage)
269     .SetMipMapFlag(Graphics::TextureMipMapFlag::DISABLED);
270
271   mGraphicsTexture = mGraphicsController->CreateTexture(createInfo, std::move(mGraphicsTexture));
272 }
273
274 void Texture::Upload(PixelDataPtr pixelData, const Internal::Texture::UploadParams& params)
275 {
276   DALI_ASSERT_ALWAYS(!mNativeImage);
277
278   if(!mGraphicsTexture)
279   {
280     Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE));
281   }
282
283   Graphics::TextureUpdateInfo info{};
284   info.dstTexture   = mGraphicsTexture.get();
285   info.dstOffset2D  = {params.xOffset, params.yOffset};
286   info.layer        = params.layer;
287   info.level        = params.mipmap;
288   info.srcReference = 0;
289   info.srcExtent2D  = {params.width, params.height};
290   info.srcOffset    = 0;
291   info.srcSize      = pixelData->GetBufferSize();
292   info.srcFormat    = ConvertPixelFormat(pixelData->GetPixelFormat());
293
294   Graphics::TextureUpdateSourceInfo updateSourceInfo{};
295   updateSourceInfo.sourceType          = Graphics::TextureUpdateSourceInfo::Type::MEMORY;
296   updateSourceInfo.memorySource.memory = pixelData->GetBuffer();
297
298   mGraphicsController->UpdateTextures({info}, {updateSourceInfo});
299 }
300
301 bool Texture::HasAlphaChannel() const
302 {
303   bool alpha = mHasAlpha;
304   if(mNativeImage)
305   {
306     alpha = mNativeImage->RequiresBlending();
307   }
308   return alpha;
309 }
310
311 void Texture::GenerateMipmaps()
312 {
313   if(!mGraphicsTexture)
314   {
315     Create(static_cast<Graphics::TextureUsageFlags>(Graphics::TextureUsageFlagBits::SAMPLE));
316   }
317
318   mGraphicsController->GenerateTextureMipmaps(*mGraphicsTexture.get());
319 }
320
321 } // namespace Render
322
323 } // namespace Internal
324
325 } // namespace Dali