widget/gonk/HwcUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/HwcUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013 The Linux Foundation. All rights reserved.
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#include <android/log.h>
    1.21 +#include "HwcUtils.h"
    1.22 +#include "gfxUtils.h"
    1.23 +
    1.24 +#define LOG_TAG "HwcUtils"
    1.25 +
    1.26 +#if (LOG_NDEBUG == 0)
    1.27 +#define LOGD(args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, ## args)
    1.28 +#else
    1.29 +#define LOGD(args...) ((void)0)
    1.30 +#endif
    1.31 +
    1.32 +#define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
    1.33 +
    1.34 +
    1.35 +namespace mozilla {
    1.36 +
    1.37 +/* Utility functions for HwcComposer */
    1.38 +
    1.39 +
    1.40 +
    1.41 +/* static */ bool
    1.42 +HwcUtils::PrepareLayerRects(nsIntRect aVisible, const gfxMatrix& aTransform,
    1.43 +                            nsIntRect aClip, nsIntRect aBufferRect,
    1.44 +                            bool aYFlipped,
    1.45 +                            hwc_rect_t* aSourceCrop, hwc_rect_t* aVisibleRegionScreen) {
    1.46 +
    1.47 +    gfxRect visibleRect(aVisible);
    1.48 +    gfxRect clip(aClip);
    1.49 +    gfxRect visibleRectScreen = aTransform.TransformBounds(visibleRect);
    1.50 +    // |clip| is guaranteed to be integer
    1.51 +    visibleRectScreen.IntersectRect(visibleRectScreen, clip);
    1.52 +
    1.53 +    if (visibleRectScreen.IsEmpty()) {
    1.54 +        LOGD("Skip layer");
    1.55 +        return false;
    1.56 +    }
    1.57 +
    1.58 +    gfxMatrix inverse(aTransform);
    1.59 +    inverse.Invert();
    1.60 +    gfxRect crop = inverse.TransformBounds(visibleRectScreen);
    1.61 +
    1.62 +    //clip to buffer size
    1.63 +    crop.IntersectRect(crop, aBufferRect);
    1.64 +    crop.Round();
    1.65 +
    1.66 +    if (crop.IsEmpty()) {
    1.67 +        LOGD("Skip layer");
    1.68 +        return false;
    1.69 +    }
    1.70 +
    1.71 +    //propagate buffer clipping back to visible rect
    1.72 +    visibleRectScreen = aTransform.TransformBounds(crop);
    1.73 +    visibleRectScreen.Round();
    1.74 +
    1.75 +    // Map from layer space to buffer space
    1.76 +    crop -= aBufferRect.TopLeft();
    1.77 +    if (aYFlipped) {
    1.78 +        crop.y = aBufferRect.height - (crop.y + crop.height);
    1.79 +    }
    1.80 +
    1.81 +    aSourceCrop->left = crop.x;
    1.82 +    aSourceCrop->top  = crop.y;
    1.83 +    aSourceCrop->right  = crop.x + crop.width;
    1.84 +    aSourceCrop->bottom = crop.y + crop.height;
    1.85 +
    1.86 +    aVisibleRegionScreen->left = visibleRectScreen.x;
    1.87 +    aVisibleRegionScreen->top  = visibleRectScreen.y;
    1.88 +    aVisibleRegionScreen->right  = visibleRectScreen.x + visibleRectScreen.width;
    1.89 +    aVisibleRegionScreen->bottom = visibleRectScreen.y + visibleRectScreen.height;
    1.90 +
    1.91 +    return true;
    1.92 +}
    1.93 +
    1.94 +/* static */ bool
    1.95 +HwcUtils::PrepareVisibleRegion(const nsIntRegion& aVisible,
    1.96 +                               const gfxMatrix& aTransform,
    1.97 +                               nsIntRect aClip, nsIntRect aBufferRect,
    1.98 +                               RectVector* aVisibleRegionScreen) {
    1.99 +
   1.100 +    nsIntRegionRectIterator rect(aVisible);
   1.101 +    bool isVisible = false;
   1.102 +    while (const nsIntRect* visibleRect = rect.Next()) {
   1.103 +        hwc_rect_t visibleRectScreen;
   1.104 +        gfxRect screenRect;
   1.105 +
   1.106 +        screenRect.IntersectRect(gfxRect(*visibleRect), aBufferRect);
   1.107 +        screenRect = aTransform.TransformBounds(screenRect);
   1.108 +        screenRect.IntersectRect(screenRect, aClip);
   1.109 +        screenRect.Round();
   1.110 +        if (screenRect.IsEmpty()) {
   1.111 +            continue;
   1.112 +        }
   1.113 +        visibleRectScreen.left = screenRect.x;
   1.114 +        visibleRectScreen.top  = screenRect.y;
   1.115 +        visibleRectScreen.right  = screenRect.XMost();
   1.116 +        visibleRectScreen.bottom = screenRect.YMost();
   1.117 +        aVisibleRegionScreen->push_back(visibleRectScreen);
   1.118 +        isVisible = true;
   1.119 +    }
   1.120 +
   1.121 +    return isVisible;
   1.122 +}
   1.123 +
   1.124 +/* static */ bool
   1.125 +HwcUtils::CalculateClipRect(const gfxMatrix& aTransform,
   1.126 +                            const nsIntRect* aLayerClip,
   1.127 +                            nsIntRect aParentClip, nsIntRect* aRenderClip) {
   1.128 +
   1.129 +    *aRenderClip = aParentClip;
   1.130 +
   1.131 +    if (!aLayerClip) {
   1.132 +        return true;
   1.133 +    }
   1.134 +
   1.135 +    if (aLayerClip->IsEmpty()) {
   1.136 +        return false;
   1.137 +    }
   1.138 +
   1.139 +    nsIntRect clip = *aLayerClip;
   1.140 +
   1.141 +    gfxRect r(clip);
   1.142 +    gfxRect trClip = aTransform.TransformBounds(r);
   1.143 +    trClip.Round();
   1.144 +    gfxUtils::GfxRectToIntRect(trClip, &clip);
   1.145 +
   1.146 +    aRenderClip->IntersectRect(*aRenderClip, clip);
   1.147 +    return true;
   1.148 +}
   1.149 +
   1.150 +} // namespace mozilla

mercurial