2 Copyright (C) 2012 Samsung Electronics.
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.
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.
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.
21 #include "VideoLayerTizen.h"
23 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
25 #include "GraphicsContext.h"
27 #include "RenderLayerBacking.h"
29 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
30 #include <gst/interfaces/xoverlay.h>
31 #include <gst/video/gstvideosink.h>
32 #include <gst/video/video.h>
34 #include <cairo-xlib.h>
36 #include <X11/Xutil.h>
37 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
39 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
40 static Display* g_nativeDisplay = 0;
41 static int g_nativeWindow = 0;
46 #if USE(ACCELERATED_VIDEO_VAAPI)
49 static bool envSet = false;
52 setenv("PSB_VIDEO_CTEXTURE", "1", true);
58 VideoLayerTizen::VideoLayerTizen(HTMLMediaElement* media)
60 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
63 , m_platformSurfaceID(0)
64 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
66 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
68 g_nativeDisplay = XOpenDisplay(0);
73 g_nativeWindow = XCreateSimpleWindow(g_nativeDisplay, XDefaultRootWindow(g_nativeDisplay),
75 BlackPixel(g_nativeDisplay, 0), WhitePixel(g_nativeDisplay, 0));
76 XFlush(g_nativeDisplay);
80 VideoLayerTizen::~VideoLayerTizen()
84 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
85 if (m_platformSurface)
86 m_platformSurface.clear();
88 if (m_platformSurfaceToBeRemoved)
89 m_platformSurfaceToBeRemoved.clear();
93 if (m_platformSurfaceID) {
94 XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
95 m_platformSurfaceID = 0;
98 XDestroyWindow(g_nativeDisplay, g_nativeWindow);
101 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
104 PlatformLayer* VideoLayerTizen::platformLayer() const
106 return const_cast<TextureMapperPlatformLayer*>(static_cast<const TextureMapperPlatformLayer*>(this));
109 void VideoLayerTizen::syncLayer(VideoLayerTizen* layer)
111 RenderBox* renderBox = m_media->renderBox();
112 if (renderBox && renderBox->hasAcceleratedCompositing()) {
113 RenderLayer* renderLayer = renderBox->layer();
114 if (renderLayer && renderLayer->isComposited()) {
115 GraphicsLayer* graphicsLayer = renderLayer->backing()->graphicsLayer();
117 graphicsLayer->setContentsToMedia(layer);
122 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
123 static gboolean platformSurfaceUpdateTimeoutCallback(VideoLayerTizen* layer)
125 layer->notifySyncRequired();
129 bool VideoLayerTizen::swapPlatformSurfaces()
134 void VideoLayerTizen::freePlatformSurface(int id)
136 if (m_platformSurfaceToBeRemoved)
137 m_platformSurfaceToBeRemoved.clear();
140 void VideoLayerTizen::platformSurfaceUpdated()
142 notifySyncRequired();
145 GstElement* VideoLayerTizen::createVideoSink()
147 #if !USE(ACCELERATED_VIDEO_VAAPI)
148 GstElement* videoSink = gst_element_factory_make("xvimagesink", "xvimagesink");
149 g_object_set(videoSink, "rotate", 0, NULL);
152 GstElement* videoSink = gst_element_factory_make("vaapisink", "vaapisink");
153 g_object_set(videoSink, "is-pixmap", 1, NULL);
155 m_videoSink = videoSink;
160 void VideoLayerTizen::notifySyncRequired()
162 if (!m_platformSurface)
168 void VideoLayerTizen::setOverlay(IntSize size)
176 if (m_platformSurface)
177 m_platformSurfaceToBeRemoved = m_platformSurface.release();
179 m_platformSurface = SharedVideoPlatformSurfaceTizen::create(size);
180 m_platformSurface->setVideoPlatformSurfaceUpdateListener(this);
182 gst_x_overlay_set_window_handle(GST_X_OVERLAY(m_videoSink), m_platformSurface->id());
183 #if !USE(ACCELERATED_VIDEO_VAAPI)
184 g_object_set(m_videoSink, "rotate", 0, NULL);
186 g_object_set(m_videoSink, "is-pixmap", 1, NULL);
190 void VideoLayerTizen::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& rect)
192 if (m_platformSurface)
193 m_platformSurface->paintCurrentFrameInContext(context, rect);
196 void VideoLayerTizen::paintVideoLayer(IntSize videoSize)
198 if (videoSize.isEmpty())
201 if (m_videoSize != videoSize) {
202 m_videoSize = videoSize;
204 if (m_platformSurfaceID) {
205 XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
206 m_platformSurfaceID = 0;
208 m_platformSurfaceID = XCreatePixmap(g_nativeDisplay, g_nativeWindow, m_videoSize.width(), m_videoSize.height(), DefaultDepth(g_nativeDisplay, DefaultScreen(g_nativeDisplay)));
209 XFlush(g_nativeDisplay);
212 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()));
213 if (cairo_surface_status(surface.get()) != CAIRO_STATUS_SUCCESS)
216 RefPtr<cairo_t> cr = adoptRef(cairo_create(surface.get()));
217 OwnPtr<WebCore::GraphicsContext> context = adoptPtr(new GraphicsContext(cr.get()));
218 m_media->player()->paint(context.get(), IntRect(0, 0, m_videoSize.width(), m_videoSize.height()));
222 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
224 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
225 uint64_t VideoLayerTizen::graphicsSurfaceToken() const
227 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
228 return (m_platformSurface ? m_platformSurface->id() : 0);
230 return m_platformSurfaceID;
234 uint32_t VideoLayerTizen::copyToGraphicsSurface()
236 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
237 return m_platformSurface ? m_platformSurface->id() : 0;
239 return m_platformSurfaceID;
243 int VideoLayerTizen::graphicsSurfaceFlags() const
245 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
246 return m_platformSurface ? m_platformSurface->graphicsSurfaceFlags() : GraphicsSurface::Is2D;
248 return GraphicsSurface::Is2D;
251 #endif // USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
255 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)