michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "SharedDIBSurface.h" michael@0: michael@0: #include "cairo.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: static const cairo_user_data_key_t SHAREDDIB_KEY = {0}; michael@0: michael@0: static const long kBytesPerPixel = 4; michael@0: michael@0: bool michael@0: SharedDIBSurface::Create(HDC adc, uint32_t aWidth, uint32_t aHeight, michael@0: bool aTransparent) michael@0: { michael@0: nsresult rv = mSharedDIB.Create(adc, aWidth, aHeight, aTransparent); michael@0: if (NS_FAILED(rv) || !mSharedDIB.IsValid()) michael@0: return false; michael@0: michael@0: InitSurface(aWidth, aHeight, aTransparent); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: SharedDIBSurface::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight, michael@0: bool aTransparent) michael@0: { michael@0: nsresult rv = mSharedDIB.Attach(aHandle, aWidth, aHeight, aTransparent); michael@0: if (NS_FAILED(rv) || !mSharedDIB.IsValid()) michael@0: return false; michael@0: michael@0: InitSurface(aWidth, aHeight, aTransparent); michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: SharedDIBSurface::InitSurface(uint32_t aWidth, uint32_t aHeight, michael@0: bool aTransparent) michael@0: { michael@0: long stride = long(aWidth * kBytesPerPixel); michael@0: unsigned char* data = reinterpret_cast(mSharedDIB.GetBits()); michael@0: michael@0: gfxImageFormat format = aTransparent ? gfxImageFormat::ARGB32 : gfxImageFormat::RGB24; michael@0: michael@0: gfxImageSurface::InitWithData(data, gfxIntSize(aWidth, aHeight), michael@0: stride, format); michael@0: michael@0: cairo_surface_set_user_data(mSurface, &SHAREDDIB_KEY, this, nullptr); michael@0: } michael@0: michael@0: bool michael@0: SharedDIBSurface::IsSharedDIBSurface(gfxASurface* aSurface) michael@0: { michael@0: return aSurface && michael@0: aSurface->GetType() == gfxSurfaceType::Image && michael@0: aSurface->GetData(&SHAREDDIB_KEY); michael@0: } michael@0: michael@0: } // namespace gfx michael@0: } // namespace mozilla