forked from flutter-tizen/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tizen] Support extenal vulkan texture for skia
- Loading branch information
1 parent
547521a
commit c9d57df
Showing
7 changed files
with
302 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
shell/platform/embedder/embedder_external_texture_vulkan.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/shell/platform/embedder/embedder_external_texture_vulkan.h" | ||
|
||
#include "flutter/fml/logging.h" | ||
#include "impeller/core/texture_descriptor.h" | ||
#include "impeller/renderer/context.h" | ||
#include "include/core/SkCanvas.h" | ||
#include "include/core/SkPaint.h" | ||
#include "third_party/skia/include/core/SkAlphaType.h" | ||
#include "third_party/skia/include/core/SkColorSpace.h" | ||
#include "third_party/skia/include/core/SkColorType.h" | ||
#include "third_party/skia/include/core/SkImage.h" | ||
#include "third_party/skia/include/core/SkSize.h" | ||
#include "third_party/skia/include/gpu/GrBackendSurface.h" | ||
#include "third_party/skia/include/gpu/GrDirectContext.h" | ||
#include "third_party/skia/include/gpu/ganesh/SkImageGanesh.h" | ||
#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h" | ||
#include "third_party/skia/include/gpu/gl/GrGLTypes.h" | ||
|
||
namespace flutter { | ||
EmbedderExternalTextureVulkan::EmbedderExternalTextureVulkan( | ||
int64_t texture_identifier, | ||
const ExternalTextureCallback& callback) | ||
: Texture(texture_identifier), external_texture_callback_(callback) { | ||
FML_DCHECK(external_texture_callback_); | ||
} | ||
|
||
// |flutter::Texture| | ||
void EmbedderExternalTextureVulkan::Paint(PaintContext& context, | ||
const SkRect& bounds, | ||
bool freeze, | ||
const DlImageSampling sampling) { | ||
if (last_image_ == nullptr) { | ||
last_image_ = | ||
ResolveTexture(Id(), // | ||
context.gr_context, // | ||
context.aiks_context, // | ||
SkISize::Make(bounds.width(), bounds.height()) // | ||
); | ||
} | ||
|
||
DlCanvas* canvas = context.canvas; | ||
const DlPaint* paint = context.paint; | ||
|
||
if (last_image_) { | ||
SkRect image_bounds = SkRect::Make(last_image_->bounds()); | ||
if (bounds != image_bounds) { | ||
canvas->DrawImageRect(last_image_, image_bounds, bounds, sampling, paint); | ||
} else { | ||
canvas->DrawImage(last_image_, {bounds.x(), bounds.y()}, sampling, paint); | ||
} | ||
} | ||
} | ||
|
||
sk_sp<DlImage> EmbedderExternalTextureVulkan::ResolveTexture( | ||
int64_t texture_id, | ||
GrDirectContext* context, | ||
impeller::AiksContext* aiks_context, | ||
const SkISize& size) { | ||
if (!!aiks_context) { | ||
return ResolveTextureImpeller(texture_id, aiks_context, size); | ||
} else { | ||
return ResolveTextureSkia(texture_id, context, size); | ||
} | ||
} | ||
|
||
sk_sp<DlImage> EmbedderExternalTextureVulkan::ResolveTextureSkia( | ||
int64_t texture_id, | ||
GrDirectContext* context, | ||
const SkISize& size) { | ||
context->flushAndSubmit(); | ||
context->resetContext(kAll_GrBackendState); | ||
std::unique_ptr<FlutterVulkanTexture> texture; | ||
|
||
if (!texture) { | ||
return nullptr; | ||
} | ||
|
||
size_t width = size.width(); | ||
size_t height = size.height(); | ||
|
||
if (texture->width != 0 && texture->height != 0) { | ||
width = texture->width; | ||
height = texture->height; | ||
} | ||
|
||
GrVkImageInfo image_info = { | ||
.fImage = reinterpret_cast<VkImage>(texture->image), | ||
.fImageTiling = VK_IMAGE_TILING_OPTIMAL, | ||
.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED, | ||
.fFormat = static_cast<VkFormat>(texture->format), | ||
.fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | | ||
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | | ||
VK_IMAGE_USAGE_TRANSFER_DST_BIT | | ||
VK_IMAGE_USAGE_SAMPLED_BIT, | ||
.fSampleCount = 1, | ||
.fLevelCount = 1, | ||
}; | ||
|
||
auto gr_backend_texture = | ||
GrBackendTextures::MakeVk(width, height, image_info); | ||
SkImages::TextureReleaseProc release_proc = texture->destruction_callback; | ||
auto image = | ||
SkImages::BorrowTextureFrom(context, // context | ||
gr_backend_texture, // texture handle | ||
kTopLeft_GrSurfaceOrigin, // origin | ||
kRGBA_8888_SkColorType, // color type | ||
kPremul_SkAlphaType, // alpha type | ||
nullptr, // colorspace | ||
release_proc, // texture release proc | ||
texture->user_data // texture release context | ||
); | ||
|
||
if (!image) { | ||
// In case Skia rejects the image, call the release proc so that | ||
// embedders can perform collection of intermediates. | ||
if (release_proc) { | ||
release_proc(texture->user_data); | ||
} | ||
FML_LOG(ERROR) << "Could not create external texture->"; | ||
return nullptr; | ||
} | ||
|
||
return DlImage::Make(std::move(image)); | ||
} | ||
|
||
sk_sp<DlImage> EmbedderExternalTextureVulkan::ResolveTextureImpeller( | ||
int64_t texture_id, | ||
impeller::AiksContext* aiks_context, | ||
const SkISize& size) { | ||
std::unique_ptr<FlutterVulkanTexture> texture = | ||
external_texture_callback_(texture_id, size.width(), size.height()); | ||
if(!texture){ | ||
return nullptr; | ||
} | ||
return nullptr; | ||
} | ||
|
||
EmbedderExternalTextureVulkan::~EmbedderExternalTextureVulkan() = default; | ||
|
||
// |flutter::Texture| | ||
void EmbedderExternalTextureVulkan::OnGrContextCreated() {} | ||
|
||
// |flutter::Texture| | ||
void EmbedderExternalTextureVulkan::OnGrContextDestroyed() {} | ||
|
||
// |flutter::Texture| | ||
void EmbedderExternalTextureVulkan::MarkNewFrameAvailable() { | ||
last_image_ = nullptr; | ||
} | ||
|
||
// |flutter::Texture| | ||
void EmbedderExternalTextureVulkan::OnTextureUnregistered() {} | ||
|
||
} // namespace flutter |
61 changes: 61 additions & 0 deletions
61
shell/platform/embedder/embedder_external_texture_vulkan.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_VULKAN_H_ | ||
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_VULKAN_H_ | ||
|
||
#include "flutter/common/graphics/texture.h" | ||
#include "flutter/fml/macros.h" | ||
#include "flutter/shell/platform/embedder/embedder.h" | ||
#include "third_party/skia/include/core/SkSize.h" | ||
|
||
namespace flutter { | ||
|
||
class EmbedderExternalTextureVulkan : public flutter::Texture { | ||
public: | ||
using ExternalTextureCallback = std::function< | ||
std::unique_ptr<FlutterVulkanTexture>(int64_t, size_t, size_t)>; | ||
EmbedderExternalTextureVulkan(int64_t texture_identifier, | ||
const ExternalTextureCallback& callback); | ||
|
||
~EmbedderExternalTextureVulkan(); | ||
|
||
private: | ||
const ExternalTextureCallback& external_texture_callback_; | ||
|
||
sk_sp<DlImage> last_image_; | ||
|
||
sk_sp<DlImage> ResolveTexture(int64_t texture_id, | ||
GrDirectContext* context, | ||
impeller::AiksContext* aiks_context, | ||
const SkISize& size); | ||
sk_sp<DlImage> ResolveTextureSkia(int64_t texture_id, | ||
GrDirectContext* context, | ||
const SkISize& size); | ||
sk_sp<DlImage> ResolveTextureImpeller(int64_t texture_id, | ||
impeller::AiksContext* aiks_context, | ||
const SkISize& size); | ||
// |flutter::Texture| | ||
void Paint(PaintContext& context, | ||
const SkRect& bounds, | ||
bool freeze, | ||
const DlImageSampling sampling) override; | ||
|
||
// |flutter::Texture| | ||
void OnGrContextCreated() override; | ||
|
||
// |flutter::Texture| | ||
void OnGrContextDestroyed() override; | ||
|
||
// |flutter::Texture| | ||
void MarkNewFrameAvailable() override; | ||
|
||
// |flutter::Texture| | ||
void OnTextureUnregistered() override; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderExternalTextureVulkan); | ||
}; | ||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_EXTERNAL_TEXTURE_VULKAN_H_ |