Skip to content

Commit

Permalink
Enforce invariants based on VASurfaceID.
Browse files Browse the repository at this point in the history
Create a FakeSurface class to keep track of surfaces that have been
created. FakeDriver stores an ObjectTracker<FakeSurface> to keep track
of all created FakeSurfaces.

Ensure that all methods that require valid VASurfaceIDs check if
a FakeSurface class with a corresponding ID exists. Implement
functionality in the fake driver to create and destroy these FakeSurface
objects.

Change-Id: I52fc6d26b85f4ec1bab3db90e8600ee7ffbdd54f
Bug: b:260845551
Test: LIBVA_DRIVERS_PATH=./{buildpath} LIBVA_DRIVER_NAME=libfake ./{buildpath}/fake_libva_driver_unittest
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4060623
Commit-Queue: Grayson LaFleur <graysonlafleur@google.com>
Reviewed-by: Andres Calderon Jaramillo <andrescj@chromium.org>
Reviewed-by: Pilar Molina Lopez <pmolinalopez@google.com>
Reviewed-by: Pilar Molina Lopez <pmolinalopez@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1084513}
  • Loading branch information
Grayson LaFleur authored and Chromium LUCI CQ committed Dec 16, 2022
1 parent 18466ed commit d3915a7
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 3 deletions.
2 changes: 2 additions & 0 deletions media/gpu/vaapi/test/fake_libva_driver/BUILD.gn
Expand Up @@ -21,6 +21,8 @@ source_set("fake_libva_driver_internals") {
"fake_config.h",
"fake_driver.cc",
"fake_driver.h",
"fake_surface.cc",
"fake_surface.h",
"object_tracker.h",
]

Expand Down
22 changes: 21 additions & 1 deletion media/gpu/vaapi/test/fake_libva_driver/fake_driver.cc
Expand Up @@ -28,4 +28,24 @@ void FakeDriver::DestroyConfig(FakeConfig::IdType id) {
config_.DestroyObject(id);
}

} // namespace media::internal
FakeSurface::IdType FakeDriver::CreateSurface(
unsigned int format,
unsigned int width,
unsigned int height,
std::vector<VASurfaceAttrib> attrib_list) {
return surface_.CreateObject(format, width, height, std::move(attrib_list));
}

bool FakeDriver::SurfaceExists(FakeSurface::IdType id) {
return surface_.ObjectExists(id);
}

const FakeSurface& FakeDriver::GetSurface(FakeSurface::IdType id) {
return surface_.GetObject(id);
}

void FakeDriver::DestroySurface(FakeSurface::IdType id) {
surface_.DestroyObject(id);
}

} // namespace media::internal
10 changes: 10 additions & 0 deletions media/gpu/vaapi/test/fake_libva_driver/fake_driver.h
Expand Up @@ -8,6 +8,7 @@
#include <va/va.h>

#include "media/gpu/vaapi/test/fake_libva_driver/fake_config.h"
#include "media/gpu/vaapi/test/fake_libva_driver/fake_surface.h"
#include "media/gpu/vaapi/test/fake_libva_driver/object_tracker.h"

namespace media::internal {
Expand All @@ -29,8 +30,17 @@ class FakeDriver {
const FakeConfig& GetConfig(FakeConfig::IdType id);
void DestroyConfig(FakeConfig::IdType id);

FakeSurface::IdType CreateSurface(unsigned int format,
unsigned int width,
unsigned int height,
std::vector<VASurfaceAttrib> attrib_list);
bool SurfaceExists(FakeSurface::IdType id);
const FakeSurface& GetSurface(FakeSurface::IdType id);
void DestroySurface(FakeSurface::IdType id);

private:
ObjectTracker<FakeConfig> config_;
ObjectTracker<FakeSurface> surface_;
};

} // namespace media::internal
Expand Down
72 changes: 70 additions & 2 deletions media/gpu/vaapi/test/fake_libva_driver/fake_drv_video.cc
Expand Up @@ -350,15 +350,21 @@ VAStatus FakeCreateSurfaces(VADriverContextP ctx,
int format,
int num_surfaces,
VASurfaceID* surfaces) {
for (int index = 0; index < num_surfaces; ++index)
surfaces[index] = index;
CHECK(false);

return VA_STATUS_SUCCESS;
}

VAStatus FakeDestroySurfaces(VADriverContextP ctx,
VASurfaceID* surface_list,
int num_surfaces) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

for (int i = 0; i < num_surfaces; i++) {
fdrv->DestroySurface(surface_list[i]);
}

return VA_STATUS_SUCCESS;
}

Expand All @@ -375,6 +381,10 @@ VAStatus FakeCreateContext(VADriverContextP ctx,

CHECK(fdrv->ConfigExists(config_id));

for (int i = 0; i < num_render_targets; i++) {
CHECK(fdrv->SurfaceExists(render_targets[i]));
}

return VA_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -413,6 +423,11 @@ VAStatus FakeDestroyBuffer(VADriverContextP ctx, VABufferID buffer_id) {
VAStatus FakeBeginPicture(VADriverContextP ctx,
VAContextID context,
VASurfaceID render_target) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(render_target));

return VA_STATUS_SUCCESS;
}

Expand All @@ -428,12 +443,22 @@ VAStatus FakeEndPicture(VADriverContextP ctx, VAContextID context) {
}

VAStatus FakeSyncSurface(VADriverContextP ctx, VASurfaceID render_target) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(render_target));

return VA_STATUS_SUCCESS;
}

VAStatus FakeQuerySurfaceStatus(VADriverContextP ctx,
VASurfaceID render_target,
VASurfaceStatus* status) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(render_target));

return VA_STATUS_SUCCESS;
}

Expand All @@ -451,6 +476,11 @@ VAStatus FakePutSurface(VADriverContextP ctx,
VARectangle* cliprects,
unsigned int number_cliprects,
unsigned int flags) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(surface));

return VA_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -486,6 +516,11 @@ VAStatus FakeGetImage(VADriverContextP ctx,
unsigned int width,
unsigned int height,
VAImageID image) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(surface));

return VA_STATUS_SUCCESS;
}

Expand All @@ -500,12 +535,22 @@ VAStatus FakePutImage(VADriverContextP ctx,
int dest_y,
unsigned int dest_width,
unsigned int dest_height) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(surface));

return VA_STATUS_SUCCESS;
}

VAStatus FakeDeriveImage(VADriverContextP ctx,
VASurfaceID surface,
VAImage* image) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

CHECK(fdrv->SurfaceExists(surface));

return VA_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -561,13 +606,27 @@ VAStatus FakeAssociateSubpicture(VADriverContextP ctx,
uint16_t dest_width,
uint16_t dest_height,
uint32_t flags) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

for (int i = 0; i < num_surfaces; i++) {
CHECK(fdrv->SurfaceExists(target_surfaces[i]));
}

return VA_STATUS_SUCCESS;
}

VAStatus FakeDeassociateSubpicture(VADriverContextP ctx,
VASubpictureID subpicture,
VASurfaceID* target_surfaces,
int num_surfaces) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

for (int i = 0; i < num_surfaces; i++) {
CHECK(fdrv->SurfaceExists(target_surfaces[i]));
}

return VA_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -664,6 +723,15 @@ VAStatus FakeCreateSurfaces2(VADriverContextP ctx,
unsigned int num_surfaces,
VASurfaceAttrib* attrib_list,
unsigned int num_attribs) {
media::internal::FakeDriver* fdrv =
static_cast<media::internal::FakeDriver*>(ctx->pDriverData);

for (unsigned int i = 0; i < num_surfaces; i++) {
surfaces[i] = fdrv->CreateSurface(
format, width, height,
std::vector<VASurfaceAttrib>(attrib_list, attrib_list + num_attribs));
}

return VA_STATUS_SUCCESS;
}

Expand Down
41 changes: 41 additions & 0 deletions media/gpu/vaapi/test/fake_libva_driver/fake_surface.cc
@@ -0,0 +1,41 @@
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/gpu/vaapi/test/fake_libva_driver/fake_surface.h"

namespace media::internal {

FakeSurface::FakeSurface(FakeSurface::IdType id,
unsigned int format,
unsigned int width,
unsigned int height,
std::vector<VASurfaceAttrib> attrib_list)
: id_(id),
format_(format),
width_(width),
height_(height),
attrib_list_(std::move(attrib_list)) {}
FakeSurface::~FakeSurface() = default;

FakeSurface::IdType FakeSurface::GetID() const {
return id_;
}

unsigned int FakeSurface::GetFormat() const {
return format_;
}

unsigned int FakeSurface::GetWidth() const {
return width_;
}

unsigned int FakeSurface::GetHeight() const {
return height_;
}

const std::vector<VASurfaceAttrib>& FakeSurface::GetSurfaceAttribs() const {
return attrib_list_;
}

} // namespace media::internal
45 changes: 45 additions & 0 deletions media/gpu/vaapi/test/fake_libva_driver/fake_surface.h
@@ -0,0 +1,45 @@
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_GPU_VAAPI_TEST_FAKE_LIBVA_DRIVER_FAKE_SURFACE_H_
#define MEDIA_GPU_VAAPI_TEST_FAKE_LIBVA_DRIVER_FAKE_SURFACE_H_

#include <va/va.h>

#include <vector>

namespace media::internal {

// Class used for tracking a VASurface and all information relevant to it.
// All objects of this class are immutable and thread safe.
class FakeSurface {
public:
using IdType = VASurfaceID;

FakeSurface(IdType id,
unsigned int format,
unsigned int width,
unsigned int height,
std::vector<VASurfaceAttrib> attrib_list);
FakeSurface(const FakeSurface&) = delete;
FakeSurface& operator=(const FakeSurface&) = delete;
~FakeSurface();

IdType GetID() const;
unsigned int GetFormat() const;
unsigned int GetWidth() const;
unsigned int GetHeight() const;
const std::vector<VASurfaceAttrib>& GetSurfaceAttribs() const;

private:
const IdType id_;
const unsigned int format_;
const unsigned int width_;
const unsigned int height_;
const std::vector<VASurfaceAttrib> attrib_list_;
};

} // namespace media::internal

#endif // MEDIA_GPU_VAAPI_TEST_FAKE_LIBVA_DRIVER_FAKE_SURFACE_H_

0 comments on commit d3915a7

Please sign in to comment.