Bumping manifests a=b2g-bump
[gecko.git] / widget / gonk / HwcUtils.cpp
bloba99b70872ad35b691a4329687d18d14f3f037767
1 /*
2 * Copyright (c) 2013 The Linux Foundation. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <android/log.h>
18 #include "HwcUtils.h"
19 #include "gfxUtils.h"
20 #include "gfx2DGlue.h"
22 #define LOG_TAG "HwcUtils"
24 #if (LOG_NDEBUG == 0)
25 #define LOGD(args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, ## args)
26 #else
27 #define LOGD(args...) ((void)0)
28 #endif
30 #define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
33 namespace mozilla {
35 /* Utility functions for HwcComposer */
39 /* static */ bool
40 HwcUtils::PrepareLayerRects(nsIntRect aVisible,
41 const gfx::Matrix& aLayerTransform,
42 const gfx::Matrix& aLayerBufferTransform,
43 nsIntRect aClip, nsIntRect aBufferRect,
44 bool aYFlipped,
45 hwc_rect_t* aSourceCrop, hwc_rect_t* aVisibleRegionScreen) {
47 gfxMatrix aTransform = gfx::ThebesMatrix(aLayerTransform);
48 gfxRect visibleRect(aVisible);
49 gfxRect clip(aClip);
50 gfxRect visibleRectScreen = aTransform.TransformBounds(visibleRect);
51 // |clip| is guaranteed to be integer
52 visibleRectScreen.IntersectRect(visibleRectScreen, clip);
54 if (visibleRectScreen.IsEmpty()) {
55 return false;
58 gfxMatrix inverse = gfx::ThebesMatrix(aLayerBufferTransform);
59 inverse.Invert();
60 gfxRect crop = inverse.TransformBounds(visibleRectScreen);
62 //clip to buffer size
63 crop.IntersectRect(crop, aBufferRect);
64 crop.Round();
66 if (crop.IsEmpty()) {
67 return false;
70 //propagate buffer clipping back to visible rect
71 gfxMatrix layerBufferTransform = gfx::ThebesMatrix(aLayerBufferTransform);
72 visibleRectScreen = layerBufferTransform.TransformBounds(crop);
73 visibleRectScreen.Round();
75 // Map from layer space to buffer space
76 crop -= aBufferRect.TopLeft();
77 if (aYFlipped) {
78 crop.y = aBufferRect.height - (crop.y + crop.height);
81 aSourceCrop->left = crop.x;
82 aSourceCrop->top = crop.y;
83 aSourceCrop->right = crop.x + crop.width;
84 aSourceCrop->bottom = crop.y + crop.height;
86 aVisibleRegionScreen->left = visibleRectScreen.x;
87 aVisibleRegionScreen->top = visibleRectScreen.y;
88 aVisibleRegionScreen->right = visibleRectScreen.x + visibleRectScreen.width;
89 aVisibleRegionScreen->bottom = visibleRectScreen.y + visibleRectScreen.height;
91 return true;
94 /* static */ bool
95 HwcUtils::PrepareVisibleRegion(const nsIntRegion& aVisible,
96 const gfx::Matrix& aLayerTransform,
97 const gfx::Matrix& aLayerBufferTransform,
98 nsIntRect aClip, nsIntRect aBufferRect,
99 RectVector* aVisibleRegionScreen) {
101 gfxMatrix layerTransform = gfx::ThebesMatrix(aLayerTransform);
102 gfxMatrix layerBufferTransform = gfx::ThebesMatrix(aLayerBufferTransform);
103 gfxRect bufferRect = layerBufferTransform.TransformBounds(aBufferRect);
104 nsIntRegionRectIterator rect(aVisible);
105 bool isVisible = false;
106 while (const nsIntRect* visibleRect = rect.Next()) {
107 hwc_rect_t visibleRectScreen;
108 gfxRect screenRect;
110 screenRect = layerTransform.TransformBounds(gfxRect(*visibleRect));
111 screenRect.IntersectRect(screenRect, bufferRect);
112 screenRect.IntersectRect(screenRect, aClip);
113 screenRect.Round();
114 if (screenRect.IsEmpty()) {
115 continue;
117 visibleRectScreen.left = screenRect.x;
118 visibleRectScreen.top = screenRect.y;
119 visibleRectScreen.right = screenRect.XMost();
120 visibleRectScreen.bottom = screenRect.YMost();
121 aVisibleRegionScreen->push_back(visibleRectScreen);
122 isVisible = true;
125 return isVisible;
128 /* static */ bool
129 HwcUtils::CalculateClipRect(const gfx::Matrix& transform,
130 const nsIntRect* aLayerClip,
131 nsIntRect aParentClip, nsIntRect* aRenderClip) {
133 gfxMatrix aTransform = gfx::ThebesMatrix(transform);
134 *aRenderClip = aParentClip;
136 if (!aLayerClip) {
137 return true;
140 if (aLayerClip->IsEmpty()) {
141 return false;
144 nsIntRect clip = *aLayerClip;
146 gfxRect r(clip);
147 gfxRect trClip = aTransform.TransformBounds(r);
148 trClip.Round();
149 gfxUtils::GfxRectToIntRect(trClip, &clip);
151 aRenderClip->IntersectRect(*aRenderClip, clip);
152 return true;
155 } // namespace mozilla