Skip to content

Commit

Permalink
Implement texture mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent 778fc11 commit 5405b9a
Show file tree
Hide file tree
Showing 23 changed files with 434 additions and 39 deletions.
7 changes: 6 additions & 1 deletion impeller/aiks/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ impeller_component("aiks") {

impeller_component("aiks_unittests") {
testonly = true
sources = [ "aiks_unittests.cc" ]
sources = [
"aiks_playground.cc",
"aiks_playground.h",
"aiks_unittests.cc",
]
deps = [
":aiks",
"../geometry:geometry_unittests",
"../playground",
"//flutter/testing",
]
}
27 changes: 27 additions & 0 deletions impeller/aiks/aiks_playground.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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/aiks_playground.h"

#include "impeller/aiks/picture_renderer.h"

namespace impeller {

AiksPlayground::AiksPlayground() = default;

AiksPlayground::~AiksPlayground() = default;

bool AiksPlayground::OpenPlaygroundHere(const Picture& picture) {
auto renderer = std::make_shared<PictureRenderer>(GetContext());
if (!renderer) {
return false;
}

return Playground::OpenPlaygroundHere(
[renderer, &picture](const Surface& surface, RenderPass& pass) -> bool {
return renderer->Render(surface, pass, picture);
});
}

} // namespace impeller
25 changes: 25 additions & 0 deletions impeller/aiks/aiks_playground.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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/aiks/picture.h"
#include "impeller/playground/playground.h"

namespace impeller {

class AiksPlayground : public Playground {
public:
AiksPlayground();

~AiksPlayground();

bool OpenPlaygroundHere(const Picture& picture);

private:
FML_DISALLOW_COPY_AND_ASSIGN(AiksPlayground);
};

} // namespace impeller
46 changes: 44 additions & 2 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
// found in the LICENSE file.

#include "flutter/testing/testing.h"
#include "impeller/aiks/aiks_playground.h"
#include "impeller/aiks/canvas.h"
#include "impeller/aiks/image.h"
#include "impeller/geometry/geometry_unittests.h"
#include "impeller/geometry/path_builder.h"

namespace impeller {
namespace testing {

TEST(AiksTest, CanvasCTMCanBeUpdated) {
using AiksTest = AiksPlayground;

TEST_F(AiksTest, CanvasCTMCanBeUpdated) {
Canvas canvas;
Matrix identity;
ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), identity);
Expand All @@ -18,7 +23,7 @@ TEST(AiksTest, CanvasCTMCanBeUpdated) {
Matrix::MakeTranslation({100.0, 100.0, 0.0}));
}

TEST(AiksTest, CanvasCanPushPopCTM) {
TEST_F(AiksTest, CanvasCanPushPopCTM) {
Canvas canvas;
ASSERT_EQ(canvas.GetSaveCount(), 1u);
ASSERT_EQ(canvas.Restore(), false);
Expand All @@ -34,5 +39,42 @@ TEST(AiksTest, CanvasCanPushPopCTM) {
Matrix::MakeTranslation({100.0, 100.0, 0.0}));
}

TEST_F(AiksTest, CanRenderColoredRect) {
Canvas canvas;
Paint paint;
paint.color = Color::Red();
canvas.DrawPath(PathBuilder{}
.AddRect(Rect::MakeXYWH(100.0, 100.0, 100.0, 100.0))
.CreatePath(),
paint);
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_F(AiksTest, CanRenderImage) {
Canvas canvas;
Paint paint;
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
paint.color = Color::Red();
canvas.DrawImage(image, Point::MakeXY(100.0, 100.0), paint);
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

TEST_F(AiksTest, CanRenderImageRect) {
Canvas canvas;
Paint paint;
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
auto source_rect = IRect::MakeSize(image->GetSize());

// Render the bottom right quarter of the source image in a stretched rect.
source_rect.size.width /= 2;
source_rect.size.height /= 2;
source_rect.origin.x += source_rect.size.width;
source_rect.origin.y += source_rect.size.height;

canvas.DrawImageRect(image, source_rect, Rect::MakeXYWH(100, 100, 600, 600),
paint);
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}

} // namespace testing
} // namespace impeller
40 changes: 40 additions & 0 deletions impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>

#include "flutter/fml/logging.h"
#include "impeller/geometry/path_builder.h"

namespace impeller {

Expand Down Expand Up @@ -98,6 +99,45 @@ void Canvas::DrawPicture(const Picture& picture) {
}
}

void Canvas::DrawImage(std::shared_ptr<Image> image,
Point offset,
Paint paint) {
if (!image) {
return;
}

const auto source = IRect::MakeSize(image->GetSize());
const auto dest =
Rect::MakeXYWH(offset.x, offset.y, source.size.width, source.size.height);

DrawImageRect(image, source, dest, std::move(paint));
}

void Canvas::DrawImageRect(std::shared_ptr<Image> image,
IRect source,
Rect dest,
Paint paint) {
if (!image || source.size.IsEmpty() || dest.size.IsEmpty()) {
return;
}

auto size = image->GetSize();

if (size.IsEmpty()) {
return;
}

auto contents = std::make_shared<TextureContents>();
contents->SetTexture(image->GetTexture());
contents->SetSourceRect(source);

Entity entity;
entity.SetPath(PathBuilder{}.AddRect(dest).CreatePath());
entity.SetContents(contents);
entity.SetTransformation(GetCurrentTransformation());
GetCurrentPass().PushEntity(std::move(entity));
}

Picture Canvas::EndRecordingAsPicture() {
Picture picture;
picture.passes = std::move(passes_);
Expand Down
9 changes: 9 additions & 0 deletions impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

#include "flutter/fml/macros.h"
#include "impeller/aiks/canvas_pass.h"
#include "impeller/aiks/image.h"
#include "impeller/aiks/paint.h"
#include "impeller/aiks/picture.h"
#include "impeller/geometry/matrix.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/point.h"
#include "impeller/geometry/vector.h"

namespace impeller {
Expand Down Expand Up @@ -49,6 +51,13 @@ class Canvas {

void DrawPath(Path path, Paint paint);

void DrawImage(std::shared_ptr<Image> image, Point offset, Paint paint);

void DrawImageRect(std::shared_ptr<Image> image,
IRect source,
Rect dest,
Paint paint);

void ClipPath(Path path);

void DrawShadow(Path path, Color color, Scalar elevation);
Expand Down
10 changes: 9 additions & 1 deletion impeller/aiks/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@

namespace impeller {

Image::Image() = default;
Image::Image(std::shared_ptr<Texture> texture) : texture_(std::move(texture)) {}

Image::~Image() = default;

ISize Image::GetSize() const {
return texture_ ? texture_->GetSize() : ISize{};
}

std::shared_ptr<Texture> Image::GetTexture() const {
return texture_;
}

} // namespace impeller
11 changes: 10 additions & 1 deletion impeller/aiks/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@

#pragma once

#include <memory>

#include "flutter/fml/macros.h"
#include "impeller/renderer/texture.h"

namespace impeller {

class Image {
public:
Image();
Image(std::shared_ptr<Texture> texture);

~Image();

ISize GetSize() const;

std::shared_ptr<Texture> GetTexture() const;

private:
const std::shared_ptr<Texture> texture_;

FML_DISALLOW_COPY_AND_ASSIGN(Image);
};

Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ impeller_shaders("entity_shaders") {
"shaders/gradient_fill.vert",
"shaders/solid_fill.frag",
"shaders/solid_fill.vert",
"shaders/texture_fill.frag",
"shaders/texture_fill.vert",
]
}

Expand Down
9 changes: 9 additions & 0 deletions impeller/entity/content_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ContentRenderer::ContentRenderer(std::shared_ptr<Context> context)

gradient_fill_pipeline_ = std::make_unique<GradientFillPipeline>(*context_);
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
texture_pipeline_ = std::make_unique<TexturePipeline>(*context_);

is_valid_ = true;
}
Expand Down Expand Up @@ -43,4 +44,12 @@ std::shared_ptr<Pipeline> ContentRenderer::GetSolidFillPipeline() const {
return solid_fill_pipeline_->WaitAndGet();
}

std::shared_ptr<Pipeline> ContentRenderer::GetTexturePipeline() const {
if (!IsValid()) {
return nullptr;
}

return texture_pipeline_->WaitAndGet();
}

} // namespace impeller
7 changes: 7 additions & 0 deletions impeller/entity/content_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "flutter/impeller/entity/gradient_fill.vert.h"
#include "flutter/impeller/entity/solid_fill.frag.h"
#include "flutter/impeller/entity/solid_fill.vert.h"
#include "flutter/impeller/entity/texture_fill.frag.h"
#include "flutter/impeller/entity/texture_fill.vert.h"
#include "impeller/renderer/pipeline.h"

namespace impeller {
Expand All @@ -19,6 +21,8 @@ using GradientFillPipeline =
PipelineT<GradientFillVertexShader, GradientFillFragmentShader>;
using SolidFillPipeline =
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
using TexturePipeline =
PipelineT<TextureFillVertexShader, TextureFillFragmentShader>;

class ContentRenderer {
public:
Expand All @@ -32,12 +36,15 @@ class ContentRenderer {

std::shared_ptr<Pipeline> GetSolidFillPipeline() const;

std::shared_ptr<Pipeline> GetTexturePipeline() const;

std::shared_ptr<Context> GetContext() const;

private:
std::shared_ptr<Context> context_;
std::unique_ptr<GradientFillPipeline> gradient_fill_pipeline_;
std::unique_ptr<SolidFillPipeline> solid_fill_pipeline_;
std::unique_ptr<TexturePipeline> texture_pipeline_;
bool is_valid_ = false;

FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer);
Expand Down
Loading

0 comments on commit 5405b9a

Please sign in to comment.