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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// 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,
IDMap<BrowserCompositorOutputSurface>* output_surface_map,
base::MessageLoopProxy* compositor_thread_loop,
int surface_id)
: impl_unsafe_(output_surface_map),
main_unsafe_(mirrored_compositor, mirroring_layer),
impl_message_loop_(compositor_thread_loop),
main_message_loop_(base::MessageLoopProxy::current()),
surface_id_(surface_id) {
GLHelper* helper = ImageTransportFactory::GetInstance()->GetGLHelper();
MainThreadData& main = GetMain();
main.mailbox = new OwnedMailbox(helper);
impl_message_loop_->PostTask(
FROM_HERE,
base::Bind(
&ReflectorImpl::InitOnImplThread, this, main.mailbox->holder()));
}
ReflectorImpl::MainThreadData::MainThreadData(
ui::Compositor* mirrored_compositor,
ui::Layer* mirroring_layer)
: needs_set_mailbox(true),
mirrored_compositor(mirrored_compositor),
mirroring_layer(mirroring_layer),
flip_texture(false) {
}
ReflectorImpl::MainThreadData::~MainThreadData() {}
ReflectorImpl::ImplThreadData::ImplThreadData(
IDMap<BrowserCompositorOutputSurface>* output_surface_map)
: output_surface_map(output_surface_map),
output_surface(NULL),
texture_id(0) {}
ReflectorImpl::ImplThreadData::~ImplThreadData() {}
ReflectorImpl::ImplThreadData& ReflectorImpl::GetImpl() {
DCHECK(impl_message_loop_->BelongsToCurrentThread());
return impl_unsafe_;
}
ReflectorImpl::MainThreadData& ReflectorImpl::GetMain() {
DCHECK(main_message_loop_->BelongsToCurrentThread());
return main_unsafe_;
}
void ReflectorImpl::InitOnImplThread(const gpu::MailboxHolder& mailbox_holder) {
ImplThreadData& impl = GetImpl();
// Ignore if the reflector was shutdown before
// initialized, or it's already initialized.
if (!impl.output_surface_map || impl.gl_helper.get())
return;
impl.mailbox_holder = mailbox_holder;
BrowserCompositorOutputSurface* source_surface =
impl.output_surface_map->Lookup(surface_id_);
// Skip if the source surface isn't ready yet. This will be
// initialized when the source surface becomes ready.
if (!source_surface)
return;
AttachToOutputSurfaceOnImplThread(impl.mailbox_holder, source_surface);
}
void ReflectorImpl::OnSourceSurfaceReady(
BrowserCompositorOutputSurface* source_surface) {
ImplThreadData& impl = GetImpl();
AttachToOutputSurfaceOnImplThread(impl.mailbox_holder, source_surface);
}
void ReflectorImpl::Shutdown() {
MainThreadData& main = GetMain();
main.mailbox = NULL;
main.mirroring_layer->SetShowSolidColorContent();
main.mirroring_layer = NULL;
impl_message_loop_->PostTask(
FROM_HERE, base::Bind(&ReflectorImpl::ShutdownOnImplThread, this));
}
void ReflectorImpl::DetachFromOutputSurface() {
ImplThreadData& impl = GetImpl();
DCHECK(impl.output_surface);
impl.output_surface->SetReflector(NULL);
DCHECK(impl.texture_id);
impl.gl_helper->DeleteTexture(impl.texture_id);
impl.texture_id = 0;
impl.gl_helper.reset();
impl.output_surface = NULL;
}
void ReflectorImpl::ShutdownOnImplThread() {
ImplThreadData& impl = GetImpl();
if (impl.output_surface)
DetachFromOutputSurface();
impl.output_surface_map = NULL;
// The instance must be deleted on main thread.
main_message_loop_->PostTask(FROM_HERE,
base::Bind(&ReflectorImpl::DeleteOnMainThread,
scoped_refptr<ReflectorImpl>(this)));
}
void ReflectorImpl::ReattachToOutputSurfaceFromMainThread(
BrowserCompositorOutputSurface* output_surface) {
MainThreadData& main = GetMain();
GLHelper* helper = ImageTransportFactory::GetInstance()->GetGLHelper();
main.mailbox = new OwnedMailbox(helper);
main.needs_set_mailbox = true;
main.mirroring_layer->SetShowSolidColorContent();
impl_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::AttachToOutputSurfaceOnImplThread,
this,
main.mailbox->holder(),
output_surface));
}
void ReflectorImpl::OnMirroringCompositorResized() {
MainThreadData& main = GetMain();
main.mirroring_layer->SchedulePaint(main.mirroring_layer->bounds());
}
void ReflectorImpl::OnSwapBuffers() {
ImplThreadData& impl = GetImpl();
gfx::Size size = impl.output_surface->SurfaceSize();
if (impl.texture_id) {
impl.gl_helper->CopyTextureFullImage(impl.texture_id, size);
impl.gl_helper->Flush();
}
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(
&ReflectorImpl::FullRedrawOnMainThread, this->AsWeakPtr(), size));
}
void ReflectorImpl::OnPostSubBuffer(gfx::Rect rect) {
ImplThreadData& impl = GetImpl();
if (impl.texture_id) {
impl.gl_helper->CopyTextureSubImage(impl.texture_id, rect);
impl.gl_helper->Flush();
}
main_message_loop_->PostTask(
FROM_HERE,
base::Bind(&ReflectorImpl::UpdateSubBufferOnMainThread,
this->AsWeakPtr(),
impl.output_surface->SurfaceSize(),
rect));
}
ReflectorImpl::~ReflectorImpl() {
// Make sure the reflector is deleted on main thread.
DCHECK_EQ(main_message_loop_.get(), base::MessageLoopProxy::current().get());
}
static void ReleaseMailbox(scoped_refptr<OwnedMailbox> mailbox,
unsigned int sync_point,
bool is_lost) {
mailbox->UpdateSyncPoint(sync_point);
}
void ReflectorImpl::AttachToOutputSurfaceOnImplThread(
const gpu::MailboxHolder& mailbox_holder,
BrowserCompositorOutputSurface* output_surface) {
ImplThreadData& impl = GetImpl();
if (output_surface == impl.output_surface)
return;
if (impl.output_surface)
DetachFromOutputSurface();
impl.output_surface = output_surface;
output_surface->context_provider()->BindToCurrentThread();
impl.gl_helper.reset(
new GLHelper(output_surface->context_provider()->ContextGL(),
output_surface->context_provider()->ContextSupport()));
impl.texture_id = impl.gl_helper->ConsumeMailboxToTexture(
mailbox_holder.mailbox, mailbox_holder.sync_point);
impl.gl_helper->ResizeTexture(impl.texture_id, output_surface->SurfaceSize());
impl.gl_helper->Flush();
output_surface->SetReflector(this);
// The texture doesn't have the data, so invokes full redraw now.
bool flip_texture = !output_surface->capabilities().flipped_output_surface;
main_message_loop_->PostTask(
FROM_HERE, base::Bind(&ReflectorImpl::FullRedrawContentOnMainThread,
scoped_refptr<ReflectorImpl>(this), flip_texture));
}
void ReflectorImpl::UpdateTextureSizeOnMainThread(gfx::Size size) {
MainThreadData& main = GetMain();
if (!main.mirroring_layer || !main.mailbox.get() ||
main.mailbox->mailbox().IsZero())
return;
if (main.needs_set_mailbox) {
main.mirroring_layer->SetTextureMailbox(
cc::TextureMailbox(main.mailbox->holder()),
cc::SingleReleaseCallback::Create(
base::Bind(ReleaseMailbox, main.mailbox)),
size);
main.needs_set_mailbox = false;
} else {
main.mirroring_layer->SetTextureSize(size);
}
main.mirroring_layer->SetBounds(gfx::Rect(size));
main.mirroring_layer->SetTextureFlipped(main.flip_texture);
}
void ReflectorImpl::FullRedrawOnMainThread(gfx::Size size) {
MainThreadData& main = GetMain();
if (!main.mirroring_layer)
return;
UpdateTextureSizeOnMainThread(size);
main.mirroring_layer->SchedulePaint(main.mirroring_layer->bounds());
}
void ReflectorImpl::UpdateSubBufferOnMainThread(const gfx::Size& size,
const gfx::Rect& rect) {
MainThreadData& main = GetMain();
if (!main.mirroring_layer)
return;
UpdateTextureSizeOnMainThread(size);
int y = rect.y();
// Flip the coordinates to compositor's one.
if (main.flip_texture)
y = size.height() - rect.y() - rect.height();
gfx::Rect new_rect(rect.x(), y, rect.width(), rect.height());
main.mirroring_layer->SchedulePaint(new_rect);
}
void ReflectorImpl::FullRedrawContentOnMainThread(bool flip_texture) {
MainThreadData& main = GetMain();
main.flip_texture = flip_texture;
main.mirrored_compositor->ScheduleFullRedraw();
}
} // namespace content
|