OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/render_widget_helper.h" | 5 #include "content/browser/renderer_host/render_widget_helper.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <IOSurface/IOSurfaceAPI.h> | 8 #include <IOSurface/IOSurfaceAPI.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | |
12 #include "base/synchronization/lock.h" | |
13 #include "content/browser/compositor/browser_compositor_view_mac.h" | 11 #include "content/browser/compositor/browser_compositor_view_mac.h" |
14 #include "content/browser/gpu/gpu_process_host.h" | 12 #include "content/browser/gpu/gpu_process_host.h" |
15 #include "content/browser/gpu/gpu_surface_tracker.h" | 13 #include "content/browser/gpu/gpu_surface_tracker.h" |
16 #include "content/common/gpu/gpu_messages.h" | 14 #include "content/common/gpu/gpu_messages.h" |
17 #include "content/common/gpu/surface_handle_types_mac.h" | 15 #include "content/common/gpu/surface_handle_types_mac.h" |
18 #include "content/common/view_messages.h" | |
19 | 16 |
20 namespace content { | |
21 namespace { | 17 namespace { |
22 typedef std::map<gfx::AcceleratedWidget,std::pair<int,int>> WidgetMap; | |
23 base::LazyInstance<WidgetMap> g_widget_map; | |
24 base::LazyInstance<base::Lock> g_lock; | |
25 } // namespace | |
26 | 18 |
27 // static | 19 void OnNativeSurfaceBuffersSwappedOnUIThread( |
28 void RenderWidgetHelper::SetRenderWidgetIDForWidget( | 20 base::ScopedCFTypeRef<IOSurfaceRef> io_surface, |
29 gfx::AcceleratedWidget native_widget, | 21 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params) { |
30 int render_process_id, | |
31 int render_widget_id) { | |
32 base::AutoLock lock(g_lock.Get()); | |
33 g_widget_map.Get()[native_widget] = std::make_pair( | |
34 render_process_id, render_widget_id); | |
35 } | |
36 | |
37 // static | |
38 void RenderWidgetHelper::ResetRenderWidgetIDForWidget( | |
39 gfx::AcceleratedWidget native_widget) { | |
40 base::AutoLock lock(g_lock.Get()); | |
41 g_widget_map.Get().erase(native_widget); | |
42 } | |
43 | |
44 // static | |
45 bool RenderWidgetHelper::GetRenderWidgetIDForWidget( | |
46 gfx::AcceleratedWidget native_widget, | |
47 int* render_process_id, | |
48 int* render_widget_id) { | |
49 base::AutoLock lock(g_lock.Get()); | |
50 | |
51 auto found = g_widget_map.Get().find(native_widget); | |
52 if (found != g_widget_map.Get().end()) { | |
53 *render_process_id = found->second.first; | |
54 *render_widget_id = found->second.second; | |
55 return true; | |
56 } | |
57 | |
58 *render_process_id = 0; | |
59 *render_widget_id = 0; | |
60 return false; | |
61 } | |
62 | |
63 // static | |
64 void RenderWidgetHelper::OnNativeSurfaceBuffersSwappedOnUIThread( | |
65 const ViewHostMsg_CompositorSurfaceBuffersSwapped_Params& params) { | |
66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
67 gfx::AcceleratedWidget native_widget = | 23 gfx::AcceleratedWidget native_widget = |
68 content::GpuSurfaceTracker::Get()->AcquireNativeWidget(params.surface_id); | 24 content::GpuSurfaceTracker::Get()->AcquireNativeWidget(params.surface_id); |
69 IOSurfaceID io_surface_id = content::IOSurfaceIDFromSurfaceHandle( | 25 IOSurfaceID io_surface_id = content::IOSurfaceIDFromSurfaceHandle( |
70 params.surface_handle); | 26 params.surface_handle); |
71 [native_widget gotAcceleratedIOSurfaceFrame:io_surface_id | 27 [native_widget gotAcceleratedIOSurfaceFrame:io_surface_id |
72 withOutputSurfaceID:params.surface_id | 28 withOutputSurfaceID:params.surface_id |
73 withLatencyInfo:params.latency_info | 29 withLatencyInfo:params.latency_info |
74 withPixelSize:params.size | 30 withPixelSize:params.size |
75 withScaleFactor:params.scale_factor]; | 31 withScaleFactor:params.scale_factor]; |
76 } | 32 } |
77 | 33 |
| 34 } // namespace |
| 35 |
| 36 namespace content { |
| 37 |
| 38 void RenderWidgetHelper::OnNativeSurfaceBuffersSwappedOnIOThread( |
| 39 GpuProcessHost* gpu_process_host, |
| 40 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params) { |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 42 |
| 43 // Immediately acknowledge this frame on the IO thread instead of the UI |
| 44 // thread. The UI thread will wait on the GPU process. If the UI thread |
| 45 // were to be responsible for acking swaps, then there would be a cycle |
| 46 // and a potential deadlock. |
| 47 // TODO(ccameron): This immediate ack circumvents GPU back-pressure that |
| 48 // is necessary to throttle renderers. Fix that. |
| 49 AcceleratedSurfaceMsg_BufferPresented_Params ack_params; |
| 50 ack_params.sync_point = 0; |
| 51 ack_params.renderer_id = 0; |
| 52 gpu_process_host->Send(new AcceleratedSurfaceMsg_BufferPresented( |
| 53 params.route_id, ack_params)); |
| 54 |
| 55 // Open the IOSurface handle before returning, to ensure that it is not |
| 56 // closed as soon as the frame is acknowledged. |
| 57 base::ScopedCFTypeRef<IOSurfaceRef> io_surface(IOSurfaceLookup( |
| 58 static_cast<uint32>(params.surface_handle))); |
| 59 |
| 60 BrowserThread::PostTask( |
| 61 BrowserThread::UI, |
| 62 FROM_HERE, |
| 63 base::Bind(&OnNativeSurfaceBuffersSwappedOnUIThread, io_surface, params)); |
| 64 } |
| 65 |
78 } // namespace content | 66 } // namespace content |
OLD | NEW |