[Impeller] Ensure that overlay surfaces are constructed with wide gamut settings. (flutter/engine#48190)
Fixes https://github.com/flutter/flutter/issues/138373 When constructing overlay layers when there are platform views, make sure the same pixel format and color space as the main view is used. Add validation to impeller HAL about blitting different pixel formats.
This commit is contained in:
@@ -176,6 +176,7 @@
|
||||
../../../flutter/impeller/renderer/backend/vulkan/pass_bindings_cache_unittests.cc
|
||||
../../../flutter/impeller/renderer/backend/vulkan/resource_manager_vk_unittests.cc
|
||||
../../../flutter/impeller/renderer/backend/vulkan/test
|
||||
../../../flutter/impeller/renderer/blit_pass_unittests.cc
|
||||
../../../flutter/impeller/renderer/capabilities_unittests.cc
|
||||
../../../flutter/impeller/renderer/compute_subgroup_unittests.cc
|
||||
../../../flutter/impeller/renderer/compute_unittests.cc
|
||||
|
||||
@@ -122,6 +122,7 @@ impeller_component("renderer_unittests") {
|
||||
testonly = true
|
||||
|
||||
sources = [
|
||||
"blit_pass_unittests.cc",
|
||||
"capabilities_unittests.cc",
|
||||
"device_buffer_unittests.cc",
|
||||
"host_buffer_unittests.cc",
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/paths.h"
|
||||
#include "flutter/fml/synchronization/sync_switch.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/sampler_descriptor.h"
|
||||
#include "impeller/renderer/backend/metal/gpu_tracer_mtl.h"
|
||||
#include "impeller/renderer/backend/metal/sampler_library_mtl.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "impeller/base/strings.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/host_buffer.h"
|
||||
#include "impeller/renderer/blit_command.h"
|
||||
|
||||
@@ -52,6 +53,16 @@ bool BlitPass::AddCopy(std::shared_ptr<Texture> source,
|
||||
static_cast<int>(destination->GetTextureDescriptor().sample_count));
|
||||
return false;
|
||||
}
|
||||
if (source->GetTextureDescriptor().format !=
|
||||
destination->GetTextureDescriptor().format) {
|
||||
VALIDATION_LOG << SPrintF(
|
||||
"The source pixel format (%s) must match the destination pixel format "
|
||||
"(%s) "
|
||||
"for blits.",
|
||||
PixelFormatToString(source->GetTextureDescriptor().format),
|
||||
PixelFormatToString(destination->GetTextureDescriptor().format));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!source_region.has_value()) {
|
||||
source_region = IRect::MakeSize(source->GetSize());
|
||||
|
||||
77
engine/src/flutter/impeller/renderer/blit_pass_unittests.cc
Normal file
77
engine/src/flutter/impeller/renderer/blit_pass_unittests.cc
Normal file
@@ -0,0 +1,77 @@
|
||||
// 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 "gtest/gtest.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/core/formats.h"
|
||||
#include "impeller/core/texture_descriptor.h"
|
||||
#include "impeller/playground/playground_test.h"
|
||||
#include "impeller/renderer/command_buffer.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
|
||||
using BlitPassTest = PlaygroundTest;
|
||||
INSTANTIATE_PLAYGROUND_SUITE(BlitPassTest);
|
||||
|
||||
TEST_P(BlitPassTest, BlitAcrossDifferentPixelFormatsFails) {
|
||||
ScopedValidationDisable scope; // avoid noise in output.
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
auto blit_pass = cmd_buffer->CreateBlitPass();
|
||||
|
||||
TextureDescriptor src_desc;
|
||||
src_desc.format = PixelFormat::kA8UNormInt;
|
||||
src_desc.size = {100, 100};
|
||||
auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
|
||||
|
||||
TextureDescriptor dst_format;
|
||||
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
dst_format.size = {100, 100};
|
||||
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
|
||||
|
||||
EXPECT_FALSE(blit_pass->AddCopy(src, dst));
|
||||
}
|
||||
|
||||
TEST_P(BlitPassTest, BlitAcrossDifferentSampleCountsFails) {
|
||||
ScopedValidationDisable scope; // avoid noise in output.
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
auto blit_pass = cmd_buffer->CreateBlitPass();
|
||||
|
||||
TextureDescriptor src_desc;
|
||||
src_desc.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
src_desc.sample_count = SampleCount::kCount4;
|
||||
src_desc.size = {100, 100};
|
||||
auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
|
||||
|
||||
TextureDescriptor dst_format;
|
||||
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
dst_format.size = {100, 100};
|
||||
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
|
||||
|
||||
EXPECT_FALSE(blit_pass->AddCopy(src, dst));
|
||||
}
|
||||
|
||||
TEST_P(BlitPassTest, BlitPassesForMatchingFormats) {
|
||||
ScopedValidationDisable scope; // avoid noise in output.
|
||||
auto context = GetContext();
|
||||
auto cmd_buffer = context->CreateCommandBuffer();
|
||||
auto blit_pass = cmd_buffer->CreateBlitPass();
|
||||
|
||||
TextureDescriptor src_desc;
|
||||
src_desc.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
src_desc.size = {100, 100};
|
||||
auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
|
||||
|
||||
TextureDescriptor dst_format;
|
||||
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
dst_format.size = {100, 100};
|
||||
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
|
||||
|
||||
EXPECT_TRUE(blit_pass->AddCopy(src, dst));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_OVERLAY_VIEW_H_
|
||||
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_OVERLAY_VIEW_H_
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
#include <UIKit/UIKit.h>
|
||||
|
||||
#include <memory>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include "flutter/fml/memory/weak_ptr.h"
|
||||
#include "flutter/shell/common/shell.h"
|
||||
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
|
||||
#include "fml/platform/darwin/cf_utils.h"
|
||||
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"
|
||||
|
||||
@@ -27,12 +29,12 @@
|
||||
/// views also handle touch propagation and the like for touches that occurs
|
||||
/// either on overlays or otherwise may be intercepted by the platform views.
|
||||
@interface FlutterOverlayView : UIView
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
|
||||
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
|
||||
|
||||
- (instancetype)init NS_DESIGNATED_INITIALIZER;
|
||||
- (instancetype)initWithContentsScale:(CGFloat)contentsScale;
|
||||
- (instancetype)initWithContentsScale:(CGFloat)contentsScale
|
||||
pixelFormat:(MTLPixelFormat)pixelFormat;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
|
||||
#include <CoreGraphics/CGColorSpace.h>
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/common/task_runners.h"
|
||||
@@ -18,7 +20,9 @@
|
||||
|
||||
// This is mostly a duplication of FlutterView.
|
||||
// TODO(amirh): once GL support is in evaluate if we can merge this with FlutterView.
|
||||
@implementation FlutterOverlayView
|
||||
@implementation FlutterOverlayView {
|
||||
fml::CFRef<CGColorSpaceRef> _colorSpaceRef;
|
||||
}
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
NSAssert(NO, @"FlutterOverlayView must init or initWithContentsScale");
|
||||
@@ -42,15 +46,24 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithContentsScale:(CGFloat)contentsScale {
|
||||
- (instancetype)initWithContentsScale:(CGFloat)contentsScale
|
||||
pixelFormat:(MTLPixelFormat)pixelFormat {
|
||||
self = [self init];
|
||||
|
||||
if ([self.layer isKindOfClass:NSClassFromString(@"CAMetalLayer")]) {
|
||||
self.layer.allowsGroupOpacity = NO;
|
||||
self.layer.contentsScale = contentsScale;
|
||||
self.layer.rasterizationScale = contentsScale;
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
|
||||
CAMetalLayer* layer = (CAMetalLayer*)self.layer;
|
||||
#pragma clang diagnostic pop
|
||||
layer.pixelFormat = pixelFormat;
|
||||
if (pixelFormat == MTLPixelFormatRGBA16Float) {
|
||||
self->_colorSpaceRef = fml::CFRef(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB));
|
||||
layer.colorspace = self->_colorSpaceRef;
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
#import <UIKit/UIGestureRecognizerSubclass.h>
|
||||
|
||||
#include <list>
|
||||
@@ -14,6 +15,7 @@
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
|
||||
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
|
||||
|
||||
@@ -85,13 +87,15 @@ BOOL canApplyBlurBackdrop = YES;
|
||||
|
||||
std::shared_ptr<FlutterPlatformViewLayer> FlutterPlatformViewLayerPool::GetLayer(
|
||||
GrDirectContext* gr_context,
|
||||
const std::shared_ptr<IOSContext>& ios_context) {
|
||||
const std::shared_ptr<IOSContext>& ios_context,
|
||||
MTLPixelFormat pixel_format) {
|
||||
if (available_layer_index_ >= layers_.size()) {
|
||||
std::shared_ptr<FlutterPlatformViewLayer> layer;
|
||||
fml::scoped_nsobject<UIView> overlay_view;
|
||||
fml::scoped_nsobject<UIView> overlay_view_wrapper;
|
||||
|
||||
if (!gr_context) {
|
||||
bool impeller_enabled = !!ios_context->GetImpellerContext();
|
||||
if (!gr_context && !impeller_enabled) {
|
||||
overlay_view.reset([[FlutterOverlayView alloc] init]);
|
||||
overlay_view_wrapper.reset([[FlutterOverlayView alloc] init]);
|
||||
|
||||
@@ -104,8 +108,10 @@ std::shared_ptr<FlutterPlatformViewLayer> FlutterPlatformViewLayerPool::GetLayer
|
||||
std::move(surface));
|
||||
} else {
|
||||
CGFloat screenScale = [UIScreen mainScreen].scale;
|
||||
overlay_view.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale]);
|
||||
overlay_view_wrapper.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale]);
|
||||
overlay_view.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
|
||||
pixelFormat:pixel_format]);
|
||||
overlay_view_wrapper.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale
|
||||
pixelFormat:pixel_format]);
|
||||
|
||||
auto ca_layer = fml::scoped_nsobject<CALayer>{[[overlay_view.get() layer] retain]};
|
||||
std::unique_ptr<IOSSurface> ios_surface = IOSSurface::Create(ios_context, ca_layer);
|
||||
@@ -735,13 +741,15 @@ bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
|
||||
// on the overlay layer.
|
||||
background_canvas->ClipRect(joined_rect, DlCanvas::ClipOp::kDifference);
|
||||
// Get a new host layer.
|
||||
std::shared_ptr<FlutterPlatformViewLayer> layer = GetLayer(gr_context, //
|
||||
ios_context, //
|
||||
slice, //
|
||||
joined_rect, //
|
||||
current_platform_view_id, //
|
||||
overlay_id //
|
||||
);
|
||||
std::shared_ptr<FlutterPlatformViewLayer> layer =
|
||||
GetLayer(gr_context, //
|
||||
ios_context, //
|
||||
slice, //
|
||||
joined_rect, //
|
||||
current_platform_view_id, //
|
||||
overlay_id, //
|
||||
((FlutterView*)flutter_view_.get()).pixelFormat //
|
||||
);
|
||||
did_submit &= layer->did_submit_last_frame;
|
||||
platform_view_layers[current_platform_view_id].push_back(layer);
|
||||
overlay_id++;
|
||||
@@ -811,9 +819,11 @@ std::shared_ptr<FlutterPlatformViewLayer> FlutterPlatformViewsController::GetLay
|
||||
EmbedderViewSlice* slice,
|
||||
SkRect rect,
|
||||
int64_t view_id,
|
||||
int64_t overlay_id) {
|
||||
int64_t overlay_id,
|
||||
MTLPixelFormat pixel_format) {
|
||||
FML_DCHECK(flutter_view_);
|
||||
std::shared_ptr<FlutterPlatformViewLayer> layer = layer_pool_->GetLayer(gr_context, ios_context);
|
||||
std::shared_ptr<FlutterPlatformViewLayer> layer =
|
||||
layer_pool_->GetLayer(gr_context, ios_context, pixel_format);
|
||||
|
||||
UIView* overlay_view_wrapper = layer->overlay_view_wrapper.get();
|
||||
auto screenScale = [UIScreen mainScreen].scale;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
#include "flutter/flow/embedded_views.h"
|
||||
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
|
||||
#include "flutter/shell/common/shell.h"
|
||||
@@ -167,9 +168,9 @@ class FlutterPlatformViewLayerPool {
|
||||
|
||||
// Gets a layer from the pool if available, or allocates a new one.
|
||||
// Finally, it marks the layer as used. That is, it increments `available_layer_index_`.
|
||||
std::shared_ptr<FlutterPlatformViewLayer> GetLayer(
|
||||
GrDirectContext* gr_context,
|
||||
const std::shared_ptr<IOSContext>& ios_context);
|
||||
std::shared_ptr<FlutterPlatformViewLayer> GetLayer(GrDirectContext* gr_context,
|
||||
const std::shared_ptr<IOSContext>& ios_context,
|
||||
MTLPixelFormat pixel_format);
|
||||
|
||||
// Gets the layers in the pool that aren't currently used.
|
||||
// This method doesn't mark the layers as unused.
|
||||
@@ -320,7 +321,8 @@ class FlutterPlatformViewsController {
|
||||
EmbedderViewSlice* slice,
|
||||
SkRect rect,
|
||||
int64_t view_id,
|
||||
int64_t overlay_id);
|
||||
int64_t overlay_id,
|
||||
MTLPixelFormat pixel_format);
|
||||
// Removes overlay views and platform views that aren't needed in the current frame.
|
||||
// Must run on the platform thread.
|
||||
void RemoveUnusedLayers();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_VIEW_H_
|
||||
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_VIEW_H_
|
||||
|
||||
#include <Metal/Metal.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include <memory>
|
||||
@@ -47,6 +48,7 @@
|
||||
enableWideGamut:(BOOL)isWideGamutEnabled NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
- (UIScreen*)screen;
|
||||
- (MTLPixelFormat)pixelFormat;
|
||||
|
||||
// Set by FlutterEngine or FlutterViewController to override software rendering.
|
||||
@property(class, nonatomic) BOOL forceSoftwareRendering;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"
|
||||
#include <Metal/Metal.h>
|
||||
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/common/task_runners.h"
|
||||
@@ -43,6 +44,17 @@
|
||||
return UIScreen.mainScreen;
|
||||
}
|
||||
|
||||
- (MTLPixelFormat)pixelFormat {
|
||||
if ([self.layer isKindOfClass:NSClassFromString(@"CAMetalLayer")]) {
|
||||
// It is a known Apple bug that CAMetalLayer incorrectly reports its supported
|
||||
// SDKs. It is, in fact, available since iOS 8.
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
|
||||
CAMetalLayer* layer = (CAMetalLayer*)self.layer;
|
||||
return layer.pixelFormat;
|
||||
}
|
||||
return MTLPixelFormatBGRA8Unorm;
|
||||
}
|
||||
- (BOOL)isWideGamutSupported {
|
||||
if (![_delegate isUsingImpeller]) {
|
||||
return NO;
|
||||
|
||||
Reference in New Issue
Block a user