1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/check_op.h"
#include "base/dcheck_is_on.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/common/resources/release_callback.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/common/resources/transferable_resource.h"
#include "gpu/command_buffer/client/client_shared_image.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/command_buffer/client/shared_image_interface.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "skia/buildflags.h"
#include "third_party/blink/public/platform/web_graphics_shared_image_interface_provider.h"
#include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_wrapper.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/geometry/size.h"
class GrBackendTexture;
class SkSurface;
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_H_
namespace gfx {
class ColorSpace;
} // namespace gfx
namespace gpu::raster {
class RasterInterface;
} // namespace gpu::raster
namespace blink {
class CanvasResourceProvider;
class StaticBitmapImage;
// Generic resource interface, used for locking (RAII) and recycling pixel
// buffers of any type.
// Note that this object may be accessed across multiple threads but not
// concurrently. The caller is responsible to call Transfer on the object before
// using it on a different thread.
class PLATFORM_EXPORT CanvasResource
: public WTF::ThreadSafeRefCounted<CanvasResource> {
public:
using ReleaseCallback = base::OnceCallback<void(
scoped_refptr<blink::CanvasResource>&& canvas_resource,
const gpu::SyncToken& sync_token,
bool is_lost)>;
using LastUnrefCallback = base::OnceCallback<void(
scoped_refptr<blink::CanvasResource> canvas_resource)>;
virtual ~CanvasResource();
static void OnPlaceholderReleasedResource(
scoped_refptr<CanvasResource> resource);
// Returns true if this instance creates TransferableResources for usage with
// GPU compositing.
virtual bool CreatesAcceleratedTransferableResources() const = 0;
virtual void OnRefReturned(scoped_refptr<CanvasResource>&& resource) {}
// Returns true if the resource is still usable. It maybe not be valid in the
// case of a context loss or if we fail to initialize the memory backing for
// the resource.
virtual bool IsValid() const = 0;
// The bounds for this resource.
gfx::Size Size() const { return GetClientSharedImage()->size(); }
const gfx::ColorSpace& GetColorSpace() const { return color_space_; }
SkAlphaType GetAlphaType() const { return alpha_type_; }
// The ClientSharedImage containing information on the SharedImage
// attached to the resource.
virtual const scoped_refptr<gpu::ClientSharedImage>& GetClientSharedImage()
const = 0;
// A CanvasResource is not thread-safe and does not allow concurrent usage
// from multiple threads. But it maybe used from any thread. It remains bound
// to the current thread until Transfer is called. Note that while the
// resource maybe used for reads on any thread, it can be written to only on
// the thread where it was created.
virtual void Transfer() {}
// Returns the sync token to indicate when all writes to the current resource
// are finished on the GPU thread. Note that the token is not guaranteed to be
// verified at the time of calling this method.
const gpu::SyncToken GetSyncToken() {
return GetSyncTokenWithOptionalVerification(false);
}
// Provides a TransferableResource representation of this resource to share it
// with the compositor.
bool PrepareTransferableResource(viz::TransferableResource*,
ReleaseCallback*,
bool needs_verified_synctoken);
// Issues a wait for this sync token on the context used by this resource for
// rendering.
void WaitSyncToken(const gpu::SyncToken&);
bool OriginClean() const { return is_origin_clean_; }
void SetOriginClean(bool flag) { is_origin_clean_ = flag; }
// Provides a StaticBitmapImage wrapping this resource. Commonly used for
// snapshots not used in compositing (for instance to draw to another canvas).
virtual scoped_refptr<StaticBitmapImage> Bitmap() = 0;
// Called when the resource is marked lost. Losing a resource does not mean
// that the backing memory has been destroyed, since the resource itself keeps
// a ref on that memory.
// It means that the consumer (commonly the compositor) can not provide a sync
// token for the resource to be safely recycled and its the GL state may be
// inconsistent with when the resource was given to the compositor. So it
// should not be recycled for writing again but can be safely read from.
virtual void NotifyResourceLost() = 0;
SkImageInfo CreateSkImageInfo() const;
bool is_cross_thread() const {
return base::PlatformThread::CurrentRef() != owning_thread_ref_;
}
protected:
CanvasResource(base::WeakPtr<CanvasResourceProvider>,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space);
virtual gfx::HDRMetadata GetHDRMetadata() const { return gfx::HDRMetadata(); }
virtual viz::TransferableResource::ResourceSource
GetTransferableResourceSource() const {
return viz::TransferableResource::ResourceSource::kCanvas;
}
gpu::InterfaceBase* InterfaceBase() const;
gpu::gles2::GLES2Interface* ContextGL() const;
gpu::raster::RasterInterface* RasterInterface() const;
gpu::webgpu::WebGPUInterface* WebGPUInterface() const;
virtual base::WeakPtr<WebGraphicsContext3DProviderWrapper>
ContextProviderWrapper() const = 0;
CanvasResourceProvider* Provider() { return provider_.get(); }
base::WeakPtr<CanvasResourceProvider> WeakProvider() { return provider_; }
const base::PlatformThreadRef owning_thread_ref_;
const scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner_;
private:
static void OnPlaceholderReleasedResourceOnOwningThread(
scoped_refptr<CanvasResource> resource);
// Returns true if the resource is rastered via the GPU.
virtual bool UsesAcceleratedRaster() const = 0;
// Returns the sync token to indicate when all writes to the current resource
// are finished on the GPU thread. Note that in some subclasses the token is
// not guaranteed to be verified at the time of calling this method. Passing
// true for `needs_verified_token` ensures that the returned token will be
// verified.
virtual const gpu::SyncToken GetSyncTokenWithOptionalVerification(
bool needs_verified_token) {
NOTREACHED();
}
base::WeakPtr<CanvasResourceProvider> provider_;
SkAlphaType alpha_type_;
gfx::ColorSpace color_space_;
bool is_origin_clean_ = true;
};
// Resource type for SharedImage
class PLATFORM_EXPORT CanvasResourceSharedImage final : public CanvasResource {
public:
static scoped_refptr<CanvasResourceSharedImage> CreateSoftware(
gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<CanvasResourceProvider>,
base::WeakPtr<WebGraphicsSharedImageInterfaceProvider>);
static scoped_refptr<CanvasResourceSharedImage> Create(
gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>,
bool is_accelerated,
gpu::SharedImageUsageSet shared_image_usage_flags);
~CanvasResourceSharedImage() override;
bool CreatesAcceleratedTransferableResources() const override {
return !GetClientSharedImage()->is_software();
}
void OnRefReturned(scoped_refptr<CanvasResource>&& resource) final;
bool IsValid() const final;
scoped_refptr<StaticBitmapImage> Bitmap() final;
void Transfer() final;
void NotifyResourceLost() final;
void BeginWriteAccess();
void EndWriteAccess();
GrBackendTexture CreateGrTexture() const;
GLuint GetTextureIdForReadAccess() const {
return owning_thread_data().texture_id_for_read_access;
}
GLuint GetTextureIdForWriteAccess() const {
return owning_thread_data().texture_id_for_write_access;
}
void WillDraw();
bool IsLost() const { return owning_thread_data().is_lost; }
const scoped_refptr<gpu::ClientSharedImage>& GetClientSharedImage()
const override;
void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
const std::string& parent_path) const;
// Signals that an external write has completed, passing the token that should
// be waited on to ensure that the service-side operations of the external
// write have completed. Ensures that the next read of this resource (whether
// via raster or the compositor) waits on this token.
void EndExternalWrite(const gpu::SyncToken& external_write_sync_token);
// Uploads the contents of |sk_surface| to the resource's backing memory.
// Should be called only if the resource is using software raster.
void UploadSoftwareRenderingResults(SkSurface* sk_surface);
private:
// These members are either only accessed on the owning thread, or are only
// updated on the owning thread and then are read on a different thread.
// We ensure to correctly update their state in Transfer, which is called
// before a resource is used on a different thread.
struct OwningThreadData {
bool mailbox_needs_new_sync_token = true;
scoped_refptr<gpu::ClientSharedImage> client_shared_image;
gpu::SyncToken sync_token;
size_t bitmap_image_read_refs = 0u;
bool is_lost = false;
// We need to create 2 representations if canvas is operating in single
// buffered mode to allow concurrent scopes for read and write access,
// because the Begin/EndSharedImageAccessDirectCHROMIUM APIs allow only one
// active access mode for a representation.
// In non single buffered mode, the 2 texture ids are the same.
GLuint texture_id_for_read_access = 0u;
GLuint texture_id_for_write_access = 0u;
};
static void OnBitmapImageDestroyed(
scoped_refptr<CanvasResourceSharedImage> resource,
bool has_read_ref_on_texture,
const gpu::SyncToken& sync_token,
bool is_lost);
base::WeakPtr<WebGraphicsContext3DProviderWrapper> ContextProviderWrapper()
const override;
const gpu::SyncToken GetSyncTokenWithOptionalVerification(
bool needs_verified_token) override;
bool UsesAcceleratedRaster() const final { return is_accelerated_; }
CanvasResourceSharedImage(
gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<CanvasResourceProvider>,
base::WeakPtr<WebGraphicsSharedImageInterfaceProvider>);
CanvasResourceSharedImage(gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>,
bool is_accelerated,
gpu::SharedImageUsageSet shared_image_usage_flags);
OwningThreadData& owning_thread_data() {
DCHECK(!is_cross_thread());
return owning_thread_data_;
}
const OwningThreadData& owning_thread_data() const {
DCHECK(!is_cross_thread());
return owning_thread_data_;
}
// Can be read on any thread.
bool mailbox_needs_new_sync_token() const {
return owning_thread_data_.mailbox_needs_new_sync_token;
}
const gpu::SyncToken& sync_token() const {
return owning_thread_data_.sync_token;
}
// This should only be de-referenced on the owning thread but may be copied
// on a different thread.
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper_;
// Accessed on any thread.
const bool is_accelerated_;
const bool use_oop_rasterization_;
OwningThreadData owning_thread_data_;
};
// Resource type for a given opaque external resource described on construction
// via a TransferableResource. This CanvasResource should only be used with
// context that support GL.
class PLATFORM_EXPORT ExternalCanvasResource final : public CanvasResource {
public:
static scoped_refptr<ExternalCanvasResource> Create(
scoped_refptr<gpu::ClientSharedImage> client_si,
const gpu::SyncToken& sync_token,
viz::TransferableResource::ResourceSource resource_source,
gfx::HDRMetadata hdr_metadata,
viz::ReleaseCallback release_callback,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>);
~ExternalCanvasResource() override;
bool IsValid() const override;
bool CreatesAcceleratedTransferableResources() const override { return true; }
void NotifyResourceLost() override { resource_is_lost_ = true; }
const scoped_refptr<gpu::ClientSharedImage>& GetClientSharedImage()
const final {
return client_si_;
}
scoped_refptr<StaticBitmapImage> Bitmap() override;
private:
gfx::HDRMetadata GetHDRMetadata() const final { return hdr_metadata_; }
viz::TransferableResource::ResourceSource GetTransferableResourceSource()
const final {
return resource_source_;
}
bool UsesAcceleratedRaster() const final { return true; }
const gpu::SyncToken GetSyncTokenWithOptionalVerification(
bool needs_verified_token) override;
base::WeakPtr<WebGraphicsContext3DProviderWrapper> ContextProviderWrapper()
const override;
ExternalCanvasResource(
scoped_refptr<gpu::ClientSharedImage> client_si,
const gpu::SyncToken& sync_token,
viz::TransferableResource::ResourceSource resource_source,
gfx::HDRMetadata hdr_metadata,
viz::ReleaseCallback out_callback,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>);
scoped_refptr<gpu::ClientSharedImage> client_si_;
const base::WeakPtr<WebGraphicsContext3DProviderWrapper>
context_provider_wrapper_;
gpu::SyncToken sync_token_;
viz::TransferableResource::ResourceSource resource_source_;
gfx::HDRMetadata hdr_metadata_;
viz::ReleaseCallback release_callback_;
bool resource_is_lost_ = false;
};
class PLATFORM_EXPORT CanvasResourceSwapChain final : public CanvasResource {
public:
// The passed-in WeakPtrs must be non-null.
static scoped_refptr<CanvasResourceSwapChain> Create(
gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>);
~CanvasResourceSwapChain() override;
bool IsValid() const override;
bool CreatesAcceleratedTransferableResources() const override { return true; }
void NotifyResourceLost() override {
// Used for single buffering mode which doesn't need to care about sync
// token synchronization.
}
scoped_refptr<StaticBitmapImage> Bitmap() override;
GLuint GetBackBufferTextureId() const { return back_buffer_texture_id_; }
scoped_refptr<gpu::ClientSharedImage> GetBackBufferClientSharedImage() {
CHECK(back_buffer_shared_image_);
return back_buffer_shared_image_;
}
void PresentSwapChain();
const scoped_refptr<gpu::ClientSharedImage>& GetClientSharedImage()
const override;
private:
bool UsesAcceleratedRaster() const final { return true; }
const gpu::SyncToken GetSyncTokenWithOptionalVerification(
bool needs_verified_token) override;
base::WeakPtr<WebGraphicsContext3DProviderWrapper> ContextProviderWrapper()
const override;
CanvasResourceSwapChain(gfx::Size size,
viz::SharedImageFormat format,
SkAlphaType alpha_type,
const gfx::ColorSpace& color_space,
base::WeakPtr<WebGraphicsContext3DProviderWrapper>,
base::WeakPtr<CanvasResourceProvider>);
const base::WeakPtr<WebGraphicsContext3DProviderWrapper>
context_provider_wrapper_;
scoped_refptr<gpu::ClientSharedImage> front_buffer_shared_image_;
scoped_refptr<gpu::ClientSharedImage> back_buffer_shared_image_;
GLuint back_buffer_texture_id_ = 0u;
gpu::SyncToken sync_token_;
const bool use_oop_rasterization_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_CANVAS_RESOURCE_H_
|