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