blob: 678eb9aade853081b349a4d9e4fd302889073412 [file] [log] [blame]
reveman7c45b3082015-06-04 01:27:081// Copyright 2015 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#ifndef CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
6#define CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_
7
8#include <mach/mach.h>
9
10#include <map>
11#include <utility>
12
13#include "base/containers/scoped_ptr_hash_map.h"
14#include "base/mac/dispatch_source_mach.h"
15#include "base/mac/scoped_mach_port.h"
16#include "base/macros.h"
17#include "base/memory/scoped_ptr.h"
18#include "base/memory/singleton.h"
19#include "base/synchronization/lock.h"
revemanac364edf42015-06-10 22:41:5520#include "content/common/content_export.h"
reveman7c45b3082015-06-04 01:27:0821#include "content/common/mac/io_surface_manager_messages.h"
22#include "content/common/mac/io_surface_manager_token.h"
dcastagna5ab612c02015-11-01 20:15:0823#include "ui/gfx/mac/io_surface_manager.h"
reveman7c45b3082015-06-04 01:27:0824
25namespace content {
26
ericrkc9984ebe2015-08-17 14:22:3727// TODO(ericrk): Use gfx::GenericSharedMemoryId as the |io_surface_id| in
28// this file. Allows for more type-safe usage of GpuMemoryBufferIds as the
29// type of the |io_surface_id|, as it is a typedef of
30// gfx::GenericSharedMemoryId.
31
reveman7c45b3082015-06-04 01:27:0832// Implementation of IOSurfaceManager that provides a mechanism for child
33// processes to register and acquire IOSurfaces through a Mach service.
dcastagna5ab612c02015-11-01 20:15:0834class CONTENT_EXPORT BrowserIOSurfaceManager : public gfx::IOSurfaceManager {
reveman7c45b3082015-06-04 01:27:0835 public:
36 // Returns the global BrowserIOSurfaceManager.
37 static BrowserIOSurfaceManager* GetInstance();
38
39 // Look up the IOSurfaceManager service port that's been registered with
40 // the bootstrap server. |pid| is the process ID of the service.
41 static base::mac::ScopedMachSendRight LookupServicePort(pid_t pid);
42
rsesekdba84112015-09-18 19:22:0743 // Returns the name of the service registered with the bootstrap server.
44 static std::string GetMachPortName();
45
reveman7c45b3082015-06-04 01:27:0846 // Overridden from IOSurfaceManager:
dcastagna5ab612c02015-11-01 20:15:0847 bool RegisterIOSurface(gfx::IOSurfaceId io_surface_id,
reveman7c45b3082015-06-04 01:27:0848 int client_id,
49 IOSurfaceRef io_surface) override;
dcastagna5ab612c02015-11-01 20:15:0850 void UnregisterIOSurface(gfx::IOSurfaceId io_surface_id,
51 int client_id) override;
52 IOSurfaceRef AcquireIOSurface(gfx::IOSurfaceId io_surface_id) override;
reveman7c45b3082015-06-04 01:27:0853
54 // Performs any necessary setup that cannot happen in the constructor.
55 void EnsureRunning();
56
reveman459965542015-06-16 18:12:5057 // Generate a unique unguessable token that the GPU process can use to
reveman7c45b3082015-06-04 01:27:0858 // register/unregister IOSurface for use by clients.
reveman459965542015-06-16 18:12:5059 IOSurfaceManagerToken GenerateGpuProcessToken();
60
61 // Invalidate the previously generated GPU process token.
62 void InvalidateGpuProcessToken();
reveman7c45b3082015-06-04 01:27:0863
64 // Generate a unique unguessable token that the child process associated
65 // |child_process_id| can use to acquire IOSurface references.
66 IOSurfaceManagerToken GenerateChildProcessToken(int child_process_id);
67
reveman459965542015-06-16 18:12:5068 // Invalidate a previously generated child process token.
reveman7c45b3082015-06-04 01:27:0869 void InvalidateChildProcessToken(const IOSurfaceManagerToken& token);
70
71 private:
72 friend class BrowserIOSurfaceManagerTest;
olli.raula36aa8be2015-09-10 11:14:2273 friend struct base::DefaultSingletonTraits<BrowserIOSurfaceManager>;
reveman7c45b3082015-06-04 01:27:0874
75 BrowserIOSurfaceManager();
76 ~BrowserIOSurfaceManager() override;
77
78 // Performs any initialization work.
79 bool Initialize();
80
81 // Message handler that is invoked on |dispatch_source_| when an
82 // incoming message needs to be received.
83 void HandleRequest();
84
85 // Message handlers that are invoked from HandleRequest.
ccameron7c766ff42015-09-18 19:43:1786 void HandleRegisterIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:0887 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
88 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply);
89 bool HandleUnregisterIOSurfaceRequest(
90 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request);
ccameron7c766ff42015-09-18 19:43:1791 void HandleAcquireIOSurfaceRequest(
reveman7c45b3082015-06-04 01:27:0892 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
93 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply);
94
95 // Whether or not the class has been initialized.
96 bool initialized_;
97
98 // The Mach port on which the server listens.
99 base::mac::ScopedMachReceiveRight server_port_;
100
101 // The dispatch source and queue on which Mach messages will be received.
102 scoped_ptr<base::DispatchSourceMach> dispatch_source_;
103
104 // Stores the IOSurfaces for all GPU clients. The key contains the IOSurface
105 // id and the Child process unique id of the owner.
dcastagna5ab612c02015-11-01 20:15:08106 using IOSurfaceMapKey = std::pair<gfx::IOSurfaceId, int>;
reveman7c45b3082015-06-04 01:27:08107 using IOSurfaceMap =
108 base::ScopedPtrHashMap<IOSurfaceMapKey,
109 scoped_ptr<base::mac::ScopedMachSendRight>>;
110 IOSurfaceMap io_surfaces_;
111
112 // Stores the Child process unique id (RenderProcessHost ID) for every
113 // token.
114 using ChildProcessIdMap = std::map<IOSurfaceManagerToken, int>;
115 ChildProcessIdMap child_process_ids_;
116
117 // Stores the GPU process token.
reveman459965542015-06-16 18:12:50118 IOSurfaceManagerToken gpu_process_token_;
reveman7c45b3082015-06-04 01:27:08119
reveman459965542015-06-16 18:12:50120 // Mutex that guards |initialized_|, |io_surfaces_|, |child_process_ids_|
121 // and |gpu_process_token_|.
122 base::Lock lock_;
reveman7c45b3082015-06-04 01:27:08123
124 DISALLOW_COPY_AND_ASSIGN(BrowserIOSurfaceManager);
125};
126
127} // namespace content
128
129#endif // CONTENT_BROWSER_BROWSER_IO_SURFACE_MANAGER_MAC_H_