Skip to content

Commit

Permalink
Allow plugging in a software backend for rendering in the shell. (flu…
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Feb 22, 2017
1 parent 98e0091 commit df9e956
Show file tree
Hide file tree
Showing 29 changed files with 921 additions and 312 deletions.
1 change: 1 addition & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct Settings {
bool enable_dart_profiling = false;
bool use_test_fonts = false;
bool dart_non_checked_mode = false;
bool force_software_rendering = false;
std::string aot_snapshot_path;
std::string aot_vm_snapshot_data_filename;
std::string aot_vm_snapshot_instr_filename;
Expand Down
11 changes: 11 additions & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

source_set("fml") {
public_deps = []

if (is_ios || is_mac) {
public_deps += [ "platform/darwin" ]
}
}
18 changes: 18 additions & 0 deletions fml/platform/darwin/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

source_set("darwin") {
visibility = [ "//flutter/fml" ]

sources = [
"cf_utils.cc",
"cf_utils.h",
]

deps = [
"//lib/ftl",
]

libs = [ "CoreFoundation.framework" ]
}
11 changes: 11 additions & 0 deletions fml/platform/darwin/cf_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2017 The Chromium 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/fml/platform/darwin/cf_utils.h"

namespace fml {

//

} // namespace fml
39 changes: 39 additions & 0 deletions fml/platform/darwin/cf_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 The Chromium 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_FML_PLATFORM_DARWIN_CF_UTILS_H_
#define FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_

#include <CoreFoundation/CoreFoundation.h>
#include "lib/ftl/macros.h"

namespace fml {

template <class T>
class CFRef {
public:
CFRef() : instance_(nullptr) {}

CFRef(T instance) : instance_(instance) {}

~CFRef() {
if (instance_ != nullptr) {
CFRelease(instance_);
}
instance_ = nullptr;
}

operator T() const { return instance_; }

operator bool() const { return instance_ != nullptr; }

private:
T instance_;

FTL_DISALLOW_COPY_AND_ASSIGN(CFRef);
};

} // namespace fml

#endif // FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
3 changes: 3 additions & 0 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ void Shell::InitStandalone(std::string icu_data_path,
settings.trace_startup =
command_line.HasSwitch(FlagForSwitch(Switch::TraceStartup));

settings.force_software_rendering =
command_line.HasSwitch(FlagForSwitch(Switch::ForceSoftwareRendering));

settings.aot_snapshot_path =
command_line.GetSwitchValueASCII(FlagForSwitch(Switch::AotSnapshotPath));
settings.aot_vm_snapshot_data_filename = command_line.GetSwitchValueASCII(
Expand Down
8 changes: 6 additions & 2 deletions shell/common/surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SurfaceFrame::SurfaceFrame(sk_sp<SkSurface> surface,
SurfaceFrame::~SurfaceFrame() {
if (submit_callback_) {
// Dropping without a Submit.
submit_callback_(nullptr);
submit_callback_(*this, nullptr);
}
}

Expand All @@ -35,12 +35,16 @@ SkCanvas* SurfaceFrame::SkiaCanvas() {
return surface_ != nullptr ? surface_->getCanvas() : nullptr;
}

sk_sp<SkSurface> SurfaceFrame::SkiaSurface() const {
return surface_;
}

bool SurfaceFrame::PerformSubmit() {
if (submit_callback_ == nullptr) {
return false;
}

if (submit_callback_(SkiaCanvas())) {
if (submit_callback_(*this, SkiaCanvas())) {
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion shell/common/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace shell {
/// rendering API. A frame may only be sumitted once.
class SurfaceFrame {
public:
using SubmitCallback = std::function<bool(SkCanvas* canvas)>;
using SubmitCallback =
std::function<bool(const SurfaceFrame& surface_frame, SkCanvas* canvas)>;

SurfaceFrame(sk_sp<SkSurface> surface, SubmitCallback submit_callback);

Expand All @@ -27,6 +28,8 @@ class SurfaceFrame {

SkCanvas* SkiaCanvas();

sk_sp<SkSurface> SkiaSurface() const;

private:
bool submitted_;
sk_sp<SkSurface> surface_;
Expand Down
4 changes: 4 additions & 0 deletions shell/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ DEF_SWITCH(DartNonCheckedMode,
"precompiled and checked mode is unsupported. However, this flag "
"may be specified if the user wishes to run in the debug product "
"mode (i.e. with JIT or DBC) with checked mode off.")
DEF_SWITCH(ForceSoftwareRendering,
"force-software-rendering",
"On platforms where it is supported (only iOS simulator currently), "
"the shell will attempt to use the software backend for rendering.")
DEF_SWITCHES_END

void PrintUsage(const std::string& executable_name);
Expand Down
6 changes: 3 additions & 3 deletions shell/gpu/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ source_set("gpu") {
"gpu_rasterizer.h",
"gpu_surface_gl.cc",
"gpu_surface_gl.h",
"gpu_surface_software.cc",
"gpu_surface_software.h",
]

if (shell_enable_vulkan) {
Expand All @@ -31,8 +33,6 @@ source_set("gpu") {
]

if (shell_enable_vulkan) {
deps += [
"//flutter/vulkan",
]
deps += [ "//flutter/vulkan" ]
}
}
3 changes: 2 additions & 1 deletion shell/gpu/gpu_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceGL::AcquireFrame(const SkISize& size) {

auto weak_this = weak_factory_.GetWeakPtr();

SurfaceFrame::SubmitCallback submit_callback = [weak_this](SkCanvas* canvas) {
SurfaceFrame::SubmitCallback submit_callback = [weak_this](
const SurfaceFrame& surface_frame, SkCanvas* canvas) {
return weak_this ? weak_this->PresentSurface(canvas) : false;
};

Expand Down
63 changes: 63 additions & 0 deletions shell/gpu/gpu_surface_software.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2017 The Chromium 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/gpu/gpu_surface_software.h"

#include <memory>
#include "lib/ftl/logging.h"

namespace shell {

GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate)
: delegate_(delegate), weak_factory_(this) {}

GPUSurfaceSoftware::~GPUSurfaceSoftware() = default;

bool GPUSurfaceSoftware::Setup() {
return IsValid();
}

bool GPUSurfaceSoftware::IsValid() {
return delegate_ != nullptr;
}

std::unique_ptr<SurfaceFrame> GPUSurfaceSoftware::AcquireFrame(
const SkISize& size) {
if (!IsValid()) {
return nullptr;
}

auto backing_store = delegate_->AcquireBackingStore(size);

if (backing_store == nullptr) {
return nullptr;
}

if (size != SkISize::Make(backing_store->width(), backing_store->height())) {
return nullptr;
}

SurfaceFrame::SubmitCallback
on_submit = [self = weak_factory_.GetWeakPtr()](
const SurfaceFrame& surface_frame, SkCanvas* canvas)
->bool {
// If the surface itself went away, there is nothing more to do.
if (!self || !self->IsValid() || canvas == nullptr) {
return false;
}

canvas->flush();

return self->delegate_->PresentBackingStore(surface_frame.SkiaSurface());
};

return std::make_unique<SurfaceFrame>(backing_store, on_submit);
}

GrContext* GPUSurfaceSoftware::GetContext() {
// The is no GrContext associated with a software surface.
return nullptr;
}

} // namespace shell
45 changes: 45 additions & 0 deletions shell/gpu/gpu_surface_software.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2017 The Chromium 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_GPU_GPU_SURFACE_SOFTWARE_H_
#define FLUTTER_SHELL_GPU_GPU_SURFACE_SOFTWARE_H_

#include "flutter/shell/common/surface.h"
#include "lib/ftl/macros.h"
#include "lib/ftl/memory/weak_ptr.h"
#include "third_party/skia/include/core/SkSurface.h"

namespace shell {

class GPUSurfaceSoftwareDelegate {
public:
virtual sk_sp<SkSurface> AcquireBackingStore(const SkISize& size) = 0;
virtual bool PresentBackingStore(sk_sp<SkSurface> backing_store) = 0;
};

class GPUSurfaceSoftware : public Surface {
public:
GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate);

~GPUSurfaceSoftware() override;

bool Setup() override;

bool IsValid() override;

std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) override;

GrContext* GetContext() override;

private:
GPUSurfaceSoftwareDelegate* delegate_;

ftl::WeakPtrFactory<GPUSurfaceSoftware> weak_factory_;

FTL_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceSoftware);
};

} // namespace shell

#endif // FLUTTER_SHELL_GPU_GPU_SURFACE_SOFTWARE_H_
1 change: 1 addition & 0 deletions shell/platform/android/android_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_SURFACE_H_

#include <memory>

#include "flutter/shell/common/platform_view.h"
#include "flutter/shell/common/surface.h"
#include "flutter/shell/platform/android/android_native_window.h"
Expand Down
1 change: 1 addition & 0 deletions shell/platform/darwin/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ source_set("common") {
"//dart/runtime:libdart",
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/runtime",
"//flutter/shell/common",
"//flutter/shell/gpu",
Expand Down
9 changes: 9 additions & 0 deletions shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ shared_library("flutter_framework_dylib") {
"framework/Source/platform_message_router.mm",
"framework/Source/vsync_waiter_ios.h",
"framework/Source/vsync_waiter_ios.mm",
"ios_gl_context.h",
"ios_gl_context.mm",
"ios_surface.h",
"ios_surface.mm",
"ios_surface_gl.h",
"ios_surface_gl.mm",
"ios_surface_software.h",
"ios_surface_software.mm",
"platform_view_ios.h",
"platform_view_ios.mm",
]
Expand All @@ -55,6 +63,7 @@ shared_library("flutter_framework_dylib") {
"//base:base",
"//dart/runtime:libdart",
"//flutter/flow",
"//flutter/fml",
"//flutter/glue",
"//flutter/lib/ui",
"//flutter/shell/common",
Expand Down
22 changes: 15 additions & 7 deletions shell/platform/darwin/ios/framework/Source/FlutterView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// found in the LICENSE file.

#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h"

#include "flutter/common/settings.h"
#include "flutter/common/threads.h"
#include "flutter/flow/layers/layer_tree.h"
#include "flutter/shell/common/rasterizer.h"
Expand All @@ -17,19 +19,25 @@ @interface FlutterView ()<UIInputViewAudioFeedback>
@implementation FlutterView

- (void)layoutSubviews {
CGFloat screenScale = [UIScreen mainScreen].scale;
CAEAGLLayer* layer = reinterpret_cast<CAEAGLLayer*>(self.layer);

layer.allowsGroupOpacity = YES;
layer.opaque = YES;
layer.contentsScale = screenScale;
layer.rasterizationScale = screenScale;
if ([self.layer isKindOfClass:[CAEAGLLayer class]]) {
CAEAGLLayer* layer = reinterpret_cast<CAEAGLLayer*>(self.layer);
layer.allowsGroupOpacity = YES;
layer.opaque = YES;
CGFloat screenScale = [UIScreen mainScreen].scale;
layer.contentsScale = screenScale;
layer.rasterizationScale = screenScale;
}

[super layoutSubviews];
}

+ (Class)layerClass {
#if TARGET_IPHONE_SIMULATOR
return blink::Settings::Get().force_software_rendering ? [CALayer class]
: [CAEAGLLayer class];
#else // TARGET_IPHONE_SIMULATOR
return [CAEAGLLayer class];
#endif // TARGET_IPHONE_SIMULATOR
}

- (BOOL)enableInputClicksWhenVisible {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "flutter/common/threads.h"
#include "flutter/shell/gpu/gpu_rasterizer.h"
#include "flutter/shell/gpu/gpu_surface_gl.h"
#include "flutter/shell/platform/darwin/common/platform_mac.h"
#include "flutter/shell/platform/darwin/common/string_conversions.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
Expand Down Expand Up @@ -473,8 +471,7 @@ - (void)surfaceUpdated:(BOOL)appeared {
CHECK(_platformView != nullptr);

if (appeared) {
_platformView->NotifyCreated(
std::make_unique<shell::GPUSurfaceGL>(_platformView.get()));
_platformView->NotifyCreated();
} else {
_platformView->NotifyDestroyed();
}
Expand Down
Loading

0 comments on commit df9e956

Please sign in to comment.