2012-11-19 09:58:37 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, 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 <EGL/egl.h>
|
|
|
|
#include <hardware/hardware.h>
|
|
|
|
|
|
|
|
#include "Framebuffer.h"
|
|
|
|
#include "HwcComposer2D.h"
|
|
|
|
#include "LayerManagerOGL.h"
|
|
|
|
#include "mozilla/layers/PLayers.h"
|
|
|
|
#include "mozilla/layers/ShadowLayerUtilsGralloc.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsIScreenManager.h"
|
|
|
|
#include "nsMathUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-01-09 04:56:37 -08:00
|
|
|
#include "cutils/properties.h"
|
2012-11-19 09:58:37 -08:00
|
|
|
|
|
|
|
#define LOG_TAG "HWComposer"
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
#define LAYER_COUNT_INCREMENTS 5
|
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
HWC_USE_GPU = HWC_FRAMEBUFFER,
|
|
|
|
HWC_USE_OVERLAY = HWC_OVERLAY,
|
|
|
|
HWC_USE_COPYBIT
|
|
|
|
};
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
// HWC layer flags
|
|
|
|
enum {
|
|
|
|
// Draw a solid color rectangle
|
|
|
|
// The color should be set on the transform member of the hwc_layer_t struct
|
|
|
|
// The expected format is a 32 bit ABGR with 8 bits per component
|
|
|
|
HWC_COLOR_FILL = 0x8
|
|
|
|
};
|
|
|
|
|
2012-11-19 09:58:37 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
static StaticRefPtr<HwcComposer2D> sInstance;
|
|
|
|
|
|
|
|
HwcComposer2D::HwcComposer2D()
|
|
|
|
: mMaxLayerCount(0)
|
|
|
|
, mList(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HwcComposer2D::~HwcComposer2D() {
|
|
|
|
free(mList);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HwcComposer2D::Init(hwc_display_t dpy, hwc_surface_t sur)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!Initialized());
|
|
|
|
|
|
|
|
if (int err = init()) {
|
|
|
|
LOGE("Failed to initialize hwc");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntSize screenSize;
|
|
|
|
|
|
|
|
mozilla::Framebuffer::GetSize(&screenSize);
|
|
|
|
mScreenWidth = screenSize.width;
|
|
|
|
mScreenHeight = screenSize.height;
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
char propValue[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.display.colorfill", propValue, "0");
|
|
|
|
mColorFill = (atoi(propValue) == 1) ? true : false;
|
|
|
|
|
2012-11-19 09:58:37 -08:00
|
|
|
mDpy = dpy;
|
|
|
|
mSur = sur;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HwcComposer2D*
|
|
|
|
HwcComposer2D::GetInstance()
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
LOGD("Creating new instance");
|
|
|
|
sInstance = new HwcComposer2D();
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HwcComposer2D::ReallocLayerList()
|
|
|
|
{
|
|
|
|
int size = sizeof(hwc_layer_list_t) +
|
|
|
|
((mMaxLayerCount + LAYER_COUNT_INCREMENTS) * sizeof(hwc_layer_t));
|
|
|
|
|
|
|
|
hwc_layer_list_t* listrealloc = (hwc_layer_list_t*)realloc(mList, size);
|
|
|
|
|
|
|
|
if (!listrealloc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mList) {
|
|
|
|
//first alloc, initialize
|
|
|
|
listrealloc->numHwLayers = 0;
|
|
|
|
listrealloc->flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mList = listrealloc;
|
|
|
|
mMaxLayerCount += LAYER_COUNT_INCREMENTS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HwcComposer2D::GetRotation()
|
|
|
|
{
|
|
|
|
int halrotation = 0;
|
|
|
|
uint32_t screenrotation;
|
|
|
|
|
|
|
|
if (!mScreen) {
|
|
|
|
nsCOMPtr<nsIScreenManager> screenMgr =
|
|
|
|
do_GetService("@mozilla.org/gfx/screenmanager;1");
|
|
|
|
if (screenMgr) {
|
|
|
|
screenMgr->GetPrimaryScreen(getter_AddRefs(mScreen));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mScreen) {
|
|
|
|
if (NS_SUCCEEDED(mScreen->GetRotation(&screenrotation))) {
|
|
|
|
switch (screenrotation) {
|
|
|
|
case nsIScreen::ROTATION_0_DEG:
|
|
|
|
halrotation = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIScreen::ROTATION_90_DEG:
|
|
|
|
halrotation = HWC_TRANSFORM_ROT_90;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIScreen::ROTATION_180_DEG:
|
|
|
|
halrotation = HWC_TRANSFORM_ROT_180;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIScreen::ROTATION_270_DEG:
|
|
|
|
halrotation = HWC_TRANSFORM_ROT_270;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return halrotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets hwc layer rectangles required for hwc composition
|
|
|
|
*
|
|
|
|
* @param aVisible Input. Layer's unclipped visible rectangle
|
2013-01-10 02:51:43 -08:00
|
|
|
* The origin is the top-left corner of the layer
|
2012-11-19 09:58:37 -08:00
|
|
|
* @param aTransform Input. Layer's transformation matrix
|
|
|
|
* It transforms from layer space to screen space
|
|
|
|
* @param aClip Input. A clipping rectangle.
|
|
|
|
* The origin is the top-left corner of the screen
|
|
|
|
* @param aBufferRect Input. The layer's buffer bounds
|
2013-01-10 02:51:43 -08:00
|
|
|
* The origin is the top-left corner of the layer
|
2012-11-19 09:58:37 -08:00
|
|
|
* @param aSurceCrop Output. Area of the source to consider,
|
|
|
|
* the origin is the top-left corner of the buffer
|
|
|
|
* @param aVisibleRegionScreen Output. Visible region in screen space.
|
|
|
|
* The origin is the top-left corner of the screen
|
|
|
|
* @return true if the layer should be rendered.
|
|
|
|
* false if the layer can be skipped
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
PrepareLayerRects(nsIntRect aVisible, const gfxMatrix& aTransform,
|
|
|
|
nsIntRect aClip, nsIntRect aBufferRect,
|
|
|
|
hwc_rect_t* aSourceCrop, hwc_rect_t* aVisibleRegionScreen) {
|
|
|
|
|
|
|
|
gfxRect visibleRect(aVisible);
|
|
|
|
gfxRect clip(aClip);
|
|
|
|
gfxRect visibleRectScreen = aTransform.TransformBounds(visibleRect);
|
|
|
|
// |clip| is guaranteed to be integer
|
|
|
|
visibleRectScreen.IntersectRect(visibleRectScreen, clip);
|
|
|
|
|
|
|
|
if (visibleRectScreen.IsEmpty()) {
|
|
|
|
LOGD("Skip layer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix inverse(aTransform);
|
|
|
|
inverse.Invert();
|
|
|
|
gfxRect crop = inverse.TransformBounds(visibleRectScreen);
|
2013-01-10 02:51:43 -08:00
|
|
|
|
2012-11-19 09:58:37 -08:00
|
|
|
//clip to buffer size
|
|
|
|
crop.IntersectRect(crop, aBufferRect);
|
|
|
|
crop.RoundOut();
|
|
|
|
|
|
|
|
if (crop.IsEmpty()) {
|
|
|
|
LOGD("Skip layer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//propagate buffer clipping back to visible rect
|
2013-01-10 02:51:43 -08:00
|
|
|
visibleRectScreen = aTransform.TransformBounds(crop);
|
2012-11-19 09:58:37 -08:00
|
|
|
visibleRectScreen.RoundOut();
|
|
|
|
|
2013-01-10 02:51:43 -08:00
|
|
|
// Map from layer space to buffer space
|
|
|
|
crop -= aBufferRect.TopLeft();
|
|
|
|
|
2012-11-19 09:58:37 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HwcComposer2D::PrepareLayerList(Layer* aLayer,
|
|
|
|
const nsIntRect& aClip)
|
|
|
|
{
|
|
|
|
// NB: we fall off this path whenever there are container layers
|
|
|
|
// that require intermediate surfaces. That means all the
|
|
|
|
// GetEffective*() coordinates are relative to the framebuffer.
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
bool fillColor = false;
|
2012-11-19 09:58:37 -08:00
|
|
|
|
|
|
|
const nsIntRegion& visibleRegion = aLayer->GetEffectiveVisibleRegion();
|
|
|
|
if (visibleRegion.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float opacity = aLayer->GetEffectiveOpacity();
|
|
|
|
if (opacity <= 0) {
|
|
|
|
LOGD("Layer is fully transparent so skip rendering");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (opacity < 1) {
|
|
|
|
LOGD("Layer has planar semitransparency which is unsupported");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-09 09:18:13 -08:00
|
|
|
if (visibleRegion.GetNumRects() > 1) {
|
2012-11-19 09:58:37 -08:00
|
|
|
// FIXME/bug 808339
|
|
|
|
LOGD("Layer has nontrivial visible region");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ContainerLayer* container = aLayer->AsContainerLayer()) {
|
|
|
|
if (container->UseIntermediateSurface()) {
|
|
|
|
LOGD("Container layer needs intermediate surface");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsAutoTArray<Layer*, 12> children;
|
|
|
|
container->SortChildrenBy3DZOrder(children);
|
|
|
|
|
|
|
|
//FIXME/bug 810334
|
2013-01-07 15:21:50 -08:00
|
|
|
for (uint32_t i = 0; i < children.Length(); i++) {
|
2012-11-19 09:58:37 -08:00
|
|
|
if (!PrepareLayerList(children[i], aClip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerOGL* layerGL = static_cast<LayerOGL*>(aLayer->ImplData());
|
|
|
|
LayerRenderState state = layerGL->GetRenderState();
|
|
|
|
|
|
|
|
if (!state.mSurface ||
|
|
|
|
state.mSurface->type() != SurfaceDescriptor::TSurfaceDescriptorGralloc) {
|
2013-01-09 04:56:37 -08:00
|
|
|
if (aLayer->AsColorLayer() && mColorFill) {
|
|
|
|
fillColor = true;
|
|
|
|
} else {
|
|
|
|
LOGD("Layer doesn't have a gralloc buffer");
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-19 09:58:37 -08:00
|
|
|
}
|
|
|
|
if (state.BufferRotated()) {
|
|
|
|
LOGD("Layer has a rotated buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix transform;
|
|
|
|
const gfx3DMatrix& transform3D = aLayer->GetEffectiveTransform();
|
|
|
|
if (!transform3D.Is2D(&transform) || !transform.PreservesAxisAlignedRectangles()) {
|
|
|
|
LOGD("Layer has a 3D transform or a non-square angle rotation");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK! We can compose this layer with hwc.
|
|
|
|
|
|
|
|
int current = mList ? mList->numHwLayers : 0;
|
|
|
|
if (!mList || current >= mMaxLayerCount) {
|
|
|
|
if (!ReallocLayerList() || current >= mMaxLayerCount) {
|
|
|
|
LOGE("PrepareLayerList failed! Could not increase the maximum layer count");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
sp<GraphicBuffer> buffer = fillColor ? nullptr : GrallocBufferActor::GetFrom(*state.mSurface);
|
2012-11-19 09:58:37 -08:00
|
|
|
|
|
|
|
nsIntRect visibleRect = visibleRegion.GetBounds();
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
nsIntRect bufferRect;
|
|
|
|
if (fillColor) {
|
2013-01-10 02:51:43 -08:00
|
|
|
bufferRect = nsIntRect(visibleRect);
|
2013-01-09 04:56:37 -08:00
|
|
|
} else {
|
2013-01-10 02:51:43 -08:00
|
|
|
if(state.mHasOwnOffset) {
|
|
|
|
bufferRect = nsIntRect(state.mOffset.x, state.mOffset.y,
|
|
|
|
int(buffer->getWidth()), int(buffer->getHeight()));
|
|
|
|
} else {
|
|
|
|
bufferRect = nsIntRect(visibleRect.x, visibleRect.y,
|
|
|
|
int(buffer->getWidth()), int(buffer->getHeight()));
|
|
|
|
}
|
2013-01-09 04:56:37 -08:00
|
|
|
}
|
2012-11-19 09:58:37 -08:00
|
|
|
|
|
|
|
hwc_layer_t& hwcLayer = mList->hwLayers[current];
|
|
|
|
|
|
|
|
//FIXME/bug 810334
|
|
|
|
if(!PrepareLayerRects(visibleRect, transform, aClip, bufferRect,
|
|
|
|
&(hwcLayer.sourceCrop), &(hwcLayer.displayFrame))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
buffer_handle_t handle = fillColor ? nullptr : buffer->getNativeBuffer()->handle;
|
2012-11-19 09:58:37 -08:00
|
|
|
hwcLayer.handle = handle;
|
|
|
|
|
|
|
|
hwcLayer.flags = 0;
|
|
|
|
hwcLayer.hints = 0;
|
2013-01-09 04:56:37 -08:00
|
|
|
hwcLayer.blending = HWC_BLENDING_NONE;
|
2012-11-19 09:58:37 -08:00
|
|
|
hwcLayer.compositionType = HWC_USE_COPYBIT;
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
if (!fillColor) {
|
|
|
|
if (transform.xx == 0) {
|
|
|
|
if (transform.xy < 0) {
|
|
|
|
hwcLayer.transform = HWC_TRANSFORM_ROT_90;
|
|
|
|
LOGD("Layer buffer rotated 90 degrees");
|
|
|
|
} else {
|
|
|
|
hwcLayer.transform = HWC_TRANSFORM_ROT_270;
|
|
|
|
LOGD("Layer buffer rotated 270 degrees");
|
|
|
|
}
|
|
|
|
} else if (transform.xx < 0) {
|
|
|
|
hwcLayer.transform = HWC_TRANSFORM_ROT_180;
|
|
|
|
LOGD("Layer buffer rotated 180 degrees");
|
|
|
|
} else {
|
|
|
|
hwcLayer.transform = 0;
|
2012-11-19 09:58:37 -08:00
|
|
|
}
|
|
|
|
|
2013-01-09 04:56:37 -08:00
|
|
|
hwcLayer.transform |= state.YFlipped() ? HWC_TRANSFORM_FLIP_V : 0;
|
|
|
|
hwc_region_t region;
|
|
|
|
region.numRects = 1;
|
|
|
|
region.rects = &(hwcLayer.displayFrame);
|
|
|
|
hwcLayer.visibleRegionScreen = region;
|
|
|
|
} else {
|
|
|
|
hwcLayer.flags |= HWC_COLOR_FILL;
|
|
|
|
ColorLayer* colorLayer = static_cast<ColorLayer*>(layerGL->GetLayer());
|
|
|
|
hwcLayer.transform = colorLayer->GetColor().Packed();
|
|
|
|
}
|
2012-11-19 09:58:37 -08:00
|
|
|
|
|
|
|
mList->numHwLayers++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HwcComposer2D::TryRender(Layer* aRoot,
|
|
|
|
const gfxMatrix& aGLWorldTransform)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Initialized());
|
|
|
|
if (mList) {
|
|
|
|
mList->numHwLayers = 0;
|
|
|
|
}
|
|
|
|
// XXX use GL world transform instead of GetRotation()
|
|
|
|
int rotation = GetRotation();
|
|
|
|
|
|
|
|
int fbHeight, fbWidth;
|
|
|
|
|
|
|
|
if (rotation == 0 || rotation == HWC_TRANSFORM_ROT_180) {
|
|
|
|
fbWidth = mScreenWidth;
|
|
|
|
fbHeight = mScreenHeight;
|
|
|
|
} else {
|
|
|
|
fbWidth = mScreenHeight;
|
|
|
|
fbHeight = mScreenWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PrepareLayerList(aRoot, nsIntRect(0, 0, fbWidth, fbHeight))) {
|
|
|
|
LOGD("Render aborted. Nothing was drawn to the screen");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHwc->set(mHwc, mDpy, mSur, mList)) {
|
|
|
|
LOGE("Hardware device failed to render");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGD("Frame rendered");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|