From 99a3514072ef70c39683237664e8fffd4977590c Mon Sep 17 00:00:00 2001
From: chris <chris@flunder.ekke.wtal>
Date: Tue, 1 Jun 2010 00:47:48 +0200
Subject: [PATCH 2/5] fixing embed video on osx quartz renderer part1

This patch makes the quarz renderer to use of the calulations
of VideoOutWindow::GetDisplayVideoRect() for the desired parameters
instead repeating that code.
However, that will break video out in a resizeable window ... a
feature which isn't used by mythth ... it's a fullscreen application,
isn't it?
---
 mythtv/libs/libmythtv/videoout_quartz.cpp |  156 +++++++++++------------------
 1 files changed, 60 insertions(+), 96 deletions(-)

diff --git a/mythtv/libs/libmythtv/videoout_quartz.cpp b/mythtv/libs/libmythtv/videoout_quartz.cpp
index 450e945..78e8f15 100644
--- a/mythtv/libs/libmythtv/videoout_quartz.cpp
+++ b/mythtv/libs/libmythtv/videoout_quartz.cpp
@@ -87,14 +87,14 @@ class VideoOutputQuartzView
 
     virtual void InputChanged(int width, int height, float aspect,
                               MythCodecID av_codec_id);
-    virtual void MoveResize(QRect newRect);
+    virtual void MoveResize();
 
     virtual void EmbedChanged(bool embedded);
 
   protected:
     virtual bool Begin(void);
     virtual void End(void);
-    virtual void Transform(QRect newRect);
+    virtual void Transform();
     virtual void BlankScreen(bool deferred);
 
     // Subclasses implement the following methods:
@@ -106,7 +106,6 @@ class VideoOutputQuartzView
     QuartzData *       parentData;    // information about video source is here
 
     CGrafPtr           thePort;       // QuickDraw graphics port
-    QRect              m_desired;     // Desired output size characteristics
     ImageSequence      theCodec;      // QuickTime sequence ID
     RgnHandle          theMask;       // clipping region
 
@@ -179,6 +178,7 @@ class QuartzData
     Rect               windowBounds;      // dimensions, to restore size later
     CGDirectDisplayID  screen;            // screen containing main window
     float              refreshRate;       // for screen above
+    QRect              display_rect;
 
     // Global preferences:
     bool               drawInWindow;      // Fullscreen or in GUI view?
@@ -222,14 +222,6 @@ bool VideoOutputQuartzView::Begin(void)
         return false;
     }
 
-    // Set initial output size
-    Rect portBounds;
-    GetPortBounds(thePort, &portBounds);
-    VERBOSE(VB_PLAYBACK, QString("%0Viewport currently %1,%2 -> %3,%4")
-                         .arg(name).arg(portBounds.left).arg(portBounds.top)
-                         .arg(portBounds.right).arg(portBounds.bottom));
-    m_desired.setWidth(portBounds.right);
-    m_desired.setHeight(portBounds.bottom);
 #if 0
     // The clipping mask
     theMask = NewRgn();
@@ -270,7 +262,7 @@ bool VideoOutputQuartzView::Begin(void)
     viewLock.unlock();
 
     // set transformation matrix
-    Transform(m_desired);
+    Transform();
 
     return true;
 }
@@ -294,82 +286,35 @@ void VideoOutputQuartzView::End(void)
 }
 
 /// Build the transformation matrix to scale the video appropriately.
-void VideoOutputQuartzView::Transform(QRect newRect)
+void VideoOutputQuartzView::Transform()
 {
     MatrixRecord matrix;
     SetIdentityMatrix(&matrix);
 
-    int x, y, w, h, sw, sh;
-    x = newRect.left();
-    y = newRect.top();
-    w = newRect.width();
-    h = newRect.height();
-    sw = parentData->srcWidth;
-    sh = parentData->srcHeight;
-
-    // constants for transformation operations
-    Fixed one, zero;
-    one  = Long2Fix(1);
-    zero = Long2Fix(0);
-
-    VERBOSE(VB_PLAYBACK, QString("%0Viewport is %1 x %2")
-                                .arg(name).arg(w).arg(h));
-    VERBOSE(VB_PLAYBACK, QString("%0Image is %1 x %2")
-                                .arg(name).arg(sw).arg(sh));
-
-    double hscale = (double) w / sw;
-    double vscale = (double) h / sh;
-
-    // cap zooming if we requested it
-    if (!parentData->scaleUpVideo)
-    {
-        double maxScale = fmax(hscale, vscale);
-        hscale /= maxScale;
-        vscale /= maxScale;
-    }
-
-    if ((hscale < 0.99) || (hscale > 1.01) ||
-        (vscale < 0.99) || (vscale > 1.01))
-    {
-        VERBOSE(VB_PLAYBACK, QString("%0Scaling to %1 x %2 of original")
-                                    .arg(name).arg(hscale).arg(vscale));
-        ScaleMatrix(&matrix,
-                    X2Fix(hscale),
-                    X2Fix(vscale),
-                    zero, zero);
+    Rect Rsrc, Rdst;
+    QRect display_rect;
 
-        // reset sw, sh for new apparent width/height
-        sw = (int)(sw * hscale);
-        sh = (int)(sh * vscale);
-    }
+    Rsrc.left   = 0;
+    Rsrc.top    = 0;
+    Rsrc.right  = parentData->srcWidth;
+    Rsrc.bottom = parentData->srcHeight;
 
-    // center image in viewport
-    if ((h != sh) || (w != sw))
-    {
-        VERBOSE(VB_PLAYBACK, QString("%0Centering with %1, %2")
-                             .arg(name).arg((w - sw)/2.0).arg((h - sh)/2.0));
-        TranslateMatrix(&matrix, X2Fix((w - sw) / 2.0), X2Fix((h - sh) / 2.0));
-    }
+    display_rect= parentData->display_rect;
+    Rdst.left   = display_rect.left();
+    Rdst.top    = display_rect.top();
+    Rdst.right  = display_rect.right();
+    Rdst.bottom = display_rect.bottom();
 
-// apply the basic sizing to DVDV
-#ifdef USING_DVDV
-    if (parentData->dvdv)
-    {
-        parentData->dvdv->MoveResize(
-            0, 0, parentData->srcWidth, parentData->srcHeight,
-            (int)((w - sw) / 2.0), (int)((h - sh) / 2.0), sw, sh);
-    }
-#endif // USING_DVDV
-
-    // apply graphics port or embedding offset
-    if (x || y)
-    {
-        VERBOSE(VB_PLAYBACK, QString("%0Translating to %1, %2")
-                                    .arg(name).arg(x).arg(y));
-        TranslateMatrix(&matrix, Long2Fix(x), Long2Fix(y));
-    }
-
-    // Apply the transformation
+    MapMatrix(&matrix, &Rsrc, &Rdst);
+#if 0
+    VERBOSE(VB_PLAYBACK, QString("%0Viewport is now %1 / %2 / %3 / %4")
+                                .arg(name)
+                                .arg(Rdst.left)
+                                .arg(Rdst.top)
+                                .arg(Rdst.right)
+                                .arg(Rdst.bottom)
+    );
+#endif
     viewLock.lock();
     SetDSequenceMatrix(theCodec, &matrix);
     viewLock.unlock();
@@ -388,7 +333,7 @@ void VideoOutputQuartzView::BlankScreen(bool deferred)
     if (thePort)
     {
         SetPort(thePort);
-
+#if 0
         // set clipping rectangle
         Rect clipRect;
         if (m_desired.width() && m_desired.height())
@@ -402,6 +347,10 @@ void VideoOutputQuartzView::BlankScreen(bool deferred)
         {
             GetPortBounds(thePort, &clipRect);
         }
+#endif
+        Rect clipRect;
+        GetPortBounds(thePort, &clipRect);
+
         RgnHandle clipRgn = NewRgn();
         RectRgn(clipRgn, &clipRect);
 
@@ -472,10 +421,10 @@ void VideoOutputQuartzView::InputChanged(int width, int height, float aspect,
     Begin();
 }
 
-void VideoOutputQuartzView::MoveResize(QRect newRect)
+void VideoOutputQuartzView::MoveResize()
 {
     if (applyMoveResize)
-        Transform(newRect);
+        Transform();
 }
 
 /// Subclasses that block the main window should suspend
@@ -576,7 +525,7 @@ class VoqvEmbedded : public VideoOutputQuartzView
     VoqvEmbedded(QuartzData *pData, int x, int y, int w, int h)
     : VideoOutputQuartzView(pData)
     {
-        m_desired = QRect(x, y, w, h);
+        // m_desired = QRect(x, y, w, h);
         name = "Embedded window: ";
     };
 
@@ -741,6 +690,7 @@ class VoqvDock : public VideoOutputQuartzView
                     "BeginQDContextForApplicationDockTile failed");
             return false;
         }
+
         return true;
     };
 
@@ -790,11 +740,14 @@ class VoqvFloater : public VideoOutputQuartzView
             // Resize complete, reset the window drawing transformation
             Rect curBounds;
             GetPortBounds(thePort, &curBounds);
-            m_desired.setWidth(curBounds.right - curBounds.left);
-            m_desired.setHeight(curBounds.bottom - curBounds.top);
-            SetRectRgn(theMask, m_desired.left(),  m_desired.top(),
-                                m_desired.width(), m_desired.height());
-            Transform(m_desired);
+
+            VERBOSE(VB_IMPORTANT, "Fixme: resizing window");
+
+//          m_desired.setWidth(curBounds.right - curBounds.left);
+//          m_desired.setHeight(curBounds.bottom - curBounds.top);
+//          SetRectRgn(theMask, m_desired.left(),  m_desired.top(),
+//                              m_desired.width(), m_desired.height());
+            Transform();
         }
         resizing = startResizing;
     }
@@ -1056,6 +1009,7 @@ class VoqvDesktop : public VideoOutputQuartzView
             viewLock.unlock();
             return false;
         }
+
         viewLock.unlock();
         ShowWindow(window);
         // don't lose focus from main window
@@ -1130,8 +1084,9 @@ VideoOutputQuartz::~VideoOutputQuartz()
 
 void VideoOutputQuartz::VideoAspectRatioChanged(float aspect)
 {
-    VERBOSE(VB_PLAYBACK, (LOC + "VideoAspectRatioChanged(aspect=%1) [was %2]")
-                         .arg(aspect).arg(data->srcAspect));
+    //VERBOSE(VB_PLAYBACK, (LOC + "VideoAspectRatioChanged(aspect=%1) [was %2]")
+    //                     .arg(aspect).arg(data->srcAspect));
+    VERBOSE(VB_PLAYBACK, "QU: VideoAspectRatioChanged");
 
     VideoOutput::VideoAspectRatioChanged(aspect);
 
@@ -1155,7 +1110,7 @@ void VideoOutputQuartz::ToggleAdjustFill(AdjustFillMode adjustFill)
 
     // We could change all the views, but the user probably only
     // wants the main one (window or fullscreen) to change.
-    data->views[0]->MoveResize(windows[0].GetDisplayVideoRect());
+    data->views[0]->MoveResize();
 }
 
 void VideoOutputQuartz::MoveResize(void)
@@ -1164,12 +1119,17 @@ void VideoOutputQuartz::MoveResize(void)
     // the user's current aspect/fill/letterbox/zoom settings.
     VideoOutput::MoveResize();
 
-    QRect newRect = windows[0].GetDisplayVideoRect();
+    const QSize video_dim = windows[0].GetVideoDim();
+    data->srcWidth  = video_dim.width();
+    data->srcHeight = video_dim.height();
+    data->srcAspect = windows[0].GetVideoAspect();
+    data->srcMode   = db_aspectoverride;
+    data->display_rect = windows[0].GetDisplayVideoRect();
 
     vector<VideoOutputQuartzView*>::iterator it;
     for (it = data->views.begin(); it != data->views.end(); ++it)
     {
-        (*it)->MoveResize(newRect);
+        (*it)->MoveResize();
     }
 }
 
@@ -1224,8 +1184,9 @@ bool VideoOutputQuartz::InputChanged(const QSize &input_size,
 
     data->srcWidth  = video_dim.width();
     data->srcHeight = video_dim.height();
-    data->srcAspect = aspect;
+    data->srcAspect = windows[0].GetVideoAspect();
     data->srcMode   = db_aspectoverride;
+    data->display_rect = windows[0].GetDisplayVideoRect();
 
     CreateQuartzBuffers();
 
@@ -1280,6 +1241,7 @@ bool VideoOutputQuartz::Init(int width, int height, float aspect,
     data->srcHeight = video_dim.height();
     data->srcAspect = aspect;
     data->srcMode   = db_aspectoverride;
+    data->display_rect = windows[0].GetDisplayVideoRect();
 
     // Initialize QuickTime
     if (EnterMovies())
@@ -1609,6 +1571,7 @@ void VideoOutputQuartz::EmbedInWidget(int x, int y, int w, int h)
     VideoOutput::EmbedInWidget(x, y, w, h);
 
     data->pixelLock.lock();
+    data->display_rect = windows[0].GetDisplayVideoRect();
 
     // warn other views that embedding is starting
     vector<VideoOutputQuartzView*>::iterator it = data->views.begin();
@@ -1638,6 +1601,7 @@ void VideoOutputQuartz::StopEmbedding(void)
     VideoOutput::StopEmbedding();
 
     data->pixelLock.lock();
+    data->display_rect = windows[0].GetDisplayVideoRect();
 
     // delete embedded widget
     if (data->embeddedView)
-- 
1.6.5.1

