Add NativeImageSurface
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / tizen-wayland / native-image-surface-impl-ecore-wl.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 <dali/internal/window-system/tizen-wayland/native-image-surface-impl-ecore-wl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <system_info.h>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/graphics/gles/egl-graphics-factory.h>
27 #include <dali/internal/graphics/gles/egl-graphics.h>
28 #include <dali/internal/graphics/gles/egl-implementation.h>
29 #include <dali/internal/system/common/environment-options.h>
30 #include <dali/internal/window-system/common/display-utils.h>
31
32 using namespace Dali::Internal::Adaptor;
33
34 namespace Dali
35 {
36 namespace Internal
37 {
38 namespace Adaptor
39 {
40 NativeImageSurfaceEcoreWl::NativeImageSurfaceEcoreWl(Dali::NativeImageSourceQueuePtr queue)
41 : mDisplayConnection(nullptr),
42   mGraphics(nullptr),
43   mEGL(nullptr),
44   mEGLSurface(nullptr),
45   mEGLContext(nullptr),
46   mColorDepth(COLOR_DEPTH_32),
47   mTbmQueue(nullptr),
48   mDepth(false),
49   mStencil(false),
50   mGLESVersion(30),
51   mMSAA(0)
52 {
53   if(queue)
54   {
55     mTbmQueue   = AnyCast<tbm_surface_queue_h>(queue->GetNativeImageSourceQueue());
56     mTbmFormat  = tbm_surface_queue_get_format(mTbmQueue);
57     mColorDepth = (mTbmFormat == TBM_FORMAT_ARGB8888) ? COLOR_DEPTH_32 : COLOR_DEPTH_24;
58   }
59   else
60   {
61     DALI_LOG_ERROR("NativeImageSourceQueue is null.");
62   }
63 }
64
65 bool NativeImageSurfaceEcoreWl::SetGraphicsConfig(bool depth, bool stencil, int msaa, int version)
66 {
67   bool featureFlag = false;
68   int  error       = SYSTEM_INFO_ERROR_NONE;
69
70   if(version == 30)
71   {
72     error = system_info_get_platform_bool("http://tizen.org/feature/opengles.version.3_0", &featureFlag);
73   }
74   else if(version == 20)
75   {
76     error = system_info_get_platform_bool("http://tizen.org/feature/opengles.version.2_0", &featureFlag);
77   }
78   else
79   {
80     DALI_LOG_ERROR("version is not valid");
81     return false;
82   }
83
84   if(error != SYSTEM_INFO_ERROR_NONE)
85   {
86     DALI_LOG_ERROR("Can't check platform feature.\n");
87     return false;
88   }
89
90   if(featureFlag)
91   {
92     mDepth   = depth;
93     mStencil = stencil;
94     if(mMSAA == 0)
95     {
96       //EGL_DONT_CARE is -1
97       mMSAA = -1;
98     }
99     else
100     {
101       mMSAA = msaa;
102     }
103     mGLESVersion = version;
104   }
105
106   return featureFlag;
107 }
108
109 Any NativeImageSurfaceEcoreWl::GetNativeRenderable()
110 {
111   return mTbmQueue;
112 }
113
114 void NativeImageSurfaceEcoreWl::InitializeGraphics()
115 {
116   std::unique_ptr<GraphicsFactory> graphicsFactoryPtr = Utils::MakeUnique<GraphicsFactory>(*(new EnvironmentOptions()));
117   auto                             graphicsFactory    = *graphicsFactoryPtr.get();
118
119   mGraphics                      = std::unique_ptr<GraphicsInterface>(&graphicsFactory.Create());
120   GraphicsInterface* graphics    = mGraphics.get();
121   auto               eglGraphics = static_cast<EglGraphics*>(graphics);
122   eglGraphics->Initialize(mDepth, mStencil, false, mMSAA);
123
124   mDisplayConnection = std::unique_ptr<Dali::DisplayConnection>(Dali::DisplayConnection::New(*mGraphics, Dali::RenderSurfaceInterface::Type::NATIVE_RENDER_SURFACE));
125   mDisplayConnection->Initialize();
126
127   mEGL = &eglGraphics->GetEglInterface();
128
129   if(mEGLContext == NULL)
130   {
131     Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
132     eglImpl.SetGlesVersion(mGLESVersion);
133
134     if(eglImpl.ChooseConfig(true, mColorDepth) == false)
135     {
136       DALI_LOG_ERROR("InitializeGraphics: Fail to choose config. Version:%d, ColorDepth:%d, depth:%d, stencil:%d, MSAA:%d",
137                      mGLESVersion,
138                      mColorDepth == COLOR_DEPTH_32 ? 32 : 24,
139                      mDepth ? 24 : 0,
140                      mStencil ? 8 : 0,
141                      mMSAA);
142       return;
143     }
144
145     // Create the OpenGL Surface & Context
146     eglImpl.CreateWindowContext(mEGLContext);
147     mEGLSurface = eglImpl.CreateSurfaceWindow(reinterpret_cast<EGLNativeWindowType>(mTbmQueue), mColorDepth);
148
149     MakeContextCurrent();
150   }
151 }
152
153 void NativeImageSurfaceEcoreWl::TerminateGraphics()
154 {
155   GraphicsInterface* graphics    = mGraphics.get();
156   auto               eglGraphics = static_cast<EglGraphics*>(graphics);
157
158   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
159   if(mEGLSurface)
160   {
161     eglImpl.DestroySurface(mEGLSurface);
162   }
163
164   if(mEGLContext)
165   {
166     eglImpl.DestroyContext(mEGLContext);
167   }
168 }
169
170 void NativeImageSurfaceEcoreWl::PreRender()
171 {
172   MakeContextCurrent();
173 }
174
175 void NativeImageSurfaceEcoreWl::PostRender()
176 {
177   GraphicsInterface* graphics    = mGraphics.get();
178   auto               eglGraphics = static_cast<EglGraphics*>(graphics);
179   if(eglGraphics)
180   {
181     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
182     eglImpl.SwapBuffers(mEGLSurface);
183   }
184 }
185
186 void NativeImageSurfaceEcoreWl::MakeContextCurrent()
187 {
188   if(mEGL != nullptr)
189   {
190     if(mEGLSurface && mEGLContext)
191     {
192       mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
193     }
194     else
195     {
196       DALI_LOG_ERROR("EGLSurface(%p) or mEGLContext(%p) is null\n", mEGLSurface, mEGLContext);
197     }
198   }
199 }
200
201 bool NativeImageSurfaceEcoreWl::CanRender()
202 {
203   return tbm_surface_queue_can_dequeue(mTbmQueue, 0);
204 }
205
206 } // namespace Adaptor
207 } // namespace Internal
208 } // namespace Dali