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

gpu / command_buffer / service / shared_image / gl_texture_image_backing_factory.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/shared_image/gl_texture_image_backing_factory.h"

#include <list>
#include <utility>

#include "build/build_config.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/gl_texture_image_backing.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/config/gpu_preferences.h"
#include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/progress_reporter.h"

namespace gpu {
namespace {

// Serves as reverse-killswitch for rolling out elimination of SCANOUT support.
// TODO(crbug.com/330865436): Eliminate post safe-rollout.
BASE_FEATURE(kSupportScanoutInGLTextureImageBacking,
             "SupportScanoutInGLTextureImageBacking",
             base::FEATURE_DISABLED_BY_DEFAULT);

// Determines whether to support SCANOUT.
// TODO(crbug.com/330865436): Eliminate once killswitches checked within this
// function roll out safely.
bool SupportScanout() {
  // If any of the below clients are not guarding their addition of SCANOUT
  // usage by SCANOUT support being present in SharedImageCapabilities, then
  // GLTextureImageBacking *must* accept SCANOUT usage for this use case.
  if (!base::FeatureList::IsEnabled(
          features::
              kCameraVideoFrameHandlerAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }
  if (!base::FeatureList::IsEnabled(
          features::kExoBufferAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }
  if (!base::FeatureList::IsEnabled(
          features::kFastInkHostAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }
  if (!base::FeatureList::IsEnabled(
          features::
              kRoundedDisplayAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }
  if (!base::FeatureList::IsEnabled(
          features::kSWVideoFrameAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }
  if (!base::FeatureList::IsEnabled(
          features::kViewTreeHostAddScanoutUsageOnlyIfSupportedBySharedImage)) {
    return true;
  }

#if BUILDFLAG(IS_OZONE)
  // If SharedImageCapabilities is computing SCANOUT support on Ozone via the
  // legacy (and too generous) native pixmaps being supported rather than by
  // overlays being supported, GLTextureImageBacking also must accept SCANOUT
  // usage as the above clients will pass SCANOUT even if they are guarding
  // adding SCANOUT usage by support being present in SharedImageCapabilities.
  if (!base::FeatureList::IsEnabled(
          features::kSharedImageSupportScanoutOnOzoneOnlyIfOverlaysSupported)) {
    return true;
  }
#endif

  return base::FeatureList::IsEnabled(kSupportScanoutInGLTextureImageBacking);
}

constexpr SharedImageUsageSet kWebGPUUsages =
    SHARED_IMAGE_USAGE_WEBGPU_READ | SHARED_IMAGE_USAGE_WEBGPU_WRITE |
    SHARED_IMAGE_USAGE_WEBGPU_SWAP_CHAIN_TEXTURE |
    SHARED_IMAGE_USAGE_WEBGPU_STORAGE_TEXTURE;

constexpr SharedImageUsageSet kSupportedUsage =
    SHARED_IMAGE_USAGE_GLES2_READ | SHARED_IMAGE_USAGE_GLES2_WRITE |
    SHARED_IMAGE_USAGE_GLES2_FOR_RASTER_ONLY |
    SHARED_IMAGE_USAGE_DISPLAY_WRITE | SHARED_IMAGE_USAGE_DISPLAY_READ |
    SHARED_IMAGE_USAGE_RASTER_READ | SHARED_IMAGE_USAGE_RASTER_WRITE |
    SHARED_IMAGE_USAGE_RASTER_OVER_GLES2_ONLY |
    SHARED_IMAGE_USAGE_OOP_RASTERIZATION | SHARED_IMAGE_USAGE_SCANOUT |
    SHARED_IMAGE_USAGE_CONCURRENT_READ_WRITE |
    SHARED_IMAGE_USAGE_HIGH_PERFORMANCE_GPU | SHARED_IMAGE_USAGE_CPU_UPLOAD |
    kWebGPUUsages;
}

///////////////////////////////////////////////////////////////////////////////
// GLTextureImageBackingFactory

GLTextureImageBackingFactory::GLTextureImageBackingFactory(
    const GpuPreferences& gpu_preferences,
    const GpuDriverBugWorkarounds& workarounds,
    const gles2::FeatureInfo* feature_info,
    gl::ProgressReporter* progress_reporter,
    bool supports_cpu_upload)
    : GLCommonImageBackingFactory(kSupportedUsage,
                                  gpu_preferences,
                                  workarounds,
                                  feature_info,
                                  progress_reporter),
      supports_cpu_upload_(supports_cpu_upload),
      support_all_metal_usages_(false) {}

GLTextureImageBackingFactory::~GLTextureImageBackingFactory() = default;

std::unique_ptr<SharedImageBacking>
GLTextureImageBackingFactory::CreateSharedImage(
    const Mailbox& mailbox,
    viz::SharedImageFormat format,
    SurfaceHandle surface_handle,
    const gfx::Size& size,
    const gfx::ColorSpace& color_space,
    GrSurfaceOrigin surface_origin,
    SkAlphaType alpha_type,
    SharedImageUsageSet usage,
    std::string debug_label,
    bool is_thread_safe) {
  CHECK(!is_thread_safe);
  return CreateSharedImageInternal(
      mailbox, format, surface_handle, size, color_space, surface_origin,
      alpha_type, usage, std::move(debug_label), base::span<const uint8_t>());
}

std::unique_ptr<SharedImageBacking>
GLTextureImageBackingFactory::CreateSharedImage(
    const Mailbox& mailbox,
    viz::SharedImageFormat format,
    const gfx::Size& size,
    const gfx::ColorSpace& color_space,
    GrSurfaceOrigin surface_origin,
    SkAlphaType alpha_type,
    SharedImageUsageSet usage,
    std::string debug_label,
    bool is_thread_safe,
    base::span<const uint8_t> pixel_data) {
  CHECK(!is_thread_safe);
  return CreateSharedImageInternal(mailbox, format, kNullSurfaceHandle, size,
                                   color_space, surface_origin, alpha_type,
                                   usage, std::move(debug_label), pixel_data);
}

std::unique_ptr<SharedImageBacking>
GLTextureImageBackingFactory::CreateSharedImage(
    const Mailbox& mailbox,
    viz::SharedImageFormat format,
    const gfx::Size& size,
    const gfx::ColorSpace& color_space,
    GrSurfaceOrigin surface_origin,
    SkAlphaType alpha_type,
    SharedImageUsageSet usage,
    std::string debug_label,
    gfx::GpuMemoryBufferHandle handle) {
  NOTREACHED();
}

bool GLTextureImageBackingFactory::IsSupported(
    SharedImageUsageSet usage,
    viz::SharedImageFormat format,
    const gfx::Size& size,
    bool thread_safe,
    gfx::GpuMemoryBufferType gmb_type,
    GrContextType gr_context_type,
    base::span<const uint8_t> pixel_data) {
  if (format.is_multi_plane() && !use_passthrough_) {
    // With validating command decoder the clear rect tracking doesn't work with
    // multi-planar textures.
    return false;
  }
  if (!pixel_data.empty() && gr_context_type != GrContextType::kGL) {
    return false;
  }
  if (thread_safe) {
    return false;
  }
  if (gmb_type != gfx::EMPTY_BUFFER) {
    return false;
  }
  if (usage.Has(SHARED_IMAGE_USAGE_SCANOUT) && !SupportScanout()) {
    return false;
  }

  if (usage.Has(SHARED_IMAGE_USAGE_CPU_UPLOAD)) {
    if (!supports_cpu_upload_ ||
        !GLTextureImageBacking::SupportsPixelUploadWithFormat(format)) {
      return false;
    }

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_FUCHSIA)
    // GLTextureImageBacking can't actually support scanout on any platform.
    // Historically GLImageBacking did accept scanout usage for shared memory
    // GpuMemoryBuffers which is still replied upon for the following:
    // - Linux and Chrome OS on X11 have no real scanout support but clients add
    //   the usage.
    // - Windows can upload pixels directly from shared memory to a D3D swap
    //   chain for overlays.
    // TODO(crbug.com/330865436): Eliminate this code once the above
    // unconditional rejection of SCANOUT usage rolls out definitively.
    if (usage.Has(SHARED_IMAGE_USAGE_SCANOUT)) {
      return false;
    }
#endif
  } else {
    // TODO(crbug.com/330865436): Eliminate this code once the above
    // unconditional rejection of SCANOUT usage rolls out definitively.
    if (usage.Has(SHARED_IMAGE_USAGE_SCANOUT)) {
      return false;
    }
  }

  // This is not beneficial on iOS. The main purpose of this is a multi-gpu
  // support.
  if (!support_all_metal_usages_) {
    if ((gl::GetGLImplementation() == gl::kGLImplementationEGLANGLE &&
         gl::GetANGLEImplementation() == gl::ANGLEImplementation::kMetal) ||
        emulate_using_angle_metal_for_testing_) {
      SharedImageUsageSet metal_invalid_usages =
          SHARED_IMAGE_USAGE_DISPLAY_READ;

      // GLES2 usage is in general not allowed, as WebGL might be on a different
      // GPU than raster/composite. However, if the GLES2 usage is for
      // raster-over-GLES2 only, it is by definition on the same GPU as
      // raster/composite and thus allowable.
      if (!usage.Has(SHARED_IMAGE_USAGE_GLES2_FOR_RASTER_ONLY)) {
        metal_invalid_usages = metal_invalid_usages |
                               SHARED_IMAGE_USAGE_GLES2_READ |
                               SHARED_IMAGE_USAGE_GLES2_WRITE;
      }
      if (usage.HasAny(metal_invalid_usages)) {
        return false;
      }
    }
  }

  // Using GLTextureImageBacking for raster/display is only appropriate when
  // running on top of GL. For the case WebGL fallback (GrContextType::kNone)
  // this usages aren't actually relevant but WebGL still adds them so ignore.
  if (gr_context_type != GrContextType::kGL &&
      gr_context_type != GrContextType::kNone) {
    SharedImageUsageSet unsupported_usages =
        SHARED_IMAGE_USAGE_DISPLAY_READ | SHARED_IMAGE_USAGE_DISPLAY_WRITE;

    // Raster usage is in general not allowed, as described above. However, if
    // this SI is being used in the context of raster-over-GLES2 only, then
    // raster is by definition using GL for the SI and thus allowable.
    if (!usage.Has(SHARED_IMAGE_USAGE_RASTER_OVER_GLES2_ONLY)) {
      unsupported_usages = unsupported_usages | SHARED_IMAGE_USAGE_RASTER_READ |
                           SHARED_IMAGE_USAGE_RASTER_WRITE;
    }
    if (usage.HasAny(unsupported_usages)) {
      return false;
    }
  }

  // Only supports WebGPU usages on Dawn's OpenGLES backend.
  if (usage.HasAny(kWebGPUUsages)) {
    if (use_webgpu_adapter_ != WebGPUAdapterName::kOpenGLES ||
        gl::GetGLImplementation() != gl::kGLImplementationEGLANGLE ||
        gl::GetANGLEImplementation() != gl::ANGLEImplementation::kOpenGL) {
      return false;
    }
  }

  return CanCreateTexture(format, size, pixel_data, GL_TEXTURE_2D);
}

void GLTextureImageBackingFactory::EnableSupportForAllMetalUsagesForTesting(
    bool enable) {
  support_all_metal_usages_ = enable;
}

void GLTextureImageBackingFactory::ForceSetUsingANGLEMetalForTesting(
    bool value) {
  emulate_using_angle_metal_for_testing_ = value;
}

std::unique_ptr<SharedImageBacking>
GLTextureImageBackingFactory::CreateSharedImageInternal(
    const Mailbox& mailbox,
    viz::SharedImageFormat format,
    SurfaceHandle surface_handle,
    const gfx::Size& size,
    const gfx::ColorSpace& color_space,
    GrSurfaceOrigin surface_origin,
    SkAlphaType alpha_type,
    SharedImageUsageSet usage,
    std::string debug_label,
    base::span<const uint8_t> pixel_data) {
  DCHECK(CanCreateTexture(format, size, pixel_data, GL_TEXTURE_2D));

  // GLTextureImageBackingFactory supports raster and display usage only for
  // Ganesh-GL, meaning that raster/display write usage implies GL writes
  // within Skia.
  const bool for_framebuffer_attachment = usage.HasAny(
      SHARED_IMAGE_USAGE_GLES2_WRITE | SHARED_IMAGE_USAGE_RASTER_WRITE |
      SHARED_IMAGE_USAGE_DISPLAY_WRITE);
  const bool framebuffer_attachment_angle =
      for_framebuffer_attachment && texture_usage_angle_;

  auto result = std::make_unique<GLTextureImageBacking>(
      mailbox, format, size, color_space, surface_origin, alpha_type, usage,
      std::move(debug_label), use_passthrough_);
  result->InitializeGLTexture(GetFormatInfo(format), pixel_data,
                              progress_reporter_, framebuffer_attachment_angle);

  return std::move(result);
}

SharedImageBackingType GLTextureImageBackingFactory::GetBackingType() {
  return SharedImageBackingType::kGLTexture;
}

}  // namespace gpu