Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -3613,8 +3613,11 @@ ORIGIN: ../../../flutter/lib/gpu/lib/src/buffer.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/context.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/formats.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/smoketest.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/lib/src/texture.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/smoketest.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/smoketest.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/texture.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/gpu/texture.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/io/dart_io.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/io/dart_io.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/snapshot/snapshot.h + ../../../flutter/LICENSE
Expand Down Expand Up @@ -6389,8 +6392,11 @@ FILE: ../../../flutter/lib/gpu/lib/src/buffer.dart
FILE: ../../../flutter/lib/gpu/lib/src/context.dart
FILE: ../../../flutter/lib/gpu/lib/src/formats.dart
FILE: ../../../flutter/lib/gpu/lib/src/smoketest.dart
FILE: ../../../flutter/lib/gpu/lib/src/texture.dart
FILE: ../../../flutter/lib/gpu/smoketest.cc
FILE: ../../../flutter/lib/gpu/smoketest.h
FILE: ../../../flutter/lib/gpu/texture.cc
FILE: ../../../flutter/lib/gpu/texture.h
FILE: ../../../flutter/lib/io/dart_io.cc
FILE: ../../../flutter/lib/io/dart_io.h
FILE: ../../../flutter/lib/snapshot/libraries_experimental.json
Expand Down
81 changes: 81 additions & 0 deletions impeller/fixtures/dart_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,84 @@ void deviceBufferOverwriteThrowsForNegativeDestinationOffset() {
}
assert(exception!.contains('destinationOffsetInBytes must be positive'));
}

@pragma('vm:entry-point')
void canCreateTexture() {
final gpu.Texture? texture =
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
assert(texture != null);

// Check the defaults.
assert(
texture!.coordinateSystem == gpu.TextureCoordinateSystem.renderToTexture);
assert(texture!.width == 100);
assert(texture!.height == 100);
assert(texture!.storageMode == gpu.StorageMode.hostVisible);
assert(texture!.sampleCount == 1);
assert(texture!.format == gpu.PixelFormat.r8g8b8a8UNormInt);
assert(texture!.enableRenderTargetUsage == true);
assert(texture!.enableShaderReadUsage == true);
assert(texture!.enableShaderWriteUsage == false);
assert(texture!.bytesPerTexel == 4);
assert(texture!.GetBaseMipLevelSizeInBytes() == 40000);
}

@pragma('vm:entry-point')
void canOverwriteTexture() {
final gpu.Texture? texture =
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 2, 2);
assert(texture != null);
final ui.Color red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
final ui.Color green = ui.Color.fromARGB(0xFF, 0, 0xFF, 0);
final bool success = texture!.overwrite(
Int32List.fromList(<int>[red.value, green.value, green.value, red.value])
.buffer
.asByteData());
assert(success);
}

@pragma('vm:entry-point')
void textureOverwriteThrowsForWrongBufferSize() {
final gpu.Texture? texture =
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
assert(texture != null);
final ui.Color red = ui.Color.fromARGB(0xFF, 0xFF, 0, 0);
String? exception;
try {
texture!.overwrite(
Int32List.fromList(<int>[red.value, red.value, red.value, red.value])
.buffer
.asByteData());
} catch (e) {
exception = e.toString();
}
assert(exception!.contains(
'The length of sourceBytes (bytes: 16) must exactly match the size of the base mip level (bytes: 40000)'));
}

@pragma('vm:entry-point')
void textureAsImageReturnsAValidUIImageHandle() {
final gpu.Texture? texture =
gpu.gpuContext.createTexture(gpu.StorageMode.hostVisible, 100, 100);
assert(texture != null);

final ui.Image image = texture!.asImage();
assert(image.width == 100);
assert(image.height == 100);
}

@pragma('vm:entry-point')
void textureAsImageThrowsWhenNotShaderReadable() {
final gpu.Texture? texture = gpu.gpuContext.createTexture(
gpu.StorageMode.hostVisible, 100, 100,
enableShaderReadUsage: false);
assert(texture != null);
String? exception;
try {
texture!.asImage();
} catch (e) {
exception = e.toString();
}
assert(exception!.contains(
'Only shader readable Flutter GPU textures can be used as UI Images'));
}
10 changes: 8 additions & 2 deletions impeller/renderer/renderer_dart_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RendererDartTest : public PlaygroundTest,
flutter::testing::AutoIsolateShutdown* GetIsolate() {
// Sneak the context into the Flutter GPU API.
assert(GetContext() != nullptr);
flutter::Context::SetOverrideContext(GetContext());
flutter::gpu::Context::SetOverrideContext(GetContext());

return isolate_.get();
}
Expand Down Expand Up @@ -128,11 +128,17 @@ TEST_P(RendererDartTest, CanInstantiateFlutterGPUContext) {
/// `flutter/impeller/fixtures/dart_tests.dart`

DART_TEST_CASE(canEmplaceHostBuffer);
DART_TEST_CASE(canCreateDeviceBuffer);

DART_TEST_CASE(canCreateDeviceBuffer);
DART_TEST_CASE(canOverwriteDeviceBuffer);
DART_TEST_CASE(deviceBufferOverwriteFailsWhenOutOfBounds);
DART_TEST_CASE(deviceBufferOverwriteThrowsForNegativeDestinationOffset);

DART_TEST_CASE(canCreateTexture);
DART_TEST_CASE(canOverwriteTexture);
DART_TEST_CASE(textureOverwriteThrowsForWrongBufferSize);
DART_TEST_CASE(textureAsImageReturnsAValidUIImageHandle);
DART_TEST_CASE(textureAsImageThrowsWhenNotShaderReadable);

} // namespace testing
} // namespace impeller
2 changes: 2 additions & 0 deletions lib/gpu/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ source_set("gpu") {
"host_buffer.h",
"smoketest.cc",
"smoketest.h",
"texture.cc",
"texture.h",
]
}
deps = [
Expand Down
25 changes: 23 additions & 2 deletions lib/gpu/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "tonic/converter/dart_converter.h"

namespace flutter {
namespace gpu {

IMPLEMENT_WRAPPERTYPEINFO(gpu, Context);

Expand All @@ -34,6 +35,7 @@ std::shared_ptr<impeller::Context> Context::GetContext() {
return context_;
}

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
Expand All @@ -44,7 +46,7 @@ Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
auto dart_state = flutter::UIDartState::Current();

std::shared_ptr<impeller::Context> impeller_context =
flutter::Context::GetDefaultContext();
flutter::gpu::Context::GetDefaultContext();

if (!impeller_context) {
if (!dart_state->IsImpellerEnabled()) {
Expand All @@ -68,8 +70,27 @@ Dart_Handle InternalFlutterGpu_Context_InitializeDefault(Dart_Handle wrapper) {
if (!impeller_context) {
return tonic::ToDart("Unable to retrieve the Impeller context.");
}
auto res = fml::MakeRefCounted<flutter::Context>(impeller_context);
auto res = fml::MakeRefCounted<flutter::gpu::Context>(impeller_context);
res->AssociateWithDartWrapper(wrapper);

return Dart_Null();
}

///
extern int InternalFlutterGpu_Context_GetDefaultColorFormat(
flutter::gpu::Context* wrapper) {
return static_cast<int>(
wrapper->GetContext()->GetCapabilities()->GetDefaultColorFormat());
}

extern int InternalFlutterGpu_Context_GetDefaultStencilFormat(
flutter::gpu::Context* wrapper) {
return static_cast<int>(
wrapper->GetContext()->GetCapabilities()->GetDefaultStencilFormat());
}

extern int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(
flutter::gpu::Context* wrapper) {
return static_cast<int>(
wrapper->GetContext()->GetCapabilities()->GetDefaultDepthStencilFormat());
}
18 changes: 18 additions & 0 deletions lib/gpu/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "impeller/renderer/context.h"

namespace flutter {
namespace gpu {

class Context : public RefCountedDartWrappable<Context> {
DEFINE_WRAPPERTYPEINFO();
Expand All @@ -36,6 +37,7 @@ class Context : public RefCountedDartWrappable<Context> {
FML_DISALLOW_COPY_AND_ASSIGN(Context);
};

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
Expand All @@ -48,4 +50,20 @@ FLUTTER_GPU_EXPORT
extern Dart_Handle InternalFlutterGpu_Context_InitializeDefault(
Dart_Handle wrapper);

FLUTTER_GPU_EXPORT
extern int InternalFlutterGpu_Context_GetBackendType(
flutter::gpu::Context* wrapper);

FLUTTER_GPU_EXPORT
extern int InternalFlutterGpu_Context_GetDefaultColorFormat(
flutter::gpu::Context* wrapper);

FLUTTER_GPU_EXPORT
extern int InternalFlutterGpu_Context_GetDefaultStencilFormat(
flutter::gpu::Context* wrapper);

FLUTTER_GPU_EXPORT
extern int InternalFlutterGpu_Context_GetDefaultDepthStencilFormat(
flutter::gpu::Context* wrapper);

} // extern "C"
19 changes: 11 additions & 8 deletions lib/gpu/device_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "tonic/converter/dart_converter.h"

namespace flutter {
namespace gpu {

IMPLEMENT_WRAPPERTYPEINFO(gpu, DeviceBuffer);

Expand All @@ -35,16 +36,18 @@ bool DeviceBuffer::Overwrite(const tonic::DartByteData& source_bytes,
return true;
}

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
/// Exports
///

bool InternalFlutterGpu_DeviceBuffer_Initialize(Dart_Handle wrapper,
flutter::Context* gpu_context,
int storage_mode,
int size_in_bytes) {
bool InternalFlutterGpu_DeviceBuffer_Initialize(
Dart_Handle wrapper,
flutter::gpu::Context* gpu_context,
int storage_mode,
int size_in_bytes) {
impeller::DeviceBufferDescriptor desc;
desc.storage_mode = static_cast<impeller::StorageMode>(storage_mode);
desc.size = size_in_bytes;
Expand All @@ -56,15 +59,15 @@ bool InternalFlutterGpu_DeviceBuffer_Initialize(Dart_Handle wrapper,
}

auto res =
fml::MakeRefCounted<flutter::DeviceBuffer>(std::move(device_buffer));
fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
res->AssociateWithDartWrapper(wrapper);

return true;
}

bool InternalFlutterGpu_DeviceBuffer_InitializeWithHostData(
Dart_Handle wrapper,
flutter::Context* gpu_context,
flutter::gpu::Context* gpu_context,
Dart_Handle byte_data) {
auto data = tonic::DartByteData(byte_data);
auto mapping = fml::NonOwnedMapping(reinterpret_cast<uint8_t*>(data.data()),
Expand All @@ -78,14 +81,14 @@ bool InternalFlutterGpu_DeviceBuffer_InitializeWithHostData(
}

auto res =
fml::MakeRefCounted<flutter::DeviceBuffer>(std::move(device_buffer));
fml::MakeRefCounted<flutter::gpu::DeviceBuffer>(std::move(device_buffer));
res->AssociateWithDartWrapper(wrapper);

return true;
}

bool InternalFlutterGpu_DeviceBuffer_Overwrite(
flutter::DeviceBuffer* device_buffer,
flutter::gpu::DeviceBuffer* device_buffer,
Dart_Handle source_byte_data,
int destination_offset_in_bytes) {
return device_buffer->Overwrite(tonic::DartByteData(source_byte_data),
Expand Down
8 changes: 5 additions & 3 deletions lib/gpu/device_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "third_party/tonic/typed_data/dart_byte_data.h"

namespace flutter {
namespace gpu {

class DeviceBuffer : public RefCountedDartWrappable<DeviceBuffer> {
DEFINE_WRAPPERTYPEINFO();
Expand All @@ -30,6 +31,7 @@ class DeviceBuffer : public RefCountedDartWrappable<DeviceBuffer> {
FML_DISALLOW_COPY_AND_ASSIGN(DeviceBuffer);
};

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
Expand All @@ -41,19 +43,19 @@ extern "C" {
FLUTTER_GPU_EXPORT
extern bool InternalFlutterGpu_DeviceBuffer_Initialize(
Dart_Handle wrapper,
flutter::Context* gpu_context,
flutter::gpu::Context* gpu_context,
int storage_mode,
int size_in_bytes);

FLUTTER_GPU_EXPORT
extern bool InternalFlutterGpu_DeviceBuffer_InitializeWithHostData(
Dart_Handle wrapper,
flutter::Context* gpu_context,
flutter::gpu::Context* gpu_context,
Dart_Handle byte_data);

FLUTTER_GPU_EXPORT
extern bool InternalFlutterGpu_DeviceBuffer_Overwrite(
flutter::DeviceBuffer* wrapper,
flutter::gpu::DeviceBuffer* wrapper,
Dart_Handle source_byte_data,
int destination_offset_in_bytes);

Expand Down
9 changes: 6 additions & 3 deletions lib/gpu/host_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "third_party/tonic/typed_data/dart_byte_data.h"

namespace flutter {
namespace gpu {

IMPLEMENT_WRAPPERTYPEINFO(gpu, HostBuffer);

Expand All @@ -23,18 +24,20 @@ size_t HostBuffer::EmplaceBytes(const tonic::DartByteData& byte_data) {
return view.range.offset;
}

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
/// Exports
///

void InternalFlutterGpu_HostBuffer_Initialize(Dart_Handle wrapper) {
auto res = fml::MakeRefCounted<flutter::HostBuffer>();
auto res = fml::MakeRefCounted<flutter::gpu::HostBuffer>();
res->AssociateWithDartWrapper(wrapper);
}

size_t InternalFlutterGpu_HostBuffer_EmplaceBytes(flutter::HostBuffer* wrapper,
Dart_Handle byte_data) {
size_t InternalFlutterGpu_HostBuffer_EmplaceBytes(
flutter::gpu::HostBuffer* wrapper,
Dart_Handle byte_data) {
return wrapper->EmplaceBytes(tonic::DartByteData(byte_data));
}
4 changes: 3 additions & 1 deletion lib/gpu/host_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "third_party/tonic/typed_data/dart_byte_data.h"

namespace flutter {
namespace gpu {

class HostBuffer : public RefCountedDartWrappable<HostBuffer> {
DEFINE_WRAPPERTYPEINFO();
Expand All @@ -28,6 +29,7 @@ class HostBuffer : public RefCountedDartWrappable<HostBuffer> {
FML_DISALLOW_COPY_AND_ASSIGN(HostBuffer);
};

} // namespace gpu
} // namespace flutter

//----------------------------------------------------------------------------
Expand All @@ -41,7 +43,7 @@ extern void InternalFlutterGpu_HostBuffer_Initialize(Dart_Handle wrapper);

FLUTTER_GPU_EXPORT
extern size_t InternalFlutterGpu_HostBuffer_EmplaceBytes(
flutter::HostBuffer* wrapper,
flutter::gpu::HostBuffer* wrapper,
Dart_Handle byte_data);

} // extern "C"
Loading