Skip to content

Commit 5405b9a

Browse files
chinmaygardednfield
authored andcommitted
Implement texture mapping.
1 parent 778fc11 commit 5405b9a

23 files changed

+434
-39
lines changed

impeller/aiks/BUILD.gn

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ impeller_component("aiks") {
3131

3232
impeller_component("aiks_unittests") {
3333
testonly = true
34-
sources = [ "aiks_unittests.cc" ]
34+
sources = [
35+
"aiks_playground.cc",
36+
"aiks_playground.h",
37+
"aiks_unittests.cc",
38+
]
3539
deps = [
3640
":aiks",
3741
"../geometry:geometry_unittests",
42+
"../playground",
3843
"//flutter/testing",
3944
]
4045
}

impeller/aiks/aiks_playground.cc

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/aiks/aiks_playground.h"
6+
7+
#include "impeller/aiks/picture_renderer.h"
8+
9+
namespace impeller {
10+
11+
AiksPlayground::AiksPlayground() = default;
12+
13+
AiksPlayground::~AiksPlayground() = default;
14+
15+
bool AiksPlayground::OpenPlaygroundHere(const Picture& picture) {
16+
auto renderer = std::make_shared<PictureRenderer>(GetContext());
17+
if (!renderer) {
18+
return false;
19+
}
20+
21+
return Playground::OpenPlaygroundHere(
22+
[renderer, &picture](const Surface& surface, RenderPass& pass) -> bool {
23+
return renderer->Render(surface, pass, picture);
24+
});
25+
}
26+
27+
} // namespace impeller

impeller/aiks/aiks_playground.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "flutter/fml/macros.h"
8+
#include "impeller/aiks/picture.h"
9+
#include "impeller/playground/playground.h"
10+
11+
namespace impeller {
12+
13+
class AiksPlayground : public Playground {
14+
public:
15+
AiksPlayground();
16+
17+
~AiksPlayground();
18+
19+
bool OpenPlaygroundHere(const Picture& picture);
20+
21+
private:
22+
FML_DISALLOW_COPY_AND_ASSIGN(AiksPlayground);
23+
};
24+
25+
} // namespace impeller

impeller/aiks/aiks_unittests.cc

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
// found in the LICENSE file.
44

55
#include "flutter/testing/testing.h"
6+
#include "impeller/aiks/aiks_playground.h"
67
#include "impeller/aiks/canvas.h"
8+
#include "impeller/aiks/image.h"
79
#include "impeller/geometry/geometry_unittests.h"
10+
#include "impeller/geometry/path_builder.h"
811

912
namespace impeller {
1013
namespace testing {
1114

12-
TEST(AiksTest, CanvasCTMCanBeUpdated) {
15+
using AiksTest = AiksPlayground;
16+
17+
TEST_F(AiksTest, CanvasCTMCanBeUpdated) {
1318
Canvas canvas;
1419
Matrix identity;
1520
ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), identity);
@@ -18,7 +23,7 @@ TEST(AiksTest, CanvasCTMCanBeUpdated) {
1823
Matrix::MakeTranslation({100.0, 100.0, 0.0}));
1924
}
2025

21-
TEST(AiksTest, CanvasCanPushPopCTM) {
26+
TEST_F(AiksTest, CanvasCanPushPopCTM) {
2227
Canvas canvas;
2328
ASSERT_EQ(canvas.GetSaveCount(), 1u);
2429
ASSERT_EQ(canvas.Restore(), false);
@@ -34,5 +39,42 @@ TEST(AiksTest, CanvasCanPushPopCTM) {
3439
Matrix::MakeTranslation({100.0, 100.0, 0.0}));
3540
}
3641

42+
TEST_F(AiksTest, CanRenderColoredRect) {
43+
Canvas canvas;
44+
Paint paint;
45+
paint.color = Color::Red();
46+
canvas.DrawPath(PathBuilder{}
47+
.AddRect(Rect::MakeXYWH(100.0, 100.0, 100.0, 100.0))
48+
.CreatePath(),
49+
paint);
50+
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
51+
}
52+
53+
TEST_F(AiksTest, CanRenderImage) {
54+
Canvas canvas;
55+
Paint paint;
56+
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
57+
paint.color = Color::Red();
58+
canvas.DrawImage(image, Point::MakeXY(100.0, 100.0), paint);
59+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
60+
}
61+
62+
TEST_F(AiksTest, CanRenderImageRect) {
63+
Canvas canvas;
64+
Paint paint;
65+
auto image = std::make_shared<Image>(CreateTextureForFixture("kalimba.jpg"));
66+
auto source_rect = IRect::MakeSize(image->GetSize());
67+
68+
// Render the bottom right quarter of the source image in a stretched rect.
69+
source_rect.size.width /= 2;
70+
source_rect.size.height /= 2;
71+
source_rect.origin.x += source_rect.size.width;
72+
source_rect.origin.y += source_rect.size.height;
73+
74+
canvas.DrawImageRect(image, source_rect, Rect::MakeXYWH(100, 100, 600, 600),
75+
paint);
76+
// ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
77+
}
78+
3779
} // namespace testing
3880
} // namespace impeller

impeller/aiks/canvas.cc

+40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <algorithm>
88

99
#include "flutter/fml/logging.h"
10+
#include "impeller/geometry/path_builder.h"
1011

1112
namespace impeller {
1213

@@ -98,6 +99,45 @@ void Canvas::DrawPicture(const Picture& picture) {
9899
}
99100
}
100101

102+
void Canvas::DrawImage(std::shared_ptr<Image> image,
103+
Point offset,
104+
Paint paint) {
105+
if (!image) {
106+
return;
107+
}
108+
109+
const auto source = IRect::MakeSize(image->GetSize());
110+
const auto dest =
111+
Rect::MakeXYWH(offset.x, offset.y, source.size.width, source.size.height);
112+
113+
DrawImageRect(image, source, dest, std::move(paint));
114+
}
115+
116+
void Canvas::DrawImageRect(std::shared_ptr<Image> image,
117+
IRect source,
118+
Rect dest,
119+
Paint paint) {
120+
if (!image || source.size.IsEmpty() || dest.size.IsEmpty()) {
121+
return;
122+
}
123+
124+
auto size = image->GetSize();
125+
126+
if (size.IsEmpty()) {
127+
return;
128+
}
129+
130+
auto contents = std::make_shared<TextureContents>();
131+
contents->SetTexture(image->GetTexture());
132+
contents->SetSourceRect(source);
133+
134+
Entity entity;
135+
entity.SetPath(PathBuilder{}.AddRect(dest).CreatePath());
136+
entity.SetContents(contents);
137+
entity.SetTransformation(GetCurrentTransformation());
138+
GetCurrentPass().PushEntity(std::move(entity));
139+
}
140+
101141
Picture Canvas::EndRecordingAsPicture() {
102142
Picture picture;
103143
picture.passes = std::move(passes_);

impeller/aiks/canvas.h

+9
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
#include "flutter/fml/macros.h"
1313
#include "impeller/aiks/canvas_pass.h"
14+
#include "impeller/aiks/image.h"
1415
#include "impeller/aiks/paint.h"
1516
#include "impeller/aiks/picture.h"
1617
#include "impeller/geometry/matrix.h"
1718
#include "impeller/geometry/path.h"
19+
#include "impeller/geometry/point.h"
1820
#include "impeller/geometry/vector.h"
1921

2022
namespace impeller {
@@ -49,6 +51,13 @@ class Canvas {
4951

5052
void DrawPath(Path path, Paint paint);
5153

54+
void DrawImage(std::shared_ptr<Image> image, Point offset, Paint paint);
55+
56+
void DrawImageRect(std::shared_ptr<Image> image,
57+
IRect source,
58+
Rect dest,
59+
Paint paint);
60+
5261
void ClipPath(Path path);
5362

5463
void DrawShadow(Path path, Color color, Scalar elevation);

impeller/aiks/image.cc

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66

77
namespace impeller {
88

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

1111
Image::~Image() = default;
1212

13+
ISize Image::GetSize() const {
14+
return texture_ ? texture_->GetSize() : ISize{};
15+
}
16+
17+
std::shared_ptr<Texture> Image::GetTexture() const {
18+
return texture_;
19+
}
20+
1321
} // namespace impeller

impeller/aiks/image.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44

55
#pragma once
66

7+
#include <memory>
8+
79
#include "flutter/fml/macros.h"
10+
#include "impeller/renderer/texture.h"
811

912
namespace impeller {
1013

1114
class Image {
1215
public:
13-
Image();
16+
Image(std::shared_ptr<Texture> texture);
1417

1518
~Image();
1619

20+
ISize GetSize() const;
21+
22+
std::shared_ptr<Texture> GetTexture() const;
23+
1724
private:
25+
const std::shared_ptr<Texture> texture_;
26+
1827
FML_DISALLOW_COPY_AND_ASSIGN(Image);
1928
};
2029

impeller/entity/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ impeller_shaders("entity_shaders") {
1212
"shaders/gradient_fill.vert",
1313
"shaders/solid_fill.frag",
1414
"shaders/solid_fill.vert",
15+
"shaders/texture_fill.frag",
16+
"shaders/texture_fill.vert",
1517
]
1618
}
1719

impeller/entity/content_renderer.cc

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ContentRenderer::ContentRenderer(std::shared_ptr<Context> context)
1414

1515
gradient_fill_pipeline_ = std::make_unique<GradientFillPipeline>(*context_);
1616
solid_fill_pipeline_ = std::make_unique<SolidFillPipeline>(*context_);
17+
texture_pipeline_ = std::make_unique<TexturePipeline>(*context_);
1718

1819
is_valid_ = true;
1920
}
@@ -43,4 +44,12 @@ std::shared_ptr<Pipeline> ContentRenderer::GetSolidFillPipeline() const {
4344
return solid_fill_pipeline_->WaitAndGet();
4445
}
4546

47+
std::shared_ptr<Pipeline> ContentRenderer::GetTexturePipeline() const {
48+
if (!IsValid()) {
49+
return nullptr;
50+
}
51+
52+
return texture_pipeline_->WaitAndGet();
53+
}
54+
4655
} // namespace impeller

impeller/entity/content_renderer.h

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "flutter/impeller/entity/gradient_fill.vert.h"
1212
#include "flutter/impeller/entity/solid_fill.frag.h"
1313
#include "flutter/impeller/entity/solid_fill.vert.h"
14+
#include "flutter/impeller/entity/texture_fill.frag.h"
15+
#include "flutter/impeller/entity/texture_fill.vert.h"
1416
#include "impeller/renderer/pipeline.h"
1517

1618
namespace impeller {
@@ -19,6 +21,8 @@ using GradientFillPipeline =
1921
PipelineT<GradientFillVertexShader, GradientFillFragmentShader>;
2022
using SolidFillPipeline =
2123
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
24+
using TexturePipeline =
25+
PipelineT<TextureFillVertexShader, TextureFillFragmentShader>;
2226

2327
class ContentRenderer {
2428
public:
@@ -32,12 +36,15 @@ class ContentRenderer {
3236

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

39+
std::shared_ptr<Pipeline> GetTexturePipeline() const;
40+
3541
std::shared_ptr<Context> GetContext() const;
3642

3743
private:
3844
std::shared_ptr<Context> context_;
3945
std::unique_ptr<GradientFillPipeline> gradient_fill_pipeline_;
4046
std::unique_ptr<SolidFillPipeline> solid_fill_pipeline_;
47+
std::unique_ptr<TexturePipeline> texture_pipeline_;
4148
bool is_valid_ = false;
4249

4350
FML_DISALLOW_COPY_AND_ASSIGN(ContentRenderer);

0 commit comments

Comments
 (0)