123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- // Copyright 2017 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_
- #define COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_
- #include <memory>
- #include <vector>
- #include "base/containers/flat_map.h"
- #include "base/threading/thread_checker.h"
- #include "components/viz/client/viz_client_export.h"
- #include "components/viz/common/display/renderer_settings.h"
- #include "components/viz/common/resources/release_callback.h"
- #include "components/viz/common/resources/resource_id.h"
- #include "components/viz/common/resources/resource_settings.h"
- #include "components/viz/common/resources/returned_resource.h"
- #include "components/viz/common/resources/transferable_resource.h"
- #include "third_party/khronos/GLES2/gl2.h"
- #include "third_party/skia/include/core/SkRefCnt.h"
- #include "third_party/skia/include/core/SkSurface.h"
- class SkColorSpace;
- namespace gpu {
- struct Capabilities;
- namespace gles2 {
- class GLES2Interface;
- }
- namespace raster {
- class RasterInterface;
- }
- } // namespace gpu
- namespace viz {
- class ContextProvider;
- class RasterContextProvider;
- // This class is used to give an integer name (ResourceId) to a gpu or software
- // resource (shipped as a TransferableResource), in order to use that name in
- // DrawQuads and give the resource to the viz display compositor. When the
- // resource is removed from the ClientResourceProvider, the ReleaseCallback will
- // be called once the resource is no longer in use by the display compositor.
- //
- // This class is not thread-safe and can only be called from the thread it was
- // created on (in practice, the impl thread).
- class VIZ_CLIENT_EXPORT ClientResourceProvider {
- public:
- ClientResourceProvider();
- ClientResourceProvider(const ClientResourceProvider&) = delete;
- ClientResourceProvider& operator=(const ClientResourceProvider&) = delete;
- ~ClientResourceProvider();
- static gpu::SyncToken GenerateSyncTokenHelper(gpu::gles2::GLES2Interface* gl);
- static gpu::SyncToken GenerateSyncTokenHelper(
- gpu::raster::RasterInterface* ri);
- // Prepares resources to be transfered to the parent, moving them to
- // mailboxes and serializing meta-data into TransferableResources.
- // Resources are not removed from the ResourceProvider, but are marked as
- // "in use".
- void PrepareSendToParent(
- const std::vector<ResourceId>& resource_ids,
- std::vector<TransferableResource>* transferable_resources,
- RasterContextProvider* context_provider);
- // TODO(sergeyu): Remove after updating all callers to use the above version
- // of this method.
- void PrepareSendToParent(
- const std::vector<ResourceId>& resource_ids,
- std::vector<TransferableResource>* transferable_resources,
- ContextProvider* context_provider);
- // Receives resources from the parent, moving them from mailboxes. ResourceIds
- // passed are in the child namespace.
- // NOTE: if the sync_token is set on any TransferableResource, this will
- // wait on it.
- void ReceiveReturnsFromParent(
- std::vector<ReturnedResource> transferable_resources);
- // Receives a resource from an external client that can be used in compositor
- // frames, via the returned ResourceId.
- ResourceId ImportResource(const TransferableResource& resource,
- ReleaseCallback release_callback);
- // Removes an imported resource, which will call the ReleaseCallback given
- // originally, once the resource is no longer in use by any compositor frame.
- void RemoveImportedResource(ResourceId resource_id);
- // Call this to indicate that the connection to the parent is lost and
- // resources previously exported will not be able to be returned. If |lose| is
- // true, the resources are also marked as lost, to indicate the state of each
- // resource can not be known, and/or they can not be reused.
- //
- // When a resource is sent to the parent (via PrepareSendToParent) it is put
- // into an exported state, preventing it from being released until the parent
- // returns the resource. Calling this drops that exported state on all
- // resources allowing immediate release of them if they are removed via
- // RemoveImportedResource().
- void ReleaseAllExportedResources(bool lose);
- // Immediately runs the ReleaseCallback for all resources that have been
- // previously imported and removed, but not released yet. There should not be
- // any imported resources yet when this is called, as they can be removed
- // first via RemoveImportedResource(), and potentially avoid being lost.
- void ShutdownAndReleaseAllResources();
- // Verify that the ResourceId is valid and is known to this class, for debug
- // checks.
- void ValidateResource(ResourceId id) const;
- // Checks whether a resource is in use by a consumer.
- bool InUseByConsumer(ResourceId id);
- size_t num_resources_for_testing() const;
- class VIZ_CLIENT_EXPORT ScopedSkSurface {
- public:
- ScopedSkSurface(GrDirectContext* gr_context,
- const gpu::Capabilities& capabilities,
- sk_sp<SkColorSpace> color_space,
- GLuint texture_id,
- GLenum texture_target,
- const gfx::Size& size,
- ResourceFormat format,
- SkSurfaceProps surface_props,
- int msaa_sample_count);
- ScopedSkSurface(const ScopedSkSurface&) = delete;
- ScopedSkSurface& operator=(const ScopedSkSurface&) = delete;
- ~ScopedSkSurface();
- SkSurface* surface() const { return surface_.get(); }
- private:
- sk_sp<SkSurface> surface_;
- };
- private:
- struct ImportedResource;
- void PrepareSendToParentInternal(
- const std::vector<ResourceId>& export_ids,
- std::vector<TransferableResource>* list,
- base::OnceCallback<void(std::vector<GLbyte*>* tokens)>
- verify_sync_tokens);
- THREAD_CHECKER(thread_checker_);
- base::flat_map<ResourceId, ImportedResource> imported_resources_;
- // The ResourceIds in ClientResourceProvider start from 1 to avoid
- // conflicts with id from DisplayResourceProvider.
- ResourceIdGenerator id_generator_;
- };
- } // namespace viz
- #endif // COMPONENTS_VIZ_CLIENT_CLIENT_RESOURCE_PROVIDER_H_
|