1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef GFX_LayerManagerCompositeUtils_H
#define GFX_LayerManagerCompositeUtils_H
#include <cstddef> // for size_t
#include "Layers.h" // for Layer
#include "Units.h" // for LayerIntRegion
#include "mozilla/RefPtr.h" // for RefPtr
#include "mozilla/gfx/BaseRect.h" // for operator-
#include "mozilla/gfx/Matrix.h" // for Matrix4x4
#include "mozilla/gfx/Rect.h" // for IntRect, Rect, RoundedOut, IntRectTyped
#include "mozilla/layers/Compositor.h" // for Compositor, INIT_MODE_CLEAR
#include "mozilla/layers/Effects.h" // for EffectChain, EffectRenderTarget
#include "mozilla/layers/LayerManagerComposite.h" // for LayerManagerComposite::AutoAddMaskEffect, LayerComposite, LayerManagerComposite
#include "mozilla/layers/TextureHost.h" // for CompositingRenderTarget
namespace mozilla {
namespace layers {
// Render aLayer using aCompositor and apply all mask layers of aLayer: The
// layer's own mask layer (aLayer->GetMaskLayer()), and any ancestor mask
// layers.
// If more than one mask layer needs to be applied, we use intermediate surfaces
// (CompositingRenderTargets) for rendering, applying one mask layer at a time.
// Callers need to provide a callback function aRenderCallback that does the
// actual rendering of the source. It needs to have the following form:
// void (EffectChain& effectChain, const Rect& clipRect)
// aRenderCallback is called exactly once, inside this function, unless aLayer's
// visible region is completely clipped out (in that case, aRenderCallback won't
// be called at all).
// This function calls aLayer->AsHostLayer()->AddBlendModeEffect for the
// final rendering pass.
//
// (This function should really live in LayerManagerComposite.cpp, but we
// need to use templates for passing lambdas until bug 1164522 is resolved.)
template <typename RenderCallbackType>
void RenderWithAllMasks(Layer* aLayer, Compositor* aCompositor,
const gfx::IntRect& aClipRect,
RenderCallbackType aRenderCallback) {
Layer* firstMask = nullptr;
size_t maskLayerCount = 0;
size_t nextAncestorMaskLayer = 0;
size_t ancestorMaskLayerCount = aLayer->GetAncestorMaskLayerCount();
if (Layer* ownMask = aLayer->GetMaskLayer()) {
firstMask = ownMask;
maskLayerCount = ancestorMaskLayerCount + 1;
nextAncestorMaskLayer = 0;
} else if (ancestorMaskLayerCount > 0) {
firstMask = aLayer->GetAncestorMaskLayerAt(0);
maskLayerCount = ancestorMaskLayerCount;
nextAncestorMaskLayer = 1;
} else {
// no mask layers at all
}
if (maskLayerCount <= 1) {
// This is the common case. Render in one pass and return.
EffectChain effectChain(aLayer);
LayerManagerComposite::AutoAddMaskEffect autoMaskEffect(firstMask,
effectChain);
static_cast<LayerComposite*>(aLayer->AsHostLayer())
->AddBlendModeEffect(effectChain);
aRenderCallback(effectChain, aClipRect);
return;
}
// We have multiple mask layers.
// We split our list of mask layers into three parts:
// (1) The first mask
// (2) The list of intermediate masks (every mask except first and last)
// (3) The final mask.
// Part (2) can be empty.
// For parts (1) and (2) we need to allocate intermediate surfaces to render
// into. The final mask gets rendered into the original render target.
// Calculate the size of the intermediate surfaces.
gfx::Rect visibleRect(
aLayer->GetLocalVisibleRegion().GetBounds().ToUnknownRect());
gfx::Matrix4x4 transform = aLayer->GetEffectiveTransform();
// TODO: Use RenderTargetIntRect and TransformBy here
gfx::IntRect surfaceRect = RoundedOut(
transform.TransformAndClipBounds(visibleRect, gfx::Rect(aClipRect)));
if (surfaceRect.IsEmpty()) {
return;
}
RefPtr<CompositingRenderTarget> originalTarget =
aCompositor->GetCurrentRenderTarget();
RefPtr<CompositingRenderTarget> firstTarget =
aCompositor->CreateRenderTarget(surfaceRect, INIT_MODE_CLEAR);
if (!firstTarget) {
return;
}
// Render the source while applying the first mask.
aCompositor->SetRenderTarget(firstTarget);
{
EffectChain firstEffectChain(aLayer);
LayerManagerComposite::AutoAddMaskEffect firstMaskEffect(firstMask,
firstEffectChain);
aRenderCallback(firstEffectChain, aClipRect - surfaceRect.TopLeft());
// firstTarget now contains the transformed source with the first mask and
// opacity already applied.
}
// Apply the intermediate masks.
gfx::IntRect intermediateClip(surfaceRect - surfaceRect.TopLeft());
RefPtr<CompositingRenderTarget> previousTarget = firstTarget;
for (size_t i = nextAncestorMaskLayer; i < ancestorMaskLayerCount - 1; i++) {
Layer* intermediateMask = aLayer->GetAncestorMaskLayerAt(i);
RefPtr<CompositingRenderTarget> intermediateTarget =
aCompositor->CreateRenderTarget(surfaceRect, INIT_MODE_CLEAR);
if (!intermediateTarget) {
break;
}
aCompositor->SetRenderTarget(intermediateTarget);
EffectChain intermediateEffectChain(aLayer);
LayerManagerComposite::AutoAddMaskEffect intermediateMaskEffect(
intermediateMask, intermediateEffectChain);
if (intermediateMaskEffect.Failed()) {
continue;
}
intermediateEffectChain.mPrimaryEffect =
new EffectRenderTarget(previousTarget);
aCompositor->DrawQuad(gfx::Rect(surfaceRect), intermediateClip,
intermediateEffectChain, 1.0, gfx::Matrix4x4());
previousTarget = intermediateTarget;
}
aCompositor->SetRenderTarget(originalTarget);
// Apply the final mask, rendering into originalTarget.
EffectChain finalEffectChain(aLayer);
finalEffectChain.mPrimaryEffect = new EffectRenderTarget(previousTarget);
Layer* finalMask = aLayer->GetAncestorMaskLayerAt(ancestorMaskLayerCount - 1);
// The blend mode needs to be applied in this final step, because this is
// where we're blending with the actual background (which is in
// originalTarget).
static_cast<LayerComposite*>(aLayer->AsHostLayer())
->AddBlendModeEffect(finalEffectChain);
LayerManagerComposite::AutoAddMaskEffect autoMaskEffect(finalMask,
finalEffectChain);
if (!autoMaskEffect.Failed()) {
aCompositor->DrawQuad(gfx::Rect(surfaceRect), aClipRect, finalEffectChain,
1.0, gfx::Matrix4x4());
}
}
} // namespace layers
} // namespace mozilla
#endif /* GFX_LayerManagerCompositeUtils_H */
|