1 // Copyright 2018 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 COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_OUTPUT_DEVICE_BACKING_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_OUTPUT_DEVICE_BACKING_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/memory/unsafe_shared_memory_region.h"
13 #include "components/viz/service/viz_service_export.h"
14 #include "ui/gfx/geometry/size.h"
15 
16 namespace base {
17 class SharedMemory;
18 }
19 
20 namespace viz {
21 
22 // Allocates and owns a SharedMemory backing for multiple SoftwareOutputDevices.
23 // The backing will be big enough to hold the largest size returned by a
24 // client's GetViewportPixelSize().
25 class VIZ_SERVICE_EXPORT OutputDeviceBacking {
26  public:
27   class Client {
28    public:
29     virtual const gfx::Size& GetViewportPixelSize() const = 0;
30     virtual void ReleaseCanvas() = 0;
31 
32    protected:
33     virtual ~Client() = default;
34   };
35 
36   OutputDeviceBacking();
37   ~OutputDeviceBacking();
38 
39   void RegisterClient(Client* client);
40   void UnregisterClient(Client* client);
41 
42   // Called when a client has resized. Clients should call Resize() after being
43   // registered when they have a valid size. Will potential invalidate
44   // SharedMemory and call ReleaseCanvas() on clients.
45   void ClientResized();
46 
47   // Requests a SharedMemory segment large enough to fit |viewport_size|. Will
48   // return null if |viewport_size| is too large to safely allocate
49   // a shared memory region.
50   base::UnsafeSharedMemoryRegion* GetSharedMemoryRegion(
51       const gfx::Size& viewport_size);
52 
53   // Returns the maximum size in bytes needed for the largest viewport from
54   // registered clients.
55   size_t GetMaxViewportBytes();
56 
57  private:
58   std::vector<Client*> clients_;
59   base::UnsafeSharedMemoryRegion region_;
60   size_t created_shm_bytes_ = 0;
61 
62   DISALLOW_COPY_AND_ASSIGN(OutputDeviceBacking);
63 };
64 
65 }  // namespace viz
66 
67 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_EMBEDDER_OUTPUT_DEVICE_BACKING_H_
68