Flutter Engine
FlutterExternalTextureGL.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h"
6 
7 #import <AppKit/AppKit.h>
8 #import <CoreVideo/CoreVideo.h>
9 #import <OpenGL/gl.h>
10 
11 static void OnCVOpenGLTextureRelease(CVOpenGLTextureRef cvOpenGLTexture) {
12  CVOpenGLTextureRelease(cvOpenGLTexture);
13 }
14 
15 @implementation FlutterExternalTextureGL {
16  /**
17  * OpenGL texture cache.
18  */
19  CVOpenGLTextureCacheRef _openGLTextureCache;
20  /**
21  * User side texture object, used to copy pixel buffer.
22  */
23  id<FlutterTexture> _texture;
24 }
25 
26 - (instancetype)initWithFlutterTexture:(id<FlutterTexture>)texture {
27  self = [super init];
28  if (self) {
29  _texture = texture;
30  }
31  return self;
32 }
33 
34 - (int64_t)textureID {
35  return reinterpret_cast<int64_t>(self);
36 }
37 
38 - (BOOL)populateTexture:(FlutterOpenGLTexture*)openGLTexture {
39  // Copy the pixel buffer from the FlutterTexture instance implemented on the user side.
40  CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer];
41 
42  if (!pixelBuffer) {
43  return NO;
44  }
45 
46  // Create the opengl texture cache if necessary.
47  if (!_openGLTextureCache) {
48  CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj;
49  CGLPixelFormatObj format = CGLGetPixelFormat(context);
50  if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL,
51  &_openGLTextureCache) != kCVReturnSuccess) {
52  NSLog(@"Could not create texture cache.");
53  CVPixelBufferRelease(pixelBuffer);
54  return NO;
55  }
56  }
57 
58  // Try to clear the cache of OpenGL textures to save memory.
59  CVOpenGLTextureCacheFlush(_openGLTextureCache, 0);
60 
61  CVOpenGLTextureRef cvOpenGLTexture = NULL;
62  if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _openGLTextureCache,
63  pixelBuffer, NULL,
64  &cvOpenGLTexture) != kCVReturnSuccess) {
65  CVPixelBufferRelease(pixelBuffer);
66  return NO;
67  }
68 
69  openGLTexture->target = static_cast<uint32_t>(CVOpenGLTextureGetTarget(cvOpenGLTexture));
70  openGLTexture->name = static_cast<uint32_t>(CVOpenGLTextureGetName(cvOpenGLTexture));
71  openGLTexture->format = static_cast<uint32_t>(GL_RGBA8);
72  openGLTexture->destruction_callback = (VoidCallback)OnCVOpenGLTextureRelease;
73  openGLTexture->user_data = cvOpenGLTexture;
74  openGLTexture->width = CVPixelBufferGetWidth(pixelBuffer);
75  openGLTexture->height = CVPixelBufferGetHeight(pixelBuffer);
76 
77  CVPixelBufferRelease(pixelBuffer);
78  return YES;
79 }
80 
81 - (void)dealloc {
82  CVOpenGLTextureCacheRelease(_openGLTextureCache);
83 }
84 
85 @end
static void OnCVOpenGLTextureRelease(CVOpenGLTextureRef cvOpenGLTexture)
id< FlutterTexture > _texture
void(* VoidCallback)(void *)
Definition: embedder.h:243