Ejemplo n.º 1
0
/***********************************************************************
 *              create_surface_image
 *
 * Caller must hold the surface lock.  On input, *rect is the requested
 * image rect, relative to the window whole_rect, a.k.a. visible_rect.
 * On output, it's been intersected with that part backed by the surface
 * and is the actual size of the returned image.  copy_data indicates if
 * the caller will keep the returned image beyond the point where the
 * surface bits can be guaranteed to remain valid and unchanged.  If so,
 * the bits are copied instead of merely referenced by the image.
 *
 * IMPORTANT: This function is called from non-Wine threads, so it
 *            must not use Win32 or Wine functions, including debug
 *            logging.
 */
CGImageRef create_surface_image(void *window_surface, CGRect *rect, int copy_data)
{
    CGImageRef cgimage = NULL;
    struct macdrv_window_surface *surface = get_mac_surface(window_surface);
    int width, height;

    width  = surface->header.rect.right - surface->header.rect.left;
    height = surface->header.rect.bottom - surface->header.rect.top;
    *rect = CGRectIntersection(cgrect_from_rect(surface->header.rect), *rect);
    if (!CGRectIsEmpty(*rect))
    {
        CGRect visrect;
        CGColorSpaceRef colorspace;
        CGDataProviderRef provider;
        int bytes_per_row, offset, size;
        CGImageAlphaInfo alphaInfo;

        visrect = CGRectOffset(*rect, -surface->header.rect.left, -surface->header.rect.top);

        colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
        bytes_per_row = get_dib_stride(width, 32);
        offset = CGRectGetMinX(visrect) * 4 + (height - CGRectGetMaxY(visrect)) * bytes_per_row;
        size = min(CGRectGetHeight(visrect) * bytes_per_row,
                   surface->info.bmiHeader.biSizeImage - offset);

        if (copy_data)
        {
            CFDataRef data = CFDataCreate(NULL, (UInt8*)surface->bits + offset, size);
            provider = CGDataProviderCreateWithCFData(data);
            CFRelease(data);
        }
        else
            provider = CGDataProviderCreateWithData(NULL, surface->bits + offset, size, NULL);

        alphaInfo = surface->use_alpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;
        cgimage = CGImageCreate(CGRectGetWidth(visrect), CGRectGetHeight(visrect),
                                8, 32, bytes_per_row, colorspace,
                                alphaInfo | kCGBitmapByteOrder32Little,
                                provider, NULL, FALSE, kCGRenderingIntentDefault);
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorspace);
    }

    return cgimage;
}
Ejemplo n.º 2
0
static void HIOpenGLViewSetContextWindowAndBounds (HIOpenGLViewData* inData)
{
    if (inData == nil) return;
    if (inData->mControl == nil) return;
    if (inData->mContext == nil) return;

    // Determine the AGL_BUFFER_RECT for the control. The coordinate
    // system for this rectangle is relative to the owning window, with
    // the origin at the bottom left corner and the y-axis inverted.
    HIRect ctrlBounds, winBounds;
    HIViewGetBounds(inData->mControl, &ctrlBounds);
    WindowRef window = GetControlOwner(inData->mControl);
    ControlRef root = nil;
    GetRootControl(window, &root);
    HIViewGetBounds(root, &winBounds);
    HIViewConvertRect(&ctrlBounds, inData->mControl, root);
    GLint bufferRect[4] = { (int)ctrlBounds.origin.x, (int)((winBounds.size.height) - (ctrlBounds.origin.y + ctrlBounds.size.height)),
                            (int)ctrlBounds.size.width, (int)ctrlBounds.size.height };
    
    // Associate the OpenGL context with the control's window, and establish the buffer rect.
    aglSetDrawable(inData->mContext, GetWindowPort(window));
    aglSetInteger(inData->mContext, AGL_BUFFER_RECT, bufferRect);
    aglEnable(inData->mContext, AGL_BUFFER_RECT);
    
    // Establish the clipping region for the OpenGL context. To properly handle clipping
    // within the view hierarchy, I'm walking the hierarchy to determine the intersection
    // of this view's bounds with its parents. Is there an easier way to do this?
    CGRect clipBounds = ctrlBounds;
    HIViewRef parent = HIViewGetSuperview(inData->mControl);
    while (parent != root)
    {
        CGRect parentBounds;
        HIViewGetBounds(parent, &parentBounds);
        HIViewConvertRect(&parentBounds, parent, root);
        clipBounds = CGRectIntersection(clipBounds, parentBounds);
        parent = HIViewGetSuperview(parent);
    }
    Rect rgnBounds = { (int)clipBounds.origin.y, (int)clipBounds.origin.x, (int)(clipBounds.origin.y + clipBounds.size.height), (int)(clipBounds.origin.x + clipBounds.size.width) };
    RgnHandle rgn = NewRgn();
    RectRgn(rgn, &rgnBounds);
    
    aglSetInteger(inData->mContext, AGL_CLIP_REGION, (const GLint*)rgn);
    aglEnable(inData->mContext, AGL_CLIP_REGION);
}