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