Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / gfx / thebes / src / gfxQuartzNativeDrawing.cpp
blobd00dabd5b339822e4767838163b443dffb43320a
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 Thebes gfx.
17 * The Initial Developer of the Original Code is Mozilla Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Matthew Gregan <kinetik@flim.org>
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 "nsMathUtils.h"
40 #include "gfxQuartzNativeDrawing.h"
41 #include "gfxQuartzSurface.h"
43 // see cairo-quartz-surface.c for the complete list of these
44 enum {
45 kPrivateCGCompositeSourceOver = 2
48 // private Quartz routine needed here
49 extern "C" {
50 CG_EXTERN void CGContextSetCompositeOperation(CGContextRef, int);
53 gfxQuartzNativeDrawing::gfxQuartzNativeDrawing(gfxContext* ctx,
54 const gfxRect& nativeRect)
55 : mContext(ctx), mNativeRect(nativeRect)
59 CGContextRef
60 gfxQuartzNativeDrawing::BeginNativeDrawing()
62 NS_ASSERTION(!mQuartzSurface, "BeginNativeDrawing called when drawing already in progress");
64 gfxPoint deviceOffset;
65 nsRefPtr<gfxASurface> surf = mContext->CurrentSurface(&deviceOffset.x, &deviceOffset.y);
66 if (!surf || surf->CairoStatus())
67 return nsnull;
69 // if this is a native Quartz surface, we don't have to redirect
70 // rendering to our own CGContextRef; in most cases, we are able to
71 // use the CGContextRef from the surface directly. we can extend
72 // this to support offscreen drawing fairly easily in the future.
73 if (surf->GetType() == gfxASurface::SurfaceTypeQuartz &&
74 (surf->GetContentType() == gfxASurface::CONTENT_COLOR ||
75 (surf->GetContentType() == gfxASurface::CONTENT_COLOR_ALPHA))) {
76 mQuartzSurface = static_cast<gfxQuartzSurface*>(static_cast<gfxASurface*>(surf.get()));
77 } else {
78 // XXXkinetik we could create and use a temp surface here and draw
79 // it back to the gfxContext in EndNativeDrawing like the Windows
80 // version of this class
81 NS_WARNING("unhandled surface type");
82 return nsnull;
85 // Need to force the clip to be set
86 mContext->UpdateSurfaceClip();
88 // grab the CGContextRef
89 mCGContext = mQuartzSurface->GetCGContext();
90 if (!mCGContext)
91 return nsnull;
93 gfxMatrix m = mContext->CurrentMatrix();
94 CGContextSaveGState(mCGContext);
95 CGContextTranslateCTM(mCGContext, deviceOffset.x, deviceOffset.y);
97 // I -think- that this context will always have an identity
98 // transform (since we don't maintain a transform on it in
99 // cairo-land, and instead push/pop as needed)
101 gfxFloat x0 = m.x0;
102 gfxFloat y0 = m.y0;
104 // We round x0/y0 if we don't have a scale, because otherwise things get
105 // rendered badly
106 // XXX how should we be rounding x0/y0?
107 if (!m.HasNonTranslationOrFlip()) {
108 x0 = floor(x0 + 0.5);
109 y0 = floor(y0 + 0.5);
112 CGContextConcatCTM(mCGContext, CGAffineTransformMake(m.xx, m.yx,
113 m.xy, m.yy,
114 x0, y0));
116 // bug 382049 - need to explicity set the composite operation to sourceOver
117 CGContextSetCompositeOperation(mCGContext, kPrivateCGCompositeSourceOver);
119 return mCGContext;
122 void
123 gfxQuartzNativeDrawing::EndNativeDrawing()
125 NS_ASSERTION(mQuartzSurface, "EndNativeDrawing called without BeginNativeDrawing");
127 // we drew directly to a shared CGContextRef; restore previous context state
128 CGContextRestoreGState(mCGContext);
129 mQuartzSurface->MarkDirty();
130 mQuartzSurface = nsnull;