From f5516e0fc0e6c22b25f9dca2b9b24ebd7618332a Mon Sep 17 00:00:00 2001
From: "Graham Madarasz (Graham Linden)" <graham@lindenlab.com>
Date: Wed, 27 Feb 2013 06:46:53 -0800
Subject: [PATCH] For MAINT-2404 Fixes detection of VRAM on OSX removed along
 with other AGL code. Code Review: callum

---
 indra/llwindow/llopenglview-objc.h    |  5 +++++
 indra/llwindow/llopenglview-objc.mm   | 22 ++++++++++++++++++++++
 indra/llwindow/llwindowmacosx-objc.h  |  1 +
 indra/llwindow/llwindowmacosx-objc.mm |  5 +++++
 indra/llwindow/llwindowmacosx.cpp     |  2 ++
 5 files changed, 35 insertions(+)

diff --git a/indra/llwindow/llopenglview-objc.h b/indra/llwindow/llopenglview-objc.h
index b344bed2efb..c3ae34ea50c 100644
--- a/indra/llwindow/llopenglview-objc.h
+++ b/indra/llwindow/llopenglview-objc.h
@@ -7,6 +7,9 @@
 //
 
 #import <Cocoa/Cocoa.h>
+#import <IOKit/IOKitLib.h>
+#import <CoreFoundation/CFBase.h>
+#import <CoreFoundation/CFNumber.h>
 #include "llwindowmacosx-objc.h"
 
 // Some nasty shovelling of LLOpenGLView from LLNativeBindings to prevent any C++ <-> Obj-C interop oddities.
@@ -33,6 +36,8 @@
 - (CGLContextObj) getCGLContextObj;
 - (CGLPixelFormatObj*)getCGLPixelFormatObj;
 
+- (unsigned long) getVramSize;
+
 @end
 
 @interface LLNSWindow : NSWindow {
diff --git a/indra/llwindow/llopenglview-objc.mm b/indra/llwindow/llopenglview-objc.mm
index e89c9267d5d..a1dece54f4c 100644
--- a/indra/llwindow/llopenglview-objc.mm
+++ b/indra/llwindow/llopenglview-objc.mm
@@ -39,6 +39,28 @@
 
 @implementation LLOpenGLView
 
+- (unsigned long)getVramSize
+{
+	unsigned long vram_bytes = 0;
+
+	io_service_t display_port = CGDisplayIOServicePort(kCGDirectMainDisplay);
+	
+	const void* type_code = IORegistryEntryCreateCFProperty(display_port, CFSTR(kIOFBMemorySizeKey), kCFAllocatorDefault, kNilOptions);
+
+	// Ensure we have valid data from IOKit
+	if(type_code && CFGetTypeID(type_code) == CFNumberGetTypeID())
+	{
+		long val;
+        // Retrieve actual number...is Apple ever embarrassed by this nonsense?
+        //
+		CFNumberGetValue((const __CFNumber*)type_code, kCFNumberSInt32Type, &val);
+		vram_bytes = (unsigned long)val;
+		CFRelease(type_code);
+	}
+	
+	return vram_bytes;
+}
+
 - (void)viewDidMoveToWindow
 {
 	[[NSNotificationCenter defaultCenter] addObserver:self
diff --git a/indra/llwindow/llwindowmacosx-objc.h b/indra/llwindow/llwindowmacosx-objc.h
index a0eab61e7cb..1724b85724f 100644
--- a/indra/llwindow/llwindowmacosx-objc.h
+++ b/indra/llwindow/llwindowmacosx-objc.h
@@ -62,6 +62,7 @@ NSWindowRef createNSWindow(int x, int y, int width, int height);
 GLViewRef createOpenGLView(NSWindowRef window, unsigned int samples, bool vsync);
 void glSwapBuffers(void* context);
 CGLContextObj getCGLContextObj(GLViewRef view);
+unsigned long getVramSize(GLViewRef view);
 void getContentViewBounds(NSWindowRef window, float* bounds);
 void getWindowSize(NSWindowRef window, float* size);
 void setWindowSize(NSWindowRef window, int width, int height);
diff --git a/indra/llwindow/llwindowmacosx-objc.mm b/indra/llwindow/llwindowmacosx-objc.mm
index b2886712199..9b57cd27cd5 100644
--- a/indra/llwindow/llwindowmacosx-objc.mm
+++ b/indra/llwindow/llwindowmacosx-objc.mm
@@ -237,6 +237,11 @@ CGLPixelFormatObj* getCGLPixelFormatObj(NSWindowRef window)
 	return [glview getCGLPixelFormatObj];
 }
 
+unsigned long getVramSize(GLViewRef view)
+{
+	return [(LLOpenGLView *)view getVramSize];
+}
+
 void getContentViewBounds(NSWindowRef window, float* bounds)
 {
 	bounds[0] = [[(LLNSWindow*)window contentView] bounds].origin.x;
diff --git a/indra/llwindow/llwindowmacosx.cpp b/indra/llwindow/llwindowmacosx.cpp
index ac0fa54a689..abb3c1e50c1 100644
--- a/indra/llwindow/llwindowmacosx.cpp
+++ b/indra/llwindow/llwindowmacosx.cpp
@@ -408,6 +408,8 @@ BOOL LLWindowMacOSX::createContext(int x, int y, int width, int height, int bits
 		mContext = getCGLContextObj(mGLView);
 		// Since we just created the context, it needs to be set up.
 		glNeedsInit = TRUE;
+		
+		gGLManager.mVRAM = getVramSize(mGLView);
 	}
 
 	// Hook up the context to a drawable
-- 
GitLab