Merge "Add some properties in web engine" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-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
18 // CLASS HEADER
19 #include "gles-graphics-texture.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
25 #include <vector>
26
27 // INTERNAL INCLUDES
28 #include "egl-graphics-controller.h"
29 #include "gles-graphics-sampler.h"
30 #include "gles-graphics-types.h"
31
32 namespace
33 {
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;
38
39 // These are the Dali defaults
40 const int32_t DALI_MINIFY_DEFAULT  = GL_LINEAR;
41 const int32_t DALI_MAGNIFY_DEFAULT = GL_LINEAR;
42 } // namespace
43
44 namespace Dali::Graphics::GLES
45 {
46 Texture::Texture(const Graphics::TextureCreateInfo& createInfo, Graphics::EglGraphicsController& controller)
47 : TextureResource(createInfo, controller)
48 {
49   // If there is any data, move it into staging buffer
50   if(mCreateInfo.data && mCreateInfo.dataSize)
51   {
52     mStagingBuffer.resize(size_t(mCreateInfo.dataSize));
53     std::copy(reinterpret_cast<char*>(mCreateInfo.data),
54               reinterpret_cast<char*>(mCreateInfo.data) + mCreateInfo.dataSize,
55               mStagingBuffer.begin());
56   }
57
58   // Add texture to the Resource queue
59   mController.AddTexture(*this);
60 }
61
62 bool Texture::InitializeResource()
63 {
64   if(mCreateInfo.nativeImagePtr)
65   {
66     return InitializeNativeImage();
67   }
68   return InitializeTexture();
69 }
70
71 bool Texture::InitializeNativeImage()
72 {
73   auto   gl = mController.GetGL();
74   GLuint texture{0};
75
76   NativeImageInterfacePtr nativeImage = mCreateInfo.nativeImagePtr;
77   bool                    created     = nativeImage->CreateResource();
78   mGlTarget                           = nativeImage->GetTextureTarget();
79   if(created)
80   {
81     DALI_LOG_RELEASE_INFO("Native Image: InitializeNativeImage, CreateResource() successful\n");
82
83     gl->GenTextures(1, &texture);
84     gl->BindTexture(mGlTarget, texture);
85
86     gl->PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
87
88     // Apply default sampling parameters
89     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, DALI_MINIFY_DEFAULT);
90     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, DALI_MAGNIFY_DEFAULT);
91     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GL_WRAP_DEFAULT);
92     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GL_WRAP_DEFAULT);
93
94     // platform specific implementation decides on what GL extension to use
95     if(nativeImage->TargetTexture() != 0u)
96     {
97       gl->DeleteTextures(1, &texture);
98       nativeImage->DestroyResource();
99       texture = 0u;
100       created = false;
101     }
102     else
103     {
104       mTextureId = texture;
105     }
106   }
107   else
108   {
109     DALI_LOG_ERROR("Native Image: InitializeNativeImage, CreateResource() failed\n");
110   }
111
112   return created; // WARNING! May be false! Needs handling! (Well, initialized on bind)
113 }
114
115 bool Texture::InitializeTexture()
116 {
117   auto gl = mController.GetGL();
118
119   GLuint texture{0};
120
121   mGlTarget = GLTextureTarget(mCreateInfo.textureType).target;
122
123   switch(mCreateInfo.textureType)
124   {
125     // Texture 2D
126     case Graphics::TextureType::TEXTURE_2D:
127     {
128       Graphics::GLES::GLTextureFormatType format(mCreateInfo.format);
129
130       // TODO: find better condition, with this test the L8 doesn't work
131       if(1) //format.format && format.type)
132       {
133         // Bind texture
134         gl->GenTextures(1, &texture);
135         gl->BindTexture(GL_TEXTURE_2D, texture);
136
137         // Allocate memory for the texture
138         gl->TexImage2D(GL_TEXTURE_2D,
139                        0,
140                        format.format,
141                        mCreateInfo.size.width,
142                        mCreateInfo.size.height,
143                        0,
144                        format.format,
145                        format.type,
146                        (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
147
148         // Clear staging buffer if there was any
149         mStagingBuffer.clear();
150
151         mTextureId = texture;
152
153         // Default texture filtering (to be set later via command buffer binding)
154         gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
155         gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
156       }
157       break;
158     }
159     default:
160     {
161       // nothing?
162     }
163   }
164   return true;
165 }
166
167 void Texture::DestroyResource()
168 {
169   // This is a proper destructor
170   if(mTextureId)
171   {
172     auto gl = mController.GetGL();
173     gl->DeleteTextures(1, &mTextureId);
174   }
175   if(mCreateInfo.nativeImagePtr)
176   {
177     mCreateInfo.nativeImagePtr->DestroyResource();
178   }
179 }
180
181 void Texture::DiscardResource()
182 {
183   mController.DiscardResource(this);
184 }
185
186 void Texture::Bind(const TextureBinding& binding) const
187 {
188   auto gl = mController.GetGL();
189
190   gl->ActiveTexture(GL_TEXTURE0 + binding.binding);
191   gl->BindTexture(mGlTarget, mTextureId);
192
193   // For GLES2 if there is a sampler set in the binding
194   if(binding.sampler)
195   {
196     // Non-default.
197     auto*       sampler           = static_cast<const GLES::Sampler*>(binding.sampler);
198     const auto& samplerCreateInfo = sampler->GetCreateInfo();
199
200     auto mipMapMode = samplerCreateInfo.mipMapMode;
201     mipMapMode      = Graphics::SamplerMipmapMode::NONE; // @todo Remove when mip-map generation is supported
202
203     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, GLSamplerFilterAndMipMapMode(samplerCreateInfo.minFilter, mipMapMode).glFilter);
204     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, GLSamplerFilter(samplerCreateInfo.magFilter).glFilter);
205     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GLAddressMode(samplerCreateInfo.addressModeU).texParameter);
206     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GLAddressMode(samplerCreateInfo.addressModeV).texParameter);
207     if(mGlTarget == GL_TEXTURE_CUBE_MAP)
208     {
209       gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_R, GLAddressMode(samplerCreateInfo.addressModeW).texParameter);
210     }
211   }
212   else
213   {
214     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, DALI_MINIFY_DEFAULT);
215     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, DALI_MAGNIFY_DEFAULT);
216     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GL_WRAP_DEFAULT);
217     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GL_WRAP_DEFAULT);
218     if(mGlTarget == GL_TEXTURE_CUBE_MAP)
219     {
220       gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_R, GL_WRAP_DEFAULT);
221     }
222   }
223 }
224
225 void Texture::Prepare()
226 {
227   NativeImageInterfacePtr nativeImage = mCreateInfo.nativeImagePtr;
228   if(nativeImage)
229   {
230     DALI_LOG_RELEASE_INFO("Native Image: PrepareTexture\n");
231     if(nativeImage->SourceChanged())
232     {
233       // Update size
234       uint32_t width  = mCreateInfo.nativeImagePtr->GetWidth();
235       uint32_t height = mCreateInfo.nativeImagePtr->GetHeight();
236       mCreateInfo.SetSize({width, height}); // Size may change
237     }
238
239     nativeImage->PrepareTexture();
240   }
241 }
242
243 } // namespace Dali::Graphics::GLES