Skip to content

Commit 25054fb

Browse files
authored
Implement GetAllocationSize for Vertices (flutter#18756)
* Implement GetAllocationSize for Vertices * Reflect vertex buffer size in pictures
1 parent 77617e4 commit 25054fb

File tree

14 files changed

+215
-66
lines changed

14 files changed

+215
-66
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ FILE: ../../../flutter/lib/ui/painting/single_frame_codec.cc
354354
FILE: ../../../flutter/lib/ui/painting/single_frame_codec.h
355355
FILE: ../../../flutter/lib/ui/painting/vertices.cc
356356
FILE: ../../../flutter/lib/ui/painting/vertices.h
357+
FILE: ../../../flutter/lib/ui/painting/vertices_unittests.cc
357358
FILE: ../../../flutter/lib/ui/plugins.dart
358359
FILE: ../../../flutter/lib/ui/plugins/callback_cache.cc
359360
FILE: ../../../flutter/lib/ui/plugins/callback_cache.h

lib/ui/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,15 @@ if (current_toolchain == host_toolchain) {
164164
"painting/image_decoder_test.cc",
165165
"painting/image_decoder_test.h",
166166
"painting/image_decoder_unittests.cc",
167+
"painting/vertices_unittests.cc",
167168
"window/pointer_data_packet_converter_unittests.cc",
168169
]
169170

170171
deps = [
171172
":ui",
172173
":ui_unittests_fixtures",
173174
"//flutter/common",
175+
"//flutter/shell/common:shell_test_fixture_sources",
174176
"//flutter/testing",
175177
"//flutter/testing:dart",
176178
"//flutter/testing:opengl",

lib/ui/fixtures/ui_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
33
// Use of this source code is governed by a BSD-style license that can be
44
// found in the LICENSE file.
55

6+
import 'dart:typed_data';
67
import 'dart:ui';
78

89
void main() {}
910

11+
@pragma('vm:entry-point')
12+
void createVertices() {
13+
const int uint16max = 65535;
14+
15+
final Int32List colors = Int32List(uint16max);
16+
final Float32List coords = Float32List(uint16max * 2);
17+
final Uint16List indices = Uint16List(uint16max);
18+
final Float32List positions = Float32List(uint16max * 2);
19+
colors[0] = const Color(0xFFFF0000).value;
20+
colors[1] = const Color(0xFF00FF00).value;
21+
colors[2] = const Color(0xFF0000FF).value;
22+
colors[3] = const Color(0xFF00FFFF).value;
23+
indices[1] = indices[3] = 1;
24+
indices[2] = indices[5] = 3;
25+
indices[4] = 2;
26+
positions[2] = positions[4] = positions[5] = positions[7] = 250.0;
27+
28+
final Vertices vertices = Vertices.raw(
29+
VertexMode.triangles,
30+
positions,
31+
textureCoordinates: coords,
32+
colors: colors,
33+
indices: indices,
34+
);
35+
_validateVertices(vertices);
36+
}
37+
void _validateVertices(Vertices vertices) native 'ValidateVertices';
38+
1039
@pragma('vm:entry-point')
1140
void frameCallback(FrameInfo info) {
1241
print('called back');

lib/ui/painting.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,8 +3249,7 @@ class Vertices extends NativeFieldWrapperClass2 {
32493249
? Uint16List.fromList(indices)
32503250
: null;
32513251

3252-
_constructor();
3253-
if (!_init(mode.index, encodedPositions, encodedTextureCoordinates, encodedColors, encodedIndices))
3252+
if (!_init(this, mode.index, encodedPositions, encodedTextureCoordinates, encodedColors, encodedIndices))
32543253
throw ArgumentError('Invalid configuration for vertices.');
32553254
}
32563255

@@ -3287,14 +3286,12 @@ class Vertices extends NativeFieldWrapperClass2 {
32873286
if (indices != null && indices.any((int i) => i < 0 || i >= positions.length))
32883287
throw ArgumentError('"indices" values must be valid indices in the positions list.');
32893288

3290-
_constructor();
3291-
if (!_init(mode.index, positions, textureCoordinates, colors, indices))
3289+
if (!_init(this, mode.index, positions, textureCoordinates, colors, indices))
32923290
throw ArgumentError('Invalid configuration for vertices.');
32933291
}
32943292

3295-
void _constructor() native 'Vertices_constructor';
3296-
3297-
bool _init(int mode,
3293+
bool _init(Vertices outVertices,
3294+
int mode,
32983295
Float32List positions,
32993296
Float32List textureCoordinates,
33003297
Int32List colors,

lib/ui/painting/canvas.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ void Canvas::drawImage(const CanvasImage* image,
293293
if (!image)
294294
Dart_ThrowException(
295295
ToDart("Canvas.drawImage called with non-genuine Image."));
296-
image_allocation_size_ += image->GetAllocationSize();
296+
external_allocation_size_ += image->GetAllocationSize();
297297
canvas_->drawImage(image->image(), x, y, paint.paint());
298298
}
299299

@@ -315,7 +315,7 @@ void Canvas::drawImageRect(const CanvasImage* image,
315315
ToDart("Canvas.drawImageRect called with non-genuine Image."));
316316
SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
317317
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
318-
image_allocation_size_ += image->GetAllocationSize();
318+
external_allocation_size_ += image->GetAllocationSize();
319319
canvas_->drawImageRect(image->image(), src, dst, paint.paint(),
320320
SkCanvas::kFast_SrcRectConstraint);
321321
}
@@ -341,7 +341,7 @@ void Canvas::drawImageNine(const CanvasImage* image,
341341
SkIRect icenter;
342342
center.round(&icenter);
343343
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
344-
image_allocation_size_ += image->GetAllocationSize();
344+
external_allocation_size_ += image->GetAllocationSize();
345345
canvas_->drawImageNine(image->image(), icenter, dst, paint.paint());
346346
}
347347

@@ -351,7 +351,7 @@ void Canvas::drawPicture(Picture* picture) {
351351
if (!picture)
352352
Dart_ThrowException(
353353
ToDart("Canvas.drawPicture called with non-genuine Picture."));
354-
image_allocation_size_ += picture->GetAllocationSize();
354+
external_allocation_size_ += picture->GetAllocationSize();
355355
canvas_->drawPicture(picture->picture().get());
356356
}
357357

@@ -380,7 +380,7 @@ void Canvas::drawVertices(const Vertices* vertices,
380380
if (!vertices)
381381
Dart_ThrowException(
382382
ToDart("Canvas.drawVertices called with non-genuine Vertices."));
383-
383+
external_allocation_size_ += vertices->GetAllocationSize();
384384
canvas_->drawVertices(vertices->vertices(), blend_mode, *paint.paint());
385385
}
386386

@@ -406,7 +406,7 @@ void Canvas::drawAtlas(const Paint& paint,
406406
static_assert(sizeof(SkRect) == sizeof(float) * 4,
407407
"SkRect doesn't use floats.");
408408

409-
image_allocation_size_ += atlas->GetAllocationSize();
409+
external_allocation_size_ += atlas->GetAllocationSize();
410410
canvas_->drawAtlas(
411411
skImage.get(), reinterpret_cast<const SkRSXform*>(transforms.data()),
412412
reinterpret_cast<const SkRect*>(rects.data()),

lib/ui/painting/canvas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
170170

171171
static void RegisterNatives(tonic::DartLibraryNatives* natives);
172172

173-
size_t image_allocation_size() const { return image_allocation_size_; }
173+
size_t external_allocation_size() const { return external_allocation_size_; }
174174

175175
private:
176176
explicit Canvas(SkCanvas* canvas);
@@ -179,7 +179,7 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
179179
// which does not transfer ownership. For this reason, we hold a raw
180180
// pointer and manually set to null in Clear.
181181
SkCanvas* canvas_;
182-
size_t image_allocation_size_ = 0;
182+
size_t external_allocation_size_ = 0;
183183
};
184184

185185
} // namespace flutter

lib/ui/painting/picture.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ DART_BIND_ALL(Picture, FOR_EACH_BINDING)
2828

2929
fml::RefPtr<Picture> Picture::Create(Dart_Handle dart_handle,
3030
flutter::SkiaGPUObject<SkPicture> picture,
31-
size_t image_allocation_size) {
32-
auto canvas_picture =
33-
fml::MakeRefCounted<Picture>(std::move(picture), image_allocation_size);
31+
size_t external_allocation_size) {
32+
auto canvas_picture = fml::MakeRefCounted<Picture>(std::move(picture),
33+
external_allocation_size);
3434

3535
canvas_picture->AssociateWithDartWrapper(dart_handle);
3636
return canvas_picture;
3737
}
3838

3939
Picture::Picture(flutter::SkiaGPUObject<SkPicture> picture,
40-
size_t image_allocation_size)
40+
size_t external_allocation_size)
4141
: picture_(std::move(picture)),
42-
image_allocation_size_(image_allocation_size) {}
42+
external_allocation_size_(external_allocation_size) {}
4343

4444
Picture::~Picture() = default;
4545

@@ -61,7 +61,7 @@ void Picture::dispose() {
6161
size_t Picture::GetAllocationSize() const {
6262
if (auto picture = picture_.get()) {
6363
return picture->approximateBytesUsed() + sizeof(Picture) +
64-
image_allocation_size_;
64+
external_allocation_size_;
6565
} else {
6666
return sizeof(Picture);
6767
}

lib/ui/painting/picture.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Picture : public RefCountedDartWrappable<Picture> {
2626
~Picture() override;
2727
static fml::RefPtr<Picture> Create(Dart_Handle dart_handle,
2828
flutter::SkiaGPUObject<SkPicture> picture,
29-
size_t image_allocation_size);
29+
size_t external_allocation_size);
3030

3131
sk_sp<SkPicture> picture() const { return picture_.get(); }
3232

@@ -45,14 +45,14 @@ class Picture : public RefCountedDartWrappable<Picture> {
4545
uint32_t height,
4646
Dart_Handle raw_image_callback);
4747

48-
size_t image_allocation_size() const { return image_allocation_size_; }
48+
size_t external_allocation_size() const { return external_allocation_size_; }
4949

5050
private:
5151
Picture(flutter::SkiaGPUObject<SkPicture> picture,
52-
size_t image_allocation_size_);
52+
size_t external_allocation_size_);
5353

5454
flutter::SkiaGPUObject<SkPicture> picture_;
55-
size_t image_allocation_size_;
55+
size_t external_allocation_size_;
5656
};
5757

5858
} // namespace flutter

lib/ui/painting/picture_recorder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fml::RefPtr<Picture> PictureRecorder::endRecording(Dart_Handle dart_picture) {
5656
Picture::Create(dart_picture,
5757
UIDartState::CreateGPUObject(
5858
picture_recorder_.finishRecordingAsPicture()),
59-
canvas_->image_allocation_size());
59+
canvas_->external_allocation_size());
6060

6161
canvas_->Clear();
6262
canvas_->ClearDartWrapper();

lib/ui/painting/vertices.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ void DecodeInts(const tonic::Int32List& ints, T* out) {
2727

2828
} // namespace
2929

30-
static void Vertices_constructor(Dart_NativeArguments args) {
31-
UIDartState::ThrowIfUIOperationsProhibited();
32-
DartCallConstructor(&Vertices::Create, args);
33-
}
34-
3530
IMPLEMENT_WRAPPERTYPEINFO(ui, Vertices);
3631

3732
#define FOR_EACH_BINDING(V) V(Vertices, init)
@@ -43,19 +38,16 @@ Vertices::Vertices() {}
4338
Vertices::~Vertices() {}
4439

4540
void Vertices::RegisterNatives(tonic::DartLibraryNatives* natives) {
46-
natives->Register({{"Vertices_constructor", Vertices_constructor, 1, true},
47-
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
48-
}
49-
50-
fml::RefPtr<Vertices> Vertices::Create() {
51-
return fml::MakeRefCounted<Vertices>();
41+
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
5242
}
5343

54-
bool Vertices::init(SkVertices::VertexMode vertex_mode,
44+
bool Vertices::init(Dart_Handle vertices_handle,
45+
SkVertices::VertexMode vertex_mode,
5546
const tonic::Float32List& positions,
5647
const tonic::Float32List& texture_coordinates,
5748
const tonic::Int32List& colors,
5849
const tonic::Uint16List& indices) {
50+
UIDartState::ThrowIfUIOperationsProhibited();
5951
uint32_t builderFlags = 0;
6052
if (texture_coordinates.data())
6153
builderFlags |= SkVertices::kHasTexCoords_BuilderFlag;
@@ -89,9 +81,15 @@ bool Vertices::init(SkVertices::VertexMode vertex_mode,
8981
builder.indices());
9082
}
9183

92-
vertices_ = builder.detach();
84+
auto vertices = fml::MakeRefCounted<Vertices>();
85+
vertices->vertices_ = builder.detach();
86+
vertices->AssociateWithDartWrapper(vertices_handle);
9387

9488
return true;
9589
}
9690

91+
size_t Vertices::GetAllocationSize() const {
92+
return sizeof(Vertices) + vertices_->approximateSize();
93+
}
94+
9795
} // namespace flutter

0 commit comments

Comments
 (0)