bool SourceSurfaceD2D::InitFromData(unsigned char *aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat, ID2D1RenderTarget *aRT) { HRESULT hr; mFormat = aFormat; mSize = aSize; if ((uint32_t)aSize.width > aRT->GetMaximumBitmapSize() || (uint32_t)aSize.height > aRT->GetMaximumBitmapSize()) { gfxDebug() << "Bitmap does not fit in texture."; return false; } D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(aFormat), AlphaMode(aFormat))); hr = aRT->CreateBitmap(D2DIntSize(aSize), aData, aStride, props, byRef(mBitmap)); if (FAILED(hr)) { gfxWarning() << "Failed to create D2D Bitmap for data. Code: " << hr; return false; } DrawTargetD2D::mVRAMUsageSS += GetByteSize(); mDevice = Factory::GetDirect3D10Device(); return true; }
bool SourceSurfaceD2D::InitFromTexture(ID3D10Texture2D *aTexture, SurfaceFormat aFormat, ID2D1RenderTarget *aRT) { HRESULT hr; RefPtr<IDXGISurface> surf; hr = aTexture->QueryInterface((IDXGISurface**)&surf); if (FAILED(hr)) { gfxWarning() << "Failed to QI texture to surface. Code: " << hr; return false; } D3D10_TEXTURE2D_DESC desc; aTexture->GetDesc(&desc); mSize = IntSize(desc.Width, desc.Height); mFormat = aFormat; D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(aFormat), AlphaMode(aFormat))); hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap)); if (FAILED(hr)) { gfxWarning() << "Failed to create SharedBitmap. Code: " << hr; return false; } DrawTargetD2D::mVRAMUsageSS += GetByteSize(); return true; }
ID2D1Bitmap* SourceSurfaceD2DTarget::GetBitmap(ID2D1RenderTarget *aRT) { if (mBitmap) { return mBitmap; } HRESULT hr; D3D10_TEXTURE2D_DESC desc; mTexture->GetDesc(&desc); IntSize size(desc.Width, desc.Height); RefPtr<IDXGISurface> surf; hr = mTexture->QueryInterface((IDXGISurface**)byRef(surf)); if (FAILED(hr)) { gfxWarning() << "Failed to query interface texture to DXGISurface. Code: " << hr; return NULL; } D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(mFormat), AlphaMode(mFormat))); hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap)); if (FAILED(hr)) { // This seems to happen for FORMAT_A8 sometimes... aRT->CreateBitmap(D2D1::SizeU(desc.Width, desc.Height), D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(mFormat), AlphaMode(mFormat))), byRef(mBitmap)); if (mDrawTarget) { mBitmap->CopyFromRenderTarget(NULL, mDrawTarget->mRT, NULL); return mBitmap; } gfxWarning() << "Failed to create shared bitmap for DrawTarget snapshot. Code: " << hr; return NULL; } return mBitmap; }
ID2D1Bitmap* SourceSurfaceD2DTarget::GetBitmap(ID2D1RenderTarget *aRT) { if (mBitmap) { return mBitmap; } HRESULT hr; D3D10_TEXTURE2D_DESC desc; mTexture->GetDesc(&desc); IntSize size(desc.Width, desc.Height); RefPtr<IDXGISurface> surf; hr = mTexture->QueryInterface((IDXGISurface**)byRef(surf)); if (FAILED(hr)) { gfxWarning() << "Failed to query interface texture to DXGISurface. Code: " << hr; return NULL; } D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(mFormat), AlphaMode(mFormat))); hr = aRT->CreateSharedBitmap(IID_IDXGISurface, surf, &props, byRef(mBitmap)); if (FAILED(hr)) { // This seems to happen for FORMAT_A8 sometimes... aRT->CreateBitmap(D2D1::SizeU(desc.Width, desc.Height), D2D1::BitmapProperties(D2D1::PixelFormat(DXGIFormat(mFormat), AlphaMode(mFormat))), byRef(mBitmap)); RefPtr<ID2D1RenderTarget> rt; if (mDrawTarget) { rt = mDrawTarget->mRT; } if (!rt) { // Okay, we already separated from our drawtarget. And we're an A8 // surface the only way we can get to a bitmap is by creating a // a rendertarget and from there copying to a bitmap! Terrible! RefPtr<IDXGISurface> surface; hr = mTexture->QueryInterface((IDXGISurface**)byRef(surface)); if (FAILED(hr)) { gfxWarning() << "Failed to QI texture to surface."; return NULL; } D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGIFormat(mFormat), AlphaMode(mFormat))); hr = DrawTargetD2D::factory()->CreateDxgiSurfaceRenderTarget(surface, props, byRef(rt)); if (FAILED(hr)) { gfxWarning() << "Failed to create D2D render target for texture."; return NULL; } } mBitmap->CopyFromRenderTarget(NULL, rt, NULL); return mBitmap; } return mBitmap; }