[go: nahoru, domu]

Migrate most uses of SkImage::MakeFromYUVATextures to new signature.

There is one remaining but it requires deeper refactoring to convert.

Bug: skia:10632
Change-Id: I95b8742c50f1af724940c937ac7f95b572e17463
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522979
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Reviewed-by: Khushal <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826296}
diff --git a/cc/paint/oop_pixeltest.cc b/cc/paint/oop_pixeltest.cc
index 8d4f81d..0f602983 100644
--- a/cc/paint/oop_pixeltest.cc
+++ b/cc/paint/oop_pixeltest.cc
@@ -37,9 +37,10 @@
 #include "third_party/khronos/GLES2/gl2ext.h"
 #include "third_party/skia/include/core/SkGraphics.h"
 #include "third_party/skia/include/core/SkSurface.h"
+#include "third_party/skia/include/core/SkYUVAInfo.h"
 #include "third_party/skia/include/gpu/GrBackendSurface.h"
 #include "third_party/skia/include/gpu/GrDirectContext.h"
-#include "third_party/skia/include/gpu/GrTypes.h"
+#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
 #include "ui/gfx/geometry/axis_transform2d.h"
 #include "ui/gfx/geometry/rect_conversions.h"
 #include "ui/gfx/skia_util.h"
@@ -1947,17 +1948,14 @@
   backend_textures[2] = MakeBackendTexture(
       gl, v_mailbox, uv_options.resource_size, GL_LUMINANCE8_EXT);
 
-  SkYUVAIndex yuva_indices[4];
-  yuva_indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
-  yuva_indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
-  yuva_indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
-  yuva_indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kA};
+  SkYUVAInfo yuva_info(
+      {options.resource_size.width(), options.resource_size.height()},
+      SkYUVAInfo::PlanarConfig::kY_U_V_420, kJPEG_Full_SkYUVColorSpace);
+  GrYUVABackendTextures yuva_textures(yuva_info, backend_textures,
+                                      kTopLeft_GrSurfaceOrigin);
 
   auto expected_image = SkImage::MakeFromYUVATextures(
-      gles2_context_provider_->GrContext(), kJPEG_SkYUVColorSpace,
-      backend_textures, yuva_indices,
-      {options.resource_size.width(), options.resource_size.height()},
-      kTopLeft_GrSurfaceOrigin, nullptr);
+      gles2_context_provider_->GrContext(), yuva_textures);
 
   SkBitmap expected_bitmap;
   expected_bitmap.allocN32Pixels(options.resource_size.width(),
@@ -2068,17 +2066,13 @@
   backend_textures[1] =
       MakeBackendTexture(gl, uv_mailbox, uv_options.resource_size, GL_RG8);
 
-  SkYUVAIndex yuva_indices[4];
-  yuva_indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
-  yuva_indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
-  yuva_indices[SkYUVAIndex::kV_Index] = {1, SkColorChannel::kG};
-  yuva_indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kA};
-
-  auto expected_image = SkImage::MakeFromYUVATextures(
-      gles2_context_provider_->GrContext(), kJPEG_SkYUVColorSpace,
-      backend_textures, yuva_indices,
+  SkYUVAInfo yuva_info(
       {options.resource_size.width(), options.resource_size.height()},
-      kTopLeft_GrSurfaceOrigin, nullptr);
+      SkYUVAInfo::PlanarConfig::kY_UV_420, kJPEG_Full_SkYUVColorSpace);
+  GrYUVABackendTextures yuva_textures(yuva_info, backend_textures,
+                                      kTopLeft_GrSurfaceOrigin);
+  auto expected_image = SkImage::MakeFromYUVATextures(
+      gles2_context_provider_->GrContext(), yuva_textures);
 
   SkBitmap expected_bitmap;
   expected_bitmap.allocN32Pixels(options.resource_size.width(),
diff --git a/cc/tiles/gpu_image_decode_cache.cc b/cc/tiles/gpu_image_decode_cache.cc
index 04398dd1..3dda25b 100644
--- a/cc/tiles/gpu_image_decode_cache.cc
+++ b/cc/tiles/gpu_image_decode_cache.cc
@@ -40,9 +40,9 @@
 #include "third_party/skia/include/core/SkData.h"
 #include "third_party/skia/include/core/SkRefCnt.h"
 #include "third_party/skia/include/core/SkSurface.h"
-#include "third_party/skia/include/core/SkYUVAIndex.h"
 #include "third_party/skia/include/gpu/GrBackendSurface.h"
 #include "third_party/skia/include/gpu/GrDirectContext.h"
+#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
 #include "ui/gfx/color_space.h"
 #include "ui/gfx/geometry/size.h"
 #include "ui/gfx/skia_util.h"
@@ -476,6 +476,38 @@
   return plane_size;
 }
 
+// Preserves the division of channels into planes and channel order within
+// planes but removes chroma subsampling. If there is no such config this will
+// return SkYUVAInfo::PlanarConfig::kUnknown.
+SkYUVAInfo::PlanarConfig ConvertPlanarConfigTo444(
+    SkYUVAInfo::PlanarConfig config) {
+  switch (config) {
+    case SkYUVAInfo::PlanarConfig::kY_U_V_444:
+    case SkYUVAInfo::PlanarConfig::kY_U_V_422:
+    case SkYUVAInfo::PlanarConfig::kY_U_V_420:
+    case SkYUVAInfo::PlanarConfig::kY_U_V_440:
+    case SkYUVAInfo::PlanarConfig::kY_U_V_411:
+    case SkYUVAInfo::PlanarConfig::kY_U_V_410:
+      return SkYUVAInfo::PlanarConfig::kY_U_V_444;
+
+    case SkYUVAInfo::PlanarConfig::kYUV_444:
+      return SkYUVAInfo::PlanarConfig::kYUV_444;
+    case SkYUVAInfo::PlanarConfig::kUYV_444:
+      return SkYUVAInfo::PlanarConfig::kUYV_444;
+    case SkYUVAInfo::PlanarConfig::kYUVA_4444:
+      return SkYUVAInfo::PlanarConfig::kYUVA_4444;
+    case SkYUVAInfo::PlanarConfig::kUYVA_4444:
+      return SkYUVAInfo::PlanarConfig::kUYVA_4444;
+
+    // There are planar configs with no 444[4] equivalent, none of which are
+    // used by GpuImageDecoderCache currently. For example, there is kY_UV_420
+    // but no kY_UV_444. Skia could easily have add the equivalents if required.
+    default:
+      NOTREACHED();
+      return SkYUVAInfo::PlanarConfig::kUnknown;
+  }
+}
+
 }  // namespace
 
 // static
@@ -873,6 +905,7 @@
     bool do_hardware_accelerated_decode,
     bool is_yuv_format,
     SkYUVColorSpace yuv_cs,
+    SkYUVAPixmapInfo::PlanarConfig yuv_config,
     SkYUVAPixmapInfo::DataType yuv_dt)
     : paint_image_id(paint_image_id),
       mode(mode),
@@ -891,6 +924,7 @@
   if (is_yuv) {
     DCHECK_LE(yuv_cs, SkYUVColorSpace::kLastEnum_SkYUVColorSpace);
     yuv_color_space = yuv_cs;
+    yuv_planar_config = yuv_config;
     yuv_data_type = yuv_dt;
   }
 }
@@ -2242,6 +2276,7 @@
       uploaded_image = CreateImageFromYUVATexturesInternal(
           uploaded_y_image.get(), uploaded_u_image.get(),
           uploaded_v_image.get(), image_width, image_height,
+          image_data->yuv_planar_config.value(),
           image_data->yuv_color_space.value(), color_space,
           decoded_target_colorspace);
     }
@@ -2437,6 +2472,8 @@
                       mode != DecodedDataMode::kCpu &&
                       !image_larger_than_max_texture;
 
+  SkYUVAInfo::PlanarConfig yuv_planar_config =
+      SkYUVAInfo::PlanarConfig::kUnknown;
   // TODO(crbug.com/910276): Change after alpha support.
   if (is_yuv) {
     if (upload_scale_mip_level > 0) {
@@ -2446,9 +2483,13 @@
           image_info.makeColorType(yuva_pixmap_info.planeInfo(0).colorType())
               .computeMinByteSize();
       DCHECK(!SkImageInfo::ByteSizeOverflowed(y_plane_size.ValueOrDie()));
+      yuv_planar_config =
+          ConvertPlanarConfigTo444(yuva_pixmap_info.yuvaInfo().planarConfig());
+      DCHECK_NE(yuv_planar_config, SkYUVAInfo::PlanarConfig::kUnknown);
       data_size = (3 * y_plane_size).ValueOrDie();
     } else {
       // Original size decode.
+      yuv_planar_config = yuva_pixmap_info.yuvaInfo().planarConfig();
       data_size = yuva_pixmap_info.computeTotalBytes();
       DCHECK(!SkImageInfo::ByteSizeOverflowed(data_size));
     }
@@ -2460,7 +2501,7 @@
       CalculateDesiredFilterQuality(draw_image), upload_scale_mip_level,
       needs_mips, is_bitmap_backed, can_do_hardware_accelerated_decode,
       do_hardware_accelerated_decode, is_yuv, yuva_pixmap_info.yuvColorSpace(),
-      yuva_pixmap_info.dataType()));
+      yuv_planar_config, yuva_pixmap_info.dataType()));
 }
 
 void GpuImageDecodeCache::WillAddCacheEntry(const DrawImage& draw_image) {
@@ -2947,23 +2988,22 @@
     const SkImage* uploaded_v_image,
     const size_t image_width,
     const size_t image_height,
-    const SkYUVColorSpace& yuv_color_space,
+    const SkYUVAInfo::PlanarConfig yuva_planar_config,
+    const SkYUVColorSpace yuv_color_space,
     sk_sp<SkColorSpace> target_color_space,
     sk_sp<SkColorSpace> decoded_color_space) const {
   DCHECK(uploaded_y_image);
   DCHECK(uploaded_u_image);
   DCHECK(uploaded_v_image);
-  GrSurfaceOrigin origin_temp = kTopLeft_GrSurfaceOrigin;
+  SkYUVAInfo yuva_info({image_width, image_height}, yuva_planar_config,
+                       yuv_color_space);
   GrBackendTexture yuv_textures[3]{};
   yuv_textures[0] = uploaded_y_image->getBackendTexture(false);
   yuv_textures[1] = uploaded_u_image->getBackendTexture(false);
   yuv_textures[2] = uploaded_v_image->getBackendTexture(false);
-
-  SkYUVAIndex indices[SkYUVAIndex::kIndexCount];
-  indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
-  indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
-  indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
-  indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kR};
+  GrYUVABackendTextures yuva_backend_textures(yuva_info, yuv_textures,
+                                              kTopLeft_GrSurfaceOrigin);
+  DCHECK(yuva_backend_textures.isValid());
 
   if (target_color_space && SkColorSpace::Equals(target_color_space.get(),
                                                  decoded_color_space.get())) {
@@ -2971,8 +3011,7 @@
   }
 
   sk_sp<SkImage> yuva_image = SkImage::MakeFromYUVATextures(
-      context_->GrContext(), yuv_color_space, yuv_textures, indices,
-      SkISize::Make(image_width, image_height), origin_temp,
+      context_->GrContext(), yuva_backend_textures,
       std::move(decoded_color_space));
   if (target_color_space)
     return yuva_image->makeColorSpace(target_color_space,
@@ -3065,6 +3104,7 @@
         CreateImageFromYUVATexturesInternal(
             image_y_with_mips_owned.get(), image_u_with_mips_owned.get(),
             image_v_with_mips_owned.get(), width, height,
+            image_data->yuv_planar_config.value(),
             image_data->yuv_color_space.value(), color_space,
             upload_color_space);
     // In case of lost context
diff --git a/cc/tiles/gpu_image_decode_cache.h b/cc/tiles/gpu_image_decode_cache.h
index 0fd9aef..0beac7b 100644
--- a/cc/tiles/gpu_image_decode_cache.h
+++ b/cc/tiles/gpu_image_decode_cache.h
@@ -522,6 +522,7 @@
               bool do_hardware_accelerated_decode,
               bool is_yuv_format,
               SkYUVColorSpace yuv_cs,
+              SkYUVAInfo::PlanarConfig yuv_config,
               SkYUVAPixmapInfo::DataType yuv_dt);
 
     bool IsGpuOrTransferCache() const;
@@ -539,6 +540,7 @@
     bool is_yuv;
     bool is_budgeted = false;
     base::Optional<SkYUVColorSpace> yuv_color_space;
+    base::Optional<SkYUVAInfo::PlanarConfig> yuv_planar_config;
     base::Optional<SkYUVAPixmapInfo::DataType> yuv_data_type;
 
     // If true, this image is no longer in our |persistent_cache_| and will be
@@ -640,7 +642,8 @@
       const SkImage* uploaded_v_image,
       const size_t image_width,
       const size_t image_height,
-      const SkYUVColorSpace& yuva_color_space,
+      const SkYUVAInfo::PlanarConfig yuva_planar_config,
+      const SkYUVColorSpace yuva_color_space,
       sk_sp<SkColorSpace> target_color_space,
       sk_sp<SkColorSpace> decoded_color_space) const;
 
diff --git a/gpu/command_buffer/service/raster_decoder.cc b/gpu/command_buffer/service/raster_decoder.cc
index 7450a16..a66b661 100644
--- a/gpu/command_buffer/service/raster_decoder.cc
+++ b/gpu/command_buffer/service/raster_decoder.cc
@@ -66,11 +66,11 @@
 #include "third_party/skia/include/core/SkSurface.h"
 #include "third_party/skia/include/core/SkSurfaceProps.h"
 #include "third_party/skia/include/core/SkTypeface.h"
-#include "third_party/skia/include/core/SkYUVAIndex.h"
+#include "third_party/skia/include/core/SkYUVAInfo.h"
 #include "third_party/skia/include/gpu/GrBackendSemaphore.h"
 #include "third_party/skia/include/gpu/GrBackendSurface.h"
 #include "third_party/skia/include/gpu/GrDirectContext.h"
-#include "third_party/skia/include/gpu/GrTypes.h"
+#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
 #include "ui/gfx/buffer_format_util.h"
 #include "ui/gfx/skia_util.h"
 #include "ui/gl/gl_context.h"
@@ -2737,19 +2737,14 @@
 
     SkISize dest_size =
         SkISize::Make(dest_surface->width(), dest_surface->height());
-
-    std::array<SkYUVAIndex, SkYUVAIndex::kIndexCount> yuva_indices;
-    yuva_indices[SkYUVAIndex::kY_Index] = {0, SkColorChannel::kR};
-    yuva_indices[SkYUVAIndex::kU_Index] = {1, SkColorChannel::kR};
-    if (is_nv12)
-      yuva_indices[SkYUVAIndex::kV_Index] = {1, SkColorChannel::kG};
-    else
-      yuva_indices[SkYUVAIndex::kV_Index] = {2, SkColorChannel::kR};
-    yuva_indices[SkYUVAIndex::kA_Index] = {-1, SkColorChannel::kA};
-
-    auto result_image = SkImage::MakeFromYUVATextures(
-        gr_context(), src_color_space, yuva_textures.data(),
-        yuva_indices.data(), dest_size, kTopLeft_GrSurfaceOrigin, nullptr);
+    SkYUVAInfo::PlanarConfig planar_config =
+        is_nv12 ? SkYUVAInfo::PlanarConfig::kY_UV_420
+                : SkYUVAInfo::PlanarConfig::kY_U_V_420;
+    SkYUVAInfo yuva_info(dest_size, planar_config, src_color_space);
+    GrYUVABackendTextures yuva_backend_textures(yuva_info, yuva_textures.data(),
+                                                kTopLeft_GrSurfaceOrigin);
+    auto result_image =
+        SkImage::MakeFromYUVATextures(gr_context(), yuva_backend_textures);
     if (!result_image) {
       LOCAL_SET_GL_ERROR(
           GL_INVALID_OPERATION, "glConvertYUVMailboxesToRGB",
diff --git a/media/renderers/video_frame_yuv_converter.cc b/media/renderers/video_frame_yuv_converter.cc
index 261552c..f9197b6 100644
--- a/media/renderers/video_frame_yuv_converter.cc
+++ b/media/renderers/video_frame_yuv_converter.cc
@@ -16,8 +16,9 @@
 #include "third_party/skia/include/core/SkImage.h"
 #include "third_party/skia/include/core/SkRefCnt.h"
 #include "third_party/skia/include/core/SkSurface.h"
-#include "third_party/skia/include/core/SkYUVAIndex.h"
+#include "third_party/skia/include/core/SkYUVAInfo.h"
 #include "third_party/skia/include/gpu/GrDirectContext.h"
+#include "third_party/skia/include/gpu/GrYUVABackendTextures.h"
 #include "third_party/skia/include/gpu/gl/GrGLTypes.h"
 
 namespace media {
@@ -39,21 +40,25 @@
     VideoPixelFormat video_format,
     GrBackendTexture* yuv_textures,
     const GrBackendTexture& result_texture) {
-  SkYUVColorSpace color_space = ColorSpaceToSkYUVColorSpace(video_color_space);
-
+  SkYUVAInfo::PlanarConfig planar_config;
   switch (video_format) {
     case PIXEL_FORMAT_NV12:
-      return SkImage::MakeFromNV12TexturesCopyWithExternalBackend(
-          gr_context, color_space, yuv_textures, kTopLeft_GrSurfaceOrigin,
-          result_texture);
+      planar_config = SkYUVAInfo::PlanarConfig::kY_UV_420;
+      break;
     case PIXEL_FORMAT_I420:
-      return SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
-          gr_context, color_space, yuv_textures, kTopLeft_GrSurfaceOrigin,
-          result_texture);
+      planar_config = SkYUVAInfo::PlanarConfig::kY_U_V_420;
+      break;
     default:
       NOTREACHED();
       return nullptr;
   }
+  SkYUVColorSpace color_space = ColorSpaceToSkYUVColorSpace(video_color_space);
+  SkYUVAInfo yuva_info(result_texture.dimensions(), planar_config, color_space);
+  GrYUVABackendTextures yuva_backend_textures(yuva_info, yuv_textures,
+                                              kTopLeft_GrSurfaceOrigin);
+  return SkImage::MakeFromYUVATexturesCopyToExternal(
+      gr_context, yuva_backend_textures, result_texture,
+      kRGBA_8888_SkColorType);
 }
 
 gfx::Size GetVideoYSize(const VideoFrame* video_frame) {
@@ -66,20 +71,6 @@
   return gfx::Size((y_size.width() + 1) / 2, (y_size.height() + 1) / 2);
 }
 
-// Some YUVA factories infer the YUVAIndices. This helper identifies the channel
-// to use for single channel textures.
-SkColorChannel GetSingleChannel(const GrBackendTexture& tex) {
-  switch (tex.getBackendFormat().channelMask()) {
-    case kGray_SkColorChannelFlag:  // Gray can be read as any of kR, kG, kB.
-    case kRed_SkColorChannelFlag:
-      return SkColorChannel::kR;
-    case kAlpha_SkColorChannelFlag:
-      return SkColorChannel::kA;
-    default:  // multiple channels in the texture. Guess kR.
-      return SkColorChannel::kR;
-  }
-}
-
 SkColorType GetCompatibleSurfaceColorType(GrGLenum format) {
   switch (format) {
     case GL_RGBA8:
@@ -124,37 +115,25 @@
                                      sk_sp<SkSurface> surface,
                                      bool flip_y,
                                      bool use_visible_rect) {
-  SkYUVAIndex indices[4];
-
+  SkYUVAInfo::PlanarConfig planar_config;
   switch (video_frame->format()) {
     case PIXEL_FORMAT_NV12:
-      indices[SkYUVAIndex::kY_Index] = {
-          0, GetSingleChannel(yuv_textures[0])};  // the first backend texture
-      indices[SkYUVAIndex::kU_Index] = {
-          1, SkColorChannel::kR};  // the second backend texture
-      indices[SkYUVAIndex::kV_Index] = {1, SkColorChannel::kG};
-      indices[SkYUVAIndex::kA_Index] = {-1,
-                                        SkColorChannel::kA};  // no alpha plane
+      planar_config = SkYUVAInfo::PlanarConfig::kY_UV_420;
       break;
     case PIXEL_FORMAT_I420:
-      indices[SkYUVAIndex::kY_Index] = {
-          0, GetSingleChannel(yuv_textures[0])};  // the first backend texture
-      indices[SkYUVAIndex::kU_Index] = {
-          1, GetSingleChannel(yuv_textures[1])};  // the second backend texture
-      indices[SkYUVAIndex::kV_Index] = {2, GetSingleChannel(yuv_textures[2])};
-      indices[SkYUVAIndex::kA_Index] = {-1,
-                                        SkColorChannel::kA};  // no alpha plane
+      planar_config = SkYUVAInfo::PlanarConfig::kY_U_V_420;
       break;
     default:
       NOTREACHED();
       return false;
   }
-
-  auto image = SkImage::MakeFromYUVATextures(
-      gr_context, ColorSpaceToSkYUVColorSpace(video_frame->ColorSpace()),
-      yuv_textures, indices,
+  SkYUVAInfo yuva_info(
       {video_frame->coded_size().width(), video_frame->coded_size().height()},
-      kTopLeft_GrSurfaceOrigin, SkColorSpace::MakeSRGB());
+      planar_config, ColorSpaceToSkYUVColorSpace(video_frame->ColorSpace()));
+  auto image = SkImage::MakeFromYUVATextures(
+      gr_context,
+      GrYUVABackendTextures(yuva_info, yuv_textures, kTopLeft_GrSurfaceOrigin),
+      SkColorSpace::MakeSRGB());
 
   if (!image) {
     return false;