Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/gfx/2D.h" |
michael@0 | 7 | #include "mozilla/gfx/Point.h" |
michael@0 | 8 | #include "mozilla/RefPtr.h" |
michael@0 | 9 | #include "mozilla/layers/PLayerTransaction.h" |
michael@0 | 10 | #include "gfxSharedImageSurface.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "ImageLayerD3D9.h" |
michael@0 | 13 | #include "ThebesLayerD3D9.h" |
michael@0 | 14 | #include "gfxPlatform.h" |
michael@0 | 15 | #include "gfx2DGlue.h" |
michael@0 | 16 | #include "yuv_convert.h" |
michael@0 | 17 | #include "nsIServiceManager.h" |
michael@0 | 18 | #include "nsIConsoleService.h" |
michael@0 | 19 | #include "Nv3DVUtils.h" |
michael@0 | 20 | #include "D3D9SurfaceImage.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { |
michael@0 | 23 | namespace layers { |
michael@0 | 24 | |
michael@0 | 25 | using namespace mozilla::gfx; |
michael@0 | 26 | |
michael@0 | 27 | static inline _D3DFORMAT |
michael@0 | 28 | D3dFormatForSurfaceFormat(SurfaceFormat aFormat) |
michael@0 | 29 | { |
michael@0 | 30 | if (aFormat == SurfaceFormat::A8) { |
michael@0 | 31 | return D3DFMT_A8; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | return D3DFMT_A8R8G8B8; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | static already_AddRefed<IDirect3DTexture9> |
michael@0 | 38 | DataToTexture(IDirect3DDevice9 *aDevice, |
michael@0 | 39 | unsigned char *aData, |
michael@0 | 40 | int aStride, |
michael@0 | 41 | const IntSize &aSize, |
michael@0 | 42 | _D3DFORMAT aFormat) |
michael@0 | 43 | { |
michael@0 | 44 | nsRefPtr<IDirect3DTexture9> texture; |
michael@0 | 45 | nsRefPtr<IDirect3DDevice9Ex> deviceEx; |
michael@0 | 46 | aDevice->QueryInterface(IID_IDirect3DDevice9Ex, |
michael@0 | 47 | (void**)getter_AddRefs(deviceEx)); |
michael@0 | 48 | |
michael@0 | 49 | nsRefPtr<IDirect3DSurface9> surface; |
michael@0 | 50 | D3DLOCKED_RECT lockedRect; |
michael@0 | 51 | if (deviceEx) { |
michael@0 | 52 | // D3D9Ex doesn't support managed textures. We could use dynamic textures |
michael@0 | 53 | // here but since Images are immutable that probably isn't such a great |
michael@0 | 54 | // idea. |
michael@0 | 55 | if (FAILED(aDevice-> |
michael@0 | 56 | CreateTexture(aSize.width, aSize.height, |
michael@0 | 57 | 1, 0, aFormat, D3DPOOL_DEFAULT, |
michael@0 | 58 | getter_AddRefs(texture), nullptr))) |
michael@0 | 59 | { |
michael@0 | 60 | return nullptr; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | nsRefPtr<IDirect3DTexture9> tmpTexture; |
michael@0 | 64 | if (FAILED(aDevice-> |
michael@0 | 65 | CreateTexture(aSize.width, aSize.height, |
michael@0 | 66 | 1, 0, aFormat, D3DPOOL_SYSTEMMEM, |
michael@0 | 67 | getter_AddRefs(tmpTexture), nullptr))) |
michael@0 | 68 | { |
michael@0 | 69 | return nullptr; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | tmpTexture->GetSurfaceLevel(0, getter_AddRefs(surface)); |
michael@0 | 73 | surface->LockRect(&lockedRect, nullptr, 0); |
michael@0 | 74 | NS_ASSERTION(lockedRect.pBits, "Could not lock surface"); |
michael@0 | 75 | } else { |
michael@0 | 76 | if (FAILED(aDevice-> |
michael@0 | 77 | CreateTexture(aSize.width, aSize.height, |
michael@0 | 78 | 1, 0, aFormat, D3DPOOL_MANAGED, |
michael@0 | 79 | getter_AddRefs(texture), nullptr))) |
michael@0 | 80 | { |
michael@0 | 81 | return nullptr; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* lock the entire texture */ |
michael@0 | 85 | texture->LockRect(0, &lockedRect, nullptr, 0); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | uint32_t width = aSize.width; |
michael@0 | 89 | if (aFormat == D3DFMT_A8R8G8B8) { |
michael@0 | 90 | width *= 4; |
michael@0 | 91 | } |
michael@0 | 92 | for (int y = 0; y < aSize.height; y++) { |
michael@0 | 93 | memcpy((char*)lockedRect.pBits + lockedRect.Pitch * y, |
michael@0 | 94 | aData + aStride * y, |
michael@0 | 95 | width); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | if (deviceEx) { |
michael@0 | 99 | surface->UnlockRect(); |
michael@0 | 100 | nsRefPtr<IDirect3DSurface9> dstSurface; |
michael@0 | 101 | texture->GetSurfaceLevel(0, getter_AddRefs(dstSurface)); |
michael@0 | 102 | aDevice->UpdateSurface(surface, nullptr, dstSurface, nullptr); |
michael@0 | 103 | } else { |
michael@0 | 104 | texture->UnlockRect(0); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | return texture.forget(); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | static already_AddRefed<IDirect3DTexture9> |
michael@0 | 111 | OpenSharedTexture(const D3DSURFACE_DESC& aDesc, |
michael@0 | 112 | HANDLE aShareHandle, |
michael@0 | 113 | IDirect3DDevice9 *aDevice) |
michael@0 | 114 | { |
michael@0 | 115 | MOZ_ASSERT(aDesc.Format == D3DFMT_X8R8G8B8); |
michael@0 | 116 | |
michael@0 | 117 | // Open the frame from DXVA's device in our device using the resource |
michael@0 | 118 | // sharing handle. |
michael@0 | 119 | nsRefPtr<IDirect3DTexture9> sharedTexture; |
michael@0 | 120 | HRESULT hr = aDevice->CreateTexture(aDesc.Width, |
michael@0 | 121 | aDesc.Height, |
michael@0 | 122 | 1, |
michael@0 | 123 | D3DUSAGE_RENDERTARGET, |
michael@0 | 124 | D3DFMT_X8R8G8B8, |
michael@0 | 125 | D3DPOOL_DEFAULT, |
michael@0 | 126 | getter_AddRefs(sharedTexture), |
michael@0 | 127 | &aShareHandle); |
michael@0 | 128 | if (FAILED(hr)) { |
michael@0 | 129 | NS_WARNING("Failed to open shared texture on our device"); |
michael@0 | 130 | } |
michael@0 | 131 | return sharedTexture.forget(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | static already_AddRefed<IDirect3DTexture9> |
michael@0 | 135 | SurfaceToTexture(IDirect3DDevice9 *aDevice, |
michael@0 | 136 | SourceSurface *aSurface, |
michael@0 | 137 | const IntSize &aSize) |
michael@0 | 138 | { |
michael@0 | 139 | RefPtr<DataSourceSurface> dataSurface = aSurface->GetDataSurface(); |
michael@0 | 140 | if (!dataSurface) { |
michael@0 | 141 | return nullptr; |
michael@0 | 142 | } |
michael@0 | 143 | DataSourceSurface::MappedSurface map; |
michael@0 | 144 | if (!dataSurface->Map(DataSourceSurface::MapType::READ, &map)) { |
michael@0 | 145 | return nullptr; |
michael@0 | 146 | } |
michael@0 | 147 | nsRefPtr<IDirect3DTexture9> texture = |
michael@0 | 148 | DataToTexture(aDevice, map.mData, map.mStride, aSize, |
michael@0 | 149 | D3dFormatForSurfaceFormat(dataSurface->GetFormat())); |
michael@0 | 150 | dataSurface->Unmap(); |
michael@0 | 151 | return texture.forget(); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | static void AllocateTexturesYCbCr(PlanarYCbCrImage *aImage, |
michael@0 | 155 | IDirect3DDevice9 *aDevice, |
michael@0 | 156 | LayerManagerD3D9 *aManager) |
michael@0 | 157 | { |
michael@0 | 158 | nsAutoPtr<PlanarYCbCrD3D9BackendData> backendData( |
michael@0 | 159 | new PlanarYCbCrD3D9BackendData); |
michael@0 | 160 | |
michael@0 | 161 | const PlanarYCbCrData *data = aImage->GetData(); |
michael@0 | 162 | |
michael@0 | 163 | D3DLOCKED_RECT lockrectY; |
michael@0 | 164 | D3DLOCKED_RECT lockrectCb; |
michael@0 | 165 | D3DLOCKED_RECT lockrectCr; |
michael@0 | 166 | uint8_t* src; |
michael@0 | 167 | uint8_t* dest; |
michael@0 | 168 | |
michael@0 | 169 | nsRefPtr<IDirect3DSurface9> tmpSurfaceY; |
michael@0 | 170 | nsRefPtr<IDirect3DSurface9> tmpSurfaceCb; |
michael@0 | 171 | nsRefPtr<IDirect3DSurface9> tmpSurfaceCr; |
michael@0 | 172 | |
michael@0 | 173 | nsRefPtr<IDirect3DDevice9Ex> deviceEx; |
michael@0 | 174 | aDevice->QueryInterface(IID_IDirect3DDevice9Ex, |
michael@0 | 175 | getter_AddRefs(deviceEx)); |
michael@0 | 176 | |
michael@0 | 177 | bool isD3D9Ex = deviceEx; |
michael@0 | 178 | |
michael@0 | 179 | if (isD3D9Ex) { |
michael@0 | 180 | nsRefPtr<IDirect3DTexture9> tmpYTexture; |
michael@0 | 181 | nsRefPtr<IDirect3DTexture9> tmpCbTexture; |
michael@0 | 182 | nsRefPtr<IDirect3DTexture9> tmpCrTexture; |
michael@0 | 183 | // D3D9Ex does not support the managed pool, could use dynamic textures |
michael@0 | 184 | // here. But since an Image is immutable static textures are probably a |
michael@0 | 185 | // better idea. |
michael@0 | 186 | |
michael@0 | 187 | HRESULT hr; |
michael@0 | 188 | hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height, |
michael@0 | 189 | 1, 0, D3DFMT_A8, D3DPOOL_DEFAULT, |
michael@0 | 190 | getter_AddRefs(backendData->mYTexture), nullptr); |
michael@0 | 191 | if (!FAILED(hr)) { |
michael@0 | 192 | hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 193 | 1, 0, D3DFMT_A8, D3DPOOL_DEFAULT, |
michael@0 | 194 | getter_AddRefs(backendData->mCbTexture), nullptr); |
michael@0 | 195 | } |
michael@0 | 196 | if (!FAILED(hr)) { |
michael@0 | 197 | hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 198 | 1, 0, D3DFMT_A8, D3DPOOL_DEFAULT, |
michael@0 | 199 | getter_AddRefs(backendData->mCrTexture), nullptr); |
michael@0 | 200 | } |
michael@0 | 201 | if (!FAILED(hr)) { |
michael@0 | 202 | hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height, |
michael@0 | 203 | 1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM, |
michael@0 | 204 | getter_AddRefs(tmpYTexture), nullptr); |
michael@0 | 205 | } |
michael@0 | 206 | if (!FAILED(hr)) { |
michael@0 | 207 | hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 208 | 1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM, |
michael@0 | 209 | getter_AddRefs(tmpCbTexture), nullptr); |
michael@0 | 210 | } |
michael@0 | 211 | if (!FAILED(hr)) { |
michael@0 | 212 | hr = aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 213 | 1, 0, D3DFMT_A8, D3DPOOL_SYSTEMMEM, |
michael@0 | 214 | getter_AddRefs(tmpCrTexture), nullptr); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | if (FAILED(hr)) { |
michael@0 | 218 | aManager->ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D9::AllocateTextures(): Failed to create texture (isD3D9Ex)"), |
michael@0 | 219 | hr); |
michael@0 | 220 | return; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | tmpYTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceY)); |
michael@0 | 224 | tmpCbTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCb)); |
michael@0 | 225 | tmpCrTexture->GetSurfaceLevel(0, getter_AddRefs(tmpSurfaceCr)); |
michael@0 | 226 | tmpSurfaceY->LockRect(&lockrectY, nullptr, 0); |
michael@0 | 227 | tmpSurfaceCb->LockRect(&lockrectCb, nullptr, 0); |
michael@0 | 228 | tmpSurfaceCr->LockRect(&lockrectCr, nullptr, 0); |
michael@0 | 229 | } else { |
michael@0 | 230 | HRESULT hr; |
michael@0 | 231 | hr = aDevice->CreateTexture(data->mYSize.width, data->mYSize.height, |
michael@0 | 232 | 1, 0, D3DFMT_A8, D3DPOOL_MANAGED, |
michael@0 | 233 | getter_AddRefs(backendData->mYTexture), nullptr); |
michael@0 | 234 | if (!FAILED(hr)) { |
michael@0 | 235 | aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 236 | 1, 0, D3DFMT_A8, D3DPOOL_MANAGED, |
michael@0 | 237 | getter_AddRefs(backendData->mCbTexture), nullptr); |
michael@0 | 238 | } |
michael@0 | 239 | if (!FAILED(hr)) { |
michael@0 | 240 | aDevice->CreateTexture(data->mCbCrSize.width, data->mCbCrSize.height, |
michael@0 | 241 | 1, 0, D3DFMT_A8, D3DPOOL_MANAGED, |
michael@0 | 242 | getter_AddRefs(backendData->mCrTexture), nullptr); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | if (FAILED(hr)) { |
michael@0 | 246 | aManager->ReportFailure(NS_LITERAL_CSTRING("PlanarYCbCrImageD3D9::AllocateTextures(): Failed to create texture (!isD3D9Ex)"), |
michael@0 | 247 | hr); |
michael@0 | 248 | return; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /* lock the entire texture */ |
michael@0 | 252 | backendData->mYTexture->LockRect(0, &lockrectY, nullptr, 0); |
michael@0 | 253 | backendData->mCbTexture->LockRect(0, &lockrectCb, nullptr, 0); |
michael@0 | 254 | backendData->mCrTexture->LockRect(0, &lockrectCr, nullptr, 0); |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | src = data->mYChannel; |
michael@0 | 258 | //FIX cast |
michael@0 | 259 | dest = (uint8_t*)lockrectY.pBits; |
michael@0 | 260 | |
michael@0 | 261 | // copy over data |
michael@0 | 262 | for (int h=0; h<data->mYSize.height; h++) { |
michael@0 | 263 | memcpy(dest, src, data->mYSize.width); |
michael@0 | 264 | dest += lockrectY.Pitch; |
michael@0 | 265 | src += data->mYStride; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | src = data->mCbChannel; |
michael@0 | 269 | //FIX cast |
michael@0 | 270 | dest = (uint8_t*)lockrectCb.pBits; |
michael@0 | 271 | |
michael@0 | 272 | // copy over data |
michael@0 | 273 | for (int h=0; h<data->mCbCrSize.height; h++) { |
michael@0 | 274 | memcpy(dest, src, data->mCbCrSize.width); |
michael@0 | 275 | dest += lockrectCb.Pitch; |
michael@0 | 276 | src += data->mCbCrStride; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | src = data->mCrChannel; |
michael@0 | 280 | //FIX cast |
michael@0 | 281 | dest = (uint8_t*)lockrectCr.pBits; |
michael@0 | 282 | |
michael@0 | 283 | // copy over data |
michael@0 | 284 | for (int h=0; h<data->mCbCrSize.height; h++) { |
michael@0 | 285 | memcpy(dest, src, data->mCbCrSize.width); |
michael@0 | 286 | dest += lockrectCr.Pitch; |
michael@0 | 287 | src += data->mCbCrStride; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | if (isD3D9Ex) { |
michael@0 | 291 | tmpSurfaceY->UnlockRect(); |
michael@0 | 292 | tmpSurfaceCb->UnlockRect(); |
michael@0 | 293 | tmpSurfaceCr->UnlockRect(); |
michael@0 | 294 | nsRefPtr<IDirect3DSurface9> dstSurface; |
michael@0 | 295 | backendData->mYTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface)); |
michael@0 | 296 | aDevice->UpdateSurface(tmpSurfaceY, nullptr, dstSurface, nullptr); |
michael@0 | 297 | backendData->mCbTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface)); |
michael@0 | 298 | aDevice->UpdateSurface(tmpSurfaceCb, nullptr, dstSurface, nullptr); |
michael@0 | 299 | backendData->mCrTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface)); |
michael@0 | 300 | aDevice->UpdateSurface(tmpSurfaceCr, nullptr, dstSurface, nullptr); |
michael@0 | 301 | } else { |
michael@0 | 302 | backendData->mYTexture->UnlockRect(0); |
michael@0 | 303 | backendData->mCbTexture->UnlockRect(0); |
michael@0 | 304 | backendData->mCrTexture->UnlockRect(0); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget()); |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | Layer* |
michael@0 | 311 | ImageLayerD3D9::GetLayer() |
michael@0 | 312 | { |
michael@0 | 313 | return this; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | /* |
michael@0 | 317 | * Returns a texture which backs aImage |
michael@0 | 318 | * Will only work if aImage is a cairo or remote image. |
michael@0 | 319 | * Returns nullptr if unsuccessful. |
michael@0 | 320 | * If successful, aHasAlpha will be set to true if the texture has an |
michael@0 | 321 | * alpha component, false otherwise. |
michael@0 | 322 | */ |
michael@0 | 323 | IDirect3DTexture9* |
michael@0 | 324 | ImageLayerD3D9::GetTexture(Image *aImage, bool& aHasAlpha) |
michael@0 | 325 | { |
michael@0 | 326 | NS_ASSERTION(aImage, "Null image."); |
michael@0 | 327 | |
michael@0 | 328 | if (aImage->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP) { |
michael@0 | 329 | RemoteBitmapImage *remoteImage = |
michael@0 | 330 | static_cast<RemoteBitmapImage*>(aImage); |
michael@0 | 331 | |
michael@0 | 332 | if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) { |
michael@0 | 333 | nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData()); |
michael@0 | 334 | dat->mTexture = DataToTexture(device(), remoteImage->mData, remoteImage->mStride, remoteImage->mSize, D3DFMT_A8R8G8B8); |
michael@0 | 335 | if (dat->mTexture) { |
michael@0 | 336 | aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget()); |
michael@0 | 337 | } |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | aHasAlpha = remoteImage->mFormat == RemoteImageData::BGRA32; |
michael@0 | 341 | } else if (aImage->GetFormat() == ImageFormat::CAIRO_SURFACE) { |
michael@0 | 342 | CairoImage *cairoImage = |
michael@0 | 343 | static_cast<CairoImage*>(aImage); |
michael@0 | 344 | |
michael@0 | 345 | RefPtr<SourceSurface> surf = cairoImage->GetAsSourceSurface(); |
michael@0 | 346 | if (!surf) { |
michael@0 | 347 | return nullptr; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) { |
michael@0 | 351 | nsAutoPtr<TextureD3D9BackendData> dat(new TextureD3D9BackendData()); |
michael@0 | 352 | dat->mTexture = SurfaceToTexture(device(), surf, cairoImage->GetSize()); |
michael@0 | 353 | if (dat->mTexture) { |
michael@0 | 354 | aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, dat.forget()); |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | aHasAlpha = surf->GetFormat() == SurfaceFormat::B8G8R8A8; |
michael@0 | 359 | } else if (aImage->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) { |
michael@0 | 360 | if (!aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) { |
michael@0 | 361 | // The texture in which the frame is stored belongs to DXVA's D3D9 device. |
michael@0 | 362 | // We need to open it on our device before we can use it. |
michael@0 | 363 | nsAutoPtr<TextureD3D9BackendData> backendData(new TextureD3D9BackendData()); |
michael@0 | 364 | D3D9SurfaceImage* image = static_cast<D3D9SurfaceImage*>(aImage); |
michael@0 | 365 | backendData->mTexture = OpenSharedTexture(image->GetDesc(), image->GetShareHandle(), device()); |
michael@0 | 366 | if (backendData->mTexture) { |
michael@0 | 367 | aImage->SetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9, backendData.forget()); |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | aHasAlpha = false; |
michael@0 | 371 | } else { |
michael@0 | 372 | NS_WARNING("Inappropriate image type."); |
michael@0 | 373 | return nullptr; |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | TextureD3D9BackendData *data = |
michael@0 | 377 | static_cast<TextureD3D9BackendData*>(aImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)); |
michael@0 | 378 | |
michael@0 | 379 | if (!data) { |
michael@0 | 380 | return nullptr; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | nsRefPtr<IDirect3DDevice9> dev; |
michael@0 | 384 | data->mTexture->GetDevice(getter_AddRefs(dev)); |
michael@0 | 385 | if (dev != device()) { |
michael@0 | 386 | return nullptr; |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | return data->mTexture; |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | void |
michael@0 | 393 | ImageLayerD3D9::RenderLayer() |
michael@0 | 394 | { |
michael@0 | 395 | ImageContainer *container = GetContainer(); |
michael@0 | 396 | if (!container || mD3DManager->CompositingDisabled()) { |
michael@0 | 397 | return; |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | AutoLockImage autoLock(container); |
michael@0 | 401 | |
michael@0 | 402 | Image *image = autoLock.GetImage(); |
michael@0 | 403 | if (!image) { |
michael@0 | 404 | return; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | SetShaderTransformAndOpacity(); |
michael@0 | 408 | |
michael@0 | 409 | gfx::IntSize size = image->GetSize(); |
michael@0 | 410 | |
michael@0 | 411 | if (image->GetFormat() == ImageFormat::CAIRO_SURFACE || |
michael@0 | 412 | image->GetFormat() == ImageFormat::REMOTE_IMAGE_BITMAP || |
michael@0 | 413 | image->GetFormat() == ImageFormat::D3D9_RGB32_TEXTURE) |
michael@0 | 414 | { |
michael@0 | 415 | NS_ASSERTION(image->GetFormat() != ImageFormat::CAIRO_SURFACE || |
michael@0 | 416 | !static_cast<CairoImage*>(image)->mSourceSurface || |
michael@0 | 417 | static_cast<CairoImage*>(image)->mSourceSurface->GetFormat() != SurfaceFormat::A8, |
michael@0 | 418 | "Image layer has alpha image"); |
michael@0 | 419 | |
michael@0 | 420 | bool hasAlpha = false; |
michael@0 | 421 | nsRefPtr<IDirect3DTexture9> texture = GetTexture(image, hasAlpha); |
michael@0 | 422 | |
michael@0 | 423 | device()->SetVertexShaderConstantF(CBvLayerQuad, |
michael@0 | 424 | ShaderConstantRect(0, |
michael@0 | 425 | 0, |
michael@0 | 426 | size.width, |
michael@0 | 427 | size.height), |
michael@0 | 428 | 1); |
michael@0 | 429 | |
michael@0 | 430 | if (hasAlpha) { |
michael@0 | 431 | mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBALAYER, GetMaskLayer()); |
michael@0 | 432 | } else { |
michael@0 | 433 | mD3DManager->SetShaderMode(DeviceManagerD3D9::RGBLAYER, GetMaskLayer()); |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | if (mFilter == GraphicsFilter::FILTER_NEAREST) { |
michael@0 | 437 | device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); |
michael@0 | 438 | device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); |
michael@0 | 439 | } |
michael@0 | 440 | device()->SetTexture(0, texture); |
michael@0 | 441 | |
michael@0 | 442 | image = nullptr; |
michael@0 | 443 | autoLock.Unlock(); |
michael@0 | 444 | |
michael@0 | 445 | device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); |
michael@0 | 446 | if (mFilter == GraphicsFilter::FILTER_NEAREST) { |
michael@0 | 447 | device()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); |
michael@0 | 448 | device()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); |
michael@0 | 449 | } |
michael@0 | 450 | } else { |
michael@0 | 451 | PlanarYCbCrImage *yuvImage = |
michael@0 | 452 | static_cast<PlanarYCbCrImage*>(image); |
michael@0 | 453 | |
michael@0 | 454 | if (!yuvImage->IsValid()) { |
michael@0 | 455 | return; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | if (!yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)) { |
michael@0 | 459 | AllocateTexturesYCbCr(yuvImage, device(), mD3DManager); |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | PlanarYCbCrD3D9BackendData *data = |
michael@0 | 463 | static_cast<PlanarYCbCrD3D9BackendData*>(yuvImage->GetBackendData(mozilla::layers::LayersBackend::LAYERS_D3D9)); |
michael@0 | 464 | |
michael@0 | 465 | if (!data) { |
michael@0 | 466 | return; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | nsRefPtr<IDirect3DDevice9> dev; |
michael@0 | 470 | data->mYTexture->GetDevice(getter_AddRefs(dev)); |
michael@0 | 471 | if (dev != device()) { |
michael@0 | 472 | return; |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | device()->SetVertexShaderConstantF(CBvLayerQuad, |
michael@0 | 476 | ShaderConstantRect(0, |
michael@0 | 477 | 0, |
michael@0 | 478 | size.width, |
michael@0 | 479 | size.height), |
michael@0 | 480 | 1); |
michael@0 | 481 | |
michael@0 | 482 | device()->SetVertexShaderConstantF(CBvTextureCoords, |
michael@0 | 483 | ShaderConstantRect( |
michael@0 | 484 | (float)yuvImage->GetData()->mPicX / yuvImage->GetData()->mYSize.width, |
michael@0 | 485 | (float)yuvImage->GetData()->mPicY / yuvImage->GetData()->mYSize.height, |
michael@0 | 486 | (float)yuvImage->GetData()->mPicSize.width / yuvImage->GetData()->mYSize.width, |
michael@0 | 487 | (float)yuvImage->GetData()->mPicSize.height / yuvImage->GetData()->mYSize.height |
michael@0 | 488 | ), |
michael@0 | 489 | 1); |
michael@0 | 490 | |
michael@0 | 491 | mD3DManager->SetShaderMode(DeviceManagerD3D9::YCBCRLAYER, GetMaskLayer()); |
michael@0 | 492 | |
michael@0 | 493 | /* |
michael@0 | 494 | * Send 3d control data and metadata |
michael@0 | 495 | */ |
michael@0 | 496 | if (mD3DManager->GetNv3DVUtils()) { |
michael@0 | 497 | Nv_Stereo_Mode mode; |
michael@0 | 498 | switch (yuvImage->GetData()->mStereoMode) { |
michael@0 | 499 | case StereoMode::LEFT_RIGHT: |
michael@0 | 500 | mode = NV_STEREO_MODE_LEFT_RIGHT; |
michael@0 | 501 | break; |
michael@0 | 502 | case StereoMode::RIGHT_LEFT: |
michael@0 | 503 | mode = NV_STEREO_MODE_RIGHT_LEFT; |
michael@0 | 504 | break; |
michael@0 | 505 | case StereoMode::BOTTOM_TOP: |
michael@0 | 506 | mode = NV_STEREO_MODE_BOTTOM_TOP; |
michael@0 | 507 | break; |
michael@0 | 508 | case StereoMode::TOP_BOTTOM: |
michael@0 | 509 | mode = NV_STEREO_MODE_TOP_BOTTOM; |
michael@0 | 510 | break; |
michael@0 | 511 | case StereoMode::MONO: |
michael@0 | 512 | mode = NV_STEREO_MODE_MONO; |
michael@0 | 513 | break; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | // Send control data even in mono case so driver knows to leave stereo mode. |
michael@0 | 517 | mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE); |
michael@0 | 518 | |
michael@0 | 519 | if (yuvImage->GetData()->mStereoMode != StereoMode::MONO) { |
michael@0 | 520 | mD3DManager->GetNv3DVUtils()->SendNv3DVControl(mode, true, FIREFOX_3DV_APP_HANDLE); |
michael@0 | 521 | |
michael@0 | 522 | nsRefPtr<IDirect3DSurface9> renderTarget; |
michael@0 | 523 | device()->GetRenderTarget(0, getter_AddRefs(renderTarget)); |
michael@0 | 524 | mD3DManager->GetNv3DVUtils()->SendNv3DVMetaData((unsigned int)yuvImage->GetSize().width, |
michael@0 | 525 | (unsigned int)yuvImage->GetSize().height, (HANDLE)(data->mYTexture), (HANDLE)(renderTarget)); |
michael@0 | 526 | } |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | // Linear scaling is default here, adhering to mFilter is difficult since |
michael@0 | 530 | // presumably even with point filtering we'll still want chroma upsampling |
michael@0 | 531 | // to be linear. In the current approach we can't. |
michael@0 | 532 | device()->SetTexture(0, data->mYTexture); |
michael@0 | 533 | device()->SetTexture(1, data->mCbTexture); |
michael@0 | 534 | device()->SetTexture(2, data->mCrTexture); |
michael@0 | 535 | |
michael@0 | 536 | image = nullptr; |
michael@0 | 537 | data = nullptr; |
michael@0 | 538 | autoLock.Unlock(); |
michael@0 | 539 | |
michael@0 | 540 | device()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); |
michael@0 | 541 | |
michael@0 | 542 | device()->SetVertexShaderConstantF(CBvTextureCoords, |
michael@0 | 543 | ShaderConstantRect(0, 0, 1.0f, 1.0f), 1); |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | GetContainer()->NotifyPaintedImage(image); |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | already_AddRefed<IDirect3DTexture9> |
michael@0 | 550 | ImageLayerD3D9::GetAsTexture(gfx::IntSize* aSize) |
michael@0 | 551 | { |
michael@0 | 552 | if (!GetContainer()) { |
michael@0 | 553 | return nullptr; |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | AutoLockImage autoLock(GetContainer()); |
michael@0 | 557 | |
michael@0 | 558 | Image *image = autoLock.GetImage(); |
michael@0 | 559 | |
michael@0 | 560 | if (!image) { |
michael@0 | 561 | return nullptr; |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | if (image->GetFormat() != ImageFormat::CAIRO_SURFACE && |
michael@0 | 565 | image->GetFormat() != ImageFormat::REMOTE_IMAGE_BITMAP) { |
michael@0 | 566 | return nullptr; |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | bool dontCare; |
michael@0 | 570 | *aSize = image->GetSize(); |
michael@0 | 571 | nsRefPtr<IDirect3DTexture9> result = GetTexture(image, dontCare); |
michael@0 | 572 | return result.forget(); |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | } /* layers */ |
michael@0 | 576 | } /* mozilla */ |