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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/compositor/reflector_impl.h"
#include "base/bind.h"
#include "base/location.h"
#include "content/browser/compositor/browser_compositor_output_surface.h"
#include "content/browser/compositor/owned_mailbox.h"
#include "content/common/gpu/client/gl_helper.h"
#include "ui/compositor/layer.h"
namespace content {
ReflectorImpl::ReflectorImpl(ui::Compositor* mirrored_compositor,
ui::Layer* mirroring_layer)
: mirrored_compositor_(mirrored_compositor),
mirroring_layer_(mirroring_layer),
mirrored_compositor_gl_helper_texture_id_(0),
needs_set_mailbox_(false),
flip_texture_(false),
output_surface_(nullptr) {
}
ReflectorImpl::~ReflectorImpl() {
}
void ReflectorImpl::Shutdown() {
if (output_surface_)
DetachFromOutputSurface();
// Prevent the ReflectorImpl from picking up a new output surface.
mirroring_layer_ = nullptr;
}
void ReflectorImpl::DetachFromOutputSurface() {
DCHECK(output_surface_);
output_surface_->SetReflector(nullptr);
DCHECK(mailbox_.get());
mailbox_ = nullptr;
output_surface_ = nullptr;
mirrored_compositor_gl_helper_->DeleteTexture(
mirrored_compositor_gl_helper_texture_id_);
mirrored_compositor_gl_helper_texture_id_ = 0;
mirrored_compositor_gl_helper_ = nullptr;
mirroring_layer_->SetShowSolidColorContent();
}
void ReflectorImpl::OnSourceSurfaceReady(
BrowserCompositorOutputSurface* output_surface) {
if (!mirroring_layer_)
return; // Was already Shutdown().
if (output_surface == output_surface_)
return; // Is already attached.
if (output_surface_)
DetachFromOutputSurface();
// Use the GLHelper from the ImageTransportFactory for our OwnedMailbox so we
// don't have to manage the lifetime of the GLHelper relative to the lifetime
// of the mailbox.
GLHelper* shared_helper = ImageTransportFactory::GetInstance()->GetGLHelper();
mailbox_ = new OwnedMailbox(shared_helper);
needs_set_mailbox_ = true;
// Create a GLHelper attached to the mirrored compositor's output surface for
// copying the output of the mirrored compositor.
mirrored_compositor_gl_helper_.reset(
new GLHelper(output_surface->context_provider()->ContextGL(),
output_surface->context_provider()->ContextSupport()));
// Create a texture id in the name space of the new GLHelper to update the
// mailbox being held by the |mirroring_layer_|.
mirrored_compositor_gl_helper_texture_id_ =
mirrored_compositor_gl_helper_->ConsumeMailboxToTexture(
mailbox_->mailbox(), mailbox_->sync_point());
flip_texture_ = !output_surface->capabilities().flipped_output_surface;
// The texture doesn't have the data. Request full redraw on mirrored
// compositor so that the full content will be copied to mirroring compositor.
// This full redraw should land us in OnSourceSwapBuffers() to resize the
// texture appropriately.
mirrored_compositor_->ScheduleFullRedraw();
output_surface_ = output_surface;
output_surface_->SetReflector(this);
}
void ReflectorImpl::OnMirroringCompositorResized() {
mirroring_layer_->SchedulePaint(mirroring_layer_->bounds());
}
void ReflectorImpl::OnSourceSwapBuffers() {
if (!mirroring_layer_)
return;
// Should be attached to the source output surface already.
DCHECK(mailbox_.get());
gfx::Size size = output_surface_->SurfaceSize();
mirrored_compositor_gl_helper_->CopyTextureFullImage(
mirrored_compositor_gl_helper_texture_id_, size);
// Insert a barrier to make the copy show up in the mirroring compositor's
// mailbox. Since the the compositor contexts and the ImageTransportFactory's
// GLHelper are all on the same GPU channel, this is sufficient instead of
// plumbing through a sync point.
mirrored_compositor_gl_helper_->InsertOrderingBarrier();
// Request full redraw on mirroring compositor.
UpdateTexture(size, mirroring_layer_->bounds());
}
void ReflectorImpl::OnSourcePostSubBuffer(const gfx::Rect& rect) {
if (!mirroring_layer_)
return;
// Should be attached to the source output surface already.
DCHECK(mailbox_.get());
gfx::Size size = output_surface_->SurfaceSize();
mirrored_compositor_gl_helper_->CopyTextureSubImage(
mirrored_compositor_gl_helper_texture_id_, rect);
// Insert a barrier to make the copy show up in the mirroring compositor's
// mailbox. Since the the compositor contexts and the ImageTransportFactory's
// GLHelper are all on the same GPU channel, this is sufficient instead of
// plumbing through a sync point.
mirrored_compositor_gl_helper_->InsertOrderingBarrier();
int y = rect.y();
// Flip the coordinates to compositor's one.
if (flip_texture_)
y = size.height() - rect.y() - rect.height();
gfx::Rect mirroring_rect(rect.x(), y, rect.width(), rect.height());
// Request redraw of the dirty portion in mirroring compositor.
UpdateTexture(size, mirroring_rect);
}
static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox,
unsigned int sync_point,
bool is_lost) {
mailbox->UpdateSyncPoint(sync_point);
}
void ReflectorImpl::UpdateTexture(const gfx::Size& source_size,
const gfx::Rect& redraw_rect) {
if (needs_set_mailbox_) {
mirroring_layer_->SetTextureMailbox(
cc::TextureMailbox(mailbox_->holder()),
cc::SingleReleaseCallback::Create(base::Bind(ReleaseMailbox, mailbox_)),
source_size);
needs_set_mailbox_ = false;
} else {
mirroring_layer_->SetTextureSize(source_size);
}
mirroring_layer_->SetBounds(gfx::Rect(source_size));
mirroring_layer_->SetTextureFlipped(flip_texture_);
mirroring_layer_->SchedulePaint(redraw_rect);
}
} // namespace content
|