/src/mozilla-central/gfx/tests/gtest/TextureHelper.h
Line | Count | Source (jump to first uncovered line) |
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 <vector> |
8 | | |
9 | | #include "gfxImageSurface.h" |
10 | | #include "gfxPlatform.h" |
11 | | #include "mozilla/layers/BufferTexture.h" |
12 | | #include "mozilla/layers/LayersTypes.h" |
13 | | #include "mozilla/layers/TextureClient.h" |
14 | | #include "mozilla/layers/TextureHost.h" |
15 | | #include "mozilla/RefPtr.h" |
16 | | #ifdef XP_WIN |
17 | | #include "IMFYCbCrImage.h" |
18 | | #include "mozilla/gfx/DeviceManagerDx.h" |
19 | | #include "mozilla/layers/D3D11YCbCrImage.h" |
20 | | #include "mozilla/layers/TextureD3D11.h" |
21 | | #include "mozilla/layers/TextureDIB.h" |
22 | | #endif |
23 | | |
24 | | using mozilla::gfx::SurfaceFormat; |
25 | | |
26 | | namespace mozilla { |
27 | | namespace layers { |
28 | | |
29 | | /** |
30 | | * Create a YCbCrTextureClient according to the given backend. |
31 | | */ |
32 | | static already_AddRefed<TextureClient> |
33 | | CreateYCbCrTextureClientWithBackend(LayersBackend aLayersBackend) |
34 | 0 | { |
35 | 0 |
|
36 | 0 | TextureData* data = nullptr; |
37 | 0 | IntSize size = IntSize(200, 150); |
38 | 0 | IntSize ySize = IntSize(400, 300); |
39 | 0 |
|
40 | 0 | RefPtr<gfxImageSurface> ySurface = |
41 | 0 | new gfxImageSurface(ySize, SurfaceFormat::A8); |
42 | 0 | RefPtr<gfxImageSurface> cbSurface = |
43 | 0 | new gfxImageSurface(size, SurfaceFormat::A8); |
44 | 0 | RefPtr<gfxImageSurface> crSurface = |
45 | 0 | new gfxImageSurface(size, SurfaceFormat::A8); |
46 | 0 |
|
47 | 0 | PlanarYCbCrData clientData; |
48 | 0 | clientData.mYChannel = ySurface->Data(); |
49 | 0 | clientData.mCbChannel = cbSurface->Data(); |
50 | 0 | clientData.mCrChannel = crSurface->Data(); |
51 | 0 | clientData.mYSize = ySurface->GetSize(); |
52 | 0 | clientData.mPicSize = ySurface->GetSize(); |
53 | 0 | clientData.mCbCrSize = cbSurface->GetSize(); |
54 | 0 | clientData.mYStride = ySurface->Stride(); |
55 | 0 | clientData.mCbCrStride = cbSurface->Stride(); |
56 | 0 | clientData.mStereoMode = StereoMode::MONO; |
57 | 0 | clientData.mYSkip = 0; |
58 | 0 | clientData.mCbSkip = 0; |
59 | 0 | clientData.mCrSkip = 0; |
60 | 0 | clientData.mCrSkip = 0; |
61 | 0 | clientData.mPicX = 0; |
62 | 0 | clientData.mPicX = 0; |
63 | 0 |
|
64 | 0 | // Create YCbCrTexture for basice backend. |
65 | 0 | if (aLayersBackend == LayersBackend::LAYERS_BASIC) { |
66 | 0 | return TextureClient::CreateForYCbCr(nullptr, |
67 | 0 | clientData.mYSize, |
68 | 0 | clientData.mYStride, |
69 | 0 | clientData.mCbCrSize, |
70 | 0 | clientData.mCbCrStride, |
71 | 0 | StereoMode::MONO, |
72 | 0 | YUVColorSpace::BT601, |
73 | 0 | 8, |
74 | 0 | TextureFlags::DEALLOCATE_CLIENT); |
75 | 0 | } |
76 | 0 | |
77 | | #ifdef XP_WIN |
78 | | RefPtr<ID3D11Device> device = DeviceManagerDx::Get()->GetImageDevice(); |
79 | | |
80 | | if (device && aLayersBackend == LayersBackend::LAYERS_D3D11) { |
81 | | DXGIYCbCrTextureAllocationHelper helper(clientData, TextureFlags::DEFAULT, device); |
82 | | RefPtr<TextureClient> texture = helper.Allocate(nullptr); |
83 | | return texture.forget(); |
84 | | } |
85 | | #endif |
86 | | |
87 | 0 | if (data) { |
88 | 0 | return MakeAndAddRef<TextureClient>(data, TextureFlags::DEALLOCATE_CLIENT, |
89 | 0 | nullptr); |
90 | 0 | } |
91 | 0 | |
92 | 0 | return nullptr; |
93 | 0 | } |
94 | | |
95 | | /** |
96 | | * Create a TextureClient according to the given backend. |
97 | | */ |
98 | | static already_AddRefed<TextureClient> |
99 | | CreateTextureClientWithBackend(LayersBackend aLayersBackend) |
100 | 0 | { |
101 | 0 | TextureData* data = nullptr; |
102 | 0 | SurfaceFormat format = gfxPlatform::GetPlatform()->Optimal2DFormatForContent( |
103 | 0 | gfxContentType::COLOR_ALPHA); |
104 | 0 | BackendType moz2DBackend = |
105 | 0 | gfxPlatform::GetPlatform()->GetContentBackendFor(aLayersBackend); |
106 | 0 | TextureAllocationFlags allocFlags = TextureAllocationFlags::ALLOC_DEFAULT; |
107 | 0 | IntSize size = IntSize(400, 300); |
108 | 0 | TextureFlags textureFlags = TextureFlags::DEALLOCATE_CLIENT; |
109 | 0 |
|
110 | 0 | if (!gfx::Factory::AllowedSurfaceSize(size)) { |
111 | 0 | return nullptr; |
112 | 0 | } |
113 | 0 | |
114 | | #ifdef XP_WIN |
115 | | if (aLayersBackend == LayersBackend::LAYERS_D3D11 && |
116 | | (moz2DBackend == BackendType::DIRECT2D || |
117 | | moz2DBackend == BackendType::DIRECT2D1_1)) { |
118 | | // Create DXGITextureData. |
119 | | data = DXGITextureData::Create(size, format, allocFlags); |
120 | | } else if (!data && format == SurfaceFormat::B8G8R8X8 && |
121 | | moz2DBackend == BackendType::CAIRO) { |
122 | | // Create DIBTextureData. |
123 | | data = DIBTextureData::Create(size, format, nullptr); |
124 | | } |
125 | | #endif |
126 | | |
127 | 0 | if (!data && aLayersBackend == LayersBackend::LAYERS_BASIC) { |
128 | 0 | // Create BufferTextureData. |
129 | 0 | data = BufferTextureData::Create(size, format, moz2DBackend, aLayersBackend, |
130 | 0 | textureFlags, allocFlags, nullptr); |
131 | 0 | } |
132 | 0 |
|
133 | 0 | if (data) { |
134 | 0 | return MakeAndAddRef<TextureClient>(data, textureFlags, nullptr); |
135 | 0 | } |
136 | 0 | |
137 | 0 | return nullptr; |
138 | 0 | } |
139 | | |
140 | | /** |
141 | | * Create a TextureHost according to the given TextureClient. |
142 | | */ |
143 | | already_AddRefed<TextureHost> |
144 | | CreateTextureHostWithBackend(TextureClient* aClient, |
145 | | ISurfaceAllocator* aDeallocator, |
146 | | LayersBackend& aLayersBackend) |
147 | 0 | { |
148 | 0 | if (!aClient) { |
149 | 0 | return nullptr; |
150 | 0 | } |
151 | 0 | |
152 | 0 | // client serialization |
153 | 0 | SurfaceDescriptor descriptor; |
154 | 0 | ReadLockDescriptor readLock = null_t(); |
155 | 0 | RefPtr<TextureHost> textureHost; |
156 | 0 |
|
157 | 0 | aClient->ToSurfaceDescriptor(descriptor); |
158 | 0 |
|
159 | 0 | wr::MaybeExternalImageId id = Nothing(); |
160 | 0 | return TextureHost::Create(descriptor, readLock, aDeallocator, aLayersBackend, |
161 | 0 | aClient->GetFlags(), id); |
162 | 0 | } |
163 | | |
164 | | } // namespace layers |
165 | | } // namespace mozilla |