-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCVPOpenGLESTextureCache.m
118 lines (93 loc) · 3.16 KB
/
CVPOpenGLESTextureCache.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#import "CVPOpenGLESTextureCache.h"
#import <CoreVideo/CoreVideo.h>
#import "NSError+CVPError.h"
#import "GLPPixelformat+CVPPixelformat.h"
#import "CVPOpenGLESTexture.h"
@interface CVPOpenGLESTextureCache ()
{
@private
CVOpenGLESTextureCacheRef textureCache;
}
@end
@implementation CVPOpenGLESTextureCache
+ (instancetype)textureCacheWithContext:(EAGLContext *)context error:(NSError **)error
{
return [[self alloc] initWithContext:context error:error];
}
- (instancetype)initWithContext:(EAGLContext *)context error:(NSError **)error
{
self = [super init];
if(self != nil)
{
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, context, NULL, &textureCache);
if(err != kCVReturnSuccess)
{
if(error != nil)
{
*error = [NSError errorWithDomain:CVPErrorDomain code:err userInfo:nil];
}
return nil;
}
}
return self;
}
- (void)dealloc
{
if(textureCache)
{
CFRelease(textureCache), textureCache = nil;
}
}
- (CVPOpenGLESTexture*)textureWithPixelBuffer:(CVPixelBufferRef)pixelBuffer error:(NSError**)error
{
return [self textureWithPixelBuffer:pixelBuffer plane:0 error:error];
}
- (CVPOpenGLESTexture*)textureWithPixelBuffer:(CVPixelBufferRef)pixelBuffer plane:(size_t)plane error:(NSError**)error
{
GLPPixelformat pixelformat = glpPixelformatFromCVPixelBuffer(pixelBuffer, plane);
size_t width = 0;
size_t height = 0;
size_t planeCount = CVPixelBufferGetPlaneCount(pixelBuffer);
if(planeCount != 0)
{
// this will return 0 if the pixel buffer is single planar
width = CVPixelBufferGetWidthOfPlane(pixelBuffer, plane);
height = CVPixelBufferGetHeightOfPlane(pixelBuffer, plane);
}
else
{
width = CVPixelBufferGetWidth(pixelBuffer);
height = CVPixelBufferGetHeight(pixelBuffer);
}
return [self textureWithPixelBuffer:pixelBuffer pixelformat:pixelformat width:(GLuint)width height:(GLuint)height plane:plane error:error];
}
- (CVPOpenGLESTexture *)textureWithPixelBuffer:(CVPixelBufferRef)pixelBuffer pixelformat:(GLPPixelformat)pixelformat width:(GLsizei)width height:(GLsizei)height error:(NSError **)error
{
return [self textureWithPixelBuffer:pixelBuffer pixelformat:pixelformat width:width height:height plane:0 error:error];
}
- (CVPOpenGLESTexture *)textureWithPixelBuffer:(CVPixelBufferRef)pixelBuffer pixelformat:(GLPPixelformat)pixelformat width:(GLsizei)width height:(GLsizei)height plane:(size_t)plane error:(NSError **)error
{
const GLenum internalFormat = glpPixelformatGetInternalFormat(pixelformat);
const GLenum format = glpPixelformatGetFormat(pixelformat);
const GLenum type = glpPixelformatGetType(pixelformat);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, internalFormat, width, height, format, type, plane, &texture);
if(err != kCVReturnSuccess)
{
if(error != nil)
{
*error = [NSError errorWithDomain:CVPErrorDomain code:err userInfo:nil];
}
if(texture != NULL)
{
CFRelease(texture), texture = NULL;
}
return nil;
}
return [CVPOpenGLESTexture textureWithCVOpenGLESTexture:texture];
}
- (void)flush
{
CVOpenGLESTextureCacheFlush(textureCache, 0);
}
@end