|
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/. */ |
|
5 |
|
6 #include "SharedDIBSurface.h" |
|
7 |
|
8 #include "cairo.h" |
|
9 |
|
10 namespace mozilla { |
|
11 namespace gfx { |
|
12 |
|
13 static const cairo_user_data_key_t SHAREDDIB_KEY = {0}; |
|
14 |
|
15 static const long kBytesPerPixel = 4; |
|
16 |
|
17 bool |
|
18 SharedDIBSurface::Create(HDC adc, uint32_t aWidth, uint32_t aHeight, |
|
19 bool aTransparent) |
|
20 { |
|
21 nsresult rv = mSharedDIB.Create(adc, aWidth, aHeight, aTransparent); |
|
22 if (NS_FAILED(rv) || !mSharedDIB.IsValid()) |
|
23 return false; |
|
24 |
|
25 InitSurface(aWidth, aHeight, aTransparent); |
|
26 return true; |
|
27 } |
|
28 |
|
29 bool |
|
30 SharedDIBSurface::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight, |
|
31 bool aTransparent) |
|
32 { |
|
33 nsresult rv = mSharedDIB.Attach(aHandle, aWidth, aHeight, aTransparent); |
|
34 if (NS_FAILED(rv) || !mSharedDIB.IsValid()) |
|
35 return false; |
|
36 |
|
37 InitSurface(aWidth, aHeight, aTransparent); |
|
38 return true; |
|
39 } |
|
40 |
|
41 void |
|
42 SharedDIBSurface::InitSurface(uint32_t aWidth, uint32_t aHeight, |
|
43 bool aTransparent) |
|
44 { |
|
45 long stride = long(aWidth * kBytesPerPixel); |
|
46 unsigned char* data = reinterpret_cast<unsigned char*>(mSharedDIB.GetBits()); |
|
47 |
|
48 gfxImageFormat format = aTransparent ? gfxImageFormat::ARGB32 : gfxImageFormat::RGB24; |
|
49 |
|
50 gfxImageSurface::InitWithData(data, gfxIntSize(aWidth, aHeight), |
|
51 stride, format); |
|
52 |
|
53 cairo_surface_set_user_data(mSurface, &SHAREDDIB_KEY, this, nullptr); |
|
54 } |
|
55 |
|
56 bool |
|
57 SharedDIBSurface::IsSharedDIBSurface(gfxASurface* aSurface) |
|
58 { |
|
59 return aSurface && |
|
60 aSurface->GetType() == gfxSurfaceType::Image && |
|
61 aSurface->GetData(&SHAREDDIB_KEY); |
|
62 } |
|
63 |
|
64 } // namespace gfx |
|
65 } // namespace mozilla |