1/* 2* Copyright (C) 2011 The Android Open Source Project 3* 4* Licensed under the Apache License, Version 2.0 (the "License"); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an "AS IS" BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16#import <QuartzCore/CALayer.h> 17#import <QuartzCore/CAMetalLayer.h> 18 19 20#include "NativeSubWindow.h" 21#include <Cocoa/Cocoa.h> 22 23/* 24 * EmuGLView inherit from NSView and override the isOpaque 25 * method to return YES. That prevents drawing of underlying window/view 26 * when the view needs to be redrawn. 27 */ 28@interface EmuGLView : NSView { 29} @end 30 31@implementation EmuGLView 32 33 - (BOOL)isOpaque { 34 return YES; 35 } 36 37@end 38 39@interface EmuGLViewWithMetal : NSView { 40} @end 41 42@implementation EmuGLViewWithMetal 43 44 - (BOOL)isOpaque { 45 return YES; 46 } 47 48 + (Class) layerClass { 49 return [CAMetalLayer class]; 50 } 51 52 - (CALayer *)makeBackingLayer { 53 CALayer * layer = [CAMetalLayer layer]; 54 return layer; 55 } 56 57@end 58 59EGLNativeWindowType createSubWindow(FBNativeWindowType p_window, 60 int x, 61 int y, 62 int width, 63 int height, 64 float dpr, 65 SubWindowRepaintCallback repaint_callback, 66 void* repaint_callback_param, 67 int hideWindow) { 68 NSWindow* win = (NSWindow *)p_window; 69 if (!win) { 70 return NULL; 71 } 72 73 /* (x,y) assume an upper-left origin, but Cocoa uses a lower-left origin */ 74 NSRect content_rect = [win contentRectForFrameRect:[win frame]]; 75 int cocoa_y = (int)content_rect.size.height - (y + height); 76 NSRect contentRect = NSMakeRect(x, cocoa_y, width, height); 77 78 NSView *glView = NULL; 79 const char* angle_default_platform = getenv("ANGLE_DEFAULT_PLATFORM"); 80 if (angle_default_platform && 0 == strcmp("metal", angle_default_platform)) { 81 glView = [[EmuGLViewWithMetal alloc] initWithFrame:contentRect]; 82 } else { 83 glView = [[EmuGLView alloc] initWithFrame:contentRect]; 84 } 85 if (!glView) { 86 return NULL; 87 } 88 [glView setWantsBestResolutionOpenGLSurface:YES]; 89 [glView setWantsLayer:YES]; 90 [[win contentView] addSubview:glView]; 91 [win makeKeyAndOrderFront:nil]; 92 if (hideWindow) { 93 [glView setHidden:YES]; 94 } 95 // We cannot use the dpr from [NSScreen mainScreen], which usually 96 // gives the wrong screen at this point. 97 [glView.layer setContentsScale:dpr]; 98 return (EGLNativeWindowType)(glView); 99} 100 101void destroySubWindow(EGLNativeWindowType win) { 102 if(win){ 103 NSView *glView = (NSView *)win; 104 [glView removeFromSuperview]; 105 [glView release]; 106 } 107} 108 109int moveSubWindow(FBNativeWindowType p_parent_window, 110 EGLNativeWindowType p_sub_window, 111 int x, 112 int y, 113 int width, 114 int height, 115 float dpr) { 116 NSWindow *win = (NSWindow *)p_parent_window; 117 if (!win) { 118 return 0; 119 } 120 121 NSView *glView = (NSView *)p_sub_window; 122 if (!glView) { 123 return 0; 124 } 125 126 /* The view must be removed from the hierarchy to be properly resized */ 127 [glView removeFromSuperview]; 128 129 /* (x,y) assume an upper-left origin, but Cocoa uses a lower-left origin */ 130 NSRect content_rect = [win contentRectForFrameRect:[win frame]]; 131 int cocoa_y = (int)content_rect.size.height - (y + height); 132 NSRect newFrame = NSMakeRect(x, cocoa_y, width, height); 133 [glView setFrame:newFrame]; 134 135 /* Re-add the sub-window to the view hierarchy */ 136 [[win contentView] addSubview:glView]; 137 138 return 1; 139} 140