xref: /aosp_15_r20/external/webrtc/test/mac/video_renderer_mac.mm (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker/*
2*d9f75844SAndroid Build Coastguard Worker *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker#include "test/mac/video_renderer_mac.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker#import <Cocoa/Cocoa.h>
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard Worker// Creates a Cocoa Window with an OpenGL context, used together with an OpenGL
16*d9f75844SAndroid Build Coastguard Worker// renderer.
17*d9f75844SAndroid Build Coastguard Worker@interface CocoaWindow : NSObject {
18*d9f75844SAndroid Build Coastguard Worker @private
19*d9f75844SAndroid Build Coastguard Worker  NSWindow *window_;
20*d9f75844SAndroid Build Coastguard Worker  NSOpenGLContext *context_;
21*d9f75844SAndroid Build Coastguard Worker  NSString *title_;
22*d9f75844SAndroid Build Coastguard Worker  int width_;
23*d9f75844SAndroid Build Coastguard Worker  int height_;
24*d9f75844SAndroid Build Coastguard Worker}
25*d9f75844SAndroid Build Coastguard Worker
26*d9f75844SAndroid Build Coastguard Worker- (id)initWithTitle:(NSString *)title width:(int)width height:(int)height;
27*d9f75844SAndroid Build Coastguard Worker// 'createWindow' must be called on the main thread.
28*d9f75844SAndroid Build Coastguard Worker- (void)createWindow:(NSObject *)ignored;
29*d9f75844SAndroid Build Coastguard Worker- (void)makeCurrentContext;
30*d9f75844SAndroid Build Coastguard Worker
31*d9f75844SAndroid Build Coastguard Worker@end
32*d9f75844SAndroid Build Coastguard Worker
33*d9f75844SAndroid Build Coastguard Worker@implementation CocoaWindow
34*d9f75844SAndroid Build Coastguard Worker  static NSInteger nextXOrigin_;
35*d9f75844SAndroid Build Coastguard Worker  static NSInteger nextYOrigin_;
36*d9f75844SAndroid Build Coastguard Worker
37*d9f75844SAndroid Build Coastguard Worker- (id)initWithTitle:(NSString *)title width:(int)width height:(int)height {
38*d9f75844SAndroid Build Coastguard Worker  if (self = [super init]) {
39*d9f75844SAndroid Build Coastguard Worker    title_ = title;
40*d9f75844SAndroid Build Coastguard Worker    width_ = width;
41*d9f75844SAndroid Build Coastguard Worker    height_ = height;
42*d9f75844SAndroid Build Coastguard Worker  }
43*d9f75844SAndroid Build Coastguard Worker  return self;
44*d9f75844SAndroid Build Coastguard Worker}
45*d9f75844SAndroid Build Coastguard Worker
46*d9f75844SAndroid Build Coastguard Worker- (void)createWindow:(NSObject *)ignored {
47*d9f75844SAndroid Build Coastguard Worker  NSInteger xOrigin = nextXOrigin_;
48*d9f75844SAndroid Build Coastguard Worker  NSRect screenFrame = [[NSScreen mainScreen] frame];
49*d9f75844SAndroid Build Coastguard Worker  if (nextXOrigin_ + width_ < screenFrame.size.width) {
50*d9f75844SAndroid Build Coastguard Worker    nextXOrigin_ += width_;
51*d9f75844SAndroid Build Coastguard Worker  } else {
52*d9f75844SAndroid Build Coastguard Worker    xOrigin = 0;
53*d9f75844SAndroid Build Coastguard Worker    nextXOrigin_ = 0;
54*d9f75844SAndroid Build Coastguard Worker    nextYOrigin_ += height_;
55*d9f75844SAndroid Build Coastguard Worker  }
56*d9f75844SAndroid Build Coastguard Worker  if (nextYOrigin_ + height_ > screenFrame.size.height) {
57*d9f75844SAndroid Build Coastguard Worker    xOrigin = 0;
58*d9f75844SAndroid Build Coastguard Worker    nextXOrigin_ = 0;
59*d9f75844SAndroid Build Coastguard Worker    nextYOrigin_ = 0;
60*d9f75844SAndroid Build Coastguard Worker  }
61*d9f75844SAndroid Build Coastguard Worker  NSInteger yOrigin = nextYOrigin_;
62*d9f75844SAndroid Build Coastguard Worker  NSRect windowFrame = NSMakeRect(xOrigin, yOrigin, width_, height_);
63*d9f75844SAndroid Build Coastguard Worker  window_ = [[NSWindow alloc] initWithContentRect:windowFrame
64*d9f75844SAndroid Build Coastguard Worker                                        styleMask:NSWindowStyleMaskTitled
65*d9f75844SAndroid Build Coastguard Worker                                          backing:NSBackingStoreBuffered
66*d9f75844SAndroid Build Coastguard Worker                                            defer:NO];
67*d9f75844SAndroid Build Coastguard Worker
68*d9f75844SAndroid Build Coastguard Worker  NSRect viewFrame = NSMakeRect(0, 0, width_, height_);
69*d9f75844SAndroid Build Coastguard Worker  NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:viewFrame pixelFormat:nil];
70*d9f75844SAndroid Build Coastguard Worker  context_ = [view openGLContext];
71*d9f75844SAndroid Build Coastguard Worker
72*d9f75844SAndroid Build Coastguard Worker  [[window_ contentView] addSubview:view];
73*d9f75844SAndroid Build Coastguard Worker  [window_ setTitle:title_];
74*d9f75844SAndroid Build Coastguard Worker  [window_ makeKeyAndOrderFront:NSApp];
75*d9f75844SAndroid Build Coastguard Worker}
76*d9f75844SAndroid Build Coastguard Worker
77*d9f75844SAndroid Build Coastguard Worker- (void)makeCurrentContext {
78*d9f75844SAndroid Build Coastguard Worker  [context_ makeCurrentContext];
79*d9f75844SAndroid Build Coastguard Worker}
80*d9f75844SAndroid Build Coastguard Worker
81*d9f75844SAndroid Build Coastguard Worker@end
82*d9f75844SAndroid Build Coastguard Worker
83*d9f75844SAndroid Build Coastguard Workernamespace webrtc {
84*d9f75844SAndroid Build Coastguard Workernamespace test {
85*d9f75844SAndroid Build Coastguard Worker
86*d9f75844SAndroid Build Coastguard WorkerVideoRenderer* VideoRenderer::CreatePlatformRenderer(const char* window_title,
87*d9f75844SAndroid Build Coastguard Worker                                                     size_t width,
88*d9f75844SAndroid Build Coastguard Worker                                                     size_t height) {
89*d9f75844SAndroid Build Coastguard Worker  MacRenderer* renderer = new MacRenderer();
90*d9f75844SAndroid Build Coastguard Worker  if (!renderer->Init(window_title, width, height)) {
91*d9f75844SAndroid Build Coastguard Worker    delete renderer;
92*d9f75844SAndroid Build Coastguard Worker    return NULL;
93*d9f75844SAndroid Build Coastguard Worker  }
94*d9f75844SAndroid Build Coastguard Worker  return renderer;
95*d9f75844SAndroid Build Coastguard Worker}
96*d9f75844SAndroid Build Coastguard Worker
97*d9f75844SAndroid Build Coastguard WorkerMacRenderer::MacRenderer()
98*d9f75844SAndroid Build Coastguard Worker    : window_(NULL) {}
99*d9f75844SAndroid Build Coastguard Worker
100*d9f75844SAndroid Build Coastguard WorkerMacRenderer::~MacRenderer() {
101*d9f75844SAndroid Build Coastguard Worker  GlRenderer::Destroy();
102*d9f75844SAndroid Build Coastguard Worker}
103*d9f75844SAndroid Build Coastguard Worker
104*d9f75844SAndroid Build Coastguard Workerbool MacRenderer::Init(const char* window_title, int width, int height) {
105*d9f75844SAndroid Build Coastguard Worker  window_ = [[CocoaWindow alloc]
106*d9f75844SAndroid Build Coastguard Worker      initWithTitle:[NSString stringWithUTF8String:window_title]
107*d9f75844SAndroid Build Coastguard Worker                                             width:width
108*d9f75844SAndroid Build Coastguard Worker                                            height:height];
109*d9f75844SAndroid Build Coastguard Worker  if (!window_)
110*d9f75844SAndroid Build Coastguard Worker    return false;
111*d9f75844SAndroid Build Coastguard Worker  [window_ performSelectorOnMainThread:@selector(createWindow:)
112*d9f75844SAndroid Build Coastguard Worker                            withObject:nil
113*d9f75844SAndroid Build Coastguard Worker                         waitUntilDone:YES];
114*d9f75844SAndroid Build Coastguard Worker
115*d9f75844SAndroid Build Coastguard Worker  [window_ makeCurrentContext];
116*d9f75844SAndroid Build Coastguard Worker  GlRenderer::Init();
117*d9f75844SAndroid Build Coastguard Worker  GlRenderer::ResizeViewport(width, height);
118*d9f75844SAndroid Build Coastguard Worker  return true;
119*d9f75844SAndroid Build Coastguard Worker}
120*d9f75844SAndroid Build Coastguard Worker
121*d9f75844SAndroid Build Coastguard Workervoid MacRenderer::OnFrame(const VideoFrame& frame) {
122*d9f75844SAndroid Build Coastguard Worker  [window_ makeCurrentContext];
123*d9f75844SAndroid Build Coastguard Worker  GlRenderer::OnFrame(frame);
124*d9f75844SAndroid Build Coastguard Worker}
125*d9f75844SAndroid Build Coastguard Worker
126*d9f75844SAndroid Build Coastguard Worker}  // test
127*d9f75844SAndroid Build Coastguard Worker}  // webrtc
128