2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#include "SourceSurfaceCairo.h"
|
2012-01-10 01:50:01 +04:00
|
|
|
#include "DrawTargetCairo.h"
|
2012-02-04 16:22:47 +04:00
|
|
|
#include "HelpersCairo.h"
|
2013-11-27 15:21:27 +04:00
|
|
|
#include "DataSourceSurfaceWrapper.h"
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-26 18:10:35 +04:00
|
|
|
#include "cairo.h"
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2012-01-10 01:50:01 +04:00
|
|
|
static SurfaceFormat CairoFormatToSurfaceFormat(cairo_format_t format) {
|
|
|
|
switch (format) {
|
|
|
|
case CAIRO_FORMAT_ARGB32:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8A8;
|
2012-01-10 01:50:01 +04:00
|
|
|
case CAIRO_FORMAT_RGB24:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8X8;
|
2016-04-07 23:40:15 +03:00
|
|
|
case CAIRO_FORMAT_RGB16_565:
|
|
|
|
return SurfaceFormat::R5G6B5_UINT16;
|
2012-01-10 01:50:01 +04:00
|
|
|
case CAIRO_FORMAT_A8:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::A8;
|
2012-02-04 16:22:47 +04:00
|
|
|
default:
|
2014-01-10 23:06:16 +04:00
|
|
|
return SurfaceFormat::B8G8R8A8;
|
2012-01-10 01:50:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceSurfaceCairo::SourceSurfaceCairo(
|
|
|
|
cairo_surface_t* aSurface, const IntSize& aSize,
|
2012-08-14 22:06:12 +04:00
|
|
|
const SurfaceFormat& aFormat, DrawTargetCairo* aDrawTarget /* = nullptr */)
|
2012-01-10 01:50:01 +04:00
|
|
|
: mSize(aSize),
|
|
|
|
mFormat(aFormat),
|
|
|
|
mSurface(aSurface),
|
|
|
|
mDrawTarget(aDrawTarget) {
|
|
|
|
cairo_surface_reference(mSurface);
|
2011-06-24 21:41:16 +04:00
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
SourceSurfaceCairo::~SourceSurfaceCairo() { cairo_surface_destroy(mSurface); }
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
IntSize SourceSurfaceCairo::GetSize() const { return mSize; }
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
SurfaceFormat SourceSurfaceCairo::GetFormat() const { return mFormat; }
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
already_AddRefed<DataSourceSurface> SourceSurfaceCairo::GetDataSurface() {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> dataSurf;
|
2012-01-10 01:50:01 +04:00
|
|
|
|
|
|
|
if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
|
|
|
|
dataSurf = new DataSourceSurfaceCairo(mSurface);
|
|
|
|
} else {
|
|
|
|
cairo_surface_t* imageSurf = cairo_image_surface_create(
|
|
|
|
GfxFormatToCairoFormat(mFormat), mSize.width, mSize.height);
|
|
|
|
|
|
|
|
// Fill the new image surface with the contents of our surface.
|
|
|
|
cairo_t* ctx = cairo_create(imageSurf);
|
2012-01-10 02:19:11 +04:00
|
|
|
cairo_set_source_surface(ctx, mSurface, 0, 0);
|
2012-01-10 01:50:01 +04:00
|
|
|
cairo_paint(ctx);
|
|
|
|
cairo_destroy(ctx);
|
|
|
|
|
|
|
|
dataSurf = new DataSourceSurfaceCairo(imageSurf);
|
|
|
|
cairo_surface_destroy(imageSurf);
|
|
|
|
}
|
|
|
|
|
2013-11-27 15:21:27 +04:00
|
|
|
// We also need to make sure that the returned surface has
|
2014-01-10 22:55:24 +04:00
|
|
|
// surface->GetType() == SurfaceType::DATA.
|
2015-04-30 22:20:30 +03:00
|
|
|
return MakeAndAddRef<DataSourceSurfaceWrapper>(dataSurf);
|
2011-06-24 21:41:16 +04:00
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2012-01-10 01:50:01 +04:00
|
|
|
cairo_surface_t* SourceSurfaceCairo::GetSurface() const { return mSurface; }
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2012-01-10 01:50:01 +04:00
|
|
|
void SourceSurfaceCairo::DrawTargetWillChange() {
|
|
|
|
if (mDrawTarget) {
|
2012-08-14 22:06:12 +04:00
|
|
|
mDrawTarget = nullptr;
|
2012-01-10 01:50:01 +04:00
|
|
|
|
|
|
|
// We're about to lose our version of the surface, so make a copy of it.
|
|
|
|
cairo_surface_t* surface = cairo_surface_create_similar(
|
|
|
|
mSurface, GfxFormatToCairoContent(mFormat), mSize.width, mSize.height);
|
|
|
|
cairo_t* ctx = cairo_create(surface);
|
|
|
|
cairo_pattern_t* pat = cairo_pattern_create_for_surface(mSurface);
|
|
|
|
cairo_set_source(ctx, pat);
|
|
|
|
cairo_paint(ctx);
|
|
|
|
cairo_destroy(ctx);
|
2012-07-24 14:18:38 +04:00
|
|
|
cairo_pattern_destroy(pat);
|
2012-01-10 01:50:01 +04:00
|
|
|
|
|
|
|
// Swap in this new surface.
|
|
|
|
cairo_surface_destroy(mSurface);
|
|
|
|
mSurface = surface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurfaceCairo::DataSourceSurfaceCairo(cairo_surface_t* imageSurf)
|
|
|
|
: mImageSurface(imageSurf) {
|
|
|
|
cairo_surface_reference(mImageSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurfaceCairo::~DataSourceSurfaceCairo() {
|
|
|
|
cairo_surface_destroy(mImageSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* DataSourceSurfaceCairo::GetData() {
|
|
|
|
return cairo_image_surface_get_data(mImageSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DataSourceSurfaceCairo::Stride() {
|
|
|
|
return cairo_image_surface_get_stride(mImageSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize DataSourceSurfaceCairo::GetSize() const {
|
|
|
|
IntSize size;
|
|
|
|
size.width = cairo_image_surface_get_width(mImageSurface);
|
|
|
|
size.height = cairo_image_surface_get_height(mImageSurface);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceFormat DataSourceSurfaceCairo::GetFormat() const {
|
|
|
|
return CairoFormatToSurfaceFormat(
|
|
|
|
cairo_image_surface_get_format(mImageSurface));
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t* DataSourceSurfaceCairo::GetSurface() const {
|
|
|
|
return mImageSurface;
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|