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

media / gpu / android / maybe_render_early_manager_unittest.cc [blame]

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

#include "base/memory/raw_ptr.h"
#include "media/gpu/android/video_frame_factory_impl.h"

#include "media/gpu/android/codec_image.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

using testing::NiceMock;
using testing::Return;

// The dimensions for specifying MockImage behavior.
enum ImageKind { kTextureOwner, kOverlay };
enum Phase { kInCodec, kInFrontBuffer, kInvalidated };
enum Expectation { kRenderToFrontBuffer, kRenderToBackBuffer, kNone };

// A mock image with the same interface as CodecImage.
struct MockImage {
  MockImage(ImageKind kind, Phase phase, Expectation expectation) {
    ON_CALL(*this, is_texture_owner_backed())
        .WillByDefault(Return(kind == kTextureOwner));

    ON_CALL(*this, was_rendered_to_front_buffer())
        .WillByDefault(Return(phase == kInFrontBuffer));

    if (expectation == kRenderToFrontBuffer) {
      EXPECT_CALL(*this, RenderToFrontBuffer())
          .WillOnce(Return(phase != kInvalidated));
    } else {
      EXPECT_CALL(*this, RenderToFrontBuffer()).Times(0);
    }

    if (expectation == kRenderToBackBuffer) {
      EXPECT_CALL(*this, RenderToTextureOwnerBackBuffer())
          .WillOnce(Return(phase != kInvalidated));
    } else {
      EXPECT_CALL(*this, RenderToTextureOwnerBackBuffer()).Times(0);
    }
  }

  MOCK_METHOD0(was_rendered_to_front_buffer, bool());
  MOCK_METHOD0(is_texture_owner_backed, bool());
  MOCK_METHOD0(RenderToFrontBuffer, bool());
  MOCK_METHOD0(RenderToTextureOwnerBackBuffer, bool());
};

class MaybeRenderEarlyTest : public testing::Test {
 public:
  MaybeRenderEarlyTest() = default;
  ~MaybeRenderEarlyTest() override = default;

  void AddImage(ImageKind kind, Phase phase, Expectation expectation) {
    owned_images_.push_back(
        std::make_unique<NiceMock<MockImage>>(kind, phase, expectation));
    images_.push_back(owned_images_.back().get());
  }

  std::vector<std::unique_ptr<NiceMock<MockImage>>> owned_images_;
  std::vector<raw_ptr<MockImage, VectorExperimental>> images_;
};

TEST_F(MaybeRenderEarlyTest, EmptyVector) {
  internal::MaybeRenderEarly(&images_);
}

TEST_F(MaybeRenderEarlyTest, SingleUnrenderedSTImageIsRendered) {
  AddImage(kTextureOwner, kInCodec, Expectation::kRenderToFrontBuffer);
  internal::MaybeRenderEarly(&images_);
}

TEST_F(MaybeRenderEarlyTest, SingleUnrenderedOverlayImageIsRendered) {
  AddImage(kOverlay, kInCodec, Expectation::kRenderToFrontBuffer);
  internal::MaybeRenderEarly(&images_);
}

TEST_F(MaybeRenderEarlyTest, InvalidatedImagesAreSkippedOver) {
  AddImage(kTextureOwner, kInvalidated, Expectation::kRenderToFrontBuffer);
  AddImage(kTextureOwner, kInvalidated, Expectation::kRenderToFrontBuffer);
  AddImage(kTextureOwner, kInCodec, Expectation::kRenderToFrontBuffer);
  internal::MaybeRenderEarly(&images_);
}

// This also serves as a test that Overlay images are not back buffer rendered.
TEST_F(MaybeRenderEarlyTest, NoFrontBufferRenderingIfAlreadyPopulated) {
  AddImage(kOverlay, kInFrontBuffer, Expectation::kNone);
  AddImage(kOverlay, kInCodec, Expectation::kNone);
  internal::MaybeRenderEarly(&images_);
}

TEST_F(MaybeRenderEarlyTest,
       ImageFollowingLatestFrontBufferIsBackBufferRendered) {
  AddImage(kTextureOwner, kInCodec, Expectation::kNone);
  AddImage(kTextureOwner, kInFrontBuffer, Expectation::kNone);
  AddImage(kTextureOwner, kInCodec, Expectation::kRenderToBackBuffer);
  AddImage(kTextureOwner, kInCodec, Expectation::kNone);
  internal::MaybeRenderEarly(&images_);
}

}  // namespace media