2005-04-06 05:54:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-04-07 23:12:19 +04:00
|
|
|
#include "gfxWindowsSurface.h"
|
2006-08-10 02:43:06 +04:00
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
|
|
|
#include "nsString.h"
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HWND wnd, uint32_t flags) :
|
2011-10-17 18:59:28 +04:00
|
|
|
mOwnsDC(true), mForPrinting(false), mWnd(wnd)
|
2006-01-31 03:00:12 +03:00
|
|
|
{
|
|
|
|
mDC = ::GetDC(mWnd);
|
2010-07-16 01:08:10 +04:00
|
|
|
InitWithDC(flags);
|
2006-01-31 03:00:12 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, uint32_t flags) :
|
2012-07-30 18:20:58 +04:00
|
|
|
mOwnsDC(false), mForPrinting(false), mDC(dc), mWnd(nullptr)
|
2005-04-06 05:54:26 +04:00
|
|
|
{
|
2007-09-25 02:50:44 +04:00
|
|
|
if (flags & FLAG_TAKE_DC)
|
2011-10-17 18:59:28 +04:00
|
|
|
mOwnsDC = true;
|
2007-09-25 02:50:44 +04:00
|
|
|
|
2008-11-04 18:43:45 +03:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-25 02:50:44 +04:00
|
|
|
if (flags & FLAG_FOR_PRINTING) {
|
2007-11-29 23:06:56 +03:00
|
|
|
Init(cairo_win32_printing_surface_create(mDC));
|
2011-10-17 18:59:28 +04:00
|
|
|
mForPrinting = true;
|
2008-11-04 18:43:45 +03:00
|
|
|
} else
|
|
|
|
#endif
|
2010-07-16 01:08:10 +04:00
|
|
|
InitWithDC(flags);
|
2005-08-20 09:36:47 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 00:53:54 +04:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(IDirect3DSurface9 *surface, uint32_t flags) :
|
|
|
|
mOwnsDC(false), mForPrinting(false), mDC(0), mWnd(nullptr)
|
|
|
|
{
|
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_d3dsurface9(surface);
|
|
|
|
Init(surf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-13 18:49:01 +04:00
|
|
|
void
|
|
|
|
gfxWindowsSurface::MakeInvalid(gfxIntSize& size)
|
|
|
|
{
|
|
|
|
size = gfxIntSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(const gfxIntSize& realSize, gfxImageFormat imageFormat) :
|
2012-07-30 18:20:58 +04:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2006-08-10 02:43:06 +04:00
|
|
|
{
|
2011-09-13 18:49:01 +04:00
|
|
|
gfxIntSize size(realSize);
|
2007-05-10 23:58:09 +04:00
|
|
|
if (!CheckSurfaceSize(size))
|
2011-09-13 18:49:01 +04:00
|
|
|
MakeInvalid(size);
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_dib((cairo_format_t)(int)imageFormat,
|
2007-02-08 23:47:48 +03:00
|
|
|
size.width, size.height);
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2006-08-10 02:43:06 +04:00
|
|
|
Init(surf);
|
|
|
|
|
2013-09-25 06:14:39 +04:00
|
|
|
if (CairoStatus() == CAIRO_STATUS_SUCCESS) {
|
2007-10-04 01:27:42 +04:00
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
2013-09-25 06:14:39 +04:00
|
|
|
RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface));
|
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
mDC = nullptr;
|
2013-09-25 06:14:39 +04:00
|
|
|
}
|
2006-08-10 02:43:06 +04:00
|
|
|
}
|
|
|
|
|
2011-09-13 18:49:01 +04:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, const gfxIntSize& realSize, gfxImageFormat imageFormat) :
|
2012-07-30 18:20:58 +04:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2005-08-20 09:36:47 +04:00
|
|
|
{
|
2011-09-13 18:49:01 +04:00
|
|
|
gfxIntSize size(realSize);
|
2007-05-10 23:58:09 +04:00
|
|
|
if (!CheckSurfaceSize(size))
|
2011-09-13 18:49:01 +04:00
|
|
|
MakeInvalid(size);
|
2007-05-10 23:58:09 +04:00
|
|
|
|
2014-01-23 22:26:40 +04:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_ddb(dc, (cairo_format_t)(int)imageFormat,
|
2007-02-08 23:47:48 +03:00
|
|
|
size.width, size.height);
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2006-08-10 02:43:06 +04:00
|
|
|
Init(surf);
|
2006-02-01 05:35:38 +03:00
|
|
|
|
2012-03-19 03:36:33 +04:00
|
|
|
if (mSurfaceValid) {
|
|
|
|
// DDBs will generally only use 3 bytes per pixel when RGB24
|
2014-01-23 22:26:40 +04:00
|
|
|
int bytesPerPixel = ((imageFormat == gfxImageFormat::RGB24) ? 3 : 4);
|
2012-03-19 03:36:33 +04:00
|
|
|
RecordMemoryUsed(size.width * size.height * bytesPerPixel + sizeof(gfxWindowsSurface));
|
|
|
|
}
|
2010-05-22 08:10:14 +04:00
|
|
|
|
2007-10-04 01:27:42 +04:00
|
|
|
if (CairoStatus() == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
|
|
|
else
|
2012-07-30 18:20:58 +04:00
|
|
|
mDC = nullptr;
|
2005-08-20 09:36:47 +04:00
|
|
|
}
|
|
|
|
|
2006-04-05 01:54:22 +04:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t *csurf) :
|
2012-07-30 18:20:58 +04:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2006-04-05 01:54:22 +04:00
|
|
|
{
|
2007-10-04 01:27:42 +04:00
|
|
|
if (cairo_surface_status(csurf) == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(csurf);
|
|
|
|
else
|
2012-07-30 18:20:58 +04:00
|
|
|
mDC = nullptr;
|
2006-04-05 01:54:22 +04:00
|
|
|
|
2007-09-25 02:50:44 +04:00
|
|
|
if (cairo_surface_get_type(csurf) == CAIRO_SURFACE_TYPE_WIN32_PRINTING)
|
2011-10-17 18:59:28 +04:00
|
|
|
mForPrinting = true;
|
2007-09-25 02:50:44 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
Init(csurf, true);
|
2006-04-05 01:54:22 +04:00
|
|
|
}
|
|
|
|
|
2010-07-16 01:08:10 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxWindowsSurface::InitWithDC(uint32_t flags)
|
2010-07-16 01:08:10 +04:00
|
|
|
{
|
|
|
|
if (flags & FLAG_IS_TRANSPARENT) {
|
|
|
|
Init(cairo_win32_surface_create_with_alpha(mDC));
|
|
|
|
} else {
|
|
|
|
Init(cairo_win32_surface_create(mDC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-01 06:47:47 +03:00
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
gfxWindowsSurface::CreateSimilarSurface(gfxContentType aContent,
|
|
|
|
const gfxIntSize& aSize)
|
|
|
|
{
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-02-01 06:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t *surface;
|
2014-01-23 22:26:40 +04:00
|
|
|
if (!mForPrinting && GetContentType() == gfxContentType::COLOR_ALPHA) {
|
2011-02-01 06:47:47 +03:00
|
|
|
// When creating a similar surface to a transparent surface, ensure
|
|
|
|
// the new surface uses a DIB. cairo_surface_create_similar won't
|
2014-01-23 22:26:40 +04:00
|
|
|
// use a DIB for a gfxContentType::COLOR surface if this surface doesn't
|
2011-02-01 06:47:47 +03:00
|
|
|
// have a DIB (e.g. if we're a transparent window surface). But
|
|
|
|
// we need a DIB to perform well if the new surface is composited into
|
2014-01-23 22:26:40 +04:00
|
|
|
// a surface that's the result of create_similar(gfxContentType::COLOR_ALPHA)
|
2011-02-01 06:47:47 +03:00
|
|
|
// (e.g. a backbuffer for the window) --- that new surface *would*
|
|
|
|
// have a DIB.
|
|
|
|
surface =
|
2014-01-23 22:26:40 +04:00
|
|
|
cairo_win32_surface_create_with_dib((cairo_format_t)(int)gfxPlatform::GetPlatform()->OptimalFormatForContent(aContent),
|
2011-02-01 06:47:47 +03:00
|
|
|
aSize.width, aSize.height);
|
|
|
|
} else {
|
|
|
|
surface =
|
2014-01-23 22:26:40 +04:00
|
|
|
cairo_surface_create_similar(mSurface, (cairo_content_t)(int)aContent,
|
2011-02-01 06:47:47 +03:00
|
|
|
aSize.width, aSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cairo_surface_status(surface)) {
|
|
|
|
cairo_surface_destroy(surface);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-02-01 06:47:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> result = Wrap(surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2005-04-07 23:12:19 +04:00
|
|
|
gfxWindowsSurface::~gfxWindowsSurface()
|
2005-04-06 05:54:26 +04:00
|
|
|
{
|
2006-01-31 03:00:12 +03:00
|
|
|
if (mOwnsDC) {
|
|
|
|
if (mWnd)
|
|
|
|
::ReleaseDC(mWnd, mDC);
|
|
|
|
else
|
|
|
|
::DeleteDC(mDC);
|
|
|
|
}
|
2005-04-06 05:54:26 +04:00
|
|
|
}
|
2006-02-01 08:21:59 +03:00
|
|
|
|
2010-04-06 06:28:54 +04:00
|
|
|
HDC
|
|
|
|
gfxWindowsSurface::GetDCWithClip(gfxContext *ctx)
|
|
|
|
{
|
|
|
|
return cairo_win32_get_dc_with_clip (ctx->GetCairo());
|
|
|
|
}
|
|
|
|
|
2013-09-05 00:53:54 +04:00
|
|
|
HDC
|
|
|
|
gfxWindowsSurface::GetDC()
|
|
|
|
{
|
|
|
|
return cairo_win32_surface_get_dc (CairoSurface());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-10 02:43:06 +04:00
|
|
|
already_AddRefed<gfxImageSurface>
|
2010-11-11 23:31:22 +03:00
|
|
|
gfxWindowsSurface::GetAsImageSurface()
|
2006-08-10 02:43:06 +04:00
|
|
|
{
|
2008-01-28 22:05:44 +03:00
|
|
|
if (!mSurfaceValid) {
|
|
|
|
NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-01-28 22:05:44 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(CairoSurface() != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
|
2008-01-28 22:05:44 +03:00
|
|
|
|
2007-09-25 02:50:44 +04:00
|
|
|
if (mForPrinting)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-09-25 02:50:44 +04:00
|
|
|
|
2006-08-10 02:43:06 +04:00
|
|
|
cairo_surface_t *isurf = cairo_win32_surface_get_image(CairoSurface());
|
|
|
|
if (!isurf)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-08-10 02:43:06 +04:00
|
|
|
|
2013-09-20 13:50:05 +04:00
|
|
|
nsRefPtr<gfxImageSurface> result = gfxASurface::Wrap(isurf).downcast<gfxImageSurface>();
|
|
|
|
result->SetOpaqueRect(GetOpaqueRect());
|
|
|
|
|
|
|
|
return result.forget();
|
2006-08-10 02:43:06 +04:00
|
|
|
}
|
|
|
|
|
2009-06-16 22:57:34 +04:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPrinting(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName)
|
2006-02-01 08:21:59 +03:00
|
|
|
{
|
2009-06-16 22:57:34 +04:00
|
|
|
#ifdef NS_PRINTING
|
2010-11-13 14:02:00 +03:00
|
|
|
#define DOC_TITLE_LENGTH (MAX_PATH-1)
|
2008-08-28 01:44:55 +04:00
|
|
|
DOCINFOW docinfo;
|
2006-02-01 08:21:59 +03:00
|
|
|
|
2008-08-28 01:44:55 +04:00
|
|
|
nsString titleStr(aTitle);
|
2006-02-01 08:21:59 +03:00
|
|
|
if (titleStr.Length() > DOC_TITLE_LENGTH) {
|
|
|
|
titleStr.SetLength(DOC_TITLE_LENGTH-3);
|
|
|
|
titleStr.AppendLiteral("...");
|
|
|
|
}
|
2008-03-12 01:46:26 +03:00
|
|
|
|
2008-08-28 01:44:55 +04:00
|
|
|
nsString docName(aPrintToFileName);
|
2006-02-01 08:21:59 +03:00
|
|
|
docinfo.cbSize = sizeof(docinfo);
|
2008-08-28 01:44:55 +04:00
|
|
|
docinfo.lpszDocName = titleStr.Length() > 0 ? titleStr.get() : L"Mozilla Document";
|
2012-07-30 18:20:58 +04:00
|
|
|
docinfo.lpszOutput = docName.Length() > 0 ? docName.get() : nullptr;
|
2013-07-31 19:44:31 +04:00
|
|
|
docinfo.lpszDatatype = nullptr;
|
2006-02-01 08:21:59 +03:00
|
|
|
docinfo.fwType = 0;
|
|
|
|
|
2008-08-28 01:44:55 +04:00
|
|
|
::StartDocW(mDC, &docinfo);
|
2008-03-27 04:21:46 +03:00
|
|
|
|
2006-02-01 08:21:59 +03:00
|
|
|
return NS_OK;
|
2009-06-16 22:57:34 +04:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2006-02-01 08:21:59 +03:00
|
|
|
}
|
|
|
|
|
2009-06-16 22:57:34 +04:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPrinting()
|
2006-02-01 08:21:59 +03:00
|
|
|
{
|
2009-06-16 22:57:34 +04:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-27 04:21:46 +03:00
|
|
|
int result = ::EndDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2006-02-01 08:21:59 +03:00
|
|
|
return NS_OK;
|
2009-06-16 22:57:34 +04:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2006-02-01 08:21:59 +03:00
|
|
|
}
|
|
|
|
|
2009-06-16 22:57:34 +04:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::AbortPrinting()
|
2006-02-01 08:21:59 +03:00
|
|
|
{
|
2009-06-16 22:57:34 +04:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-27 04:21:46 +03:00
|
|
|
int result = ::AbortDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-02-01 08:21:59 +03:00
|
|
|
return NS_OK;
|
2009-06-16 22:57:34 +04:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2006-02-01 08:21:59 +03:00
|
|
|
}
|
|
|
|
|
2009-06-16 22:57:34 +04:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPage()
|
2006-02-01 08:21:59 +03:00
|
|
|
{
|
2009-06-16 22:57:34 +04:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-27 04:21:46 +03:00
|
|
|
int result = ::StartPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-02-01 08:21:59 +03:00
|
|
|
return NS_OK;
|
2009-06-16 22:57:34 +04:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2006-02-01 08:21:59 +03:00
|
|
|
}
|
|
|
|
|
2009-06-16 22:57:34 +04:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPage()
|
2006-02-01 08:21:59 +03:00
|
|
|
{
|
2009-06-16 22:57:34 +04:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-25 02:50:44 +04:00
|
|
|
if (mForPrinting)
|
|
|
|
cairo_surface_show_page(CairoSurface());
|
2008-03-27 04:21:46 +03:00
|
|
|
int result = ::EndPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-02-01 08:21:59 +03:00
|
|
|
return NS_OK;
|
2009-06-16 22:57:34 +04:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2006-02-01 08:21:59 +03:00
|
|
|
}
|
2007-11-29 23:06:56 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2009-06-16 22:57:34 +04:00
|
|
|
gfxWindowsSurface::GetDefaultContextFlags() const
|
2007-11-29 23:06:56 +03:00
|
|
|
{
|
|
|
|
if (mForPrinting)
|
2008-01-07 03:50:18 +03:00
|
|
|
return gfxContext::FLAG_SIMPLIFY_OPERATORS |
|
2011-01-15 12:40:33 +03:00
|
|
|
gfxContext::FLAG_DISABLE_SNAPPING |
|
|
|
|
gfxContext::FLAG_DISABLE_COPY_BACKGROUND;
|
2007-11-29 23:06:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-18 17:20:27 +04:00
|
|
|
|
2011-11-18 08:00:37 +04:00
|
|
|
const gfxIntSize
|
|
|
|
gfxWindowsSurface::GetSize() const
|
|
|
|
{
|
|
|
|
if (!mSurfaceValid) {
|
|
|
|
NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
|
|
|
|
return gfxIntSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION(mSurface != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
|
2011-11-18 08:00:37 +04:00
|
|
|
|
|
|
|
return gfxIntSize(cairo_win32_surface_get_width(mSurface),
|
|
|
|
cairo_win32_surface_get_height(mSurface));
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxMemoryLocation
|
2011-07-18 17:20:27 +04:00
|
|
|
gfxWindowsSurface::GetMemoryLocation() const
|
|
|
|
{
|
2014-01-23 22:26:40 +04:00
|
|
|
return gfxMemoryLocation::IN_PROCESS_NONHEAP;
|
2011-07-18 17:20:27 +04:00
|
|
|
}
|