From: Luke Esau <lukesau@gmail.com>
Date: Wed, 21 Oct 2026 16:06:18 -0700
Subject: vaExportSurfaceHandle: allocate the surface storage if it does not exist yet
Forwarded: not-needed
Comment: Upstream repository (github.com/intel/intel-vaapi-driver) is archived
 and read-only, so this cannot be forwarded.

The buffer object backing a surface is allocated lazily, on first use.
Chromium's VA-API decoder exports surface handles right after
vaCreateSurfaces(), so vaExportSurfaceHandle() returned
VA_STATUS_ERROR_INVALID_SURFACE and Chromium silently fell back to
software decoding on every i965 GPU (Sandy Bridge .. Cannon Lake).

Allocate the storage on export for 8-bit 4:2:0 surfaces, as NV12 and
tiled where the hardware supports it, which is exactly what the decoders
allocate in i965_decoder_utils.c, so the later decode finds a matching
buffer.  Other render-target formats keep the previous behavior.

Tested on Sandy Bridge (HD 3000) with Google Chrome 156: hardware
decode of 1080p H.264 works, CPU use drops from ~14 to ~5 CPU-seconds
per 15 s of playback, output verified correct.  Firefox (which exports
after decoding) is unaffected.
---
--- a/src/i965_drv_video.c
+++ b/src/i965_drv_video.c
@@ -6802,9 +6802,39 @@
     int composite_object =
         flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS;
 
-    if (!obj_surface || !obj_surface->bo)
+    if (!obj_surface)
         return VA_STATUS_ERROR_INVALID_SURFACE;
 
+    if (!obj_surface->bo) {
+        /*
+         * The buffer object backing a surface is allocated lazily, on the
+         * first vaBeginPicture() or vaDeriveImage() that touches it.
+         * Chromium exports its decode surfaces right after
+         * vaCreateSurfaces(), before any decode, which used to fail here
+         * and made it fall back to software decoding.  For 8-bit 4:2:0
+         * surfaces the layout is unambiguous (NV12, tiled where the
+         * hardware supports it, the same choice the decoders make in
+         * i965_decoder_utils.c), so allocate the storage now.  Other
+         * render-target formats keep the previous behavior.
+         */
+        VAStatus va_status;
+
+        if (obj_surface->expected_format != VA_RT_FORMAT_YUV420) {
+            i965_log_info(ctx, "vaExportSurfaceHandle: surface %08x has no "
+                          "storage yet and render-target format %08x is "
+                          "not handled.\n",
+                          surface_id, obj_surface->expected_format);
+            return VA_STATUS_ERROR_INVALID_SURFACE;
+        }
+
+        va_status = i965_check_alloc_surface_bo(ctx, obj_surface,
+                                                HAS_TILED_SURFACE(i965),
+                                                VA_FOURCC_NV12,
+                                                SUBSAMPLE_YUV420);
+        if (va_status != VA_STATUS_SUCCESS)
+            return va_status;
+    }
+
     if (mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2) {
         i965_log_info(ctx, "vaExportSurfaceHandle: memory type %08x "
                       "is not supported.\n", mem_type);
