Backed out changeset b88172246b66 due to Win32 debug failures.
[mozilla-central.git] / gfx / thebes / gfxSharedImageSurface.h
blob7f80c56e812df25e20448e06df2d778e9d5e28d0
1 // vim:set ts=4 sts=4 sw=4 et cin:
2 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Nokia Corporation code.
18 * The Initial Developer of the Original Code is Nokia Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Oleg Romashin <romaxa@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef GFX_SHARED_IMAGESURFACE_H
40 #define GFX_SHARED_IMAGESURFACE_H
42 #include "mozilla/ipc/Shmem.h"
43 #include "mozilla/ipc/SharedMemory.h"
45 #include "gfxASurface.h"
46 #include "gfxImageSurface.h"
48 class THEBES_API gfxSharedImageSurface : public gfxImageSurface {
49 typedef mozilla::ipc::SharedMemory SharedMemory;
50 typedef mozilla::ipc::Shmem Shmem;
52 public:
53 virtual ~gfxSharedImageSurface();
55 /**
56 * Return a new gfxSharedImageSurface around a shmem segment newly
57 * allocated by this function. |aAllocator| is the object used to
58 * allocate the new shmem segment. Null is returned if creating
59 * the surface failed.
61 * NB: the *caller* is responsible for freeing the Shmem allocated
62 * by this function.
64 template<class ShmemAllocator>
65 static already_AddRefed<gfxSharedImageSurface>
66 Create(ShmemAllocator* aAllocator,
67 const gfxIntSize& aSize,
68 gfxImageFormat aFormat,
69 SharedMemory::SharedMemoryType aShmType = SharedMemory::TYPE_BASIC)
71 return Create<ShmemAllocator, false>(aAllocator, aSize, aFormat, aShmType);
74 /**
75 * Return a new gfxSharedImageSurface that wraps a shmem segment
76 * already created by the Create() above. Bad things will happen
77 * if an attempt is made to wrap any other shmem segment. Null is
78 * returned if creating the surface failed.
80 static already_AddRefed<gfxSharedImageSurface>
81 Open(const Shmem& aShmem);
83 template<class ShmemAllocator>
84 static already_AddRefed<gfxSharedImageSurface>
85 CreateUnsafe(ShmemAllocator* aAllocator,
86 const gfxIntSize& aSize,
87 gfxImageFormat aFormat,
88 SharedMemory::SharedMemoryType aShmType = SharedMemory::TYPE_BASIC)
90 return Create<ShmemAllocator, true>(aAllocator, aSize, aFormat, aShmType);
93 Shmem& GetShmem() { return mShmem; }
95 static PRBool IsSharedImage(gfxASurface *aSurface);
97 private:
98 gfxSharedImageSurface(const gfxIntSize&, gfxImageFormat, const Shmem&);
100 void WriteShmemInfo();
102 static size_t GetAlignedSize(const gfxIntSize&, long aStride);
104 template<class ShmemAllocator, bool Unsafe>
105 static already_AddRefed<gfxSharedImageSurface>
106 Create(ShmemAllocator* aAllocator,
107 const gfxIntSize& aSize,
108 gfxImageFormat aFormat,
109 SharedMemory::SharedMemoryType aShmType)
111 if (!CheckSurfaceSize(aSize))
112 return nsnull;
114 Shmem shmem;
115 long stride = ComputeStride(aSize, aFormat);
116 size_t size = GetAlignedSize(aSize, stride);
117 if (!Unsafe) {
118 if (!aAllocator->AllocShmem(size, aShmType, &shmem))
119 return nsnull;
120 } else {
121 if (!aAllocator->AllocUnsafeShmem(size, aShmType, &shmem))
122 return nsnull;
125 nsRefPtr<gfxSharedImageSurface> s =
126 new gfxSharedImageSurface(aSize, aFormat, shmem);
127 if (s->CairoStatus() != 0) {
128 aAllocator->DeallocShmem(shmem);
129 return nsnull;
131 s->WriteShmemInfo();
132 return s.forget();
135 Shmem mShmem;
137 // Calling these is very bad, disallow it
138 gfxSharedImageSurface(const gfxSharedImageSurface&);
139 gfxSharedImageSurface& operator=(const gfxSharedImageSurface&);
142 #endif /* GFX_SHARED_IMAGESURFACE_H */