Bumping manifests a=b2g-bump
[gecko.git] / gfx / layers / D3D9SurfaceImage.cpp
blob7e0098ae52275649f130b4c899f37c244aa08f11
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"
7 #include "gfx2DGlue.h"
8 #include "mozilla/layers/TextureD3D9.h"
9 #include "mozilla/gfx/Types.h"
11 namespace mozilla {
12 namespace layers {
15 D3D9SurfaceImage::D3D9SurfaceImage()
16 : Image(nullptr, ImageFormat::D3D9_RGB32_TEXTURE)
17 , mSize(0, 0)
20 D3D9SurfaceImage::~D3D9SurfaceImage() {}
22 HRESULT
23 D3D9SurfaceImage::SetData(const Data& aData)
25 NS_ENSURE_TRUE(aData.mSurface, E_POINTER);
26 HRESULT hr;
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);
37 D3DSURFACE_DESC desc;
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,
42 D3DDEVTYPE_HAL,
43 desc.Format,
44 D3DFMT_X8R8G8B8);
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
49 // device.
50 const nsIntRect& region = aData.mRegion;
51 RefPtr<IDirect3DTexture9> texture;
52 HANDLE shareHandle = nullptr;
53 hr = device->CreateTexture(region.width,
54 region.height,
56 D3DUSAGE_RENDERTARGET,
57 D3DFMT_X8R8G8B8,
58 D3DPOOL_DEFAULT,
59 byRef(texture),
60 &shareHandle);
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
77 // complete.
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);
84 mTexture = texture;
85 mShareHandle = shareHandle;
86 mSize = gfx::IntSize(region.width, region.height);
87 mQuery = query;
89 return S_OK;
92 void
93 D3D9SurfaceImage::EnsureSynchronized()
95 if (!mQuery) {
96 // Not setup, or already synchronized.
97 return;
99 int iterations = 0;
100 while (iterations < 10 && S_FALSE == mQuery->GetData(nullptr, 0, D3DGETDATA_FLUSH)) {
101 Sleep(1);
102 iterations++;
104 mQuery = nullptr;
107 HANDLE
108 D3D9SurfaceImage::GetShareHandle()
110 // Ensure the image has completed its synchronization,
111 // and safe to used by the caller on another device.
112 EnsureSynchronized();
113 return mShareHandle;
116 const D3DSURFACE_DESC&
117 D3D9SurfaceImage::GetDesc() const
119 return mDesc;
122 gfx::IntSize
123 D3D9SurfaceImage::GetSize()
125 return mSize;
128 TextureClient*
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);
146 HRESULT hr;
147 RefPtr<gfx::DataSourceSurface> surface = gfx::Factory::CreateDataSourceSurface(mSize, gfx::SurfaceFormat::B8G8R8X8);
148 if (NS_WARN_IF(!surface)) {
149 return nullptr;
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,
167 mDesc.Height,
168 D3DFMT_X8R8G8B8,
169 D3DPOOL_SYSTEMMEM,
170 byRef(systemMemorySurface),
172 NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
174 hr = device->GetRenderTargetData(textureSurface, systemMemorySurface);
175 NS_ENSURE_TRUE(SUCCEEDED(hr), nullptr);
177 D3DLOCKED_RECT rect;
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();
184 return nullptr;
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,
192 mSize.width * 4);
195 systemMemorySurface->UnlockRect();
196 surface->Unmap();
198 return surface;
201 } /* layers */
202 } /* mozilla */