1// Copyright (c) 2013 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_COMMON_HOST_SHARED_BITMAP_MANAGER_H_
6#define CONTENT_COMMON_HOST_SHARED_BITMAP_MANAGER_H_
7
8#include <map>
9#include <set>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/containers/hash_tables.h"
14#include "base/hash.h"
15#include "base/memory/ref_counted.h"
16#include "base/memory/scoped_ptr.h"
17#include "base/memory/shared_memory.h"
18#include "base/synchronization/lock.h"
19#include "cc/resources/shared_bitmap_manager.h"
20#include "content/common/content_export.h"
21
22namespace BASE_HASH_NAMESPACE {
23#if defined(COMPILER_GCC)
24template <>
25struct hash<cc::SharedBitmapId> {
26  size_t operator()(const cc::SharedBitmapId& id) const {
27    return base::Hash(reinterpret_cast<const char*>(id.name), sizeof(id.name));
28  }
29};
30#elif defined(COMPILER_MSVC)
31inline std::size_t hash_value(const cc::SharedBitmapId& id) {
32  return base::Hash(reinterpret_cast<const char*>(id.name), sizeof(id.name));
33}
34#endif  // COMPILER
35}  // namespace BASE_HASH_NAMESPACE
36
37namespace content {
38class BitmapData;
39
40class CONTENT_EXPORT HostSharedBitmapManager : public cc::SharedBitmapManager {
41 public:
42  HostSharedBitmapManager();
43  virtual ~HostSharedBitmapManager();
44
45  static HostSharedBitmapManager* current();
46
47  // cc::SharedBitmapManager implementation.
48  virtual scoped_ptr<cc::SharedBitmap> AllocateSharedBitmap(
49      const gfx::Size& size) OVERRIDE;
50  virtual scoped_ptr<cc::SharedBitmap> GetSharedBitmapFromId(
51      const gfx::Size& size,
52      const cc::SharedBitmapId&) OVERRIDE;
53  virtual scoped_ptr<cc::SharedBitmap> GetBitmapForSharedMemory(
54      base::SharedMemory*) OVERRIDE;
55
56  void AllocateSharedBitmapForChild(
57      base::ProcessHandle process_handle,
58      size_t buffer_size,
59      const cc::SharedBitmapId& id,
60      base::SharedMemoryHandle* shared_memory_handle);
61  void ChildAllocatedSharedBitmap(size_t buffer_size,
62                                  const base::SharedMemoryHandle& handle,
63                                  base::ProcessHandle process_handle,
64                                  const cc::SharedBitmapId& id);
65  void ChildDeletedSharedBitmap(const cc::SharedBitmapId& id);
66  void ProcessRemoved(base::ProcessHandle process_handle);
67
68  size_t AllocatedBitmapCount() const;
69
70 private:
71  void FreeSharedMemoryFromMap(cc::SharedBitmap* bitmap);
72
73  mutable base::Lock lock_;
74
75  typedef base::hash_map<cc::SharedBitmapId, scoped_refptr<BitmapData> >
76      BitmapMap;
77  BitmapMap handle_map_;
78
79  typedef base::hash_map<base::ProcessHandle,
80                         base::hash_set<cc::SharedBitmapId> > ProcessMap;
81  ProcessMap process_map_;
82};
83
84}  // namespace content
85
86#endif  // CONTENT_COMMON_HOST_SHARED_BITMAP_MANAGER_H_
87