IOSurfacePort.h (2194B)
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 #ifndef mozilla_gfx_IOSurfacePort_h__ 8 #define mozilla_gfx_IOSurfacePort_h__ 9 10 #include "chrome/common/ipc_message_utils.h" 11 12 #ifdef XP_DARWIN 13 # include <IOSurface/IOSurfaceRef.h> 14 # include "mozilla/UniquePtrExtensions.h" 15 # include "CFTypeRefPtr.h" 16 #endif 17 18 // This header file defines an IOSurface struct. 19 // 20 // It is a separate file for the following reasons: 21 // - Forward-declaring CFTypeRefPtr<IOSurfaceRef> correctly is tricky. 22 // - IOSurfaceRef.h defines global types like "Point" which can interfere with 23 // name resolution. 24 // - Smaller header files allow limiting the scope of the name pollution. 25 26 namespace mozilla::layers { 27 28 // The IPC descriptor for an IOSurface. 29 // As long as the port is alive, the IOSurface is automatically marked as "in 30 // use", including while the port is in IPC transit. 31 struct IOSurfacePort { 32 #ifdef XP_DARWIN 33 UniqueMachSendRight mPort; 34 35 CFTypeRefPtr<IOSurfaceRef> GetSurface() const { 36 // Note that IOSurfaceLookupFromMachPort does *not* consume the port. 37 if (IOSurfaceRef s = IOSurfaceLookupFromMachPort(mPort.get())) { 38 return CFTypeRefPtr<IOSurfaceRef>::WrapUnderCreateRule(s); 39 } 40 return {}; 41 } 42 43 static IOSurfacePort FromSurface(const CFTypeRefPtr<IOSurfaceRef>& aSurface) { 44 return {UniqueMachSendRight(IOSurfaceCreateMachPort(aSurface.get()))}; 45 } 46 #endif 47 }; 48 49 } // namespace mozilla::layers 50 51 namespace IPC { 52 53 template <> 54 struct ParamTraits<mozilla::layers::IOSurfacePort> { 55 using paramType = mozilla::layers::IOSurfacePort; 56 57 static void Write(MessageWriter* writer, paramType&& param) { 58 #ifdef XP_DARWIN 59 WriteParam(writer, std::move(param.mPort)); 60 #endif 61 } 62 static bool Read(MessageReader* reader, paramType* result) { 63 #ifdef XP_DARWIN 64 return ReadParam(reader, &result->mPort); 65 #else 66 return true; 67 #endif 68 } 69 }; 70 71 } // namespace IPC 72 73 #endif /* mozilla_gfx_IOSurfacePort_h__ */