cairo-templates.h revision 259fbef23dd30285e7355b13c6ce524565fa0cb6
/**
* @file
* @brief Cairo software blending templates
*//*
* Authors:
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
*
* Copyright (C) 2010 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_OPENMP
#include <omp.h>
#include "preferences.h"
// single-threaded operation if the number of pixels is below this threshold
#define OPENMP_THRESHOLD 4096
#endif
#include <algorithm>
#include <cairo.h>
#include <glib.h>
#include "display/nr-3dutils.h"
/**
* @brief Blend two surfaces using the supplied functor.
* This template blends two Cairo image surfaces using a blending functor that takes
* two 32-bit ARGB pixel values and returns a modified 32-bit pixel value.
* Differences in input surface formats are handled transparently. In future, this template
* will also handle software fallback for GL surfaces. */
void ink_cairo_surface_blend(cairo_surface_t *in1, cairo_surface_t *in2, cairo_surface_t *out, Blend blend)
{
// ASSUMPTIONS
// 1. Cairo ARGB32 surface strides are always divisible by 4
// 2. We can only receive CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_A8 surfaces
// 3. Both surfaces are of the same size
// 4. Output surface is ARGB32 if at least one input is ARGB32
int w = cairo_image_surface_get_width(in2);
int h = cairo_image_surface_get_height(in2);
// Check whether we can loop over pixels without taking stride into account.
bool fast_path = true;
int limit = w * h;
// NOTE
// OpenMP probably doesn't help much here.
// It would be better to render more than 1 tile at a time.
#if HAVE_OPENMP
int num_threads = prefs->getIntLimited("/options/threading/numthreads", omp_get_num_procs(), 1, 256);
#endif
// The number of code paths here is evil.
if (bpp1 == 4) {
if (bpp2 == 4) {
if (fast_path) {
#if HAVE_OPENMP
#endif
for (int i = 0; i < limit; ++i) {
}
} else {
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
}
}
}
} else {
// bpp2 == 1
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
in2_px <<= 24;
}
}
}
} else {
if (bpp2 == 4) {
// bpp1 == 1
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
in1_px <<= 24;
}
}
} else {
// bpp1 == 1 && bpp2 == 1
if (fast_path) {
#if HAVE_OPENMP
#endif
for (int i = 0; i < limit; ++i) {
}
} else {
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
}
}
}
}
}
}
{
// ASSUMPTIONS
// 1. Cairo ARGB32 surface strides are always divisible by 4
// 2. We can only receive CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_A8 surfaces
// 3. Surfaces have the same dimensions
// 4. Output surface is A8 if input is A8
int w = cairo_image_surface_get_width(in);
int h = cairo_image_surface_get_height(in);
int limit = w * h;
// Check whether we can loop over pixels without taking stride into account.
bool fast_path = true;
#if HAVE_OPENMP
int num_threads = prefs->getIntLimited("/options/threading/numthreads", omp_get_num_procs(), 1, 256);
#endif
if (bppin == 4) {
if (bppout == 4) {
// bppin == 4, bppout == 4
if (fast_path) {
#if HAVE_OPENMP
#endif
for (int i = 0; i < limit; ++i) {
}
} else {
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
}
}
}
} else {
// bppin == 4, bppout == 1
// we use this path with COLORMATRIX_LUMINANCETOALPHA
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
}
}
}
} else {
// bppin == 1, bppout == 1
// Note: there is no path for bppin == 1, bppout == 4 because it is useless
if (fast_path) {
#if HAVE_OPENMP
#endif
for (int i = 0; i < limit; ++i) {
}
} else {
#if HAVE_OPENMP
#endif
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
}
}
}
}
}
/**
* @brief Synthesize surface pixels based on their position.
* This template accepts a functor that gets called with the x and y coordinates of the pixels,
* given as integers.
* @param out Output surface
* @param out_area The region of the output surface that should be synthesized
* @param synth Synthesis functor */
void ink_cairo_surface_synthesize(cairo_surface_t *out, cairo_rectangle_t const &out_area, Synth synth)
{
// ASSUMPTIONS
// 1. Cairo ARGB32 surface strides are always divisible by 4
// 2. We can only receive CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_A8 surfaces
// NOTE: fast path is not used, because we would need 2 divisions to get pixel indices
#if HAVE_OPENMP
int limit = w * h;
int num_threads = prefs->getIntLimited("/options/threading/numthreads", omp_get_num_procs(), 1, 256);
#endif
if (bppout == 4) {
#if HAVE_OPENMP
#endif
for (int i = out_area.y; i < h; ++i) {
for (int j = out_area.x; j < w; ++j) {
++out_p;
}
}
} else {
// bppout == 1
#if HAVE_OPENMP
#endif
for (int i = out_area.y; i < h; ++i) {
for (int j = out_area.x; j < w; ++j) {
++out_p;
}
}
}
}
{
int w = cairo_image_surface_get_width(out);
int h = cairo_image_surface_get_height(out);
area.x = 0;
area.y = 0;
}
struct SurfaceSynth {
{
}
if (_alpha) {
return *px << 24;
} else {
}
}
if (_alpha) {
return *px;
} else {
return (p & 0xff000000) >> 24;
}
}
// Below there are some multiplies by zero. They will be optimized out.
// Do not remove them, because they improve readability.
if (G_UNLIKELY(x == 0)) {
// leftmost column
if (G_UNLIKELY(y == 0)) {
// upper left corner
// lower left corner
} else {
// leftmost column
}
// rightmost column
if (G_UNLIKELY(y == 0)) {
// top right corner
// bottom right corner
} else {
// rightmost column
}
} else {
// interior
if (G_UNLIKELY(y == 0)) {
// top row
// bottom row
} else {
// interior pixels
// note: p11 is actually unused, so we don't fetch its value
}
}
return normal;
}
unsigned char *_px;
bool _alpha;
};
/*
// simple pixel accessor for image surface that handles different edge wrapping modes
class PixelAccessor {
public:
typedef PixelAccessor self;
enum EdgeMode {
EDGE_PAD,
EDGE_WRAP,
EDGE_ZERO
};
PixelAccessor(cairo_surface_t *s, EdgeMode e)
: _surface(s)
, _px(cairo_image_surface_get_data(s))
, _x(0), _y(0)
, _w(cairo_image_surface_get_width(s))
, _h(cairo_image_surface_get_height(s))
, _stride(cairo_image_surface_get_stride(s))
, _edge_mode(e)
, _alpha(cairo_image_surface_get_format(s) == CAIRO_FORMAT_A8)
{}
guint32 pixelAt(int x, int y) {
// This is a lot of ifs for a single pixel access. However, branch prediction
// should help us a lot, as the result of ifs is always the same for a single image.
int real_x = x, real_y = y;
switch (_edge_mode) {
case EDGE_PAD:
real_x = CLAMP(x, 0, _w-1);
real_y = CLAMP(y, 0, _h-1);
break;
case EDGE_WRAP:
real_x %= _w;
real_y %= _h;
break;
case EDGE_ZERO:
default:
if (x < 0 || x >= _w || y < 0 || y >= _h)
return 0;
break;
}
if (_alpha) {
return *(_px + real_y*_stride + real_x) << 24;
} else {
guint32 *px = reinterpret_cast<guint32*>(_px +real_y*_stride + real_x*4);
return *px;
}
}
private:
cairo_surface_t *_surface;
guint8 *_px;
int _x, _y, _w, _h, _stride;
EdgeMode _edge_mode;
bool _alpha;
};*/
// Some helpers for pixel manipulation
G_GNUC_CONST inline gint32
return v;
}
#define EXTRACT_ARGB32(px,a,r,g,b) \
guint32 a, r, g, b; \
b = (px & 0x000000ff);
#define ASSEMBLE_ARGB32(px,a,r,g,b) \
#endif
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :