Skip to content

Commit

Permalink
Switch from char to uint8_t to represent memory (flutter#3147)
Browse files Browse the repository at this point in the history
Previously we had a mix of the two.
  • Loading branch information
abarth authored Oct 19, 2016
1 parent 4e68860 commit ae5d521
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 52 deletions.
2 changes: 1 addition & 1 deletion glue/drain_data_pipe_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace glue {

class DrainDataPipeJob {
public:
using ResultCallback = std::function<void(std::vector<char>)>;
using ResultCallback = std::function<void(std::vector<uint8_t>)>;

DrainDataPipeJob(mojo::ScopedDataPipeConsumerHandle handle,
const ResultCallback& callback);
Expand Down
4 changes: 2 additions & 2 deletions glue/drain_data_pipe_job_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class DrainDataPipeJob::JobImpl : public DataPipeDrainer::Client {
private:
// mojo::common::DataPipeDrainer::Client
void OnDataAvailable(const void* data, size_t num_bytes) override {
const char* bytes = static_cast<const char*>(data);
const uint8_t* bytes = static_cast<const uint8_t*>(data);
buffer_.insert(buffer_.end(), bytes, bytes + num_bytes);
}

void OnDataComplete() override { callback_(std::move(buffer_)); }

std::vector<char> buffer_;
std::vector<uint8_t> buffer_;
ResultCallback callback_;
DataPipeDrainer drainer_;

Expand Down
4 changes: 2 additions & 2 deletions glue/drain_data_pipe_job_fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class DrainDataPipeJob::JobImpl : public DataPipeDrainer::Client {
private:
// mojo::common::DataPipeDrainer::Client
void OnDataAvailable(const void* data, size_t num_bytes) override {
const char* bytes = static_cast<const char*>(data);
const uint8_t* bytes = static_cast<const uint8_t*>(data);
buffer_.insert(buffer_.end(), bytes, bytes + num_bytes);
}

void OnDataComplete() override { callback_(std::move(buffer_)); }

std::vector<char> buffer_;
std::vector<uint8_t> buffer_;
ResultCallback callback_;
DataPipeDrainer drainer_;

Expand Down
15 changes: 7 additions & 8 deletions lib/ui/painting/image_decoding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using tonic::ToDart;
namespace blink {
namespace {

sk_sp<SkImage> DecodeImage(std::vector<char> buffer) {
sk_sp<SkImage> DecodeImage(std::vector<uint8_t> buffer) {
TRACE_EVENT0("blink", "DecodeImage");

if (buffer.empty())
Expand Down Expand Up @@ -70,7 +70,7 @@ void InvokeImageCallback(sk_sp<SkImage> image,

void DecodeImageAndInvokeImageCallback(
glue::MovableWrapper<std::unique_ptr<DartPersistentValue>> callback,
std::vector<char> buffer) {
std::vector<uint8_t> buffer) {
sk_sp<SkImage> image = DecodeImage(std::move(buffer));
Threads::UI()->PostTask([callback, image]() mutable {
InvokeImageCallback(image, callback.Unwrap());
Expand Down Expand Up @@ -100,7 +100,7 @@ void DecodeImageFromDataPipe(Dart_NativeArguments args) {
Threads::IO()->PostTask([callback, consumer]() mutable {
glue::DrainDataPipeJob* job = nullptr;
job = new glue::DrainDataPipeJob(
consumer.Unwrap(), [callback, job](std::vector<char> buffer) {
consumer.Unwrap(), [callback, job](std::vector<uint8_t> buffer) {
delete job;
DecodeImageAndInvokeImageCallback(callback, std::move(buffer));
});
Expand All @@ -126,12 +126,11 @@ void DecodeImageFromList(Dart_NativeArguments args) {
auto callback = glue::WrapMovable(std::unique_ptr<DartPersistentValue>(
new DartPersistentValue(tonic::DartState::Current(), callback_handle)));

const char* bytes = reinterpret_cast<const char*>(list.data());
auto buffer = glue::WrapMovable(std::unique_ptr<std::vector<char>>(
new std::vector<char>(bytes, bytes + list.num_elements())));
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(list.data());
std::vector<uint8_t> buffer(bytes, bytes + list.num_elements());

Threads::IO()->PostTask([callback, buffer]() mutable {
DecodeImageAndInvokeImageCallback(callback, std::move(*buffer.Unwrap()));
Threads::IO()->PostTask([ callback, buffer = std::move(buffer) ]() mutable {
DecodeImageAndInvokeImageCallback(callback, std::move(buffer));
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/window/platform_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace blink {

PlatformMessage::PlatformMessage(std::string name,
std::vector<char> data,
std::vector<uint8_t> data,
ftl::RefPtr<PlatformMessageResponse> response)
: name_(std::move(name)),
data_(std::move(data)),
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/window/platform_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ class PlatformMessage : public ftl::RefCountedThreadSafe<PlatformMessage> {

public:
const std::string& name() const { return name_; }
const std::vector<char>& data() const { return data_; }
const std::vector<uint8_t>& data() const { return data_; }

const ftl::RefPtr<PlatformMessageResponse>& response() const {
return response_;
}

private:
PlatformMessage(std::string name,
std::vector<char> data,
std::vector<uint8_t> data,
ftl::RefPtr<PlatformMessageResponse> response);
~PlatformMessage();

std::string name_;
std::vector<char> data_;
std::vector<uint8_t> data_;
ftl::RefPtr<PlatformMessageResponse> response_;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/window/platform_message_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlatformMessageResponse

public:
// Callable on any thread.
virtual void Complete(std::vector<char> data) = 0;
virtual void Complete(std::vector<uint8_t> data) = 0;
// TODO(abarth): You should be able to pass data with the error.
virtual void CompleteWithError() = 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/ui/window/platform_message_response_dart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ PlatformMessageResponseDart::~PlatformMessageResponseDart() {
}
}

void PlatformMessageResponseDart::Complete(std::vector<char> data) {
void PlatformMessageResponseDart::Complete(std::vector<uint8_t> data) {
if (callback_.is_empty())
return;
FTL_DCHECK(!is_complete_);
Expand Down Expand Up @@ -63,7 +63,7 @@ void PlatformMessageResponseDart::Complete(std::vector<char> data) {

void PlatformMessageResponseDart::CompleteWithError() {
// TODO(abarth): We should have a dedicated error pathway.
Complete(std::vector<char>());
Complete(std::vector<uint8_t>());
}

} // namespace blink
2 changes: 1 addition & 1 deletion lib/ui/window/platform_message_response_dart.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PlatformMessageResponseDart : public PlatformMessageResponse {

public:
// Callable on any thread.
void Complete(std::vector<char> data) override;
void Complete(std::vector<uint8_t> data) override;
void CompleteWithError() override;

protected:
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/window/pointer_data_packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace blink {
PointerDataPacket::PointerDataPacket(size_t count)
: data_(count * sizeof(PointerData)) {}

PointerDataPacket::PointerDataPacket(char* data, size_t num_bytes)
PointerDataPacket::PointerDataPacket(uint8_t* data, size_t num_bytes)
: data_(data, data + num_bytes) {}

PointerDataPacket::~PointerDataPacket() = default;
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/window/pointer_data_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ namespace blink {
class PointerDataPacket {
public:
explicit PointerDataPacket(size_t count);
PointerDataPacket(char* data, size_t num_bytes);
PointerDataPacket(uint8_t* data, size_t num_bytes);
~PointerDataPacket();

void SetPointerData(size_t i, const PointerData& data);
const std::vector<char>& data() const { return data_; }
const std::vector<uint8_t>& data() const { return data_; }

private:
std::vector<char> data_;
std::vector<uint8_t> data_;

FTL_DISALLOW_COPY_AND_ASSIGN(PointerDataPacket);
};
Expand Down
13 changes: 7 additions & 6 deletions lib/ui/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using tonic::ToDart;
namespace blink {
namespace {

Dart_Handle ToByteData(const std::vector<char> buffer) {
Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
if (buffer.empty())
return Dart_Null();

Expand Down Expand Up @@ -72,7 +72,7 @@ void SendPlatformMessage(Dart_Handle window,
Dart_Handle callback,
const tonic::DartByteData& data) {
UIDartState* dart_state = UIDartState::Current();
const char* buffer = static_cast<const char*>(data.data());
const uint8_t* buffer = static_cast<const uint8_t*>(data.data());

ftl::RefPtr<PlatformMessageResponse> response;
if (!Dart_IsNull(callback)) {
Expand All @@ -82,7 +82,7 @@ void SendPlatformMessage(Dart_Handle window,

UIDartState::Current()->window()->client()->HandlePlatformMessage(
ftl::MakeRefCounted<PlatformMessage>(
name, std::vector<char>(buffer, buffer + data.length_in_bytes()),
name, std::vector<uint8_t>(buffer, buffer + data.length_in_bytes()),
response));
}

Expand All @@ -93,9 +93,10 @@ void _SendPlatformMessage(Dart_NativeArguments args) {
void RespondToPlatformMessage(Dart_Handle window,
int response_id,
const tonic::DartByteData& data) {
const char* buffer = static_cast<const char*>(data.data());
const uint8_t* buffer = static_cast<const uint8_t*>(data.data());
UIDartState::Current()->window()->CompletePlatformMessageResponse(
response_id, std::vector<char>(buffer, buffer + data.length_in_bytes()));
response_id,
std::vector<uint8_t>(buffer, buffer + data.length_in_bytes()));
}

void _RespondToPlatformMessage(Dart_NativeArguments args) {
Expand Down Expand Up @@ -244,7 +245,7 @@ void Window::OnAppLifecycleStateChanged(sky::AppLifecycleState state) {
}

void Window::CompletePlatformMessageResponse(int response_id,
std::vector<char> data) {
std::vector<uint8_t> data) {
if (!response_id)
return;
auto it = pending_responses_.find(response_id);
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Window {

void OnAppLifecycleStateChanged(sky::AppLifecycleState state);

void CompletePlatformMessageResponse(int response_id, std::vector<char> data);
void CompletePlatformMessageResponse(int response_id,
std::vector<uint8_t> data);

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

Expand Down
6 changes: 3 additions & 3 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ void Engine::RunFromSnapshotStream(
TRACE_EVENT0("flutter", "Engine::RunFromSnapshotStream");
ConfigureRuntime(script_uri);
snapshot_drainer_.reset(new glue::DrainDataPipeJob(
std::move(snapshot), [this](std::vector<char> snapshot) {
std::move(snapshot), [this](std::vector<uint8_t> snapshot) {
FTL_DCHECK(runtime_);
FTL_DCHECK(runtime_->dart_controller());
runtime_->dart_controller()->RunFromSnapshot(
reinterpret_cast<uint8_t*>(snapshot.data()), snapshot.size());
runtime_->dart_controller()->RunFromSnapshot(snapshot.data(),
snapshot.size());
}));
}

Expand Down
22 changes: 12 additions & 10 deletions shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PlatformMessageResponseAndroid : public blink::PlatformMessageResponse {
FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseAndroid);

public:
void Complete(std::vector<char> data) override {
void Complete(std::vector<uint8_t> data) override {
ftl::RefPtr<PlatformMessageResponseAndroid> self(this);
blink::Threads::Platform()->PostTask(
[ self, data = std::move(data) ]() mutable {
Expand All @@ -48,7 +48,7 @@ class PlatformMessageResponseAndroid : public blink::PlatformMessageResponse {
});
}

void CompleteWithError() override { Complete(std::vector<char>()); }
void CompleteWithError() override { Complete(std::vector<uint8_t>()); }

private:
PlatformMessageResponseAndroid(int response_id,
Expand Down Expand Up @@ -151,18 +151,18 @@ void PlatformViewAndroid::DispatchPlatformMessage(JNIEnv* env,
response_id, GetWeakPtr());
}

const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data.data());
PlatformView::DispatchPlatformMessage(
ftl::MakeRefCounted<blink::PlatformMessage>(
std::move(name),
std::vector<char>(data.data(), data.data() + data.size()),
std::move(name), std::vector<uint8_t>(buffer, buffer + data.size()),
std::move(response)));
}

void PlatformViewAndroid::DispatchPointerDataPacket(JNIEnv* env,
jobject obj,
jobject buffer,
jint position) {
char* data = static_cast<char*>(env->GetDirectBufferAddress(buffer));
uint8_t* data = static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));

blink::Threads::UI()->PostTask(ftl::MakeCopyable([
engine = engine_->GetWeakPtr(),
Expand Down Expand Up @@ -190,7 +190,7 @@ void PlatformViewAndroid::InvokePlatformMessageResponseCallback(
pending_responses_.erase(it);
// TODO(abarth): There's an extra copy here.
message_response->Complete(
std::vector<char>(response.data(), response.data() + response.size()));
std::vector<uint8_t>(response.data(), response.data() + response.size()));
}

void PlatformViewAndroid::HandlePlatformMessage(
Expand All @@ -209,7 +209,8 @@ void PlatformViewAndroid::HandlePlatformMessage(
base::StringPiece message_name = message->name();

auto data = message->data();
base::StringPiece message_data(data.data(), data.size());
base::StringPiece message_data(reinterpret_cast<const char*>(data.data()),
data.size());

auto java_message_name =
base::android::ConvertUTF8ToJavaString(env, message_name);
Expand All @@ -225,13 +226,14 @@ void PlatformViewAndroid::HandlePlatformMessage(

void PlatformViewAndroid::HandlePlatformMessageResponse(
int response_id,
std::vector<char> data) {
std::vector<uint8_t> data) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> view = flutter_view_.get(env);
if (view.is_null())
return;

base::StringPiece message_data(data.data(), data.size());
base::StringPiece message_data(reinterpret_cast<const char*>(data.data()),
data.size());
auto java_message_data =
base::android::ConvertUTF8ToJavaString(env, message_data);

Expand Down Expand Up @@ -281,7 +283,7 @@ void PlatformViewAndroid::UpdateSemantics(
num_bytes += node.children.size() * kBytesPerChild;
}

std::vector<char> buffer(num_bytes);
std::vector<uint8_t> buffer(num_bytes);
int32_t* buffer_int32 = reinterpret_cast<int32_t*>(&buffer[0]);
float* buffer_float32 = reinterpret_cast<float*>(&buffer[0]);

Expand Down
3 changes: 2 additions & 1 deletion shell/platform/android/platform_view_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class PlatformViewAndroid : public PlatformView {
void HandlePlatformMessage(
ftl::RefPtr<blink::PlatformMessage> message) override;

void HandlePlatformMessageResponse(int response_id, std::vector<char> data);
void HandlePlatformMessageResponse(int response_id,
std::vector<uint8_t> data);

void RunFromSource(const std::string& main,
const std::string& packages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,11 @@ - (void)sendJSON:(NSDictionary*)message withMessageName:(NSString*)messageName {
NSData* data = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil];
if (!data)
return;
const char* bytes = static_cast<const char*>(data.bytes);
const uint8_t* bytes = static_cast<const uint8_t*>(data.bytes);
_platformView->DispatchPlatformMessage(
ftl::MakeRefCounted<blink::PlatformMessage>(
messageName.UTF8String,
std::vector<char>(bytes, bytes + data.length),
std::vector<uint8_t>(bytes, bytes + data.length),
nullptr));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
namespace shell {
namespace {

std::vector<char> SysNSStringToVector(NSString* string) {
std::vector<uint8_t> SysNSStringToVector(NSString* string) {
if (!string.length)
return std::vector<char>();
const char* utf8 = string.UTF8String;
return std::vector<char>(utf8, utf8 + strlen(utf8));
return std::vector<uint8_t>();
const char* chars = string.UTF8String;
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(chars);
return std::vector<uint8_t>(bytes, bytes + strlen(chars));
}

} // namespace
Expand Down

0 comments on commit ae5d521

Please sign in to comment.