CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / gfx / layers / ReadbackProcessor.cpp
blobe3b78ce0150348a5a8870d495a5df1b1473df5a4
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
13 * License.
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.
21 * Contributor(s):
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"
40 namespace mozilla {
41 namespace layers {
43 void
44 ReadbackProcessor::BuildUpdates(ContainerLayer* aContainer)
46 NS_ASSERTION(mAllUpdates.IsEmpty(), "Some updates not processed?");
48 if (!aContainer->mMayHaveReadbackChild)
49 return;
51 aContainer->mMayHaveReadbackChild = PR_FALSE;
52 // go backwards so the updates read from earlier layers are later in the
53 // array.
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));
62 static Layer*
63 FindBackgroundLayer(ReadbackLayer* aLayer, nsIntPoint* aOffset)
65 gfxMatrix transform;
66 if (!aLayer->GetTransform().Is2D(&transform) ||
67 transform.HasNonIntegerTranslation())
68 return nsnull;
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())
75 return nsnull;
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))
81 continue;
82 // Since l is present in the background, from here on we either choose l
83 // or nothing.
84 if (!visibleRegion.Contains(rectInBackground))
85 return nsnull;
87 if (l->GetEffectiveOpacity() != 1.0 ||
88 !(l->GetContentFlags() & Layer::CONTENT_OPAQUE))
89 return nsnull;
91 // cliprects are post-transform
92 const nsIntRect* clipRect = l->GetEffectiveClipRect();
93 if (clipRect && !clipRect->Contains(nsIntRect(transformOffset, aLayer->GetSize())))
94 return nsnull;
96 Layer::LayerType type = l->GetType();
97 if (type != Layer::TYPE_COLOR && type != Layer::TYPE_THEBES)
98 return nsnull;
100 *aOffset = backgroundOffset - transformOffset;
101 return l;
104 return nsnull;
107 void
108 ReadbackProcessor::BuildUpdatesForLayer(ReadbackLayer* aLayer)
110 if (!aLayer->mSink)
111 return;
113 nsIntPoint offset;
114 Layer* newBackground = FindBackgroundLayer(aLayer, &offset);
115 if (!newBackground) {
116 aLayer->SetUnknown();
117 return;
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());
130 if (ctx) {
131 ctx->SetColor(aLayer->mBackgroundColor);
132 nsIntSize size = aLayer->GetSize();
133 ctx->Rectangle(gfxRect(0, 0, size.width, size.height));
134 ctx->Fill();
135 aLayer->mSink->EndUpdate(ctx, aLayer->GetRect());
138 } else {
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);
149 } else {
150 nsIntRegion invalid;
151 invalid.Sub(updateRect, thebesLayer->GetValidRegion());
152 updateRect = invalid.GetBounds();
155 Update update = { aLayer, updateRect, aLayer->AllocateSequenceNumber() };
156 mAllUpdates.AppendElement(update);
160 void
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);
168 if (aUpdateRegion) {
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);
178 if (aUpdateRegion) {
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
192 // unknown.
193 update.mLayer->SetUnknown();