gecko-dev/widget/gonk/HwcUtils.cpp
Nicholas Nethercote 489c83b1dd Bug 1223690 - Remove implicit Rect conversions. r=jrmuizel.
gfxRect can be implicitly constructed from IntRect, which hides a number of
implicit conversion points, makes Moz2Dification harder, and has some
surprising effects.

This patch removes the implicit constructor and replaces it with an explicit
conversion function:

  gfxRect ThebesRect(const IntRect&)

This is the obvious outcome of removing the constructor.

But there is also a second, less obvious outcome: currently we do a number of
IntRect-to-Rect conversions using ToRect(), which (surprisingly) works because
it turns into an implicit IntRect-to-gfxRect conversion (via the implicit
constructor) combined with an explicit gfxRect-to-Rect conversion (via
ToRect()). I.e. we do two conversions, going from a Moz2D type to a Thebes
type and back to a Moz2D type!

So this patch also changes these conversion. It moves this existing function:

  Rect ToRect(const IntRect&)

from gfx2DGlue.h -- where it doesn't really belong because it doesn't involve
any Thebes types -- to gfx/2d/Rect.h, templatifying and renaming it as
IntRectToRect() in the process.

The rest of the patch deals with fall-out from these changes. The call sites
change as follows:

- IntRect-to-gfxRect conversions:
  - old: implicit
  - new: ThebesRect()

- IntRect-to-Rect conversions:
  - old: ToRect()
  - new: IntRectToRect()

--HG--
extra : rebase_source : e4e4c2ad10b36ecad4d57d1630158f3374e403be
2015-11-11 14:23:14 -08:00

170 lines
5.5 KiB
C++

/*
* Copyright (c) 2013 The Linux Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android/log.h>
#include "HwcUtils.h"
#include "gfxUtils.h"
#include "gfx2DGlue.h"
#define LOG_TAG "HwcUtils"
#if (LOG_NDEBUG == 0)
#define LOGD(args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, ## args)
#else
#define LOGD(args...) ((void)0)
#endif
#define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
namespace mozilla {
/* Utility functions for HwcComposer */
/* static */ bool
HwcUtils::PrepareLayerRects(nsIntRect aVisible,
const gfx::Matrix& aLayerTransform,
const gfx::Matrix& aLayerBufferTransform,
nsIntRect aClip, nsIntRect aBufferRect,
bool aYFlipped,
hwc_rect_t* aSourceCrop, hwc_rect_t* aVisibleRegionScreen) {
gfxMatrix aTransform = gfx::ThebesMatrix(aLayerTransform);
gfxRect visibleRect(ThebesRect(aVisible));
gfxRect clip(ThebesRect(aClip));
gfxRect visibleRectScreen = aTransform.TransformBounds(visibleRect);
// |clip| is guaranteed to be integer
visibleRectScreen.IntersectRect(visibleRectScreen, clip);
if (visibleRectScreen.IsEmpty()) {
return false;
}
gfxMatrix inverse = gfx::ThebesMatrix(aLayerBufferTransform);
inverse.Invert();
gfxRect crop = inverse.TransformBounds(visibleRectScreen);
//clip to buffer size
crop.IntersectRect(crop, ThebesRect(aBufferRect));
crop.Round();
if (crop.IsEmpty()) {
return false;
}
//propagate buffer clipping back to visible rect
gfxMatrix layerBufferTransform = gfx::ThebesMatrix(aLayerBufferTransform);
visibleRectScreen = layerBufferTransform.TransformBounds(crop);
visibleRectScreen.Round();
// Map from layer space to buffer space
crop -= aBufferRect.TopLeft();
if (aYFlipped) {
crop.y = aBufferRect.height - (crop.y + crop.height);
}
aSourceCrop->left = crop.x;
aSourceCrop->top = crop.y;
aSourceCrop->right = crop.x + crop.width;
aSourceCrop->bottom = crop.y + crop.height;
aVisibleRegionScreen->left = visibleRectScreen.x;
aVisibleRegionScreen->top = visibleRectScreen.y;
aVisibleRegionScreen->right = visibleRectScreen.x + visibleRectScreen.width;
aVisibleRegionScreen->bottom = visibleRectScreen.y + visibleRectScreen.height;
return true;
}
/* static */ bool
HwcUtils::PrepareVisibleRegion(const nsIntRegion& aVisible,
const gfx::Matrix& aLayerTransform,
const gfx::Matrix& aLayerBufferTransform,
nsIntRect aClip, nsIntRect aBufferRect,
RectVector* aVisibleRegionScreen,
bool& aIsVisible) {
const float MIN_SRC_WIDTH = 2.f;
const float MIN_SRC_HEIGHT = 2.f;
gfxMatrix layerTransform = gfx::ThebesMatrix(aLayerTransform);
gfxMatrix layerBufferTransform = gfx::ThebesMatrix(aLayerBufferTransform);
gfxRect bufferRect =
layerBufferTransform.TransformBounds(ThebesRect(aBufferRect));
gfxMatrix inverse = gfx::ThebesMatrix(aLayerBufferTransform);
inverse.Invert();
nsIntRegionRectIterator rect(aVisible);
aIsVisible = false;
while (const nsIntRect* visibleRect = rect.Next()) {
hwc_rect_t visibleRectScreen;
gfxRect screenRect;
screenRect = layerTransform.TransformBounds(ThebesRect(*visibleRect));
screenRect.IntersectRect(screenRect, bufferRect);
screenRect.IntersectRect(screenRect, ThebesRect(aClip));
screenRect.Round();
if (screenRect.IsEmpty()) {
continue;
}
visibleRectScreen.left = screenRect.x;
visibleRectScreen.top = screenRect.y;
visibleRectScreen.right = screenRect.XMost();
visibleRectScreen.bottom = screenRect.YMost();
gfxRect srcCrop = inverse.TransformBounds(screenRect);
// When src crop is very small, HWC could not render correctly in some cases.
// See Bug 1169093
if(srcCrop.Width() < MIN_SRC_WIDTH || srcCrop.Height() < MIN_SRC_HEIGHT) {
return false;
}
aVisibleRegionScreen->push_back(visibleRectScreen);
aIsVisible = true;
}
return true;
}
/* static */ bool
HwcUtils::CalculateClipRect(const gfx::Matrix& transform,
const nsIntRect* aLayerClip,
nsIntRect aParentClip, nsIntRect* aRenderClip) {
gfxMatrix aTransform = gfx::ThebesMatrix(transform);
*aRenderClip = aParentClip;
if (!aLayerClip) {
return true;
}
if (aLayerClip->IsEmpty()) {
return false;
}
nsIntRect clip = *aLayerClip;
gfxRect r = ThebesRect(clip);
gfxRect trClip = aTransform.TransformBounds(r);
trClip.Round();
gfxUtils::GfxRectToIntRect(trClip, &clip);
aRenderClip->IntersectRect(*aRenderClip, clip);
return true;
}
} // namespace mozilla