1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Corporation code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2011
19 * the Initial Developer. All Rights Reserved.
22 * Robert O'Callahan <robert@ocallahan.org>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "ReadbackProcessor.h"
44 ReadbackProcessor::BuildUpdates(ContainerLayer
* aContainer
)
46 NS_ASSERTION(mAllUpdates
.IsEmpty(), "Some updates not processed?");
48 if (!aContainer
->mMayHaveReadbackChild
)
51 aContainer
->mMayHaveReadbackChild
= PR_FALSE
;
52 // go backwards so the updates read from earlier layers are later in the
54 for (Layer
* l
= aContainer
->GetLastChild(); l
; l
= l
->GetPrevSibling()) {
55 if (l
->GetType() == Layer::TYPE_READBACK
) {
56 aContainer
->mMayHaveReadbackChild
= PR_TRUE
;
57 BuildUpdatesForLayer(static_cast<ReadbackLayer
*>(l
));
63 FindBackgroundLayer(ReadbackLayer
* aLayer
, nsIntPoint
* aOffset
)
66 if (!aLayer
->GetTransform().Is2D(&transform
) ||
67 transform
.HasNonIntegerTranslation())
69 nsIntPoint
transformOffset(PRInt32(transform
.x0
), PRInt32(transform
.y0
));
71 for (Layer
* l
= aLayer
->GetPrevSibling(); l
; l
= l
->GetPrevSibling()) {
72 gfxMatrix backgroundTransform
;
73 if (!l
->GetTransform().Is2D(&backgroundTransform
) ||
74 backgroundTransform
.HasNonIntegerTranslation())
77 nsIntPoint
backgroundOffset(PRInt32(backgroundTransform
.x0
), PRInt32(backgroundTransform
.y0
));
78 nsIntRect
rectInBackground(transformOffset
- backgroundOffset
, aLayer
->GetSize());
79 const nsIntRegion
& visibleRegion
= l
->GetEffectiveVisibleRegion();
80 if (!visibleRegion
.Intersects(rectInBackground
))
82 // Since l is present in the background, from here on we either choose l
84 if (!visibleRegion
.Contains(rectInBackground
))
87 if (l
->GetEffectiveOpacity() != 1.0 ||
88 !(l
->GetContentFlags() & Layer::CONTENT_OPAQUE
))
91 // cliprects are post-transform
92 const nsIntRect
* clipRect
= l
->GetEffectiveClipRect();
93 if (clipRect
&& !clipRect
->Contains(nsIntRect(transformOffset
, aLayer
->GetSize())))
96 Layer::LayerType type
= l
->GetType();
97 if (type
!= Layer::TYPE_COLOR
&& type
!= Layer::TYPE_THEBES
)
100 *aOffset
= backgroundOffset
- transformOffset
;
108 ReadbackProcessor::BuildUpdatesForLayer(ReadbackLayer
* aLayer
)
114 Layer
* newBackground
= FindBackgroundLayer(aLayer
, &offset
);
115 if (!newBackground
) {
116 aLayer
->SetUnknown();
120 if (newBackground
->GetType() == Layer::TYPE_COLOR
) {
121 ColorLayer
* colorLayer
= static_cast<ColorLayer
*>(newBackground
);
122 if (aLayer
->mBackgroundColor
!= colorLayer
->GetColor()) {
123 aLayer
->mBackgroundLayer
= nsnull
;
124 aLayer
->mBackgroundColor
= colorLayer
->GetColor();
125 NS_ASSERTION(aLayer
->mBackgroundColor
.a
== 1.0,
126 "Color layer said it was opaque!");
127 nsRefPtr
<gfxContext
> ctx
=
128 aLayer
->mSink
->BeginUpdate(aLayer
->GetRect(),
129 aLayer
->AllocateSequenceNumber());
131 ctx
->SetColor(aLayer
->mBackgroundColor
);
132 nsIntSize size
= aLayer
->GetSize();
133 ctx
->Rectangle(gfxRect(0, 0, size
.width
, size
.height
));
135 aLayer
->mSink
->EndUpdate(ctx
, aLayer
->GetRect());
139 NS_ASSERTION(newBackground
->AsThebesLayer(), "Must be ThebesLayer");
140 ThebesLayer
* thebesLayer
= static_cast<ThebesLayer
*>(newBackground
);
141 // updateRect is relative to the ThebesLayer
142 nsIntRect updateRect
= aLayer
->GetRect() - offset
;
143 if (thebesLayer
!= aLayer
->mBackgroundLayer
||
144 offset
!= aLayer
->mBackgroundLayerOffset
) {
145 aLayer
->mBackgroundLayer
= thebesLayer
;
146 aLayer
->mBackgroundLayerOffset
= offset
;
147 aLayer
->mBackgroundColor
= gfxRGBA(0,0,0,0);
148 thebesLayer
->SetUsedForReadback(true);
151 invalid
.Sub(updateRect
, thebesLayer
->GetValidRegion());
152 updateRect
= invalid
.GetBounds();
155 Update update
= { aLayer
, updateRect
, aLayer
->AllocateSequenceNumber() };
156 mAllUpdates
.AppendElement(update
);
161 ReadbackProcessor::GetThebesLayerUpdates(ThebesLayer
* aLayer
,
162 nsTArray
<Update
>* aUpdates
,
163 nsIntRegion
* aUpdateRegion
)
165 // All ThebesLayers used for readback are in mAllUpdates (some possibly
166 // with an empty update rect).
167 aLayer
->SetUsedForReadback(false);
169 aUpdateRegion
->SetEmpty();
171 for (PRUint32 i
= mAllUpdates
.Length(); i
> 0; --i
) {
172 const Update
& update
= mAllUpdates
[i
- 1];
173 if (update
.mLayer
->mBackgroundLayer
== aLayer
) {
174 aLayer
->SetUsedForReadback(true);
175 // Don't bother asking for updates if we have an empty update rect.
176 if (!update
.mUpdateRect
.IsEmpty()) {
177 aUpdates
->AppendElement(update
);
179 aUpdateRegion
->Or(*aUpdateRegion
, update
.mUpdateRect
);
182 mAllUpdates
.RemoveElementAt(i
- 1);
187 ReadbackProcessor::~ReadbackProcessor()
189 for (PRUint32 i
= mAllUpdates
.Length(); i
> 0; --i
) {
190 const Update
& update
= mAllUpdates
[i
- 1];
191 // Unprocessed update. Notify the readback sink that this content is
193 update
.mLayer
->SetUnknown();