| 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 "services/ui/surfaces/direct_output_surface_ozone.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "cc/output/context_provider.h" | |
| 12 #include "cc/output/output_surface_client.h" | |
| 13 #include "cc/output/output_surface_frame.h" | |
| 14 #include "cc/scheduler/begin_frame_source.h" | |
| 15 #include "components/display_compositor/buffer_queue.h" | |
| 16 #include "gpu/command_buffer/client/context_support.h" | |
| 17 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 18 #include "ui/display/types/display_snapshot.h" | |
| 19 | |
| 20 using display_compositor::BufferQueue; | |
| 21 | |
| 22 namespace ui { | |
| 23 | |
| 24 DirectOutputSurfaceOzone::DirectOutputSurfaceOzone( | |
| 25 scoped_refptr<cc::InProcessContextProvider> context_provider, | |
| 26 gfx::AcceleratedWidget widget, | |
| 27 cc::SyntheticBeginFrameSource* synthetic_begin_frame_source, | |
| 28 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 29 uint32_t target, | |
| 30 uint32_t internalformat) | |
| 31 : cc::OutputSurface(context_provider), | |
| 32 gl_helper_(context_provider->ContextGL(), | |
| 33 context_provider->ContextSupport()), | |
| 34 synthetic_begin_frame_source_(synthetic_begin_frame_source), | |
| 35 weak_ptr_factory_(this) { | |
| 36 buffer_queue_.reset( | |
| 37 new BufferQueue(context_provider->ContextGL(), target, internalformat, | |
| 38 ui::DisplaySnapshot::PrimaryFormat(), &gl_helper_, | |
| 39 gpu_memory_buffer_manager, widget)); | |
| 40 | |
| 41 capabilities_.uses_default_gl_framebuffer = false; | |
| 42 capabilities_.flipped_output_surface = true; | |
| 43 // Set |max_frames_pending| to 2 for surfaceless, which aligns scheduling | |
| 44 // more closely with the previous surfaced behavior. | |
| 45 // With a surface, swap buffer ack used to return early, before actually | |
| 46 // presenting the back buffer, enabling the browser compositor to run ahead. | |
| 47 // Surfaceless implementation acks at the time of actual buffer swap, which | |
| 48 // shifts the start of the new frame forward relative to the old | |
| 49 // implementation. | |
| 50 capabilities_.max_frames_pending = 2; | |
| 51 | |
| 52 buffer_queue_->Initialize(); | |
| 53 | |
| 54 context_provider->SetSwapBuffersCompletionCallback( | |
| 55 base::Bind(&DirectOutputSurfaceOzone::OnGpuSwapBuffersCompleted, | |
| 56 weak_ptr_factory_.GetWeakPtr())); | |
| 57 context_provider->SetUpdateVSyncParametersCallback( | |
| 58 base::Bind(&DirectOutputSurfaceOzone::OnVSyncParametersUpdated, | |
| 59 weak_ptr_factory_.GetWeakPtr())); | |
| 60 } | |
| 61 | |
| 62 DirectOutputSurfaceOzone::~DirectOutputSurfaceOzone() { | |
| 63 // TODO(rjkroege): Support cleanup. | |
| 64 } | |
| 65 | |
| 66 void DirectOutputSurfaceOzone::BindToClient(cc::OutputSurfaceClient* client) { | |
| 67 DCHECK(client); | |
| 68 DCHECK(!client_); | |
| 69 client_ = client; | |
| 70 } | |
| 71 | |
| 72 void DirectOutputSurfaceOzone::EnsureBackbuffer() {} | |
| 73 | |
| 74 void DirectOutputSurfaceOzone::DiscardBackbuffer() { | |
| 75 context_provider()->ContextGL()->DiscardBackbufferCHROMIUM(); | |
| 76 } | |
| 77 | |
| 78 void DirectOutputSurfaceOzone::BindFramebuffer() { | |
| 79 DCHECK(buffer_queue_); | |
| 80 buffer_queue_->BindFramebuffer(); | |
| 81 } | |
| 82 | |
| 83 // We call this on every frame that a value changes, but changing the size once | |
| 84 // we've allocated backing NativePixmapOzone instances will cause a DCHECK | |
| 85 // because Chrome never Reshape(s) after the first one from (0,0). NB: this | |
| 86 // implies that screen size changes need to be plumbed differently. In | |
| 87 // particular, we must create the native window in the size that the hardware | |
| 88 // reports. | |
| 89 void DirectOutputSurfaceOzone::Reshape(const gfx::Size& size, | |
| 90 float device_scale_factor, | |
| 91 const gfx::ColorSpace& color_space, | |
| 92 bool has_alpha) { | |
| 93 reshape_size_ = size; | |
| 94 context_provider()->ContextGL()->ResizeCHROMIUM( | |
| 95 size.width(), size.height(), device_scale_factor, has_alpha); | |
| 96 buffer_queue_->Reshape(size, device_scale_factor, color_space); | |
| 97 } | |
| 98 | |
| 99 void DirectOutputSurfaceOzone::SwapBuffers(cc::OutputSurfaceFrame frame) { | |
| 100 DCHECK(buffer_queue_); | |
| 101 | |
| 102 // TODO(rjkroege): What if swap happens again before OnGpuSwapBuffersCompleted | |
| 103 // then it would see the wrong size? | |
| 104 DCHECK(reshape_size_ == frame.size); | |
| 105 swap_size_ = reshape_size_; | |
| 106 | |
| 107 buffer_queue_->SwapBuffers(frame.sub_buffer_rect); | |
| 108 | |
| 109 // Code combining GpuBrowserCompositorOutputSurface + DirectOutputSurface | |
| 110 if (frame.sub_buffer_rect == gfx::Rect(frame.size)) { | |
| 111 context_provider_->ContextSupport()->Swap(); | |
| 112 } else { | |
| 113 context_provider_->ContextSupport()->PartialSwapBuffers( | |
| 114 frame.sub_buffer_rect); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 uint32_t DirectOutputSurfaceOzone::GetFramebufferCopyTextureFormat() { | |
| 119 return buffer_queue_->internal_format(); | |
| 120 } | |
| 121 | |
| 122 cc::OverlayCandidateValidator* | |
| 123 DirectOutputSurfaceOzone::GetOverlayCandidateValidator() const { | |
| 124 return nullptr; | |
| 125 } | |
| 126 | |
| 127 bool DirectOutputSurfaceOzone::IsDisplayedAsOverlayPlane() const { | |
| 128 // TODO(rjkroege): implement remaining overlay functionality. | |
| 129 return true; | |
| 130 } | |
| 131 | |
| 132 unsigned DirectOutputSurfaceOzone::GetOverlayTextureId() const { | |
| 133 return buffer_queue_->current_texture_id(); | |
| 134 } | |
| 135 | |
| 136 bool DirectOutputSurfaceOzone::SurfaceIsSuspendForRecycle() const { | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 bool DirectOutputSurfaceOzone::HasExternalStencilTest() const { | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 void DirectOutputSurfaceOzone::ApplyExternalStencil() {} | |
| 145 | |
| 146 void DirectOutputSurfaceOzone::OnGpuSwapBuffersCompleted( | |
| 147 const std::vector<ui::LatencyInfo>& latency_info, | |
| 148 gfx::SwapResult result, | |
| 149 const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) { | |
| 150 bool force_swap = false; | |
| 151 if (result == gfx::SwapResult::SWAP_NAK_RECREATE_BUFFERS) { | |
| 152 // Even through the swap failed, this is a fixable error so we can pretend | |
| 153 // it succeeded to the rest of the system. | |
| 154 result = gfx::SwapResult::SWAP_ACK; | |
| 155 buffer_queue_->RecreateBuffers(); | |
| 156 force_swap = true; | |
| 157 } | |
| 158 | |
| 159 buffer_queue_->PageFlipComplete(); | |
| 160 client_->DidReceiveSwapBuffersAck(); | |
| 161 | |
| 162 if (force_swap) | |
| 163 client_->SetNeedsRedrawRect(gfx::Rect(swap_size_)); | |
| 164 } | |
| 165 | |
| 166 void DirectOutputSurfaceOzone::OnVSyncParametersUpdated( | |
| 167 base::TimeTicks timebase, | |
| 168 base::TimeDelta interval) { | |
| 169 // TODO(brianderson): We should not be receiving 0 intervals. | |
| 170 synthetic_begin_frame_source_->OnUpdateVSyncParameters( | |
| 171 timebase, | |
| 172 interval.is_zero() ? cc::BeginFrameArgs::DefaultInterval() : interval); | |
| 173 } | |
| 174 | |
| 175 } // namespace ui | |
| OLD | NEW |