| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/compositor/mus_browser_compositor_output_surface.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "cc/output/compositor_frame.h" | |
| 10 #include "cc/output/output_surface_frame.h" | |
| 11 #include "cc/quads/render_pass.h" | |
| 12 #include "cc/quads/texture_draw_quad.h" | |
| 13 #include "components/display_compositor/compositor_overlay_candidate_validator.h
" | |
| 14 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 15 #include "gpu/ipc/client/command_buffer_proxy_impl.h" | |
| 16 #include "services/ui/public/cpp/client_compositor_frame_sink.h" | |
| 17 #include "services/ui/public/cpp/gpu/context_provider_command_buffer.h" | |
| 18 #include "ui/aura/mus/window_port_mus.h" | |
| 19 #include "ui/aura/window.h" | |
| 20 #include "ui/base/layout.h" | |
| 21 #include "ui/gfx/geometry/dip_util.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 MusBrowserCompositorOutputSurface::MusBrowserCompositorOutputSurface( | |
| 26 aura::Window* window, | |
| 27 scoped_refptr<ui::ContextProviderCommandBuffer> context, | |
| 28 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 29 const UpdateVSyncParametersCallback& update_vsync_parameters_callback, | |
| 30 std::unique_ptr<display_compositor::CompositorOverlayCandidateValidator> | |
| 31 overlay_candidate_validator) | |
| 32 : GpuBrowserCompositorOutputSurface(std::move(context), | |
| 33 update_vsync_parameters_callback, | |
| 34 std::move(overlay_candidate_validator)), | |
| 35 window_(window), | |
| 36 begin_frame_source_(nullptr) { | |
| 37 aura::WindowPortMus* window_port = aura::WindowPortMus::Get(window_); | |
| 38 DCHECK(window_port); | |
| 39 compositor_frame_sink_ = window_port->RequestCompositorFrameSink( | |
| 40 context, gpu_memory_buffer_manager); | |
| 41 compositor_frame_sink_->BindToClient(this); | |
| 42 } | |
| 43 | |
| 44 MusBrowserCompositorOutputSurface::~MusBrowserCompositorOutputSurface() {} | |
| 45 | |
| 46 cc::BeginFrameSource* MusBrowserCompositorOutputSurface::GetBeginFrameSource() { | |
| 47 return begin_frame_source_; | |
| 48 } | |
| 49 | |
| 50 void MusBrowserCompositorOutputSurface::SwapBuffers( | |
| 51 cc::OutputSurfaceFrame frame) { | |
| 52 cc::CompositorFrame ui_frame; | |
| 53 ui_frame.metadata.device_scale_factor = | |
| 54 ui::GetScaleFactorForNativeView(window_); | |
| 55 ui_frame.metadata.latency_info = std::move(frame.latency_info); | |
| 56 // Reset latency_info to known empty state after moving contents. | |
| 57 frame.latency_info.clear(); | |
| 58 const int render_pass_id = 1; | |
| 59 const gfx::Rect bounds_in_dip = gfx::Rect(window_->bounds().size()); | |
| 60 const gfx::Rect bounds_in_pixels = gfx::ConvertRectToPixel( | |
| 61 ui_frame.metadata.device_scale_factor, bounds_in_dip); | |
| 62 std::unique_ptr<cc::RenderPass> pass = cc::RenderPass::Create(); | |
| 63 pass->SetNew(render_pass_id, bounds_in_pixels, bounds_in_pixels, | |
| 64 gfx::Transform()); | |
| 65 // The SharedQuadState is owned by the SharedQuadStateList | |
| 66 // shared_quad_state_list. | |
| 67 cc::SharedQuadState* sqs = pass->CreateAndAppendSharedQuadState(); | |
| 68 sqs->SetAll(gfx::Transform(), bounds_in_pixels.size(), bounds_in_pixels, | |
| 69 bounds_in_pixels, false /* is_clipped */, 1.f /* opacity */, | |
| 70 SkBlendMode::kSrc, 0 /* sorting_context_id */); | |
| 71 | |
| 72 cc::TransferableResource resource; | |
| 73 resource.id = AllocateResourceId(); | |
| 74 resource.format = cc::ResourceFormat::RGBA_8888; | |
| 75 resource.filter = GL_LINEAR; | |
| 76 resource.size = frame.size; | |
| 77 | |
| 78 const gpu::Mailbox& mailbox = GetMailboxFromResourceId(resource.id); | |
| 79 DCHECK(!mailbox.IsZero()); | |
| 80 const gfx::Rect rect(frame.size); | |
| 81 | |
| 82 // Call parent's SwapBuffers to generate the front buffer, and then send the | |
| 83 // front buffer to mus. | |
| 84 // TODO(penghuang): we should avoid extra copies here by sending frames to mus | |
| 85 // directly from renderer. | |
| 86 GpuBrowserCompositorOutputSurface::SwapBuffers(std::move(frame)); | |
| 87 GetCommandBufferProxy()->TakeFrontBuffer(mailbox); | |
| 88 | |
| 89 auto* gl = context_provider()->ContextGL(); | |
| 90 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
| 91 gl->ShallowFlushCHROMIUM(); | |
| 92 gpu::SyncToken sync_token; | |
| 93 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
| 94 | |
| 95 resource.mailbox_holder = | |
| 96 gpu::MailboxHolder(mailbox, sync_token, GL_TEXTURE_2D); | |
| 97 resource.read_lock_fences_enabled = false; | |
| 98 resource.is_software = false; | |
| 99 resource.is_overlay_candidate = false; | |
| 100 ui_frame.resource_list.push_back(std::move(resource)); | |
| 101 | |
| 102 const bool needs_blending = true; | |
| 103 const bool premultiplied_alpha = true; | |
| 104 const gfx::PointF uv_top_left(0.f, 0.f); | |
| 105 const gfx::PointF uv_bottom_right(1.f, 1.f); | |
| 106 const uint32_t background_color = 0x00000000; | |
| 107 const float vertex_opacity[4] = {1.f, 1.f, 1.f, 1.f}; | |
| 108 const bool y_flipped = true; | |
| 109 const bool nearest_neighbor = false; | |
| 110 const bool secure_output_only = false; | |
| 111 | |
| 112 cc::TextureDrawQuad* quad = | |
| 113 pass->CreateAndAppendDrawQuad<cc::TextureDrawQuad>(); | |
| 114 quad->SetAll(sqs, rect, rect, rect, needs_blending, resource.id, gfx::Size(), | |
| 115 premultiplied_alpha, uv_top_left, uv_bottom_right, | |
| 116 background_color, vertex_opacity, y_flipped, nearest_neighbor, | |
| 117 secure_output_only); | |
| 118 | |
| 119 ui_frame.render_pass_list.push_back(std::move(pass)); | |
| 120 | |
| 121 compositor_frame_sink_->SubmitCompositorFrame(std::move(ui_frame)); | |
| 122 } | |
| 123 | |
| 124 void MusBrowserCompositorOutputSurface::SetBeginFrameSource( | |
| 125 cc::BeginFrameSource* source) { | |
| 126 begin_frame_source_ = source; | |
| 127 } | |
| 128 | |
| 129 void MusBrowserCompositorOutputSurface::ReclaimResources( | |
| 130 const cc::ReturnedResourceArray& resources) { | |
| 131 for (const auto& resource : resources) { | |
| 132 DCHECK_EQ(1, resource.count); | |
| 133 const gpu::Mailbox& mailbox = GetMailboxFromResourceId(resource.id); | |
| 134 GetCommandBufferProxy()->ReturnFrontBuffer(mailbox, resource.sync_token, | |
| 135 resource.lost); | |
| 136 FreeResourceId(resource.id); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void MusBrowserCompositorOutputSurface::SetTreeActivationCallback( | |
| 141 const base::Closure& callback) {} | |
| 142 | |
| 143 void MusBrowserCompositorOutputSurface::DidReceiveCompositorFrameAck() { | |
| 144 OnGpuSwapBuffersCompleted(std::vector<ui::LatencyInfo>(), | |
| 145 gfx::SwapResult::SWAP_ACK, nullptr); | |
| 146 } | |
| 147 | |
| 148 void MusBrowserCompositorOutputSurface::DidLoseCompositorFrameSink() {} | |
| 149 | |
| 150 void MusBrowserCompositorOutputSurface::OnDraw( | |
| 151 const gfx::Transform& transform, | |
| 152 const gfx::Rect& viewport, | |
| 153 bool resourceless_software_draw) {} | |
| 154 | |
| 155 void MusBrowserCompositorOutputSurface::SetMemoryPolicy( | |
| 156 const cc::ManagedMemoryPolicy& policy) {} | |
| 157 | |
| 158 void MusBrowserCompositorOutputSurface::SetExternalTilePriorityConstraints( | |
| 159 const gfx::Rect& viewport_rect, | |
| 160 const gfx::Transform& transform) {} | |
| 161 | |
| 162 uint32_t MusBrowserCompositorOutputSurface::AllocateResourceId() { | |
| 163 if (!free_resource_ids_.empty()) { | |
| 164 uint32_t id = free_resource_ids_.back(); | |
| 165 free_resource_ids_.pop_back(); | |
| 166 return id; | |
| 167 } | |
| 168 // If there is no free resource id, we generate a new mailbox in the mailbox | |
| 169 // vector, and the index of the new mailbox is the new allocated resource id. | |
| 170 uint32_t id = mailboxes_.size(); | |
| 171 mailboxes_.push_back(gpu::Mailbox::Generate()); | |
| 172 return id; | |
| 173 } | |
| 174 | |
| 175 void MusBrowserCompositorOutputSurface::FreeResourceId(uint32_t id) { | |
| 176 DCHECK_LT(id, mailboxes_.size()); | |
| 177 DCHECK(std::find(free_resource_ids_.begin(), free_resource_ids_.end(), id) == | |
| 178 free_resource_ids_.end()); | |
| 179 free_resource_ids_.push_back(id); | |
| 180 } | |
| 181 | |
| 182 const gpu::Mailbox& MusBrowserCompositorOutputSurface::GetMailboxFromResourceId( | |
| 183 uint32_t id) { | |
| 184 DCHECK_LT(id, mailboxes_.size()); | |
| 185 DCHECK(std::find(free_resource_ids_.begin(), free_resource_ids_.end(), id) == | |
| 186 free_resource_ids_.end()); | |
| 187 return mailboxes_[id]; | |
| 188 } | |
| 189 | |
| 190 } // namespace content | |
| OLD | NEW |