Skip to content

Commit

Permalink
Add texture support (eg video, camera) (flutter#4159)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and mravn-google committed Nov 2, 2017
1 parent b41511e commit 9a960f8
Show file tree
Hide file tree
Showing 53 changed files with 967 additions and 18 deletions.
4 changes: 4 additions & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ source_set("flow") {
"layers/picture_layer.h",
"layers/shader_mask_layer.cc",
"layers/shader_mask_layer.h",
"layers/texture_layer.cc",
"layers/texture_layer.h",
"layers/transform_layer.cc",
"layers/transform_layer.h",
"matrix_decomposition.cc",
Expand All @@ -51,6 +53,8 @@ source_set("flow") {
"raster_cache.h",
"raster_cache_key.cc",
"raster_cache_key.h",
"texture.cc",
"texture.h",
]

public_deps = [
Expand Down
5 changes: 5 additions & 0 deletions flow/compositor_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ CompositorContext::ScopedFrame::~ScopedFrame() {
context_.EndFrame(*this, instrumentation_enabled_);
}

void CompositorContext::OnGrContextCreated() {
texture_registry_.OnGrContextCreated();
}

void CompositorContext::OnGrContextDestroyed() {
texture_registry_.OnGrContextDestroyed();
raster_cache_.Clear();
}

Expand Down
6 changes: 6 additions & 0 deletions flow/compositor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "flutter/flow/instrumentation.h"
#include "flutter/flow/process_info.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/texture.h"
#include "lib/fxl/macros.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkPictureRecorder.h"
Expand Down Expand Up @@ -55,10 +56,14 @@ class CompositorContext {
SkCanvas* canvas,
bool instrumentation_enabled = true);

void OnGrContextCreated();

void OnGrContextDestroyed();

RasterCache& raster_cache() { return raster_cache_; }

TextureRegistry& texture_registry() { return texture_registry_; }

const Counter& frame_count() const { return frame_count_; }

const Stopwatch& frame_time() const { return frame_time_; }
Expand All @@ -69,6 +74,7 @@ class CompositorContext {

private:
RasterCache raster_cache_;
TextureRegistry texture_registry_;
std::unique_ptr<ProcessInfo> process_info_;
Counter frame_count_;
Stopwatch frame_time_;
Expand Down
14 changes: 14 additions & 0 deletions flow/layers/default_layer_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "flutter/flow/layers/physical_model_layer.h"
#include "flutter/flow/layers/picture_layer.h"
#include "flutter/flow/layers/shader_mask_layer.h"
#include "flutter/flow/layers/texture_layer.h"
#include "flutter/flow/layers/transform_layer.h"

#if defined(OS_FUCHSIA)
Expand Down Expand Up @@ -151,6 +152,19 @@ void DefaultLayerBuilder::PushPicture(const SkPoint& offset,
current_layer_->Add(std::move(layer));
}

void DefaultLayerBuilder::PushTexture(const SkPoint& offset,
const SkSize& size,
int64_t texture_id) {
if (!current_layer_) {
return;
}
auto layer = std::make_unique<flow::TextureLayer>();
layer->set_offset(offset);
layer->set_size(size);
layer->set_texture_id(texture_id);
current_layer_->Add(std::move(layer));
}

#if defined(OS_FUCHSIA)
void DefaultLayerBuilder::PushChildScene(
const SkPoint& offset,
Expand Down
5 changes: 5 additions & 0 deletions flow/layers/default_layer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class DefaultLayerBuilder final : public LayerBuilder {
bool picture_is_complex,
bool picture_will_change) override;

// |flow::LayerBuilder|
void PushTexture(const SkPoint& offset,
const SkSize& size,
int64_t texture_id) override;

#if defined(OS_FUCHSIA)
// |flow::LayerBuilder|
void PushChildScene(const SkPoint& offset,
Expand Down
2 changes: 2 additions & 0 deletions flow/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "flutter/flow/instrumentation.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/texture.h"
#include "flutter/glue/trace_event.h"
#include "lib/fxl/build_config.h"
#include "lib/fxl/logging.h"
Expand Down Expand Up @@ -56,6 +57,7 @@ class Layer {
const Stopwatch& frame_time;
const Stopwatch& engine_time;
const CounterValues& memory_usage;
TextureRegistry& texture_registry;
const bool checkerboard_offscreen_layers;
};

Expand Down
4 changes: 4 additions & 0 deletions flow/layers/layer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class LayerBuilder {
bool picture_is_complex,
bool picture_will_change) = 0;

virtual void PushTexture(const SkPoint& offset,
const SkSize& size,
int64_t texture_id) = 0;

#if defined(OS_FUCHSIA)
virtual void PushChildScene(
const SkPoint& offset,
Expand Down
4 changes: 3 additions & 1 deletion flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ void LayerTree::UpdateScene(SceneUpdateContext& context,
#endif

void LayerTree::Paint(CompositorContext::ScopedFrame& frame) const {
Layer::PaintContext context = {*frame.canvas(), frame.context().frame_time(),
Layer::PaintContext context = {*frame.canvas(),
frame.context().frame_time(),
frame.context().engine_time(),
frame.context().memory_usage(),
frame.context().texture_registry(),
checkerboard_offscreen_layers_};
TRACE_EVENT0("flutter", "LayerTree::Paint");

Expand Down
29 changes: 29 additions & 0 deletions flow/layers/texture_layer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/flow/layers/texture_layer.h"

#include "flutter/flow/texture.h"

namespace flow {

TextureLayer::TextureLayer() = default;

TextureLayer::~TextureLayer() = default;

void TextureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
set_paint_bounds(SkRect::MakeXYWH(offset_.x(), offset_.y(), size_.width(),
size_.height()));
}

void TextureLayer::Paint(PaintContext& context) const {
std::shared_ptr<Texture> texture =
context.texture_registry.GetTexture(texture_id_);
if (!texture) {
return;
}
texture->Paint(context.canvas, paint_bounds());
}

} // namespace flow
39 changes: 39 additions & 0 deletions flow/layers/texture_layer.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_FLOW_LAYERS_TEXTURE_LAYER_H_
#define FLUTTER_FLOW_LAYERS_TEXTURE_LAYER_H_

#include "flutter/flow/layers/layer.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "third_party/skia/include/gpu/GrContext.h"
#include "third_party/skia/include/gpu/GrTexture.h"
#include "third_party/skia/include/gpu/GrTypes.h"

namespace flow {

class TextureLayer : public Layer {
public:
TextureLayer();
~TextureLayer() override;

void set_offset(const SkPoint& offset) { offset_ = offset; }
void set_size(const SkSize& size) { size_ = size; }
void set_texture_id(int64_t texture_id) { texture_id_ = texture_id; }

void Preroll(PrerollContext* context, const SkMatrix& matrix) override;
void Paint(PaintContext& context) const override;

private:
SkPoint offset_;
SkSize size_;
int64_t texture_id_;

FXL_DISALLOW_COPY_AND_ASSIGN(TextureLayer);
};

} // namespace flow

#endif // FLUTTER_FLOW_LAYERS_TEXTURE_LAYER_H_
45 changes: 45 additions & 0 deletions flow/texture.cc
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.

#include "flutter/flow/texture.h"

namespace flow {

TextureRegistry::TextureRegistry() = default;

TextureRegistry::~TextureRegistry() = default;

void TextureRegistry::RegisterTexture(std::shared_ptr<Texture> texture) {
ASSERT_IS_GPU_THREAD
mapping_[texture->Id()] = texture;
}

void TextureRegistry::UnregisterTexture(int64_t id) {
ASSERT_IS_GPU_THREAD
mapping_.erase(id);
}

void TextureRegistry::OnGrContextCreated() {
ASSERT_IS_GPU_THREAD;
for (auto& it : mapping_) {
it.second->OnGrContextCreated();
}
}

void TextureRegistry::OnGrContextDestroyed() {
ASSERT_IS_GPU_THREAD;
for (auto& it : mapping_) {
it.second->OnGrContextDestroyed();
}
}

std::shared_ptr<Texture> TextureRegistry::GetTexture(int64_t id) {
ASSERT_IS_GPU_THREAD
return mapping_[id];
}

Texture::Texture(int64_t id) : id_(id) {}
Texture::~Texture() = default;

} // namespace flow
68 changes: 68 additions & 0 deletions flow/texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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_FLOW_TEXTURE_H_
#define FLUTTER_FLOW_TEXTURE_H_

#include <map>
#include "flutter/common/threads.h"
#include "lib/fxl/synchronization/waitable_event.h"
#include "third_party/skia/include/core/SkCanvas.h"

namespace flow {

class Texture {
protected:
Texture(int64_t id);

public:
// Called from GPU thread.
virtual ~Texture();

// Called from GPU thread.
virtual void Paint(SkCanvas& canvas, const SkRect& bounds) = 0;

// Called from GPU thread.
virtual void OnGrContextCreated() = 0;

// Called from GPU thread.
virtual void OnGrContextDestroyed() = 0;

int64_t Id() { return id_; }

private:
int64_t id_;

FXL_DISALLOW_COPY_AND_ASSIGN(Texture);
};

class TextureRegistry {
public:
TextureRegistry();
~TextureRegistry();

// Called from GPU thread.
void RegisterTexture(std::shared_ptr<Texture> texture);

// Called from GPU thread.
void UnregisterTexture(int64_t id);

// Called from GPU thread.
std::shared_ptr<Texture> GetTexture(int64_t id);

// Called from GPU thread.
void OnGrContextCreated();

// Called from GPU thread.
void OnGrContextDestroyed();

private:
std::map<int64_t, std::shared_ptr<Texture>> mapping_;

FXL_DISALLOW_COPY_AND_ASSIGN(TextureRegistry);
};

} // namespace flow

#endif // FLUTTER_FLOW_TEXTURE_H_
9 changes: 9 additions & 0 deletions lib/ui/compositing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
}
void _addPicture(double dx, double dy, Picture picture, int hints) native "SceneBuilder_addPicture";

/// Adds a backend texture to the scene.
///
/// The texture is scaled to the given size and rasterized at the given offset.
void addTexture(int textureId, { Offset offset: Offset.zero, double width: 0.0, double height: 0.0 }) {
assert(offset != null, 'Offset argument was null');
_addTexture(offset.dx, offset.dy, width, height, textureId);
}
void _addTexture(double dx, double dy, double width, double height, int textureId) native "SceneBuilder_addTexture";

/// (Fuchsia-only) Adds a scene rendered by another application to the scene
/// for this application.
void addChildScene({
Expand Down
10 changes: 10 additions & 0 deletions lib/ui/compositing/scene_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, SceneBuilder);
V(SceneBuilder, pushPhysicalModel) \
V(SceneBuilder, pop) \
V(SceneBuilder, addPicture) \
V(SceneBuilder, addTexture) \
V(SceneBuilder, addChildScene) \
V(SceneBuilder, addPerformanceOverlay) \
V(SceneBuilder, setRasterizerTracingThreshold) \
Expand Down Expand Up @@ -124,6 +125,15 @@ void SceneBuilder::addPicture(double dx,
);
}

void SceneBuilder::addTexture(double dx,
double dy,
double width,
double height,
int64_t textureId) {
layer_builder_->PushTexture(SkPoint::Make(dx, dy),
SkSize::Make(width, height), textureId);
}

void SceneBuilder::addChildScene(double dx,
double dy,
double width,
Expand Down
8 changes: 8 additions & 0 deletions lib/ui/compositing/scene_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ class SceneBuilder : public fxl::RefCountedThreadSafe<SceneBuilder>,
double right,
double top,
double bottom);

void addPicture(double dx, double dy, Picture* picture, int hints);

void addTexture(double dx,
double dy,
double width,
double height,
int64_t textureId);

void addChildScene(double dx,
double dy,
double width,
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace blink {
class RuntimeDelegate {
public:
virtual std::string DefaultRouteName() = 0;
virtual void ScheduleFrame() = 0;
virtual void ScheduleFrame(bool regenerate_layer_tree = true) = 0;
virtual void Render(std::unique_ptr<flow::LayerTree> layer_tree) = 0;
virtual void UpdateSemantics(std::vector<SemanticsNode> update) = 0;
virtual void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) = 0;
Expand Down
Loading

0 comments on commit 9a960f8

Please sign in to comment.