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: 4 -*- |
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 "TextureD3D9.h" |
michael@0 | 7 | #include "CompositorD3D9.h" |
michael@0 | 8 | #include "gfxContext.h" |
michael@0 | 9 | #include "gfxImageSurface.h" |
michael@0 | 10 | #include "Effects.h" |
michael@0 | 11 | #include "mozilla/layers/YCbCrImageDataSerializer.h" |
michael@0 | 12 | #include "gfxWindowsPlatform.h" |
michael@0 | 13 | #include "gfx2DGlue.h" |
michael@0 | 14 | #include "gfxUtils.h" |
michael@0 | 15 | #include "mozilla/gfx/2D.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace mozilla::gfx; |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace layers { |
michael@0 | 21 | |
michael@0 | 22 | static uint32_t GetRequiredTilesD3D9(uint32_t aSize, uint32_t aMaxSize) |
michael@0 | 23 | { |
michael@0 | 24 | uint32_t requiredTiles = aSize / aMaxSize; |
michael@0 | 25 | if (aSize % aMaxSize) { |
michael@0 | 26 | requiredTiles++; |
michael@0 | 27 | } |
michael@0 | 28 | return requiredTiles; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | TextureSourceD3D9::~TextureSourceD3D9() |
michael@0 | 32 | { |
michael@0 | 33 | MOZ_ASSERT(!mCreatingDeviceManager || |
michael@0 | 34 | mCreatingDeviceManager->IsInTextureHostList(this), |
michael@0 | 35 | "Inconsistency in list of texture hosts."); |
michael@0 | 36 | // Remove ourselves from the list of d3d9 texture hosts. |
michael@0 | 37 | if (mPreviousHost) { |
michael@0 | 38 | MOZ_ASSERT(mPreviousHost->mNextHost == this); |
michael@0 | 39 | mPreviousHost->mNextHost = mNextHost; |
michael@0 | 40 | } else if (mCreatingDeviceManager) { |
michael@0 | 41 | mCreatingDeviceManager->RemoveTextureListHead(this); |
michael@0 | 42 | } |
michael@0 | 43 | if (mNextHost) { |
michael@0 | 44 | MOZ_ASSERT(mNextHost->mPreviousHost == this); |
michael@0 | 45 | mNextHost->mPreviousHost = mPreviousHost; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | TemporaryRef<TextureHost> |
michael@0 | 50 | CreateTextureHostD3D9(const SurfaceDescriptor& aDesc, |
michael@0 | 51 | ISurfaceAllocator* aDeallocator, |
michael@0 | 52 | TextureFlags aFlags) |
michael@0 | 53 | { |
michael@0 | 54 | RefPtr<TextureHost> result; |
michael@0 | 55 | switch (aDesc.type()) { |
michael@0 | 56 | case SurfaceDescriptor::TSurfaceDescriptorShmem: |
michael@0 | 57 | case SurfaceDescriptor::TSurfaceDescriptorMemory: { |
michael@0 | 58 | result = CreateBackendIndependentTextureHost(aDesc, aDeallocator, aFlags); |
michael@0 | 59 | break; |
michael@0 | 60 | } |
michael@0 | 61 | case SurfaceDescriptor::TSurfaceDescriptorD3D9: { |
michael@0 | 62 | result = new TextureHostD3D9(aFlags, aDesc); |
michael@0 | 63 | break; |
michael@0 | 64 | } |
michael@0 | 65 | case SurfaceDescriptor::TSurfaceDescriptorDIB: { |
michael@0 | 66 | result = new DIBTextureHostD3D9(aFlags, aDesc); |
michael@0 | 67 | break; |
michael@0 | 68 | } |
michael@0 | 69 | case SurfaceDescriptor::TSurfaceDescriptorD3D10: { |
michael@0 | 70 | result = new DXGITextureHostD3D9(aFlags, aDesc); |
michael@0 | 71 | break; |
michael@0 | 72 | } |
michael@0 | 73 | default: { |
michael@0 | 74 | NS_WARNING("Unsupported SurfaceDescriptor type"); |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | return result.forget(); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | static SurfaceFormat |
michael@0 | 81 | D3D9FormatToSurfaceFormat(_D3DFORMAT format) |
michael@0 | 82 | { |
michael@0 | 83 | switch (format) { |
michael@0 | 84 | case D3DFMT_X8R8G8B8: |
michael@0 | 85 | return SurfaceFormat::B8G8R8X8; |
michael@0 | 86 | case D3DFMT_A8R8G8B8: |
michael@0 | 87 | return SurfaceFormat::B8G8R8A8; |
michael@0 | 88 | case D3DFMT_A8: |
michael@0 | 89 | return SurfaceFormat::A8; |
michael@0 | 90 | default: |
michael@0 | 91 | NS_ERROR("Bad texture format"); |
michael@0 | 92 | } |
michael@0 | 93 | return SurfaceFormat::UNKNOWN; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | static _D3DFORMAT |
michael@0 | 97 | SurfaceFormatToD3D9Format(SurfaceFormat format) |
michael@0 | 98 | { |
michael@0 | 99 | switch (format) { |
michael@0 | 100 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 101 | return D3DFMT_X8R8G8B8; |
michael@0 | 102 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 103 | return D3DFMT_A8R8G8B8; |
michael@0 | 104 | case SurfaceFormat::A8: |
michael@0 | 105 | return D3DFMT_A8; |
michael@0 | 106 | default: |
michael@0 | 107 | NS_ERROR("Bad texture format"); |
michael@0 | 108 | } |
michael@0 | 109 | return D3DFMT_A8R8G8B8; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | CompositingRenderTargetD3D9::CompositingRenderTargetD3D9(IDirect3DTexture9* aTexture, |
michael@0 | 113 | SurfaceInitMode aInit, |
michael@0 | 114 | const gfx::IntRect& aRect) |
michael@0 | 115 | : CompositingRenderTarget(aRect.TopLeft()) |
michael@0 | 116 | , mInitMode(aInit) |
michael@0 | 117 | , mInitialized(false) |
michael@0 | 118 | { |
michael@0 | 119 | MOZ_COUNT_CTOR(CompositingRenderTargetD3D9); |
michael@0 | 120 | MOZ_ASSERT(aTexture); |
michael@0 | 121 | |
michael@0 | 122 | mTexture = aTexture; |
michael@0 | 123 | HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(mSurface)); |
michael@0 | 124 | NS_ASSERTION(mSurface, "Couldn't create surface for texture"); |
michael@0 | 125 | TextureSourceD3D9::SetSize(aRect.Size()); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | CompositingRenderTargetD3D9::CompositingRenderTargetD3D9(IDirect3DSurface9* aSurface, |
michael@0 | 129 | SurfaceInitMode aInit, |
michael@0 | 130 | const gfx::IntRect& aRect) |
michael@0 | 131 | : CompositingRenderTarget(aRect.TopLeft()) |
michael@0 | 132 | , mSurface(aSurface) |
michael@0 | 133 | , mInitMode(aInit) |
michael@0 | 134 | , mInitialized(false) |
michael@0 | 135 | { |
michael@0 | 136 | MOZ_COUNT_CTOR(CompositingRenderTargetD3D9); |
michael@0 | 137 | MOZ_ASSERT(mSurface); |
michael@0 | 138 | TextureSourceD3D9::SetSize(aRect.Size()); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | CompositingRenderTargetD3D9::~CompositingRenderTargetD3D9() |
michael@0 | 142 | { |
michael@0 | 143 | MOZ_COUNT_DTOR(CompositingRenderTargetD3D9); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void |
michael@0 | 147 | CompositingRenderTargetD3D9::BindRenderTarget(IDirect3DDevice9* aDevice) |
michael@0 | 148 | { |
michael@0 | 149 | aDevice->SetRenderTarget(0, mSurface); |
michael@0 | 150 | if (!mInitialized && |
michael@0 | 151 | mInitMode == INIT_MODE_CLEAR) { |
michael@0 | 152 | mInitialized = true; |
michael@0 | 153 | aDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 0, 0); |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | IntSize |
michael@0 | 158 | CompositingRenderTargetD3D9::GetSize() const |
michael@0 | 159 | { |
michael@0 | 160 | return TextureSourceD3D9::GetSize(); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Helper method for DataToTexture and SurfaceToTexture. |
michael@0 | 165 | * The last three params are out params. |
michael@0 | 166 | * Returns the created texture, or null if we fail. |
michael@0 | 167 | */ |
michael@0 | 168 | TemporaryRef<IDirect3DTexture9> |
michael@0 | 169 | TextureSourceD3D9::InitTextures(DeviceManagerD3D9* aDeviceManager, |
michael@0 | 170 | const IntSize &aSize, |
michael@0 | 171 | _D3DFORMAT aFormat, |
michael@0 | 172 | RefPtr<IDirect3DSurface9>& aSurface, |
michael@0 | 173 | D3DLOCKED_RECT& aLockedRect) |
michael@0 | 174 | { |
michael@0 | 175 | if (!aDeviceManager) { |
michael@0 | 176 | return nullptr; |
michael@0 | 177 | } |
michael@0 | 178 | RefPtr<IDirect3DTexture9> result; |
michael@0 | 179 | // D3D9Ex doesn't support managed textures and we don't want the hassle even |
michael@0 | 180 | // if we don't have Ex. We could use dynamic textures |
michael@0 | 181 | // here but since Images are immutable that probably isn't such a great |
michael@0 | 182 | // idea. |
michael@0 | 183 | result = aDeviceManager->CreateTexture(aSize, aFormat, D3DPOOL_DEFAULT, this); |
michael@0 | 184 | if (!result) { |
michael@0 | 185 | return nullptr; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | RefPtr<IDirect3DTexture9> tmpTexture = |
michael@0 | 189 | aDeviceManager->CreateTexture(aSize, aFormat, D3DPOOL_SYSTEMMEM, this); |
michael@0 | 190 | if (!tmpTexture) { |
michael@0 | 191 | return nullptr; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | tmpTexture->GetSurfaceLevel(0, byRef(aSurface)); |
michael@0 | 195 | aSurface->LockRect(&aLockedRect, nullptr, 0); |
michael@0 | 196 | if (!aLockedRect.pBits) { |
michael@0 | 197 | NS_WARNING("Could not lock surface"); |
michael@0 | 198 | return nullptr; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | return result; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * Helper method for DataToTexture and SurfaceToTexture. |
michael@0 | 206 | */ |
michael@0 | 207 | static void |
michael@0 | 208 | FinishTextures(DeviceManagerD3D9* aDeviceManager, |
michael@0 | 209 | IDirect3DTexture9* aTexture, |
michael@0 | 210 | IDirect3DSurface9* aSurface) |
michael@0 | 211 | { |
michael@0 | 212 | if (!aDeviceManager) { |
michael@0 | 213 | return; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | aSurface->UnlockRect(); |
michael@0 | 217 | nsRefPtr<IDirect3DSurface9> dstSurface; |
michael@0 | 218 | aTexture->GetSurfaceLevel(0, getter_AddRefs(dstSurface)); |
michael@0 | 219 | aDeviceManager->device()->UpdateSurface(aSurface, nullptr, dstSurface, |
michael@0 | 220 | nullptr); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | TemporaryRef<IDirect3DTexture9> |
michael@0 | 224 | TextureSourceD3D9::DataToTexture(DeviceManagerD3D9* aDeviceManager, |
michael@0 | 225 | unsigned char *aData, |
michael@0 | 226 | int aStride, |
michael@0 | 227 | const IntSize &aSize, |
michael@0 | 228 | _D3DFORMAT aFormat, |
michael@0 | 229 | uint32_t aBPP) |
michael@0 | 230 | { |
michael@0 | 231 | RefPtr<IDirect3DSurface9> surface; |
michael@0 | 232 | D3DLOCKED_RECT lockedRect; |
michael@0 | 233 | RefPtr<IDirect3DTexture9> texture = InitTextures(aDeviceManager, aSize, aFormat, |
michael@0 | 234 | surface, lockedRect); |
michael@0 | 235 | if (!texture) { |
michael@0 | 236 | return nullptr; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | uint32_t width = aSize.width * aBPP; |
michael@0 | 240 | |
michael@0 | 241 | for (int y = 0; y < aSize.height; y++) { |
michael@0 | 242 | memcpy((char*)lockedRect.pBits + lockedRect.Pitch * y, |
michael@0 | 243 | aData + aStride * y, |
michael@0 | 244 | width); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | FinishTextures(aDeviceManager, texture, surface); |
michael@0 | 248 | |
michael@0 | 249 | return texture; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | TemporaryRef<IDirect3DTexture9> |
michael@0 | 253 | TextureSourceD3D9::TextureToTexture(DeviceManagerD3D9* aDeviceManager, |
michael@0 | 254 | IDirect3DTexture9* aTexture, |
michael@0 | 255 | const IntSize& aSize, |
michael@0 | 256 | _D3DFORMAT aFormat) |
michael@0 | 257 | { |
michael@0 | 258 | if (!aDeviceManager) { |
michael@0 | 259 | return nullptr; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | RefPtr<IDirect3DTexture9> texture = |
michael@0 | 263 | aDeviceManager->CreateTexture(aSize, aFormat, D3DPOOL_DEFAULT, this); |
michael@0 | 264 | if (!texture) { |
michael@0 | 265 | return nullptr; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | HRESULT hr = aDeviceManager->device()->UpdateTexture(aTexture, texture); |
michael@0 | 269 | if (FAILED(hr)) { |
michael@0 | 270 | return nullptr; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | return texture; |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | TemporaryRef<IDirect3DTexture9> |
michael@0 | 277 | TextureSourceD3D9::SurfaceToTexture(DeviceManagerD3D9* aDeviceManager, |
michael@0 | 278 | gfxWindowsSurface* aSurface, |
michael@0 | 279 | const IntSize& aSize, |
michael@0 | 280 | _D3DFORMAT aFormat) |
michael@0 | 281 | { |
michael@0 | 282 | RefPtr<IDirect3DSurface9> surface; |
michael@0 | 283 | D3DLOCKED_RECT lockedRect; |
michael@0 | 284 | |
michael@0 | 285 | RefPtr<IDirect3DTexture9> texture = InitTextures(aDeviceManager, aSize, aFormat, |
michael@0 | 286 | surface, lockedRect); |
michael@0 | 287 | if (!texture) { |
michael@0 | 288 | return nullptr; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | nsRefPtr<gfxImageSurface> imgSurface = |
michael@0 | 292 | new gfxImageSurface(reinterpret_cast<unsigned char*>(lockedRect.pBits), |
michael@0 | 293 | gfxIntSize(aSize.width, aSize.height), |
michael@0 | 294 | lockedRect.Pitch, |
michael@0 | 295 | gfxPlatform::GetPlatform()->OptimalFormatForContent(aSurface->GetContentType())); |
michael@0 | 296 | |
michael@0 | 297 | nsRefPtr<gfxContext> context = new gfxContext(imgSurface); |
michael@0 | 298 | context->SetSource(aSurface); |
michael@0 | 299 | context->SetOperator(gfxContext::OPERATOR_SOURCE); |
michael@0 | 300 | context->Paint(); |
michael@0 | 301 | |
michael@0 | 302 | FinishTextures(aDeviceManager, texture, surface); |
michael@0 | 303 | |
michael@0 | 304 | return texture; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | class D3D9TextureClientData : public TextureClientData |
michael@0 | 308 | { |
michael@0 | 309 | public: |
michael@0 | 310 | D3D9TextureClientData(IDirect3DTexture9* aTexture) |
michael@0 | 311 | : mTexture(aTexture) |
michael@0 | 312 | {} |
michael@0 | 313 | |
michael@0 | 314 | D3D9TextureClientData(gfxWindowsSurface* aWindowSurface) |
michael@0 | 315 | : mWindowSurface(aWindowSurface) |
michael@0 | 316 | {} |
michael@0 | 317 | |
michael@0 | 318 | virtual void DeallocateSharedData(ISurfaceAllocator*) MOZ_OVERRIDE |
michael@0 | 319 | { |
michael@0 | 320 | mWindowSurface = nullptr; |
michael@0 | 321 | mTexture = nullptr; |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | private: |
michael@0 | 325 | RefPtr<IDirect3DTexture9> mTexture; |
michael@0 | 326 | nsRefPtr<gfxWindowsSurface> mWindowSurface; |
michael@0 | 327 | }; |
michael@0 | 328 | |
michael@0 | 329 | DataTextureSourceD3D9::DataTextureSourceD3D9(gfx::SurfaceFormat aFormat, |
michael@0 | 330 | CompositorD3D9* aCompositor, |
michael@0 | 331 | TextureFlags aFlags, |
michael@0 | 332 | StereoMode aStereoMode) |
michael@0 | 333 | : mFormat(aFormat) |
michael@0 | 334 | , mCompositor(aCompositor) |
michael@0 | 335 | , mCurrentTile(0) |
michael@0 | 336 | , mFlags(aFlags) |
michael@0 | 337 | , mIsTiled(false) |
michael@0 | 338 | , mIterating(false) |
michael@0 | 339 | { |
michael@0 | 340 | mStereoMode = aStereoMode; |
michael@0 | 341 | MOZ_COUNT_CTOR(DataTextureSourceD3D9); |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | DataTextureSourceD3D9::DataTextureSourceD3D9(gfx::SurfaceFormat aFormat, |
michael@0 | 345 | gfx::IntSize aSize, |
michael@0 | 346 | CompositorD3D9* aCompositor, |
michael@0 | 347 | IDirect3DTexture9* aTexture, |
michael@0 | 348 | TextureFlags aFlags) |
michael@0 | 349 | : mFormat(aFormat) |
michael@0 | 350 | , mCompositor(aCompositor) |
michael@0 | 351 | , mCurrentTile(0) |
michael@0 | 352 | , mFlags(aFlags) |
michael@0 | 353 | , mIsTiled(false) |
michael@0 | 354 | , mIterating(false) |
michael@0 | 355 | { |
michael@0 | 356 | mSize = aSize; |
michael@0 | 357 | mTexture = aTexture; |
michael@0 | 358 | mStereoMode = StereoMode::MONO; |
michael@0 | 359 | MOZ_COUNT_CTOR(DataTextureSourceD3D9); |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | DataTextureSourceD3D9::~DataTextureSourceD3D9() |
michael@0 | 363 | { |
michael@0 | 364 | MOZ_COUNT_DTOR(DataTextureSourceD3D9); |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | IDirect3DTexture9* |
michael@0 | 368 | DataTextureSourceD3D9::GetD3D9Texture() |
michael@0 | 369 | { |
michael@0 | 370 | return mIterating ? mTileTextures[mCurrentTile] |
michael@0 | 371 | : mTexture; |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | bool |
michael@0 | 375 | DataTextureSourceD3D9::Update(gfx::DataSourceSurface* aSurface, |
michael@0 | 376 | nsIntRegion* aDestRegion, |
michael@0 | 377 | gfx::IntPoint* aSrcOffset) |
michael@0 | 378 | { |
michael@0 | 379 | // Right now we only support full surface update. If aDestRegion is provided, |
michael@0 | 380 | // It will be ignored. Incremental update with a source offset is only used |
michael@0 | 381 | // on Mac so it is not clear that we ever will need to support it for D3D. |
michael@0 | 382 | MOZ_ASSERT(!aSrcOffset); |
michael@0 | 383 | |
michael@0 | 384 | if (!mCompositor || !mCompositor->device()) { |
michael@0 | 385 | NS_WARNING("No D3D device to update the texture."); |
michael@0 | 386 | return false; |
michael@0 | 387 | } |
michael@0 | 388 | mSize = aSurface->GetSize(); |
michael@0 | 389 | |
michael@0 | 390 | uint32_t bpp = 0; |
michael@0 | 391 | |
michael@0 | 392 | _D3DFORMAT format = D3DFMT_A8R8G8B8; |
michael@0 | 393 | mFormat = aSurface->GetFormat(); |
michael@0 | 394 | switch (mFormat) { |
michael@0 | 395 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 396 | format = D3DFMT_X8R8G8B8; |
michael@0 | 397 | bpp = 4; |
michael@0 | 398 | break; |
michael@0 | 399 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 400 | format = D3DFMT_A8R8G8B8; |
michael@0 | 401 | bpp = 4; |
michael@0 | 402 | break; |
michael@0 | 403 | case SurfaceFormat::A8: |
michael@0 | 404 | format = D3DFMT_A8; |
michael@0 | 405 | bpp = 1; |
michael@0 | 406 | break; |
michael@0 | 407 | default: |
michael@0 | 408 | NS_WARNING("Bad image format"); |
michael@0 | 409 | return false; |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | int32_t maxSize = mCompositor->GetMaxTextureSize(); |
michael@0 | 413 | DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); |
michael@0 | 414 | if ((mSize.width <= maxSize && mSize.height <= maxSize) || |
michael@0 | 415 | (mFlags & TEXTURE_DISALLOW_BIGIMAGE)) { |
michael@0 | 416 | mTexture = DataToTexture(deviceManager, |
michael@0 | 417 | aSurface->GetData(), aSurface->Stride(), |
michael@0 | 418 | IntSize(mSize), format, bpp); |
michael@0 | 419 | if (!mTexture) { |
michael@0 | 420 | NS_WARNING("Could not upload texture"); |
michael@0 | 421 | Reset(); |
michael@0 | 422 | return false; |
michael@0 | 423 | } |
michael@0 | 424 | mIsTiled = false; |
michael@0 | 425 | } else { |
michael@0 | 426 | mIsTiled = true; |
michael@0 | 427 | uint32_t tileCount = GetRequiredTilesD3D9(mSize.width, maxSize) * |
michael@0 | 428 | GetRequiredTilesD3D9(mSize.height, maxSize); |
michael@0 | 429 | mTileTextures.resize(tileCount); |
michael@0 | 430 | mTexture = nullptr; |
michael@0 | 431 | |
michael@0 | 432 | for (uint32_t i = 0; i < tileCount; i++) { |
michael@0 | 433 | IntRect tileRect = GetTileRect(i); |
michael@0 | 434 | unsigned char* data = aSurface->GetData() + |
michael@0 | 435 | tileRect.y * aSurface->Stride() + |
michael@0 | 436 | tileRect.x * bpp; |
michael@0 | 437 | mTileTextures[i] = DataToTexture(deviceManager, |
michael@0 | 438 | data, |
michael@0 | 439 | aSurface->Stride(), |
michael@0 | 440 | IntSize(tileRect.width, tileRect.height), |
michael@0 | 441 | format, |
michael@0 | 442 | bpp); |
michael@0 | 443 | if (!mTileTextures[i]) { |
michael@0 | 444 | NS_WARNING("Could not upload texture"); |
michael@0 | 445 | Reset(); |
michael@0 | 446 | return false; |
michael@0 | 447 | } |
michael@0 | 448 | } |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | return true; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | bool |
michael@0 | 455 | DataTextureSourceD3D9::Update(gfxWindowsSurface* aSurface) |
michael@0 | 456 | { |
michael@0 | 457 | MOZ_ASSERT(aSurface); |
michael@0 | 458 | if (!mCompositor || !mCompositor->device()) { |
michael@0 | 459 | NS_WARNING("No D3D device to update the texture."); |
michael@0 | 460 | return false; |
michael@0 | 461 | } |
michael@0 | 462 | mSize = ToIntSize(aSurface->GetSize()); |
michael@0 | 463 | |
michael@0 | 464 | uint32_t bpp = 0; |
michael@0 | 465 | |
michael@0 | 466 | _D3DFORMAT format = D3DFMT_A8R8G8B8; |
michael@0 | 467 | mFormat = ImageFormatToSurfaceFormat( |
michael@0 | 468 | gfxPlatform::GetPlatform()->OptimalFormatForContent(aSurface->GetContentType())); |
michael@0 | 469 | switch (mFormat) { |
michael@0 | 470 | case SurfaceFormat::B8G8R8X8: |
michael@0 | 471 | format = D3DFMT_X8R8G8B8; |
michael@0 | 472 | bpp = 4; |
michael@0 | 473 | break; |
michael@0 | 474 | case SurfaceFormat::B8G8R8A8: |
michael@0 | 475 | format = D3DFMT_A8R8G8B8; |
michael@0 | 476 | bpp = 4; |
michael@0 | 477 | break; |
michael@0 | 478 | case SurfaceFormat::A8: |
michael@0 | 479 | format = D3DFMT_A8; |
michael@0 | 480 | bpp = 1; |
michael@0 | 481 | break; |
michael@0 | 482 | default: |
michael@0 | 483 | NS_WARNING("Bad image format"); |
michael@0 | 484 | return false; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | int32_t maxSize = mCompositor->GetMaxTextureSize(); |
michael@0 | 488 | DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); |
michael@0 | 489 | if ((mSize.width <= maxSize && mSize.height <= maxSize) || |
michael@0 | 490 | (mFlags & TEXTURE_DISALLOW_BIGIMAGE)) { |
michael@0 | 491 | mTexture = SurfaceToTexture(deviceManager, aSurface, mSize, format); |
michael@0 | 492 | |
michael@0 | 493 | if (!mTexture) { |
michael@0 | 494 | NS_WARNING("Could not upload texture"); |
michael@0 | 495 | Reset(); |
michael@0 | 496 | return false; |
michael@0 | 497 | } |
michael@0 | 498 | mIsTiled = false; |
michael@0 | 499 | } else { |
michael@0 | 500 | mIsTiled = true; |
michael@0 | 501 | uint32_t tileCount = GetRequiredTilesD3D9(mSize.width, maxSize) * |
michael@0 | 502 | GetRequiredTilesD3D9(mSize.height, maxSize); |
michael@0 | 503 | mTileTextures.resize(tileCount); |
michael@0 | 504 | mTexture = nullptr; |
michael@0 | 505 | nsRefPtr<gfxImageSurface> imgSurface = aSurface->GetAsImageSurface(); |
michael@0 | 506 | |
michael@0 | 507 | for (uint32_t i = 0; i < tileCount; i++) { |
michael@0 | 508 | IntRect tileRect = GetTileRect(i); |
michael@0 | 509 | unsigned char* data = imgSurface->Data() + |
michael@0 | 510 | tileRect.y * imgSurface->Stride() + |
michael@0 | 511 | tileRect.x * bpp; |
michael@0 | 512 | mTileTextures[i] = DataToTexture(deviceManager, |
michael@0 | 513 | data, |
michael@0 | 514 | imgSurface->Stride(), |
michael@0 | 515 | IntSize(tileRect.width, tileRect.height), |
michael@0 | 516 | format, |
michael@0 | 517 | bpp); |
michael@0 | 518 | if (!mTileTextures[i]) { |
michael@0 | 519 | NS_WARNING("Could not upload texture"); |
michael@0 | 520 | Reset(); |
michael@0 | 521 | return false; |
michael@0 | 522 | } |
michael@0 | 523 | } |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | return true; |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | void |
michael@0 | 530 | DataTextureSourceD3D9::SetCompositor(Compositor* aCompositor) |
michael@0 | 531 | { |
michael@0 | 532 | CompositorD3D9* d3dCompositor = static_cast<CompositorD3D9*>(aCompositor); |
michael@0 | 533 | if (mCompositor && mCompositor != d3dCompositor) { |
michael@0 | 534 | Reset(); |
michael@0 | 535 | } |
michael@0 | 536 | mCompositor = d3dCompositor; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | void |
michael@0 | 540 | DataTextureSourceD3D9::Reset() |
michael@0 | 541 | { |
michael@0 | 542 | mSize.width = 0; |
michael@0 | 543 | mSize.height = 0; |
michael@0 | 544 | mIsTiled = false; |
michael@0 | 545 | mTexture = nullptr; |
michael@0 | 546 | mTileTextures.clear(); |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | IntRect |
michael@0 | 550 | DataTextureSourceD3D9::GetTileRect(uint32_t aTileIndex) const |
michael@0 | 551 | { |
michael@0 | 552 | uint32_t maxSize = mCompositor->GetMaxTextureSize(); |
michael@0 | 553 | uint32_t horizontalTiles = GetRequiredTilesD3D9(mSize.width, maxSize); |
michael@0 | 554 | uint32_t verticalTiles = GetRequiredTilesD3D9(mSize.height, maxSize); |
michael@0 | 555 | |
michael@0 | 556 | uint32_t verticalTile = aTileIndex / horizontalTiles; |
michael@0 | 557 | uint32_t horizontalTile = aTileIndex % horizontalTiles; |
michael@0 | 558 | |
michael@0 | 559 | return IntRect(horizontalTile * maxSize, |
michael@0 | 560 | verticalTile * maxSize, |
michael@0 | 561 | horizontalTile < (horizontalTiles - 1) ? maxSize : mSize.width % maxSize, |
michael@0 | 562 | verticalTile < (verticalTiles - 1) ? maxSize : mSize.height % maxSize); |
michael@0 | 563 | } |
michael@0 | 564 | |
michael@0 | 565 | nsIntRect |
michael@0 | 566 | DataTextureSourceD3D9::GetTileRect() |
michael@0 | 567 | { |
michael@0 | 568 | return ThebesIntRect(GetTileRect(mCurrentTile)); |
michael@0 | 569 | } |
michael@0 | 570 | |
michael@0 | 571 | CairoTextureClientD3D9::CairoTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags) |
michael@0 | 572 | : TextureClient(aFlags) |
michael@0 | 573 | , mFormat(aFormat) |
michael@0 | 574 | , mIsLocked(false) |
michael@0 | 575 | , mNeedsClear(false) |
michael@0 | 576 | , mLockRect(false) |
michael@0 | 577 | { |
michael@0 | 578 | MOZ_COUNT_CTOR(CairoTextureClientD3D9); |
michael@0 | 579 | } |
michael@0 | 580 | |
michael@0 | 581 | CairoTextureClientD3D9::~CairoTextureClientD3D9() |
michael@0 | 582 | { |
michael@0 | 583 | MOZ_COUNT_DTOR(CairoTextureClientD3D9); |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | bool |
michael@0 | 587 | CairoTextureClientD3D9::Lock(OpenMode) |
michael@0 | 588 | { |
michael@0 | 589 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 590 | if (!IsValid() || !IsAllocated()) { |
michael@0 | 591 | return false; |
michael@0 | 592 | } |
michael@0 | 593 | |
michael@0 | 594 | if (!gfxWindowsPlatform::GetPlatform()->GetD3D9Device()) { |
michael@0 | 595 | // If the device has failed then we should not lock the surface, |
michael@0 | 596 | // even if we could. |
michael@0 | 597 | mD3D9Surface = nullptr; |
michael@0 | 598 | return false; |
michael@0 | 599 | } |
michael@0 | 600 | |
michael@0 | 601 | if (!mD3D9Surface) { |
michael@0 | 602 | HRESULT hr = mTexture->GetSurfaceLevel(0, getter_AddRefs(mD3D9Surface)); |
michael@0 | 603 | if (FAILED(hr)) { |
michael@0 | 604 | NS_WARNING("Failed to get texture surface level."); |
michael@0 | 605 | return false; |
michael@0 | 606 | } |
michael@0 | 607 | } |
michael@0 | 608 | |
michael@0 | 609 | mIsLocked = true; |
michael@0 | 610 | |
michael@0 | 611 | if (mNeedsClear) { |
michael@0 | 612 | mDrawTarget = GetAsDrawTarget(); |
michael@0 | 613 | mDrawTarget->ClearRect(Rect(0, 0, GetSize().width, GetSize().height)); |
michael@0 | 614 | mNeedsClear = false; |
michael@0 | 615 | } |
michael@0 | 616 | |
michael@0 | 617 | return true; |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | void |
michael@0 | 621 | CairoTextureClientD3D9::Unlock() |
michael@0 | 622 | { |
michael@0 | 623 | MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!"); |
michael@0 | 624 | if (mDrawTarget) { |
michael@0 | 625 | mDrawTarget->Flush(); |
michael@0 | 626 | mDrawTarget = nullptr; |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | if (mLockRect) { |
michael@0 | 630 | mD3D9Surface->UnlockRect(); |
michael@0 | 631 | mLockRect = false; |
michael@0 | 632 | } |
michael@0 | 633 | |
michael@0 | 634 | if (mSurface) { |
michael@0 | 635 | mSurface = nullptr; |
michael@0 | 636 | } |
michael@0 | 637 | mIsLocked = false; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | bool |
michael@0 | 641 | CairoTextureClientD3D9::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) |
michael@0 | 642 | { |
michael@0 | 643 | MOZ_ASSERT(IsValid()); |
michael@0 | 644 | if (!IsAllocated()) { |
michael@0 | 645 | return false; |
michael@0 | 646 | } |
michael@0 | 647 | |
michael@0 | 648 | mTexture->AddRef(); // Release in TextureHostD3D9::TextureHostD3D9 |
michael@0 | 649 | aOutDescriptor = SurfaceDescriptorD3D9(reinterpret_cast<uintptr_t>(mTexture.get())); |
michael@0 | 650 | return true; |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | TemporaryRef<gfx::DrawTarget> |
michael@0 | 654 | CairoTextureClientD3D9::GetAsDrawTarget() |
michael@0 | 655 | { |
michael@0 | 656 | MOZ_ASSERT(mIsLocked && mD3D9Surface); |
michael@0 | 657 | if (mDrawTarget) { |
michael@0 | 658 | return mDrawTarget; |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | if (ContentForFormat(mFormat) == gfxContentType::COLOR) { |
michael@0 | 662 | mSurface = new gfxWindowsSurface(mD3D9Surface); |
michael@0 | 663 | if (!mSurface || mSurface->CairoStatus()) { |
michael@0 | 664 | NS_WARNING("Could not create surface for d3d9 surface"); |
michael@0 | 665 | mSurface = nullptr; |
michael@0 | 666 | return nullptr; |
michael@0 | 667 | } |
michael@0 | 668 | } else { |
michael@0 | 669 | // gfxWindowsSurface don't support transparency so we can't use the d3d9 |
michael@0 | 670 | // windows surface optimization. |
michael@0 | 671 | // Instead we have to use a gfxImageSurface and fallback for font drawing. |
michael@0 | 672 | D3DLOCKED_RECT rect; |
michael@0 | 673 | mD3D9Surface->LockRect(&rect, nullptr, 0); |
michael@0 | 674 | mSurface = new gfxImageSurface((uint8_t*)rect.pBits, ThebesIntSize(mSize), |
michael@0 | 675 | rect.Pitch, SurfaceFormatToImageFormat(mFormat)); |
michael@0 | 676 | mLockRect = true; |
michael@0 | 677 | } |
michael@0 | 678 | |
michael@0 | 679 | mDrawTarget = |
michael@0 | 680 | gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mSurface, mSize); |
michael@0 | 681 | |
michael@0 | 682 | return mDrawTarget; |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | bool |
michael@0 | 686 | CairoTextureClientD3D9::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags) |
michael@0 | 687 | { |
michael@0 | 688 | MOZ_ASSERT(!IsAllocated()); |
michael@0 | 689 | mSize = aSize; |
michael@0 | 690 | _D3DFORMAT format = SurfaceFormatToD3D9Format(mFormat); |
michael@0 | 691 | |
michael@0 | 692 | DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); |
michael@0 | 693 | if (!deviceManager || |
michael@0 | 694 | !(mTexture = deviceManager->CreateTexture(mSize, format, D3DPOOL_SYSTEMMEM, nullptr))) { |
michael@0 | 695 | NS_WARNING("Could not create d3d9 texture"); |
michael@0 | 696 | return false; |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | mNeedsClear = aFlags & ALLOC_CLEAR_BUFFER; |
michael@0 | 700 | |
michael@0 | 701 | MOZ_ASSERT(mTexture); |
michael@0 | 702 | return true; |
michael@0 | 703 | } |
michael@0 | 704 | |
michael@0 | 705 | TextureClientData* |
michael@0 | 706 | CairoTextureClientD3D9::DropTextureData() |
michael@0 | 707 | { |
michael@0 | 708 | TextureClientData* data = new D3D9TextureClientData(mTexture); |
michael@0 | 709 | mTexture = nullptr; |
michael@0 | 710 | MarkInvalid(); |
michael@0 | 711 | return data; |
michael@0 | 712 | } |
michael@0 | 713 | |
michael@0 | 714 | DIBTextureClientD3D9::DIBTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags) |
michael@0 | 715 | : TextureClient(aFlags) |
michael@0 | 716 | , mFormat(aFormat) |
michael@0 | 717 | , mIsLocked(false) |
michael@0 | 718 | { |
michael@0 | 719 | MOZ_COUNT_CTOR(DIBTextureClientD3D9); |
michael@0 | 720 | } |
michael@0 | 721 | |
michael@0 | 722 | DIBTextureClientD3D9::~DIBTextureClientD3D9() |
michael@0 | 723 | { |
michael@0 | 724 | MOZ_COUNT_DTOR(DIBTextureClientD3D9); |
michael@0 | 725 | } |
michael@0 | 726 | |
michael@0 | 727 | bool |
michael@0 | 728 | DIBTextureClientD3D9::Lock(OpenMode) |
michael@0 | 729 | { |
michael@0 | 730 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 731 | if (!IsValid()) { |
michael@0 | 732 | return false; |
michael@0 | 733 | } |
michael@0 | 734 | mIsLocked = true; |
michael@0 | 735 | return true; |
michael@0 | 736 | } |
michael@0 | 737 | |
michael@0 | 738 | void |
michael@0 | 739 | DIBTextureClientD3D9::Unlock() |
michael@0 | 740 | { |
michael@0 | 741 | MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!"); |
michael@0 | 742 | if (mDrawTarget) { |
michael@0 | 743 | mDrawTarget->Flush(); |
michael@0 | 744 | mDrawTarget = nullptr; |
michael@0 | 745 | } |
michael@0 | 746 | |
michael@0 | 747 | mIsLocked = false; |
michael@0 | 748 | } |
michael@0 | 749 | |
michael@0 | 750 | bool |
michael@0 | 751 | DIBTextureClientD3D9::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) |
michael@0 | 752 | { |
michael@0 | 753 | MOZ_ASSERT(IsValid()); |
michael@0 | 754 | if (!IsAllocated()) { |
michael@0 | 755 | return false; |
michael@0 | 756 | } |
michael@0 | 757 | MOZ_ASSERT(mSurface); |
michael@0 | 758 | // The host will release this ref when it receives the surface descriptor. |
michael@0 | 759 | // We AddRef in case we die before the host receives the pointer. |
michael@0 | 760 | aOutDescriptor = SurfaceDescriptorDIB(reinterpret_cast<uintptr_t>(mSurface.get())); |
michael@0 | 761 | mSurface->AddRef(); |
michael@0 | 762 | return true; |
michael@0 | 763 | } |
michael@0 | 764 | |
michael@0 | 765 | TemporaryRef<gfx::DrawTarget> |
michael@0 | 766 | DIBTextureClientD3D9::GetAsDrawTarget() |
michael@0 | 767 | { |
michael@0 | 768 | MOZ_ASSERT(mIsLocked && IsAllocated()); |
michael@0 | 769 | |
michael@0 | 770 | if (!mDrawTarget) { |
michael@0 | 771 | mDrawTarget = |
michael@0 | 772 | gfxPlatform::GetPlatform()->CreateDrawTargetForSurface(mSurface, mSize); |
michael@0 | 773 | } |
michael@0 | 774 | |
michael@0 | 775 | return mDrawTarget; |
michael@0 | 776 | } |
michael@0 | 777 | |
michael@0 | 778 | bool |
michael@0 | 779 | DIBTextureClientD3D9::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags) |
michael@0 | 780 | { |
michael@0 | 781 | MOZ_ASSERT(!IsAllocated()); |
michael@0 | 782 | mSize = aSize; |
michael@0 | 783 | |
michael@0 | 784 | mSurface = new gfxWindowsSurface(gfxIntSize(aSize.width, aSize.height), |
michael@0 | 785 | SurfaceFormatToImageFormat(mFormat)); |
michael@0 | 786 | if (!mSurface || mSurface->CairoStatus()) |
michael@0 | 787 | { |
michael@0 | 788 | NS_WARNING("Could not create surface"); |
michael@0 | 789 | mSurface = nullptr; |
michael@0 | 790 | return false; |
michael@0 | 791 | } |
michael@0 | 792 | |
michael@0 | 793 | return true; |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | TextureClientData* |
michael@0 | 797 | DIBTextureClientD3D9::DropTextureData() |
michael@0 | 798 | { |
michael@0 | 799 | TextureClientData* data = new D3D9TextureClientData(mSurface); |
michael@0 | 800 | mSurface = nullptr; |
michael@0 | 801 | MarkInvalid(); |
michael@0 | 802 | return data; |
michael@0 | 803 | } |
michael@0 | 804 | |
michael@0 | 805 | SharedTextureClientD3D9::SharedTextureClientD3D9(gfx::SurfaceFormat aFormat, TextureFlags aFlags) |
michael@0 | 806 | : TextureClient(aFlags) |
michael@0 | 807 | , mFormat(aFormat) |
michael@0 | 808 | , mHandle(0) |
michael@0 | 809 | , mIsLocked(false) |
michael@0 | 810 | { |
michael@0 | 811 | MOZ_COUNT_CTOR(SharedTextureClientD3D9); |
michael@0 | 812 | } |
michael@0 | 813 | |
michael@0 | 814 | SharedTextureClientD3D9::~SharedTextureClientD3D9() |
michael@0 | 815 | { |
michael@0 | 816 | MOZ_COUNT_DTOR(SharedTextureClientD3D9); |
michael@0 | 817 | } |
michael@0 | 818 | |
michael@0 | 819 | TextureClientData* |
michael@0 | 820 | SharedTextureClientD3D9::DropTextureData() |
michael@0 | 821 | { |
michael@0 | 822 | TextureClientData* data = new D3D9TextureClientData(mTexture); |
michael@0 | 823 | mTexture = nullptr; |
michael@0 | 824 | MarkInvalid(); |
michael@0 | 825 | return data; |
michael@0 | 826 | } |
michael@0 | 827 | |
michael@0 | 828 | bool |
michael@0 | 829 | SharedTextureClientD3D9::Lock(OpenMode) |
michael@0 | 830 | { |
michael@0 | 831 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 832 | if (!IsValid()) { |
michael@0 | 833 | return false; |
michael@0 | 834 | } |
michael@0 | 835 | mIsLocked = true; |
michael@0 | 836 | return true; |
michael@0 | 837 | } |
michael@0 | 838 | |
michael@0 | 839 | void |
michael@0 | 840 | SharedTextureClientD3D9::Unlock() |
michael@0 | 841 | { |
michael@0 | 842 | MOZ_ASSERT(mIsLocked, "Unlock called while the texture is not locked!"); |
michael@0 | 843 | } |
michael@0 | 844 | |
michael@0 | 845 | bool |
michael@0 | 846 | SharedTextureClientD3D9::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor) |
michael@0 | 847 | { |
michael@0 | 848 | MOZ_ASSERT(IsValid()); |
michael@0 | 849 | if (!IsAllocated()) { |
michael@0 | 850 | return false; |
michael@0 | 851 | } |
michael@0 | 852 | |
michael@0 | 853 | aOutDescriptor = SurfaceDescriptorD3D10((WindowsHandle)(mHandle), mFormat, GetSize()); |
michael@0 | 854 | return true; |
michael@0 | 855 | } |
michael@0 | 856 | |
michael@0 | 857 | TextureHostD3D9::TextureHostD3D9(TextureFlags aFlags, |
michael@0 | 858 | const SurfaceDescriptorD3D9& aDescriptor) |
michael@0 | 859 | : TextureHost(aFlags) |
michael@0 | 860 | , mFormat(SurfaceFormat::UNKNOWN) |
michael@0 | 861 | , mIsLocked(false) |
michael@0 | 862 | { |
michael@0 | 863 | mTexture = reinterpret_cast<IDirect3DTexture9*>(aDescriptor.texture()); |
michael@0 | 864 | mTexture->Release(); // see AddRef in CairoTextureClientD3D9::ToSurfaceDescriptor |
michael@0 | 865 | MOZ_ASSERT(mTexture); |
michael@0 | 866 | D3DSURFACE_DESC desc; |
michael@0 | 867 | HRESULT hr = mTexture->GetLevelDesc(0, &desc); |
michael@0 | 868 | if (!FAILED(hr)) { |
michael@0 | 869 | mFormat = D3D9FormatToSurfaceFormat(desc.Format); |
michael@0 | 870 | mSize.width = desc.Width; |
michael@0 | 871 | mSize.height = desc.Height; |
michael@0 | 872 | } |
michael@0 | 873 | } |
michael@0 | 874 | |
michael@0 | 875 | bool |
michael@0 | 876 | DataTextureSourceD3D9::UpdateFromTexture(IDirect3DTexture9* aTexture, |
michael@0 | 877 | const nsIntRegion* aRegion) |
michael@0 | 878 | { |
michael@0 | 879 | MOZ_ASSERT(aTexture); |
michael@0 | 880 | |
michael@0 | 881 | D3DSURFACE_DESC desc; |
michael@0 | 882 | HRESULT hr = aTexture->GetLevelDesc(0, &desc); |
michael@0 | 883 | if (FAILED(hr)) { |
michael@0 | 884 | return false; |
michael@0 | 885 | } else { |
michael@0 | 886 | // If we changed the compositor, the size might have been reset to zero |
michael@0 | 887 | // Otherwise the texture size must not change. |
michael@0 | 888 | MOZ_ASSERT(mFormat == D3D9FormatToSurfaceFormat(desc.Format)); |
michael@0 | 889 | MOZ_ASSERT(!mSize.width || mSize.width == desc.Width); |
michael@0 | 890 | MOZ_ASSERT(!mSize.height || mSize.height == desc.Height); |
michael@0 | 891 | mSize = IntSize(desc.Width, desc.Height); |
michael@0 | 892 | } |
michael@0 | 893 | |
michael@0 | 894 | DeviceManagerD3D9* dm = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); |
michael@0 | 895 | if (!mTexture) { |
michael@0 | 896 | mTexture = dm->CreateTexture(mSize, SurfaceFormatToD3D9Format(mFormat), |
michael@0 | 897 | D3DPOOL_DEFAULT, this); |
michael@0 | 898 | if (!mTexture) { |
michael@0 | 899 | NS_WARNING("Failed to create a texture"); |
michael@0 | 900 | return false; |
michael@0 | 901 | } |
michael@0 | 902 | } |
michael@0 | 903 | |
michael@0 | 904 | RefPtr<IDirect3DSurface9> srcSurface; |
michael@0 | 905 | RefPtr<IDirect3DSurface9> dstSurface; |
michael@0 | 906 | |
michael@0 | 907 | hr = aTexture->GetSurfaceLevel(0, byRef(srcSurface)); |
michael@0 | 908 | if (FAILED(hr)) { |
michael@0 | 909 | return false; |
michael@0 | 910 | } |
michael@0 | 911 | hr = mTexture->GetSurfaceLevel(0, byRef(dstSurface)); |
michael@0 | 912 | if (FAILED(hr)) { |
michael@0 | 913 | return false; |
michael@0 | 914 | } |
michael@0 | 915 | |
michael@0 | 916 | if (aRegion) { |
michael@0 | 917 | nsIntRegionRectIterator iter(*aRegion); |
michael@0 | 918 | const nsIntRect *iterRect; |
michael@0 | 919 | while ((iterRect = iter.Next())) { |
michael@0 | 920 | RECT rect; |
michael@0 | 921 | rect.left = iterRect->x; |
michael@0 | 922 | rect.top = iterRect->y; |
michael@0 | 923 | rect.right = iterRect->XMost(); |
michael@0 | 924 | rect.bottom = iterRect->YMost(); |
michael@0 | 925 | |
michael@0 | 926 | POINT point; |
michael@0 | 927 | point.x = iterRect->x; |
michael@0 | 928 | point.y = iterRect->y; |
michael@0 | 929 | hr = dm->device()->UpdateSurface(srcSurface, &rect, dstSurface, &point); |
michael@0 | 930 | if (FAILED(hr)) { |
michael@0 | 931 | NS_WARNING("Failed Update the surface"); |
michael@0 | 932 | return false; |
michael@0 | 933 | } |
michael@0 | 934 | } |
michael@0 | 935 | } else { |
michael@0 | 936 | hr = dm->device()->UpdateSurface(srcSurface, nullptr, dstSurface, nullptr); |
michael@0 | 937 | if (FAILED(hr)) { |
michael@0 | 938 | NS_WARNING("Failed Update the surface"); |
michael@0 | 939 | return false; |
michael@0 | 940 | } |
michael@0 | 941 | } |
michael@0 | 942 | mIsTiled = false; |
michael@0 | 943 | return true; |
michael@0 | 944 | } |
michael@0 | 945 | |
michael@0 | 946 | void |
michael@0 | 947 | TextureHostD3D9::Updated(const nsIntRegion* aRegion) |
michael@0 | 948 | { |
michael@0 | 949 | if (!mTexture) { |
michael@0 | 950 | return; |
michael@0 | 951 | } |
michael@0 | 952 | |
michael@0 | 953 | if (!mTextureSource) { |
michael@0 | 954 | mTextureSource = new DataTextureSourceD3D9(mFormat, mSize, mCompositor, |
michael@0 | 955 | nullptr, mFlags); |
michael@0 | 956 | } |
michael@0 | 957 | |
michael@0 | 958 | mTextureSource->UpdateFromTexture(mTexture, aRegion); |
michael@0 | 959 | } |
michael@0 | 960 | |
michael@0 | 961 | IDirect3DDevice9* |
michael@0 | 962 | TextureHostD3D9::GetDevice() |
michael@0 | 963 | { |
michael@0 | 964 | return mCompositor ? mCompositor->device() : nullptr; |
michael@0 | 965 | } |
michael@0 | 966 | |
michael@0 | 967 | void |
michael@0 | 968 | TextureHostD3D9::SetCompositor(Compositor* aCompositor) |
michael@0 | 969 | { |
michael@0 | 970 | mCompositor = static_cast<CompositorD3D9*>(aCompositor); |
michael@0 | 971 | if (mTextureSource) { |
michael@0 | 972 | mTextureSource->SetCompositor(aCompositor); |
michael@0 | 973 | } |
michael@0 | 974 | } |
michael@0 | 975 | |
michael@0 | 976 | NewTextureSource* |
michael@0 | 977 | TextureHostD3D9::GetTextureSources() |
michael@0 | 978 | { |
michael@0 | 979 | MOZ_ASSERT(mIsLocked); |
michael@0 | 980 | return mTextureSource; |
michael@0 | 981 | } |
michael@0 | 982 | |
michael@0 | 983 | bool |
michael@0 | 984 | TextureHostD3D9::Lock() |
michael@0 | 985 | { |
michael@0 | 986 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 987 | mIsLocked = true; |
michael@0 | 988 | return true; |
michael@0 | 989 | } |
michael@0 | 990 | |
michael@0 | 991 | void |
michael@0 | 992 | TextureHostD3D9::Unlock() |
michael@0 | 993 | { |
michael@0 | 994 | MOZ_ASSERT(mIsLocked); |
michael@0 | 995 | mIsLocked = false; |
michael@0 | 996 | } |
michael@0 | 997 | |
michael@0 | 998 | void |
michael@0 | 999 | TextureHostD3D9::DeallocateDeviceData() |
michael@0 | 1000 | { |
michael@0 | 1001 | mTextureSource = nullptr; |
michael@0 | 1002 | } |
michael@0 | 1003 | |
michael@0 | 1004 | DIBTextureHostD3D9::DIBTextureHostD3D9(TextureFlags aFlags, |
michael@0 | 1005 | const SurfaceDescriptorDIB& aDescriptor) |
michael@0 | 1006 | : TextureHost(aFlags) |
michael@0 | 1007 | , mIsLocked(false) |
michael@0 | 1008 | { |
michael@0 | 1009 | // We added an extra ref for transport, so we shouldn't AddRef now. |
michael@0 | 1010 | mSurface = |
michael@0 | 1011 | dont_AddRef(reinterpret_cast<gfxWindowsSurface*>(aDescriptor.surface())); |
michael@0 | 1012 | MOZ_ASSERT(mSurface); |
michael@0 | 1013 | |
michael@0 | 1014 | mSize = ToIntSize(mSurface->GetSize()); |
michael@0 | 1015 | mFormat = ImageFormatToSurfaceFormat( |
michael@0 | 1016 | gfxPlatform::GetPlatform()->OptimalFormatForContent(mSurface->GetContentType())); |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 | NewTextureSource* |
michael@0 | 1020 | DIBTextureHostD3D9::GetTextureSources() |
michael@0 | 1021 | { |
michael@0 | 1022 | if (!mTextureSource) { |
michael@0 | 1023 | Updated(); |
michael@0 | 1024 | } |
michael@0 | 1025 | |
michael@0 | 1026 | return mTextureSource; |
michael@0 | 1027 | } |
michael@0 | 1028 | |
michael@0 | 1029 | void |
michael@0 | 1030 | DIBTextureHostD3D9::Updated(const nsIntRegion*) |
michael@0 | 1031 | { |
michael@0 | 1032 | if (!mTextureSource) { |
michael@0 | 1033 | mTextureSource = new DataTextureSourceD3D9(mFormat, mCompositor, mFlags); |
michael@0 | 1034 | } |
michael@0 | 1035 | |
michael@0 | 1036 | if (!mTextureSource->Update(mSurface)) { |
michael@0 | 1037 | mTextureSource = nullptr; |
michael@0 | 1038 | } |
michael@0 | 1039 | } |
michael@0 | 1040 | |
michael@0 | 1041 | bool |
michael@0 | 1042 | DIBTextureHostD3D9::Lock() |
michael@0 | 1043 | { |
michael@0 | 1044 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 1045 | mIsLocked = true; |
michael@0 | 1046 | return true; |
michael@0 | 1047 | } |
michael@0 | 1048 | |
michael@0 | 1049 | void |
michael@0 | 1050 | DIBTextureHostD3D9::Unlock() |
michael@0 | 1051 | { |
michael@0 | 1052 | MOZ_ASSERT(mIsLocked); |
michael@0 | 1053 | mIsLocked = false; |
michael@0 | 1054 | } |
michael@0 | 1055 | |
michael@0 | 1056 | void |
michael@0 | 1057 | DIBTextureHostD3D9::SetCompositor(Compositor* aCompositor) |
michael@0 | 1058 | { |
michael@0 | 1059 | mCompositor = static_cast<CompositorD3D9*>(aCompositor); |
michael@0 | 1060 | } |
michael@0 | 1061 | |
michael@0 | 1062 | void |
michael@0 | 1063 | DIBTextureHostD3D9::DeallocateDeviceData() |
michael@0 | 1064 | { |
michael@0 | 1065 | mTextureSource = nullptr; |
michael@0 | 1066 | } |
michael@0 | 1067 | |
michael@0 | 1068 | DXGITextureHostD3D9::DXGITextureHostD3D9(TextureFlags aFlags, |
michael@0 | 1069 | const SurfaceDescriptorD3D10& aDescriptor) |
michael@0 | 1070 | : TextureHost(aFlags) |
michael@0 | 1071 | , mHandle(aDescriptor.handle()) |
michael@0 | 1072 | , mFormat(aDescriptor.format()) |
michael@0 | 1073 | , mSize(aDescriptor.size()) |
michael@0 | 1074 | , mIsLocked(false) |
michael@0 | 1075 | { |
michael@0 | 1076 | MOZ_ASSERT(mHandle); |
michael@0 | 1077 | } |
michael@0 | 1078 | |
michael@0 | 1079 | NewTextureSource* |
michael@0 | 1080 | DXGITextureHostD3D9::GetTextureSources() |
michael@0 | 1081 | { |
michael@0 | 1082 | return mTextureSource; |
michael@0 | 1083 | } |
michael@0 | 1084 | |
michael@0 | 1085 | bool |
michael@0 | 1086 | DXGITextureHostD3D9::Lock() |
michael@0 | 1087 | { |
michael@0 | 1088 | DeviceManagerD3D9* deviceManager = gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(); |
michael@0 | 1089 | if (!deviceManager) { |
michael@0 | 1090 | NS_WARNING("trying to lock a TextureHost without a D3D device"); |
michael@0 | 1091 | return false; |
michael@0 | 1092 | } |
michael@0 | 1093 | |
michael@0 | 1094 | if (!mTextureSource) { |
michael@0 | 1095 | nsRefPtr<IDirect3DTexture9> tex; |
michael@0 | 1096 | HRESULT hr = deviceManager->device()->CreateTexture(mSize.width, |
michael@0 | 1097 | mSize.height, |
michael@0 | 1098 | 1, |
michael@0 | 1099 | D3DUSAGE_RENDERTARGET, |
michael@0 | 1100 | SurfaceFormatToD3D9Format(mFormat), |
michael@0 | 1101 | D3DPOOL_DEFAULT, |
michael@0 | 1102 | getter_AddRefs(tex), |
michael@0 | 1103 | (HANDLE*)&mHandle); |
michael@0 | 1104 | if (FAILED(hr)) { |
michael@0 | 1105 | NS_WARNING("Failed to open shared texture"); |
michael@0 | 1106 | return false; |
michael@0 | 1107 | } |
michael@0 | 1108 | |
michael@0 | 1109 | mTextureSource = new DataTextureSourceD3D9(mFormat, mSize, mCompositor, tex); |
michael@0 | 1110 | } |
michael@0 | 1111 | |
michael@0 | 1112 | MOZ_ASSERT(!mIsLocked); |
michael@0 | 1113 | mIsLocked = true; |
michael@0 | 1114 | return true; |
michael@0 | 1115 | } |
michael@0 | 1116 | |
michael@0 | 1117 | void |
michael@0 | 1118 | DXGITextureHostD3D9::Unlock() |
michael@0 | 1119 | { |
michael@0 | 1120 | MOZ_ASSERT(mIsLocked); |
michael@0 | 1121 | mIsLocked = false; |
michael@0 | 1122 | } |
michael@0 | 1123 | |
michael@0 | 1124 | void |
michael@0 | 1125 | DXGITextureHostD3D9::SetCompositor(Compositor* aCompositor) |
michael@0 | 1126 | { |
michael@0 | 1127 | mCompositor = static_cast<CompositorD3D9*>(aCompositor); |
michael@0 | 1128 | } |
michael@0 | 1129 | |
michael@0 | 1130 | void |
michael@0 | 1131 | DXGITextureHostD3D9::DeallocateDeviceData() |
michael@0 | 1132 | { |
michael@0 | 1133 | mTextureSource = nullptr; |
michael@0 | 1134 | } |
michael@0 | 1135 | |
michael@0 | 1136 | } |
michael@0 | 1137 | } |