gfxWindowsSurface.cpp (3298B)
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/. */ 5 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" 12 13 #include "cairo.h" 14 #include "cairo-win32.h" 15 16 #include "nsString.h" 17 18 gfxWindowsSurface::gfxWindowsSurface(HDC dc, uint32_t flags) : mDC(dc) { 19 InitWithDC(flags); 20 } 21 22 void gfxWindowsSurface::MakeInvalid(mozilla::gfx::IntSize& size) { 23 size = mozilla::gfx::IntSize(-1, -1); 24 } 25 26 gfxWindowsSurface::gfxWindowsSurface(const mozilla::gfx::IntSize& realSize, 27 gfxImageFormat imageFormat) { 28 mozilla::gfx::IntSize size(realSize); 29 if (!mozilla::gfx::Factory::CheckSurfaceSize(size)) { 30 MakeInvalid(size); 31 } 32 33 cairo_format_t cformat = GfxFormatToCairoFormat(imageFormat); 34 cairo_surface_t* surf = 35 cairo_win32_surface_create_with_dib(cformat, size.width, size.height); 36 37 Init(surf); 38 39 if (CairoStatus() == CAIRO_STATUS_SUCCESS) { 40 mDC = cairo_win32_surface_get_dc(CairoSurface()); 41 RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface)); 42 } 43 } 44 45 gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t* csurf) { 46 if (cairo_surface_status(csurf) == 0) { 47 mDC = cairo_win32_surface_get_dc(csurf); 48 } 49 50 MOZ_ASSERT(cairo_surface_get_type(csurf) != 51 CAIRO_SURFACE_TYPE_WIN32_PRINTING); 52 53 Init(csurf, true); 54 } 55 56 void gfxWindowsSurface::InitWithDC(uint32_t flags) { 57 if (flags & FLAG_IS_TRANSPARENT) { 58 Init(cairo_win32_surface_create_with_format(mDC, CAIRO_FORMAT_ARGB32)); 59 } else { 60 Init(cairo_win32_surface_create(mDC)); 61 } 62 } 63 64 gfxWindowsSurface::~gfxWindowsSurface() = default; 65 66 HDC gfxWindowsSurface::GetDC() { 67 return cairo_win32_surface_get_dc(CairoSurface()); 68 } 69 70 already_AddRefed<gfxImageSurface> gfxWindowsSurface::GetAsImageSurface() { 71 if (!mSurfaceValid) { 72 NS_WARNING( 73 "GetImageSurface on an invalid (null) surface; who's calling this " 74 "without checking for surface errors?"); 75 return nullptr; 76 } 77 78 NS_ASSERTION( 79 CairoSurface() != nullptr, 80 "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!"); 81 82 cairo_surface_t* isurf = cairo_win32_surface_get_image(CairoSurface()); 83 if (!isurf) { 84 return nullptr; 85 } 86 87 RefPtr<gfxImageSurface> result = 88 gfxASurface::Wrap(isurf).downcast<gfxImageSurface>(); 89 result->SetOpaqueRect(GetOpaqueRect()); 90 91 return result.forget(); 92 } 93 94 const mozilla::gfx::IntSize gfxWindowsSurface::GetSize() const { 95 if (!mSurfaceValid) { 96 NS_WARNING( 97 "GetImageSurface on an invalid (null) surface; who's calling this " 98 "without checking for surface errors?"); 99 return mozilla::gfx::IntSize(-1, -1); 100 } 101 102 NS_ASSERTION( 103 mSurface != nullptr, 104 "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!"); 105 106 int width, height; 107 if (cairo_win32_surface_get_size(mSurface, &width, &height) != 108 CAIRO_STATUS_SUCCESS) { 109 return mozilla::gfx::IntSize(-1, -1); 110 } 111 112 return mozilla::gfx::IntSize(width, height); 113 }