Bug 1638136 [wpt PR 23617] - Clipboard API Tests: Move permissions tests to WPT....
[gecko.git] / gfx / 2d / MacIOSurface.h
blobf459cf3260506f7c431ee2b1458de7ebea5dbd40
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__
9 #ifdef XP_DARWIN
10 # include <CoreVideo/CoreVideo.h>
11 # include <IOSurface/IOSurface.h>
12 # include <QuartzCore/QuartzCore.h>
13 # include <dlfcn.h>
15 # include "mozilla/gfx/Types.h"
16 # include "CFTypeRefPtr.h"
18 namespace mozilla {
19 namespace gl {
20 class GLContext;
22 } // namespace mozilla
24 struct _CGLContextObject;
26 typedef _CGLContextObject* CGLContextObj;
27 typedef uint32_t IOSurfaceID;
29 # ifdef XP_IOS
30 typedef kern_return_t IOReturn;
31 typedef int CGLError;
32 # endif
34 # ifdef XP_MACOSX
35 # import <OpenGL/OpenGL.h>
36 # else
37 # import <OpenGLES/ES2/gl.h>
38 # endif
40 # include "2D.h"
41 # include "mozilla/RefCounted.h"
42 # include "mozilla/RefPtr.h"
44 class MacIOSurface final
45 : public mozilla::external::AtomicRefCounted<MacIOSurface> {
46 public:
47 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(MacIOSurface)
48 typedef mozilla::gfx::SourceSurface SourceSurface;
49 typedef mozilla::gfx::DrawTarget DrawTarget;
50 typedef mozilla::gfx::BackendType BackendType;
52 // The usage count of the IOSurface is increased by 1 during the lifetime
53 // of the MacIOSurface instance.
54 // MacIOSurface holds a reference to the corresponding IOSurface.
56 static already_AddRefed<MacIOSurface> CreateIOSurface(
57 int aWidth, int aHeight, double aContentsScaleFactor = 1.0,
58 bool aHasAlpha = true);
59 static void ReleaseIOSurface(MacIOSurface* aIOSurface);
60 static already_AddRefed<MacIOSurface> LookupSurface(
61 IOSurfaceID aSurfaceID, double aContentsScaleFactor = 1.0,
62 bool aHasAlpha = true,
63 mozilla::gfx::YUVColorSpace aColorSpace =
64 mozilla::gfx::YUVColorSpace::UNKNOWN);
66 explicit MacIOSurface(CFTypeRefPtr<IOSurfaceRef> aIOSurfaceRef,
67 double aContentsScaleFactor = 1.0,
68 bool aHasAlpha = true,
69 mozilla::gfx::YUVColorSpace aColorSpace =
70 mozilla::gfx::YUVColorSpace::UNKNOWN);
71 ~MacIOSurface();
72 IOSurfaceID GetIOSurfaceID() const;
73 void* GetBaseAddress() const;
74 void* GetBaseAddressOfPlane(size_t planeIndex) const;
75 size_t GetPlaneCount() const;
76 OSType GetPixelFormat() const;
77 // GetWidth() and GetHeight() return values in "display pixels". A
78 // "display pixel" is the smallest fully addressable part of a display.
79 // But in HiDPI modes each "display pixel" corresponds to more than one
80 // device pixel. Use GetDevicePixel**() to get device pixels.
81 size_t GetWidth(size_t plane = 0) const;
82 size_t GetHeight(size_t plane = 0) const;
83 double GetContentsScaleFactor() const { return mContentsScaleFactor; }
84 size_t GetDevicePixelWidth(size_t plane = 0) const;
85 size_t GetDevicePixelHeight(size_t plane = 0) const;
86 size_t GetBytesPerRow(size_t plane = 0) const;
87 void Lock(bool aReadOnly = true);
88 void Unlock(bool aReadOnly = true);
89 bool IsLocked() const { return mIsLocked; }
90 void IncrementUseCount();
91 void DecrementUseCount();
92 bool HasAlpha() const { return mHasAlpha; }
93 mozilla::gfx::SurfaceFormat GetFormat() const;
94 mozilla::gfx::SurfaceFormat GetReadFormat() const;
95 // This would be better suited on MacIOSurfaceImage type, however due to the
96 // current data structure, this is not possible as only the IOSurfaceRef is
97 // being used across.
98 void SetYUVColorSpace(mozilla::gfx::YUVColorSpace aColorSpace) {
99 mColorSpace = aColorSpace;
101 mozilla::gfx::YUVColorSpace GetYUVColorSpace() const { return mColorSpace; }
102 bool IsFullRange() const {
103 return GetPixelFormat() == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
106 // We would like to forward declare NSOpenGLContext, but it is an @interface
107 // and this file is also used from c++, so we use a void *.
108 CGLError CGLTexImageIOSurface2D(
109 mozilla::gl::GLContext* aGL, CGLContextObj ctxt, size_t plane,
110 mozilla::gfx::SurfaceFormat* aOutReadFormat = nullptr);
111 CGLError CGLTexImageIOSurface2D(CGLContextObj ctxt, GLenum target,
112 GLenum internalFormat, GLsizei width,
113 GLsizei height, GLenum format, GLenum type,
114 GLuint plane) const;
115 already_AddRefed<SourceSurface> GetAsSurface();
117 // Creates a DrawTarget that wraps the data in the IOSurface. Rendering to
118 // this DrawTarget directly manipulates the contents of the IOSurface.
119 // Only call when the surface is already locked for writing!
120 // The returned DrawTarget must only be used while the surface is still
121 // locked.
122 // Also, only call this if you're reasonably sure that the DrawTarget of the
123 // selected backend supports the IOSurface's SurfaceFormat.
124 already_AddRefed<DrawTarget> GetAsDrawTargetLocked(BackendType aBackendType);
126 static size_t GetMaxWidth();
127 static size_t GetMaxHeight();
128 CFTypeRefPtr<IOSurfaceRef> GetIOSurfaceRef() { return mIOSurfaceRef; }
130 private:
131 CFTypeRefPtr<IOSurfaceRef> mIOSurfaceRef;
132 double mContentsScaleFactor;
133 bool mHasAlpha;
134 bool mIsLocked = false;
135 mozilla::gfx::YUVColorSpace mColorSpace =
136 mozilla::gfx::YUVColorSpace::UNKNOWN;
139 #endif
140 #endif