1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "D3D9SurfaceImage.h"
8 #include "mozilla/layers/TextureD3D9.h"
9 #include "mozilla/gfx/Types.h"
15 D3D9SurfaceImage::D3D9SurfaceImage()
16 : Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE
)
20 D3D9SurfaceImage::~D3D9SurfaceImage() {}
23 D3D9SurfaceImage::SetData(const Data
& aData
)
25 NS_ENSURE_TRUE(aData
.mSurface
, E_POINTER
);
27 RefPtr
<IDirect3DSurface9
> surface
= aData
.mSurface
;
29 RefPtr
<IDirect3DDevice9
> device
;
30 hr
= surface
->GetDevice(byRef(device
));
31 NS_ENSURE_TRUE(SUCCEEDED(hr
), E_FAIL
);
33 RefPtr
<IDirect3D9
> d3d9
;
34 hr
= device
->GetDirect3D(byRef(d3d9
));
35 NS_ENSURE_TRUE(SUCCEEDED(hr
), E_FAIL
);
38 surface
->GetDesc(&desc
);
39 // Ensure we can convert the textures format to RGB conversion
40 // in StretchRect. Fail if we can't.
41 hr
= d3d9
->CheckDeviceFormatConversion(D3DADAPTER_DEFAULT
,
45 NS_ENSURE_TRUE(SUCCEEDED(hr
), hr
);
47 // DXVA surfaces aren't created sharable, so we need to copy the surface
48 // to a sharable texture to that it's accessible to the layer manager's
50 const nsIntRect
& region
= aData
.mRegion
;
51 RefPtr
<IDirect3DTexture9
> texture
;
52 HANDLE shareHandle
= nullptr;
53 hr
= device
->CreateTexture(region
.width
,
56 D3DUSAGE_RENDERTARGET
,
61 NS_ENSURE_TRUE(SUCCEEDED(hr
) && shareHandle
, hr
);
63 // Copy the image onto the texture, preforming YUV -> RGB conversion if necessary.
64 RefPtr
<IDirect3DSurface9
> textureSurface
;
65 hr
= texture
->GetSurfaceLevel(0, byRef(textureSurface
));
66 NS_ENSURE_TRUE(SUCCEEDED(hr
), hr
);
68 // Stash the surface description for later use.
69 textureSurface
->GetDesc(&mDesc
);
71 RECT src
= { region
.x
, region
.y
, region
.x
+region
.width
, region
.y
+region
.height
};
72 hr
= device
->StretchRect(surface
, &src
, textureSurface
, nullptr, D3DTEXF_NONE
);
73 NS_ENSURE_TRUE(SUCCEEDED(hr
), hr
);
75 // Flush the draw command now, so that by the time we come to draw this
76 // image, we're less likely to need to wait for the draw operation to
78 RefPtr
<IDirect3DQuery9
> query
;
79 hr
= device
->CreateQuery(D3DQUERYTYPE_EVENT
, byRef(query
));
80 NS_ENSURE_TRUE(SUCCEEDED(hr
), hr
);
81 hr
= query
->Issue(D3DISSUE_END
);
82 NS_ENSURE_TRUE(SUCCEEDED(hr
), hr
);
85 mShareHandle
= shareHandle
;
86 mSize
= gfx::IntSize(region
.width
, region
.height
);
93 D3D9SurfaceImage::EnsureSynchronized()
96 // Not setup, or already synchronized.
100 while (iterations
< 10 && S_FALSE
== mQuery
->GetData(nullptr, 0, D3DGETDATA_FLUSH
)) {
108 D3D9SurfaceImage::GetShareHandle()
110 // Ensure the image has completed its synchronization,
111 // and safe to used by the caller on another device.
112 EnsureSynchronized();
116 const D3DSURFACE_DESC
&
117 D3D9SurfaceImage::GetDesc() const
123 D3D9SurfaceImage::GetSize()
129 D3D9SurfaceImage::GetTextureClient(CompositableClient
* aClient
)
131 EnsureSynchronized();
132 if (!mTextureClient
) {
133 RefPtr
<SharedTextureClientD3D9
> textureClient
=
134 new SharedTextureClientD3D9(gfx::SurfaceFormat::B8G8R8X8
, TextureFlags::DEFAULT
);
135 textureClient
->InitWith(mTexture
, mShareHandle
, mDesc
);
136 mTextureClient
= textureClient
;
138 return mTextureClient
;
141 TemporaryRef
<gfx::SourceSurface
>
142 D3D9SurfaceImage::GetAsSourceSurface()
144 NS_ENSURE_TRUE(mTexture
, nullptr);
147 RefPtr
<gfx::DataSourceSurface
> surface
= gfx::Factory::CreateDataSourceSurface(mSize
, gfx::SurfaceFormat::B8G8R8X8
);
148 if (NS_WARN_IF(!surface
)) {
152 // Ensure that the texture is ready to be used.
153 EnsureSynchronized();
155 // Readback the texture from GPU memory into system memory, so that
156 // we can copy it into the Cairo image. This is expensive.
157 RefPtr
<IDirect3DSurface9
> textureSurface
;
158 hr
= mTexture
->GetSurfaceLevel(0, byRef(textureSurface
));
159 NS_ENSURE_TRUE(SUCCEEDED(hr
), nullptr);
161 RefPtr
<IDirect3DDevice9
> device
;
162 hr
= mTexture
->GetDevice(byRef(device
));
163 NS_ENSURE_TRUE(SUCCEEDED(hr
), nullptr);
165 RefPtr
<IDirect3DSurface9
> systemMemorySurface
;
166 hr
= device
->CreateOffscreenPlainSurface(mDesc
.Width
,
170 byRef(systemMemorySurface
),
172 NS_ENSURE_TRUE(SUCCEEDED(hr
), nullptr);
174 hr
= device
->GetRenderTargetData(textureSurface
, systemMemorySurface
);
175 NS_ENSURE_TRUE(SUCCEEDED(hr
), nullptr);
178 hr
= systemMemorySurface
->LockRect(&rect
, nullptr, 0);
179 NS_ENSURE_TRUE(SUCCEEDED(hr
), nullptr);
181 gfx::DataSourceSurface::MappedSurface mappedSurface
;
182 if (!surface
->Map(gfx::DataSourceSurface::WRITE
, &mappedSurface
)) {
183 systemMemorySurface
->UnlockRect();
187 const unsigned char* src
= (const unsigned char*)(rect
.pBits
);
188 const unsigned srcPitch
= rect
.Pitch
;
189 for (int y
= 0; y
< mSize
.height
; y
++) {
190 memcpy(mappedSurface
.mData
+ mappedSurface
.mStride
* y
,
191 (unsigned char*)(src
) + srcPitch
* y
,
195 systemMemorySurface
->UnlockRect();