Bug 435739 Poor performance of Firefox 3 with no X RENDER extension
[wine-gecko.git] / gfx / thebes / src / gfxImageSurface.cpp
blob486cf51282284de3a45d1577ebaf1744aea95261
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Oracle Corporation code.
17 * The Initial Developer of the Original Code is Oracle Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Stuart Parmenter <pavlov@pavlov.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "prmem.h"
40 #include "gfxImageSurface.h"
42 #include "cairo.h"
44 gfxImageSurface::gfxImageSurface(const gfxIntSize& size, gfxImageFormat format) :
45 mSize(size), mFormat(format)
47 mStride = ComputeStride();
49 if (!CheckSurfaceSize(size))
50 return;
52 // if we have a zero-sized surface, just set mData to nsnull
53 if (mSize.height * mStride > 0) {
54 mData = (unsigned char *) calloc(mSize.height, mStride);
55 if (!mData)
56 return;
57 } else {
58 mData = nsnull;
61 mOwnsData = PR_TRUE;
63 cairo_surface_t *surface =
64 cairo_image_surface_create_for_data((unsigned char*)mData,
65 (cairo_format_t)format,
66 mSize.width,
67 mSize.height,
68 mStride);
69 Init(surface);
72 gfxImageSurface::gfxImageSurface(cairo_surface_t *csurf)
74 mSize.width = cairo_image_surface_get_width(csurf);
75 mSize.height = cairo_image_surface_get_height(csurf);
76 mData = cairo_image_surface_get_data(csurf);
77 mFormat = (gfxImageFormat) cairo_image_surface_get_format(csurf);
78 mOwnsData = PR_FALSE;
79 mStride = cairo_image_surface_get_stride(csurf);
81 Init(csurf, PR_TRUE);
84 gfxImageSurface::~gfxImageSurface()
86 if (!mSurfaceValid)
87 return;
89 if (mOwnsData) {
90 free(mData);
91 mData = nsnull;
95 long
96 gfxImageSurface::ComputeStride() const
98 long stride;
100 if (mFormat == ImageFormatARGB32)
101 stride = mSize.width * 4;
102 else if (mFormat == ImageFormatRGB24)
103 stride = mSize.width * 4;
104 else if (mFormat == ImageFormatA8)
105 stride = mSize.width;
106 else if (mFormat == ImageFormatA1) {
107 stride = (mSize.width + 7) / 8;
108 } else {
109 NS_WARNING("Unknown format specified to gfxImageSurface!");
110 stride = mSize.width * 4;
113 stride = ((stride + 3) / 4) * 4;
115 return stride;
118 PRBool
119 gfxImageSurface::CopyFrom(gfxImageSurface *other)
121 if (other->mSize != mSize)
123 return PR_FALSE;
126 if (other->mFormat != mFormat &&
127 !(other->mFormat == ImageFormatARGB32 && mFormat == ImageFormatRGB24) &&
128 !(other->mFormat == ImageFormatRGB24 && mFormat == ImageFormatARGB32))
130 return PR_FALSE;
133 if (other->mStride == mStride) {
134 memcpy (mData, other->mData, mStride * mSize.height);
135 } else {
136 int lineSize = PR_MIN(other->mStride, mStride);
137 for (int i = 0; i < mSize.height; i++) {
138 unsigned char *src = other->mData + other->mStride * i;
139 unsigned char *dst = mData + mStride * i;
141 memcpy (dst, src, lineSize);
145 return PR_TRUE;