[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / windows / pixmap-render-surface-ecore-win.cpp
1 /*
2  * Copyright (c) 2018 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/windows/pixmap-render-surface-ecore-win.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/threading/mutex.h>
25 #include <Win32WindowSystem.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/integration-api/thread-synchronization-interface.h>
29 #include <dali/internal/system/common/trigger-event.h>
30 #include <dali/internal/window-system/common/display-connection.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gPixmapRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_PIXMAP_RENDER_SURFACE_ECORE_X");
41 #endif
42
43 namespace
44 {
45 static const int INITIAL_PRODUCE_BUFFER_INDEX = 0;
46 static const int INITIAL_CONSUME_BUFFER_INDEX = 1;
47 }
48
49 PixmapRenderSurfaceEcoreWin::PixmapRenderSurfaceEcoreWin(Dali::PositionSize positionSize,
50                                          Any surface,
51                                          const std::string& name,
52                                          bool isTransparent)
53 : mPosition( positionSize ),
54   mTitle( name ),
55   mRenderNotification( NULL ),
56   mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
57   mOwnSurface( false ),
58   mProduceBufferIndex( INITIAL_PRODUCE_BUFFER_INDEX ),
59   mConsumeBufferIndex( INITIAL_CONSUME_BUFFER_INDEX ),
60   mThreadSynchronization(NULL)
61 {
62   for( int i = 0; i != BUFFER_COUNT; ++i )
63   {
64     mX11Pixmaps[i] = 0;
65     mEglSurfaces[i] = 0;
66   }
67
68   Initialize( surface );
69 }
70
71 PixmapRenderSurfaceEcoreWin::~PixmapRenderSurfaceEcoreWin()
72 {
73   // release the surface if we own one
74   //if( mOwnSurface )
75   //{
76   //  for (int i = 0; i < BUFFER_COUNT; ++i)
77   //  {
78   //    Ecore_X_Pixmap pixmap = mX11Pixmaps[i];
79
80   //    // if we did create the pixmap, delete the pixmap
81   //    DALI_LOG_INFO( gPixmapRenderSurfaceLogFilter, Debug::General, "Own pixmap (%x) freed\n", pixmap );
82   //    ecore_x_pixmap_free( pixmap );
83   //  }
84   //}
85 }
86
87 void PixmapRenderSurfaceEcoreWin::Initialize( Any surface )
88 {
89   // see if there is a surface in Any surface
90   unsigned int surfaceId  = GetSurfaceId( surface );
91
92   // if the surface is empty, create a new one.
93   if ( surfaceId == 0 )
94   {
95     // we own the surface about to created
96     mOwnSurface = true;
97     CreateRenderable();
98   }
99   else
100   {
101     // XLib should already be initialized so no point in calling XInitThreads
102     UseExistingRenderable( surfaceId );
103   }
104 }
105
106 Any PixmapRenderSurfaceEcoreWin::GetSurface()
107 {
108   //Ecore_X_Pixmap pixmap = 0;
109   //{
110   //  ConditionalWait::ScopedLock lock( mPixmapCondition );
111   //  pixmap = mX11Pixmaps[mProduceBufferIndex];
112   //}
113
114   return Any( 0 );
115 }
116
117 void PixmapRenderSurfaceEcoreWin::SetRenderNotification(TriggerEventInterface* renderNotification)
118 {
119   mRenderNotification = renderNotification;
120 }
121
122 PositionSize PixmapRenderSurfaceEcoreWin::GetPositionSize() const
123 {
124   return mPosition;
125 }
126
127 void PixmapRenderSurfaceEcoreWin::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
128 {
129   // calculate DPI 
130   float xres, yres;
131
132   //// 1 inch = 25.4 millimeters
133   Win32WindowSystem::GetDPI( xres, yres );
134
135   xres *= 1.5;
136   yres *= 1.5;
137
138   dpiHorizontal = int( xres + 0.5f );  // rounding
139   dpiVertical = int( yres + 0.5f );
140 }
141
142 void PixmapRenderSurfaceEcoreWin::InitializeEgl( EglInterface& egl )
143 {
144   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
145
146   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
147
148   eglImpl.ChooseConfig(false, mColorDepth);
149 }
150
151 void PixmapRenderSurfaceEcoreWin::CreateEglSurface( EglInterface& egl )
152 {
153   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
154
155   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
156
157   for (int i = 0; i < BUFFER_COUNT; ++i)
158   {
159     // create the EGL surface
160     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
161     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
162     mEglSurfaces[i] = eglImpl.CreateSurfacePixmap( EGLNativePixmapType( pixmap ), mColorDepth ); // reinterpret_cast does not compile
163   }
164 }
165
166 void PixmapRenderSurfaceEcoreWin::DestroyEglSurface( EglInterface& egl )
167 {
168   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
169
170   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
171
172   for (int i = 0; i < BUFFER_COUNT; ++i)
173   {
174     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
175     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
176     eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[i] );
177     eglImpl.DestroySurface();
178   }
179 }
180
181 bool PixmapRenderSurfaceEcoreWin::ReplaceEGLSurface( EglInterface& egl )
182 {
183   DALI_LOG_TRACE_METHOD( gPixmapRenderSurfaceLogFilter );
184
185   bool contextLost = false;
186
187   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
188
189   for (int i = 0; i < BUFFER_COUNT; ++i)
190   {
191     // a new surface for the new pixmap
192     // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
193     XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[i] );
194     contextLost = eglImpl.ReplaceSurfacePixmap( EGLNativePixmapType( pixmap ), mEglSurfaces[i] ); // reinterpret_cast does not compile
195   }
196
197   // need to cast to X handle as in 64bit system ECore handle is 32 bit whereas EGLnative and XWindow are 64 bit
198   XPixmap pixmap = static_cast<XPixmap>( mX11Pixmaps[mProduceBufferIndex] );
199   eglImpl.MakeCurrent( EGLNativePixmapType( pixmap ), mEglSurfaces[mProduceBufferIndex] );
200
201   return contextLost;
202 }
203
204 void PixmapRenderSurfaceEcoreWin::StartRender()
205 {
206 }
207
208 bool PixmapRenderSurfaceEcoreWin::PreRender( EglInterface& egl, Integration::GlAbstraction&, bool )
209 {
210   // Nothing to do for pixmaps
211   return true;
212 }
213
214 void PixmapRenderSurfaceEcoreWin::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, Dali::DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
215 {
216 }
217
218 void PixmapRenderSurfaceEcoreWin::StopRender()
219 {
220   ReleaseLock();
221 }
222
223 void PixmapRenderSurfaceEcoreWin::SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization )
224 {
225   mThreadSynchronization = &threadSynchronization;
226 }
227
228 void PixmapRenderSurfaceEcoreWin::ReleaseLock()
229 {
230   if( mThreadSynchronization )
231   {
232     mThreadSynchronization->PostRenderComplete();
233   }
234 }
235
236 RenderSurface::Type PixmapRenderSurfaceEcoreWin::GetSurfaceType()
237 {
238   return RenderSurface::PIXMAP_RENDER_SURFACE;
239 }
240
241 void PixmapRenderSurfaceEcoreWin::CreateRenderable()
242 {
243 }
244
245 void PixmapRenderSurfaceEcoreWin::UseExistingRenderable( unsigned int surfaceId )
246 {
247 }
248
249 unsigned int PixmapRenderSurfaceEcoreWin::GetSurfaceId( Any surface ) const
250 {
251   return 0;
252 }
253
254 } // namespace Adaptor
255
256 } // namespace internal
257
258 } // namespace Dali