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/. */
7 #ifndef MacIOSurface_h__
8 #define MacIOSurface_h__
10 # include <CoreVideo/CoreVideo.h>
11 # include <IOSurface/IOSurfaceRef.h>
12 # include <QuartzCore/QuartzCore.h>
15 # include "mozilla/gfx/Types.h"
16 # include "CFTypeRefPtr.h"
22 } // namespace mozilla
25 struct _CGLContextObject
;
27 typedef _CGLContextObject
* CGLContextObj
;
29 typedef uint32_t IOSurfaceID
;
32 # import <OpenGL/OpenGL.h>
35 typedef realGLboolean GLboolean
;
36 # include <OpenGLES/ES2/gl.h>
40 # include "mozilla/RefCounted.h"
41 # include "mozilla/RefPtr.h"
43 class MacIOSurface final
44 : public mozilla::external::AtomicRefCounted
<MacIOSurface
> {
46 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface
)
47 typedef mozilla::gfx::SourceSurface SourceSurface
;
48 typedef mozilla::gfx::DrawTarget DrawTarget
;
49 typedef mozilla::gfx::BackendType BackendType
;
50 typedef mozilla::gfx::IntSize IntSize
;
51 typedef mozilla::gfx::ChromaSubsampling ChromaSubsampling
;
52 typedef mozilla::gfx::YUVColorSpace YUVColorSpace
;
53 typedef mozilla::gfx::ColorSpace2 ColorSpace2
;
54 typedef mozilla::gfx::TransferFunction TransferFunction
;
55 typedef mozilla::gfx::ColorRange ColorRange
;
56 typedef mozilla::gfx::ColorDepth ColorDepth
;
58 // The usage count of the IOSurface is increased by 1 during the lifetime
59 // of the MacIOSurface instance.
60 // MacIOSurface holds a reference to the corresponding IOSurface.
62 static already_AddRefed
<MacIOSurface
> CreateIOSurface(int aWidth
, int aHeight
,
63 bool aHasAlpha
= true);
64 static already_AddRefed
<MacIOSurface
> CreateBiPlanarSurface(
65 const IntSize
& aYSize
, const IntSize
& aCbCrSize
,
66 ChromaSubsampling aChromaSubsampling
, YUVColorSpace aColorSpace
,
67 TransferFunction aTransferFunction
, ColorRange aColorRange
,
68 ColorDepth aColorDepth
);
69 static already_AddRefed
<MacIOSurface
> CreateSinglePlanarSurface(
70 const IntSize
& aSize
, YUVColorSpace aColorSpace
,
71 TransferFunction aTransferFunction
, ColorRange aColorRange
);
72 static void ReleaseIOSurface(MacIOSurface
* aIOSurface
);
73 static already_AddRefed
<MacIOSurface
> LookupSurface(
74 IOSurfaceID aSurfaceID
, bool aHasAlpha
= true,
75 mozilla::gfx::YUVColorSpace aColorSpace
=
76 mozilla::gfx::YUVColorSpace::Identity
);
78 explicit MacIOSurface(CFTypeRefPtr
<IOSurfaceRef
> aIOSurfaceRef
,
79 bool aHasAlpha
= true,
80 mozilla::gfx::YUVColorSpace aColorSpace
=
81 mozilla::gfx::YUVColorSpace::Identity
);
84 IOSurfaceID
GetIOSurfaceID() const;
85 void* GetBaseAddress() const;
86 void* GetBaseAddressOfPlane(size_t planeIndex
) const;
87 size_t GetPlaneCount() const;
88 OSType
GetPixelFormat() const;
89 // GetWidth() and GetHeight() return values in "display pixels". A
90 // "display pixel" is the smallest fully addressable part of a display.
91 // But in HiDPI modes each "display pixel" corresponds to more than one
92 // device pixel. Use GetDevicePixel**() to get device pixels.
93 size_t GetWidth(size_t plane
= 0) const;
94 size_t GetHeight(size_t plane
= 0) const;
95 IntSize
GetSize(size_t plane
= 0) const {
96 return IntSize(GetWidth(plane
), GetHeight(plane
));
98 size_t GetDevicePixelWidth(size_t plane
= 0) const;
99 size_t GetDevicePixelHeight(size_t plane
= 0) const;
100 size_t GetBytesPerRow(size_t plane
= 0) const;
101 size_t GetAllocSize() const;
102 void Lock(bool aReadOnly
= true);
103 void Unlock(bool aReadOnly
= true);
104 bool IsLocked() const { return mIsLocked
; }
105 void IncrementUseCount();
106 void DecrementUseCount();
107 bool HasAlpha() const { return mHasAlpha
; }
108 mozilla::gfx::SurfaceFormat
GetFormat() const;
109 mozilla::gfx::SurfaceFormat
GetReadFormat() const;
110 mozilla::gfx::ColorDepth
GetColorDepth() const;
111 // This would be better suited on MacIOSurfaceImage type, however due to the
112 // current data structure, this is not possible as only the IOSurfaceRef is
113 // being used across.
114 void SetYUVColorSpace(YUVColorSpace aColorSpace
) {
115 mColorSpace
= aColorSpace
;
117 YUVColorSpace
GetYUVColorSpace() const { return mColorSpace
; }
118 bool IsFullRange() const {
119 OSType format
= GetPixelFormat();
120 return (format
== kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
||
121 format
== kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
||
122 format
== kCVPixelFormatType_422YpCbCr10BiPlanarFullRange
||
123 format
== kCVPixelFormatType_422YpCbCr8FullRange
);
125 mozilla::gfx::ColorRange
GetColorRange() const {
126 if (IsFullRange()) return mozilla::gfx::ColorRange::FULL
;
127 return mozilla::gfx::ColorRange::LIMITED
;
130 // Bind this IOSurface to a texture using the most efficient mechanism
131 // available on the current platform.
133 // Note that on iOS simulator, due to incomplete support for
134 // texImageIOSurface, this will only use texImage2D to upload, and cannot be
135 // used to read-back the GL texture to an IOSurface.
136 bool BindTexImage(mozilla::gl::GLContext
* aGL
, size_t aPlane
,
137 mozilla::gfx::SurfaceFormat
* aOutReadFormat
= nullptr);
139 already_AddRefed
<SourceSurface
> GetAsSurface();
141 // Creates a DrawTarget that wraps the data in the IOSurface. Rendering to
142 // this DrawTarget directly manipulates the contents of the IOSurface.
143 // Only call when the surface is already locked for writing!
144 // The returned DrawTarget must only be used while the surface is still
146 // Also, only call this if you're reasonably sure that the DrawTarget of the
147 // selected backend supports the IOSurface's SurfaceFormat.
148 already_AddRefed
<DrawTarget
> GetAsDrawTargetLocked(BackendType aBackendType
);
150 static size_t GetMaxWidth();
151 static size_t GetMaxHeight();
152 CFTypeRefPtr
<IOSurfaceRef
> GetIOSurfaceRef() { return mIOSurfaceRef
; }
154 void SetColorSpace(mozilla::gfx::ColorSpace2
) const;
156 ColorSpace2 mColorPrimaries
= ColorSpace2::UNKNOWN
;
159 CFTypeRefPtr
<IOSurfaceRef
> mIOSurfaceRef
;
160 const bool mHasAlpha
;
161 YUVColorSpace mColorSpace
= YUVColorSpace::Identity
;
162 bool mIsLocked
= false;