gfx/layers/d3d11/TextureD3D11.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 "TextureD3D11.h"
michael@0 7 #include "CompositorD3D11.h"
michael@0 8 #include "gfxContext.h"
michael@0 9 #include "Effects.h"
michael@0 10 #include "mozilla/layers/YCbCrImageDataSerializer.h"
michael@0 11 #include "gfxWindowsPlatform.h"
michael@0 12 #include "gfxD2DSurface.h"
michael@0 13 #include "gfx2DGlue.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 using namespace gfx;
michael@0 18
michael@0 19 namespace layers {
michael@0 20
michael@0 21 static DXGI_FORMAT
michael@0 22 SurfaceFormatToDXGIFormat(gfx::SurfaceFormat aFormat)
michael@0 23 {
michael@0 24 switch (aFormat) {
michael@0 25 case SurfaceFormat::B8G8R8A8:
michael@0 26 return DXGI_FORMAT_B8G8R8A8_UNORM;
michael@0 27 case SurfaceFormat::B8G8R8X8:
michael@0 28 return DXGI_FORMAT_B8G8R8A8_UNORM;
michael@0 29 case SurfaceFormat::A8:
michael@0 30 return DXGI_FORMAT_A8_UNORM;
michael@0 31 default:
michael@0 32 MOZ_ASSERT(false, "unsupported format");
michael@0 33 return DXGI_FORMAT_UNKNOWN;
michael@0 34 }
michael@0 35 }
michael@0 36
michael@0 37 static uint32_t
michael@0 38 GetRequiredTilesD3D11(uint32_t aSize, uint32_t aMaxSize)
michael@0 39 {
michael@0 40 uint32_t requiredTiles = aSize / aMaxSize;
michael@0 41 if (aSize % aMaxSize) {
michael@0 42 requiredTiles++;
michael@0 43 }
michael@0 44 return requiredTiles;
michael@0 45 }
michael@0 46
michael@0 47 static IntRect
michael@0 48 GetTileRectD3D11(uint32_t aID, IntSize aSize, uint32_t aMaxSize)
michael@0 49 {
michael@0 50 uint32_t horizontalTiles = GetRequiredTilesD3D11(aSize.width, aMaxSize);
michael@0 51 uint32_t verticalTiles = GetRequiredTilesD3D11(aSize.height, aMaxSize);
michael@0 52
michael@0 53 uint32_t verticalTile = aID / horizontalTiles;
michael@0 54 uint32_t horizontalTile = aID % horizontalTiles;
michael@0 55
michael@0 56 return IntRect(horizontalTile * aMaxSize,
michael@0 57 verticalTile * aMaxSize,
michael@0 58 horizontalTile < (horizontalTiles - 1) ? aMaxSize : aSize.width % aMaxSize,
michael@0 59 verticalTile < (verticalTiles - 1) ? aMaxSize : aSize.height % aMaxSize);
michael@0 60 }
michael@0 61
michael@0 62 DataTextureSourceD3D11::DataTextureSourceD3D11(SurfaceFormat aFormat,
michael@0 63 CompositorD3D11* aCompositor,
michael@0 64 TextureFlags aFlags)
michael@0 65 : mCompositor(aCompositor)
michael@0 66 , mFormat(aFormat)
michael@0 67 , mFlags(aFlags)
michael@0 68 , mCurrentTile(0)
michael@0 69 , mIsTiled(false)
michael@0 70 , mIterating(false)
michael@0 71 {
michael@0 72 MOZ_COUNT_CTOR(DataTextureSourceD3D11);
michael@0 73 }
michael@0 74
michael@0 75 DataTextureSourceD3D11::DataTextureSourceD3D11(SurfaceFormat aFormat,
michael@0 76 CompositorD3D11* aCompositor,
michael@0 77 ID3D11Texture2D* aTexture)
michael@0 78 : mCompositor(aCompositor)
michael@0 79 , mFormat(aFormat)
michael@0 80 , mFlags(0)
michael@0 81 , mCurrentTile(0)
michael@0 82 , mIsTiled(false)
michael@0 83 , mIterating(false)
michael@0 84 {
michael@0 85 MOZ_COUNT_CTOR(DataTextureSourceD3D11);
michael@0 86
michael@0 87 mTexture = aTexture;
michael@0 88 D3D11_TEXTURE2D_DESC desc;
michael@0 89 aTexture->GetDesc(&desc);
michael@0 90
michael@0 91 mSize = IntSize(desc.Width, desc.Height);
michael@0 92 }
michael@0 93
michael@0 94
michael@0 95
michael@0 96 DataTextureSourceD3D11::~DataTextureSourceD3D11()
michael@0 97 {
michael@0 98 MOZ_COUNT_DTOR(DataTextureSourceD3D11);
michael@0 99 }
michael@0 100
michael@0 101
michael@0 102 template<typename T> // ID3D10Texture2D or ID3D11Texture2D
michael@0 103 static void LockD3DTexture(T* aTexture)
michael@0 104 {
michael@0 105 MOZ_ASSERT(aTexture);
michael@0 106 RefPtr<IDXGIKeyedMutex> mutex;
michael@0 107 aTexture->QueryInterface((IDXGIKeyedMutex**)byRef(mutex));
michael@0 108 if (mutex) {
michael@0 109 mutex->AcquireSync(0, INFINITE);
michael@0 110 }
michael@0 111 }
michael@0 112
michael@0 113 template<typename T> // ID3D10Texture2D or ID3D11Texture2D
michael@0 114 static void UnlockD3DTexture(T* aTexture)
michael@0 115 {
michael@0 116 MOZ_ASSERT(aTexture);
michael@0 117 RefPtr<IDXGIKeyedMutex> mutex;
michael@0 118 aTexture->QueryInterface((IDXGIKeyedMutex**)byRef(mutex));
michael@0 119 if (mutex) {
michael@0 120 mutex->ReleaseSync(0);
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 TemporaryRef<TextureHost>
michael@0 125 CreateTextureHostD3D11(const SurfaceDescriptor& aDesc,
michael@0 126 ISurfaceAllocator* aDeallocator,
michael@0 127 TextureFlags aFlags)
michael@0 128 {
michael@0 129 RefPtr<TextureHost> result;
michael@0 130 switch (aDesc.type()) {
michael@0 131 case SurfaceDescriptor::TSurfaceDescriptorShmem:
michael@0 132 case SurfaceDescriptor::TSurfaceDescriptorMemory: {
michael@0 133 result = CreateBackendIndependentTextureHost(aDesc, aDeallocator, aFlags);
michael@0 134 break;
michael@0 135 }
michael@0 136 case SurfaceDescriptor::TSurfaceDescriptorD3D10: {
michael@0 137 result = new DXGITextureHostD3D11(aFlags,
michael@0 138 aDesc.get_SurfaceDescriptorD3D10());
michael@0 139 break;
michael@0 140 }
michael@0 141 default: {
michael@0 142 NS_WARNING("Unsupported SurfaceDescriptor type");
michael@0 143 }
michael@0 144 }
michael@0 145 return result;
michael@0 146 }
michael@0 147
michael@0 148 TextureClientD3D11::TextureClientD3D11(gfx::SurfaceFormat aFormat, TextureFlags aFlags)
michael@0 149 : TextureClient(aFlags)
michael@0 150 , mFormat(aFormat)
michael@0 151 , mIsLocked(false)
michael@0 152 , mNeedsClear(false)
michael@0 153 {}
michael@0 154
michael@0 155 TextureClientD3D11::~TextureClientD3D11()
michael@0 156 {
michael@0 157 #ifdef DEBUG
michael@0 158 // An Azure DrawTarget needs to be locked when it gets nullptr'ed as this is
michael@0 159 // when it calls EndDraw. This EndDraw should not execute anything so it
michael@0 160 // shouldn't -really- need the lock but the debug layer chokes on this.
michael@0 161 if (mDrawTarget) {
michael@0 162 MOZ_ASSERT(!mIsLocked);
michael@0 163 MOZ_ASSERT(mTexture);
michael@0 164 MOZ_ASSERT(mDrawTarget->refCount() == 1);
michael@0 165 LockD3DTexture(mTexture.get());
michael@0 166 mDrawTarget = nullptr;
michael@0 167 UnlockD3DTexture(mTexture.get());
michael@0 168 }
michael@0 169 #endif
michael@0 170 }
michael@0 171
michael@0 172 bool
michael@0 173 TextureClientD3D11::Lock(OpenMode aMode)
michael@0 174 {
michael@0 175 if (!mTexture) {
michael@0 176 return false;
michael@0 177 }
michael@0 178 MOZ_ASSERT(!mIsLocked, "The Texture is already locked!");
michael@0 179 LockD3DTexture(mTexture.get());
michael@0 180 mIsLocked = true;
michael@0 181
michael@0 182 if (mNeedsClear) {
michael@0 183 mDrawTarget = GetAsDrawTarget();
michael@0 184 mDrawTarget->ClearRect(Rect(0, 0, GetSize().width, GetSize().height));
michael@0 185 mNeedsClear = false;
michael@0 186 }
michael@0 187
michael@0 188 return true;
michael@0 189 }
michael@0 190
michael@0 191 void
michael@0 192 TextureClientD3D11::Unlock()
michael@0 193 {
michael@0 194 MOZ_ASSERT(mIsLocked, "Unlocked called while the texture is not locked!");
michael@0 195
michael@0 196 if (mDrawTarget) {
michael@0 197 // see the comment on TextureClient::GetAsDrawTarget.
michael@0 198 // This DrawTarget is internal to the TextureClient and is only exposed to the
michael@0 199 // outside world between Lock() and Unlock(). This assertion checks that no outside
michael@0 200 // reference remains by the time Unlock() is called.
michael@0 201 MOZ_ASSERT(mDrawTarget->refCount() == 1);
michael@0 202 mDrawTarget->Flush();
michael@0 203 }
michael@0 204
michael@0 205 // The DrawTarget is created only once, and is only usable between calls
michael@0 206 // to Lock and Unlock.
michael@0 207 UnlockD3DTexture(mTexture.get());
michael@0 208 mIsLocked = false;
michael@0 209 }
michael@0 210
michael@0 211 TemporaryRef<DrawTarget>
michael@0 212 TextureClientD3D11::GetAsDrawTarget()
michael@0 213 {
michael@0 214 MOZ_ASSERT(mIsLocked, "Calling TextureClient::GetAsDrawTarget without locking :(");
michael@0 215
michael@0 216 if (!mTexture) {
michael@0 217 return nullptr;
michael@0 218 }
michael@0 219
michael@0 220 if (mDrawTarget) {
michael@0 221 return mDrawTarget;
michael@0 222 }
michael@0 223
michael@0 224 mDrawTarget = Factory::CreateDrawTargetForD3D10Texture(mTexture, mFormat);
michael@0 225 return mDrawTarget;
michael@0 226 }
michael@0 227
michael@0 228 bool
michael@0 229 TextureClientD3D11::AllocateForSurface(gfx::IntSize aSize, TextureAllocationFlags aFlags)
michael@0 230 {
michael@0 231 mSize = aSize;
michael@0 232 ID3D10Device* device = gfxWindowsPlatform::GetPlatform()->GetD3D10Device();
michael@0 233
michael@0 234 CD3D10_TEXTURE2D_DESC newDesc(DXGI_FORMAT_B8G8R8A8_UNORM,
michael@0 235 aSize.width, aSize.height, 1, 1,
michael@0 236 D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE);
michael@0 237
michael@0 238 newDesc.MiscFlags = D3D10_RESOURCE_MISC_SHARED_KEYEDMUTEX;
michael@0 239
michael@0 240 HRESULT hr = device->CreateTexture2D(&newDesc, nullptr, byRef(mTexture));
michael@0 241
michael@0 242 if (FAILED(hr)) {
michael@0 243 LOGD3D11("Error creating texture for client!");
michael@0 244 return false;
michael@0 245 }
michael@0 246
michael@0 247 // Defer clearing to the next time we lock to avoid an extra (expensive) lock.
michael@0 248 mNeedsClear = aFlags & ALLOC_CLEAR_BUFFER;
michael@0 249
michael@0 250 return true;
michael@0 251 }
michael@0 252
michael@0 253 bool
michael@0 254 TextureClientD3D11::ToSurfaceDescriptor(SurfaceDescriptor& aOutDescriptor)
michael@0 255 {
michael@0 256 if (!IsAllocated()) {
michael@0 257 return false;
michael@0 258 }
michael@0 259
michael@0 260 RefPtr<IDXGIResource> resource;
michael@0 261 mTexture->QueryInterface((IDXGIResource**)byRef(resource));
michael@0 262 HANDLE sharedHandle;
michael@0 263 HRESULT hr = resource->GetSharedHandle(&sharedHandle);
michael@0 264
michael@0 265 if (FAILED(hr)) {
michael@0 266 LOGD3D11("Error getting shared handle for texture.");
michael@0 267 return false;
michael@0 268 }
michael@0 269
michael@0 270 aOutDescriptor = SurfaceDescriptorD3D10((WindowsHandle)sharedHandle, mFormat, mSize);
michael@0 271 return true;
michael@0 272 }
michael@0 273
michael@0 274 DXGITextureHostD3D11::DXGITextureHostD3D11(TextureFlags aFlags,
michael@0 275 const SurfaceDescriptorD3D10& aDescriptor)
michael@0 276 : TextureHost(aFlags)
michael@0 277 , mHandle(aDescriptor.handle())
michael@0 278 , mFormat(aDescriptor.format())
michael@0 279 , mIsLocked(false)
michael@0 280 {}
michael@0 281
michael@0 282 ID3D11Device*
michael@0 283 DXGITextureHostD3D11::GetDevice()
michael@0 284 {
michael@0 285 return mCompositor ? mCompositor->GetDevice() : nullptr;
michael@0 286 }
michael@0 287
michael@0 288 void
michael@0 289 DXGITextureHostD3D11::SetCompositor(Compositor* aCompositor)
michael@0 290 {
michael@0 291 mCompositor = static_cast<CompositorD3D11*>(aCompositor);
michael@0 292 }
michael@0 293
michael@0 294 bool
michael@0 295 DXGITextureHostD3D11::Lock()
michael@0 296 {
michael@0 297 if (!GetDevice()) {
michael@0 298 NS_WARNING("trying to lock a TextureHost without a D3D device");
michael@0 299 return false;
michael@0 300 }
michael@0 301 if (!mTextureSource) {
michael@0 302 RefPtr<ID3D11Texture2D> tex;
michael@0 303 HRESULT hr = GetDevice()->OpenSharedResource((HANDLE)mHandle,
michael@0 304 __uuidof(ID3D11Texture2D),
michael@0 305 (void**)(ID3D11Texture2D**)byRef(tex));
michael@0 306 if (FAILED(hr)) {
michael@0 307 NS_WARNING("Failed to open shared texture");
michael@0 308 return false;
michael@0 309 }
michael@0 310
michael@0 311 mTextureSource = new DataTextureSourceD3D11(mFormat, mCompositor, tex);
michael@0 312 D3D11_TEXTURE2D_DESC desc;
michael@0 313 tex->GetDesc(&desc);
michael@0 314 mSize = IntSize(desc.Width, desc.Height);
michael@0 315 }
michael@0 316
michael@0 317 LockD3DTexture(mTextureSource->GetD3D11Texture());
michael@0 318
michael@0 319 mIsLocked = true;
michael@0 320 return true;
michael@0 321 }
michael@0 322
michael@0 323 void
michael@0 324 DXGITextureHostD3D11::Unlock()
michael@0 325 {
michael@0 326 MOZ_ASSERT(mIsLocked);
michael@0 327 UnlockD3DTexture(mTextureSource->GetD3D11Texture());
michael@0 328 mIsLocked = false;
michael@0 329 }
michael@0 330
michael@0 331 NewTextureSource*
michael@0 332 DXGITextureHostD3D11::GetTextureSources()
michael@0 333 {
michael@0 334 return mTextureSource.get();
michael@0 335 }
michael@0 336
michael@0 337 bool
michael@0 338 DataTextureSourceD3D11::Update(DataSourceSurface* aSurface,
michael@0 339 nsIntRegion* aDestRegion,
michael@0 340 IntPoint* aSrcOffset)
michael@0 341 {
michael@0 342 // Right now we only support full surface update. If aDestRegion is provided,
michael@0 343 // It will be ignored. Incremental update with a source offset is only used
michael@0 344 // on Mac so it is not clear that we ever will need to support it for D3D.
michael@0 345 MOZ_ASSERT(!aSrcOffset);
michael@0 346 MOZ_ASSERT(aSurface);
michael@0 347
michael@0 348 if (!mCompositor || !mCompositor->GetDevice()) {
michael@0 349 return false;
michael@0 350 }
michael@0 351
michael@0 352 uint32_t bpp = BytesPerPixel(aSurface->GetFormat());
michael@0 353 DXGI_FORMAT dxgiFormat = SurfaceFormatToDXGIFormat(aSurface->GetFormat());
michael@0 354
michael@0 355 mSize = aSurface->GetSize();
michael@0 356 mFormat = aSurface->GetFormat();
michael@0 357
michael@0 358 CD3D11_TEXTURE2D_DESC desc(dxgiFormat, mSize.width, mSize.height,
michael@0 359 1, 1, D3D11_BIND_SHADER_RESOURCE,
michael@0 360 D3D11_USAGE_IMMUTABLE);
michael@0 361
michael@0 362 int32_t maxSize = mCompositor->GetMaxTextureSize();
michael@0 363 if ((mSize.width <= maxSize && mSize.height <= maxSize) ||
michael@0 364 (mFlags & TEXTURE_DISALLOW_BIGIMAGE)) {
michael@0 365 D3D11_SUBRESOURCE_DATA initData;
michael@0 366 initData.pSysMem = aSurface->GetData();
michael@0 367 initData.SysMemPitch = aSurface->Stride();
michael@0 368
michael@0 369 mCompositor->GetDevice()->CreateTexture2D(&desc, &initData, byRef(mTexture));
michael@0 370 mIsTiled = false;
michael@0 371 if (!mTexture) {
michael@0 372 Reset();
michael@0 373 return false;
michael@0 374 }
michael@0 375 } else {
michael@0 376 mIsTiled = true;
michael@0 377 uint32_t tileCount = GetRequiredTilesD3D11(mSize.width, maxSize) *
michael@0 378 GetRequiredTilesD3D11(mSize.height, maxSize);
michael@0 379
michael@0 380 mTileTextures.resize(tileCount);
michael@0 381 mTexture = nullptr;
michael@0 382
michael@0 383 for (uint32_t i = 0; i < tileCount; i++) {
michael@0 384 IntRect tileRect = GetTileRect(i);
michael@0 385
michael@0 386 desc.Width = tileRect.width;
michael@0 387 desc.Height = tileRect.height;
michael@0 388
michael@0 389 D3D11_SUBRESOURCE_DATA initData;
michael@0 390 initData.pSysMem = aSurface->GetData() +
michael@0 391 tileRect.y * aSurface->Stride() +
michael@0 392 tileRect.x * bpp;
michael@0 393 initData.SysMemPitch = aSurface->Stride();
michael@0 394
michael@0 395 mCompositor->GetDevice()->CreateTexture2D(&desc, &initData, byRef(mTileTextures[i]));
michael@0 396 if (!mTileTextures[i]) {
michael@0 397 Reset();
michael@0 398 return false;
michael@0 399 }
michael@0 400 }
michael@0 401 }
michael@0 402 return true;
michael@0 403 }
michael@0 404
michael@0 405 ID3D11Texture2D*
michael@0 406 DataTextureSourceD3D11::GetD3D11Texture() const
michael@0 407 {
michael@0 408 return mIterating ? mTileTextures[mCurrentTile]
michael@0 409 : mTexture;
michael@0 410 }
michael@0 411
michael@0 412 void
michael@0 413 DataTextureSourceD3D11::Reset()
michael@0 414 {
michael@0 415 mTexture = nullptr;
michael@0 416 mTileTextures.resize(0);
michael@0 417 mIsTiled = false;
michael@0 418 mSize.width = 0;
michael@0 419 mSize.height = 0;
michael@0 420 }
michael@0 421
michael@0 422 IntRect
michael@0 423 DataTextureSourceD3D11::GetTileRect(uint32_t aIndex) const
michael@0 424 {
michael@0 425 return GetTileRectD3D11(aIndex, mSize, mCompositor->GetMaxTextureSize());
michael@0 426 }
michael@0 427
michael@0 428 nsIntRect
michael@0 429 DataTextureSourceD3D11::GetTileRect()
michael@0 430 {
michael@0 431 IntRect rect = GetTileRect(mCurrentTile);
michael@0 432 return nsIntRect(rect.x, rect.y, rect.width, rect.height);
michael@0 433 }
michael@0 434
michael@0 435 void
michael@0 436 DataTextureSourceD3D11::SetCompositor(Compositor* aCompositor)
michael@0 437 {
michael@0 438 CompositorD3D11* d3dCompositor = static_cast<CompositorD3D11*>(aCompositor);
michael@0 439 if (mCompositor && mCompositor != d3dCompositor) {
michael@0 440 Reset();
michael@0 441 }
michael@0 442 mCompositor = d3dCompositor;
michael@0 443 }
michael@0 444
michael@0 445 CompositingRenderTargetD3D11::CompositingRenderTargetD3D11(ID3D11Texture2D* aTexture,
michael@0 446 const gfx::IntPoint& aOrigin)
michael@0 447 : CompositingRenderTarget(aOrigin)
michael@0 448 {
michael@0 449 MOZ_ASSERT(aTexture);
michael@0 450
michael@0 451 mTexture = aTexture;
michael@0 452
michael@0 453 RefPtr<ID3D11Device> device;
michael@0 454 mTexture->GetDevice(byRef(device));
michael@0 455
michael@0 456 HRESULT hr = device->CreateRenderTargetView(mTexture, nullptr, byRef(mRTView));
michael@0 457
michael@0 458 if (FAILED(hr)) {
michael@0 459 LOGD3D11("Failed to create RenderTargetView.");
michael@0 460 }
michael@0 461 }
michael@0 462
michael@0 463 IntSize
michael@0 464 CompositingRenderTargetD3D11::GetSize() const
michael@0 465 {
michael@0 466 return TextureSourceD3D11::GetSize();
michael@0 467 }
michael@0 468
michael@0 469 }
michael@0 470 }

mercurial