OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 MEDIA_VIDEO_CAPTURE_SCREEN_SHARED_BUFFER_H_ | |
6 #define MEDIA_VIDEO_CAPTURE_SCREEN_SHARED_BUFFER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/process.h" | |
11 #include "base/shared_memory.h" | |
12 #include "media/base/media_export.h" | |
13 | |
14 namespace media { | |
15 | |
16 // Represents a memory buffer that can be shared between multiple processes. | |
17 // It is more or less a convenience wrapper around base::SharedMemory providing | |
18 // ref-counted lifetime management and unique buffer identifiers. | |
19 class MEDIA_EXPORT SharedBuffer | |
20 : public base::RefCountedThreadSafe<SharedBuffer> { | |
21 public: | |
22 // Creates a new shared memory buffer of the given size and maps it to | |
23 // the memory of the calling process. If the operation fails for any reason, | |
24 // ptr() method will return NULL. This constructor set the identifier of this | |
25 // buffer to 0. | |
26 explicit SharedBuffer(uint32 size); | |
27 | |
28 // Opens an existing shared memory buffer and maps it to the memory of | |
29 // the calling process (in read only mode). If the operation fails for any | |
30 // reason, ptr() method will return NULL. | |
31 SharedBuffer(int id, base::SharedMemoryHandle handle, uint32 size); | |
32 | |
33 // Opens an existing shared memory buffer created by a different process and | |
34 // maps it to the memory of the calling process (in read only mode). If | |
35 // the operation fails for any reason, ptr() method will return NULL. | |
36 SharedBuffer(int id, base::SharedMemoryHandle handle, | |
37 base::ProcessHandle process, uint32 size); | |
38 | |
39 // Returns pointer to the beginning of the allocated data buffer. Returns NULL | |
40 // if the object initialization failed for any reason. | |
41 void* ptr() const { return shared_memory_.memory(); } | |
42 | |
43 // Returns handle of the shared memory section containing the allocated | |
44 // data buffer. | |
45 base::SharedMemoryHandle handle() const { return shared_memory_.handle(); } | |
46 | |
47 // Calls SharedMemory::ShareToProcess to share the handle with a different | |
48 // process. | |
49 bool ShareToProcess(base::ProcessHandle process, | |
50 base::SharedMemoryHandle* new_handle); | |
51 | |
52 int id() const { return id_; } | |
53 uint32 size() const { return size_; } | |
54 | |
55 void set_id(int id) { id_ = id; } | |
56 | |
57 private: | |
58 friend class base::RefCountedThreadSafe<SharedBuffer>; | |
59 virtual ~SharedBuffer(); | |
60 | |
61 // Unique identifier of the buffer or 0 if ID hasn't been set. | |
62 int id_; | |
63 | |
64 // Shared memory section backing up the buffer. | |
65 base::SharedMemory shared_memory_; | |
66 | |
67 // Size of the buffer in bytes. | |
68 uint32 size_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(SharedBuffer); | |
71 }; | |
72 | |
73 } // namespace media | |
74 | |
75 #endif // MEDIA_VIDEO_CAPTURE_SCREEN_SHARED_BUFFER_H_ | |
OLD | NEW |