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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright (c) 2013 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/aura/reflector_impl.h"
#include "base/bind.h"
#include "base/location.h"
#include "content/browser/aura/browser_compositor_output_surface.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,
IDMap<BrowserCompositorOutputSurface>* output_surface_map,
int surface_id)
: texture_id_(0),
texture_size_(mirrored_compositor->size()),
output_surface_map_(output_surface_map),
mirrored_compositor_(mirrored_compositor),
mirroring_compositor_(mirroring_layer->GetCompositor()),
mirroring_layer_(mirroring_layer),
impl_message_loop_(ui::Compositor::GetCompositorMessageLoop()),
main_message_loop_(base::MessageLoopProxy::current()),
surface_id_(surface_id) {
CreateSharedTexture();
impl_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::InitOnImplThread, this));
}
void ReflectorImpl::InitOnImplThread() {
// Ignore if the reflector was shutdown before
// initialized, or it's already initialized.
if (!output_surface_map_ || gl_helper_.get())
return;
BrowserCompositorOutputSurface* source_surface =
output_surface_map_->Lookup(surface_id_);
// Skip if the source surface isn't ready yet. This will be
// initiailze when the source surface becomes ready.
if (!source_surface)
return;
AttachToOutputSurface(source_surface);
gl_helper_->CopyTextureFullImage(texture_id_, texture_size_);
// The shared texture doesn't have the data, so invokes full redraw
// now.
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::FullRedrawContentOnMainThread,
scoped_refptr<ReflectorImpl>(this)));
}
void ReflectorImpl::OnSourceSurfaceReady(int surface_id) {
DCHECK_EQ(surface_id_, surface_id);
InitOnImplThread();
}
void ReflectorImpl::Shutdown() {
mirroring_compositor_ = NULL;
mirroring_layer_ = NULL;
shared_texture_ = NULL;
impl_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::ShutdownOnImplThread, this));
}
void ReflectorImpl::ShutdownOnImplThread() {
BrowserCompositorOutputSurface* output_surface =
output_surface_map_->Lookup(surface_id_);
if (output_surface)
output_surface->SetReflector(NULL);
output_surface_map_ = NULL;
gl_helper_.reset();
// The instance must be deleted on main thread.
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::DeleteOnMainThread,
scoped_refptr<ReflectorImpl>(this)));
}
// This must be called on ImplThread, or before the surface is passed to
// ImplThread.
void ReflectorImpl::AttachToOutputSurface(
BrowserCompositorOutputSurface* output_surface) {
gl_helper_.reset(
new GLHelper(output_surface->context_provider()->Context3d(),
output_surface->context_provider()->ContextSupport()));
output_surface->SetReflector(this);
}
void ReflectorImpl::OnMirroringCompositorResized() {
mirroring_compositor_->ScheduleFullRedraw();
}
void ReflectorImpl::OnLostResources() {
shared_texture_ = NULL;
mirroring_layer_->SetShowPaintedContent();
}
void ReflectorImpl::OnReshape(gfx::Size size) {
if (texture_size_ == size)
return;
texture_size_ = size;
DCHECK(texture_id_);
gl_helper_->ResizeTexture(texture_id_, size);
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::UpdateTextureSizeOnMainThread,
this->AsWeakPtr(),
texture_size_));
}
void ReflectorImpl::OnSwapBuffers() {
DCHECK(texture_id_);
gl_helper_->CopyTextureFullImage(texture_id_, texture_size_);
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::FullRedrawOnMainThread,
this->AsWeakPtr(),
texture_size_));
}
void ReflectorImpl::OnPostSubBuffer(gfx::Rect rect) {
DCHECK(texture_id_);
gl_helper_->CopyTextureSubImage(texture_id_, rect);
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::UpdateSubBufferOnMainThread,
this->AsWeakPtr(),
texture_size_,
rect));
}
void ReflectorImpl::CreateSharedTexture() {
texture_id_ =
ImageTransportFactory::GetInstance()->GetGLHelper()->CreateTexture();
shared_texture_ =
ImageTransportFactory::GetInstance()->CreateOwnedTexture(
texture_size_, 1.0f, texture_id_);
mirroring_layer_->SetExternalTexture(shared_texture_.get());
}
ReflectorImpl::~ReflectorImpl() {
// Make sure the reflector is deleted on main thread.
DCHECK_EQ(main_message_loop_.get(),
base::MessageLoopProxy::current().get());
}
void ReflectorImpl::UpdateTextureSizeOnMainThread(gfx::Size size) {
if (!mirroring_layer_)
return;
mirroring_layer_->SetBounds(gfx::Rect(size));
}
void ReflectorImpl::FullRedrawOnMainThread(gfx::Size size) {
if (!mirroring_compositor_)
return;
UpdateTextureSizeOnMainThread(size);
mirroring_compositor_->ScheduleFullRedraw();
}
void ReflectorImpl::UpdateSubBufferOnMainThread(gfx::Size size,
gfx::Rect rect) {
if (!mirroring_compositor_)
return;
UpdateTextureSizeOnMainThread(size);
// Flip the coordinates to compositor's one.
int y = size.height() - rect.y() - rect.height();
gfx::Rect new_rect(rect.x(), y, rect.width(), rect.height());
mirroring_layer_->SchedulePaint(new_rect);
}
void ReflectorImpl::FullRedrawContentOnMainThread() {
mirrored_compositor_->ScheduleFullRedraw();
}
} // namespace content
|