프로그래밍/OpenGL-ES2010. 8. 3. 00:42
/**
 *  @file SurfaceView.h
 *  A View that implements a CoreSurface buffer
 *  The buffer is created automatically in initWithFrame: with the size
 *  passed to it and 5-6-5 RGB format.
 *  The memory can be accessed with the pixels property, and after modifying
 *  it you must call setNeedsDisplay on it for it to update.
 */
 
#import <UIKit/UIKit.h>
#import <CoreSurface/CoreSurface.h>
 
@interface SurfaceView : UIView
{
    CALayer *surfaceLayer;
    unsigned short *pixels;
    CoreSurfaceBufferRef surfaceBuffer;
}
 
@property (nonatomic, readonly) unsigned short *pixels;
 
@end
 
 
 
/**
 *  @file SurfaceView.m
 *  Implementation of SurfaceView
 */
 
#import "SurfaceView.h"
 
@implementation SurfaceView
 
- (id)initWithFrame:(CGRect)frame {
    frame.origin.x /= 2;
    frame.origin.y /= 2;
   
    if (self = [super initWithFrame:frame]) {
        // Create Surface
        int w = frame.size.width, h = frame.size.height;
        surfaceBuffer = CoreSurfaceBufferCreate((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
            [NSNumber numberWithInt:w],         kCoreSurfaceBufferWidth,
            [NSNumber numberWithInt:h],         kCoreSurfaceBufferHeight,
            [NSNumber numberWithInt:'L565'],    kCoreSurfaceBufferPixelFormat,
            [NSNumber numberWithInt:2*w*h],     kCoreSurfaceBufferAllocSize,
            [NSNumber numberWithBool:YES],      kCoreSurfaceBufferGlobal,
            [NSNumber numberWithInt:2*w],       kCoreSurfaceBufferPitch,
            @"PurpleGFXMem",                    kCoreSurfaceBufferMemoryRegion,
            nil]);
       
        // Create layer for surface
        CGRect frame2 = CGRectMake(frame.origin.x, frame.origin.y,
                                   frame.size.width+1, frame.size.height+1);
        CoreSurfaceBufferLock(surfaceBuffer, 3);
        surfaceLayer = [[CALayer layer] retain];
        [surfaceLayer setMagnificationFilter:0];
        [surfaceLayer setEdgeAntialiasingMask:0];
        [surfaceLayer setFrame:frame2];
        [surfaceLayer setContents:surfaceBuffer];
        [surfaceLayer setOpaque:YES];
        [[self _layer] addSublayer:surfaceLayer];
        CoreSurfaceBufferUnlock(surfaceBuffer);
       
        // Get base address
        pixels = CoreSurfaceBufferGetBaseAddress(surfaceBuffer);
    }
    return self;
}
 
- (void)dealloc {
    [surfaceLayer release];
    [super dealloc];
}
 
- (void)drawRect:(CGRect)rect {
    // It will break if you remove this empty function
}
 
- (unsigned short*)pixels {
    return pixels;
}
@end

/** 
 *  @file SurfaceView.h
 *  A View that implements a CoreSurface buffer
 *  The buffer is created automatically in initWithFrame: with the size
 *  passed to it and 5-6-5 RGB format.
 *  The memory can be accessed with the pixels property, and after modifying
 *  it you must call setNeedsDisplay on it for it to update.
 */
 
#import <UIKit/UIKit.h>
#import <CoreSurface/CoreSurface.h>

@interface SurfaceView : UIView 
{
    CALayer *surfaceLayer;
    unsigned short *pixels;
    CoreSurfaceBufferRef surfaceBuffer;
}

@property (nonatomic, readonly) unsigned short *pixels;

@end



/** 
 *  @file SurfaceView.m
 *  Implementation of SurfaceView
 */
 
#import "SurfaceView.h"

@implementation SurfaceView

- (id)initWithFrame:(CGRect)frame {
    frame.origin.x /= 2;
    frame.origin.y /= 2;
    
    if (self = [super initWithFrame:frame]) {
        // Create Surface
        int w = frame.size.width, h = frame.size.height;
        surfaceBuffer = CoreSurfaceBufferCreate((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
            [NSNumber numberWithInt:w],         kCoreSurfaceBufferWidth,
            [NSNumber numberWithInt:h],         kCoreSurfaceBufferHeight,
            [NSNumber numberWithInt:'L565'],    kCoreSurfaceBufferPixelFormat,
            [NSNumber numberWithInt:2*w*h],     kCoreSurfaceBufferAllocSize,
            [NSNumber numberWithBool:YES],      kCoreSurfaceBufferGlobal,
            [NSNumber numberWithInt:2*w],       kCoreSurfaceBufferPitch,
            @"PurpleGFXMem",                    kCoreSurfaceBufferMemoryRegion,
            nil]);
        
        // Create layer for surface
        CGRect frame2 = CGRectMake(frame.origin.x, frame.origin.y, 
                                   frame.size.width+1, frame.size.height+1);
        CoreSurfaceBufferLock(surfaceBuffer, 3);
        surfaceLayer = [[CALayer layer] retain];
        [surfaceLayer setMagnificationFilter:0];
        [surfaceLayer setEdgeAntialiasingMask:0];
        [surfaceLayer setFrame:frame2];
        [surfaceLayer setContents:surfaceBuffer];
        [surfaceLayer setOpaque:YES];
        [[self _layer] addSublayer:surfaceLayer];
        CoreSurfaceBufferUnlock(surfaceBuffer);
        
        // Get base address
        pixels = CoreSurfaceBufferGetBaseAddress(surfaceBuffer);
    }
    return self;
}

- (void)dealloc {
    [surfaceLayer release];
    [super dealloc];
}

- (void)drawRect:(CGRect)rect {
    // It will break if you remove this empty function
}

- (unsigned short*)pixels {
    return pixels;
}
@end
Posted by windship