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/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/transferable_resource.h" 19 20 namespace gpu { 21 struct SyncToken; 22 } 23 24 namespace viz { 25 class SingleReleaseCallback; 26 } 27 28 namespace cc { 29 class SingleReleaseCallback; 30 class TextureLayer; 31 class TextureLayerClient; 32 33 // A Layer containing a the rendered output of a plugin instance. It can be used 34 // to display gpu or software resources, depending if the compositor is working 35 // in gpu or software compositing mode (the resources must match the compositing 36 // mode). 37 class CC_EXPORT TextureLayer : public Layer, SharedBitmapIdRegistrar { 38 public: 39 class CC_EXPORT TransferableResourceHolder 40 : public base::RefCountedThreadSafe<TransferableResourceHolder> { 41 public: 42 class CC_EXPORT MainThreadReference { 43 public: 44 explicit MainThreadReference(TransferableResourceHolder* holder); 45 MainThreadReference(const MainThreadReference&) = delete; 46 ~MainThreadReference(); 47 48 MainThreadReference& operator=(const MainThreadReference&) = delete; 49 holder()50 TransferableResourceHolder* holder() { return holder_.get(); } 51 52 private: 53 scoped_refptr<TransferableResourceHolder> holder_; 54 }; 55 56 TransferableResourceHolder(const TransferableResourceHolder&) = delete; 57 TransferableResourceHolder& operator=(const TransferableResourceHolder&) = 58 delete; 59 resource()60 const viz::TransferableResource& resource() const { return resource_; } 61 void Return(const gpu::SyncToken& sync_token, bool is_lost); 62 63 // Gets a viz::ReleaseCallback that can be called from another thread. Note: 64 // the caller must ensure the callback is called. 65 std::unique_ptr<viz::SingleReleaseCallback> GetCallbackForImplThread( 66 scoped_refptr<base::SequencedTaskRunner> main_thread_task_runner); 67 68 protected: 69 friend class TextureLayer; 70 71 // Protected visiblity so only TextureLayer and unit tests can create these. 72 static std::unique_ptr<MainThreadReference> Create( 73 const viz::TransferableResource& resource, 74 std::unique_ptr<viz::SingleReleaseCallback> release_callback); 75 virtual ~TransferableResourceHolder(); 76 77 private: 78 friend class base::RefCountedThreadSafe<TransferableResourceHolder>; 79 friend class MainThreadReference; 80 explicit TransferableResourceHolder( 81 const viz::TransferableResource& resource, 82 std::unique_ptr<viz::SingleReleaseCallback> release_callback); 83 84 void InternalAddRef(); 85 void InternalRelease(); 86 void ReturnAndReleaseOnImplThread( 87 const scoped_refptr<base::SequencedTaskRunner>& main_thread_task_runner, 88 const gpu::SyncToken& sync_token, 89 bool is_lost); 90 91 // These members are only accessed on the main thread, or on the impl thread 92 // during commit where the main thread is blocked. 93 int internal_references_ = 0; 94 #if DCHECK_IS_ON() 95 // The number of derefs posted from the impl thread, and a lock for 96 // accessing it. 97 base::Lock posted_internal_derefs_lock_; 98 int posted_internal_derefs_ = 0; 99 #endif 100 viz::TransferableResource resource_; 101 std::unique_ptr<viz::SingleReleaseCallback> release_callback_; 102 103 // This lock guards the sync_token_ and is_lost_ fields because they can be 104 // accessed on both the impl and main thread. We do this to ensure that the 105 // values of these fields are well-ordered such that the last call to 106 // ReturnAndReleaseOnImplThread() defines their values. 107 base::Lock arguments_lock_; 108 gpu::SyncToken sync_token_; 109 bool is_lost_ = false; 110 base::ThreadChecker main_thread_checker_; 111 }; 112 113 // Used when mailbox names are specified instead of texture IDs. 114 static scoped_refptr<TextureLayer> CreateForMailbox( 115 TextureLayerClient* client); 116 117 TextureLayer(const TextureLayer&) = delete; 118 TextureLayer& operator=(const TextureLayer&) = delete; 119 120 // Resets the client, which also resets the texture. 121 void ClearClient(); 122 123 // Resets the texture. 124 void ClearTexture(); 125 126 std::unique_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override; 127 128 // Sets whether this texture should be Y-flipped at draw time. Defaults to 129 // true. 130 void SetFlipped(bool flipped); flipped()131 bool flipped() const { return flipped_; } 132 133 // Sets whether this texture should use nearest neighbor interpolation as 134 // opposed to bilinear. Defaults to false. 135 void SetNearestNeighbor(bool nearest_neighbor); 136 137 // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1). 138 void SetUV(const gfx::PointF& top_left, const gfx::PointF& bottom_right); 139 140 // Sets whether the alpha channel is premultiplied or unpremultiplied. 141 // Defaults to true. 142 void SetPremultipliedAlpha(bool premultiplied_alpha); 143 144 // Sets whether the texture should be blended with the background color 145 // at draw time. Defaults to false. 146 void SetBlendBackgroundColor(bool blend); 147 148 // Sets whether we need to ensure that Texture is opaque before using it. 149 // This will blend texture with black color. Defaults to false. 150 void SetForceTextureToOpaque(bool opaque); 151 152 // Code path for plugins which supply their own mailbox. 153 void SetTransferableResource( 154 const viz::TransferableResource& resource, 155 std::unique_ptr<viz::SingleReleaseCallback> release_callback); 156 157 void SetNeedsDisplayRect(const gfx::Rect& dirty_rect) override; 158 159 void SetLayerTreeHost(LayerTreeHost* layer_tree_host) override; 160 bool Update() override; 161 bool IsSnappedToPixelGridInTarget() override; 162 void PushPropertiesTo(LayerImpl* layer) override; 163 164 // Request a mapping from SharedBitmapId to SharedMemory be registered via the 165 // LayerTreeFrameSink with the display compositor. Once this mapping is 166 // registered, the SharedBitmapId can be used in TransferableResources given 167 // to the TextureLayer for display. The SharedBitmapId registration will end 168 // when the returned SharedBitmapIdRegistration object is destroyed. 169 // Implemented as a SharedBitmapIdRegistrar interface so that clients can 170 // have a limited API access. 171 SharedBitmapIdRegistration RegisterSharedBitmapId( 172 const viz::SharedBitmapId& id, 173 scoped_refptr<CrossThreadSharedBitmap> bitmap) override; 174 current_transferable_resource()175 viz::TransferableResource current_transferable_resource() const { 176 return holder_ref_ ? holder_ref_->holder()->resource() 177 : viz::TransferableResource(); 178 } 179 180 protected: 181 explicit TextureLayer(TextureLayerClient* client); 182 ~TextureLayer() override; 183 bool HasDrawableContent() const override; 184 185 private: 186 void SetTransferableResourceInternal( 187 const viz::TransferableResource& resource, 188 std::unique_ptr<viz::SingleReleaseCallback> release_callback, 189 bool requires_commit); 190 191 // Friends to give access to UnregisterSharedBitmapId(). 192 friend SharedBitmapIdRegistration; 193 // Remove a mapping from SharedBitmapId to SharedMemory in the display 194 // compositor. 195 void UnregisterSharedBitmapId(viz::SharedBitmapId id); 196 197 TextureLayerClient* client_; 198 199 bool flipped_ = true; 200 bool nearest_neighbor_ = false; 201 gfx::PointF uv_top_left_ = gfx::PointF(); 202 gfx::PointF uv_bottom_right_ = gfx::PointF(1.f, 1.f); 203 // [bottom left, top left, top right, bottom right] 204 bool premultiplied_alpha_ = true; 205 bool blend_background_color_ = false; 206 bool force_texture_to_opaque_ = false; 207 208 std::unique_ptr<TransferableResourceHolder::MainThreadReference> holder_ref_; 209 bool needs_set_resource_ = false; 210 211 // The set of SharedBitmapIds to register with the LayerTreeFrameSink on the 212 // compositor thread. These requests are forwarded to the TextureLayerImpl to 213 // use, then stored in |registered_bitmaps_| to re-send if the 214 // TextureLayerImpl object attached to this layer changes, by moving out of 215 // the LayerTreeHost. 216 base::flat_map<viz::SharedBitmapId, scoped_refptr<CrossThreadSharedBitmap>> 217 to_register_bitmaps_; 218 // The set of previously registered SharedBitmapIds for the current 219 // LayerTreeHost. If the LayerTreeHost changes, these must be re-sent to the 220 // (new) TextureLayerImpl to be re-registered. 221 base::flat_map<viz::SharedBitmapId, scoped_refptr<CrossThreadSharedBitmap>> 222 registered_bitmaps_; 223 // The SharedBitmapIds to unregister on the compositor thread, passed to the 224 // TextureLayerImpl. 225 std::vector<viz::SharedBitmapId> to_unregister_bitmap_ids_; 226 227 base::WeakPtrFactory<TextureLayer> weak_ptr_factory_{this}; 228 }; 229 230 } // namespace cc 231 #endif // CC_LAYERS_TEXTURE_LAYER_H_ 232