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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/render_widget_helper.h"
7 #import <Cocoa/Cocoa.h>
8 #include <IOSurface/IOSurfaceAPI.h>
10 #include "base/bind.h"
11 #include "content/browser/compositor/browser_compositor_view_mac.h"
12 #include "content/browser/gpu/gpu_process_host.h"
13 #include "content/browser/gpu/gpu_surface_tracker.h"
14 #include "content/common/gpu/gpu_messages.h"
15 #include "content/common/gpu/surface_handle_types_mac.h"
19 void OnNativeSurfaceBuffersSwappedOnUIThread(
20 base::ScopedCFTypeRef<IOSurfaceRef> io_surface,
21 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params) {
22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
23 gfx::AcceleratedWidget native_widget =
24 content::GpuSurfaceTracker::Get()->AcquireNativeWidget(params.surface_id);
25 IOSurfaceID io_surface_id = content::IOSurfaceIDFromSurfaceHandle(
26 params.surface_handle);
27 [native_widget gotAcceleratedIOSurfaceFrame:io_surface_id
28 withOutputSurfaceID:params.surface_id
29 withLatencyInfo:params.latency_info
30 withPixelSize:params.size
31 withScaleFactor:params.scale_factor];
38 void RenderWidgetHelper::OnNativeSurfaceBuffersSwappedOnIOThread(
39 GpuProcessHost* gpu_process_host,
40 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params& params) {
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
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));
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)));
60 BrowserThread::PostTask(
63 base::Bind(&OnNativeSurfaceBuffersSwappedOnUIThread, io_surface, params));
66 } // namespace content