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.
19 #include "gles-graphics-texture.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
28 #include "egl-graphics-controller.h"
29 #include "gles-graphics-sampler.h"
30 #include "gles-graphics-types.h"
34 // These match the GL specification
35 //const int32_t GL_MINIFY_DEFAULT = GL_NEAREST_MIPMAP_LINEAR;
36 //const int32_t GL_MAGNIFY_DEFAULT = GL_LINEAR;
37 const int32_t GL_WRAP_DEFAULT = GL_CLAMP_TO_EDGE;
39 // These are the Dali defaults
40 const int32_t DALI_MINIFY_DEFAULT = GL_LINEAR;
41 const int32_t DALI_MAGNIFY_DEFAULT = GL_LINEAR;
44 namespace Dali::Graphics::GLES
46 struct ColorConversion
50 std::vector<uint8_t> (*pConversionFunc)(const void*, uint32_t, uint32_t, uint32_t, uint32_t);
51 void (*pConversionWriteFunc)(const void*, uint32_t, uint32_t, uint32_t, uint32_t, void*);
54 inline void WriteRGB32ToRGBA32(const void* __restrict__ pData, uint32_t sizeInBytes, uint32_t width, uint32_t height, uint32_t rowStride, void* __restrict__ pOutput)
56 const uint8_t* __restrict__ inData = reinterpret_cast<const uint8_t*>(pData);
57 uint8_t* __restrict__ outData = reinterpret_cast<uint8_t*>(pOutput);
62 for(auto y = 0u; y < height; ++y)
66 for(auto x = 0u; x < width; ++x)
68 outData[outIdx] = inData[inIdx];
69 outData[outIdx + 1] = inData[inIdx + 1];
70 outData[outIdx + 2] = inData[inIdx + 2];
71 outData[outIdx + 3] = 0xff;
75 inData += rowStride * 3u;
76 outData += width * 4u;
81 * Converts RGB to RGBA
83 inline std::vector<uint8_t> ConvertRGB32ToRGBA32(const void* pData, uint32_t sizeInBytes, uint32_t width, uint32_t height, uint32_t rowStride)
85 std::vector<uint8_t> rgbaBuffer{};
86 rgbaBuffer.resize(width * height * 4);
87 WriteRGB32ToRGBA32(pData, sizeInBytes, width, height, rowStride, &rgbaBuffer[0]);
92 * Format conversion table
94 static const std::vector<ColorConversion> COLOR_CONVERSION_TABLE = {
95 {Format::R8G8B8_UNORM, Format::R8G8B8A8_UNORM, ConvertRGB32ToRGBA32, WriteRGB32ToRGBA32}};
100 Texture::Texture(const Graphics::TextureCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
101 : TextureResource(createInfo, controller)
103 // If there is any data, move it into staging buffer
104 if(mCreateInfo.data && mCreateInfo.dataSize)
106 mStagingBuffer.resize(size_t(mCreateInfo.dataSize));
107 std::copy(reinterpret_cast<char*>(mCreateInfo.data),
108 reinterpret_cast<char*>(mCreateInfo.data) + mCreateInfo.dataSize,
109 mStagingBuffer.begin());
112 // Add texture to the Resource queue
113 mController.AddTexture(*this);
116 bool Texture::InitializeResource()
118 if(mCreateInfo.nativeImagePtr)
120 return InitializeNativeImage();
122 return InitializeTexture();
125 bool Texture::InitializeNativeImage()
127 auto context = mController.GetCurrentContext();
128 auto gl = mController.GetGL();
133 // Do nothing during shutdown
137 NativeImageInterfacePtr nativeImage = mCreateInfo.nativeImagePtr;
138 bool created = nativeImage->CreateResource();
139 mGlTarget = nativeImage->GetTextureTarget();
142 gl->GenTextures(1, &texture);
143 context->BindTexture(mGlTarget, GetTextureTypeId(), texture);
145 gl->PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
147 // Apply default sampling parameters
148 SetSamplerParameter(GL_TEXTURE_MIN_FILTER, mDefaultSamplerState.minFilter, DALI_MINIFY_DEFAULT);
149 SetSamplerParameter(GL_TEXTURE_MAG_FILTER, mDefaultSamplerState.magFilter, DALI_MAGNIFY_DEFAULT);
150 SetSamplerParameter(GL_TEXTURE_WRAP_S, mDefaultSamplerState.wrapS, GL_WRAP_DEFAULT);
151 SetSamplerParameter(GL_TEXTURE_WRAP_T, mDefaultSamplerState.wrapT, GL_WRAP_DEFAULT);
153 // platform specific implementation decides on what GL extension to use
154 if(nativeImage->TargetTexture() != 0u)
156 gl->DeleteTextures(1, &texture);
157 nativeImage->DestroyResource();
163 mTextureId = texture;
168 DALI_LOG_ERROR("Native Image: InitializeNativeImage, CreateResource() failed\n");
171 return created; // WARNING! May be false! Needs handling! (Well, initialized on bind)
174 bool Texture::InitializeTexture()
176 auto context = mController.GetCurrentContext();
177 auto gl = mController.GetGL();
180 // Do nothing during shutdown
186 mGlTarget = GLTextureTarget(mCreateInfo.textureType).target;
187 mIsCompressed = Graphics::GLES::FormatCompression(mCreateInfo.format).compressed;
189 switch(mCreateInfo.textureType)
192 case Graphics::TextureType::TEXTURE_2D:
194 Graphics::GLES::GLTextureFormatType format(mCreateInfo.format);
196 // TODO: find better condition, with this test the L8 doesn't work
197 if(1) //format.format && format.type)
200 gl->GenTextures(1, &texture);
201 context->BindTexture(GL_TEXTURE_2D, GetTextureTypeId(), texture);
203 // Allocate memory for the texture
206 gl->TexImage2D(GL_TEXTURE_2D,
208 format.internalFormat,
209 mCreateInfo.size.width,
210 mCreateInfo.size.height,
214 (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
218 gl->CompressedTexImage2D(GL_TEXTURE_2D,
220 format.internalFormat,
221 mCreateInfo.size.width,
222 mCreateInfo.size.height,
224 mCreateInfo.dataSize,
225 (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
228 // Clear staging buffer if there was any
229 mStagingBuffer.clear();
230 mTextureId = texture;
231 // Default texture filtering (to be set later via command buffer binding)
232 SetSamplerParameter(GL_TEXTURE_MIN_FILTER, mDefaultSamplerState.minFilter, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
233 SetSamplerParameter(GL_TEXTURE_MAG_FILTER, mDefaultSamplerState.magFilter, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
234 SetSamplerParameter(GL_TEXTURE_WRAP_S, mDefaultSamplerState.wrapS, GL_WRAP_DEFAULT);
235 SetSamplerParameter(GL_TEXTURE_WRAP_T, mDefaultSamplerState.wrapT, GL_WRAP_DEFAULT);
240 case Graphics::TextureType::TEXTURE_CUBEMAP:
242 Graphics::GLES::GLTextureFormatType format(mCreateInfo.format);
244 if(format.format && format.type)
247 gl->GenTextures(1, &texture);
248 context->BindTexture(GL_TEXTURE_CUBE_MAP, GetTextureTypeId(), texture);
249 gl->PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
251 SetSamplerParameter(GL_TEXTURE_MIN_FILTER, mDefaultSamplerState.minFilter, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
252 SetSamplerParameter(GL_TEXTURE_MAG_FILTER, mDefaultSamplerState.magFilter, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
253 SetSamplerParameter(GL_TEXTURE_WRAP_S, mDefaultSamplerState.wrapS, GL_WRAP_DEFAULT);
254 SetSamplerParameter(GL_TEXTURE_WRAP_T, mDefaultSamplerState.wrapT, GL_WRAP_DEFAULT);
256 // Allocate memory for the texture
257 for(uint32_t i = 0; i < 6; ++i)
261 gl->TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
263 format.internalFormat,
264 mCreateInfo.size.width,
265 mCreateInfo.size.height,
269 (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
273 gl->CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
275 format.internalFormat,
276 mCreateInfo.size.width,
277 mCreateInfo.size.height,
279 mCreateInfo.dataSize,
280 (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
284 // Clear staging buffer if there was any
285 mStagingBuffer.clear();
287 mTextureId = texture;
289 SetSamplerParameter(GL_TEXTURE_WRAP_R, mDefaultSamplerState.wrapR, GL_WRAP_DEFAULT);
301 void Texture::DestroyResource()
303 auto gl = mController.GetGL();
309 // This is a proper destructor
312 gl->DeleteTextures(1, &mTextureId);
314 if(mCreateInfo.nativeImagePtr)
316 mCreateInfo.nativeImagePtr->DestroyResource();
320 void Texture::DiscardResource()
322 mController.DiscardResource(this);
325 void Texture::Bind(const TextureBinding& binding) const
327 auto context = mController.GetCurrentContext();
328 auto gl = mController.GetGL();
331 // Do nothing during shutdown
335 context->ActiveTexture(binding.binding);
336 context->BindTexture(mGlTarget, GetTextureTypeId(), mTextureId);
338 // For GLES2 if there is a sampler set in the binding
341 const auto& samplerCreateInfo = static_cast<const GLES::Sampler*>(binding.sampler)->GetCreateInfo();
343 auto mipMapMode = samplerCreateInfo.mipMapMode;
345 SetSamplerParameter(GL_TEXTURE_MIN_FILTER, mDefaultSamplerState.minFilter, GLSamplerFilterAndMipMapMode(samplerCreateInfo.minFilter, mipMapMode).glFilter);
346 SetSamplerParameter(GL_TEXTURE_MAG_FILTER, mDefaultSamplerState.magFilter, GLSamplerFilter(samplerCreateInfo.magFilter).glFilter);
347 SetSamplerParameter(GL_TEXTURE_WRAP_S, mDefaultSamplerState.wrapS, GLAddressMode(samplerCreateInfo.addressModeU).texParameter);
348 SetSamplerParameter(GL_TEXTURE_WRAP_T, mDefaultSamplerState.wrapT, GLAddressMode(samplerCreateInfo.addressModeV).texParameter);
350 if(mGlTarget == GL_TEXTURE_CUBE_MAP)
352 SetSamplerParameter(GL_TEXTURE_WRAP_R, mDefaultSamplerState.wrapR, GLAddressMode(samplerCreateInfo.addressModeW).texParameter);
357 SetSamplerParameter(GL_TEXTURE_MIN_FILTER, mDefaultSamplerState.minFilter, DALI_MINIFY_DEFAULT);
358 SetSamplerParameter(GL_TEXTURE_MAG_FILTER, mDefaultSamplerState.magFilter, DALI_MAGNIFY_DEFAULT);
359 SetSamplerParameter(GL_TEXTURE_WRAP_S, mDefaultSamplerState.wrapS, GL_WRAP_DEFAULT);
360 SetSamplerParameter(GL_TEXTURE_WRAP_T, mDefaultSamplerState.wrapT, GL_WRAP_DEFAULT);
361 if(mGlTarget == GL_TEXTURE_CUBE_MAP)
363 SetSamplerParameter(GL_TEXTURE_WRAP_R, mDefaultSamplerState.wrapR, GL_WRAP_DEFAULT);
369 SetSamplerParameter(GL_TEXTURE_MAX_LEVEL, mDefaultSamplerState.maxLevel, mMaxMipMapLevel);
373 void Texture::Prepare()
375 NativeImageInterfacePtr nativeImage = mCreateInfo.nativeImagePtr;
378 if(nativeImage->SourceChanged())
381 uint32_t width = mCreateInfo.nativeImagePtr->GetWidth();
382 uint32_t height = mCreateInfo.nativeImagePtr->GetHeight();
383 mCreateInfo.SetSize({width, height}); // Size may change
386 nativeImage->PrepareTexture();
391 * This function tests whether format is supported by the driver. If possible it applies
392 * format conversion to suitable supported pixel format.
394 bool Texture::TryConvertPixelData(const void* pData, Graphics::Format srcFormat, Graphics::Format destFormat, uint32_t sizeInBytes, uint32_t inStride, uint32_t width, uint32_t height, std::vector<uint8_t>& outputBuffer)
396 // No need to convert
397 if(srcFormat == destFormat)
402 auto it = std::find_if(COLOR_CONVERSION_TABLE.begin(), COLOR_CONVERSION_TABLE.end(), [&](auto& item) {
403 return item.srcFormat == srcFormat && item.destFormat == destFormat;
406 // No suitable format, return empty array
407 if(it == COLOR_CONVERSION_TABLE.end())
411 auto begin = reinterpret_cast<const uint8_t*>(pData);
413 outputBuffer = std::move(it->pConversionFunc(begin, sizeInBytes, width, height, inStride));
414 return !outputBuffer.empty();
417 void Texture::SetSamplerParameter(uint32_t param, uint32_t& cacheValue, uint32_t value) const
419 if(cacheValue != value)
421 auto gl = mController.GetGL();
422 gl->TexParameteri(mGlTarget, param, value);
427 } // namespace Dali::Graphics::GLES