blob: 411a7d061446b3955902575adca62d7955dd0d8c [file] [log] [blame]
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +00001/*
2 * Copyright (c) 2013 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
pbos@webrtc.org16e03b72013-10-28 16:32:01 +000011#include "webrtc/test/mac/video_renderer_mac.h"
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000012
13#import <Cocoa/Cocoa.h>
14
15// Creates a Cocoa Window with an OpenGL context, used together with an OpenGL
16// renderer.
17@interface CocoaWindow : NSObject {
18 @private
19 NSWindow *window_;
20 NSOpenGLContext *context_;
21 NSString *title_;
22 int width_;
23 int height_;
24}
25
26- (id)initWithTitle:(NSString *)title width:(int)width height:(int)height;
27// 'createWindow' must be called on the main thread.
28- (void)createWindow:(NSObject *)ignored;
29- (void)makeCurrentContext;
30
31@end
32
33@implementation CocoaWindow
34 static NSInteger nextXOrigin_;
35 static NSInteger nextYOrigin_;
36
37- (id)initWithTitle:(NSString *)title width:(int)width height:(int)height {
38 if (self = [super init]) {
39 title_ = title;
40 width_ = width;
41 height_ = height;
42 }
43 return self;
44}
45
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000046- (void)createWindow:(NSObject *)ignored {
47 NSInteger xOrigin = nextXOrigin_;
48 NSRect screenFrame = [[NSScreen mainScreen] frame];
49 if (nextXOrigin_ + width_ < screenFrame.size.width) {
50 nextXOrigin_ += width_;
51 } else {
52 xOrigin = 0;
53 nextXOrigin_ = 0;
54 nextYOrigin_ += height_;
55 }
56 if (nextYOrigin_ + height_ > screenFrame.size.height) {
57 xOrigin = 0;
58 nextXOrigin_ = 0;
59 nextYOrigin_ = 0;
60 }
61 NSInteger yOrigin = nextYOrigin_;
62 NSRect windowFrame = NSMakeRect(xOrigin, yOrigin, width_, height_);
63 window_ = [[NSWindow alloc] initWithContentRect:windowFrame
64 styleMask:NSTitledWindowMask
65 backing:NSBackingStoreBuffered
66 defer:NO];
67
68 NSRect viewFrame = NSMakeRect(0, 0, width_, height_);
kthelgasonc0977102017-04-24 00:57:16 -070069 NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:viewFrame pixelFormat:nil];
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +000070 context_ = [view openGLContext];
71
72 [[window_ contentView] addSubview:view];
73 [window_ setTitle:title_];
74 [window_ makeKeyAndOrderFront:NSApp];
75}
76
77- (void)makeCurrentContext {
78 [context_ makeCurrentContext];
79}
80
81@end
82
83namespace webrtc {
84namespace test {
85
86VideoRenderer* VideoRenderer::CreatePlatformRenderer(const char* window_title,
87 size_t width,
88 size_t height) {
89 MacRenderer* renderer = new MacRenderer();
90 if (!renderer->Init(window_title, width, height)) {
91 delete renderer;
92 return NULL;
93 }
94 return renderer;
95}
96
97MacRenderer::MacRenderer()
98 : window_(NULL) {}
99
100MacRenderer::~MacRenderer() {
101 GlRenderer::Destroy();
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +0000102}
103
104bool MacRenderer::Init(const char* window_title, int width, int height) {
105 window_ = [[CocoaWindow alloc]
106 initWithTitle:[NSString stringWithUTF8String:window_title]
107 width:width
108 height:height];
109 if (!window_)
110 return false;
111 [window_ performSelectorOnMainThread:@selector(createWindow:)
112 withObject:nil
113 waitUntilDone:YES];
114
115 [window_ makeCurrentContext];
116 GlRenderer::Init();
117 GlRenderer::ResizeViewport(width, height);
118 return true;
119}
120
nisseeb83a1a2016-03-21 01:27:56 -0700121void MacRenderer::OnFrame(const VideoFrame& frame) {
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +0000122 [window_ makeCurrentContext];
nisseeb83a1a2016-03-21 01:27:56 -0700123 GlRenderer::OnFrame(frame);
mflodman@webrtc.org7f944f32013-05-27 15:52:38 +0000124}
125
126} // test
127} // webrtc