Chromium Code Reviews| Index: content/browser/compositor/mus_browser_compositor_output_surface.cc |
| diff --git a/content/browser/compositor/mus_browser_compositor_output_surface.cc b/content/browser/compositor/mus_browser_compositor_output_surface.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de3f887b4086cf575fadd08122ca3aa50dcd7a37 |
| --- /dev/null |
| +++ b/content/browser/compositor/mus_browser_compositor_output_surface.cc |
| @@ -0,0 +1,176 @@ |
| +// Copyright 2016 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/mus_browser_compositor_output_surface.h" |
| + |
| +#include <utility> |
| + |
| +#include "build/build_config.h" |
| +#include "cc/output/compositor_frame.h" |
| +#include "cc/output/compositor_frame_ack.h" |
| +#include "cc/output/output_surface_client.h" |
| +#include "cc/quads/render_pass.h" |
| +#include "cc/quads/texture_draw_quad.h" |
| +#include "components/display_compositor/compositor_overlay_candidate_validator.h" |
| +#include "components/mus/common/gpu_service.h" |
| +#include "components/mus/public/cpp/window.h" |
| +#include "components/mus/public/cpp/window_surface.h" |
| +#include "content/browser/compositor/reflector_impl.h" |
| +#include "content/browser/compositor/reflector_texture.h" |
| +#include "content/browser/renderer_host/render_widget_host_impl.h" |
| +#include "content/common/gpu/client/context_provider_command_buffer.h" |
| +#include "gpu/command_buffer/client/context_support.h" |
| +#include "gpu/command_buffer/client/gles2_interface.h" |
| +#include "gpu/ipc/client/command_buffer_proxy_impl.h" |
| +#include "ui/views/mus/native_widget_mus.h" |
| +#include "ui/views/mus/window_tree_host_mus.h" |
| + |
| +namespace content { |
| + |
| +MusBrowserCompositorOutputSurface::MusBrowserCompositorOutputSurface( |
| + gpu::SurfaceHandle surface_handle, |
| + scoped_refptr<ContextProviderCommandBuffer> context, |
| + scoped_refptr<ui::CompositorVSyncManager> vsync_manager, |
| + cc::SyntheticBeginFrameSource* begin_frame_source, |
| + std::unique_ptr<display_compositor::CompositorOverlayCandidateValidator> |
| + overlay_candidate_validator) |
| + : GpuBrowserCompositorOutputSurface(std::move(context), |
| + std::move(vsync_manager), |
| + begin_frame_source, |
| + std::move(overlay_candidate_validator)), |
| + mus_window_(nullptr) { |
| + DCHECK(mus::GpuService::UseChromeGpuCommandBuffer()); |
| + views::WindowTreeHostMus* window_tree_host = |
| + static_cast<views::WindowTreeHostMus*>( |
| + aura::WindowTreeHost::GetForAcceleratedWidget(surface_handle)); |
| + mus_window_ = window_tree_host->native_widget()->window(); |
| + mus_window_surface_ = |
| + mus_window_->RequestSurface(mus::mojom::SurfaceType::DEFAULT); |
| +} |
| + |
| +MusBrowserCompositorOutputSurface::~MusBrowserCompositorOutputSurface() {} |
| + |
| +void MusBrowserCompositorOutputSurface::SwapBuffers(cc::CompositorFrame frame) { |
| + const gfx::Rect bounds(mus_window_->bounds().size()); |
| + cc::CompositorFrame mus_frame; |
| + mus_frame.metadata.device_scale_factor = 1.0f; |
| + mus_frame.delegated_frame_data = base::MakeUnique<cc::DelegatedFrameData>(); |
| + mus_frame.delegated_frame_data->resource_list.resize(0u); |
| + const cc::RenderPassId render_pass_id(1, 1); |
| + std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); |
| + const bool has_transparent_background = true; |
| + pass->SetAll(render_pass_id, bounds, bounds, gfx::Transform(), |
| + has_transparent_background); |
| + // The SharedQuadState is owned by the SharedQuadStateList |
| + // shared_quad_state_list. |
| + cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); |
| + sqs->SetAll(gfx::Transform(), bounds.size(), bounds, bounds, |
| + false /* is_clipped */, 1.f /* opacity */, SkXfermode::kSrc_Mode, |
| + 0 /* sorting_context_id */); |
| + |
| + cc::TransferableResource resource; |
| + resource.id = AllocateResourceId(); |
| + resource.format = cc::ResourceFormat::RGBA_8888; |
| + resource.filter = GL_LINEAR; |
| + resource.size = frame.gl_frame_data->size; |
| + |
| + const gpu::Mailbox& mailbox = GetMailboxFromResourceId(resource.id); |
| + DCHECK(!mailbox.IsZero()); |
| + const gfx::Rect rect(frame.gl_frame_data->size); |
| + |
| + // Call parent's SwapBuffers to generate the front buffer, and then send the |
| + // front buffer to mus. |
| + // TODO(penghuang): we should avoid extra copies here by sending frames to mus |
| + // directly from renderer. |
| + GpuBrowserCompositorOutputSurface::SwapBuffers(std::move(frame)); |
| + GetCommandBufferProxy()->TakeFrontBuffer(mailbox); |
|
Fady Samuel
2016/06/30 20:28:04
Question for my own edification: Does this mean we
Peng
2016/06/30 20:49:07
As my understanding, for the chrome offscreen chro
piman
2016/06/30 20:56:31
Yes, please, set it to false. The default is true
|
| + |
| + auto* gl = context_provider()->ContextGL(); |
| + const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| + gl->ShallowFlushCHROMIUM(); |
| + gpu::SyncToken sync_token; |
| + gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
| + |
| + resource.mailbox_holder = |
| + gpu::MailboxHolder(mailbox, sync_token, GL_TEXTURE_2D); |
| + resource.read_lock_fences_enabled = false; |
| + resource.is_software = false; |
| + resource.is_overlay_candidate = false; |
| + mus_frame.delegated_frame_data->resource_list.push_back(std::move(resource)); |
| + |
| + const bool needs_blending = true; |
| + const bool premultiplied_alpha = true; |
| + const gfx::PointF uv_top_left(0.f, 0.f); |
| + const gfx::PointF uv_bottom_right(1.f, 1.f); |
| + const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f}; |
| + const bool y_flipped = true; |
| + const bool nearest_neighbor = false; |
| + |
| + cc::TextureDrawQuad* quad = |
| + pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); |
| + const uint32_t transparent_color = 0x00000000; |
| + quad->SetAll(sqs, rect, rect, rect, needs_blending, resource.id, gfx::Size(), |
| + premultiplied_alpha, uv_top_left, uv_bottom_right, |
| + transparent_color, vertex_opacity, y_flipped, nearest_neighbor, |
| + false); |
| + |
| + mus_frame.delegated_frame_data->render_pass_list.push_back(std::move(pass)); |
| + mus_window_surface_->SubmitCompositorFrame( |
| + std::move(mus_frame), |
| + base::Bind(&MusBrowserCompositorOutputSurface::OnGpuSwapBuffersCompleted, |
| + base::Unretained(this), std::vector<ui::LatencyInfo>(), |
| + gfx::SwapResult::SWAP_ACK, nullptr)); |
| + return; |
| +} |
| + |
| +bool MusBrowserCompositorOutputSurface::BindToClient( |
| + cc::OutputSurfaceClient* client) { |
| + if (!GpuBrowserCompositorOutputSurface::BindToClient(client)) |
| + return false; |
| + mus_window_surface_->BindToThread(); |
| + mus_window_surface_->set_client(this); |
| + return true; |
| +} |
| + |
| +void MusBrowserCompositorOutputSurface::OnResourcesReturned( |
| + mus::WindowSurface* surface, |
| + mojo::Array<cc::ReturnedResource> resources) { |
| + for (size_t i = 0; i < resources.size(); ++i) { |
| + cc::ReturnedResource resource = std::move(resources[i]); |
| + DCHECK_EQ(1, resource.count); |
| + const gpu::Mailbox& mailbox = GetMailboxFromResourceId(resource.id); |
| + GetCommandBufferProxy()->ReturnFrontBuffer(mailbox, resource.sync_token, |
| + resource.lost); |
| + FreeResourceId(resource.id); |
| + } |
| +} |
| + |
| +uint32_t MusBrowserCompositorOutputSurface::AllocateResourceId() { |
| + if (!free_resource_ids_.empty()) { |
| + uint32_t id = free_resource_ids_.back(); |
| + free_resource_ids_.pop_back(); |
| + return id; |
| + } |
| + // Generate a new mailbox. |
| + uint32_t id = mailboxs_.size(); |
| + mailboxs_.push_back(gpu::Mailbox::Generate()); |
| + return id; |
| +} |
| + |
| +void MusBrowserCompositorOutputSurface::FreeResourceId(uint32_t id) { |
| + DCHECK_LT(id, mailboxs_.size()); |
| + DCHECK(std::find(free_resource_ids_.begin(), free_resource_ids_.end(), id) == |
| + free_resource_ids_.end()); |
| + free_resource_ids_.push_back(id); |
| +} |
| + |
| +const gpu::Mailbox& MusBrowserCompositorOutputSurface::GetMailboxFromResourceId( |
| + uint32_t id) { |
| + DCHECK_LT(id, mailboxs_.size()); |
| + DCHECK(std::find(free_resource_ids_.begin(), free_resource_ids_.end(), id) == |
| + free_resource_ids_.end()); |
| + return mailboxs_[id]; |
| +} |
| + |
| +} // namespace content |