1 /* -*- Mode: C++; tab-width: 20; 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 "gfxWindowsSurface.h"
7 #include "gfxContext.h"
8 #include "gfxPlatform.h"
9 #include "mozilla/gfx/2D.h"
10 #include "mozilla/gfx/HelpersCairo.h"
11 #include "mozilla/gfx/Logging.h"
14 #include "cairo-win32.h"
18 gfxWindowsSurface::gfxWindowsSurface(HDC dc
, uint32_t flags
)
19 : mOwnsDC(false), mDC(dc
), mWnd(nullptr) {
23 void gfxWindowsSurface::MakeInvalid(mozilla::gfx::IntSize
& size
) {
24 size
= mozilla::gfx::IntSize(-1, -1);
27 gfxWindowsSurface::gfxWindowsSurface(const mozilla::gfx::IntSize
& realSize
,
28 gfxImageFormat imageFormat
)
29 : mOwnsDC(false), mWnd(nullptr) {
30 mozilla::gfx::IntSize
size(realSize
);
31 if (!mozilla::gfx::Factory::CheckSurfaceSize(size
)) MakeInvalid(size
);
33 cairo_format_t cformat
= GfxFormatToCairoFormat(imageFormat
);
34 cairo_surface_t
* surf
=
35 cairo_win32_surface_create_with_dib(cformat
, size
.width
, size
.height
);
39 if (CairoStatus() == CAIRO_STATUS_SUCCESS
) {
40 mDC
= cairo_win32_surface_get_dc(CairoSurface());
41 RecordMemoryUsed(size
.width
* size
.height
* 4 + sizeof(gfxWindowsSurface
));
47 gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t
* csurf
)
48 : mOwnsDC(false), mWnd(nullptr) {
49 if (cairo_surface_status(csurf
) == 0)
50 mDC
= cairo_win32_surface_get_dc(csurf
);
54 MOZ_ASSERT(cairo_surface_get_type(csurf
) !=
55 CAIRO_SURFACE_TYPE_WIN32_PRINTING
);
60 void gfxWindowsSurface::InitWithDC(uint32_t flags
) {
61 if (flags
& FLAG_IS_TRANSPARENT
) {
62 Init(cairo_win32_surface_create_with_format(mDC
, CAIRO_FORMAT_ARGB32
));
64 Init(cairo_win32_surface_create(mDC
));
68 gfxWindowsSurface::~gfxWindowsSurface() {
71 ::ReleaseDC(mWnd
, mDC
);
77 HDC
gfxWindowsSurface::GetDC() {
78 return cairo_win32_surface_get_dc(CairoSurface());
81 already_AddRefed
<gfxImageSurface
> gfxWindowsSurface::GetAsImageSurface() {
84 "GetImageSurface on an invalid (null) surface; who's calling this "
85 "without checking for surface errors?");
90 CairoSurface() != nullptr,
91 "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
93 cairo_surface_t
* isurf
= cairo_win32_surface_get_image(CairoSurface());
94 if (!isurf
) return nullptr;
96 RefPtr
<gfxImageSurface
> result
=
97 gfxASurface::Wrap(isurf
).downcast
<gfxImageSurface
>();
98 result
->SetOpaqueRect(GetOpaqueRect());
100 return result
.forget();
103 const mozilla::gfx::IntSize
gfxWindowsSurface::GetSize() const {
104 if (!mSurfaceValid
) {
106 "GetImageSurface on an invalid (null) surface; who's calling this "
107 "without checking for surface errors?");
108 return mozilla::gfx::IntSize(-1, -1);
113 "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
116 if (cairo_win32_surface_get_size(mSurface
, &width
, &height
) !=
117 CAIRO_STATUS_SUCCESS
) {
118 return mozilla::gfx::IntSize(-1, -1);
121 return mozilla::gfx::IntSize(width
, height
);