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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Nokia Corporation code.
17 : *
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.
21 : *
22 : * Contributor(s):
23 : * Oleg Romashin <romaxa@gmail.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef GFX_SHARED_IMAGESURFACE_H
40 : #define GFX_SHARED_IMAGESURFACE_H
41 :
42 : #include "mozilla/ipc/Shmem.h"
43 : #include "mozilla/ipc/SharedMemory.h"
44 :
45 : #include "gfxASurface.h"
46 : #include "gfxImageSurface.h"
47 :
48 : class THEBES_API gfxSharedImageSurface : public gfxImageSurface {
49 : typedef mozilla::ipc::SharedMemory SharedMemory;
50 : typedef mozilla::ipc::Shmem Shmem;
51 :
52 : public:
53 : virtual ~gfxSharedImageSurface();
54 :
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.
60 : *
61 : * NB: the *caller* is responsible for freeing the Shmem allocated
62 : * by this function.
63 : */
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)
70 : {
71 : return Create<ShmemAllocator, false>(aAllocator, aSize, aFormat, aShmType);
72 : }
73 :
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.
79 : */
80 : static already_AddRefed<gfxSharedImageSurface>
81 : Open(const Shmem& aShmem);
82 :
83 : template<class ShmemAllocator>
84 : static already_AddRefed<gfxSharedImageSurface>
85 0 : CreateUnsafe(ShmemAllocator* aAllocator,
86 : const gfxIntSize& aSize,
87 : gfxImageFormat aFormat,
88 : SharedMemory::SharedMemoryType aShmType = SharedMemory::TYPE_BASIC)
89 : {
90 0 : return Create<ShmemAllocator, true>(aAllocator, aSize, aFormat, aShmType);
91 : }
92 :
93 0 : Shmem& GetShmem() { return mShmem; }
94 :
95 : static bool IsSharedImage(gfxASurface *aSurface);
96 :
97 : private:
98 : gfxSharedImageSurface(const gfxIntSize&, gfxImageFormat, const Shmem&);
99 :
100 : void WriteShmemInfo();
101 :
102 : static size_t GetAlignedSize(const gfxIntSize&, long aStride);
103 :
104 : template<class ShmemAllocator, bool Unsafe>
105 : static already_AddRefed<gfxSharedImageSurface>
106 0 : Create(ShmemAllocator* aAllocator,
107 : const gfxIntSize& aSize,
108 : gfxImageFormat aFormat,
109 : SharedMemory::SharedMemoryType aShmType)
110 : {
111 0 : if (!CheckSurfaceSize(aSize))
112 0 : return nsnull;
113 :
114 0 : Shmem shmem;
115 0 : long stride = ComputeStride(aSize, aFormat);
116 0 : size_t size = GetAlignedSize(aSize, stride);
117 : if (!Unsafe) {
118 : if (!aAllocator->AllocShmem(size, aShmType, &shmem))
119 : return nsnull;
120 : } else {
121 0 : if (!aAllocator->AllocUnsafeShmem(size, aShmType, &shmem))
122 0 : return nsnull;
123 : }
124 :
125 : nsRefPtr<gfxSharedImageSurface> s =
126 0 : new gfxSharedImageSurface(aSize, aFormat, shmem);
127 0 : if (s->CairoStatus() != 0) {
128 0 : aAllocator->DeallocShmem(shmem);
129 0 : return nsnull;
130 : }
131 0 : s->WriteShmemInfo();
132 0 : return s.forget();
133 : }
134 :
135 : Shmem mShmem;
136 :
137 : // Calling these is very bad, disallow it
138 : gfxSharedImageSurface(const gfxSharedImageSurface&);
139 : gfxSharedImageSurface& operator=(const gfxSharedImageSurface&);
140 : };
141 :
142 : #endif /* GFX_SHARED_IMAGESURFACE_H */
|