surface.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#ifndef CC_SURFACES_SURFACE_H_
6#define CC_SURFACES_SURFACE_H_
7
8#include "base/containers/hash_tables.h"
9#include "base/macros.h"
10#include "base/memory/scoped_ptr.h"
11#include "cc/resources/resource_provider.h"
12#include "cc/resources/return_callback.h"
13#include "cc/surfaces/surface_client.h"
14#include "cc/surfaces/surface_id.h"
15#include "cc/surfaces/surfaces_export.h"
16#include "ui/gfx/size.h"
17
18namespace cc {
19class CompositorFrame;
20class SurfaceManager;
21
22class CC_SURFACES_EXPORT Surface {
23 public:
24  Surface(SurfaceManager* manager,
25          SurfaceClient* client,
26          const gfx::Size& size);
27  ~Surface();
28
29  const gfx::Size& size() const { return size_; }
30  SurfaceId surface_id() const { return surface_id_; }
31
32  void QueueFrame(scoped_ptr<CompositorFrame> frame);
33  // Returns the most recent frame that is eligible to be rendered.
34  CompositorFrame* GetEligibleFrame();
35
36  // Takes a reference to all of the current frame's resources for an external
37  // consumer (e.g. a ResourceProvider). The caller to this should call
38  // UnrefResources() when they are done with the resources.
39  void RefCurrentFrameResources();
40  void UnrefResources(const ReturnedResourceArray& resources);
41
42  // Returns all resources that are currently not in use to the client.
43  void ReturnUnusedResourcesToClient();
44
45 private:
46  void ReceiveResourcesFromClient(const TransferableResourceArray& resources);
47
48  SurfaceManager* manager_;
49  SurfaceClient* client_;
50  gfx::Size size_;
51  SurfaceId surface_id_;
52  // TODO(jamesr): Support multiple frames in flight.
53  scoped_ptr<CompositorFrame> current_frame_;
54
55  struct ResourceRefs {
56    ResourceRefs();
57
58    int refs_received_from_child;
59    int refs_holding_resource_alive;
60  };
61  // Keeps track of the number of users currently in flight for each resource
62  // ID we've received from the client. When this counter hits zero for a
63  // particular resource, that ID is available to return to the client.
64  typedef base::hash_map<ResourceProvider::ResourceId, ResourceRefs>
65      ResourceIdCountMap;
66  ResourceIdCountMap resource_id_use_count_map_;
67
68  ReturnedResourceArray resources_available_to_return_;
69
70  DISALLOW_COPY_AND_ASSIGN(Surface);
71};
72
73}  // namespace cc
74
75#endif  // CC_SURFACES_SURFACE_H_
76