|
| 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/embedder/embedder_external_texture_gl_impeller.h" |
| 6 | + |
| 7 | +#include "flutter/fml/logging.h" |
| 8 | +#include "flutter/impeller/display_list/dl_image_impeller.h" |
| 9 | +#include "flutter/impeller/renderer/backend/gles/context_gles.h" |
| 10 | +#include "flutter/impeller/renderer/backend/gles/texture_gles.h" |
| 11 | +#include "impeller/aiks/aiks_context.h" |
| 12 | +#include "impeller/renderer/backend/gles/gles.h" |
| 13 | +#include "include/core/SkCanvas.h" |
| 14 | +#include "include/core/SkPaint.h" |
| 15 | + |
| 16 | +namespace flutter { |
| 17 | + |
| 18 | +EmbedderExternalTextureGLImpeller::EmbedderExternalTextureGLImpeller( |
| 19 | + int64_t texture_identifier, |
| 20 | + const EmbedderExternalTextureGL::ExternalTextureCallback& callback) |
| 21 | + : Texture(texture_identifier), external_texture_callback_(callback) { |
| 22 | + FML_DCHECK(external_texture_callback_); |
| 23 | +} |
| 24 | + |
| 25 | +EmbedderExternalTextureGLImpeller::~EmbedderExternalTextureGLImpeller() = |
| 26 | + default; |
| 27 | + |
| 28 | +// |flutter::Texture| |
| 29 | +void EmbedderExternalTextureGLImpeller::Paint(PaintContext& context, |
| 30 | + const SkRect& bounds, |
| 31 | + bool freeze, |
| 32 | + const DlImageSampling sampling) { |
| 33 | + if (last_image_ == nullptr) { |
| 34 | + last_image_ = |
| 35 | + ResolveTexture(Id(), // |
| 36 | + context, // |
| 37 | + SkISize::Make(bounds.width(), bounds.height()) // |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + DlCanvas* canvas = context.canvas; |
| 42 | + const DlPaint* paint = context.paint; |
| 43 | + |
| 44 | + if (last_image_) { |
| 45 | + SkRect image_bounds = SkRect::Make(last_image_->bounds()); |
| 46 | + if (bounds != image_bounds) { |
| 47 | + canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint); |
| 48 | + } else { |
| 49 | + canvas->DrawImage(last_image_, {bounds.x(), bounds.y()}, sampling, paint); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// |flutter::Texture| |
| 55 | +void EmbedderExternalTextureGLImpeller::OnGrContextCreated() {} |
| 56 | + |
| 57 | +// |flutter::Texture| |
| 58 | +void EmbedderExternalTextureGLImpeller::OnGrContextDestroyed() {} |
| 59 | + |
| 60 | +// |flutter::Texture| |
| 61 | +void EmbedderExternalTextureGLImpeller::MarkNewFrameAvailable() { |
| 62 | + last_image_ = nullptr; |
| 63 | +} |
| 64 | + |
| 65 | +// |flutter::Texture| |
| 66 | +void EmbedderExternalTextureGLImpeller::OnTextureUnregistered() {} |
| 67 | + |
| 68 | +sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolveTexture( |
| 69 | + int64_t texture_id, |
| 70 | + PaintContext& context, |
| 71 | + const SkISize& size) { |
| 72 | + std::unique_ptr<FlutterOpenGLTexture> texture = |
| 73 | + external_texture_callback_(texture_id, size.width(), size.height()); |
| 74 | + if (!texture) { |
| 75 | + return nullptr; |
| 76 | + } |
| 77 | + size_t width = size.width(); |
| 78 | + size_t height = size.height(); |
| 79 | + if (texture->width != 0 && texture->height != 0) { |
| 80 | + width = texture->width; |
| 81 | + height = texture->height; |
| 82 | + } |
| 83 | + if (texture->impeller_texture_type == |
| 84 | + FlutterGLImpellerTextureType::kFlutterGLImpellerTexturePixelBuffer) { |
| 85 | + return ResolvePixelBufferTexture(texture.get(), context, |
| 86 | + SkISize::Make(width, height)); |
| 87 | + } else { |
| 88 | + return ResolveGpuSurfaceTexture(texture.get(), context, |
| 89 | + SkISize::Make(width, height)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolvePixelBufferTexture( |
| 94 | + FlutterOpenGLTexture* texture, |
| 95 | + PaintContext& context, |
| 96 | + const SkISize& size) { |
| 97 | + impeller::TextureDescriptor desc; |
| 98 | + desc.type = impeller::TextureType::kTexture2D; |
| 99 | + impeller::AiksContext* aiks_context = context.aiks_context; |
| 100 | + const auto& gl_context = |
| 101 | + impeller::ContextGLES::Cast(*aiks_context->GetContext()); |
| 102 | + desc.storage_mode = impeller::StorageMode::kDevicePrivate; |
| 103 | + desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; |
| 104 | + desc.size = {static_cast<int>(size.width()), static_cast<int>(size.height())}; |
| 105 | + desc.mip_count = 1; |
| 106 | + |
| 107 | + auto textureGLES = |
| 108 | + std::make_shared<impeller::TextureGLES>(gl_context.GetReactor(), desc); |
| 109 | + if (!textureGLES->SetContents(texture->buffer, texture->buffer_size)) { |
| 110 | + if (texture->destruction_callback) { |
| 111 | + texture->destruction_callback(texture->user_data); |
| 112 | + } |
| 113 | + return nullptr; |
| 114 | + } |
| 115 | + if (texture->destruction_callback) { |
| 116 | + texture->destruction_callback(texture->user_data); |
| 117 | + } |
| 118 | + return impeller::DlImageImpeller::Make(textureGLES); |
| 119 | +} |
| 120 | + |
| 121 | +sk_sp<DlImage> EmbedderExternalTextureGLImpeller::ResolveGpuSurfaceTexture( |
| 122 | + FlutterOpenGLTexture* texture, |
| 123 | + PaintContext& context, |
| 124 | + const SkISize& size) { |
| 125 | + impeller::TextureDescriptor desc; |
| 126 | + desc.type = impeller::TextureType::kTextureExternalOES; |
| 127 | + impeller::AiksContext* aiks_context = context.aiks_context; |
| 128 | + const auto& gl_context = |
| 129 | + impeller::ContextGLES::Cast(*aiks_context->GetContext()); |
| 130 | + desc.storage_mode = impeller::StorageMode::kDevicePrivate; |
| 131 | + desc.format = impeller::PixelFormat::kR8G8B8A8UNormInt; |
| 132 | + desc.size = {static_cast<int>(size.width()), static_cast<int>(size.height())}; |
| 133 | + desc.mip_count = 1; |
| 134 | + auto textureGLES = std::make_shared<impeller::TextureGLES>( |
| 135 | + gl_context.GetReactor(), desc, |
| 136 | + impeller::TextureGLES::IsWrapped::kWrapped); |
| 137 | + textureGLES->SetCoordinateSystem( |
| 138 | + impeller::TextureCoordinateSystem::kUploadFromHost); |
| 139 | + if (!textureGLES->Bind()) { |
| 140 | + if (texture->destruction_callback) { |
| 141 | + texture->destruction_callback(texture->user_data); |
| 142 | + } |
| 143 | + return nullptr; |
| 144 | + } |
| 145 | + |
| 146 | + if (!texture->bind_callback(texture->user_data)) { |
| 147 | + if (texture->destruction_callback) { |
| 148 | + texture->destruction_callback(texture->user_data); |
| 149 | + } |
| 150 | + return nullptr; |
| 151 | + } |
| 152 | + |
| 153 | + if (texture->destruction_callback) { |
| 154 | + texture->destruction_callback(texture->user_data); |
| 155 | + } |
| 156 | + |
| 157 | + return impeller::DlImageImpeller::Make(textureGLES); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace flutter |
0 commit comments