GPUImage源码解读(八)

从名称就可以知道GPUImageView是GPUImage框架中显示图片相关的类。GPUImageView实现了GPUImageInput协议,从而可以知道它能够接受GPUImageFramebuffer的输入。因此,常常作为响应链的终端节点,用于显示处理后的帧缓存。

- 重写静态方法 返回 CAEAGLLayer

GPUImageView 是UIView的子类,所以会有UIView所有方法特性。UIView也可以使用不同的CALayer来显示,添加OpenGL ES实现View绘制的使用

- (Class)layerClass
{

return [CAEAGLLayer class];
}

初始化 方法

最后都会调用 commonInit 方法

- (id)initWithFrame:(CGRect)frame;

- (id)initWithCoder:(NSCoder *)coder;

实现方法

- (void)commonInit;
{

// Set scaling to account for Retina display

if ([self respondsToSelector:@selector(setContentScaleFactor:)])

{

    self.contentScaleFactor = [[UIScreen mainScreen] scale];

}



inputRotation = kGPUImageNoRotation;

self.opaque = YES;

self.hidden = NO;

//1,设置OpenGL ES的相关属性

CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;

eaglLayer.opaque = YES;

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];



self.enabled = YES;



runSynchronouslyOnVideoProcessingQueue(^{

    [GPUImageContext useImageProcessingContext];



    //2、创建着色器程序;

    displayProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImagePassthroughFragmentShaderString];

    if (!displayProgram.initialized)

    {

        //3、获取属性变量、统一变量的位置

        [displayProgram addAttribute:@"position"];

        [displayProgram addAttribute:@"inputTextureCoordinate"];



        if (![displayProgram link])

        {

            NSString *progLog = [displayProgram programLog];

            NSLog(@"Program link log: %@", progLog);

            NSString *fragLog = [displayProgram fragmentShaderLog];

            NSLog(@"Fragment shader compile log: %@", fragLog);

            NSString *vertLog = [displayProgram vertexShaderLog];

            NSLog(@"Vertex shader compile log: %@", vertLog);

            displayProgram = nil;

            NSAssert(NO, @"Filter shader link failed");

        }

    }



    displayPositionAttribute = [displayProgram attributeIndex:@"position"];

    displayTextureCoordinateAttribute = [displayProgram attributeIndex:@"inputTextureCoordinate"];

    displayInputTextureUniform = [displayProgram uniformIndex:@"inputImageTexture"]; // This does assume a name of "inputTexture" for the fragment shader



    [GPUImageContext setActiveShaderProgram:displayProgram];

    glEnableVertexAttribArray(displayPositionAttribute);

    glEnableVertexAttribArray(displayTextureCoordinateAttribute);

    //4、设置清屏颜色

    [self setBackgroundColorRed:0.0 green:0.0 blue:0.0 alpha:1.0];

    _fillMode = kGPUImageFillModePreserveAspectRatio;

    //5、创建默认的帧缓存、渲染缓存,用于图像的显示

    [self createDisplayFramebuffer];

});
}

方法列表

// 设置背景颜色
- (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
// 该方法未实现

(void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;

实现方法

// 根据旋转模式获取纹理坐标

- (const GLfloat *)textureCoordinatesForRotation:(GPUImageRotationMode)rotationMode;
{

// static const GLfloat noRotationTextureCoordinates[] = {

// 0.0f, 0.0f,

// 1.0f, 0.0f,

// 0.0f, 1.0f,

// 1.0f, 1.0f,

// };

static const GLfloat noRotationTextureCoordinates[] = {

    0.0f, 1.0f,

    1.0f, 1.0f,

    0.0f, 0.0f,

    1.0f, 0.0f,

};



static const GLfloat rotateRightTextureCoordinates[] = {

    1.0f, 1.0f,

    1.0f, 0.0f,

    0.0f, 1.0f,

    0.0f, 0.0f,

};



static const GLfloat rotateLeftTextureCoordinates[] = {

    0.0f, 0.0f,

    0.0f, 1.0f,

    1.0f, 0.0f,

    1.0f, 1.0f,

};



static const GLfloat verticalFlipTextureCoordinates[] = {

    0.0f, 0.0f,

    1.0f, 0.0f,

    0.0f, 1.0f,

    1.0f, 1.0f,

};



static const GLfloat horizontalFlipTextureCoordinates[] = {

    1.0f, 1.0f,

    0.0f, 1.0f,

    1.0f, 0.0f,

    0.0f, 0.0f,

};



static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {

    1.0f, 0.0f,

    1.0f, 1.0f,

    0.0f, 0.0f,

    0.0f, 1.0f,

};



static const GLfloat rotateRightHorizontalFlipTextureCoordinates[] = {

    1.0f, 1.0f,

    1.0f, 0.0f,

    0.0f, 1.0f,

    0.0f, 0.0f,

};



static const GLfloat rotate180TextureCoordinates[] = {

    1.0f, 0.0f,

    0.0f, 0.0f,

    1.0f, 1.0f,

    0.0f, 1.0f,

};



switch(rotationMode)

{

    case kGPUImageNoRotation: return noRotationTextureCoordinates;

    case kGPUImageRotateLeft: return rotateLeftTextureCoordinates;

    case kGPUImageRotateRight: return rotateRightTextureCoordinates;

    case kGPUImageFlipVertical: return verticalFlipTextureCoordinates;

    case kGPUImageFlipHorizonal: return horizontalFlipTextureCoordinates;

    case kGPUImageRotateRightFlipVertical: return rotateRightVerticalFlipTextureCoordinates;

    case kGPUImageRotateRightFlipHorizontal: return rotateRightHorizontalFlipTextureCoordinates;

    case kGPUImageRotate180: return rotate180TextureCoordinates;

}
}

// 覆盖父类的方法,作用是负责OpenGL的图形绘制,并显示在屏幕上

- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
{

runSynchronouslyOnVideoProcessingQueue(^{

    [GPUImageContext setActiveShaderProgram:displayProgram];

    [self setDisplayFramebuffer];



    // 清屏

    glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



    glActiveTexture(GL_TEXTURE4);

    glBindTexture(GL_TEXTURE_2D, [inputFramebufferForDisplay texture]);

    glUniform1i(displayInputTextureUniform, 4);



    glVertexAttribPointer(displayPositionAttribute, 2, GL_FLOAT, 0, 0, imageVertices);

    glVertexAttribPointer(displayTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [GPUImageView textureCoordinatesForRotation:inputRotation]);



    // 绘制

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



    // 显示

    [self presentFramebuffer];

    [inputFramebufferForDisplay unlock];

    inputFramebufferForDisplay = nil;

});
}

// 显示帧缓存

- (void)presentFramebuffer;
{

glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);

[[GPUImageContext sharedImageProcessingContext] presentBufferForDisplay];
}

Last updated