1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "gfxConfig.h"
8 #include "gfxPlatform.h"
9 #include "gtest/gtest.h"
10 #include "MockWidget.h"
11 #include "mozilla/layers/BasicCompositor.h"
12 #include "mozilla/layers/Compositor.h"
13 #include "mozilla/layers/LayersTypes.h"
14 #include "mozilla/layers/TextureClient.h"
15 #include "mozilla/layers/TextureHost.h"
16 #include "mozilla/RefPtr.h"
17 #include "TestLayers.h"
18 #include "TextureHelper.h"
19
20 using mozilla::gfx::Feature;
21 using mozilla::gfx::gfxConfig;
22 using mozilla::layers::BasicCompositor;
23 using mozilla::layers::Compositor;
24 using mozilla::layers::CompositorOptions;
25 using mozilla::layers::ISurfaceAllocator;
26 using mozilla::layers::LayersBackend;
27 using mozilla::layers::TestSurfaceAllocator;
28 using mozilla::layers::TextureClient;
29 using mozilla::layers::TextureHost;
30 using mozilla::widget::CompositorWidget;
31 using mozilla::widget::InProcessCompositorWidget;
32
33 /**
34 * This function will create the possible TextureClient and TextureHost pairs
35 * according to the given backend.
36 */
CreateTextureWithBackend(LayersBackend & aLayersBackend,ISurfaceAllocator * aDeallocator,nsTArray<RefPtr<TextureClient>> & aTextureClients,nsTArray<RefPtr<TextureHost>> & aTextureHosts)37 static void CreateTextureWithBackend(
38 LayersBackend& aLayersBackend, ISurfaceAllocator* aDeallocator,
39 nsTArray<RefPtr<TextureClient>>& aTextureClients,
40 nsTArray<RefPtr<TextureHost>>& aTextureHosts) {
41 aTextureClients.AppendElement(CreateTextureClientWithBackend(aLayersBackend));
42
43 aTextureClients.AppendElement(
44 CreateYCbCrTextureClientWithBackend(aLayersBackend));
45
46 for (uint32_t i = 0; i < aTextureClients.Length(); i++) {
47 aTextureHosts.AppendElement(CreateTextureHostWithBackend(
48 aTextureClients[i], aDeallocator, aLayersBackend));
49 }
50 }
51
52 /**
53 * This will return the default list of backends that units test should run
54 * against.
55 */
GetPlatformBackends(nsTArray<LayersBackend> & aBackends)56 static void GetPlatformBackends(nsTArray<LayersBackend>& aBackends) {
57 gfxPlatform* platform = gfxPlatform::GetPlatform();
58 MOZ_ASSERT(platform);
59
60 platform->GetCompositorBackends(gfxConfig::IsEnabled(Feature::HW_COMPOSITING),
61 aBackends);
62
63 if (aBackends.IsEmpty()) {
64 aBackends.AppendElement(LayersBackend::LAYERS_BASIC);
65 }
66 }
67
68 /**
69 * This function will return a BasicCompositor to caller.
70 */
CreateBasicCompositor()71 static already_AddRefed<Compositor> CreateBasicCompositor() {
72 RefPtr<Compositor> compositor;
73 // Init the platform.
74 if (gfxPlatform::GetPlatform()) {
75 RefPtr<MockWidget> widget = new MockWidget(256, 256);
76 CompositorOptions options;
77 RefPtr<CompositorWidget> proxy =
78 new InProcessCompositorWidget(options, widget);
79 compositor = new BasicCompositor(nullptr, proxy);
80 }
81 return compositor.forget();
82 }
83
84 /**
85 * This function checks if the textures react correctly when setting them to
86 * BasicCompositor.
87 */
CheckCompatibilityWithBasicCompositor(LayersBackend aBackends,nsTArray<RefPtr<TextureHost>> & aTextures)88 static void CheckCompatibilityWithBasicCompositor(
89 LayersBackend aBackends, nsTArray<RefPtr<TextureHost>>& aTextures) {
90 RefPtr<Compositor> compositor = CreateBasicCompositor();
91 for (uint32_t i = 0; i < aTextures.Length(); i++) {
92 if (!aTextures[i]) {
93 continue;
94 }
95 aTextures[i]->SetTextureSourceProvider(compositor);
96
97 // The lock function will fail if the texture is not compatible with
98 // BasicCompositor.
99 bool lockResult = aTextures[i]->Lock();
100 if (aBackends != LayersBackend::LAYERS_BASIC) {
101 EXPECT_FALSE(lockResult);
102 } else {
103 EXPECT_TRUE(lockResult);
104 }
105 if (lockResult) {
106 aTextures[i]->Unlock();
107 }
108 }
109 }
110
TEST(Gfx,TestTextureCompatibility)111 TEST(Gfx, TestTextureCompatibility)
112 {
113 nsTArray<LayersBackend> backendHints;
114 RefPtr<TestSurfaceAllocator> deallocator = new TestSurfaceAllocator();
115
116 GetPlatformBackends(backendHints);
117 for (uint32_t i = 0; i < backendHints.Length(); i++) {
118 nsTArray<RefPtr<TextureClient>> textureClients;
119 nsTArray<RefPtr<TextureHost>> textureHosts;
120
121 CreateTextureWithBackend(backendHints[i], deallocator, textureClients,
122 textureHosts);
123 CheckCompatibilityWithBasicCompositor(backendHints[i], textureHosts);
124 }
125 }
126