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 nsCoreAnimationSupport_h__
8 #define nsCoreAnimationSupport_h__
9 #ifdef XP_MACOSX
10 
11 #  import <OpenGL/OpenGL.h>
12 #  import <OpenGL/gl.h>
13 #  import "ApplicationServices/ApplicationServices.h"
14 #  include "gfxTypes.h"
15 #  include "mozilla/RefPtr.h"
16 #  include "mozilla/gfx/MacIOSurface.h"
17 #  include "nsError.h"
18 
19 // Get the system color space.
20 CGColorSpaceRef CreateSystemColorSpace();
21 
22 // Manages a CARenderer
23 struct _CGLContextObject;
24 
25 enum AllowOfflineRendererEnum {
26   ALLOW_OFFLINE_RENDERER,
27   DISALLOW_OFFLINE_RENDERER
28 };
29 
30 class nsCARenderer : public mozilla::RefCounted<nsCARenderer> {
31  public:
MOZ_DECLARE_REFCOUNTED_TYPENAME(nsCARenderer)32   MOZ_DECLARE_REFCOUNTED_TYPENAME(nsCARenderer)
33   nsCARenderer()
34       : mCARenderer(nullptr),
35         mWrapperCALayer(nullptr),
36         mFBOTexture(0),
37         mOpenGLContext(nullptr),
38         mCGImage(nullptr),
39         mCGData(nullptr),
40         mIOSurface(nullptr),
41         mFBO(0),
42         mIOTexture(0),
43         mUnsupportedWidth(UINT32_MAX),
44         mUnsupportedHeight(UINT32_MAX),
45         mAllowOfflineRenderer(DISALLOW_OFFLINE_RENDERER),
46         mContentsScaleFactor(1.0) {}
47   ~nsCARenderer();
48   // aWidth and aHeight are in "display pixels".  A "display pixel" is the
49   // smallest fully addressable part of a display.  But in HiDPI modes each
50   // "display pixel" corresponds to more than one device pixel.  Multiply
51   // display pixels by aContentsScaleFactor to get device pixels.
52   nsresult SetupRenderer(void* aCALayer, int aWidth, int aHeight,
53                          double aContentsScaleFactor,
54                          AllowOfflineRendererEnum aAllowOfflineRenderer);
55   // aWidth and aHeight are in "display pixels".  Multiply by
56   // aContentsScaleFactor to get device pixels.
57   nsresult Render(int aWidth, int aHeight, double aContentsScaleFactor,
58                   CGImageRef* aOutCAImage);
isInit()59   bool isInit() { return mCARenderer != nullptr; }
60   /*
61    * Render the CALayer to an IOSurface. If no IOSurface
62    * is attached then an internal pixel buffer will be
63    * used.
64    */
65   void AttachIOSurface(MacIOSurface* aSurface);
66   IOSurfaceID GetIOSurfaceID();
67   // aX, aY, aWidth and aHeight are in "display pixels".  Multiply by
68   // surf->GetContentsScaleFactor() to get device pixels.
69   static nsresult DrawSurfaceToCGContext(CGContextRef aContext,
70                                          MacIOSurface* surf,
71                                          CGColorSpaceRef aColorSpace, int aX,
72                                          int aY, size_t aWidth, size_t aHeight);
73 
74   // Remove & Add the layer without destroying
75   // the renderer for fast back buffer swapping.
76   void DetachCALayer();
77   void AttachCALayer(void* aCALayer);
78 #  ifdef DEBUG
79   static void SaveToDisk(MacIOSurface* surf);
80 #  endif
81  private:
82   // aWidth and aHeight are in "display pixels".  Multiply by
83   // mContentsScaleFactor to get device pixels.
84   void SetBounds(int aWidth, int aHeight);
85   // aWidth and aHeight are in "display pixels".  Multiply by
86   // mContentsScaleFactor to get device pixels.
87   void SetViewport(int aWidth, int aHeight);
88   void Destroy();
89 
90   void* mCARenderer;
91   void* mWrapperCALayer;
92   GLuint mFBOTexture;
93   _CGLContextObject* mOpenGLContext;
94   CGImageRef mCGImage;
95   void* mCGData;
96   RefPtr<MacIOSurface> mIOSurface;
97   uint32_t mFBO;
98   uint32_t mIOTexture;
99   int mUnsupportedWidth;
100   int mUnsupportedHeight;
101   AllowOfflineRendererEnum mAllowOfflineRenderer;
102   double mContentsScaleFactor;
103 };
104 
105 #endif  // XP_MACOSX
106 #endif  // nsCoreAnimationSupport_h__
107