1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/ipc/SharedDIBSurface.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "SharedDIBSurface.h" 1.10 + 1.11 +#include "cairo.h" 1.12 + 1.13 +namespace mozilla { 1.14 +namespace gfx { 1.15 + 1.16 +static const cairo_user_data_key_t SHAREDDIB_KEY = {0}; 1.17 + 1.18 +static const long kBytesPerPixel = 4; 1.19 + 1.20 +bool 1.21 +SharedDIBSurface::Create(HDC adc, uint32_t aWidth, uint32_t aHeight, 1.22 + bool aTransparent) 1.23 +{ 1.24 + nsresult rv = mSharedDIB.Create(adc, aWidth, aHeight, aTransparent); 1.25 + if (NS_FAILED(rv) || !mSharedDIB.IsValid()) 1.26 + return false; 1.27 + 1.28 + InitSurface(aWidth, aHeight, aTransparent); 1.29 + return true; 1.30 +} 1.31 + 1.32 +bool 1.33 +SharedDIBSurface::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight, 1.34 + bool aTransparent) 1.35 +{ 1.36 + nsresult rv = mSharedDIB.Attach(aHandle, aWidth, aHeight, aTransparent); 1.37 + if (NS_FAILED(rv) || !mSharedDIB.IsValid()) 1.38 + return false; 1.39 + 1.40 + InitSurface(aWidth, aHeight, aTransparent); 1.41 + return true; 1.42 +} 1.43 + 1.44 +void 1.45 +SharedDIBSurface::InitSurface(uint32_t aWidth, uint32_t aHeight, 1.46 + bool aTransparent) 1.47 +{ 1.48 + long stride = long(aWidth * kBytesPerPixel); 1.49 + unsigned char* data = reinterpret_cast<unsigned char*>(mSharedDIB.GetBits()); 1.50 + 1.51 + gfxImageFormat format = aTransparent ? gfxImageFormat::ARGB32 : gfxImageFormat::RGB24; 1.52 + 1.53 + gfxImageSurface::InitWithData(data, gfxIntSize(aWidth, aHeight), 1.54 + stride, format); 1.55 + 1.56 + cairo_surface_set_user_data(mSurface, &SHAREDDIB_KEY, this, nullptr); 1.57 +} 1.58 + 1.59 +bool 1.60 +SharedDIBSurface::IsSharedDIBSurface(gfxASurface* aSurface) 1.61 +{ 1.62 + return aSurface && 1.63 + aSurface->GetType() == gfxSurfaceType::Image && 1.64 + aSurface->GetData(&SHAREDDIB_KEY); 1.65 +} 1.66 + 1.67 +} // namespace gfx 1.68 +} // namespace mozilla