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);
57 VideoLayerTizen::VideoLayerTizen(HTMLMediaElement* media)
59 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
62 , m_platformSurfaceID(0)
63 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
65 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
67 g_nativeDisplay = XOpenDisplay(0);
72 g_nativeWindow = XCreateSimpleWindow(g_nativeDisplay, XDefaultRootWindow(g_nativeDisplay),
74 BlackPixel(g_nativeDisplay, 0), WhitePixel(g_nativeDisplay, 0));
75 XFlush(g_nativeDisplay);
79 VideoLayerTizen::~VideoLayerTizen()
83 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
84 if (m_platformSurface)
85 m_platformSurface.clear();
87 if (m_platformSurfaceToBeRemoved)
88 m_platformSurfaceToBeRemoved.clear();
92 if (m_platformSurfaceID) {
93 XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
94 m_platformSurfaceID = 0;
97 XDestroyWindow(g_nativeDisplay, g_nativeWindow);
100 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
103 PlatformLayer* VideoLayerTizen::platformLayer() const
105 return const_cast<TextureMapperPlatformLayer*>(static_cast<const TextureMapperPlatformLayer*>(this));
108 void VideoLayerTizen::syncLayer(VideoLayerTizen* layer)
110 RenderBox* renderBox = m_media->renderBox();
111 if (renderBox && renderBox->hasAcceleratedCompositing()) {
112 RenderLayer* renderLayer = renderBox->layer();
113 if (renderLayer && renderLayer->isComposited()) {
114 GraphicsLayer* graphicsLayer = renderLayer->backing()->graphicsLayer();
116 graphicsLayer->setContentsToMedia(layer);
121 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
122 static gboolean platformSurfaceUpdateTimeoutCallback(VideoLayerTizen* layer)
124 layer->notifySyncRequired();
128 bool VideoLayerTizen::swapPlatformSurfaces()
133 void VideoLayerTizen::freePlatformSurface(int id)
135 if (m_platformSurfaceToBeRemoved)
136 m_platformSurfaceToBeRemoved.clear();
139 void VideoLayerTizen::platformSurfaceUpdated()
141 notifySyncRequired();
144 GstElement* VideoLayerTizen::createVideoSink()
146 #if !USE(ACCELERATED_VIDEO_VAAPI)
147 GstElement* videoSink = gst_element_factory_make("xvimagesink", "xvimagesink");
148 g_object_set(videoSink, "rotate", 0, NULL);
151 GstElement* videoSink = gst_element_factory_make("vaapisink", "vaapisink");
152 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);
197 void VideoLayerTizen::paintVideoLayer(IntSize videoSize)
199 if (videoSize.isEmpty())
202 if (m_videoSize != videoSize) {
203 m_videoSize = videoSize;
205 if (m_platformSurfaceID) {
206 XFreePixmap(g_nativeDisplay, m_platformSurfaceID);
207 m_platformSurfaceID = 0;
209 m_platformSurfaceID = XCreatePixmap(g_nativeDisplay, g_nativeWindow, m_videoSize.width(), m_videoSize.height(), DefaultDepth(g_nativeDisplay, DefaultScreen(g_nativeDisplay)));
210 XFlush(g_nativeDisplay);
213 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()));
214 if (cairo_surface_status(surface.get()) != CAIRO_STATUS_SUCCESS)
217 RefPtr<cairo_t> cr = adoptRef(cairo_create(surface.get()));
218 OwnPtr<WebCore::GraphicsContext> context = adoptPtr(new GraphicsContext(cr.get()));
219 m_media->player()->paint(context.get(), IntRect(0, 0, m_videoSize.width(), m_videoSize.height()));
220 XFlush(g_nativeDisplay);
224 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
226 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
227 uint64_t VideoLayerTizen::graphicsSurfaceToken() const
229 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
230 return (m_platformSurface ? m_platformSurface->id() : 0);
232 return m_platformSurfaceID;
236 uint32_t VideoLayerTizen::copyToGraphicsSurface()
238 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
239 return (m_platformSurface ? m_platformSurface->id() : 0);
241 return m_platformSurfaceID;
245 int VideoLayerTizen::graphicsSurfaceFlags() const
247 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
248 return m_platformSurface ? m_platformSurface->graphicsSurfaceFlags() : GraphicsSurface::Is2D;
250 return GraphicsSurface::Is2D;
253 #endif // USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
257 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)