Repository: xbmc/xbmc
File: xbmc/cores/RetroPlayer/rendering/VideoRenderers/RPBaseRenderer.cpp (C++)

Code before:
  float zoomAmount = 1.0f;
  m_sourceRect.x1 = 0.0f;
  m_sourceRect.y1 = 0.0f;
  m_sourceRect.x2 = static_cast<float>(sourceWidth);
  m_sourceRect.y2 = static_cast<float>(sourceHeight);
  if (scaleMode == SCALINGMETHOD::DISPLAY && stretchMode == STRETCHMODE::Original && bhw_filter)
  {
    RESOLUTION_INFO m_gameRes=m_context.GetResInfo();
    m_gameRes.Overscan.left = 0;
    m_gameRes.Overscan.top = 0;
    m_gameRes.Overscan.right = sourceWidth;
    m_gameRes.Overscan.bottom = sourceHeight;
    m_gameRes.iWidth = sourceWidth;
    m_gameRes.iHeight = sourceHeight;
    m_gameRes.iScreenWidth = sourceWidth;
    m_gameRes.iScreenHeight = sourceHeight;
    CServiceBroker::GetWinSystem()->SetFullScreen(true,  m_gameRes, false);

    screenWidth = sourceWidth;
    screenHeight = sourceHeight;
    screenPixelRatio = 1.0;
    viewRect.x1 =0;
    viewRect.y1 =0;
    viewRect.x2 =sourceWidth;
    viewRect.y2 =sourceHeight;
    CRenderUtils::CalculateStretchMode(stretchMode, rotationDegCCW, sourceWidth, sourceHeight,
                                       screenWidth, screenHeight, pixelRatio, zoomAmount);
    CRenderUtils::CalcNormalRenderRect(viewRect, sourceAspectRatio * pixelRatio, zoomAmount,destRect);
    if (!(m_context.IsFullScreenVideo() || m_context.IsCalibrating()))
      CRenderUtils::ClipRect(viewRect, m_sourceRect, destRect);
    m_rotatedDestCoords = CRenderUtils::ReorderDrawPoints(destRect, rotationDegCCW);
  }
  else
  {
    // Calculate pixel ratio and zoom amount
    CRenderUtils::CalculateStretchMode(stretchMode, rotationDegCCW, sourceWidth, sourceHeight,
                                       screenWidth, screenHeight, pixelRatio, zoomAmount);

    // Calculate destination dimensions
    CRect destRect;
    CRenderUtils::CalcNormalRenderRect(viewRect, sourceAspectRatio * pixelRatio, zoomAmount,
                                       destRect);

    // Clip as needed
    if (!(m_context.IsFullScreenVideo() || m_context.IsCalibrating()))
      CRenderUtils::ClipRect(viewRect, m_sourceRect, destRect);

    // Adapt the drawing rect points if we have to rotate
    m_rotatedDestCoords = CRenderUtils::ReorderDrawPoints(destRect, rotationDegCCW);
  }
}

Review comment (refactor):
`CalculateStretchMode()` is duplicated here. It and other CRenderUtils friends encapsulate some pretty extensive math and logic, and I'd prefer calls to each on a single codepath instead of the two branches of an `if`.

The math and logic I stole from VideoPlayer. It was jumbled and full of globals, and I tediously untangled it into the CRenderUtils functions you see. You can still see some dependencies left over, where later logic depends on the output of previous logic. That's why I say it's best to keep the sequence of these function calls on a single codepath.

Code after:
    GetScreenDimensions(screenWidth, screenHeight, screenPixelRatio);
  }

  // Entire target rendering area for the video (including black bars)
  const CRect viewRect = m_context.GetViewWindow();

  // Calculate pixel ratio and zoom amount
  float pixelRatio = 1.0f;
  float zoomAmount = 1.0f;
  CRenderUtils::CalculateStretchMode(stretchMode, rotationDegCCW, sourceWidth, sourceHeight,
                                     screenWidth, screenHeight, pixelRatio, zoomAmount);

  // Calculate destination dimensions
  CRect destRect;
  CRenderUtils::CalcNormalRenderRect(viewRect, sourceAspectRatio * pixelRatio, zoomAmount,
                                     destRect);

  m_sourceRect.x1 = 0.0f;
  m_sourceRect.y1 = 0.0f;
  m_sourceRect.x2 = static_cast<float>(sourceWidth);
  m_sourceRect.y2 = static_cast<float>(sourceHeight);

  // Clip as needed
  if (!(m_context.IsFullScreenVideo() || m_context.IsCalibrating()))
    CRenderUtils::ClipRect(viewRect, m_sourceRect, destRect);

  // Adapt the drawing rect points if we have to rotate
  m_rotatedDestCoords = CRenderUtils::ReorderDrawPoints(destRect, rotationDegCCW);
}

void CRPBaseRenderer::MarkDirty()
{
  // CServiceBroker::GetGUI()->GetWindowManager().MarkDirty(m_dimensions); //! @todo
}

void CRPBaseRenderer::PreRender(bool clear)
{
  if (!m_bConfigured)
    return;

  // Clear screen
  if (clear)
    m_context.Clear(m_context.UseLimitedColor() ? 0x101010 : 0);
}

void CRPBaseRenderer::PostRender()
{
  m_context.ApplyStateBlock();
}

void CRPBaseRenderer::GetScreenDimensions(float& screenWidth,