texture_layer.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
1// Copyright 2010 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_LAYERS_TEXTURE_LAYER_H_
6#define CC_LAYERS_TEXTURE_LAYER_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/synchronization/lock.h"
12#include "cc/base/cc_export.h"
13#include "cc/layers/layer.h"
14#include "cc/resources/texture_mailbox.h"
15
16namespace cc {
17class BlockingTaskRunner;
18class SingleReleaseCallback;
19class TextureLayerClient;
20
21// A Layer containing a the rendered output of a plugin instance.
22class CC_EXPORT TextureLayer : public Layer {
23 public:
24  class CC_EXPORT TextureMailboxHolder
25      : public base::RefCountedThreadSafe<TextureMailboxHolder> {
26   public:
27    class CC_EXPORT MainThreadReference {
28     public:
29      explicit MainThreadReference(TextureMailboxHolder* holder);
30      ~MainThreadReference();
31      TextureMailboxHolder* holder() { return holder_.get(); }
32
33     private:
34      scoped_refptr<TextureMailboxHolder> holder_;
35      DISALLOW_COPY_AND_ASSIGN(MainThreadReference);
36    };
37
38    const TextureMailbox& mailbox() const { return mailbox_; }
39    void Return(uint32 sync_point, bool is_lost);
40
41    // Gets a ReleaseCallback that can be called from another thread. Note: the
42    // caller must ensure the callback is called.
43    scoped_ptr<SingleReleaseCallback> GetCallbackForImplThread();
44
45   protected:
46    friend class TextureLayer;
47
48    // Protected visiblity so only TextureLayer and unit tests can create these.
49    static scoped_ptr<MainThreadReference> Create(
50        const TextureMailbox& mailbox,
51        scoped_ptr<SingleReleaseCallback> release_callback);
52    virtual ~TextureMailboxHolder();
53
54   private:
55    friend class base::RefCountedThreadSafe<TextureMailboxHolder>;
56    friend class MainThreadReference;
57    explicit TextureMailboxHolder(
58        const TextureMailbox& mailbox,
59        scoped_ptr<SingleReleaseCallback> release_callback);
60
61    void InternalAddRef();
62    void InternalRelease();
63    void ReturnAndReleaseOnImplThread(uint32 sync_point, bool is_lost);
64
65    // This member is thread safe, and is accessed on main and impl threads.
66    const scoped_refptr<BlockingTaskRunner> message_loop_;
67
68    // These members are only accessed on the main thread, or on the impl thread
69    // during commit where the main thread is blocked.
70    unsigned internal_references_;
71    TextureMailbox mailbox_;
72    scoped_ptr<SingleReleaseCallback> release_callback_;
73
74    // This lock guards the sync_point_ and is_lost_ fields because they can be
75    // accessed on both the impl and main thread. We do this to ensure that the
76    // values of these fields are well-ordered such that the last call to
77    // ReturnAndReleaseOnImplThread() defines their values.
78    base::Lock arguments_lock_;
79    uint32 sync_point_;
80    bool is_lost_;
81    DISALLOW_COPY_AND_ASSIGN(TextureMailboxHolder);
82  };
83
84  // Used when mailbox names are specified instead of texture IDs.
85  static scoped_refptr<TextureLayer> CreateForMailbox(
86      TextureLayerClient* client);
87
88  // Resets the client, which also resets the texture.
89  void ClearClient();
90
91  // Resets the texture.
92  void ClearTexture();
93
94  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
95      OVERRIDE;
96
97  // Sets whether this texture should be Y-flipped at draw time. Defaults to
98  // true.
99  void SetFlipped(bool flipped);
100
101  // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1).
102  void SetUV(const gfx::PointF& top_left, const gfx::PointF& bottom_right);
103
104  // Sets an opacity value per vertex. It will be multiplied by the layer
105  // opacity value.
106  void SetVertexOpacity(float bottom_left,
107                        float top_left,
108                        float top_right,
109                        float bottom_right);
110
111  // Sets whether the alpha channel is premultiplied or unpremultiplied.
112  // Defaults to true.
113  void SetPremultipliedAlpha(bool premultiplied_alpha);
114
115  // Sets whether the texture should be blended with the background color
116  // at draw time. Defaults to false.
117  void SetBlendBackgroundColor(bool blend);
118
119  // Sets whether this context should rate limit on damage to prevent too many
120  // frames from being queued up before the compositor gets a chance to run.
121  // Requires a non-nil client.  Defaults to false.
122  void SetRateLimitContext(bool rate_limit);
123
124  // Code path for plugins which supply their own mailbox.
125  void SetTextureMailbox(const TextureMailbox& mailbox,
126                         scoped_ptr<SingleReleaseCallback> release_callback);
127
128  // Use this for special cases where the same texture is used to back the
129  // TextureLayer across all frames.
130  // WARNING: DON'T ACTUALLY USE THIS WHAT YOU ARE DOING IS WRONG.
131  // TODO(danakj): Remove this when pepper doesn't need it. crbug.com/350204
132  void SetTextureMailboxWithoutReleaseCallback(const TextureMailbox& mailbox);
133
134  virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect) OVERRIDE;
135
136  virtual void SetLayerTreeHost(LayerTreeHost* layer_tree_host) OVERRIDE;
137  virtual bool Update(ResourceUpdateQueue* queue,
138                      const OcclusionTracker<Layer>* occlusion) OVERRIDE;
139  virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE;
140  virtual Region VisibleContentOpaqueRegion() const OVERRIDE;
141
142 protected:
143  explicit TextureLayer(TextureLayerClient* client);
144  virtual ~TextureLayer();
145  virtual bool HasDrawableContent() const OVERRIDE;
146
147 private:
148  void SetTextureMailboxInternal(
149      const TextureMailbox& mailbox,
150      scoped_ptr<SingleReleaseCallback> release_callback,
151      bool requires_commit,
152      bool allow_mailbox_reuse);
153
154  TextureLayerClient* client_;
155
156  bool flipped_;
157  gfx::PointF uv_top_left_;
158  gfx::PointF uv_bottom_right_;
159  // [bottom left, top left, top right, bottom right]
160  float vertex_opacity_[4];
161  bool premultiplied_alpha_;
162  bool blend_background_color_;
163  bool rate_limit_context_;
164
165  scoped_ptr<TextureMailboxHolder::MainThreadReference> holder_ref_;
166  bool needs_set_mailbox_;
167
168  DISALLOW_COPY_AND_ASSIGN(TextureLayer);
169};
170
171}  // namespace cc
172#endif  // CC_LAYERS_TEXTURE_LAYER_H_
173