downscale video surface to screen resolution
authorZhao Halley <halley.zhao@intel.com>
Mon, 1 Apr 2013 03:09:09 +0000 (11:09 +0800)
committerGerrit Code Review <gerrit2@kim11>
Thu, 18 Apr 2013 09:04:33 +0000 (18:04 +0900)
[Title] downscale video surface to screen resolution
[Issue#] N/A
[Problem] high definition video consume too much GPU load
        and memory bandwidth.
[Cause] when video size is bigger than screen resolution,
        video texture with original size consumes more GPU load
        and memory bandwidth without quality improvement
[Solution] downscale video size to not bigger than screen resolution
        when it is rendered to Pixmap: scaleHDVideoToDisplaySize.
        The solution is enclosed in USE(ACCELERATED_VIDEO_VAAPI),
        since other device doesn't prefer such optimization for the time being.

Change-Id: Ia41b7d9d0622560ed3659df05622711b55c4788f

Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h

index fee5f97..660d8f9 100755 (executable)
@@ -774,7 +774,12 @@ IntSize MediaPlayerPrivateGStreamer::naturalSize() const
     }
 
     LOG_MEDIA_MESSAGE("Natural size: %" G_GUINT64_FORMAT "x%" G_GUINT64_FORMAT, width, height);
-    m_videoSize = IntSize(static_cast<int>(width), static_cast<int>(height));
+#if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER) && ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE) && USE(ACCELERATED_VIDEO_VAAPI)
+    if(!m_displaySize.isEmpty())
+        m_videoSize = scaleHDVideoToDisplaySize(static_cast<int>(width), static_cast<int>(height), m_displaySize.width(), m_displaySize.height());
+    else
+#endif
+        m_videoSize = IntSize(static_cast<int>(width), static_cast<int>(height));
     return m_videoSize;
 }
 
@@ -2323,21 +2328,69 @@ void MediaPlayerPrivateGStreamer::paintCurrentFrameInContext(GraphicsContext* co
     m_videoLayer->paintCurrentFrameInContext(context, rect);
 }
 
+#if USE(ACCELERATED_VIDEO_VAAPI)
+// Other device doesn't prefer such optimization, so enclose it in USE(ACCELERATED_VIDEO_VAAPI) macro
+IntSize MediaPlayerPrivateGStreamer::scaleHDVideoToDisplaySize(int videoWidth, int videoHeight, int displayWidth, int displayHeight) const
+{
+    int fitWidth, fitHeight;
+
+#if ENABLE(TIZEN_DLOG_SUPPORT)
+    TIZEN_LOGI("videoWidth: %d, videoHeight: %d, displayWidth: %d, displayHeight: %d ", videoWidth, videoHeight, displayWidth, displayHeight);
+#endif
+    fitWidth = videoWidth;
+    fitHeight = videoHeight;
+    if (displayWidth > 0 && displayHeight > 0) {
+        // In case video is rotated, we always use a 'better'
+        // orientation (landscape or portrait) to calculate fitWidth/fitHeight.
+        // It means we use a bigger size which doesn't lose quality
+        // in either landscape or portrait orientation
+        if ((videoWidth > videoHeight && displayWidth < displayHeight)
+            || (videoWidth < videoHeight && displayWidth > displayHeight)) {
+            int tmp;
+            tmp = displayWidth;
+            displayWidth = displayHeight;
+            displayHeight = tmp;
+        }
+
+        if (videoWidth > displayWidth || videoHeight > displayHeight) {
+            if (videoWidth * displayHeight > videoHeight * displayWidth) {
+                fitWidth = displayWidth;
+                fitHeight = videoHeight * displayWidth / videoWidth;
+                ASSERT(fitHeight <= displayHeight);
+            } else {
+                fitWidth = videoWidth * displayHeight / videoHeight;
+                fitHeight = displayHeight;
+                ASSERT(fitWidth <= displayWidth);
+            }
+        }
+    }
+
+    return IntSize(fitWidth, fitHeight);
+}
+#endif
 void MediaPlayerPrivateGStreamer::xWindowIdPrepared(GstMessage* message)
 {
-    int width;
-    int height;
-    gst_structure_get_int(message->structure, "video-width", &width);
-    gst_structure_get_int(message->structure, "video-height", &height);
+    int videoWidth, videoHeight;
+
+    gst_structure_get_int(message->structure, "video-width", &videoWidth);
+    gst_structure_get_int(message->structure, "video-height", &videoHeight);
 
 #if ENABLE(TIZEN_DLOG_SUPPORT)
-    TIZEN_LOGI("Width: %d, Height: %d", width, height);
+    TIZEN_LOGI("videoWidth: %d, videoHeight: %d", videoWidth, videoHeight);
 #endif
 
-    IntSize size(width, height);
-    m_videoSize = size;
-    m_videoLayer->setOverlay(size);
+#if USE(ACCELERATED_VIDEO_VAAPI)
+    int displayWidth, displayHeight;
+    gst_structure_get_int(message->structure, "display-width", &displayWidth);
+    gst_structure_get_int(message->structure, "display-height", &displayHeight);
+    m_displaySize = IntSize(displayWidth, displayHeight);
+    m_videoSize = scaleHDVideoToDisplaySize(videoWidth, videoHeight, displayWidth, displayHeight);
+#else
+    m_videoSize = IntSize(videoWidth, videoHeight);
+#endif
+    m_videoLayer->setOverlay(m_videoSize);
 }
+
 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
 
index 3316be2..5277651 100644 (file)
@@ -145,6 +145,9 @@ class MediaPlayerPrivateGStreamer : public MediaPlayerPrivateInterface
             virtual PlatformLayer* platformLayer() const;
 
             void xWindowIdPrepared(GstMessage*);
+#if USE(ACCELERATED_VIDEO_VAAPI)
+            IntSize scaleHDVideoToDisplaySize(int videoWidth, int videoHeight, int displayWidth, int displayHeight) const;
+#endif
 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
             virtual void paintCurrentFrameInContext(GraphicsContext*, const IntRect&);
 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
@@ -245,6 +248,9 @@ class MediaPlayerPrivateGStreamer : public MediaPlayerPrivateInterface
             GRefPtr<GstPad> m_videoSinkPad;
 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
             OwnPtr<VideoLayerTizen> m_videoLayer;
+#if USE(ACCELERATED_VIDEO_VAAPI)
+            IntSize m_displaySize;
+#endif
 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
 
             mutable IntSize m_videoSize;