Revert "Hardware Accelerated Video Playback doesn't work on IA"
[framework/web/webkit-efl.git] / Source / WebCore / platform / graphics / gstreamer / tizen / VideoLayerTizen.cpp
1 /*
2     Copyright (C) 2012 Samsung Electronics.
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "VideoLayerTizen.h"
22
23 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
24
25 #include "GraphicsContext.h"
26 #include "IntRect.h"
27 #include "RenderLayerBacking.h"
28
29 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
30 #include <gst/interfaces/xoverlay.h>
31 #include <gst/video/gstvideosink.h>
32 #include <gst/video/video.h>
33 #else
34 #include <cairo-xlib.h>
35 #include <X11/Xlib.h>
36 #include <X11/Xutil.h>
37 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
38
39 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) || CPU(X86) || CPU(X86_64)
40 static Display* g_nativeDisplay = 0;
41 static int g_nativeWindow = 0;
42 #endif
43
44 namespace WebCore {
45
46 VideoLayerTizen::VideoLayerTizen(HTMLMediaElement* media)
47     : m_media(media)
48 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
49     , m_videoSink(0)
50 #else
51     , m_platformSurfaceID(0)
52 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
53 {
54 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) || CPU(X86) || CPU(X86_64)
55     if (!g_nativeDisplay)
56            g_nativeDisplay = XOpenDisplay(0);
57
58        if (!g_nativeDisplay)
59            return;
60
61        g_nativeWindow = XCreateSimpleWindow(g_nativeDisplay, XDefaultRootWindow(g_nativeDisplay),
62                                0, 0, 1, 1, 0,
63                                BlackPixel(g_nativeDisplay, 0), WhitePixel(g_nativeDisplay, 0));
64        XFlush(g_nativeDisplay);
65 #endif
66 }
67
68 VideoLayerTizen::~VideoLayerTizen()
69 {
70     syncLayer(0);
71
72 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
73     if (m_platformSurface)
74         m_platformSurface.clear();
75
76     if (m_platformSurfaceToBeRemoved)
77         m_platformSurfaceToBeRemoved.clear();
78
79     m_videoSink = 0;
80 #else
81     if (m_platformSurfaceID) {
82         XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
83         m_platformSurfaceID = 0;
84     }
85     if (g_nativeWindow) {
86         XDestroyWindow(g_nativeDisplay, g_nativeWindow);
87         g_nativeWindow = 0;
88     }
89 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
90 }
91
92 PlatformLayer* VideoLayerTizen::platformLayer() const
93 {
94     return const_cast<TextureMapperPlatformLayer*>(static_cast<const TextureMapperPlatformLayer*>(this));
95 }
96
97 void VideoLayerTizen::syncLayer(VideoLayerTizen* layer)
98 {
99     RenderBox* renderBox = m_media->renderBox();
100     if (renderBox && renderBox->hasAcceleratedCompositing()) {
101         RenderLayer* renderLayer = renderBox->layer();
102         if (renderLayer && renderLayer->isComposited()) {
103             GraphicsLayer* graphicsLayer = renderLayer->backing()->graphicsLayer();
104             if (graphicsLayer)
105                 graphicsLayer->setContentsToMedia(layer);
106         }
107     }
108 }
109
110 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
111 static gboolean platformSurfaceUpdateTimeoutCallback(VideoLayerTizen* layer)
112 {
113     layer->notifySyncRequired();
114     return FALSE;
115 }
116
117 bool VideoLayerTizen::swapPlatformSurfaces()
118 {
119     return true;
120 }
121
122 void VideoLayerTizen::freePlatformSurface(int id)
123 {
124     if (m_platformSurfaceToBeRemoved)
125         m_platformSurfaceToBeRemoved.clear();
126 }
127
128 void VideoLayerTizen::platformSurfaceUpdated()
129 {
130     notifySyncRequired();
131 }
132
133 GstElement* VideoLayerTizen::createVideoSink()
134 {
135     GstElement* videoSink = gst_element_factory_make("xvimagesink", "xvimagesink");
136     g_object_set(videoSink, "rotate", 0, NULL);
137
138     m_videoSink = videoSink;
139
140     return videoSink;
141 }
142
143 void VideoLayerTizen::notifySyncRequired()
144 {
145     if (!m_platformSurface)
146         return;
147
148     syncLayer(this);
149 }
150
151 void VideoLayerTizen::setOverlay(IntSize size)
152 {
153     if (size.isEmpty())
154         return;
155
156     if (!m_videoSink)
157         return;
158
159     if (m_platformSurface)
160         m_platformSurfaceToBeRemoved = m_platformSurface.release();
161
162     m_platformSurface = SharedVideoPlatformSurfaceTizen::create(size);
163     m_platformSurface->setVideoPlatformSurfaceUpdateListener(this);
164
165     gst_x_overlay_set_window_handle(GST_X_OVERLAY(m_videoSink), m_platformSurface->id());
166     g_object_set(m_videoSink, "rotate", 0, NULL);
167 }
168
169 void VideoLayerTizen::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
170 {
171     if (m_platformSurface)
172         m_platformSurface->paintCurrentFrameInContext(context, rect);
173 }
174 #else
175 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && (CPU(X86) || CPU(X86_64))
176 bool VideoLayerTizen::swapPlatformSurfaces()
177 {
178     return true;
179 }
180 #endif
181 void VideoLayerTizen::paintVideoLayer(IntSize videoSize)
182 {
183     if (videoSize.isEmpty())
184         return;
185
186     if (m_videoSize != videoSize) {
187         m_videoSize = videoSize;
188
189         if (m_platformSurfaceID) {
190             XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
191             m_platformSurfaceID = 0;
192         }
193         m_platformSurfaceID = XCreatePixmap(g_nativeDisplay, g_nativeWindow, m_videoSize.width(), m_videoSize.height(), DefaultDepth(g_nativeDisplay, DefaultScreen(g_nativeDisplay)));
194         XFlush(g_nativeDisplay);
195     }
196
197     RefPtr<cairo_surface_t> surface = adoptRef(cairo_xlib_surface_create(g_nativeDisplay, m_platformSurfaceID, DefaultVisual(g_nativeDisplay, DefaultScreen(g_nativeDisplay)), m_videoSize.width(), m_videoSize.height()));
198     if (cairo_surface_status(surface.get()) != CAIRO_STATUS_SUCCESS)
199        return;
200
201     RefPtr<cairo_t> cr = adoptRef(cairo_create(surface.get()));
202     OwnPtr<WebCore::GraphicsContext> context = adoptPtr(new GraphicsContext(cr.get()));
203     m_media->player()->paint(context.get(), IntRect(0, 0, m_videoSize.width(), m_videoSize.height()));
204     XFlush(g_nativeDisplay);
205
206     syncLayer(this);
207 }
208 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
209
210 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
211 uint64_t VideoLayerTizen::graphicsSurfaceToken() const
212 {
213 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
214     return (m_platformSurface ? m_platformSurface->id() : 0);
215 #else
216     return m_platformSurfaceID;
217 #endif
218 }
219
220 uint32_t VideoLayerTizen::copyToGraphicsSurface()
221 {
222 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
223     return (m_platformSurface ? m_platformSurface->id() : 0);
224 #else
225     return m_platformSurfaceID;
226 #endif
227 }
228
229 int VideoLayerTizen::graphicsSurfaceFlags() const
230 {
231 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && !(CPU(X86) || CPU(X86_64))
232     return GraphicsSurface::Is2D | GraphicsSurface::UseLinearFilter | GraphicsSurface::Alpha;
233 #else
234     return GraphicsSurface::Is2D;
235 #endif
236 }
237 #endif // USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
238
239 } // WebCore
240
241 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)