1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "mozilla/layers/TextureClientX11.h"
8
9 #include "mozilla/layers/CompositableClient.h"
10 #include "mozilla/layers/CompositableForwarder.h"
11 #include "mozilla/layers/ISurfaceAllocator.h"
12 #include "mozilla/layers/ShadowLayerUtilsX11.h"
13 #include "mozilla/layers/TextureForwarder.h"
14 #include "mozilla/gfx/2D.h"
15 #include "mozilla/gfx/Logging.h"
16 #include "gfx2DGlue.h"
17 #include "gfxPlatform.h"
18 #include "gfxXlibSurface.h"
19
20 #include "mozilla/X11Util.h"
21 #include <X11/Xlib.h>
22
23 using namespace mozilla;
24 using namespace mozilla::gfx;
25
26 namespace mozilla::layers {
27
X11TextureData(gfx::IntSize aSize,gfx::SurfaceFormat aFormat,bool aClientDeallocation,bool aIsCrossProcess,gfxXlibSurface * aSurface)28 X11TextureData::X11TextureData(gfx::IntSize aSize, gfx::SurfaceFormat aFormat,
29 bool aClientDeallocation, bool aIsCrossProcess,
30 gfxXlibSurface* aSurface)
31 : mSize(aSize),
32 mFormat(aFormat),
33 mSurface(aSurface),
34 mClientDeallocation(aClientDeallocation),
35 mIsCrossProcess(aIsCrossProcess) {
36 MOZ_ASSERT(mSurface);
37 }
38
Lock(OpenMode aMode)39 bool X11TextureData::Lock(OpenMode aMode) { return true; }
40
Unlock()41 void X11TextureData::Unlock() {
42 if (mSurface && mIsCrossProcess) {
43 FinishX(DefaultXDisplay());
44 }
45 }
46
FillInfo(TextureData::Info & aInfo) const47 void X11TextureData::FillInfo(TextureData::Info& aInfo) const {
48 aInfo.size = mSize;
49 aInfo.format = mFormat;
50 aInfo.supportsMoz2D = true;
51 aInfo.hasIntermediateBuffer = false;
52 aInfo.hasSynchronization = false;
53 aInfo.canExposeMappedData = false;
54 }
55
Serialize(SurfaceDescriptor & aOutDescriptor)56 bool X11TextureData::Serialize(SurfaceDescriptor& aOutDescriptor) {
57 MOZ_ASSERT(mSurface);
58 if (!mSurface) {
59 return false;
60 }
61
62 if (!mClientDeallocation) {
63 // Pass to the host the responsibility of freeing the pixmap. ReleasePixmap
64 // means the underlying pixmap will not be deallocated in mSurface's
65 // destructor. ToSurfaceDescriptor is at most called once per TextureClient.
66 mSurface->ReleasePixmap();
67 }
68
69 aOutDescriptor = SurfaceDescriptorX11(mSurface);
70 return true;
71 }
72
BorrowDrawTarget()73 already_AddRefed<gfx::DrawTarget> X11TextureData::BorrowDrawTarget() {
74 MOZ_ASSERT(mSurface);
75 if (!mSurface) {
76 return nullptr;
77 }
78
79 IntSize size = mSurface->GetSize();
80 RefPtr<gfx::DrawTarget> dt =
81 Factory::CreateDrawTargetForCairoSurface(mSurface->CairoSurface(), size);
82
83 return dt.forget();
84 }
85
UpdateFromSurface(gfx::SourceSurface * aSurface)86 bool X11TextureData::UpdateFromSurface(gfx::SourceSurface* aSurface) {
87 RefPtr<DrawTarget> dt = BorrowDrawTarget();
88
89 if (!dt) {
90 return false;
91 }
92
93 dt->CopySurface(aSurface, IntRect(IntPoint(), aSurface->GetSize()),
94 IntPoint());
95
96 return true;
97 }
98
Deallocate(LayersIPCChannel *)99 void X11TextureData::Deallocate(LayersIPCChannel*) { mSurface = nullptr; }
100
CreateSimilar(LayersIPCChannel * aAllocator,LayersBackend aLayersBackend,TextureFlags aFlags,TextureAllocationFlags aAllocFlags) const101 TextureData* X11TextureData::CreateSimilar(
102 LayersIPCChannel* aAllocator, LayersBackend aLayersBackend,
103 TextureFlags aFlags, TextureAllocationFlags aAllocFlags) const {
104 return X11TextureData::Create(mSize, mFormat, aFlags, aAllocator);
105 }
106
Create(gfx::IntSize aSize,gfx::SurfaceFormat aFormat,TextureFlags aFlags,LayersIPCChannel * aAllocator)107 X11TextureData* X11TextureData::Create(gfx::IntSize aSize,
108 gfx::SurfaceFormat aFormat,
109 TextureFlags aFlags,
110 LayersIPCChannel* aAllocator) {
111 MOZ_ASSERT(aSize.width >= 0 && aSize.height >= 0);
112 if (aSize.width <= 0 || aSize.height <= 0 ||
113 aSize.width > XLIB_IMAGE_SIDE_SIZE_LIMIT ||
114 aSize.height > XLIB_IMAGE_SIDE_SIZE_LIMIT) {
115 gfxDebug() << "Asking for X11 surface of invalid size " << aSize.width
116 << "x" << aSize.height;
117 return nullptr;
118 }
119 gfxImageFormat imageFormat = SurfaceFormatToImageFormat(aFormat);
120 RefPtr<gfxASurface> surface =
121 gfxPlatform::GetPlatform()->CreateOffscreenSurface(aSize, imageFormat);
122 if (!surface || surface->GetType() != gfxSurfaceType::Xlib) {
123 NS_ERROR("creating Xlib surface failed!");
124 return nullptr;
125 }
126
127 gfxXlibSurface* xlibSurface = static_cast<gfxXlibSurface*>(surface.get());
128
129 bool crossProcess = !aAllocator->IsSameProcess();
130 X11TextureData* texture = new X11TextureData(
131 aSize, aFormat, !!(aFlags & TextureFlags::DEALLOCATE_CLIENT),
132 crossProcess, xlibSurface);
133 if (crossProcess) {
134 FinishX(DefaultXDisplay());
135 }
136
137 return texture;
138 }
139
140 } // namespace mozilla::layers
141