client_resource_provider.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2017 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 COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_
  5. #define COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/containers/flat_map.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "components/viz/client/viz_client_export.h"
  11. #include "components/viz/common/display/renderer_settings.h"
  12. #include "components/viz/common/resources/release_callback.h"
  13. #include "components/viz/common/resources/resource_id.h"
  14. #include "components/viz/common/resources/resource_settings.h"
  15. #include "components/viz/common/resources/returned_resource.h"
  16. #include "components/viz/common/resources/transferable_resource.h"
  17. #include "third_party/khronos/GLES2/gl2.h"
  18. #include "third_party/skia/include/core/SkRefCnt.h"
  19. #include "third_party/skia/include/core/SkSurface.h"
  20. class SkColorSpace;
  21. namespace gpu {
  22. struct Capabilities;
  23. namespace gles2 {
  24. class GLES2Interface;
  25. }
  26. namespace raster {
  27. class RasterInterface;
  28. }
  29. } // namespace gpu
  30. namespace viz {
  31. class ContextProvider;
  32. class RasterContextProvider;
  33. // This class is used to give an integer name (ResourceId) to a gpu or software
  34. // resource (shipped as a TransferableResource), in order to use that name in
  35. // DrawQuads and give the resource to the viz display compositor. When the
  36. // resource is removed from the ClientResourceProvider, the ReleaseCallback will
  37. // be called once the resource is no longer in use by the display compositor.
  38. //
  39. // This class is not thread-safe and can only be called from the thread it was
  40. // created on (in practice, the impl thread).
  41. class VIZ_CLIENT_EXPORT ClientResourceProvider {
  42. public:
  43. ClientResourceProvider();
  44. ClientResourceProvider(const ClientResourceProvider&) = delete;
  45. ClientResourceProvider& operator=(const ClientResourceProvider&) = delete;
  46. ~ClientResourceProvider();
  47. static gpu::SyncToken GenerateSyncTokenHelper(gpu::gles2::GLES2Interface* gl);
  48. static gpu::SyncToken GenerateSyncTokenHelper(
  49. gpu::raster::RasterInterface* ri);
  50. // Prepares resources to be transfered to the parent, moving them to
  51. // mailboxes and serializing meta-data into TransferableResources.
  52. // Resources are not removed from the ResourceProvider, but are marked as
  53. // "in use".
  54. void PrepareSendToParent(
  55. const std::vector<ResourceId>& resource_ids,
  56. std::vector<TransferableResource>* transferable_resources,
  57. RasterContextProvider* context_provider);
  58. // TODO(sergeyu): Remove after updating all callers to use the above version
  59. // of this method.
  60. void PrepareSendToParent(
  61. const std::vector<ResourceId>& resource_ids,
  62. std::vector<TransferableResource>* transferable_resources,
  63. ContextProvider* context_provider);
  64. // Receives resources from the parent, moving them from mailboxes. ResourceIds
  65. // passed are in the child namespace.
  66. // NOTE: if the sync_token is set on any TransferableResource, this will
  67. // wait on it.
  68. void ReceiveReturnsFromParent(
  69. std::vector<ReturnedResource> transferable_resources);
  70. // Receives a resource from an external client that can be used in compositor
  71. // frames, via the returned ResourceId.
  72. ResourceId ImportResource(const TransferableResource& resource,
  73. ReleaseCallback release_callback);
  74. // Removes an imported resource, which will call the ReleaseCallback given
  75. // originally, once the resource is no longer in use by any compositor frame.
  76. void RemoveImportedResource(ResourceId resource_id);
  77. // Call this to indicate that the connection to the parent is lost and
  78. // resources previously exported will not be able to be returned. If |lose| is
  79. // true, the resources are also marked as lost, to indicate the state of each
  80. // resource can not be known, and/or they can not be reused.
  81. //
  82. // When a resource is sent to the parent (via PrepareSendToParent) it is put
  83. // into an exported state, preventing it from being released until the parent
  84. // returns the resource. Calling this drops that exported state on all
  85. // resources allowing immediate release of them if they are removed via
  86. // RemoveImportedResource().
  87. void ReleaseAllExportedResources(bool lose);
  88. // Immediately runs the ReleaseCallback for all resources that have been
  89. // previously imported and removed, but not released yet. There should not be
  90. // any imported resources yet when this is called, as they can be removed
  91. // first via RemoveImportedResource(), and potentially avoid being lost.
  92. void ShutdownAndReleaseAllResources();
  93. // Verify that the ResourceId is valid and is known to this class, for debug
  94. // checks.
  95. void ValidateResource(ResourceId id) const;
  96. // Checks whether a resource is in use by a consumer.
  97. bool InUseByConsumer(ResourceId id);
  98. size_t num_resources_for_testing() const;
  99. class VIZ_CLIENT_EXPORT ScopedSkSurface {
  100. public:
  101. ScopedSkSurface(GrDirectContext* gr_context,
  102. const gpu::Capabilities& capabilities,
  103. sk_sp<SkColorSpace> color_space,
  104. GLuint texture_id,
  105. GLenum texture_target,
  106. const gfx::Size& size,
  107. ResourceFormat format,
  108. SkSurfaceProps surface_props,
  109. int msaa_sample_count);
  110. ScopedSkSurface(const ScopedSkSurface&) = delete;
  111. ScopedSkSurface& operator=(const ScopedSkSurface&) = delete;
  112. ~ScopedSkSurface();
  113. SkSurface* surface() const { return surface_.get(); }
  114. private:
  115. sk_sp<SkSurface> surface_;
  116. };
  117. private:
  118. struct ImportedResource;
  119. void PrepareSendToParentInternal(
  120. const std::vector<ResourceId>& export_ids,
  121. std::vector<TransferableResource>* list,
  122. base::OnceCallback<void(std::vector<GLbyte*>* tokens)>
  123. verify_sync_tokens);
  124. THREAD_CHECKER(thread_checker_);
  125. base::flat_map<ResourceId, ImportedResource> imported_resources_;
  126. // The ResourceIds in ClientResourceProvider start from 1 to avoid
  127. // conflicts with id from DisplayResourceProvider.
  128. ResourceIdGenerator id_generator_;
  129. };
  130. } // namespace viz
  131. #endif // COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_