texture_layer.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #ifndef CC_LAYERS_TEXTURE_LAYER_H_
  5. #define CC_LAYERS_TEXTURE_LAYER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "cc/cc_export.h"
  15. #include "cc/layers/layer.h"
  16. #include "cc/resources/cross_thread_shared_bitmap.h"
  17. #include "cc/resources/shared_bitmap_id_registrar.h"
  18. #include "components/viz/common/resources/release_callback.h"
  19. #include "components/viz/common/resources/transferable_resource.h"
  20. namespace gpu {
  21. struct SyncToken;
  22. }
  23. namespace cc {
  24. class TextureLayer;
  25. class TextureLayerClient;
  26. // A Layer containing a the rendered output of a plugin instance. It can be used
  27. // to display gpu or software resources, depending if the compositor is working
  28. // in gpu or software compositing mode (the resources must match the compositing
  29. // mode).
  30. class CC_EXPORT TextureLayer : public Layer, SharedBitmapIdRegistrar {
  31. public:
  32. class CC_EXPORT TransferableResourceHolder
  33. : public base::RefCountedThreadSafe<TransferableResourceHolder> {
  34. public:
  35. TransferableResourceHolder(const TransferableResourceHolder&) = delete;
  36. TransferableResourceHolder& operator=(const TransferableResourceHolder&) =
  37. delete;
  38. const viz::TransferableResource& resource() const { return resource_; }
  39. void Return(
  40. scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner,
  41. const gpu::SyncToken& sync_token,
  42. bool is_lost);
  43. static scoped_refptr<TransferableResourceHolder> Create(
  44. const viz::TransferableResource& resource,
  45. viz::ReleaseCallback release_callback);
  46. protected:
  47. virtual ~TransferableResourceHolder();
  48. private:
  49. friend class base::RefCountedThreadSafe<TransferableResourceHolder>;
  50. explicit TransferableResourceHolder(
  51. const viz::TransferableResource& resource,
  52. viz::ReleaseCallback release_callback);
  53. const viz::TransferableResource resource_;
  54. // This is accessed only on the main thread.
  55. viz::ReleaseCallback release_callback_;
  56. // release_callback_task_runner_, sync_token_, and is_lost_ are only
  57. // modified on the impl thread, and only read from the destructor, so they
  58. // are not subject to race conditions.
  59. // If a reference to the resource is sent to the impl thread, then there's a
  60. // possibility that the resource will be destructed on the impl thread; but
  61. // release_callback_ has to run on the main thread. In that case, we use
  62. // release_callback_task_runner_ to PostTask to run the ReleaseCallback.
  63. scoped_refptr<base::SequencedTaskRunner> release_callback_task_runner_;
  64. gpu::SyncToken sync_token_;
  65. bool is_lost_ = false;
  66. };
  67. // Used when mailbox names are specified instead of texture IDs.
  68. static scoped_refptr<TextureLayer> CreateForMailbox(
  69. TextureLayerClient* client);
  70. TextureLayer(const TextureLayer&) = delete;
  71. TextureLayer& operator=(const TextureLayer&) = delete;
  72. // Resets the client, which also resets the texture.
  73. void ClearClient();
  74. // Resets the texture.
  75. void ClearTexture();
  76. std::unique_ptr<LayerImpl> CreateLayerImpl(
  77. LayerTreeImpl* tree_impl) const override;
  78. // Sets whether this texture should be Y-flipped at draw time. Defaults to
  79. // true.
  80. void SetFlipped(bool flipped);
  81. bool flipped() const { return flipped_.Read(*this); }
  82. // Sets whether this texture should use nearest neighbor interpolation as
  83. // opposed to bilinear. Defaults to false.
  84. void SetNearestNeighbor(bool nearest_neighbor);
  85. // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1).
  86. void SetUV(const gfx::PointF& top_left, const gfx::PointF& bottom_right);
  87. // Sets whether the alpha channel is premultiplied or unpremultiplied.
  88. // Defaults to true.
  89. void SetPremultipliedAlpha(bool premultiplied_alpha);
  90. // Sets whether the texture should be blended with the background color
  91. // at draw time. Defaults to false.
  92. void SetBlendBackgroundColor(bool blend);
  93. // Sets whether we need to ensure that Texture is opaque before using it.
  94. // This will blend texture with black color. Defaults to false.
  95. void SetForceTextureToOpaque(bool opaque);
  96. // Code path for plugins which supply their own mailbox.
  97. void SetTransferableResource(const viz::TransferableResource& resource,
  98. viz::ReleaseCallback release_callback);
  99. void SetLayerTreeHost(LayerTreeHost* layer_tree_host) override;
  100. bool Update() override;
  101. bool IsSnappedToPixelGridInTarget() const override;
  102. void PushPropertiesTo(LayerImpl* layer,
  103. const CommitState& commit_state,
  104. const ThreadUnsafeCommitState& unsafe_state) override;
  105. // Request a mapping from SharedBitmapId to SharedMemory be registered via the
  106. // LayerTreeFrameSink with the display compositor. Once this mapping is
  107. // registered, the SharedBitmapId can be used in TransferableResources given
  108. // to the TextureLayer for display. The SharedBitmapId registration will end
  109. // when the returned SharedBitmapIdRegistration object is destroyed.
  110. // Implemented as a SharedBitmapIdRegistrar interface so that clients can
  111. // have a limited API access.
  112. SharedBitmapIdRegistration RegisterSharedBitmapId(
  113. const viz::SharedBitmapId& id,
  114. scoped_refptr<CrossThreadSharedBitmap> bitmap) override;
  115. const viz::TransferableResource current_transferable_resource() const {
  116. if (const auto& resource_holder = resource_holder_.Read(*this))
  117. return resource_holder->resource();
  118. return viz::TransferableResource();
  119. }
  120. protected:
  121. explicit TextureLayer(TextureLayerClient* client);
  122. ~TextureLayer() override;
  123. bool HasDrawableContent() const override;
  124. private:
  125. void SetTransferableResourceInternal(
  126. const viz::TransferableResource& resource,
  127. viz::ReleaseCallback release_callback,
  128. bool requires_commit);
  129. // Friends to give access to UnregisterSharedBitmapId().
  130. friend SharedBitmapIdRegistration;
  131. // Remove a mapping from SharedBitmapId to SharedMemory in the display
  132. // compositor.
  133. void UnregisterSharedBitmapId(viz::SharedBitmapId id);
  134. ProtectedSequenceForbidden<raw_ptr<TextureLayerClient>> client_;
  135. ProtectedSequenceReadable<bool> flipped_;
  136. ProtectedSequenceReadable<bool> nearest_neighbor_;
  137. ProtectedSequenceReadable<gfx::PointF> uv_top_left_;
  138. ProtectedSequenceReadable<gfx::PointF> uv_bottom_right_;
  139. // [bottom left, top left, top right, bottom right]
  140. ProtectedSequenceReadable<bool> premultiplied_alpha_;
  141. ProtectedSequenceReadable<bool> blend_background_color_;
  142. ProtectedSequenceReadable<bool> force_texture_to_opaque_;
  143. ProtectedSequenceWritable<scoped_refptr<TransferableResourceHolder>>
  144. resource_holder_;
  145. ProtectedSequenceWritable<bool> needs_set_resource_;
  146. typedef base::flat_map<viz::SharedBitmapId,
  147. scoped_refptr<CrossThreadSharedBitmap>>
  148. BitMapMap;
  149. // The set of SharedBitmapIds to register with the LayerTreeFrameSink on the
  150. // compositor thread. These requests are forwarded to the TextureLayerImpl to
  151. // use, then stored in |registered_bitmaps_| to re-send if the
  152. // TextureLayerImpl object attached to this layer changes, by moving out of
  153. // the LayerTreeHost.
  154. ProtectedSequenceWritable<BitMapMap> to_register_bitmaps_;
  155. // The set of previously registered SharedBitmapIds for the current
  156. // LayerTreeHost. If the LayerTreeHost changes, these must be re-sent to the
  157. // (new) TextureLayerImpl to be re-registered.
  158. ProtectedSequenceWritable<BitMapMap> registered_bitmaps_;
  159. // The SharedBitmapIds to unregister on the compositor thread, passed to the
  160. // TextureLayerImpl.
  161. ProtectedSequenceWritable<std::vector<viz::SharedBitmapId>>
  162. to_unregister_bitmap_ids_;
  163. const base::WeakPtrFactory<TextureLayer> weak_ptr_factory_{this};
  164. };
  165. } // namespace cc
  166. #endif // CC_LAYERS_TEXTURE_LAYER_H_