/*----------------------------------------------------------------------------*/
Bool FullRotatedRects_rv (void *vB, UN_DATAAIMS OldAim, UN_DATAAIMS NewAim
					   , int32_t SkewReg, char *pStr, char *pWho)
{
	int Ind, nTeor, nPartUnits, i;
	UN_BUFF *pB;
	Rect16 *pRcRe, *pRcId;
	pB = (UN_BUFF *)vB;
	/*  Доступ к преобразуемым данным.  */
	Ind = FindSuchAimedData (vB, UN_DT_Rect16, OldAim);
	if (Ind < 0)
		return RV_FALSE;
	nTeor = sizeof (Rect16);
	nPartUnits = pB->nPartUnits[Ind];
	if (nPartUnits > pB->SizeCurr/nTeor)
	{
		sprintf (pStr, "Не хватило памяти под %d %s!"
			, nPartUnits-pB->SizeCurr/nTeor, pWho);
		return RV_DOUBT;
	}
	pRcRe = (Rect16 *)(pB->vPart[Ind]);
	pRcId = (Rect16 *)(pB->vCurr);
	for (i=0; i<nPartUnits; i++)
	{
		RotateRect (pRcRe, pRcId, SkewReg);
		pRcRe++;
		pRcId++;
	}
	EndLastDataPart (vB, NewAim, UN_DT_Rect16, nTeor, nPartUnits);
	return RV_TRUE;
}
void
ClientLayerManager::MakeSnapshotIfRequired()
{
  if (!mShadowTarget) {
    return;
  }
  if (mWidget) {
    if (CompositorBridgeChild* remoteRenderer = GetRemoteRenderer()) {
      // The compositor doesn't draw to a different sized surface
      // when there's a rotation. Instead we rotate the result
      // when drawing into dt
      LayoutDeviceIntRect outerBounds;
      mWidget->GetBounds(outerBounds);

      IntRect bounds = ToOutsideIntRect(mShadowTarget->GetClipExtents());
      if (mTargetRotation) {
        bounds =
          RotateRect(bounds, outerBounds.ToUnknownRect(), mTargetRotation);
      }

      SurfaceDescriptor inSnapshot;
      if (!bounds.IsEmpty() &&
          mForwarder->AllocSurfaceDescriptor(bounds.Size(),
                                             gfxContentType::COLOR_ALPHA,
                                             &inSnapshot)) {

        // Make a copy of |inSnapshot| because the call to send it over IPC
        // will call forget() on the Shmem inside, and zero it out.
        SurfaceDescriptor outSnapshot = inSnapshot;

        if (remoteRenderer->SendMakeSnapshot(inSnapshot, bounds)) {
          RefPtr<DataSourceSurface> surf = GetSurfaceForDescriptor(outSnapshot);
          DrawTarget* dt = mShadowTarget->GetDrawTarget();

          Rect dstRect(bounds.x, bounds.y, bounds.width, bounds.height);
          Rect srcRect(0, 0, bounds.width, bounds.height);

          gfx::Matrix rotate =
            ComputeTransformForUnRotation(outerBounds.ToUnknownRect(),
                                          mTargetRotation);

          gfx::Matrix oldMatrix = dt->GetTransform();
          dt->SetTransform(rotate * oldMatrix);
          dt->DrawSurface(surf, dstRect, srcRect,
                          DrawSurfaceOptions(),
                          DrawOptions(1.0f, CompositionOp::OP_OVER));
          dt->SetTransform(oldMatrix);
        }
        mForwarder->DestroySurfaceDescriptor(&outSnapshot);
      }
    }
  }
  mShadowTarget = nullptr;
}
void
ClientLayerManager::MakeSnapshotIfRequired()
{
  if (!mShadowTarget) {
    return;
  }
  if (mWidget) {
    if (CompositorChild* remoteRenderer = GetRemoteRenderer()) {
      // The compositor doesn't draw to a different sized surface
      // when there's a rotation. Instead we rotate the result
      // when drawing into dt
      nsIntRect outerBounds;
      mWidget->GetBounds(outerBounds);

      nsIntRect bounds = ToOutsideIntRect(mShadowTarget->GetClipExtents());
      if (mTargetRotation) {
        bounds = RotateRect(bounds, outerBounds, mTargetRotation);
      }

      SurfaceDescriptor inSnapshot;
      if (!bounds.IsEmpty() &&
          mForwarder->AllocSurfaceDescriptor(bounds.Size().ToIntSize(),
                                             gfxContentType::COLOR_ALPHA,
                                             &inSnapshot) &&
          remoteRenderer->SendMakeSnapshot(inSnapshot, bounds)) {
        RefPtr<DataSourceSurface> surf = GetSurfaceForDescriptor(inSnapshot);
        DrawTarget* dt = mShadowTarget->GetDrawTarget();

        Rect dstRect(bounds.x, bounds.y, bounds.width, bounds.height);
        Rect srcRect(0, 0, bounds.width, bounds.height);

        gfx::Matrix rotate = ComputeTransformForUnRotation(outerBounds, mTargetRotation);

        gfx::Matrix oldMatrix = dt->GetTransform();
        dt->SetTransform(oldMatrix * rotate);
        dt->DrawSurface(surf, dstRect, srcRect,
                        DrawSurfaceOptions(),
                        DrawOptions(1.0f, CompositionOp::OP_OVER));
        dt->SetTransform(oldMatrix);
      }
      mForwarder->DestroySharedSurface(&inSnapshot);
    }
  }
  mShadowTarget = nullptr;
}
void RenderScissorComponent::FilterOnRender(VariantList *pVList)
{

	CHECK_GL_ERROR()
	g_globalBatcher.Flush();

	GLboolean b = false;
	
	//Note: This fails when using the webOS emulation libs on Windows.. but works on the real device
	glGetBooleanv(GL_SCISSOR_TEST, &b);
	
#if defined(WIN32) && defined(RT_WEBOS) && defined(_DEBUG)
	//clear the error out so it doesn't flood our log
	glGetError();
#endif
	m_bOldScissorEnabled = b != 0;
	CHECK_GL_ERROR()
	if (m_bOldScissorEnabled)
	{
		//warning: Untested code...
		GLint nums[4];
		glGetIntegerv(GL_SCISSOR_BOX, &nums[0]);
		m_oldScissorPos = CL_Vec2f((float)nums[0],(float) nums[1]);
		m_oldScissorSize = CL_Vec2f((float)nums[2],(float) nums[3]);
		CHECK_GL_ERROR()
	}

	CL_Vec2f vFinalPos = pVList->m_variant[0].GetVector2()+*m_pPos2d;
	//vFinalPos -= GetAlignmentOffset(*m_pSize2d, eAlignment(*m_pAlignment));	

	CL_Rectf clipRect(vFinalPos.x, vFinalPos.y, vFinalPos.x+m_pSize2d->x, vFinalPos.y+m_pSize2d->y);
		
	//well, turns out we need to always do this on iOS, as this accounts for the screen being rotated as well, not just
	//the scaling issue

//	if (NeedToUseFakeScreenSize())
	{
		//Oh shit-sticks. We're stretching our content and using a fake screensize.  We'll need to convert
		//our glScissor rect to match the real gl surface size.
	
		float angle = OrientationToDegrees(GetOrientation());
		while (angle < 0)
		{
			angle+= 360;
		}
		rtRectf r = rtRectf(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
		r = ConvertFakeScreenRectToReal(r);
		clipRect = CL_Rectf(r.left, r.top, r.right, r.bottom);

		float primaryX = (float)GetPrimaryGLX();
		float primaryY = (float)GetPrimaryGLY();
		
		if (InLandscapeGUIMode())
		{
			swap(primaryX, primaryY);
		}
		
		clipRect = RotateRect(clipRect, angle, CL_Vec2f(primaryX, primaryY));
	}

	//remember, glScissors x/y is the LOWER LEFT of the rect, not upper left. (and lower left is 0,0)
	glScissor((GLint)clipRect.left, (GLint)GetPrimaryGLY()-((GLint)clipRect.top+(GLint)clipRect.get_height()), (GLint)clipRect.get_width(),(GLint) clipRect.get_height());
	glEnable(GL_SCISSOR_TEST);

}