2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "gfxWindowsSurface.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HWND wnd, uint32_t flags) :
|
2011-10-17 07:59:28 -07:00
|
|
|
mOwnsDC(true), mForPrinting(false), mWnd(wnd)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mDC = ::GetDC(mWnd);
|
2010-07-15 14:08:10 -07:00
|
|
|
InitWithDC(flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, uint32_t flags) :
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnsDC(false), mForPrinting(false), mDC(dc), mWnd(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-09-24 15:50:44 -07:00
|
|
|
if (flags & FLAG_TAKE_DC)
|
2011-10-17 07:59:28 -07:00
|
|
|
mOwnsDC = true;
|
2007-09-24 15:50:44 -07:00
|
|
|
|
2008-11-04 07:43:45 -08:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-24 15:50:44 -07:00
|
|
|
if (flags & FLAG_FOR_PRINTING) {
|
2007-11-29 12:06:56 -08:00
|
|
|
Init(cairo_win32_printing_surface_create(mDC));
|
2011-10-17 07:59:28 -07:00
|
|
|
mForPrinting = true;
|
2008-11-04 07:43:45 -08:00
|
|
|
} else
|
|
|
|
#endif
|
2010-07-15 14:08:10 -07:00
|
|
|
InitWithDC(flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-13 07:49:01 -07:00
|
|
|
void
|
|
|
|
gfxWindowsSurface::MakeInvalid(gfxIntSize& size)
|
|
|
|
{
|
|
|
|
size = gfxIntSize(-1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(const gfxIntSize& realSize, gfxImageFormat imageFormat) :
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-13 07:49:01 -07:00
|
|
|
gfxIntSize size(realSize);
|
2007-05-10 12:58:09 -07:00
|
|
|
if (!CheckSurfaceSize(size))
|
2011-09-13 07:49:01 -07:00
|
|
|
MakeInvalid(size);
|
2007-05-10 12:58:09 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_dib((cairo_format_t)imageFormat,
|
|
|
|
size.width, size.height);
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Init(surf);
|
|
|
|
|
2010-05-21 21:10:14 -07:00
|
|
|
RecordMemoryUsed(size.width * size.height * 4 + sizeof(gfxWindowsSurface));
|
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (CairoStatus() == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
|
|
|
else
|
2012-07-30 07:20:58 -07:00
|
|
|
mDC = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-13 07:49:01 -07:00
|
|
|
gfxWindowsSurface::gfxWindowsSurface(HDC dc, const gfxIntSize& realSize, gfxImageFormat imageFormat) :
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-13 07:49:01 -07:00
|
|
|
gfxIntSize size(realSize);
|
2007-05-10 12:58:09 -07:00
|
|
|
if (!CheckSurfaceSize(size))
|
2011-09-13 07:49:01 -07:00
|
|
|
MakeInvalid(size);
|
2007-05-10 12:58:09 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *surf = cairo_win32_surface_create_with_ddb(dc, (cairo_format_t)imageFormat,
|
|
|
|
size.width, size.height);
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
Init(surf);
|
|
|
|
|
2012-03-18 16:36:33 -07:00
|
|
|
if (mSurfaceValid) {
|
|
|
|
// DDBs will generally only use 3 bytes per pixel when RGB24
|
|
|
|
int bytesPerPixel = ((imageFormat == gfxASurface::ImageFormatRGB24) ? 3 : 4);
|
|
|
|
RecordMemoryUsed(size.width * size.height * bytesPerPixel + sizeof(gfxWindowsSurface));
|
|
|
|
}
|
2010-05-21 21:10:14 -07:00
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (CairoStatus() == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(CairoSurface());
|
|
|
|
else
|
2012-07-30 07:20:58 -07:00
|
|
|
mDC = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxWindowsSurface::gfxWindowsSurface(cairo_surface_t *csurf) :
|
2012-07-30 07:20:58 -07:00
|
|
|
mOwnsDC(false), mForPrinting(false), mWnd(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-10-03 14:27:42 -07:00
|
|
|
if (cairo_surface_status(csurf) == 0)
|
|
|
|
mDC = cairo_win32_surface_get_dc(csurf);
|
|
|
|
else
|
2012-07-30 07:20:58 -07:00
|
|
|
mDC = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-24 15:50:44 -07:00
|
|
|
if (cairo_surface_get_type(csurf) == CAIRO_SURFACE_TYPE_WIN32_PRINTING)
|
2011-10-17 07:59:28 -07:00
|
|
|
mForPrinting = true;
|
2007-09-24 15:50:44 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
Init(csurf, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 14:08:10 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxWindowsSurface::InitWithDC(uint32_t flags)
|
2010-07-15 14:08:10 -07:00
|
|
|
{
|
|
|
|
if (flags & FLAG_IS_TRANSPARENT) {
|
|
|
|
Init(cairo_win32_surface_create_with_alpha(mDC));
|
|
|
|
} else {
|
|
|
|
Init(cairo_win32_surface_create(mDC));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-31 19:47:47 -08:00
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
gfxWindowsSurface::CreateSimilarSurface(gfxContentType aContent,
|
|
|
|
const gfxIntSize& aSize)
|
|
|
|
{
|
|
|
|
if (!mSurface || !mSurfaceValid) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-01-31 19:47:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
if (GetContentType() == CONTENT_COLOR_ALPHA) {
|
|
|
|
// When creating a similar surface to a transparent surface, ensure
|
|
|
|
// the new surface uses a DIB. cairo_surface_create_similar won't
|
|
|
|
// use a DIB for a CONTENT_COLOR surface if this surface doesn't
|
|
|
|
// 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
|
|
|
|
// a surface that's the result of create_similar(CONTENT_COLOR_ALPHA)
|
|
|
|
// (e.g. a backbuffer for the window) --- that new surface *would*
|
|
|
|
// have a DIB.
|
|
|
|
surface =
|
2012-05-25 21:38:17 -07:00
|
|
|
cairo_win32_surface_create_with_dib(cairo_format_t(gfxPlatform::GetPlatform()->OptimalFormatForContent(aContent)),
|
2011-01-31 19:47:47 -08:00
|
|
|
aSize.width, aSize.height);
|
|
|
|
} else {
|
|
|
|
surface =
|
|
|
|
cairo_surface_create_similar(mSurface, cairo_content_t(aContent),
|
|
|
|
aSize.width, aSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cairo_surface_status(surface)) {
|
|
|
|
cairo_surface_destroy(surface);
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-01-31 19:47:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> result = Wrap(surface);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxWindowsSurface::~gfxWindowsSurface()
|
|
|
|
{
|
|
|
|
if (mOwnsDC) {
|
|
|
|
if (mWnd)
|
|
|
|
::ReleaseDC(mWnd, mDC);
|
|
|
|
else
|
|
|
|
::DeleteDC(mDC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-05 19:28:54 -07:00
|
|
|
HDC
|
|
|
|
gfxWindowsSurface::GetDCWithClip(gfxContext *ctx)
|
|
|
|
{
|
|
|
|
return cairo_win32_get_dc_with_clip (ctx->GetCairo());
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
already_AddRefed<gfxImageSurface>
|
2010-11-11 12:31:22 -08:00
|
|
|
gfxWindowsSurface::GetAsImageSurface()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-28 11:05:44 -08:00
|
|
|
if (!mSurfaceValid) {
|
|
|
|
NS_WARNING ("GetImageSurface on an invalid (null) surface; who's calling this without checking for surface errors?");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-01-28 11:05:44 -08:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(CairoSurface() != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
|
2008-01-28 11:05:44 -08:00
|
|
|
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-09-24 15:50:44 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *isurf = cairo_win32_surface_get_image(CairoSurface());
|
|
|
|
if (!isurf)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> asurf = gfxASurface::Wrap(isurf);
|
|
|
|
gfxImageSurface *imgsurf = (gfxImageSurface*) asurf.get();
|
|
|
|
NS_ADDREF(imgsurf);
|
|
|
|
return imgsurf;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxWindowsSurface>
|
|
|
|
gfxWindowsSurface::OptimizeToDDB(HDC dc, const gfxIntSize& size, gfxImageFormat format)
|
|
|
|
{
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-09-24 15:50:44 -07:00
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
if (format != ImageFormatRGB24)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-03 14:27:42 -07:00
|
|
|
nsRefPtr<gfxWindowsSurface> wsurf = new gfxWindowsSurface(dc, size, format);
|
|
|
|
if (wsurf->CairoStatus() != 0)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-31 20:42:49 -08:00
|
|
|
gfxContext tmpCtx(wsurf);
|
|
|
|
tmpCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
tmpCtx.SetSource(this);
|
|
|
|
tmpCtx.Paint();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
gfxWindowsSurface *raw = (gfxWindowsSurface*) (wsurf.get());
|
|
|
|
NS_ADDREF(raw);
|
2009-08-12 07:44:53 -07:00
|
|
|
|
|
|
|
// we let the new DDB surfaces be converted back to dibsections if
|
|
|
|
// acquire_source_image is called on them
|
|
|
|
cairo_win32_surface_set_can_convert_to_dib(raw->CairoSurface(), TRUE);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPrinting(const nsAString& aTitle,
|
|
|
|
const nsAString& aPrintToFileName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2010-11-13 03:02:00 -08:00
|
|
|
#define DOC_TITLE_LENGTH (MAX_PATH-1)
|
2008-08-27 14:44:55 -07:00
|
|
|
DOCINFOW docinfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
nsString titleStr(aTitle);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (titleStr.Length() > DOC_TITLE_LENGTH) {
|
|
|
|
titleStr.SetLength(DOC_TITLE_LENGTH-3);
|
|
|
|
titleStr.AppendLiteral("...");
|
|
|
|
}
|
2008-03-11 15:46:26 -07:00
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
nsString docName(aPrintToFileName);
|
2007-03-22 10:30:00 -07:00
|
|
|
docinfo.cbSize = sizeof(docinfo);
|
2008-08-27 14:44:55 -07:00
|
|
|
docinfo.lpszDocName = titleStr.Length() > 0 ? titleStr.get() : L"Mozilla Document";
|
2012-07-30 07:20:58 -07:00
|
|
|
docinfo.lpszOutput = docName.Length() > 0 ? docName.get() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
docinfo.lpszDatatype = NULL;
|
|
|
|
docinfo.fwType = 0;
|
|
|
|
|
2008-08-27 14:44:55 -07:00
|
|
|
::StartDocW(mDC, &docinfo);
|
2008-03-26 18:21:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPrinting()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::EndDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::AbortPrinting()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::AbortDoc(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::BeginPage()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::StartPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-16 11:57:34 -07:00
|
|
|
nsresult
|
|
|
|
gfxWindowsSurface::EndPage()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-06-16 11:57:34 -07:00
|
|
|
#ifdef NS_PRINTING
|
2007-09-24 15:50:44 -07:00
|
|
|
if (mForPrinting)
|
|
|
|
cairo_surface_show_page(CairoSurface());
|
2008-03-26 18:21:46 -07:00
|
|
|
int result = ::EndPage(mDC);
|
|
|
|
if (result <= 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2009-06-16 11:57:34 -07:00
|
|
|
#else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-29 12:06:56 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2009-06-16 11:57:34 -07:00
|
|
|
gfxWindowsSurface::GetDefaultContextFlags() const
|
2007-11-29 12:06:56 -08:00
|
|
|
{
|
|
|
|
if (mForPrinting)
|
2008-01-06 16:50:18 -08:00
|
|
|
return gfxContext::FLAG_SIMPLIFY_OPERATORS |
|
2011-01-15 01:40:33 -08:00
|
|
|
gfxContext::FLAG_DISABLE_SNAPPING |
|
|
|
|
gfxContext::FLAG_DISABLE_COPY_BACKGROUND;
|
2007-11-29 12:06:56 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-18 06:20:27 -07:00
|
|
|
|
2011-11-17 20:00:37 -08: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 07:20:58 -07:00
|
|
|
NS_ASSERTION(mSurface != nullptr, "CairoSurface() shouldn't be nullptr when mSurfaceValid is TRUE!");
|
2011-11-17 20:00:37 -08:00
|
|
|
|
|
|
|
return gfxIntSize(cairo_win32_surface_get_width(mSurface),
|
|
|
|
cairo_win32_surface_get_height(mSurface));
|
|
|
|
}
|
|
|
|
|
2011-07-18 06:20:27 -07:00
|
|
|
gfxASurface::MemoryLocation
|
|
|
|
gfxWindowsSurface::GetMemoryLocation() const
|
|
|
|
{
|
|
|
|
return MEMORY_IN_PROCESS_NONHEAP;
|
|
|
|
}
|