12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2010 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CC_LAYERS_TEXTURE_LAYER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CC_LAYERS_TEXTURE_LAYER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/synchronization/lock.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/threading/thread_checker.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/base/cc_export.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/layer.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/resources/texture_mailbox.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace cc {
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class BlockingTaskRunner;
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class SingleReleaseCallback;
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass SingleReleaseCallbackImpl;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TextureLayerClient;
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// A Layer containing a the rendered output of a plugin instance.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CC_EXPORT TextureLayer : public Layer {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class CC_EXPORT TextureMailboxHolder
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : public base::RefCountedThreadSafe<TextureMailboxHolder> {
28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)   public:
29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    class CC_EXPORT MainThreadReference {
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)     public:
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      explicit MainThreadReference(TextureMailboxHolder* holder);
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      ~MainThreadReference();
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      TextureMailboxHolder* holder() { return holder_.get(); }
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)     private:
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<TextureMailboxHolder> holder_;
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      DISALLOW_COPY_AND_ASSIGN(MainThreadReference);
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    };
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const TextureMailbox& mailbox() const { return mailbox_; }
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    void Return(uint32 sync_point, bool is_lost);
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Gets a ReleaseCallback that can be called from another thread. Note: the
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // caller must ensure the callback is called.
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_ptr<SingleReleaseCallbackImpl> GetCallbackForImplThread();
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)   protected:
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    friend class TextureLayer;
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Protected visiblity so only TextureLayer and unit tests can create these.
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    static scoped_ptr<MainThreadReference> Create(
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        const TextureMailbox& mailbox,
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        scoped_ptr<SingleReleaseCallback> release_callback);
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual ~TextureMailboxHolder();
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)   private:
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    friend class base::RefCountedThreadSafe<TextureMailboxHolder>;
58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    friend class MainThreadReference;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    explicit TextureMailboxHolder(
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        const TextureMailbox& mailbox,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        scoped_ptr<SingleReleaseCallback> release_callback);
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    void InternalAddRef();
64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    void InternalRelease();
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    void ReturnAndReleaseOnImplThread(
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        uint32 sync_point,
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        bool is_lost,
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        BlockingTaskRunner* main_thread_task_runner);
69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // These members are only accessed on the main thread, or on the impl thread
71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // during commit where the main thread is blocked.
72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    unsigned internal_references_;
73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    TextureMailbox mailbox_;
74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    scoped_ptr<SingleReleaseCallback> release_callback_;
75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // This lock guards the sync_point_ and is_lost_ fields because they can be
77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // accessed on both the impl and main thread. We do this to ensure that the
78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // values of these fields are well-ordered such that the last call to
79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // ReturnAndReleaseOnImplThread() defines their values.
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    base::Lock arguments_lock_;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    uint32 sync_point_;
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    bool is_lost_;
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::ThreadChecker main_thread_checker_;
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(TextureMailboxHolder);
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  };
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used when mailbox names are specified instead of texture IDs.
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static scoped_refptr<TextureLayer> CreateForMailbox(
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      TextureLayerClient* client);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
91c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Resets the client, which also resets the texture.
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void ClearClient();
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
94c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Resets the texture.
95c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void ClearTexture();
96c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      OVERRIDE;
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets whether this texture should be Y-flipped at draw time. Defaults to
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true.
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetFlipped(bool flipped);
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1).
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetUV(const gfx::PointF& top_left, const gfx::PointF& bottom_right);
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets an opacity value per vertex. It will be multiplied by the layer
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // opacity value.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetVertexOpacity(float bottom_left,
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        float top_left,
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        float top_right,
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        float bottom_right);
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets whether the alpha channel is premultiplied or unpremultiplied.
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Defaults to true.
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetPremultipliedAlpha(bool premultiplied_alpha);
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Sets whether the texture should be blended with the background color
1197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // at draw time. Defaults to false.
1207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void SetBlendBackgroundColor(bool blend);
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets whether this context should rate limit on damage to prevent too many
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // frames from being queued up before the compositor gets a chance to run.
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Requires a non-nil client.  Defaults to false.
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetRateLimitContext(bool rate_limit);
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Code path for plugins which supply their own mailbox.
128d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void SetTextureMailbox(const TextureMailbox& mailbox,
129d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                         scoped_ptr<SingleReleaseCallback> release_callback);
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Use this for special cases where the same texture is used to back the
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TextureLayer across all frames.
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // WARNING: DON'T ACTUALLY USE THIS WHAT YOU ARE DOING IS WRONG.
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(danakj): Remove this when pepper doesn't need it. crbug.com/350204
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void SetTextureMailboxWithoutReleaseCallback(const TextureMailbox& mailbox);
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect) OVERRIDE;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetLayerTreeHost(LayerTreeHost* layer_tree_host) OVERRIDE;
1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  virtual bool Update(ResourceUpdateQueue* queue,
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                      const OcclusionTracker<Layer>* occlusion) OVERRIDE;
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE;
14303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual SimpleEnclosedRegion VisibleContentOpaqueRegion() const OVERRIDE;
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
146c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  explicit TextureLayer(TextureLayerClient* client);
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~TextureLayer();
1486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual bool HasDrawableContent() const OVERRIDE;
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SetTextureMailboxInternal(
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const TextureMailbox& mailbox,
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      scoped_ptr<SingleReleaseCallback> release_callback,
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      bool requires_commit,
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      bool allow_mailbox_reuse);
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TextureLayerClient* client_;
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool flipped_;
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF uv_top_left_;
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF uv_bottom_right_;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // [bottom left, top left, top right, bottom right]
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  float vertex_opacity_[4];
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool premultiplied_alpha_;
1657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool blend_background_color_;
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool rate_limit_context_;
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<TextureMailboxHolder::MainThreadReference> holder_ref_;
1697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool needs_set_mailbox_;
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TextureLayer);
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
174c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace cc
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CC_LAYERS_TEXTURE_LAYER_H_
176