Backed out changeset 62f7af8fe549 (bug 1843981) for causing valgrind bustage. CLOSED...
[gecko.git] / gfx / thebes / gfxWindowsSurface.cpp
blob419c9ee696ab6308f8e745951c9b28d02940732a
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"
13 #include "cairo.h"
14 #include "cairo-win32.h"
16 #include "nsString.h"
18 gfxWindowsSurface::gfxWindowsSurface(HDC dc, uint32_t flags)
19 : mOwnsDC(false), mDC(dc), mWnd(nullptr) {
20 InitWithDC(flags);
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);
37 Init(surf);
39 if (CairoStatus() == CAIRO_STATUS_SUCCESS) {
40 mDC = cairo_win32_surface_get_dc(CairoSurface());
41 RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface));
42 } else {
43 mDC = nullptr;
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);
51 else
52 mDC = nullptr;
54 MOZ_ASSERT(cairo_surface_get_type(csurf) !=
55 CAIRO_SURFACE_TYPE_WIN32_PRINTING);
57 Init(csurf, true);
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));
63 } else {
64 Init(cairo_win32_surface_create(mDC));
68 gfxWindowsSurface::~gfxWindowsSurface() {
69 if (mOwnsDC) {
70 if (mWnd)
71 ::ReleaseDC(mWnd, mDC);
72 else
73 ::DeleteDC(mDC);
77 HDC gfxWindowsSurface::GetDC() {
78 return cairo_win32_surface_get_dc(CairoSurface());
81 already_AddRefed<gfxImageSurface> gfxWindowsSurface::GetAsImageSurface() {
82 if (!mSurfaceValid) {
83 NS_WARNING(
84 "GetImageSurface on an invalid (null) surface; who's calling this "
85 "without checking for surface errors?");
86 return nullptr;
89 NS_ASSERTION(
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) {
105 NS_WARNING(
106 "GetImageSurface on an invalid (null) surface; who's calling this "
107 "without checking for surface errors?");
108 return mozilla::gfx::IntSize(-1, -1);
111 NS_ASSERTION(
112 mSurface != nullptr,
113 "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
115 int width, height;
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);