// Returns depth of screen int wxDisplayDepth() { int theDepth = 0; #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 if ( UMAGetSystemVersion() >= 0x1060 ) { CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay); CFStringRef encoding = CGDisplayModeCopyPixelEncoding(currentMode); if(CFStringCompare(encoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) theDepth = 32; else if(CFStringCompare(encoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) theDepth = 16; else if(CFStringCompare(encoding, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) theDepth = 8; else theDepth = 32; // some reasonable default CFRelease(encoding); CGDisplayModeRelease(currentMode); } else #endif { #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID()); #endif } return theDepth; }
size_t displayBitsPerPixel(CGDirectDisplayID displayId) { #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 return CGDisplayBitsPerPixel(displayId); #else // MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 // Get the display mode. CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayId); // Get bpp for the mode. size_t const bpp = modeBitsPerPixel(mode); // Clean up Memory. CGDisplayModeRelease(mode); return bpp; #endif }
MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect) { #if defined(IS_MACOSX) /* The following is a very modified version of the glGrab code example * given by Apple (as are some of the convenience functions called). */ size_t bytewidth; uint8_t bitsPerPixel, bytesPerPixel; uint8_t *buffer; /* Build OpenGL context of entire screen */ CGDirectDisplayID displayID = CGMainDisplayID(); CGOpenGLDisplayMask mask = CGDisplayIDToOpenGLDisplayMask(displayID); CGLContextObj glContext = createFullScreenCGLContext(mask); if (glContext == NULL) return NULL; /* TODO: CGDisplayBitsPerPixel() is deprecated in Snow Leopard; I'm not * sure of the replacement function. */ bitsPerPixel = (uint8_t)CGDisplayBitsPerPixel(displayID); bytesPerPixel = bitsPerPixel / 8; /* Align width to padding. */ bytewidth = ADD_PADDING(rect.size.width * bytesPerPixel); /* Convert Quartz point to postscript point. */ rect.origin.y = CGDisplayPixelsHigh(displayID) - rect.origin.y - rect.size.height; /* Extract buffer from context */ buffer = createBufferFromCurrentCGLContext((GLint)rect.origin.x, (GLint)rect.origin.y, (GLsizei)rect.size.width, (GLsizei)rect.size.height, bytewidth); /* Reset and release GL context */ destroyFullScreenCGLContext(glContext); if (buffer == NULL) return NULL; /* Convert from OpenGL (origin at bottom left) to Quartz (origin at top * left) coordinate system. */ flipBitmapData(buffer, rect.size.width, rect.size.height, bytewidth); return createMMBitmap(buffer, rect.size.width, rect.size.height, bytewidth, bitsPerPixel, bytesPerPixel); #elif defined(USE_X11) MMBitmapRef bitmap; Display *display = XOpenDisplay(NULL); XImage *image = XGetImage(display, XDefaultRootWindow(display), (int)rect.origin.x, (int)rect.origin.y, (unsigned int)rect.size.width, (unsigned int)rect.size.height, AllPlanes, ZPixmap); XCloseDisplay(display); if (image == NULL) return NULL; bitmap = createMMBitmap((uint8_t *)image->data, rect.size.width, rect.size.height, (size_t)image->bytes_per_line, (uint8_t)image->bits_per_pixel, (uint8_t)image->bits_per_pixel / 8); image->data = NULL; /* Steal ownership of bitmap data so we don't have to * copy it. */ XDestroyImage(image); return bitmap; #elif defined(IS_WINDOWS) MMBitmapRef bitmap; void *data; HDC screen = NULL, screenMem = NULL; HBITMAP dib; BITMAPINFO bi; /* Initialize bitmap info. */ bi.bmiHeader.biSize = sizeof(bi.bmiHeader); bi.bmiHeader.biWidth = (long)rect.size.width; bi.bmiHeader.biHeight = -(long)rect.size.height; /* Non-cartesian, please */ bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = (DWORD)(4 * rect.size.width * rect.size.height); bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; screen = GetDC(NULL); /* Get entire screen */ if (screen == NULL) return NULL; /* Get screen data in display device context. */ dib = CreateDIBSection(screen, &bi, DIB_RGB_COLORS, &data, NULL, 0); /* Copy the data into a bitmap struct. */ if ((screenMem = CreateCompatibleDC(screen)) == NULL || SelectObject(screenMem, dib) == NULL || !BitBlt(screenMem, (int)rect.origin.x, (int)rect.origin.y, (int)rect.size.width, (int)rect.size.height, screen, 0, 0, SRCCOPY)) { /* Error copying data. */ ReleaseDC(NULL, screen); DeleteObject(dib); if (screenMem != NULL) DeleteDC(screenMem); return NULL; } bitmap = createMMBitmap(NULL, rect.size.width, rect.size.height, 4 * rect.size.width, (uint8_t)bi.bmiHeader.biBitCount, 4); /* Copy the data to our pixel buffer. */ if (bitmap != NULL) { bitmap->imageBuffer = malloc(bitmap->bytewidth * bitmap->height); memcpy(bitmap->imageBuffer, data, bitmap->bytewidth * bitmap->height); } ReleaseDC(NULL, screen); DeleteObject(dib); DeleteDC(screenMem); return bitmap; #endif }
// Returns depth of screen int wxDisplayDepth() { int theDepth = (int) CGDisplayBitsPerPixel(CGMainDisplayID()); return theDepth; }
void changeResolution(int x, int y) { CGDirectDisplayID main = CGMainDisplayID(); CFDictionaryRef mode = CGDisplayBestModeForParameters(main, CGDisplayBitsPerPixel(main), x, y, NULL); CGDisplaySwitchToMode(main, mode); }
/* * xprAddScreen * Init the framebuffer and record pixmap parameters for the screen. */ static Bool xprAddScreen(int index, ScreenPtr pScreen) { DarwinFramebufferPtr dfb = SCREEN_PRIV(pScreen); /* If no specific depth chosen, look for the depth of the main display. Else if 16bpp specified, use that. Else use 32bpp. */ dfb->colorType = TrueColor; dfb->bitsPerComponent = 8; dfb->bitsPerPixel = 32; dfb->colorBitsPerPixel = 24; if (darwinDesiredDepth == -1) { dfb->bitsPerComponent = CGDisplayBitsPerSample(kCGDirectMainDisplay); dfb->bitsPerPixel = CGDisplayBitsPerPixel(kCGDirectMainDisplay); dfb->colorBitsPerPixel = CGDisplaySamplesPerPixel(kCGDirectMainDisplay) * dfb->bitsPerComponent; } else if (darwinDesiredDepth == 15) { dfb->bitsPerComponent = 5; dfb->bitsPerPixel = 16; dfb->colorBitsPerPixel = 15; } else if (darwinDesiredDepth == 8) { dfb->colorType = PseudoColor; dfb->bitsPerComponent = 8; dfb->bitsPerPixel = 8; dfb->colorBitsPerPixel = 8; } if (noPseudoramiXExtension) { CGDirectDisplayID dpy; CGRect frame; dpy = displayAtIndex(index); frame = displayScreenBounds(dpy); dfb->x = frame.origin.x; dfb->y = frame.origin.y; dfb->width = frame.size.width; dfb->height = frame.size.height; } else { xprAddPseudoramiXScreens(&dfb->x, &dfb->y, &dfb->width, &dfb->height); } /* Passing zero width (pitch) makes miCreateScreenResources set the screen pixmap to the framebuffer pointer, i.e. NULL. The generic rootless code takes care of making this work. */ dfb->pitch = 0; dfb->framebuffer = NULL; DRIScreenInit(pScreen); return TRUE; }