WIP on wiring up the aiks entity renderer.

This commit is contained in:
Chinmay Garde
2021-08-17 23:19:53 -07:00
committed by Dan Field
parent b77f2b8b66
commit 2766e1e97f
27 changed files with 354 additions and 90 deletions

View File

@@ -14,10 +14,10 @@ impeller_component("aiks") {
"paint.h",
"picture.cc",
"picture.h",
"picture_operation.cc",
"picture_operation.h",
"picture_recorder.cc",
"picture_recorder.h",
"picture_renderer.h",
"picture_renderer.mm",
]
public_deps = [

View File

@@ -4,8 +4,9 @@
#include "impeller/aiks/canvas.h"
#include <algorithm>
#include "flutter/fml/logging.h"
#include "impeller/aiks/picture_operation.h"
namespace impeller {
@@ -53,18 +54,35 @@ size_t Canvas::GetSaveCount() const {
return xformation_stack_.size();
}
void Canvas::DrawPath(Path path, Paint paint) {}
void Canvas::DrawPath(Path path, Paint paint) {
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetBackgroundColor(paint.color);
ops_.emplace_back(std::move(entity));
}
void Canvas::SaveLayer(const Paint& paint, std::optional<Rect> bounds) {}
void Canvas::ClipPath(Path path) {}
void Canvas::ClipPath(Path path) {
Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetPath(std::move(path));
entity.SetIsClip(true);
ops_.emplace_back(std::move(entity));
}
void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {}
void Canvas::DrawPicture(std::shared_ptr<Picture> picture) {}
void Canvas::DrawPicture(const Picture& picture) {
std::copy(std::begin(picture.entities), std::end(picture.entities),
std::back_inserter(ops_));
}
std::shared_ptr<Picture> Canvas::EndRecordingAsPicture() {
return std::make_shared<Picture>(std::move(ops_));
Picture Canvas::EndRecordingAsPicture() {
Picture picture;
picture.entities = std::move(ops_);
return picture;
}
} // namespace impeller

View File

@@ -18,7 +18,7 @@
namespace impeller {
class PictureOperation;
class Entity;
class Canvas {
public:
@@ -50,13 +50,13 @@ class Canvas {
void DrawShadow(Path path, Color color, Scalar elevation);
void DrawPicture(std::shared_ptr<Picture> picture);
void DrawPicture(const Picture& picture);
std::shared_ptr<Picture> EndRecordingAsPicture();
Picture EndRecordingAsPicture();
private:
std::stack<Matrix> xformation_stack_;
std::vector<std::unique_ptr<PictureOperation>> ops_;
std::vector<Entity> ops_;
FML_DISALLOW_COPY_AND_ASSIGN(Canvas);
};

View File

@@ -6,12 +6,6 @@
namespace impeller {
Paint::Paint() = default;
Paint::~Paint() = default;
void Paint::SetColor(Color color) {
color_ = std::move(color);
}
//
} // namespace impeller

View File

@@ -10,16 +10,8 @@
namespace impeller {
class Paint {
public:
Paint();
~Paint();
void SetColor(Color color);
private:
Color color_;
struct Paint {
Color color;
};
} // namespace impeller

View File

@@ -4,13 +4,10 @@
#include "impeller/aiks/picture.h"
#include "impeller/aiks/picture_operation.h"
#include "impeller/entity/entity.h"
namespace impeller {
Picture::Picture(std::vector<std::unique_ptr<PictureOperation>> operations)
: ops_(std::move(operations)) {}
Picture::~Picture() = default;
//
} // namespace impeller

View File

@@ -8,21 +8,12 @@
#include <vector>
#include "flutter/fml/macros.h"
#include "impeller/entity/entity.h"
namespace impeller {
class PictureOperation;
class Picture {
public:
Picture(std::vector<std::unique_ptr<PictureOperation>> operations);
~Picture();
private:
std::vector<std::unique_ptr<PictureOperation>> ops_;
FML_DISALLOW_COPY_AND_ASSIGN(Picture);
struct Picture {
std::vector<Entity> entities;
};
} // namespace impeller

View File

@@ -1,13 +0,0 @@
// 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/impeller/aiks/picture_operation.h"
namespace impeller {
PictureOperation::PictureOperation() = default;
PictureOperation::~PictureOperation() = default;
} // namespace impeller

View File

@@ -1,21 +0,0 @@
// 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.
#pragma once
#include "flutter/fml/macros.h"
namespace impeller {
class PictureOperation {
public:
PictureOperation();
~PictureOperation();
private:
FML_DISALLOW_COPY_AND_ASSIGN(PictureOperation);
};
} // namespace impeller

View File

@@ -16,7 +16,7 @@ std::shared_ptr<Canvas> PictureRecorder::GetCanvas() const {
return canvas_;
}
std::shared_ptr<Picture> PictureRecorder::EndRecordingAsPicture() {
Picture PictureRecorder::EndRecordingAsPicture() {
return canvas_->EndRecordingAsPicture();
}

View File

@@ -18,7 +18,7 @@ class PictureRecorder {
std::shared_ptr<Canvas> GetCanvas() const;
std::shared_ptr<Picture> EndRecordingAsPicture();
Picture EndRecordingAsPicture();
private:
std::shared_ptr<Canvas> canvas_;

View File

@@ -0,0 +1,36 @@
// 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.
#pragma once
#include "flutter/fml/macros.h"
#include "impeller/entity/entity_renderer.h"
namespace impeller {
class Surface;
class RenderPass;
class Context;
struct Picture;
class PictureRenderer {
public:
PictureRenderer(std::shared_ptr<Context> context);
~PictureRenderer();
bool IsValid() const;
[[nodiscard]] bool Render(const Surface& surface,
const RenderPass& onscreen_pass,
const Picture& picture);
private:
EntityRenderer entity_renderer_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(PictureRenderer);
};
} // namespace impeller

View File

@@ -0,0 +1,38 @@
// 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 "impeller/aiks/picture_renderer.h"
#include "impeller/aiks/picture.h"
namespace impeller {
PictureRenderer::PictureRenderer(std::shared_ptr<Context> context)
: entity_renderer_(std::move(context)) {
if (!entity_renderer_.IsValid()) {
return;
}
is_valid_ = true;
}
PictureRenderer::~PictureRenderer() = default;
bool PictureRenderer::IsValid() const {
return is_valid_;
}
bool PictureRenderer::Render(const Surface& surface,
const RenderPass& onscreen_pass,
const Picture& picture) {
if (!IsValid()) {
return false;
}
return entity_renderer_.RenderEntities(surface, //
onscreen_pass, //
picture.entities //
);
}
} // namespace impeller

View File

@@ -7,6 +7,7 @@
#include <Metal/Metal.h>
#include <memory>
#include <string>
#include "flutter/fml/macros.h"
#include "impeller/compositor/pipeline_library.h"
@@ -20,7 +21,7 @@ class Allocator;
class Context {
public:
Context(std::string shaders_directory);
Context(std::string shaders_directory, std::string main_library_file_name);
~Context();

View File

@@ -14,7 +14,8 @@
namespace impeller {
Context::Context(std::string shaders_directory)
Context::Context(std::string shaders_directory,
std::string main_library_file_name)
: device_(::MTLCreateSystemDefaultDevice()) {
// Setup device.
if (!device_) {
@@ -36,7 +37,7 @@ Context::Context(std::string shaders_directory)
{
NSError* shader_library_error = nil;
auto shader_library_path =
fml::paths::JoinPaths({shaders_directory, "impeller.metallib"});
fml::paths::JoinPaths({shaders_directory, main_library_file_name});
auto library_exists = fml::IsFile(shader_library_path);

View File

@@ -23,7 +23,7 @@ class Renderer {
using RenderCallback =
std::function<bool(const Surface& surface, RenderPass& pass)>;
Renderer(std::string shaders_directory);
Renderer(std::string shaders_directory, std::string main_library_file);
~Renderer();

View File

@@ -12,9 +12,10 @@ namespace impeller {
constexpr size_t kMaxFramesInFlight = 3u;
Renderer::Renderer(std::string shaders_directory)
Renderer::Renderer(std::string shaders_directory, std::string main_library_file)
: frames_in_flight_sema_(::dispatch_semaphore_create(kMaxFramesInFlight)),
context_(std::make_shared<Context>(std::move(shaders_directory))) {
context_(std::make_shared<Context>(std::move(shaders_directory),
std::move(main_library_file))) {
if (!context_->IsValid()) {
return;
}

View File

@@ -4,12 +4,27 @@
import("//flutter/impeller/tools/impeller.gni")
impeller_shaders("entity_shaders") {
name = "entity"
shaders = [
"path.vert",
"solid_fill.frag",
]
}
impeller_component("entity") {
sources = [
"entity.cc",
"entity.h",
"entity_renderer.h",
"entity_renderer.mm",
"entity_renderer_impl.h",
"entity_renderer_impl.mm",
]
deps = [ ":entity_shaders" ]
public_deps = [
"../compositor",
"../image",

View File

@@ -50,4 +50,34 @@ void Entity::SetPath(Path path) {
path_ = std::move(path);
}
void Entity::SetIsClip(bool is_clip) {
is_clip_ = is_clip;
}
bool Entity::IsClip() const {
return is_clip_;
}
bool Entity::HasStroke() const {
return stroke_size_ > 0.0 && !stroke_color_.IsTransparent();
}
bool Entity::HasRenderableContents() const {
const bool has_empty_path = path_.GetBoundingBox().IsZero();
if (has_empty_path) {
return false;
}
if (HasStroke()) {
return true;
}
if (!background_color_.IsTransparent()) {
return true;
}
return false;
}
} // namespace impeller

View File

@@ -38,6 +38,14 @@ class Entity {
void SetPath(Path path);
void SetIsClip(bool is_clip);
bool IsClip() const;
bool HasStroke() const;
bool HasRenderableContents() const;
private:
Matrix transformation_;
Color background_color_;
@@ -45,8 +53,7 @@ class Entity {
Path path_;
Color stroke_color_;
double stroke_size_ = 1.0;
FML_DISALLOW_COPY_AND_ASSIGN(Entity);
bool is_clip_ = false;
};
} // namespace impeller

View File

@@ -0,0 +1,38 @@
// 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.
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/entity/entity.h"
namespace impeller {
class Surface;
class RenderPass;
class Context;
class EntityRendererImpl;
class EntityRenderer {
public:
EntityRenderer(std::shared_ptr<Context> context);
~EntityRenderer();
bool IsValid() const;
[[nodiscard]] bool RenderEntities(const Surface& surface,
const RenderPass& onscreen_pass,
const std::vector<Entity>& entities) const;
private:
std::unique_ptr<EntityRendererImpl> renderer_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(EntityRenderer);
};
} // namespace impeller

View File

@@ -0,0 +1,42 @@
// 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/impeller/entity/entity_renderer.h"
#include "flutter/fml/trace_event.h"
#include "flutter/impeller/entity/entity_renderer_impl.h"
namespace impeller {
EntityRenderer::EntityRenderer(std::shared_ptr<Context> context)
: renderer_(std::make_unique<EntityRendererImpl>(std::move(context))) {
if (!renderer_->IsValid()) {
return;
}
is_valid_ = true;
}
EntityRenderer::~EntityRenderer() = default;
bool EntityRenderer::IsValid() const {
return is_valid_;
}
bool EntityRenderer::RenderEntities(const Surface& surface,
const RenderPass& onscreen_pass,
const std::vector<Entity>& entities) const {
if (!IsValid()) {
return false;
}
for (const auto& entity : entities) {
if (!renderer_->RenderEntity(surface, onscreen_pass, entity)) {
return false;
}
}
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,40 @@
// 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.
#pragma once
#include <memory>
#include "flutter/fml/macros.h"
#include "impeller/compositor/context.h"
#include "impeller/compositor/render_pass.h"
#include "impeller/compositor/surface.h"
namespace impeller {
class Entity;
// TODO(csg): Only present to hide Objective-C++ interface in the headers. Once
// the backend is separated into its own TU, this can be merged with
// EntityRenderer.
class EntityRendererImpl {
public:
EntityRendererImpl(std::shared_ptr<Context> context);
~EntityRendererImpl();
bool IsValid() const;
[[nodiscard]] bool RenderEntity(const Surface& surface,
const RenderPass& onscreen_pass,
const Entity& entities);
private:
std::shared_ptr<Context> context_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(EntityRendererImpl);
};
} // namespace impeller

View File

@@ -0,0 +1,33 @@
// 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 "impeller/entity/entity_renderer_impl.h"
namespace impeller {
EntityRendererImpl::EntityRendererImpl(std::shared_ptr<Context> context)
: context_(std::move(context)) {
if (!context_ || !context_->IsValid()) {
return;
}
is_valid_ = true;
}
EntityRendererImpl::~EntityRendererImpl() = default;
bool EntityRendererImpl::IsValid() const {
return is_valid_;
}
bool EntityRendererImpl::RenderEntity(const Surface& surface,
const RenderPass& onscreen_pass,
const Entity& entity) {
if (!entity.HasRenderableContents()) {
return true;
}
return true;
}
} // namespace impeller

View File

@@ -0,0 +1,13 @@
// 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.
uniform FrameInfo {
mat4 mvp;
} frame_info;
in vec2 vertices;
void main() {
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
}

View File

@@ -0,0 +1,9 @@
// 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.
out vec4 frag_color;
void main() {
frag_color = vec4(1.0, 1.0, 1.0, 1.0);
}

View File

@@ -638,6 +638,8 @@ struct Color {
1.0 //
};
}
constexpr bool IsTransparent() const { return alpha == 0.0; }
};
/**