Skip to content

Commit

Permalink
Fix low-res rasterized images on Fuchsia. (flutter#4325)
Browse files Browse the repository at this point in the history
Plumb through Scenic display metrics to use during
Preroll.
  • Loading branch information
mikejurka authored Nov 8, 2017
1 parent d615678 commit 0e8331a
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 15 deletions.
4 changes: 4 additions & 0 deletions content_handler/session_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SessionConnection {

bool has_metrics() const { return scene_update_context_.has_metrics(); }

const scenic::MetricsPtr& metrics() const {
return scene_update_context_.metrics();
}

void set_metrics_changed_callback(fxl::Closure callback) {
metrics_changed_callback_ = std::move(callback);
}
Expand Down
2 changes: 1 addition & 1 deletion content_handler/vulkan_rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void VulkanRasterizer::Draw(std::unique_ptr<flow::LayerTree> layer_tree,
// Preroll the Flutter layer tree. This allows Flutter to perform pre-paint
// optimizations.
TRACE_EVENT0("flutter", "Preroll");
layer_tree->Preroll(frame);
layer_tree->Preroll(frame, session_connection_->metrics().get());
}

{
Expand Down
7 changes: 6 additions & 1 deletion flow/debug_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ std::ostream& operator<<(std::ostream& os, const SkPoint& r) {

std::ostream& operator<<(std::ostream& os, const flow::RasterCacheKey& k) {
os << "Picture: " << k.picture_id() << " Scale: " << k.scale_key().width()
<< ", " << k.scale_key().height();
<< ", " << k.scale_key().height()
#if defined(OS_FUCHSIA)
<< " Metrics scale: (" << k.metrics_scale_x() << ", "
<< k.metrics_scale_y() << ")"
#endif
;
return os;
}
3 changes: 3 additions & 0 deletions flow/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Layer {
virtual ~Layer();

struct PrerollContext {
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics = nullptr;
#endif
RasterCache* raster_cache;
GrContext* gr_context;
SkColorSpace* dst_color_space;
Expand Down
29 changes: 24 additions & 5 deletions flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,42 @@ LayerTree::LayerTree()
LayerTree::~LayerTree() = default;

void LayerTree::Raster(CompositorContext::ScopedFrame& frame,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool ignore_raster_cache) {
Preroll(frame, ignore_raster_cache);
#if defined(OS_FUCHSIA)
FXL_DCHECK(metrics);
#endif
Preroll(frame,
#if defined(OS_FUCHSIA)
metrics,
#endif
ignore_raster_cache);
Paint(frame);
}

void LayerTree::Preroll(CompositorContext::ScopedFrame& frame,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool ignore_raster_cache) {
#if defined(OS_FUCHSIA)
FXL_DCHECK(metrics);
#endif
TRACE_EVENT0("flutter", "LayerTree::Preroll");
SkColorSpace* color_space =
frame.canvas() ? frame.canvas()->imageInfo().colorSpace() : nullptr;
frame.context().raster_cache().SetCheckboardCacheImages(
checkerboard_raster_cache_images_);
Layer::PrerollContext context = {
ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
frame.gr_context(),
color_space,
SkRect::MakeEmpty(),
#if defined(OS_FUCHSIA)
metrics,
#endif
ignore_raster_cache ? nullptr : &frame.context().raster_cache(),
frame.gr_context(),
color_space,
SkRect::MakeEmpty(),
};

root_layer_->Preroll(&context, SkMatrix::I());
Expand Down
9 changes: 9 additions & 0 deletions flow/layers/layer_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include "flutter/flow/layers/layer.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/time/time_delta.h"
#if defined(OS_FUCHSIA)
#include "lib/ui/scenic/fidl/events.fidl.h"
#endif
#include "third_party/skia/include/core/SkSize.h"

namespace flow {
Expand All @@ -25,9 +28,15 @@ class LayerTree {

// Raster includes both Preroll and Paint.
void Raster(CompositorContext::ScopedFrame& frame,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool ignore_raster_cache = false);

void Preroll(CompositorContext::ScopedFrame& frame,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool ignore_raster_cache = false);

#if defined(OS_FUCHSIA)
Expand Down
3 changes: 3 additions & 0 deletions flow/layers/picture_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ void PictureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
if (auto cache = context->raster_cache) {
raster_cache_result_ = cache->GetPrerolledImage(
context->gr_context, picture_.get(), matrix, context->dst_color_space,
#if defined(OS_FUCHSIA)
context->metrics,
#endif
is_complex_, will_change_);
}

Expand Down
33 changes: 28 additions & 5 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ RasterCacheResult RasterizePicture(SkPicture* picture,
GrContext* context,
const MatrixDecomposition& matrix,
SkColorSpace* dst_color_space,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool checkerboard) {
TRACE_EVENT0("flutter", "RasterCachePopulate");

const SkVector3& scale = matrix.scale();

const SkRect logical_rect = picture->cullRect();
const SkRect physical_rect =
SkRect::MakeWH(std::fabs(logical_rect.width() * scale.x()),
std::fabs(logical_rect.height() * scale.y()));

#if defined(OS_FUCHSIA)
float metrics_scale_x = metrics->scale_x;
float metrics_scale_y = metrics->scale_y;
#else
float metrics_scale_x = 1.f;
float metrics_scale_y = 1.f;
#endif

const SkRect physical_rect = SkRect::MakeWH(
std::fabs(logical_rect.width() * metrics_scale_x * scale.x()),
std::fabs(logical_rect.height() * metrics_scale_y * scale.y()));

const SkImageInfo image_info = SkImageInfo::MakeN32Premul(
std::ceil(physical_rect.width()), // physical width
Expand All @@ -100,7 +112,8 @@ RasterCacheResult RasterizePicture(SkPicture* picture,
SkCanvas* canvas = surface->getCanvas();

canvas->clear(SK_ColorTRANSPARENT);
canvas->scale(std::abs(scale.x()), std::abs(scale.y()));
canvas->scale(std::abs(scale.x() * metrics_scale_x),
std::abs(scale.y() * metrics_scale_y));
canvas->translate(-logical_rect.left(), -logical_rect.top());
canvas->drawPicture(picture);

Expand Down Expand Up @@ -132,6 +145,9 @@ RasterCacheResult RasterCache::GetPrerolledImage(
SkPicture* picture,
const SkMatrix& transformation_matrix,
SkColorSpace* dst_color_space,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool is_complex,
bool will_change) {
if (!IsPictureWorthRasterizing(picture, will_change, is_complex)) {
Expand All @@ -148,7 +164,11 @@ RasterCacheResult RasterCache::GetPrerolledImage(
return {};
}

RasterCacheKey cache_key(*picture, matrix);
RasterCacheKey cache_key(*picture,
#if defined(OS_FUCHSIA)
metrics->scale_x, metrics->scale_y,
#endif
matrix);

Entry& entry = cache_[cache_key];
entry.access_count = ClampSize(entry.access_count + 1, 0, threshold_);
Expand All @@ -161,6 +181,9 @@ RasterCacheResult RasterCache::GetPrerolledImage(

if (!entry.image.is_valid()) {
entry.image = RasterizePicture(picture, context, matrix, dst_color_space,
#if defined(OS_FUCHSIA)
metrics,
#endif
checkerboard_images_);
}

Expand Down
6 changes: 6 additions & 0 deletions flow/raster_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "flutter/flow/raster_cache_key.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/weak_ptr.h"
#if defined(OS_FUCHSIA)
#include "lib/ui/scenic/fidl/events.fidl.h"
#endif
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSize.h"

Expand Down Expand Up @@ -54,6 +57,9 @@ class RasterCache {
SkPicture* picture,
const SkMatrix& transformation_matrix,
SkColorSpace* dst_color_space,
#if defined(OS_FUCHSIA)
scenic::Metrics* metrics,
#endif
bool is_complex,
bool will_change);

Expand Down
30 changes: 27 additions & 3 deletions flow/raster_cache_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ namespace flow {

class RasterCacheKey {
public:
RasterCacheKey(const SkPicture& picture, const MatrixDecomposition& matrix)
RasterCacheKey(const SkPicture& picture,
#if defined(OS_FUCHSIA)
float metrics_scale_x,
float metrics_scale_y,
#endif
const MatrixDecomposition& matrix)
: picture_id_(picture.uniqueID()),
scale_key_(SkISize::Make(matrix.scale().x() * 1e3,
matrix.scale().y() * 1e3)) {}
#if defined(OS_FUCHSIA)
metrics_scale_x_(metrics_scale_x),
metrics_scale_y_(metrics_scale_y),
#endif
scale_key_(
SkISize::Make(matrix.scale().x() * 1e3, matrix.scale().y() * 1e3)) {
}

uint32_t picture_id() const { return picture_id_; }

const SkISize& scale_key() const { return scale_key_; }

#if defined(OS_FUCHSIA)
float metrics_scale_x() const { return metrics_scale_x_; }
float metrics_scale_y() const { return metrics_scale_y_; }
#endif

struct Hash {
std::size_t operator()(RasterCacheKey const& key) const {
return key.picture_id_;
Expand All @@ -34,6 +49,11 @@ class RasterCacheKey {
constexpr bool operator()(const RasterCacheKey& lhs,
const RasterCacheKey& rhs) const {
return lhs.picture_id_ == rhs.picture_id_ &&
#if defined(OS_FUCHSIA)
lhs.metrics_scale_x_ == rhs.metrics_scale_x_ &&

lhs.metrics_scale_y_ == rhs.metrics_scale_y_ &&
#endif
lhs.scale_key_ == rhs.scale_key_;
}
};
Expand All @@ -43,6 +63,10 @@ class RasterCacheKey {

private:
uint32_t picture_id_;
#if defined(OS_FUCHSIA)
float metrics_scale_x_;
float metrics_scale_y_;
#endif
SkISize scale_key_;
};

Expand Down
1 change: 1 addition & 0 deletions flow/scene_update_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class SceneUpdateContext {
void set_metrics(scenic::MetricsPtr metrics) {
metrics_ = std::move(metrics);
}
const scenic::MetricsPtr& metrics() const { return metrics_; }

void AddChildScene(ExportNode* export_node,
SkPoint offset,
Expand Down

0 comments on commit 0e8331a

Please sign in to comment.