Merge "Add the APIs of webview settings" 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     gl->GenTextures(1, &texture);
82     gl->BindTexture(mGlTarget, texture);
83
84     gl->PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
85
86     // Apply default sampling parameters
87     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, DALI_MINIFY_DEFAULT);
88     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, DALI_MAGNIFY_DEFAULT);
89     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GL_WRAP_DEFAULT);
90     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GL_WRAP_DEFAULT);
91
92     // platform specific implementation decides on what GL extension to use
93     if(nativeImage->TargetTexture() != 0u)
94     {
95       gl->DeleteTextures(1, &texture);
96       nativeImage->DestroyResource();
97       texture = 0u;
98       created = false;
99     }
100     else
101     {
102       mTextureId = texture;
103     }
104   }
105   else
106   {
107     DALI_LOG_ERROR("Native Image: InitializeNativeImage, CreateResource() failed\n");
108   }
109
110   return created; // WARNING! May be false! Needs handling! (Well, initialized on bind)
111 }
112
113 bool Texture::InitializeTexture()
114 {
115   auto gl = mController.GetGL();
116
117   GLuint texture{0};
118
119   mGlTarget = GLTextureTarget(mCreateInfo.textureType).target;
120
121   switch(mCreateInfo.textureType)
122   {
123     // Texture 2D
124     case Graphics::TextureType::TEXTURE_2D:
125     {
126       Graphics::GLES::GLTextureFormatType format(mCreateInfo.format);
127
128       // TODO: find better condition, with this test the L8 doesn't work
129       if(1) //format.format && format.type)
130       {
131         // Bind texture
132         gl->GenTextures(1, &texture);
133         gl->BindTexture(GL_TEXTURE_2D, texture);
134
135         // Allocate memory for the texture
136         gl->TexImage2D(GL_TEXTURE_2D,
137                        0,
138                        format.format,
139                        mCreateInfo.size.width,
140                        mCreateInfo.size.height,
141                        0,
142                        format.format,
143                        format.type,
144                        (mCreateInfo.data ? mStagingBuffer.data() : nullptr));
145
146         // Clear staging buffer if there was any
147         mStagingBuffer.clear();
148
149         mTextureId = texture;
150
151         // Default texture filtering (to be set later via command buffer binding)
152         gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
153         gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Graphics::GLES::GLSamplerFilterAndMipMapMode(Graphics::SamplerFilter::LINEAR, SamplerMipmapMode::NONE));
154       }
155       break;
156     }
157     default:
158     {
159       // nothing?
160     }
161   }
162   return true;
163 }
164
165 void Texture::DestroyResource()
166 {
167   auto gl = mController.GetGL();
168   if(!gl)
169   {
170     return;
171   }
172
173   // This is a proper destructor
174   if(mTextureId)
175   {
176     gl->DeleteTextures(1, &mTextureId);
177   }
178   if(mCreateInfo.nativeImagePtr)
179   {
180     mCreateInfo.nativeImagePtr->DestroyResource();
181   }
182 }
183
184 void Texture::DiscardResource()
185 {
186   mController.DiscardResource(this);
187 }
188
189 void Texture::Bind(const TextureBinding& binding) const
190 {
191   auto gl = mController.GetGL();
192
193   gl->ActiveTexture(GL_TEXTURE0 + binding.binding);
194   gl->BindTexture(mGlTarget, mTextureId);
195
196   // For GLES2 if there is a sampler set in the binding
197   if(binding.sampler)
198   {
199     // Non-default.
200     auto*       sampler           = static_cast<const GLES::Sampler*>(binding.sampler);
201     const auto& samplerCreateInfo = sampler->GetCreateInfo();
202
203     auto mipMapMode = samplerCreateInfo.mipMapMode;
204     mipMapMode      = Graphics::SamplerMipmapMode::NONE; // @todo Remove when mip-map generation is supported
205
206     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, GLSamplerFilterAndMipMapMode(samplerCreateInfo.minFilter, mipMapMode).glFilter);
207     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, GLSamplerFilter(samplerCreateInfo.magFilter).glFilter);
208     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GLAddressMode(samplerCreateInfo.addressModeU).texParameter);
209     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GLAddressMode(samplerCreateInfo.addressModeV).texParameter);
210     if(mGlTarget == GL_TEXTURE_CUBE_MAP)
211     {
212       gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_R, GLAddressMode(samplerCreateInfo.addressModeW).texParameter);
213     }
214   }
215   else
216   {
217     gl->TexParameteri(mGlTarget, GL_TEXTURE_MIN_FILTER, DALI_MINIFY_DEFAULT);
218     gl->TexParameteri(mGlTarget, GL_TEXTURE_MAG_FILTER, DALI_MAGNIFY_DEFAULT);
219     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_S, GL_WRAP_DEFAULT);
220     gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_T, GL_WRAP_DEFAULT);
221     if(mGlTarget == GL_TEXTURE_CUBE_MAP)
222     {
223       gl->TexParameteri(mGlTarget, GL_TEXTURE_WRAP_R, GL_WRAP_DEFAULT);
224     }
225   }
226 }
227
228 void Texture::Prepare()
229 {
230   NativeImageInterfacePtr nativeImage = mCreateInfo.nativeImagePtr;
231   if(nativeImage)
232   {
233     if(nativeImage->SourceChanged())
234     {
235       // Update size
236       uint32_t width  = mCreateInfo.nativeImagePtr->GetWidth();
237       uint32_t height = mCreateInfo.nativeImagePtr->GetHeight();
238       mCreateInfo.SetSize({width, height}); // Size may change
239     }
240
241     nativeImage->PrepareTexture();
242   }
243 }
244
245 } // namespace Dali::Graphics::GLES