Bumping manifests a=b2g-bump
[gecko.git] / gfx / ipc / SharedDIBSurface.cpp
blobc078389fe7616f357facabc427375047182c3289
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "SharedDIBSurface.h"
8 #include "cairo.h"
10 namespace mozilla {
11 namespace gfx {
13 static const cairo_user_data_key_t SHAREDDIB_KEY = {0};
15 static const long kBytesPerPixel = 4;
17 bool
18 SharedDIBSurface::Create(HDC adc, uint32_t aWidth, uint32_t aHeight,
19 bool aTransparent)
21 nsresult rv = mSharedDIB.Create(adc, aWidth, aHeight, aTransparent);
22 if (NS_FAILED(rv) || !mSharedDIB.IsValid())
23 return false;
25 InitSurface(aWidth, aHeight, aTransparent);
26 return true;
29 bool
30 SharedDIBSurface::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight,
31 bool aTransparent)
33 nsresult rv = mSharedDIB.Attach(aHandle, aWidth, aHeight, aTransparent);
34 if (NS_FAILED(rv) || !mSharedDIB.IsValid())
35 return false;
37 InitSurface(aWidth, aHeight, aTransparent);
38 return true;
41 void
42 SharedDIBSurface::InitSurface(uint32_t aWidth, uint32_t aHeight,
43 bool aTransparent)
45 long stride = long(aWidth * kBytesPerPixel);
46 unsigned char* data = reinterpret_cast<unsigned char*>(mSharedDIB.GetBits());
48 gfxImageFormat format = aTransparent ? gfxImageFormat::ARGB32 : gfxImageFormat::RGB24;
50 gfxImageSurface::InitWithData(data, gfxIntSize(aWidth, aHeight),
51 stride, format);
53 cairo_surface_set_user_data(mSurface, &SHAREDDIB_KEY, this, nullptr);
56 bool
57 SharedDIBSurface::IsSharedDIBSurface(gfxASurface* aSurface)
59 return aSurface &&
60 aSurface->GetType() == gfxSurfaceType::Image &&
61 aSurface->GetData(&SHAREDDIB_KEY);
64 } // namespace gfx
65 } // namespace mozilla