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 : #include "base/basictypes.h"
40 : #include "gfxSharedImageSurface.h"
41 : #include "cairo.h"
42 :
43 : #define MOZ_ALIGN_WORD(x) (((x) + 3) & ~3)
44 :
45 : using namespace mozilla::ipc;
46 :
47 : static const cairo_user_data_key_t SHM_KEY = {0};
48 :
49 : struct SharedImageInfo {
50 : PRInt32 width;
51 : PRInt32 height;
52 : PRInt32 format;
53 : };
54 :
55 : static SharedImageInfo*
56 0 : GetShmInfoPtr(const Shmem& aShmem)
57 : {
58 : return reinterpret_cast<SharedImageInfo*>
59 0 : (aShmem.get<char>() + aShmem.Size<char>() - sizeof(SharedImageInfo));
60 : }
61 :
62 0 : gfxSharedImageSurface::~gfxSharedImageSurface()
63 : {
64 0 : MOZ_COUNT_DTOR(gfxSharedImageSurface);
65 0 : }
66 :
67 : /*static*/ bool
68 0 : gfxSharedImageSurface::IsSharedImage(gfxASurface* aSurface)
69 : {
70 : return (aSurface
71 0 : && aSurface->GetType() == gfxASurface::SurfaceTypeImage
72 0 : && aSurface->GetData(&SHM_KEY));
73 : }
74 :
75 0 : gfxSharedImageSurface::gfxSharedImageSurface(const gfxIntSize& aSize,
76 : gfxImageFormat aFormat,
77 0 : const Shmem& aShmem)
78 : {
79 0 : MOZ_COUNT_CTOR(gfxSharedImageSurface);
80 :
81 0 : mSize = aSize;
82 0 : mFormat = aFormat;
83 0 : mStride = ComputeStride(aSize, aFormat);
84 0 : mShmem = aShmem;
85 0 : mData = aShmem.get<unsigned char>();
86 : cairo_surface_t *surface =
87 : cairo_image_surface_create_for_data(mData,
88 : (cairo_format_t)mFormat,
89 : mSize.width,
90 : mSize.height,
91 0 : mStride);
92 0 : if (surface) {
93 0 : cairo_surface_set_user_data(surface, &SHM_KEY, this, NULL);
94 : }
95 0 : Init(surface);
96 0 : }
97 :
98 : void
99 0 : gfxSharedImageSurface::WriteShmemInfo()
100 : {
101 0 : SharedImageInfo* shmInfo = GetShmInfoPtr(mShmem);
102 0 : shmInfo->width = mSize.width;
103 0 : shmInfo->height = mSize.height;
104 0 : shmInfo->format = mFormat;
105 0 : }
106 :
107 : /*static*/ size_t
108 0 : gfxSharedImageSurface::GetAlignedSize(const gfxIntSize& aSize, long aStride)
109 : {
110 0 : return MOZ_ALIGN_WORD(sizeof(SharedImageInfo) + aSize.height * aStride);
111 : }
112 :
113 : /*static*/ already_AddRefed<gfxSharedImageSurface>
114 0 : gfxSharedImageSurface::Open(const Shmem& aShmem)
115 : {
116 0 : SharedImageInfo* shmInfo = GetShmInfoPtr(aShmem);
117 0 : gfxIntSize size(shmInfo->width, shmInfo->height);
118 0 : if (!CheckSurfaceSize(size))
119 0 : return nsnull;
120 :
121 : nsRefPtr<gfxSharedImageSurface> s =
122 : new gfxSharedImageSurface(size,
123 : (gfxImageFormat)shmInfo->format,
124 0 : aShmem);
125 : // We didn't create this Shmem and so don't free it on errors
126 0 : return (s->CairoStatus() != 0) ? nsnull : s.forget();
127 : }
|