1/*
2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/engine_configurations.h"
12
13#if defined(CARBON_RENDERING)
14
15#ifndef WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
16#define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
17
18#include "webrtc/base/platform_thread.h"
19#include "webrtc/modules/video_render/video_render_defines.h"
20
21#define NEW_HIVIEW_PARENT_EVENT_HANDLER 1
22#define NEW_HIVIEW_EVENT_HANDLER 1
23#define USE_STRUCT_RGN
24
25#include <AGL/agl.h>
26#include <Carbon/Carbon.h>
27#include <OpenGL/OpenGL.h>
28#include <OpenGL/glext.h>
29#include <OpenGL/glu.h>
30#include <list>
31#include <map>
32
33class VideoRenderAGL;
34
35namespace webrtc {
36class CriticalSectionWrapper;
37class EventWrapper;
38
39class VideoChannelAGL : public VideoRenderCallback {
40 public:
41
42  VideoChannelAGL(AGLContext& aglContext, int iId, VideoRenderAGL* owner);
43  virtual ~VideoChannelAGL();
44  virtual int FrameSizeChange(int width, int height, int numberOfStreams);
45  virtual int DeliverFrame(const VideoFrame& videoFrame);
46  virtual int UpdateSize(int width, int height);
47  int SetStreamSettings(int streamId, float startWidth, float startHeight,
48                        float stopWidth, float stopHeight);
49  int SetStreamCropSettings(int streamId, float startWidth, float startHeight,
50                            float stopWidth, float stopHeight);
51  int RenderOffScreenBuffer();
52  int IsUpdated(bool& isUpdated);
53  virtual int UpdateStretchSize(int stretchHeight, int stretchWidth);
54  virtual int32_t RenderFrame(const uint32_t streamId, VideoFrame& videoFrame);
55
56 private:
57
58  AGLContext _aglContext;
59  int _id;
60  VideoRenderAGL* _owner;
61  int _width;
62  int _height;
63  int _stretchedWidth;
64  int _stretchedHeight;
65  float _startHeight;
66  float _startWidth;
67  float _stopWidth;
68  float _stopHeight;
69  int _xOldWidth;
70  int _yOldHeight;
71  int _oldStretchedHeight;
72  int _oldStretchedWidth;
73  unsigned char* _buffer;
74  size_t _bufferSize;
75  size_t _incomingBufferSize;
76  bool _bufferIsUpdated;
77  bool _sizeInitialized;
78  int _numberOfStreams;
79  bool _bVideoSizeStartedChanging;
80  GLenum _pixelFormat;
81  GLenum _pixelDataType;
82  unsigned int _texture;
83};
84
85class VideoRenderAGL {
86 public:
87  VideoRenderAGL(WindowRef windowRef, bool fullscreen, int iId);
88  VideoRenderAGL(HIViewRef windowRef, bool fullscreen, int iId);
89  ~VideoRenderAGL();
90
91  int Init();
92  VideoChannelAGL* CreateAGLChannel(int channel, int zOrder, float startWidth,
93                                    float startHeight, float stopWidth,
94                                    float stopHeight);
95  VideoChannelAGL* ConfigureAGLChannel(int channel, int zOrder,
96                                       float startWidth, float startHeight,
97                                       float stopWidth, float stopHeight);
98  int DeleteAGLChannel(int channel);
99  int DeleteAllAGLChannels();
100  int StopThread();
101  bool IsFullScreen();
102  bool HasChannels();
103  bool HasChannel(int channel);
104  int GetChannels(std::list<int>& channelList);
105  void LockAGLCntx();
106  void UnlockAGLCntx();
107
108  static int GetOpenGLVersion(int& aglMajor, int& aglMinor);
109
110  // ********** new module functions ************ //
111  int ChangeWindow(void* newWindowRef);
112  int32_t StartRender();
113  int32_t StopRender();
114  int32_t DeleteAGLChannel(const uint32_t streamID);
115  int32_t GetChannelProperties(const uint16_t streamId, uint32_t& zOrder,
116                               float& left, float& top, float& right,
117                               float& bottom);
118
119 protected:
120  static bool ScreenUpdateThreadProc(void* obj);
121  bool ScreenUpdateProcess();
122  int GetWindowRect(Rect& rect);
123
124 private:
125  int CreateMixingContext();
126  int RenderOffScreenBuffers();
127  int SwapAndDisplayBuffers();
128  int UpdateClipping();
129  int CalculateVisibleRegion(ControlRef control, RgnHandle& visibleRgn,
130                             bool clipChildren);
131  bool CheckValidRegion(RgnHandle rHandle);
132  void ParentWindowResized(WindowRef window);
133
134  // Carbon GUI event handlers
135  static pascal OSStatus sHandleWindowResized(
136      EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
137  static pascal OSStatus sHandleHiViewResized(
138      EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
139
140  HIViewRef _hiviewRef;
141  WindowRef _windowRef;
142  bool _fullScreen;
143  int _id;
144  webrtc::CriticalSectionWrapper& _renderCritSec;
145  // TODO(pbos): Remove scoped_ptr and use PlatformThread directly.
146  rtc::scoped_ptr<rtc::PlatformThread> _screenUpdateThread;
147  webrtc::EventWrapper* _screenUpdateEvent;
148  bool _isHIViewRef;
149  AGLContext _aglContext;
150  int _windowWidth;
151  int _windowHeight;
152  int _lastWindowWidth;
153  int _lastWindowHeight;
154  int _lastHiViewWidth;
155  int _lastHiViewHeight;
156  int _currentParentWindowHeight;
157  int _currentParentWindowWidth;
158  Rect _currentParentWindowBounds;
159  bool _windowHasResized;
160  Rect _lastParentWindowBounds;
161  Rect _currentHIViewBounds;
162  Rect _lastHIViewBounds;
163  Rect _windowRect;
164  std::map<int, VideoChannelAGL*> _aglChannels;
165  std::multimap<int, int> _zOrderToChannel;
166  EventHandlerRef _hiviewEventHandlerRef;
167  EventHandlerRef _windowEventHandlerRef;
168  HIRect _currentViewBounds;
169  HIRect _lastViewBounds;
170  bool _renderingIsPaused;
171};
172
173}  // namespace webrtc
174
175#endif  // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_MAC_VIDEO_RENDER_AGL_H_
176
177#endif  // CARBON_RENDERING
178