[go: nahoru, domu]

Reland "Use SharedImageInterface for gpu and OOP raster"

This wires up SharedImageInterface all the way to cc, and uses it for OOP and GPU raster.
This also adds memory accounting in SharedImageFactory/SharedImageStub.

TBR=boliu@chromium.org,sadrul@chromium.org

Bug: 870116
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: I7cb3837f2082c5a4d03a9bc71e91dc265cbd0e82
Reviewed-on: https://chromium-review.googlesource.com/1242080
Commit-Queue: Antoine Labour <piman@chromium.org>
Reviewed-by: Jonathan Backer <backer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594755}
diff --git a/cc/raster/gpu_raster_buffer_provider.cc b/cc/raster/gpu_raster_buffer_provider.cc
index 3db004cf..a7df553 100644
--- a/cc/raster/gpu_raster_buffer_provider.cc
+++ b/cc/raster/gpu_raster_buffer_provider.cc
@@ -8,8 +8,10 @@
 
 #include <algorithm>
 
+#include "base/format_macros.h"
 #include "base/macros.h"
 #include "base/metrics/histogram_macros.h"
+#include "base/strings/stringprintf.h"
 #include "base/trace_event/process_memory_dump.h"
 #include "base/trace_event/trace_event.h"
 #include "cc/base/histograms.h"
@@ -28,13 +30,15 @@
 #include "gpu/command_buffer/client/context_support.h"
 #include "gpu/command_buffer/client/gles2_interface.h"
 #include "gpu/command_buffer/client/raster_interface.h"
+#include "gpu/command_buffer/client/shared_image_interface.h"
 #include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
+#include "gpu/command_buffer/common/shared_image_trace_utils.h"
+#include "gpu/command_buffer/common/shared_image_usage.h"
 #include "third_party/skia/include/core/SkMultiPictureDraw.h"
 #include "third_party/skia/include/core/SkPictureRecorder.h"
 #include "third_party/skia/include/core/SkSurface.h"
 #include "third_party/skia/include/gpu/GrContext.h"
 #include "ui/gfx/geometry/axis_transform2d.h"
-#include "ui/gl/trace_util.h"
 #include "url/gurl.h"
 
 namespace cc {
@@ -114,10 +118,10 @@
 static void RasterizeSourceOOP(
     const RasterSource* raster_source,
     bool resource_has_previous_content,
-    const gpu::Mailbox& mailbox,
+    gpu::Mailbox* mailbox,
+    const gpu::SyncToken& sync_token,
     GLenum texture_target,
     bool texture_is_overlay_candidate,
-    bool texture_storage_allocated,
     const gfx::Size& resource_size,
     viz::ResourceFormat resource_format,
     const gfx::ColorSpace& color_space,
@@ -128,16 +132,22 @@
     viz::RasterContextProvider* context_provider,
     int msaa_sample_count) {
   gpu::raster::RasterInterface* ri = context_provider->RasterInterface();
+  if (mailbox->IsZero()) {
+    DCHECK(!sync_token.HasData());
+    auto* sii = context_provider->SharedImageInterface();
+    uint32_t flags = gpu::SHARED_IMAGE_USAGE_RASTER;
+    if (texture_is_overlay_candidate)
+      flags |= gpu::SHARED_IMAGE_USAGE_SCANOUT;
+    *mailbox = sii->CreateSharedImage(resource_format, resource_size,
+                                      color_space, flags);
+    ri->WaitSyncTokenCHROMIUM(sii->GenUnverifiedSyncToken().GetConstData());
+  } else {
+    ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
+  }
+
   GLuint texture_id = ri->CreateAndConsumeTexture(
       texture_is_overlay_candidate, gfx::BufferUsage::SCANOUT, resource_format,
-      mailbox.name);
-  if (!texture_storage_allocated) {
-    viz::TextureAllocation alloc = {texture_id, texture_target,
-                                    texture_is_overlay_candidate};
-    viz::TextureAllocation::AllocateStorage(
-        ri, context_provider->ContextCapabilities(), resource_format,
-        resource_size, alloc, color_space);
-  }
+      mailbox->name);
 
   // TODO(enne): Use the |texture_target|? GpuMemoryBuffer backed textures don't
   // use GL_TEXTURE_2D.
@@ -145,7 +155,7 @@
                           playback_settings.use_lcd_text,
                           viz::ResourceFormatToClosestSkColorType(
                               /*gpu_compositing=*/true, resource_format),
-                          playback_settings.raster_color_space, mailbox.name);
+                          playback_settings.raster_color_space, mailbox->name);
   float recording_to_raster_scale =
       transform.scale() / raster_source->recording_scale_factor();
   gfx::Size content_size = raster_source->GetContentSize(transform.scale());
@@ -168,10 +178,10 @@
 static void RasterizeSource(
     const RasterSource* raster_source,
     bool resource_has_previous_content,
-    const gpu::Mailbox& mailbox,
+    gpu::Mailbox* mailbox,
+    const gpu::SyncToken& sync_token,
     GLenum texture_target,
     bool texture_is_overlay_candidate,
-    bool texture_storage_allocated,
     const gfx::Size& resource_size,
     viz::ResourceFormat resource_format,
     const gfx::ColorSpace& color_space,
@@ -184,17 +194,24 @@
     bool unpremultiply_and_dither,
     const gfx::Size& max_tile_size) {
   gpu::raster::RasterInterface* ri = context_provider->RasterInterface();
+  if (mailbox->IsZero()) {
+    auto* sii = context_provider->SharedImageInterface();
+    uint32_t flags = gpu::SHARED_IMAGE_USAGE_GLES2 |
+                     gpu::SHARED_IMAGE_USAGE_GLES2_FRAMEBUFFER_HINT;
+    if (texture_is_overlay_candidate)
+      flags |= gpu::SHARED_IMAGE_USAGE_SCANOUT;
+    *mailbox = sii->CreateSharedImage(resource_format, resource_size,
+                                      color_space, flags);
+    ri->WaitSyncTokenCHROMIUM(sii->GenUnverifiedSyncToken().GetConstData());
+  } else {
+    // Wait on the SyncToken that was created on the compositor thread after
+    // making the mailbox. This ensures that the mailbox we consume here is
+    // valid by the time the consume command executes.
+    ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
+  }
   GLuint texture_id = ri->CreateAndConsumeTexture(
       texture_is_overlay_candidate, gfx::BufferUsage::SCANOUT, resource_format,
-      mailbox.name);
-  if (!texture_storage_allocated) {
-    viz::TextureAllocation alloc = {texture_id, texture_target,
-                                    texture_is_overlay_candidate};
-    viz::TextureAllocation::AllocateStorage(
-        ri, context_provider->ContextCapabilities(), resource_format,
-        resource_size, alloc, color_space);
-  }
-
+      mailbox->name);
   {
     ScopedGrContextAccess gr_context_access(context_provider);
     base::Optional<viz::ClientResourceProvider::ScopedSkSurface> scoped_surface;
@@ -244,13 +261,13 @@
     : public ResourcePool::GpuBacking {
  public:
   ~GpuRasterBacking() override {
-    gpu::gles2::GLES2Interface* gl = compositor_context_provider->ContextGL();
+    if (mailbox.IsZero())
+      return;
+    auto* sii = worker_context_provider->SharedImageInterface();
     if (returned_sync_token.HasData())
-      gl->WaitSyncTokenCHROMIUM(returned_sync_token.GetConstData());
-    if (mailbox_sync_token.HasData())
-      gl->WaitSyncTokenCHROMIUM(mailbox_sync_token.GetConstData());
-    if (texture_id)
-      gl->DeleteTextures(1, &texture_id);
+      sii->DestroySharedImage(returned_sync_token, mailbox);
+    else if (mailbox_sync_token.HasData())
+      sii->DestroySharedImage(mailbox_sync_token, mailbox);
   }
 
   void OnMemoryDump(
@@ -258,23 +275,16 @@
       const base::trace_event::MemoryAllocatorDumpGuid& buffer_dump_guid,
       uint64_t tracing_process_id,
       int importance) const override {
-    if (!storage_allocated)
+    if (mailbox.IsZero())
       return;
 
-    auto texture_tracing_guid = gl::GetGLTextureClientGUIDForTracing(
-        compositor_context_provider->ContextSupport()->ShareGroupTracingGUID(),
-        texture_id);
-    pmd->CreateSharedGlobalAllocatorDump(texture_tracing_guid);
-    pmd->AddOwnershipEdge(buffer_dump_guid, texture_tracing_guid, importance);
+    auto tracing_guid = gpu::GetSharedImageGUIDForTracing(mailbox);
+    pmd->CreateSharedGlobalAllocatorDump(tracing_guid);
+    pmd->AddOwnershipEdge(buffer_dump_guid, tracing_guid, importance);
   }
 
-  // The ContextProvider used to clean up the texture id.
-  viz::ContextProvider* compositor_context_provider = nullptr;
-  // The texture backing of the resource.
-  GLuint texture_id = 0;
-  // The allocation of storage for the |texture_id| is deferred, and this tracks
-  // if it has been done.
-  bool storage_allocated = false;
+  // The ContextProvider used to clean up the mailbox
+  viz::RasterContextProvider* worker_context_provider = nullptr;
 };
 
 GpuRasterBufferProvider::RasterBufferImpl::RasterBufferImpl(
@@ -289,10 +299,9 @@
       color_space_(in_use_resource.color_space()),
       resource_has_previous_content_(resource_has_previous_content),
       before_raster_sync_token_(backing->returned_sync_token),
-      mailbox_(backing->mailbox),
       texture_target_(backing->texture_target),
       texture_is_overlay_candidate_(backing->overlay_candidate),
-      texture_storage_allocated_(backing->storage_allocated) {}
+      mailbox_(backing->mailbox) {}
 
 GpuRasterBufferProvider::RasterBufferImpl::~RasterBufferImpl() {
   // This SyncToken was created on the worker context after rastering the
@@ -303,7 +312,7 @@
     // happened if the |after_raster_sync_token_| was set.
     backing_->returned_sync_token = gpu::SyncToken();
   }
-  backing_->storage_allocated = texture_storage_allocated_;
+  backing_->mailbox = mailbox_;
 }
 
 void GpuRasterBufferProvider::RasterBufferImpl::Playback(
@@ -320,12 +329,10 @@
   // returns another SyncToken generated on the worker thread to synchronize
   // with after the raster is complete.
   after_raster_sync_token_ = client_->PlaybackOnWorkerThread(
-      mailbox_, texture_target_, texture_is_overlay_candidate_,
-      texture_storage_allocated_, before_raster_sync_token_, resource_size_,
-      resource_format_, color_space_, resource_has_previous_content_,
-      raster_source, raster_full_rect, raster_dirty_rect, new_content_id,
-      transform, playback_settings, url);
-  texture_storage_allocated_ = true;
+      &mailbox_, texture_target_, texture_is_overlay_candidate_,
+      before_raster_sync_token_, resource_size_, resource_format_, color_space_,
+      resource_has_previous_content_, raster_source, raster_full_rect,
+      raster_dirty_rect, new_content_id, transform, playback_settings, url);
 }
 
 GpuRasterBufferProvider::GpuRasterBufferProvider(
@@ -359,24 +366,17 @@
     uint64_t previous_content_id) {
   if (!resource.gpu_backing()) {
     auto backing = std::make_unique<GpuRasterBacking>();
-    backing->compositor_context_provider = compositor_context_provider_;
-
-    gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL();
+    backing->worker_context_provider = worker_context_provider_;
     const auto& caps = compositor_context_provider_->ContextCapabilities();
-
-    viz::TextureAllocation alloc = viz::TextureAllocation::MakeTextureId(
-        gl, caps, resource.format(), use_gpu_memory_buffer_resources_,
-        /*for_framebuffer_attachment=*/true);
-    backing->texture_id = alloc.texture_id;
-    backing->texture_target = alloc.texture_target;
-    backing->overlay_candidate = alloc.overlay_candidate;
-    gl->ProduceTextureDirectCHROMIUM(backing->texture_id,
-                                     backing->mailbox.name);
-    // Save a sync token in the backing so that we always wait on it even if
-    // this task is cancelled between being scheduled and running.
-    backing->returned_sync_token =
-        viz::ClientResourceProvider::GenerateSyncTokenHelper(gl);
-
+    backing->overlay_candidate =
+        use_gpu_memory_buffer_resources_ && caps.texture_storage_image &&
+        IsGpuMemoryBufferFormatSupported(resource.format());
+    if (backing->overlay_candidate) {
+      backing->texture_target = gpu::GetBufferTextureTarget(
+          gfx::BufferUsage::SCANOUT, BufferFormat(resource.format()), caps);
+    } else {
+      backing->texture_target = GL_TEXTURE_2D;
+    }
     resource.set_gpu_backing(std::move(backing));
   }
   GpuRasterBacking* backing =
@@ -454,10 +454,9 @@
 }
 
 gpu::SyncToken GpuRasterBufferProvider::PlaybackOnWorkerThread(
-    const gpu::Mailbox& mailbox,
+    gpu::Mailbox* mailbox,
     GLenum texture_target,
     bool texture_is_overlay_candidate,
-    bool texture_storage_allocated,
     const gpu::SyncToken& sync_token,
     const gfx::Size& resource_size,
     viz::ResourceFormat resource_format,
@@ -475,11 +474,6 @@
   gpu::raster::RasterInterface* ri = scoped_context.RasterInterface();
   DCHECK(ri);
 
-  // Wait on the SyncToken that was created on the compositor thread after
-  // making the mailbox. This ensures that the mailbox we consume here is valid
-  // by the time the consume command executes.
-  ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
-
   gfx::Rect playback_rect = raster_full_rect;
   if (resource_has_previous_content) {
     playback_rect.Intersect(raster_dirty_rect);
@@ -503,15 +497,15 @@
 
   if (enable_oop_rasterization_) {
     RasterizeSourceOOP(raster_source, resource_has_previous_content, mailbox,
-                       texture_target, texture_is_overlay_candidate,
-                       texture_storage_allocated, resource_size,
-                       resource_format, color_space, raster_full_rect,
-                       playback_rect, transform, playback_settings,
-                       worker_context_provider_, msaa_sample_count_);
+                       sync_token, texture_target, texture_is_overlay_candidate,
+                       resource_size, resource_format, color_space,
+                       raster_full_rect, playback_rect, transform,
+                       playback_settings, worker_context_provider_,
+                       msaa_sample_count_);
   } else {
     RasterizeSource(
-        raster_source, resource_has_previous_content, mailbox, texture_target,
-        texture_is_overlay_candidate, texture_storage_allocated, resource_size,
+        raster_source, resource_has_previous_content, mailbox, sync_token,
+        texture_target, texture_is_overlay_candidate, resource_size,
         resource_format, color_space, raster_full_rect, playback_rect,
         transform, playback_settings, worker_context_provider_,
         msaa_sample_count_,