Flutter Engine
The Flutter Engine
external_texture_d3d.cc
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#include "flutter/shell/platform/windows/external_texture_d3d.h"
6
7#include "flutter/fml/logging.h"
8#include "flutter/shell/platform/embedder/embedder_struct_macros.h"
9
10namespace flutter {
11
15 void* user_data,
16 const egl::Manager* egl_manager,
17 std::shared_ptr<egl::ProcTable> gl)
18 : type_(type),
19 texture_callback_(texture_callback),
20 user_data_(user_data),
21 egl_manager_(egl_manager),
22 gl_(std::move(gl)) {}
23
25 ReleaseImage();
26
27 if (gl_texture_ != 0) {
28 gl_->DeleteTextures(1, &gl_texture_);
29 }
30}
31
33 size_t height,
34 FlutterOpenGLTexture* opengl_texture) {
35 const FlutterDesktopGpuSurfaceDescriptor* descriptor =
36 texture_callback_(width, height, user_data_);
37
38 if (!CreateOrUpdateTexture(descriptor)) {
39 return false;
40 }
41
42 // Populate the texture object used by the engine.
43 opengl_texture->target = GL_TEXTURE_2D;
44 opengl_texture->name = gl_texture_;
45 opengl_texture->format = GL_RGBA8_OES;
46 opengl_texture->destruction_callback = nullptr;
47 opengl_texture->user_data = nullptr;
48 opengl_texture->width = SAFE_ACCESS(descriptor, visible_width, 0);
49 opengl_texture->height = SAFE_ACCESS(descriptor, visible_height, 0);
50
51 return true;
52}
53
54void ExternalTextureD3d::ReleaseImage() {
55 if (egl_surface_ != EGL_NO_SURFACE) {
56 eglReleaseTexImage(egl_manager_->egl_display(), egl_surface_,
57 EGL_BACK_BUFFER);
58 eglDestroySurface(egl_manager_->egl_display(), egl_surface_);
59 egl_surface_ = EGL_NO_SURFACE;
60 }
61}
62
63bool ExternalTextureD3d::CreateOrUpdateTexture(
64 const FlutterDesktopGpuSurfaceDescriptor* descriptor) {
65 if (descriptor == nullptr ||
66 SAFE_ACCESS(descriptor, handle, nullptr) == nullptr) {
67 ReleaseImage();
68 return false;
69 }
70
71 if (gl_texture_ == 0) {
72 gl_->GenTextures(1, &gl_texture_);
73
74 gl_->BindTexture(GL_TEXTURE_2D, gl_texture_);
75 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
76 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
77 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79 } else {
80 gl_->BindTexture(GL_TEXTURE_2D, gl_texture_);
81 }
82
83 auto handle = SAFE_ACCESS(descriptor, handle, nullptr);
84 if (handle != last_surface_handle_) {
85 ReleaseImage();
86
87 EGLint attributes[] = {
88 EGL_WIDTH,
89 static_cast<EGLint>(SAFE_ACCESS(descriptor, width, 0)),
90 EGL_HEIGHT,
91 static_cast<EGLint>(SAFE_ACCESS(descriptor, height, 0)),
92 EGL_TEXTURE_TARGET,
93 EGL_TEXTURE_2D,
94 EGL_TEXTURE_FORMAT,
95 EGL_TEXTURE_RGBA, // always EGL_TEXTURE_RGBA
96 EGL_NONE};
97
98 egl_surface_ = egl_manager_->CreateSurfaceFromHandle(
100 ? EGL_D3D_TEXTURE_ANGLE
101 : EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
102 handle, attributes);
103
104 if (egl_surface_ == EGL_NO_SURFACE ||
105 eglBindTexImage(egl_manager_->egl_display(), egl_surface_,
106 EGL_BACK_BUFFER) == EGL_FALSE) {
107 FML_LOG(ERROR) << "Binding D3D surface failed.";
108 }
109 last_surface_handle_ = handle;
110 }
111
112 auto release_callback = SAFE_ACCESS(descriptor, release_callback, nullptr);
113 if (release_callback) {
114 release_callback(SAFE_ACCESS(descriptor, release_context, nullptr));
115 }
116 return egl_surface_ != EGL_NO_SURFACE;
117}
118
119} // namespace flutter
GLenum type
bool PopulateTexture(size_t width, size_t height, FlutterOpenGLTexture *opengl_texture) override
ExternalTextureD3d(FlutterDesktopGpuSurfaceType type, const FlutterDesktopGpuSurfaceTextureCallback texture_callback, void *user_data, const egl::Manager *egl_manager, std::shared_ptr< egl::ProcTable > gl)
EGLSurface CreateSurfaceFromHandle(EGLenum handle_type, EGLClientBuffer handle, const EGLint *attributes) const
Definition: manager.cc:300
EGLDisplay egl_display() const
Definition: manager.h:63
#define SAFE_ACCESS(pointer, member, default_value)
const FlutterDesktopGpuSurfaceDescriptor *(* FlutterDesktopGpuSurfaceTextureCallback)(size_t width, size_t height, void *user_data)
FlutterDesktopGpuSurfaceType
@ kFlutterDesktopGpuSurfaceTypeD3d11Texture2D
#define FML_LOG(severity)
Definition: logging.h:82
gl
Definition: malisc.py:41
Definition: ref_ptr.h:256
int32_t height
int32_t width
uint32_t name
The name of the texture.
Definition: embedder.h:371
VoidCallback destruction_callback
Definition: embedder.h:378
void * user_data
User data to be returned on the invocation of the destruction callback.
Definition: embedder.h:375
size_t height
Height of the texture.
Definition: embedder.h:386
uint32_t format
The texture format (example GL_RGBA8).
Definition: embedder.h:373
void * user_data
FlutterDesktopPixelBufferTextureCallback texture_callback
#define ERROR(message)
Definition: elf_loader.cc:260