xref: /aosp_15_r20/external/webrtc/test/mac/video_renderer_mac.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1/*
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
11#include "test/mac/video_renderer_mac.h"
12
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
46- (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:NSWindowStyleMaskTitled
65                                          backing:NSBackingStoreBuffered
66                                            defer:NO];
67
68  NSRect viewFrame = NSMakeRect(0, 0, width_, height_);
69  NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:viewFrame pixelFormat:nil];
70  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();
102}
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
121void MacRenderer::OnFrame(const VideoFrame& frame) {
122  [window_ makeCurrentContext];
123  GlRenderer::OnFrame(frame);
124}
125
126}  // test
127}  // webrtc
128