summaryrefslogtreecommitdiffstats
path: root/gfx/tests/gtest/TestTextureCompatibility.cpp
blob: 2e5236614dd3f796cbab77f18d01cade1180df22 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gfxConfig.h"
#include "gfxPlatform.h"
#include "gtest/gtest.h"
#include "MockWidget.h"
#include "mozilla/layers/BasicCompositor.h"
#include "mozilla/layers/Compositor.h"
#include "mozilla/layers/LayersTypes.h"
#include "mozilla/layers/TextureClient.h"
#include "mozilla/layers/TextureHost.h"
#include "mozilla/RefPtr.h"
#include "TestLayers.h"
#include "TextureHelper.h"

using mozilla::gfx::Feature;
using mozilla::gfx::gfxConfig;
using mozilla::layers::BasicCompositor;
using mozilla::layers::Compositor;
using mozilla::layers::CompositorOptions;
using mozilla::layers::ISurfaceAllocator;
using mozilla::layers::LayersBackend;
using mozilla::layers::TestSurfaceAllocator;
using mozilla::layers::TextureClient;
using mozilla::layers::TextureHost;
using mozilla::widget::CompositorWidget;
using mozilla::widget::InProcessCompositorWidget;

/**
 * This function will create the possible TextureClient and TextureHost pairs
 * according to the given backend.
 */
static void CreateTextureWithBackend(
    LayersBackend& aLayersBackend, ISurfaceAllocator* aDeallocator,
    nsTArray<RefPtr<TextureClient>>& aTextureClients,
    nsTArray<RefPtr<TextureHost>>& aTextureHosts) {
  aTextureClients.AppendElement(CreateTextureClientWithBackend(aLayersBackend));

  aTextureClients.AppendElement(
      CreateYCbCrTextureClientWithBackend(aLayersBackend));

  for (uint32_t i = 0; i < aTextureClients.Length(); i++) {
    aTextureHosts.AppendElement(CreateTextureHostWithBackend(
        aTextureClients[i], aDeallocator, aLayersBackend));
  }
}

/**
 * This will return the default list of backends that units test should run
 * against.
 */
static void GetPlatformBackends(nsTArray<LayersBackend>& aBackends) {
  gfxPlatform* platform = gfxPlatform::GetPlatform();
  MOZ_ASSERT(platform);

  platform->GetCompositorBackends(gfxConfig::IsEnabled(Feature::HW_COMPOSITING),
                                  aBackends);

  if (aBackends.IsEmpty()) {
    aBackends.AppendElement(LayersBackend::LAYERS_BASIC);
  }
}

/**
 * This function will return a BasicCompositor to caller.
 */
static already_AddRefed<Compositor> CreateBasicCompositor() {
  RefPtr<Compositor> compositor;
  // Init the platform.
  if (gfxPlatform::GetPlatform()) {
    RefPtr<MockWidget> widget = new MockWidget(256, 256);
    CompositorOptions options;
    RefPtr<CompositorWidget> proxy =
        new InProcessCompositorWidget(options, widget);
    compositor = new BasicCompositor(nullptr, proxy);
  }
  return compositor.forget();
}

/**
 * This function checks if the textures react correctly when setting them to
 * BasicCompositor.
 */
static void CheckCompatibilityWithBasicCompositor(
    LayersBackend aBackends, nsTArray<RefPtr<TextureHost>>& aTextures) {
  RefPtr<Compositor> compositor = CreateBasicCompositor();
  for (uint32_t i = 0; i < aTextures.Length(); i++) {
    if (!aTextures[i]) {
      continue;
    }
    aTextures[i]->SetTextureSourceProvider(compositor);

    // The lock function will fail if the texture is not compatible with
    // BasicCompositor.
    bool lockResult = aTextures[i]->Lock();
    if (aBackends != LayersBackend::LAYERS_BASIC) {
      EXPECT_FALSE(lockResult);
    } else {
      EXPECT_TRUE(lockResult);
    }
    if (lockResult) {
      aTextures[i]->Unlock();
    }
  }
}

TEST(Gfx, TestTextureCompatibility)
{
  nsTArray<LayersBackend> backendHints;
  RefPtr<TestSurfaceAllocator> deallocator = new TestSurfaceAllocator();

  GetPlatformBackends(backendHints);
  for (uint32_t i = 0; i < backendHints.Length(); i++) {
    nsTArray<RefPtr<TextureClient>> textureClients;
    nsTArray<RefPtr<TextureHost>> textureHosts;

    CreateTextureWithBackend(backendHints[i], deallocator, textureClients,
                             textureHosts);
    CheckCompatibilityWithBasicCompositor(backendHints[i], textureHosts);
  }
}