|
| 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/linux_embedded/external_texture_d3d.h" |
| 6 | + |
| 7 | +#include "flutter/shell/platform/embedder/embedder_struct_macros.h" |
| 8 | +#include "flutter/shell/platform/linux_embedded/logger.h" |
| 9 | + |
| 10 | +namespace flutter { |
| 11 | + |
| 12 | +ExternalTextureD3d::ExternalTextureD3d( |
| 13 | + const FlutterDesktopGpuSurfaceTextureCallback texture_callback, |
| 14 | + void* user_data, |
| 15 | + const ELinuxRenderSurfaceTarget* surface_manager, |
| 16 | + const GlProcs& gl_procs) |
| 17 | + : texture_callback_(texture_callback), |
| 18 | + user_data_(user_data), |
| 19 | + surface_manager_(surface_manager), |
| 20 | + gl_(gl_procs) {} |
| 21 | + |
| 22 | +ExternalTextureD3d::~ExternalTextureD3d() { |
| 23 | + ReleaseImage(); |
| 24 | + |
| 25 | + if (gl_texture_ != 0) { |
| 26 | + gl_.glDeleteTextures(1, &gl_texture_); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +bool ExternalTextureD3d::PopulateTexture(size_t width, |
| 31 | + size_t height, |
| 32 | + FlutterOpenGLTexture* opengl_texture) { |
| 33 | + const FlutterDesktopGpuSurfaceDescriptor* descriptor = |
| 34 | + texture_callback_(width, height, user_data_); |
| 35 | + |
| 36 | + if (!CreateOrUpdateTexture(descriptor)) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + // Populate the texture object used by the engine. |
| 41 | + opengl_texture->target = GL_TEXTURE_2D; |
| 42 | + opengl_texture->name = gl_texture_; |
| 43 | + opengl_texture->format = GL_RGBA8_OES; |
| 44 | + opengl_texture->destruction_callback = nullptr; |
| 45 | + opengl_texture->user_data = nullptr; |
| 46 | + opengl_texture->width = SAFE_ACCESS(descriptor, visible_width, 0); |
| 47 | + opengl_texture->height = SAFE_ACCESS(descriptor, visible_height, 0); |
| 48 | + |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +void ExternalTextureD3d::ReleaseImage() { |
| 53 | + if (egl_surface_ != EGL_NO_SURFACE) { |
| 54 | + eglReleaseTexImage(surface_manager_->EglDisplay(), egl_surface_, |
| 55 | + EGL_BACK_BUFFER); |
| 56 | + eglDestroySurface(surface_manager_->EglDisplay(), egl_surface_); |
| 57 | + egl_surface_ = EGL_NO_SURFACE; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +bool ExternalTextureD3d::CreateOrUpdateTexture( |
| 62 | + const FlutterDesktopGpuSurfaceDescriptor* descriptor) { |
| 63 | + if (descriptor == nullptr || |
| 64 | + SAFE_ACCESS(descriptor, handle, nullptr) == nullptr) { |
| 65 | + ReleaseImage(); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if (gl_texture_ == 0) { |
| 70 | + gl_.glGenTextures(1, &gl_texture_); |
| 71 | + |
| 72 | + gl_.glBindTexture(GL_TEXTURE_2D, gl_texture_); |
| 73 | + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 74 | + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 75 | + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 76 | + gl_.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 77 | + } else { |
| 78 | + gl_.glBindTexture(GL_TEXTURE_2D, gl_texture_); |
| 79 | + } |
| 80 | + |
| 81 | + auto handle = SAFE_ACCESS(descriptor, handle, nullptr); |
| 82 | + if (handle != last_surface_handle_) { |
| 83 | + ReleaseImage(); |
| 84 | + |
| 85 | + EGLint attributes[] = { |
| 86 | + EGL_WIDTH, |
| 87 | + static_cast<EGLint>(SAFE_ACCESS(descriptor, width, 0)), |
| 88 | + EGL_HEIGHT, |
| 89 | + static_cast<EGLint>(SAFE_ACCESS(descriptor, height, 0)), |
| 90 | + EGL_TEXTURE_TARGET, |
| 91 | + EGL_TEXTURE_2D, |
| 92 | + EGL_TEXTURE_FORMAT, |
| 93 | + EGL_TEXTURE_RGBA, // always EGL_TEXTURE_RGBA |
| 94 | + EGL_NONE}; |
| 95 | + |
| 96 | + egl_surface_ = surface_manager_->CreateSurfaceFromHandle( |
| 97 | + EGL_OPENVG_IMAGE, handle, attributes); |
| 98 | + |
| 99 | + if (egl_surface_ == EGL_NO_SURFACE || |
| 100 | + eglBindTexImage(surface_manager_->EglDisplay(), egl_surface_, |
| 101 | + EGL_BACK_BUFFER) == EGL_FALSE) { |
| 102 | + ELINUX_LOG(ERROR) << "Binding D3D surface failed."; |
| 103 | + } |
| 104 | + last_surface_handle_ = handle; |
| 105 | + } |
| 106 | + |
| 107 | + auto release_callback = SAFE_ACCESS(descriptor, release_callback, nullptr); |
| 108 | + if (release_callback) { |
| 109 | + release_callback(SAFE_ACCESS(descriptor, release_context, nullptr)); |
| 110 | + } |
| 111 | + return egl_surface_ != EGL_NO_SURFACE; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace flutter |
0 commit comments