Update paths to account for buildroot
This commit is contained in:
2
DEPS
2
DEPS
@@ -43,6 +43,8 @@ allowed_hosts = [
|
||||
]
|
||||
|
||||
deps = {
|
||||
'src': 'https://github.com/flutter/buildroot.git' + '@' + '9a9f454f5f454c6c01ee3814daa151b0fd49df85',
|
||||
|
||||
# Fuchsia compatibility
|
||||
#
|
||||
# The dependencies in this section should match the layout in the Fuchsia gn
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
# found in the LICENSE file.
|
||||
|
||||
# This target will be built if no target is specified when invoking ninja.
|
||||
group("default") {
|
||||
group("flutter") {
|
||||
testonly = true
|
||||
deps = [
|
||||
"//sky",
|
||||
"//flutter/sky",
|
||||
]
|
||||
|
||||
if (is_ios) {
|
||||
deps += [
|
||||
"//sky/services/dynamic:sdk_lib_archive",
|
||||
"//sky/shell:flutter_framework",
|
||||
"//flutter/services/dynamic:sdk_lib_archive",
|
||||
"//flutter/sky/shell:flutter_framework",
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,6 @@ group("dist") {
|
||||
testonly = true
|
||||
|
||||
deps = [
|
||||
"//sky/dist",
|
||||
"//flutter/sky/dist",
|
||||
]
|
||||
}
|
||||
|
||||
33
engine/src/flutter/assets/BUILD.gn
Normal file
33
engine/src/flutter/assets/BUILD.gn
Normal file
@@ -0,0 +1,33 @@
|
||||
# Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
import("//mojo/public/mojo_application.gni")
|
||||
|
||||
source_set("assets") {
|
||||
sources = [
|
||||
"directory_asset_bundle.cc",
|
||||
"directory_asset_bundle.h",
|
||||
"unique_unzipper.cc",
|
||||
"unique_unzipper.h",
|
||||
"unzip_job.cc",
|
||||
"unzip_job.h",
|
||||
"zip_asset_bundle.cc",
|
||||
"zip_asset_bundle.h",
|
||||
"zip_asset_store.cc",
|
||||
"zip_asset_store.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//flutter/glue",
|
||||
"//lib/ftl",
|
||||
"//mojo/public/cpp/bindings:callback",
|
||||
"//mojo/public/cpp/system",
|
||||
"//mojo/public/cpp/bindings:utility",
|
||||
"//third_party/zlib:minizip",
|
||||
]
|
||||
|
||||
public_deps = [
|
||||
"//mojo/services/asset_bundle/interfaces",
|
||||
]
|
||||
}
|
||||
51
engine/src/flutter/assets/directory_asset_bundle.cc
Normal file
51
engine/src/flutter/assets/directory_asset_bundle.cc
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/assets/directory_asset_bundle.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "flutter/glue/data_pipe_utils.h"
|
||||
#include "lib/ftl/files/eintr_wrapper.h"
|
||||
#include "lib/ftl/files/path.h"
|
||||
#include "lib/ftl/files/unique_fd.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
void DirectoryAssetBundle::GetAsStream(
|
||||
const mojo::String& asset_name,
|
||||
const mojo::Callback<void(mojo::ScopedDataPipeConsumerHandle)>& callback) {
|
||||
mojo::DataPipe pipe;
|
||||
callback.Run(std::move(pipe.consumer_handle));
|
||||
|
||||
std::string asset_path =
|
||||
files::SimplifyPath(directory_ + "/" + asset_name.get());
|
||||
if (asset_path.find(directory_) != 0u) {
|
||||
FTL_LOG(ERROR) << "Asset name '" << asset_name
|
||||
<< "' attempted to traverse outside asset bundle.";
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO(abarth): Consider moving the |open| call to task_runner_.
|
||||
ftl::UniqueFD fd(HANDLE_EINTR(open(asset_path.c_str(), O_RDONLY)));
|
||||
if (fd.get() < 0)
|
||||
return;
|
||||
glue::CopyFromFileDescriptor(std::move(fd), std::move(pipe.producer_handle),
|
||||
task_runner_.get(), [](bool ignored) {});
|
||||
}
|
||||
|
||||
DirectoryAssetBundle::~DirectoryAssetBundle() {}
|
||||
|
||||
DirectoryAssetBundle::DirectoryAssetBundle(
|
||||
mojo::InterfaceRequest<AssetBundle> request,
|
||||
std::string directory,
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner)
|
||||
: binding_(this, std::move(request)),
|
||||
directory_(std::move(directory)),
|
||||
task_runner_(std::move(task_runner)) {}
|
||||
|
||||
} // namespace blink
|
||||
42
engine/src/flutter/assets/directory_asset_bundle.h
Normal file
42
engine/src/flutter/assets/directory_asset_bundle.h
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_DIRECTORY_ASSET_BUNDLE_H_
|
||||
#define FLUTTER_ASSETS_DIRECTORY_ASSET_BUNDLE_H_
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/ftl/tasks/task_runner.h"
|
||||
#include "mojo/public/cpp/bindings/interface_request.h"
|
||||
#include "mojo/public/cpp/bindings/strong_binding.h"
|
||||
#include "mojo/public/cpp/environment/async_waiter.h"
|
||||
#include "mojo/services/asset_bundle/interfaces/asset_bundle.mojom.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
// A mojo service that serves assets out of a directory.
|
||||
class DirectoryAssetBundle : public mojo::asset_bundle::AssetBundle {
|
||||
public:
|
||||
DirectoryAssetBundle(mojo::InterfaceRequest<AssetBundle> request,
|
||||
std::string directory,
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner);
|
||||
|
||||
// mojo::assert_bundle::AssetBundle implementation:
|
||||
void GetAsStream(
|
||||
const mojo::String& asset_name,
|
||||
const mojo::Callback<void(mojo::ScopedDataPipeConsumerHandle)>& callback)
|
||||
override;
|
||||
|
||||
private:
|
||||
~DirectoryAssetBundle() override;
|
||||
|
||||
mojo::StrongBinding<mojo::asset_bundle::AssetBundle> binding_;
|
||||
const std::string directory_;
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner_;
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetBundle);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_DIRECTORY_ASSET_BUNDLE_H_
|
||||
15
engine/src/flutter/assets/unique_unzipper.cc
Normal file
15
engine/src/flutter/assets/unique_unzipper.cc
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/assets/unique_unzipper.h"
|
||||
|
||||
#include "third_party/zlib/contrib/minizip/unzip.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
void UniqueUnzipperTraits::Free(void* file) {
|
||||
unzClose(file);
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
22
engine/src/flutter/assets/unique_unzipper.h
Normal file
22
engine/src/flutter/assets/unique_unzipper.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_UNIQUE_UNZIPPER_H_
|
||||
#define FLUTTER_ASSETS_UNIQUE_UNZIPPER_H_
|
||||
|
||||
#include "lib/ftl/memory/unique_object.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
struct UniqueUnzipperTraits {
|
||||
static inline void* InvalidValue() { return nullptr; }
|
||||
static inline bool IsValid(void* value) { return value != InvalidValue(); }
|
||||
static void Free(void* file);
|
||||
};
|
||||
|
||||
using UniqueUnzipper = ftl::UniqueObject<void*, UniqueUnzipperTraits>;
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_UNIQUE_UNZIPPER_H_
|
||||
97
engine/src/flutter/assets/unzip_job.cc
Normal file
97
engine/src/flutter/assets/unzip_job.cc
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/assets/unzip_job.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "mojo/public/cpp/environment/environment.h"
|
||||
#include "third_party/zlib/contrib/minizip/unzip.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
UnzipJob::UnzipJob(std::string zip_path,
|
||||
std::string asset_name,
|
||||
mojo::ScopedDataPipeProducerHandle producer,
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner)
|
||||
: zip_path_(std::move(zip_path)),
|
||||
asset_name_(std::move(asset_name)),
|
||||
producer_(std::move(producer)),
|
||||
task_runner_(std::move(task_runner)),
|
||||
waiter_(mojo::Environment::GetDefaultAsyncWaiter()),
|
||||
wait_id_(0) {
|
||||
task_runner_->PostTask([this]() { Start(); });
|
||||
}
|
||||
|
||||
UnzipJob::~UnzipJob() {}
|
||||
|
||||
void UnzipJob::Start() {
|
||||
zip_file_.reset(unzOpen2(zip_path_.c_str(), nullptr));
|
||||
|
||||
if (!zip_file_.is_valid()) {
|
||||
FTL_LOG(ERROR) << "Unable to open ZIP file: " << zip_path_;
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
int result = unzLocateFile(zip_file_.get(), asset_name_.c_str(), 0);
|
||||
if (result != UNZ_OK) {
|
||||
FTL_LOG(WARNING) << "Requested asset '" << asset_name_
|
||||
<< "' does not exist.";
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
result = unzOpenCurrentFile(zip_file_.get());
|
||||
if (result != UNZ_OK) {
|
||||
FTL_LOG(WARNING) << "unzOpenCurrentFile failed, error=" << result;
|
||||
delete this;
|
||||
return;
|
||||
}
|
||||
|
||||
OnHandleReady(MOJO_RESULT_OK);
|
||||
}
|
||||
|
||||
void UnzipJob::OnHandleReady(MojoResult result) {
|
||||
if (result == MOJO_RESULT_OK) {
|
||||
void* buffer = nullptr;
|
||||
uint32_t size = 0;
|
||||
result = mojo::BeginWriteDataRaw(producer_.get(), &buffer, &size,
|
||||
MOJO_WRITE_DATA_FLAG_NONE);
|
||||
if (result == MOJO_RESULT_OK) {
|
||||
FTL_DCHECK(size < static_cast<uint32_t>(std::numeric_limits<int>::max()));
|
||||
ssize_t bytes_read = unzReadCurrentFile(zip_file_.get(), buffer, size);
|
||||
result = mojo::EndWriteDataRaw(producer_.get(),
|
||||
std::max<ssize_t>(0l, bytes_read));
|
||||
if (bytes_read < 0) {
|
||||
FTL_LOG(WARNING) << "Asset unzip failed, error=" << bytes_read;
|
||||
delete this;
|
||||
} else if (result != MOJO_RESULT_OK) {
|
||||
FTL_LOG(WARNING) << "Mojo EndWrite failed, error=" << result;
|
||||
delete this;
|
||||
} else if (bytes_read < size) {
|
||||
// Reached EOF. Stop the process.
|
||||
delete this;
|
||||
} else {
|
||||
task_runner_->PostTask([this]() { OnHandleReady(MOJO_RESULT_OK); });
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (result == MOJO_RESULT_SHOULD_WAIT) {
|
||||
wait_id_ =
|
||||
waiter_->AsyncWait(producer_.get().value(), MOJO_HANDLE_SIGNAL_WRITABLE,
|
||||
MOJO_DEADLINE_INDEFINITE, &WaitComplete, this);
|
||||
return;
|
||||
}
|
||||
delete this;
|
||||
}
|
||||
|
||||
void UnzipJob::WaitComplete(void* context, MojoResult result) {
|
||||
UnzipJob* job = static_cast<UnzipJob*>(context);
|
||||
job->wait_id_ = 0;
|
||||
job->OnHandleReady(result);
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
47
engine/src/flutter/assets/unzip_job.h
Normal file
47
engine/src/flutter/assets/unzip_job.h
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_UNZIP_JOB_H_
|
||||
#define FLUTTER_ASSETS_UNZIP_JOB_H_
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/assets/unique_unzipper.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/ftl/memory/ref_counted.h"
|
||||
#include "lib/ftl/tasks/task_runner.h"
|
||||
#include "mojo/public/cpp/environment/async_waiter.h"
|
||||
#include "mojo/services/asset_bundle/interfaces/asset_bundle.mojom.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class UnzipJob {
|
||||
public:
|
||||
UnzipJob(std::string zip_path,
|
||||
std::string asset_name,
|
||||
mojo::ScopedDataPipeProducerHandle producer,
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner);
|
||||
~UnzipJob();
|
||||
|
||||
private:
|
||||
void Start();
|
||||
void OnHandleReady(MojoResult result);
|
||||
static void WaitComplete(void* context, MojoResult result);
|
||||
|
||||
const std::string zip_path_;
|
||||
const std::string asset_name_;
|
||||
mojo::ScopedDataPipeProducerHandle producer_;
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner_;
|
||||
UniqueUnzipper zip_file_;
|
||||
|
||||
const MojoAsyncWaiter* waiter_;
|
||||
MojoAsyncWaitID wait_id_;
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(UnzipJob);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_UNZIP_JOB_H_
|
||||
26
engine/src/flutter/assets/zip_asset_bundle.cc
Normal file
26
engine/src/flutter/assets/zip_asset_bundle.cc
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/assets/zip_asset_bundle.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace blink {
|
||||
|
||||
void ZipAssetBundle::GetAsStream(
|
||||
const mojo::String& asset_name,
|
||||
const mojo::Callback<void(mojo::ScopedDataPipeConsumerHandle)>& callback) {
|
||||
mojo::DataPipe pipe;
|
||||
callback.Run(std::move(pipe.consumer_handle));
|
||||
store_->GetAsStream(asset_name.get(), std::move(pipe.producer_handle));
|
||||
}
|
||||
|
||||
ZipAssetBundle::ZipAssetBundle(
|
||||
mojo::InterfaceRequest<mojo::asset_bundle::AssetBundle> request,
|
||||
ftl::RefPtr<ZipAssetStore> store)
|
||||
: binding_(this, std::move(request)), store_(std::move(store)) {}
|
||||
|
||||
ZipAssetBundle::~ZipAssetBundle() {}
|
||||
|
||||
} // namespace blink
|
||||
36
engine/src/flutter/assets/zip_asset_bundle.h
Normal file
36
engine/src/flutter/assets/zip_asset_bundle.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_ZIP_ASSET_BUNDLE_H_
|
||||
#define FLUTTER_ASSETS_ZIP_ASSET_BUNDLE_H_
|
||||
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "mojo/public/cpp/bindings/interface_request.h"
|
||||
#include "mojo/public/cpp/bindings/strong_binding.h"
|
||||
#include "mojo/services/asset_bundle/interfaces/asset_bundle.mojom.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class ZipAssetBundle : public mojo::asset_bundle::AssetBundle {
|
||||
public:
|
||||
ZipAssetBundle(
|
||||
mojo::InterfaceRequest<mojo::asset_bundle::AssetBundle> request,
|
||||
ftl::RefPtr<ZipAssetStore> store);
|
||||
|
||||
// mojo::assert_bundle::AssetBundle implementation:
|
||||
void GetAsStream(
|
||||
const mojo::String& asset_name,
|
||||
const mojo::Callback<void(mojo::ScopedDataPipeConsumerHandle)>& callback)
|
||||
override;
|
||||
|
||||
private:
|
||||
~ZipAssetBundle() override;
|
||||
|
||||
mojo::StrongBinding<mojo::asset_bundle::AssetBundle> binding_;
|
||||
ftl::RefPtr<ZipAssetStore> store_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_ZIP_ASSET_BUNDLE_H_
|
||||
87
engine/src/flutter/assets/zip_asset_store.cc
Normal file
87
engine/src/flutter/assets/zip_asset_store.cc
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/assets/zip_asset_store.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "flutter/assets/unique_unzipper.h"
|
||||
#include "flutter/assets/unzip_job.h"
|
||||
#include "flutter/glue/data_pipe_utils.h"
|
||||
#include "lib/ftl/files/eintr_wrapper.h"
|
||||
#include "lib/ftl/files/unique_fd.h"
|
||||
#include "third_party/zlib/contrib/minizip/unzip.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
ZipAssetStore::ZipAssetStore(std::string zip_path,
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner)
|
||||
: zip_path_(std::move(zip_path)), task_runner_(std::move(task_runner)) {}
|
||||
|
||||
ZipAssetStore::~ZipAssetStore() {}
|
||||
|
||||
void ZipAssetStore::AddOverlayFile(std::string asset_name,
|
||||
std::string file_path) {
|
||||
overlay_files_.emplace(std::move(asset_name), std::move(file_path));
|
||||
}
|
||||
|
||||
void ZipAssetStore::GetAsStream(const std::string& asset_name,
|
||||
mojo::ScopedDataPipeProducerHandle producer) {
|
||||
auto overlay = overlay_files_.find(asset_name);
|
||||
if (overlay != overlay_files_.end()) {
|
||||
// TODO(abarth): Consider moving the |open| call to task_runner_.
|
||||
ftl::UniqueFD fd(HANDLE_EINTR(open(overlay->second.c_str(), O_RDONLY)));
|
||||
if (fd.get() < 0)
|
||||
return;
|
||||
glue::CopyFromFileDescriptor(std::move(fd), std::move(producer),
|
||||
task_runner_.get(), [](bool ignored) {});
|
||||
} else {
|
||||
new UnzipJob(zip_path_, asset_name, std::move(producer), task_runner_);
|
||||
}
|
||||
}
|
||||
|
||||
bool ZipAssetStore::GetAsBuffer(const std::string& asset_name,
|
||||
std::vector<uint8_t>* data) {
|
||||
UniqueUnzipper zip_file(unzOpen2(zip_path_.c_str(), nullptr));
|
||||
if (!zip_file.is_valid()) {
|
||||
FTL_LOG(ERROR) << "Unable to open ZIP file: " << zip_path_;
|
||||
return false;
|
||||
}
|
||||
|
||||
int result = unzLocateFile(zip_file.get(), asset_name.c_str(), 0);
|
||||
if (result != UNZ_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
unz_file_info file_info;
|
||||
result = unzGetCurrentFileInfo(zip_file.get(), &file_info, nullptr, 0,
|
||||
nullptr, 0, nullptr, 0);
|
||||
if (result != UNZ_OK) {
|
||||
FTL_LOG(WARNING) << "unzGetCurrentFileInfo failed, error=" << result;
|
||||
return false;
|
||||
}
|
||||
|
||||
result = unzOpenCurrentFile(zip_file.get());
|
||||
if (result != UNZ_OK) {
|
||||
FTL_LOG(WARNING) << "unzOpenCurrentFile failed, error=" << result;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->resize(file_info.uncompressed_size);
|
||||
int total_read = 0;
|
||||
while (total_read < static_cast<int>(data->size())) {
|
||||
int bytes_read = unzReadCurrentFile(
|
||||
zip_file.get(), data->data() + total_read, data->size() - total_read);
|
||||
if (bytes_read <= 0)
|
||||
return false;
|
||||
total_read += bytes_read;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
40
engine/src/flutter/assets/zip_asset_store.h
Normal file
40
engine/src/flutter/assets/zip_asset_store.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_ZIP_ASSET_STORE_H_
|
||||
#define FLUTTER_ASSETS_ZIP_ASSET_STORE_H_
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/ftl/memory/ref_counted.h"
|
||||
#include "lib/ftl/tasks/task_runner.h"
|
||||
#include "mojo/public/cpp/system/data_pipe.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class ZipAssetStore : public ftl::RefCountedThreadSafe<ZipAssetStore> {
|
||||
public:
|
||||
ZipAssetStore(std::string zip_path, ftl::RefPtr<ftl::TaskRunner> task_runner);
|
||||
~ZipAssetStore();
|
||||
|
||||
// Serve this asset from another file instead of using the ZIP contents.
|
||||
void AddOverlayFile(std::string asset_name, std::string file_path);
|
||||
|
||||
void GetAsStream(const std::string& asset_name,
|
||||
mojo::ScopedDataPipeProducerHandle producer);
|
||||
bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);
|
||||
|
||||
private:
|
||||
const std::string zip_path_;
|
||||
ftl::RefPtr<ftl::TaskRunner> task_runner_;
|
||||
std::map<std::string, std::string> overlay_files_;
|
||||
|
||||
FTL_DISALLOW_COPY_AND_ASSIGN(ZipAssetStore);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_ZIP_ASSET_STORE_H_
|
||||
@@ -44,10 +44,10 @@ source_set("flow") {
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//glue",
|
||||
"//flutter/glue",
|
||||
"//lib/ftl",
|
||||
"//mojo/services/gfx/composition/interfaces",
|
||||
"//mojo/skia",
|
||||
"//skia",
|
||||
"//flutter/skia",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/compositor_context.h"
|
||||
#include "flutter/flow/compositor_context.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_COMPOSITOR_CONTEXT_H_
|
||||
#define FLOW_COMPOSITOR_CONTEXT_H_
|
||||
#ifndef FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
|
||||
#define FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "flow/instrumentation.h"
|
||||
#include "flow/raster_cache.h"
|
||||
#include "flutter/flow/instrumentation.h"
|
||||
#include "flutter/flow/raster_cache.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "third_party/skia/include/core/SkPictureRecorder.h"
|
||||
@@ -72,4 +72,4 @@ class CompositorContext {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_COMPOSITOR_CONTEXT_H_
|
||||
#endif // FLUTTER_FLOW_COMPOSITOR_CONTEXT_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/instrumentation.h"
|
||||
#include "flutter/flow/instrumentation.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_INSTRUMENTATION_H_
|
||||
#define FLOW_INSTRUMENTATION_H_
|
||||
#ifndef FLUTTER_FLOW_INSTRUMENTATION_H_
|
||||
#define FLUTTER_FLOW_INSTRUMENTATION_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -67,4 +67,4 @@ class Counter {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_INSTRUMENTATION_H_
|
||||
#endif // FLUTTER_FLOW_INSTRUMENTATION_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/backdrop_filter_layer.h"
|
||||
#include "flutter/flow/layers/backdrop_filter_layer.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkImageFilter.h"
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
#define FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -27,4 +27,4 @@ class BackdropFilterLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_BACKDROP_FILTER_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/child_scene_layer.h"
|
||||
#include "flutter/flow/layers/child_scene_layer.h"
|
||||
|
||||
#include "mojo/skia/type_converters.h"
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
#define FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
#include "mojo/services/gfx/composition/interfaces/scenes.mojom.h"
|
||||
|
||||
namespace flow {
|
||||
@@ -46,4 +46,4 @@ class ChildSceneLayer : public Layer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_CHILD_SCENE_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/clip_path_layer.h"
|
||||
#include "flutter/flow/layers/clip_path_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
#define FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -28,4 +28,4 @@ class ClipPathLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_CLIP_PATH_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/clip_rect_layer.h"
|
||||
#include "flutter/flow/layers/clip_rect_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
#define FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -28,4 +28,4 @@ class ClipRectLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_CLIP_RECT_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/clip_rrect_layer.h"
|
||||
#include "flutter/flow/layers/clip_rrect_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
#define FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -28,4 +28,4 @@ class ClipRRectLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_CLIP_RRECT_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/color_filter_layer.h"
|
||||
#include "flutter/flow/layers/color_filter_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
#define FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -32,4 +32,4 @@ class ColorFilterLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_COLOR_FILTER_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
#define FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
|
||||
#include <vector>
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -35,4 +35,4 @@ class ContainerLayer : public Layer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_CONTAINER_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkColorFilter.h"
|
||||
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_LAYER_H_
|
||||
#define FLOW_LAYERS_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_LAYER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "flow/instrumentation.h"
|
||||
#include "flow/raster_cache.h"
|
||||
#include "glue/trace_event.h"
|
||||
#include "flutter/flow/instrumentation.h"
|
||||
#include "flutter/flow/raster_cache.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
@@ -85,4 +85,4 @@ class Layer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_LAYER_H_
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/layer_tree.h"
|
||||
#include "flutter/flow/layers/layer_tree.h"
|
||||
|
||||
#include "glue/trace_event.h"
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_LAYER_TREE_H_
|
||||
#define FLOW_LAYERS_LAYER_TREE_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_LAYER_TREE_H_
|
||||
#define FLUTTER_FLOW_LAYERS_LAYER_TREE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flow/compositor_context.h"
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/compositor_context.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "lib/ftl/time/time_delta.h"
|
||||
#include "third_party/skia/include/core/SkSize.h"
|
||||
@@ -75,4 +75,4 @@ class LayerTree {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_LAYER_TREE_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_LAYER_TREE_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/opacity_layer.h"
|
||||
#include "flutter/flow/layers/opacity_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
#define FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -27,4 +27,4 @@ class OpacityLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_OPACITY_LAYER_H_
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "flow/layers/performance_overlay_layer.h"
|
||||
#include "flutter/flow/layers/performance_overlay_layer.h"
|
||||
|
||||
namespace flow {
|
||||
namespace {
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
#define FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
|
||||
namespace flow {
|
||||
@@ -29,4 +29,4 @@ class PerformanceOverlayLayer : public Layer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_PERFORMANCE_OVERLAY_LAYER_H_
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/picture_layer.h"
|
||||
#include "flutter/flow/layers/picture_layer.h"
|
||||
|
||||
#include "flow/raster_cache.h"
|
||||
#include "flutter/flow/raster_cache.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
#define FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
|
||||
#include "flow/layers/layer.h"
|
||||
#include "flutter/flow/layers/layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -39,4 +39,4 @@ class PictureLayer : public Layer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_PICTURE_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/shader_mask_layer.h"
|
||||
#include "flutter/flow/layers/shader_mask_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
#define FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
#include "third_party/skia/include/core/SkShader.h"
|
||||
|
||||
@@ -37,4 +37,4 @@ class ShaderMaskLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_SHADER_MASK_LAYER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/layers/transform_layer.h"
|
||||
#include "flutter/flow/layers/transform_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
#define FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
#ifndef FLUTTER_FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
#define FLUTTER_FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
|
||||
#include "flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -27,4 +27,4 @@ class TransformLayer : public ContainerLayer {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
#endif // FLUTTER_FLOW_LAYERS_TRANSFORM_LAYER_H_
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_OPEN_GL_H_
|
||||
#define FLOW_OPEN_GL_H_
|
||||
#ifndef FLUTTER_FLOW_OPEN_GL_H_
|
||||
#define FLUTTER_FLOW_OPEN_GL_H_
|
||||
|
||||
#include "lib/ftl/build_config.h"
|
||||
|
||||
@@ -29,4 +29,4 @@
|
||||
|
||||
#endif
|
||||
|
||||
#endif // FLOW_OPEN_GL_H_
|
||||
#endif // FLUTTER_FLOW_OPEN_GL_H_
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/raster_cache.h"
|
||||
#include "flutter/flow/raster_cache.h"
|
||||
|
||||
#include "glue/trace_event.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
#include "lib/ftl/logging.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "third_party/skia/include/core/SkImage.h"
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_RASTER_CACHE_H_
|
||||
#define FLOW_RASTER_CACHE_H_
|
||||
#ifndef FLUTTER_FLOW_RASTER_CACHE_H_
|
||||
#define FLUTTER_FLOW_RASTER_CACHE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "flow/instrumentation.h"
|
||||
#include "flutter/flow/instrumentation.h"
|
||||
#include "lib/ftl/macros.h"
|
||||
#include "third_party/skia/include/core/SkImage.h"
|
||||
#include "third_party/skia/include/core/SkSize.h"
|
||||
@@ -48,4 +48,4 @@ class RasterCache {
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_RASTER_CACHE_H_
|
||||
#endif // FLUTTER_FLOW_RASTER_CACHE_H_
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flow/open_gl.h"
|
||||
#include "flow/texture_image.h"
|
||||
#include "glue/trace_event.h"
|
||||
#include "flutter/flow/open_gl.h"
|
||||
#include "flutter/flow/texture_image.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
#include "third_party/skia/include/gpu/gl/GrGLTypes.h"
|
||||
|
||||
namespace flow {
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLOW_TEXTURE_IMAGE_H_
|
||||
#define FLOW_TEXTURE_IMAGE_H_
|
||||
#ifndef FLUTTER_FLOW_TEXTURE_IMAGE_H_
|
||||
#define FLUTTER_FLOW_TEXTURE_IMAGE_H_
|
||||
|
||||
#include "third_party/skia/include/core/SkImage.h"
|
||||
#include "third_party/skia/include/core/SkImageGenerator.h"
|
||||
@@ -17,4 +17,4 @@ sk_sp<SkImage> BitmapImageCreate(SkImageGenerator& generator);
|
||||
|
||||
} // namespace flow
|
||||
|
||||
#endif // FLOW_TEXTURE_IMAGE_H_
|
||||
#endif // FLUTTER_FLOW_TEXTURE_IMAGE_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "glue/data_pipe_utils.h"
|
||||
#include "flutter/glue/data_pipe_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_DATA_PIPE_FILES_H_
|
||||
#define GLUE_DATA_PIPE_FILES_H_
|
||||
#ifndef FLUTTER_GLU_DATA_PIPE_FILES_H_
|
||||
#define FLUTTER_GLU_DATA_PIPE_FILES_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -35,4 +35,4 @@ void CopyFromFileDescriptor(
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_DATA_PIPE_FILES_H_
|
||||
#endif // FLUTTER_GLU_DATA_PIPE_FILES_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "glue/drain_data_pipe_job.h"
|
||||
#include "flutter/glue/drain_data_pipe_job.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_DRAIN_DATA_PIPE_JOB_H_
|
||||
#define GLUE_DRAIN_DATA_PIPE_JOB_H_
|
||||
#ifndef FLUTTER_GLU_DRAIN_DATA_PIPE_JOB_H_
|
||||
#define FLUTTER_GLU_DRAIN_DATA_PIPE_JOB_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
@@ -32,4 +32,4 @@ class DrainDataPipeJob {
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_DRAIN_DATA_PIPE_JOB_H_
|
||||
#endif // FLUTTER_GLU_DRAIN_DATA_PIPE_JOB_H_
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_MOVABLE_WRAPPER_H_
|
||||
#define GLUE_MOVABLE_WRAPPER_H_
|
||||
#ifndef FLUTTER_GLU_MOVABLE_WRAPPER_H_
|
||||
#define FLUTTER_GLU_MOVABLE_WRAPPER_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
@@ -34,4 +34,4 @@ MovableWrapper<T> WrapMovable(T object) {
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_MOVABLE_WRAPPER_H_
|
||||
#endif // FLUTTER_GLU_MOVABLE_WRAPPER_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "glue/stack_trace.h"
|
||||
#include "flutter/glue/stack_trace.h"
|
||||
|
||||
#include "base/debug/stack_trace.h"
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_STACK_TRACE_H_
|
||||
#define GLUE_STACK_TRACE_H_
|
||||
#ifndef FLUTTER_GLU_STACK_TRACE_H_
|
||||
#define FLUTTER_GLU_STACK_TRACE_H_
|
||||
|
||||
namespace glue {
|
||||
|
||||
@@ -11,4 +11,4 @@ void PrintStackTrace();
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_STACK_TRACE_H_
|
||||
#endif // FLUTTER_GLU_STACK_TRACE_H_
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "glue/task_runner_adaptor.h"
|
||||
#include "flutter/glue/task_runner_adaptor.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_BASE_TASK_RUNNER_H_
|
||||
#define GLUE_BASE_TASK_RUNNER_H_
|
||||
#ifndef FLUTTER_GLU_BASE_TASK_RUNNER_H_
|
||||
#define FLUTTER_GLU_BASE_TASK_RUNNER_H_
|
||||
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "lib/ftl/tasks/task_runner.h"
|
||||
@@ -30,4 +30,4 @@ class TaskRunnerAdaptor : public ftl::TaskRunner {
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_BASE_TASK_RUNNER_H_
|
||||
#endif // FLUTTER_GLU_BASE_TASK_RUNNER_H_
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "glue/thread.h"
|
||||
#include "flutter/glue/thread.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/threading/thread.h"
|
||||
#include "glue/task_runner_adaptor.h"
|
||||
#include "flutter/glue/task_runner_adaptor.h"
|
||||
|
||||
namespace glue {
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef GLUE_THREAD_H_
|
||||
#define GLUE_THREAD_H_
|
||||
#ifndef FLUTTER_GLU_THREAD_H_
|
||||
#define FLUTTER_GLU_THREAD_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
@@ -31,4 +31,4 @@ class Thread {
|
||||
|
||||
} // namespace glue
|
||||
|
||||
#endif // GLUE_THREAD_H_
|
||||
#endif // FLUTTER_GLU_THREAD_H_
|
||||
|
||||
25
engine/src/flutter/lib/jni/BUILD.gn
Normal file
25
engine/src/flutter/lib/jni/BUILD.gn
Normal file
@@ -0,0 +1,25 @@
|
||||
# Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
source_set("jni") {
|
||||
sources = [
|
||||
"dart_jni.cc",
|
||||
"dart_jni.h",
|
||||
"jni_api.cc",
|
||||
"jni_api.h",
|
||||
"jni_array.cc",
|
||||
"jni_array.h",
|
||||
"jni_class.cc",
|
||||
"jni_class.h",
|
||||
"jni_object.cc",
|
||||
"jni_object.h",
|
||||
"jni_string.cc",
|
||||
"jni_string.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//flutter/tonic",
|
||||
]
|
||||
}
|
||||
411
engine/src/flutter/lib/jni/dart_jni.cc
Normal file
411
engine/src/flutter/lib/jni/dart_jni.cc
Normal file
@@ -0,0 +1,411 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/dart_jni.h"
|
||||
|
||||
#include "base/android/jni_android.h"
|
||||
#include "base/android/jni_string.h"
|
||||
#include "base/logging.h"
|
||||
#include "flutter/lib/jni/jni_api.h"
|
||||
#include "flutter/lib/jni/jni_array.h"
|
||||
#include "flutter/lib/jni/jni_class.h"
|
||||
#include "flutter/lib/jni/jni_object.h"
|
||||
#include "flutter/lib/jni/jni_string.h"
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
using base::android::ScopedJavaGlobalRef;
|
||||
using base::android::ScopedJavaLocalRef;
|
||||
using tonic::DartConverter;
|
||||
using tonic::StdStringToDart;
|
||||
using tonic::ToDart;
|
||||
|
||||
namespace {
|
||||
|
||||
tonic::DartLibraryNatives* g_natives = nullptr;
|
||||
|
||||
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
|
||||
int argument_count,
|
||||
bool* auto_setup_scope) {
|
||||
return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
|
||||
}
|
||||
|
||||
const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
|
||||
return g_natives->GetSymbol(native_function);
|
||||
}
|
||||
|
||||
// Data cached from the Java VM.
|
||||
struct DartJniJvmData {
|
||||
ScopedJavaGlobalRef<jobject> class_loader;
|
||||
ScopedJavaGlobalRef<jclass> class_clazz;
|
||||
jmethodID class_loader_load_class_method_id;
|
||||
jmethodID class_get_name_method_id;
|
||||
};
|
||||
|
||||
DartJniJvmData* g_jvm_data = nullptr;
|
||||
DartJniIsolateDataProvider g_isolate_data_provider = nullptr;
|
||||
|
||||
DartJniIsolateData* IsolateData() {
|
||||
return g_isolate_data_provider();
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
// Check if a JNI API has thrown an exception. If so, convert it to a
|
||||
// Dart exception.
|
||||
bool CheckJniException(JNIEnv* env, Dart_Handle* exception) {
|
||||
if (env->ExceptionCheck() == JNI_FALSE)
|
||||
return false;
|
||||
|
||||
jthrowable java_throwable = env->ExceptionOccurred();
|
||||
env->ExceptionClear();
|
||||
std::string info = base::android::GetJavaExceptionInfo(env, java_throwable);
|
||||
|
||||
*exception = StdStringToDart(info);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if a Dart API returned an error handle.
|
||||
bool CheckDartException(Dart_Handle result, Dart_Handle* exception) {
|
||||
if (!Dart_IsError(result))
|
||||
return false;
|
||||
|
||||
*exception = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
DART_NATIVE_CALLBACK_STATIC(JniApi, FromReflectedField);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniApi, FromReflectedMethod);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniApi, GetApplicationContext);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniApi, GetClassLoader);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniBooleanArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniByteArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniCharArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniClass, FromName);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniClass, FromClassObject);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniDoubleArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniFloatArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniIntArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniLongArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniObjectArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniShortArray, Create);
|
||||
DART_NATIVE_CALLBACK_STATIC(JniString, Create);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(JniArray, GetLength) \
|
||||
V(JniBooleanArray, GetArrayElement) \
|
||||
V(JniBooleanArray, SetArrayElement) \
|
||||
V(JniByteArray, GetArrayElement) \
|
||||
V(JniByteArray, SetArrayElement) \
|
||||
V(JniCharArray, GetArrayElement) \
|
||||
V(JniCharArray, SetArrayElement) \
|
||||
V(JniClass, CallStaticBooleanMethod) \
|
||||
V(JniClass, CallStaticByteMethod) \
|
||||
V(JniClass, CallStaticCharMethod) \
|
||||
V(JniClass, CallStaticDoubleMethod) \
|
||||
V(JniClass, CallStaticFloatMethod) \
|
||||
V(JniClass, CallStaticIntMethod) \
|
||||
V(JniClass, CallStaticLongMethod) \
|
||||
V(JniClass, CallStaticObjectMethod) \
|
||||
V(JniClass, CallStaticShortMethod) \
|
||||
V(JniClass, CallStaticVoidMethod) \
|
||||
V(JniClass, GetFieldId) \
|
||||
V(JniClass, GetMethodId) \
|
||||
V(JniClass, GetStaticBooleanField) \
|
||||
V(JniClass, GetStaticByteField) \
|
||||
V(JniClass, GetStaticCharField) \
|
||||
V(JniClass, GetStaticDoubleField) \
|
||||
V(JniClass, GetStaticFieldId) \
|
||||
V(JniClass, GetStaticFloatField) \
|
||||
V(JniClass, GetStaticIntField) \
|
||||
V(JniClass, GetStaticLongField) \
|
||||
V(JniClass, GetStaticMethodId) \
|
||||
V(JniClass, GetStaticObjectField) \
|
||||
V(JniClass, GetStaticShortField) \
|
||||
V(JniClass, IsAssignable) \
|
||||
V(JniClass, NewObject) \
|
||||
V(JniClass, SetStaticBooleanField) \
|
||||
V(JniClass, SetStaticByteField) \
|
||||
V(JniClass, SetStaticCharField) \
|
||||
V(JniClass, SetStaticDoubleField) \
|
||||
V(JniClass, SetStaticFloatField) \
|
||||
V(JniClass, SetStaticIntField) \
|
||||
V(JniClass, SetStaticLongField) \
|
||||
V(JniClass, SetStaticObjectField) \
|
||||
V(JniClass, SetStaticShortField) \
|
||||
V(JniDoubleArray, GetArrayElement) \
|
||||
V(JniDoubleArray, SetArrayElement) \
|
||||
V(JniFloatArray, GetArrayElement) \
|
||||
V(JniFloatArray, SetArrayElement) \
|
||||
V(JniIntArray, GetArrayElement) \
|
||||
V(JniIntArray, SetArrayElement) \
|
||||
V(JniLongArray, GetArrayElement) \
|
||||
V(JniLongArray, SetArrayElement) \
|
||||
V(JniObject, CallBooleanMethod) \
|
||||
V(JniObject, CallByteMethod) \
|
||||
V(JniObject, CallCharMethod) \
|
||||
V(JniObject, CallDoubleMethod) \
|
||||
V(JniObject, CallFloatMethod) \
|
||||
V(JniObject, CallIntMethod) \
|
||||
V(JniObject, CallLongMethod) \
|
||||
V(JniObject, CallObjectMethod) \
|
||||
V(JniObject, CallShortMethod) \
|
||||
V(JniObject, CallVoidMethod) \
|
||||
V(JniObject, GetBooleanField) \
|
||||
V(JniObject, GetByteField) \
|
||||
V(JniObject, GetCharField) \
|
||||
V(JniObject, GetDoubleField) \
|
||||
V(JniObject, GetFloatField) \
|
||||
V(JniObject, GetIntField) \
|
||||
V(JniObject, GetLongField) \
|
||||
V(JniObject, GetObjectClass) \
|
||||
V(JniObject, GetObjectField) \
|
||||
V(JniObject, GetShortField) \
|
||||
V(JniObject, SetBooleanField) \
|
||||
V(JniObject, SetByteField) \
|
||||
V(JniObject, SetCharField) \
|
||||
V(JniObject, SetDoubleField) \
|
||||
V(JniObject, SetFloatField) \
|
||||
V(JniObject, SetIntField) \
|
||||
V(JniObject, SetLongField) \
|
||||
V(JniObject, SetObjectField) \
|
||||
V(JniObject, SetShortField) \
|
||||
V(JniObjectArray, GetArrayElement) \
|
||||
V(JniObjectArray, SetArrayElement) \
|
||||
V(JniShortArray, GetArrayElement) \
|
||||
V(JniShortArray, SetArrayElement) \
|
||||
V(JniString, GetText)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void DartJni::InitForGlobal(DartJniIsolateDataProvider provider) {
|
||||
if (!g_isolate_data_provider)
|
||||
g_isolate_data_provider = provider;
|
||||
|
||||
if (!g_natives) {
|
||||
g_natives = new tonic::DartLibraryNatives();
|
||||
g_natives->Register(
|
||||
{DART_REGISTER_NATIVE_STATIC(JniApi, FromReflectedField),
|
||||
DART_REGISTER_NATIVE_STATIC(JniApi, FromReflectedMethod),
|
||||
DART_REGISTER_NATIVE_STATIC(JniApi, GetApplicationContext),
|
||||
DART_REGISTER_NATIVE_STATIC(JniApi, GetClassLoader),
|
||||
DART_REGISTER_NATIVE_STATIC(JniBooleanArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniByteArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniCharArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniClass, FromName),
|
||||
DART_REGISTER_NATIVE_STATIC(JniClass, FromClassObject),
|
||||
DART_REGISTER_NATIVE_STATIC(JniDoubleArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniFloatArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniIntArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniLongArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniObjectArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniShortArray, Create),
|
||||
DART_REGISTER_NATIVE_STATIC(JniString, Create),
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
}
|
||||
|
||||
void DartJni::InitForIsolate() {
|
||||
DCHECK(g_natives);
|
||||
|
||||
Dart_Handle jni_library = Dart_LookupLibrary(ToDart("dart:jni"));
|
||||
DART_CHECK_VALID(jni_library)
|
||||
|
||||
DART_CHECK_VALID(
|
||||
Dart_SetNativeResolver(jni_library, GetNativeFunction, GetSymbol));
|
||||
|
||||
Dart_Handle object_type =
|
||||
Dart_GetType(jni_library, ToDart("JniObject"), 0, nullptr);
|
||||
DART_CHECK_VALID(object_type);
|
||||
IsolateData()->jni_object_type = Dart_NewPersistentHandle(object_type);
|
||||
DART_CHECK_VALID(IsolateData()->jni_object_type);
|
||||
|
||||
Dart_Handle float_type =
|
||||
Dart_GetType(jni_library, ToDart("JniFloat"), 0, nullptr);
|
||||
DART_CHECK_VALID(float_type);
|
||||
IsolateData()->jni_float_type = Dart_NewPersistentHandle(float_type);
|
||||
DART_CHECK_VALID(IsolateData()->jni_float_type);
|
||||
}
|
||||
|
||||
bool DartJni::InitJni() {
|
||||
JNIEnv* env = base::android::AttachCurrentThread();
|
||||
|
||||
DCHECK(!g_jvm_data);
|
||||
g_jvm_data = new DartJniJvmData();
|
||||
|
||||
g_jvm_data->class_loader.Reset(base::android::GetClassLoader(env));
|
||||
|
||||
ScopedJavaLocalRef<jclass> class_loader_clazz(
|
||||
env, env->FindClass("java/lang/ClassLoader"));
|
||||
CHECK(!base::android::ClearException(env));
|
||||
|
||||
g_jvm_data->class_loader_load_class_method_id =
|
||||
env->GetMethodID(class_loader_clazz.obj(), "loadClass",
|
||||
"(Ljava/lang/String;)Ljava/lang/Class;");
|
||||
CHECK(!base::android::ClearException(env));
|
||||
|
||||
g_jvm_data->class_clazz.Reset(env, env->FindClass("java/lang/Class"));
|
||||
CHECK(!base::android::ClearException(env));
|
||||
|
||||
g_jvm_data->class_get_name_method_id = env->GetMethodID(
|
||||
g_jvm_data->class_clazz.obj(), "getName", "()Ljava/lang/String;");
|
||||
CHECK(!base::android::ClearException(env));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void DartJni::OnThreadExit() {
|
||||
base::android::DetachFromVM();
|
||||
}
|
||||
|
||||
ScopedJavaLocalRef<jclass> DartJni::GetClass(JNIEnv* env, const char* name) {
|
||||
jobject clazz = env->CallObjectMethod(
|
||||
g_jvm_data->class_loader.obj(),
|
||||
g_jvm_data->class_loader_load_class_method_id,
|
||||
base::android::ConvertUTF8ToJavaString(env, name).obj());
|
||||
|
||||
return ScopedJavaLocalRef<jclass>(env, static_cast<jclass>(clazz));
|
||||
}
|
||||
|
||||
std::string DartJni::GetObjectClassName(JNIEnv* env, jobject obj) {
|
||||
jclass clazz = env->GetObjectClass(obj);
|
||||
DCHECK(clazz);
|
||||
jstring name = static_cast<jstring>(
|
||||
env->CallObjectMethod(clazz, g_jvm_data->class_get_name_method_id));
|
||||
DCHECK(name);
|
||||
|
||||
return base::android::ConvertJavaStringToUTF8(env, name);
|
||||
}
|
||||
|
||||
jstring DartJni::DartToJavaString(JNIEnv* env,
|
||||
Dart_Handle dart_string,
|
||||
Dart_Handle* exception) {
|
||||
if (!Dart_IsString(dart_string)) {
|
||||
*exception = ToDart("Argument must be a string");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
intptr_t length;
|
||||
Dart_Handle result = Dart_StringLength(dart_string, &length);
|
||||
if (CheckDartException(result, exception))
|
||||
return nullptr;
|
||||
|
||||
std::vector<uint16_t> string_data(length);
|
||||
result = Dart_StringToUTF16(dart_string, string_data.data(), &length);
|
||||
if (CheckDartException(result, exception))
|
||||
return nullptr;
|
||||
|
||||
jstring java_string = env->NewString(string_data.data(), length);
|
||||
CheckJniException(env, exception);
|
||||
return java_string;
|
||||
}
|
||||
|
||||
jobject DartJni::class_loader() {
|
||||
return g_jvm_data->class_loader.obj();
|
||||
}
|
||||
|
||||
jclass DartJni::class_clazz() {
|
||||
return g_jvm_data->class_clazz.obj();
|
||||
}
|
||||
|
||||
Dart_Handle DartJni::jni_object_type() {
|
||||
Dart_Handle object_type =
|
||||
Dart_HandleFromPersistent(IsolateData()->jni_object_type);
|
||||
DCHECK(!Dart_IsError(object_type));
|
||||
return object_type;
|
||||
}
|
||||
|
||||
Dart_Handle DartJni::jni_float_type() {
|
||||
Dart_Handle float_type =
|
||||
Dart_HandleFromPersistent(IsolateData()->jni_float_type);
|
||||
DCHECK(!Dart_IsError(float_type));
|
||||
return float_type;
|
||||
}
|
||||
|
||||
void JniMethodArgs::Convert(JNIEnv* env,
|
||||
const std::vector<Dart_Handle>& dart_args,
|
||||
Dart_Handle* exception) {
|
||||
jvalues_.reserve(dart_args.size());
|
||||
|
||||
for (Dart_Handle dart_arg : dart_args) {
|
||||
jvalue value = DartToJavaValue(env, dart_arg, exception);
|
||||
if (*exception)
|
||||
return;
|
||||
jvalues_.push_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
jvalue JniMethodArgs::DartToJavaValue(JNIEnv* env,
|
||||
Dart_Handle dart_value,
|
||||
Dart_Handle* exception) {
|
||||
jvalue java_value = jvalue();
|
||||
|
||||
if (Dart_IsBoolean(dart_value)) {
|
||||
java_value.z = DartConverter<bool>::FromDart(dart_value);
|
||||
return java_value;
|
||||
}
|
||||
|
||||
if (Dart_IsInteger(dart_value)) {
|
||||
java_value.j = DartConverter<jlong>::FromDart(dart_value);
|
||||
return java_value;
|
||||
}
|
||||
|
||||
if (Dart_IsDouble(dart_value)) {
|
||||
java_value.d = DartConverter<jdouble>::FromDart(dart_value);
|
||||
return java_value;
|
||||
}
|
||||
|
||||
if (Dart_IsString(dart_value)) {
|
||||
java_value.l = DartJni::DartToJavaString(env, dart_value, exception);
|
||||
return java_value;
|
||||
}
|
||||
|
||||
if (Dart_IsNull(dart_value)) {
|
||||
java_value.l = nullptr;
|
||||
return java_value;
|
||||
}
|
||||
|
||||
bool is_object;
|
||||
Dart_Handle result =
|
||||
Dart_ObjectIsType(dart_value, DartJni::jni_object_type(), &is_object);
|
||||
if (CheckDartException(result, exception))
|
||||
return java_value;
|
||||
|
||||
if (is_object) {
|
||||
JniObject* jni_object = DartConverter<JniObject*>::FromDart(dart_value);
|
||||
if (jni_object != nullptr) {
|
||||
java_value.l = jni_object->java_object();
|
||||
} else {
|
||||
*exception = ToDart("Invalid JniObject argument");
|
||||
}
|
||||
return java_value;
|
||||
}
|
||||
|
||||
bool is_float;
|
||||
result = Dart_ObjectIsType(dart_value, DartJni::jni_float_type(), &is_float);
|
||||
if (CheckDartException(result, exception))
|
||||
return java_value;
|
||||
|
||||
if (is_float) {
|
||||
Dart_Handle value_handle = Dart_GetField(dart_value, ToDart("value"));
|
||||
if (CheckDartException(value_handle, exception))
|
||||
return java_value;
|
||||
|
||||
double double_value;
|
||||
result = Dart_DoubleValue(value_handle, &double_value);
|
||||
if (CheckDartException(result, exception))
|
||||
return java_value;
|
||||
|
||||
java_value.f = static_cast<jfloat>(double_value);
|
||||
return java_value;
|
||||
}
|
||||
|
||||
*exception = ToDart("Argument has unsupported data type");
|
||||
return java_value;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
11
engine/src/flutter/lib/jni/dart_jni.gni
Normal file
11
engine/src/flutter/lib/jni/dart_jni.gni
Normal file
@@ -0,0 +1,11 @@
|
||||
# Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
dart_jni_files = [
|
||||
"//flutter/lib/jni/jni_helper.dart",
|
||||
"//flutter/lib/jni/jni_raw.dart",
|
||||
"//flutter/lib/jni/jni.dart",
|
||||
]
|
||||
|
||||
dart_jni_path = "//flutter/lib/jni/jni.dart"
|
||||
101
engine/src/flutter/lib/jni/dart_jni.h
Normal file
101
engine/src/flutter/lib/jni/dart_jni.h
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_DART_JNI_H_
|
||||
#define FLUTTER_LIB_JNI_DART_JNI_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "base/android/jni_android.h"
|
||||
#include "base/android/jni_utils.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
|
||||
#define ENTER_JNI() \
|
||||
JNIEnv* env = base::android::AttachCurrentThread(); \
|
||||
base::android::ScopedJavaLocalFrame java_frame(env);
|
||||
|
||||
namespace blink {
|
||||
|
||||
bool CheckJniException(JNIEnv* env, Dart_Handle* exception);
|
||||
bool CheckDartException(Dart_Handle result, Dart_Handle* exception);
|
||||
|
||||
// Data cached for each Dart isolate.
|
||||
struct DartJniIsolateData {
|
||||
Dart_PersistentHandle jni_object_type;
|
||||
Dart_PersistentHandle jni_float_type;
|
||||
};
|
||||
|
||||
typedef DartJniIsolateData* (*DartJniIsolateDataProvider)();
|
||||
|
||||
class DartJni {
|
||||
public:
|
||||
static void InitForGlobal(DartJniIsolateDataProvider provider);
|
||||
static void InitForIsolate();
|
||||
static bool InitJni();
|
||||
static void OnThreadExit();
|
||||
|
||||
static base::android::ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env,
|
||||
const char* name);
|
||||
|
||||
static std::string GetObjectClassName(JNIEnv* env, jobject obj);
|
||||
|
||||
static jstring DartToJavaString(JNIEnv* env,
|
||||
Dart_Handle dart_string,
|
||||
Dart_Handle* exception);
|
||||
|
||||
static jobject class_loader();
|
||||
static jclass class_clazz();
|
||||
static Dart_Handle jni_object_type();
|
||||
static Dart_Handle jni_float_type();
|
||||
};
|
||||
|
||||
class JniMethodArgs {
|
||||
public:
|
||||
void Convert(JNIEnv* env,
|
||||
const std::vector<Dart_Handle>& dart_args,
|
||||
Dart_Handle* exception);
|
||||
jvalue* jvalues() { return jvalues_.data(); }
|
||||
|
||||
private:
|
||||
jvalue DartToJavaValue(JNIEnv* env,
|
||||
Dart_Handle handle,
|
||||
Dart_Handle* exception);
|
||||
|
||||
std::vector<jvalue> jvalues_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
namespace tonic {
|
||||
|
||||
template <>
|
||||
struct DartConverter<jfieldID> {
|
||||
static jfieldID FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
int64_t result = 0;
|
||||
Dart_Handle handle = Dart_GetNativeIntegerArgument(args, index, &result);
|
||||
if (Dart_IsError(handle))
|
||||
exception = handle;
|
||||
return reinterpret_cast<jfieldID>(result);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct DartConverter<jmethodID> {
|
||||
static jmethodID FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
int64_t result = 0;
|
||||
Dart_Handle handle = Dart_GetNativeIntegerArgument(args, index, &result);
|
||||
if (Dart_IsError(handle))
|
||||
exception = handle;
|
||||
return reinterpret_cast<jmethodID>(result);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tonic
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_DART_JNI_H_
|
||||
67
engine/src/flutter/lib/jni/jni.dart
Normal file
67
engine/src/flutter/lib/jni/jni.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/// Invoke Java Native Interface APIs from Dart.
|
||||
///
|
||||
/// To use, import `dart:jni`.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// JavaClass dateFormatClass = Java.getClass('java.text.SimpleDateFormat');
|
||||
/// JavaObject format = dateFormatClass.newInstance('yyyy.MM.dd');
|
||||
/// print(format.parse('2016.01.01').getYear());
|
||||
///
|
||||
/// This library provides a way to access Java classes that are accessible to
|
||||
/// the Android runtime hosting Flutter (including Android system APIs).
|
||||
///
|
||||
/// The library consists of two parts: a raw module that offers Dart access
|
||||
/// to low-level JNI functions, and a helper module that uses reflection to
|
||||
/// build Java object wrappers that act like standard Dart objects.
|
||||
///
|
||||
/// # Raw JNI
|
||||
///
|
||||
/// Call [JniClass.fromName] to load a JNI class given its fully-qualified name.
|
||||
/// The resulting [JniClass] object provides methods for constructing instances,
|
||||
/// looking up field and method IDs, getting and setting static fields, and calling
|
||||
/// static methods. These methods closely match the JNI functions provided by the VM.
|
||||
///
|
||||
/// Getters/setters of object fields and calls to methods that return objects will
|
||||
/// return instances of [JniObject]. [JniObject] similarly offers wrappers of the
|
||||
/// JNI functions that get and set object fields and call object methods.
|
||||
///
|
||||
/// Array instances are represented by [JniArray] and its subclasses, which provide
|
||||
/// a Dart [List] interface backed by an underlying Java array.
|
||||
///
|
||||
/// # Java Object Wrappers
|
||||
///
|
||||
/// Call [Java.getClass] to create a [JavaClass] instance that acts as a higher-level
|
||||
/// wrapper for a Java class. Using this wrapper, you can call static methods and read
|
||||
/// and write static fields using standard Dart field and method syntax. The wrapper
|
||||
/// will use reflection to locate the corresponding Java fields and methods and map Dart
|
||||
/// operations to JNI calls.
|
||||
///
|
||||
/// Use the [JavaClass.newInstance] method to construct instances of the class.
|
||||
/// `newInstance` will pass its arguments to the appropriate constructor.
|
||||
///
|
||||
/// Instances of Java objects are represented by [JavaObject], which similarly exposes
|
||||
/// field and method accessors derived via reflection.
|
||||
///
|
||||
/// Dart boolean, number, and string types as well as [JavaObject] instances can be passed
|
||||
/// as arguments to wrapped Java methods. The JNI libraries will convert these arguments
|
||||
/// from Dart types to the matching Java type.
|
||||
///
|
||||
/// Dart does not support function overloading, and as a result the Java method
|
||||
/// wrappers need to locate the method on the underlying Java class that most closely
|
||||
/// matches the arguments passed to the wrapper. If there are multiple Java method overloads
|
||||
/// that could be suitable for a given set of arguments and the wrappers do not choose the
|
||||
/// desired overload, then you can obtain the [JniObject] wrapped by the [JavaObject] and
|
||||
/// use the raw JNI library to invoke the method.
|
||||
|
||||
library dart_jni;
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:nativewrappers';
|
||||
|
||||
part 'jni_raw.dart';
|
||||
part 'jni_helper.dart';
|
||||
53
engine/src/flutter/lib/jni/jni_api.cc
Normal file
53
engine/src/flutter/lib/jni/jni_api.cc
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/jni_api.h"
|
||||
|
||||
#include "flutter/lib/jni/dart_jni.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
int64_t JniApi::FromReflectedField(const JniObject* field) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfieldID result = env->FromReflectedField(field->java_object());
|
||||
if (CheckJniException(env, &exception)) goto fail;
|
||||
|
||||
return reinterpret_cast<int64_t>(result);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniApi::FromReflectedMethod(const JniObject* method) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jmethodID result = env->FromReflectedMethod(method->java_object());
|
||||
if (CheckJniException(env, &exception)) goto fail;
|
||||
|
||||
return reinterpret_cast<int64_t>(result);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniApi::GetApplicationContext() {
|
||||
ENTER_JNI();
|
||||
return JniObject::Create(env, base::android::GetApplicationContext());
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniApi::GetClassLoader() {
|
||||
ENTER_JNI();
|
||||
return JniObject::Create(env, DartJni::class_loader());
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
24
engine/src/flutter/lib/jni/jni_api.h
Normal file
24
engine/src/flutter/lib/jni/jni_api.h
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_JNI_API_H_
|
||||
#define FLUTTER_LIB_JNI_JNI_API_H_
|
||||
|
||||
#include "flutter/lib/jni/jni_class.h"
|
||||
#include "flutter/lib/jni/jni_object.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
// Dart wrappers for basic JNI APIs.
|
||||
class JniApi {
|
||||
public:
|
||||
static int64_t FromReflectedField(const JniObject* field);
|
||||
static int64_t FromReflectedMethod(const JniObject* method);
|
||||
static ftl::RefPtr<JniObject> GetApplicationContext();
|
||||
static ftl::RefPtr<JniObject> GetClassLoader();
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_JNI_API_H_
|
||||
579
engine/src/flutter/lib/jni/jni_array.cc
Normal file
579
engine/src/flutter/lib/jni/jni_array.cc
Normal file
@@ -0,0 +1,579 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/jni_array.h"
|
||||
|
||||
#include "flutter/lib/jni/dart_jni.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniArray);
|
||||
|
||||
JniArray::JniArray(JNIEnv* env, jarray array) : JniObject(env, array) {}
|
||||
|
||||
JniArray::~JniArray() {}
|
||||
|
||||
jsize JniArray::GetLength() {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jsize result = env->GetArrayLength(java_array<jarray>());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename JArrayType>
|
||||
JArrayType JniArray::java_array() const {
|
||||
return static_cast<JArrayType>(java_object());
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniObjectArray);
|
||||
|
||||
JniObjectArray::JniObjectArray(JNIEnv* env, jobjectArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniObjectArray::~JniObjectArray() {}
|
||||
|
||||
ftl::RefPtr<JniObjectArray> JniObjectArray::Create(const JniClass* clazz,
|
||||
jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jobjectArray array =
|
||||
env->NewObjectArray(length, clazz->java_class(), nullptr);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniObjectArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniObjectArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jobject obj = env->GetObjectArrayElement(java_array<jobjectArray>(), index);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void JniObjectArray::SetArrayElement(jsize index, const JniObject* value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetObjectArrayElement(java_array<jobjectArray>(), index,
|
||||
value ? value->java_object() : nullptr);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniBooleanArray);
|
||||
|
||||
JniBooleanArray::JniBooleanArray(JNIEnv* env, jbooleanArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniBooleanArray::~JniBooleanArray() {}
|
||||
|
||||
ftl::RefPtr<JniBooleanArray> JniBooleanArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbooleanArray array = env->NewBooleanArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniBooleanArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniBooleanArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jboolean result;
|
||||
env->GetBooleanArrayRegion(java_array<jbooleanArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniBooleanArray::SetArrayElement(jsize index, bool value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jboolean jni_value = value ? JNI_TRUE : JNI_FALSE;
|
||||
env->SetBooleanArrayRegion(java_array<jbooleanArray>(), index, 1,
|
||||
&jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniByteArray);
|
||||
|
||||
JniByteArray::JniByteArray(JNIEnv* env, jbyteArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniByteArray::~JniByteArray() {}
|
||||
|
||||
ftl::RefPtr<JniByteArray> JniByteArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbyteArray array = env->NewByteArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniByteArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t JniByteArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbyte result;
|
||||
env->GetByteArrayRegion(java_array<jbyteArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniByteArray::SetArrayElement(jsize index, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbyte jni_value = static_cast<jbyte>(value);
|
||||
env->SetByteArrayRegion(java_array<jbyteArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniCharArray);
|
||||
|
||||
JniCharArray::JniCharArray(JNIEnv* env, jcharArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniCharArray::~JniCharArray() {}
|
||||
|
||||
ftl::RefPtr<JniCharArray> JniCharArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jcharArray array = env->NewCharArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniCharArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t JniCharArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jchar result;
|
||||
env->GetCharArrayRegion(java_array<jcharArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniCharArray::SetArrayElement(jsize index, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jchar jni_value = static_cast<jchar>(value);
|
||||
env->SetCharArrayRegion(java_array<jcharArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniShortArray);
|
||||
|
||||
JniShortArray::JniShortArray(JNIEnv* env, jshortArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniShortArray::~JniShortArray() {}
|
||||
|
||||
ftl::RefPtr<JniShortArray> JniShortArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jshortArray array = env->NewShortArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniShortArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t JniShortArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jshort result;
|
||||
env->GetShortArrayRegion(java_array<jshortArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniShortArray::SetArrayElement(jsize index, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jshort jni_value = static_cast<jshort>(value);
|
||||
env->SetShortArrayRegion(java_array<jshortArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniIntArray);
|
||||
|
||||
JniIntArray::JniIntArray(JNIEnv* env, jintArray array) : JniArray(env, array) {}
|
||||
|
||||
JniIntArray::~JniIntArray() {}
|
||||
|
||||
ftl::RefPtr<JniIntArray> JniIntArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jintArray array = env->NewIntArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniIntArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t JniIntArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jint result;
|
||||
env->GetIntArrayRegion(java_array<jintArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniIntArray::SetArrayElement(jsize index, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jint jni_value = static_cast<jint>(value);
|
||||
env->SetIntArrayRegion(java_array<jintArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniLongArray);
|
||||
|
||||
JniLongArray::JniLongArray(JNIEnv* env, jlongArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniLongArray::~JniLongArray() {}
|
||||
|
||||
ftl::RefPtr<JniLongArray> JniLongArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jlongArray array = env->NewLongArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniLongArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int64_t JniLongArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jlong result;
|
||||
env->GetLongArrayRegion(java_array<jlongArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return static_cast<int64_t>(result);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniLongArray::SetArrayElement(jsize index, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jlong jni_value = static_cast<jlong>(value);
|
||||
env->SetLongArrayRegion(java_array<jlongArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniFloatArray);
|
||||
|
||||
JniFloatArray::JniFloatArray(JNIEnv* env, jfloatArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniFloatArray::~JniFloatArray() {}
|
||||
|
||||
ftl::RefPtr<JniFloatArray> JniFloatArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloatArray array = env->NewFloatArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniFloatArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
double JniFloatArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat result;
|
||||
env->GetFloatArrayRegion(java_array<jfloatArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniFloatArray::SetArrayElement(jsize index, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat jni_value = static_cast<jfloat>(value);
|
||||
env->SetFloatArrayRegion(java_array<jfloatArray>(), index, 1, &jni_value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniDoubleArray);
|
||||
|
||||
JniDoubleArray::JniDoubleArray(JNIEnv* env, jdoubleArray array)
|
||||
: JniArray(env, array) {}
|
||||
|
||||
JniDoubleArray::~JniDoubleArray() {}
|
||||
|
||||
ftl::RefPtr<JniDoubleArray> JniDoubleArray::Create(jsize length) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jdoubleArray array = env->NewDoubleArray(length);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniDoubleArray>(env, array);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
double JniDoubleArray::GetArrayElement(jsize index) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jdouble result;
|
||||
env->GetDoubleArrayRegion(java_array<jdoubleArray>(), index, 1, &result);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
void JniDoubleArray::SetArrayElement(jsize index, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetDoubleArrayRegion(java_array<jdoubleArray>(), index, 1, &value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
177
engine/src/flutter/lib/jni/jni_array.h
Normal file
177
engine/src/flutter/lib/jni/jni_array.h
Normal file
@@ -0,0 +1,177 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_JNI_ARRAY_H_
|
||||
#define FLUTTER_LIB_JNI_JNI_ARRAY_H_
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "flutter/lib/jni/jni_class.h"
|
||||
#include "flutter/lib/jni/jni_object.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
// Wrapper for a JNI array
|
||||
class JniArray : public JniObject {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
|
||||
public:
|
||||
~JniArray() override;
|
||||
|
||||
jsize GetLength();
|
||||
|
||||
protected:
|
||||
JniArray(JNIEnv* env, jarray array);
|
||||
template <typename JArrayType>
|
||||
JArrayType java_array() const;
|
||||
};
|
||||
|
||||
class JniObjectArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniObjectArray);
|
||||
|
||||
public:
|
||||
~JniObjectArray() override;
|
||||
|
||||
static ftl::RefPtr<JniObjectArray> Create(const JniClass* clazz,
|
||||
jsize length);
|
||||
ftl::RefPtr<JniObject> GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, const JniObject* value);
|
||||
|
||||
private:
|
||||
JniObjectArray(JNIEnv* env, jobjectArray array);
|
||||
};
|
||||
|
||||
class JniBooleanArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniBooleanArray);
|
||||
|
||||
public:
|
||||
~JniBooleanArray() override;
|
||||
|
||||
static ftl::RefPtr<JniBooleanArray> Create(jsize length);
|
||||
bool GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, bool value);
|
||||
|
||||
private:
|
||||
JniBooleanArray(JNIEnv* env, jbooleanArray array);
|
||||
};
|
||||
|
||||
class JniByteArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniByteArray);
|
||||
|
||||
public:
|
||||
~JniByteArray() override;
|
||||
|
||||
static ftl::RefPtr<JniByteArray> Create(jsize length);
|
||||
int64_t GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, int64_t value);
|
||||
|
||||
private:
|
||||
JniByteArray(JNIEnv* env, jbyteArray array);
|
||||
};
|
||||
|
||||
class JniCharArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniCharArray);
|
||||
|
||||
public:
|
||||
~JniCharArray() override;
|
||||
|
||||
static ftl::RefPtr<JniCharArray> Create(jsize length);
|
||||
int64_t GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, int64_t value);
|
||||
|
||||
private:
|
||||
JniCharArray(JNIEnv* env, jcharArray array);
|
||||
};
|
||||
|
||||
class JniShortArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniShortArray);
|
||||
|
||||
public:
|
||||
~JniShortArray() override;
|
||||
|
||||
static ftl::RefPtr<JniShortArray> Create(jsize length);
|
||||
int64_t GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, int64_t value);
|
||||
|
||||
private:
|
||||
JniShortArray(JNIEnv* env, jshortArray array);
|
||||
};
|
||||
|
||||
class JniIntArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniIntArray);
|
||||
|
||||
public:
|
||||
~JniIntArray() override;
|
||||
|
||||
static ftl::RefPtr<JniIntArray> Create(jsize length);
|
||||
int64_t GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, int64_t value);
|
||||
|
||||
private:
|
||||
JniIntArray(JNIEnv* env, jintArray array);
|
||||
};
|
||||
|
||||
class JniLongArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniLongArray);
|
||||
|
||||
public:
|
||||
~JniLongArray() override;
|
||||
|
||||
static ftl::RefPtr<JniLongArray> Create(jsize length);
|
||||
int64_t GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, int64_t value);
|
||||
|
||||
private:
|
||||
JniLongArray(JNIEnv* env, jlongArray array);
|
||||
};
|
||||
|
||||
class JniFloatArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniFloatArray);
|
||||
|
||||
public:
|
||||
~JniFloatArray() override;
|
||||
|
||||
static ftl::RefPtr<JniFloatArray> Create(jsize length);
|
||||
double GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, double value);
|
||||
|
||||
private:
|
||||
JniFloatArray(JNIEnv* env, jfloatArray array);
|
||||
};
|
||||
|
||||
class JniDoubleArray : public JniArray {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniDoubleArray);
|
||||
|
||||
public:
|
||||
~JniDoubleArray() override;
|
||||
|
||||
static ftl::RefPtr<JniDoubleArray> Create(jsize length);
|
||||
double GetArrayElement(jsize index);
|
||||
void SetArrayElement(jsize index, double value);
|
||||
|
||||
private:
|
||||
JniDoubleArray(JNIEnv* env, jdoubleArray array);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_JNI_ARRAY_H_
|
||||
720
engine/src/flutter/lib/jni/jni_class.cc
Normal file
720
engine/src/flutter/lib/jni/jni_class.cc
Normal file
@@ -0,0 +1,720 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/jni_class.h"
|
||||
|
||||
#include "flutter/lib/jni/dart_jni.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
using tonic::ToDart;
|
||||
using base::android::ScopedJavaLocalRef;
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniClass);
|
||||
|
||||
JniClass::JniClass(JNIEnv* env, jclass clazz) : JniObject(env, clazz) {}
|
||||
|
||||
JniClass::~JniClass() {}
|
||||
|
||||
ftl::RefPtr<JniClass> JniClass::FromName(const char* name) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
if (!name)
|
||||
return nullptr;
|
||||
|
||||
ScopedJavaLocalRef<jclass> clazz = DartJni::GetClass(env, name);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniClass>(env, clazz.obj());
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniClass> JniClass::FromClassObject(const JniObject* clazz) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
if (!clazz)
|
||||
return nullptr;
|
||||
|
||||
jobject class_object = clazz->java_object();
|
||||
if (!env->IsInstanceOf(class_object, DartJni::class_clazz())) {
|
||||
exception = ToDart("invalid JNI class object");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return ftl::MakeRefCounted<JniClass>(env,
|
||||
static_cast<jclass>(class_object));
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
intptr_t JniClass::GetFieldId(const char* name, const char* sig) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfieldID id = env->GetFieldID(java_class(), name, sig);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return reinterpret_cast<intptr_t>(id);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
intptr_t JniClass::GetStaticFieldId(const char* name, const char* sig) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfieldID id = env->GetStaticFieldID(java_class(), name, sig);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return reinterpret_cast<intptr_t>(id);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
intptr_t JniClass::GetMethodId(const char* name, const char* sig) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jmethodID id = env->GetMethodID(java_class(), name, sig);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return reinterpret_cast<intptr_t>(id);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
intptr_t JniClass::GetStaticMethodId(const char* name, const char* sig) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jmethodID id = env->GetStaticMethodID(java_class(), name, sig);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return reinterpret_cast<intptr_t>(id);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniClass::NewObject(
|
||||
jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jobject obj = env->NewObjectA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniClass::IsAssignable(const JniClass* clazz) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jboolean result = env->IsAssignableFrom(java_class(), clazz->java_class());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniClass::GetStaticObjectField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jobject obj = env->GetStaticObjectField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniClass::GetStaticBooleanField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jboolean result = env->GetStaticBooleanField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t JniClass::GetStaticByteField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbyte result = env->GetStaticByteField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::GetStaticCharField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jchar result = env->GetStaticCharField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::GetStaticShortField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jshort result = env->GetStaticShortField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::GetStaticIntField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jint result = env->GetStaticIntField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::GetStaticLongField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jlong result = env->GetStaticLongField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniClass::GetStaticFloatField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat result = env->GetStaticFloatField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniClass::GetStaticDoubleField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat result = env->GetStaticDoubleField(java_class(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticObjectField(jfieldID fieldId, const JniObject* value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticObjectField(java_class(), fieldId,
|
||||
value ? value->java_object() : nullptr);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticBooleanField(jfieldID fieldId, bool value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticBooleanField(java_class(), fieldId,
|
||||
value ? JNI_TRUE : JNI_FALSE);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticByteField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticByteField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticCharField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticCharField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticShortField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticShortField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticIntField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticIntField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticLongField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticLongField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticFloatField(jfieldID fieldId, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticFloatField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniClass::SetStaticDoubleField(jfieldID fieldId, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetStaticDoubleField(java_class(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniClass::CallStaticObjectMethod(
|
||||
jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jobject obj = env->CallStaticObjectMethodA(java_class(), methodId,
|
||||
java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniClass::CallStaticBooleanMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jboolean result = env->CallStaticBooleanMethodA(java_class(), methodId,
|
||||
java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t JniClass::CallStaticByteMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jbyte result =
|
||||
env->CallStaticByteMethodA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::CallStaticCharMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jchar result =
|
||||
env->CallStaticCharMethodA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::CallStaticShortMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jshort result = env->CallStaticShortMethodA(java_class(), methodId,
|
||||
java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::CallStaticIntMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jint result =
|
||||
env->CallStaticIntMethodA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniClass::CallStaticLongMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jlong result =
|
||||
env->CallStaticLongMethodA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniClass::CallStaticFloatMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jfloat result = env->CallStaticFloatMethodA(java_class(), methodId,
|
||||
java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniClass::CallStaticDoubleMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jdouble result = env->CallStaticDoubleMethodA(java_class(), methodId,
|
||||
java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void JniClass::CallStaticVoidMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
env->CallStaticVoidMethodA(java_class(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
88
engine/src/flutter/lib/jni/jni_class.h
Normal file
88
engine/src/flutter/lib/jni/jni_class.h
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_JNI_CLASS_H_
|
||||
#define FLUTTER_LIB_JNI_JNI_CLASS_H_
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "base/android/jni_android.h"
|
||||
#include "flutter/lib/jni/jni_object.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
// Wrapper that exposes a JNI jclass to Dart
|
||||
class JniClass : public JniObject {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniClass);
|
||||
|
||||
public:
|
||||
~JniClass() override;
|
||||
|
||||
static ftl::RefPtr<JniClass> FromName(const char* className);
|
||||
static ftl::RefPtr<JniClass> FromClassObject(const JniObject* clazz);
|
||||
|
||||
jclass java_class() const { return static_cast<jclass>(object_.obj()); }
|
||||
|
||||
intptr_t GetFieldId(const char* name, const char* sig);
|
||||
intptr_t GetStaticFieldId(const char* name, const char* sig);
|
||||
intptr_t GetMethodId(const char* name, const char* sig);
|
||||
intptr_t GetStaticMethodId(const char* name, const char* sig);
|
||||
|
||||
ftl::RefPtr<JniObject> NewObject(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
|
||||
bool IsAssignable(const JniClass* clazz);
|
||||
|
||||
ftl::RefPtr<JniObject> GetStaticObjectField(jfieldID fieldId);
|
||||
bool GetStaticBooleanField(jfieldID fieldId);
|
||||
int64_t GetStaticByteField(jfieldID fieldId);
|
||||
int64_t GetStaticCharField(jfieldID fieldId);
|
||||
int64_t GetStaticShortField(jfieldID fieldId);
|
||||
int64_t GetStaticIntField(jfieldID fieldId);
|
||||
int64_t GetStaticLongField(jfieldID fieldId);
|
||||
double GetStaticFloatField(jfieldID fieldId);
|
||||
double GetStaticDoubleField(jfieldID fieldId);
|
||||
|
||||
void SetStaticObjectField(jfieldID fieldId, const JniObject* value);
|
||||
void SetStaticBooleanField(jfieldID fieldId, bool value);
|
||||
void SetStaticByteField(jfieldID fieldId, int64_t value);
|
||||
void SetStaticCharField(jfieldID fieldId, int64_t value);
|
||||
void SetStaticShortField(jfieldID fieldId, int64_t value);
|
||||
void SetStaticIntField(jfieldID fieldId, int64_t value);
|
||||
void SetStaticLongField(jfieldID fieldId, int64_t value);
|
||||
void SetStaticFloatField(jfieldID fieldId, double value);
|
||||
void SetStaticDoubleField(jfieldID fieldId, double value);
|
||||
|
||||
ftl::RefPtr<JniObject> CallStaticObjectMethod(
|
||||
jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
bool CallStaticBooleanMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallStaticByteMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallStaticCharMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallStaticShortMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallStaticIntMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallStaticLongMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
double CallStaticFloatMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
double CallStaticDoubleMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
void CallStaticVoidMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
|
||||
private:
|
||||
JniClass(JNIEnv* env, jclass clazz);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_JNI_CLASS_H_
|
||||
647
engine/src/flutter/lib/jni/jni_helper.dart
Normal file
647
engine/src/flutter/lib/jni/jni_helper.dart
Normal file
@@ -0,0 +1,647 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of dart_jni;
|
||||
|
||||
class JavaError extends Error {
|
||||
JavaError(this._message);
|
||||
String _message;
|
||||
String toString() => _message;
|
||||
}
|
||||
|
||||
// JNI methods used to invoke Java reflection
|
||||
class _JavaReflect {
|
||||
JniClass classClazz;
|
||||
int classForName;
|
||||
int classGetConstructors;
|
||||
int classGetFields;
|
||||
int classGetMethods;
|
||||
int classGetName;
|
||||
int constructorGetParameterTypes;
|
||||
int fieldGetType;
|
||||
int memberGetModifiers;
|
||||
int memberGetName;
|
||||
int methodGetParameterTypes;
|
||||
int methodGetReturnType;
|
||||
int objectGetClass;
|
||||
int objectToString;
|
||||
JniClass stringClazz;
|
||||
|
||||
int modifierStatic;
|
||||
|
||||
_JavaReflect() {
|
||||
classClazz = JniClass.fromName('java.lang.Class');
|
||||
classForName = classClazz.getStaticMethodId('forName',
|
||||
'(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;');
|
||||
classGetConstructors = classClazz.getMethodId('getConstructors',
|
||||
'()[Ljava/lang/reflect/Constructor;');
|
||||
classGetFields = classClazz.getMethodId('getFields', '()[Ljava/lang/reflect/Field;');
|
||||
classGetMethods = classClazz.getMethodId('getMethods', '()[Ljava/lang/reflect/Method;');
|
||||
classGetName = classClazz.getMethodId('getName', '()Ljava/lang/String;');
|
||||
|
||||
JniClass constructorClazz = JniClass.fromName('java.lang.reflect.Constructor');
|
||||
constructorGetParameterTypes = constructorClazz.getMethodId(
|
||||
'getParameterTypes', '()[Ljava/lang/Class;');
|
||||
|
||||
JniClass fieldClazz = JniClass.fromName('java.lang.reflect.Field');
|
||||
fieldGetType = fieldClazz.getMethodId('getType', '()Ljava/lang/Class;');
|
||||
|
||||
JniClass memberClazz = JniClass.fromName('java.lang.reflect.Member');
|
||||
memberGetModifiers = memberClazz.getMethodId('getModifiers', '()I');
|
||||
memberGetName = memberClazz.getMethodId('getName', '()Ljava/lang/String;');
|
||||
|
||||
JniClass methodClazz = JniClass.fromName('java.lang.reflect.Method');
|
||||
methodGetParameterTypes = methodClazz.getMethodId(
|
||||
'getParameterTypes', '()[Ljava/lang/Class;');
|
||||
methodGetReturnType = methodClazz.getMethodId(
|
||||
'getReturnType', '()Ljava/lang/Class;');
|
||||
|
||||
JniClass modifierClazz = JniClass.fromName('java.lang.reflect.Modifier');
|
||||
modifierStatic = modifierClazz.getStaticIntField(
|
||||
modifierClazz.getStaticFieldId('STATIC', 'I'));
|
||||
|
||||
JniClass objectClazz = JniClass.fromName('java.lang.Object');
|
||||
objectGetClass = objectClazz.getMethodId('getClass', '()Ljava/lang/Class;');
|
||||
objectToString = objectClazz.getMethodId('toString', '()Ljava/lang/String;');
|
||||
|
||||
stringClazz = JniClass.fromName('java.lang.String');
|
||||
}
|
||||
}
|
||||
|
||||
final _JavaReflect _reflect = new _JavaReflect();
|
||||
|
||||
class _JavaType {
|
||||
final String name;
|
||||
final JniClass clazz;
|
||||
final bool isPrimitive;
|
||||
|
||||
_JavaType(this.name, this.clazz)
|
||||
: isPrimitive = false;
|
||||
|
||||
_JavaType._primitive(this.name)
|
||||
: clazz = null,
|
||||
isPrimitive = true;
|
||||
|
||||
String toString() => 'JavaType:$name';
|
||||
}
|
||||
|
||||
class _JavaPrimitive {
|
||||
static final _JavaType jvoid = new _JavaType._primitive('void');
|
||||
static final _JavaType jboolean = new _JavaType._primitive('boolean');
|
||||
static final _JavaType jbyte = new _JavaType._primitive('byte');
|
||||
static final _JavaType jchar = new _JavaType._primitive('char');
|
||||
static final _JavaType jshort = new _JavaType._primitive('short');
|
||||
static final _JavaType jint = new _JavaType._primitive('int');
|
||||
static final _JavaType jlong = new _JavaType._primitive('long');
|
||||
static final _JavaType jfloat = new _JavaType._primitive('float');
|
||||
static final _JavaType jdouble = new _JavaType._primitive('double');
|
||||
}
|
||||
|
||||
final Map<String, _JavaType> _typeCache = <String, _JavaType>{
|
||||
'void': _JavaPrimitive.jvoid,
|
||||
'boolean': _JavaPrimitive.jboolean,
|
||||
'byte': _JavaPrimitive.jbyte,
|
||||
'char': _JavaPrimitive.jchar,
|
||||
'short': _JavaPrimitive.jshort,
|
||||
'int': _JavaPrimitive.jint,
|
||||
'long': _JavaPrimitive.jlong,
|
||||
'float': _JavaPrimitive.jfloat,
|
||||
'double': _JavaPrimitive.jdouble,
|
||||
};
|
||||
|
||||
_JavaType _javaTypeForClass(JniClass clazz) {
|
||||
String className = JniString.unwrap(clazz.callObjectMethod(_reflect.classGetName, []));
|
||||
|
||||
_JavaType type = _typeCache[className];
|
||||
if (type != null)
|
||||
return type;
|
||||
|
||||
type = new _JavaType(className, clazz);
|
||||
_typeCache[className] = type;
|
||||
return type;
|
||||
}
|
||||
|
||||
class _JavaField {
|
||||
String name;
|
||||
int fieldId;
|
||||
_JavaType type;
|
||||
int modifiers;
|
||||
|
||||
_JavaField(JniObject field) {
|
||||
name = JniString.unwrap(field.callObjectMethod(_reflect.memberGetName, []));
|
||||
fieldId = JniApi.fromReflectedField(field);
|
||||
type = _javaTypeForClass(
|
||||
field.callObjectMethod(_reflect.fieldGetType, []));
|
||||
modifiers = field.callIntMethod(_reflect.memberGetModifiers, []);
|
||||
}
|
||||
|
||||
bool get isStatic => (modifiers & _reflect.modifierStatic) != 0;
|
||||
}
|
||||
|
||||
abstract class _HasArguments {
|
||||
String get name;
|
||||
List<_JavaType> get argTypes;
|
||||
int get methodId;
|
||||
}
|
||||
|
||||
class _JavaMethod implements _HasArguments {
|
||||
String _name;
|
||||
int _methodId;
|
||||
_JavaType returnType;
|
||||
int modifiers;
|
||||
List<_JavaType> _argTypes;
|
||||
|
||||
_JavaMethod(JniObject method) {
|
||||
_name = JniString.unwrap(method.callObjectMethod(_reflect.memberGetName, []));
|
||||
_methodId = JniApi.fromReflectedMethod(method);
|
||||
returnType = _javaTypeForClass(
|
||||
method.callObjectMethod(_reflect.methodGetReturnType, []));
|
||||
modifiers = method.callIntMethod(_reflect.memberGetModifiers, []);
|
||||
|
||||
JniObjectArray argClasses = method.callObjectMethod(_reflect.methodGetParameterTypes, []);
|
||||
_argTypes = new List<_JavaType>();
|
||||
for (JniObject argClass in argClasses) {
|
||||
_argTypes.add(_javaTypeForClass(argClass));
|
||||
}
|
||||
}
|
||||
|
||||
String get name => _name;
|
||||
List<_JavaType> get argTypes => _argTypes;
|
||||
int get methodId => _methodId;
|
||||
|
||||
bool get isStatic => (modifiers & _reflect.modifierStatic) != 0;
|
||||
}
|
||||
|
||||
class _JavaConstructor implements _HasArguments {
|
||||
String _name;
|
||||
int _methodId;
|
||||
List<_JavaType> _argTypes;
|
||||
|
||||
_JavaConstructor(JniObject ctor) {
|
||||
_name = JniString.unwrap(ctor.callObjectMethod(_reflect.memberGetName, []));
|
||||
_methodId = JniApi.fromReflectedMethod(ctor);
|
||||
|
||||
JniObjectArray argClasses = ctor.callObjectMethod(
|
||||
_reflect.constructorGetParameterTypes, []);
|
||||
_argTypes = new List<_JavaType>();
|
||||
for (JniObject argClass in argClasses) {
|
||||
_argTypes.add(_javaTypeForClass(argClass));
|
||||
}
|
||||
}
|
||||
|
||||
String get name => _name;
|
||||
List<_JavaType> get argTypes => _argTypes;
|
||||
int get methodId => _methodId;
|
||||
}
|
||||
|
||||
// Determines whether a Dart value can be used as a method argument declared as
|
||||
// the given Java type.
|
||||
bool _typeCompatible(dynamic value, _JavaType type) {
|
||||
if (type.isPrimitive) {
|
||||
if (type == _JavaPrimitive.jboolean) {
|
||||
return (value is bool);
|
||||
} else {
|
||||
return (value is num || value is bool || value is JniFloat);
|
||||
}
|
||||
}
|
||||
|
||||
if (value == null)
|
||||
return true;
|
||||
|
||||
if (value is JavaObject)
|
||||
return value.javaClass.jniClass.isAssignable(type.clazz);
|
||||
|
||||
if (value is JniObject)
|
||||
return value.getObjectClass().isAssignable(type.clazz);
|
||||
|
||||
if (value is String) {
|
||||
if (type.isPrimitive)
|
||||
return false;
|
||||
return _reflect.stringClazz.isAssignable(type.clazz);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Given a list of overloaded methods, select one that is the best match for
|
||||
// the provided arguments.
|
||||
_HasArguments _findMatchingMethod(List<_HasArguments> overloads, List args) {
|
||||
List<_HasArguments> matches = new List<_HasArguments>();
|
||||
for (_HasArguments overload in overloads) {
|
||||
if (overload.argTypes.length == args.length)
|
||||
matches.add(overload);
|
||||
}
|
||||
|
||||
if (matches.length == 1)
|
||||
return matches[0];
|
||||
|
||||
if (matches.isEmpty)
|
||||
throw new JavaError('Argument mismatch when invoking method ${overloads[0].name}');
|
||||
|
||||
outer: for (_HasArguments match in matches) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (!_typeCompatible(args[i], match.argTypes[i]))
|
||||
continue outer;
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
throw new JavaError('Unable to find matching method for ${overloads[0].name}');
|
||||
}
|
||||
|
||||
// Convert an object received from JNI into the corresponding Dart type.
|
||||
dynamic _javaObjectToDart(JniObject object) {
|
||||
if (object == null)
|
||||
return null;
|
||||
|
||||
if (object is JniString)
|
||||
return object.text;
|
||||
|
||||
if (object is JniClass)
|
||||
return Java.wrapClassObject(object);
|
||||
|
||||
if (object is JniArray)
|
||||
return object;
|
||||
|
||||
return new JavaObject(object);
|
||||
}
|
||||
|
||||
// Convert a Dart object to a type suitable for passing to JNI.
|
||||
dynamic _dartObjectToJava(dynamic object, _JavaType javaType) {
|
||||
if (javaType.isPrimitive) {
|
||||
if (javaType == _JavaPrimitive.jboolean) {
|
||||
if (object is bool)
|
||||
return object;
|
||||
throw new JavaError('Unable to convert Dart object to Java boolean: $object');
|
||||
}
|
||||
if (javaType == _JavaPrimitive.jfloat) {
|
||||
if (object is JniFloat)
|
||||
return object;
|
||||
if (object is num)
|
||||
return new JniFloat(object);
|
||||
throw new JavaError('Unable to convert Dart object to Java float: $object');
|
||||
}
|
||||
|
||||
if (object is num)
|
||||
return object;
|
||||
throw new JavaError('Unable to convert Dart object to Java primitive type: $object');
|
||||
}
|
||||
|
||||
if (object == null)
|
||||
return object;
|
||||
|
||||
if (object is JniObject)
|
||||
return object;
|
||||
|
||||
if (object is JavaObject)
|
||||
return object.jniObject;
|
||||
|
||||
if (object is String)
|
||||
return JniString.create(object);
|
||||
|
||||
throw new JavaError('Unable to convert Dart object to Java: $object');
|
||||
}
|
||||
|
||||
List _convertArgumentsToJava(List dartArgs, _HasArguments method) {
|
||||
List result = new List();
|
||||
for (int i = 0; i < dartArgs.length; i++) {
|
||||
result.add(_dartObjectToJava(dartArgs[i], method.argTypes[i]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// A class that provides access to Java objects from within Dart.
|
||||
class Java {
|
||||
static final Map<String, JavaClass> _classCache = new Map<String, JavaClass>();
|
||||
|
||||
/// Returns a [JavaClass] for the given class name.
|
||||
static JavaClass getClass(String className) {
|
||||
JavaClass cacheEntry = _classCache[className];
|
||||
if (cacheEntry != null)
|
||||
return cacheEntry;
|
||||
|
||||
// Load and initialize the class
|
||||
JniClass jniClass = _reflect.classClazz.callStaticObjectMethod(
|
||||
_reflect.classForName,
|
||||
[className, true, JniApi.getClassLoader()]
|
||||
);
|
||||
|
||||
JavaClass javaClass = new JavaClass._(jniClass);
|
||||
_classCache[javaClass.className] = javaClass;
|
||||
return javaClass;
|
||||
}
|
||||
|
||||
/// Returns a [JavaClass] that wraps a raw JNI class object.
|
||||
static JavaClass wrapClassObject(dynamic classObject) {
|
||||
JniClass jniClass;
|
||||
if (classObject is JniClass) {
|
||||
jniClass = classObject;
|
||||
} else if (classObject is JavaObject && classObject.jniObject is JniClass) {
|
||||
jniClass = classObject.jniObject;
|
||||
} else {
|
||||
throw new JavaError('fromClassObject: $classObject is not a Java class');
|
||||
}
|
||||
|
||||
String className = JniString.unwrap(jniClass.callObjectMethod(_reflect.classGetName, []));
|
||||
JavaClass cacheEntry = _classCache[className];
|
||||
if (cacheEntry != null)
|
||||
return cacheEntry;
|
||||
|
||||
JavaClass javaClass = new JavaClass._(jniClass);
|
||||
_classCache[javaClass.className] = javaClass;
|
||||
return javaClass;
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for a JNI class that uses reflection to provide access to the
|
||||
/// class' static fields, methods, and constructors.
|
||||
class JavaClass {
|
||||
JniClass _clazz;
|
||||
String _className;
|
||||
Map<Symbol, _JavaField> _fields;
|
||||
Map<Symbol, _JavaField> _staticFields;
|
||||
Map<Symbol, List<_JavaMethod>> _methods;
|
||||
Map<Symbol, List<_JavaMethod>> _staticMethods;
|
||||
List<_JavaConstructor> _constructors;
|
||||
|
||||
static final Symbol _newInstanceSymbol = new Symbol('newInstance');
|
||||
|
||||
JavaClass._(JniClass classObject) {
|
||||
_clazz = classObject;
|
||||
_className = JniString.unwrap(_clazz.callObjectMethod(_reflect.classGetName, []));
|
||||
|
||||
_fields = new Map<Symbol, _JavaField>();
|
||||
_staticFields = new Map<Symbol, _JavaField>();
|
||||
JniObjectArray reflectFields = _clazz.callObjectMethod(_reflect.classGetFields, []);
|
||||
for (JniObject reflectField in reflectFields) {
|
||||
_JavaField javaField = new _JavaField(reflectField);
|
||||
Map<Symbol, _JavaField> fieldMap =
|
||||
javaField.isStatic ? _staticFields : _fields;
|
||||
fieldMap[new Symbol(javaField.name)] = javaField;
|
||||
|
||||
// Dart will identify the field setter with a symbol name ending with an equal sign.
|
||||
fieldMap[new Symbol(javaField.name + '=')] = javaField;
|
||||
}
|
||||
|
||||
_methods = new Map<Symbol, List<_JavaMethod>>();
|
||||
_staticMethods = new Map<Symbol, List<_JavaMethod>>();
|
||||
JniObjectArray reflectMethods = _clazz.callObjectMethod(_reflect.classGetMethods, []);
|
||||
for (JniObject reflectMethod in reflectMethods) {
|
||||
_JavaMethod javaMethod = new _JavaMethod(reflectMethod);
|
||||
Map<Symbol, List<_JavaMethod>> methodMap =
|
||||
javaMethod.isStatic ? _staticMethods : _methods;
|
||||
Symbol symbol = new Symbol(javaMethod.name);
|
||||
List<_JavaMethod> overloads = methodMap[symbol];
|
||||
if (overloads != null) {
|
||||
overloads.add(javaMethod);
|
||||
} else {
|
||||
methodMap[symbol] = <_JavaMethod>[javaMethod];
|
||||
}
|
||||
}
|
||||
|
||||
_constructors = new List<_JavaConstructor>();
|
||||
JniObjectArray reflectCtors = _clazz.callObjectMethod(_reflect.classGetConstructors, []);
|
||||
for (JniObject ctor in reflectCtors) {
|
||||
_constructors.add(new _JavaConstructor(ctor));
|
||||
}
|
||||
}
|
||||
|
||||
String toString() => 'JavaClass:$_className';
|
||||
|
||||
/// The name of the wrapped Java class.
|
||||
String get className => _className;
|
||||
|
||||
/// A raw [JniClass] representing the java.lang.Class instance for this class
|
||||
JniClass get jniClass => _clazz;
|
||||
|
||||
/// A [JavaObject] representing the java.lang.Class instance for this class
|
||||
JavaObject get asJavaObject => new JavaObject(_clazz);
|
||||
|
||||
dynamic noSuchMethod(Invocation invocation) {
|
||||
if (invocation.isMethod) {
|
||||
List<_HasArguments> overloads;
|
||||
bool isConstructor = (invocation.memberName == _newInstanceSymbol);
|
||||
if (isConstructor) {
|
||||
overloads = _constructors;
|
||||
} else {
|
||||
overloads = _staticMethods[invocation.memberName];
|
||||
if (overloads == null)
|
||||
throw new JavaError('Static method ${invocation.memberName} not found');
|
||||
}
|
||||
|
||||
_HasArguments method = _findMatchingMethod(overloads, invocation.positionalArguments);
|
||||
List args = _convertArgumentsToJava(invocation.positionalArguments, method);
|
||||
|
||||
if (isConstructor) {
|
||||
return new JavaObject(_clazz.newObject(method.methodId, args));
|
||||
}
|
||||
|
||||
_JavaType returnType = (method as _JavaMethod).returnType;
|
||||
if (returnType == _JavaPrimitive.jvoid) {
|
||||
return _clazz.callStaticVoidMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jboolean) {
|
||||
return _clazz.callStaticBooleanMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jbyte) {
|
||||
return _clazz.callStaticByteMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jchar) {
|
||||
return _clazz.callStaticCharMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jshort) {
|
||||
return _clazz.callStaticShortMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jint) {
|
||||
return _clazz.callStaticIntMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jlong) {
|
||||
return _clazz.callStaticLongMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jfloat) {
|
||||
return _clazz.callStaticFloatMethod(method.methodId, args);
|
||||
} else if (returnType == _JavaPrimitive.jdouble) {
|
||||
return _clazz.callStaticDoubleMethod(method.methodId, args);
|
||||
} else {
|
||||
return _javaObjectToDart(
|
||||
_clazz.callStaticObjectMethod(method.methodId, args));
|
||||
}
|
||||
}
|
||||
|
||||
if (invocation.isGetter) {
|
||||
_JavaField field = _staticFields[invocation.memberName];
|
||||
if (field == null)
|
||||
throw new JavaError('Static field ${invocation.memberName} not found');
|
||||
|
||||
if (field.type == _JavaPrimitive.jboolean) {
|
||||
return _clazz.getStaticBooleanField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jbyte) {
|
||||
return _clazz.getStaticByteField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jchar) {
|
||||
return _clazz.getStaticCharField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jshort) {
|
||||
return _clazz.getStaticShortField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jint) {
|
||||
return _clazz.getStaticIntField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jlong) {
|
||||
return _clazz.getStaticLongField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jfloat) {
|
||||
return _clazz.getStaticFloatField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jdouble) {
|
||||
return _clazz.getStaticDoubleField(field.fieldId);
|
||||
} else {
|
||||
return _javaObjectToDart(_clazz.getStaticObjectField(field.fieldId));
|
||||
}
|
||||
}
|
||||
|
||||
if (invocation.isSetter) {
|
||||
_JavaField field = _staticFields[invocation.memberName];
|
||||
if (field == null)
|
||||
throw new JavaError('Static field ${invocation.memberName} not found');
|
||||
|
||||
dynamic value = invocation.positionalArguments[0];
|
||||
|
||||
if (field.type == _JavaPrimitive.jboolean) {
|
||||
_clazz.setStaticBooleanField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jbyte) {
|
||||
_clazz.setStaticByteField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jchar) {
|
||||
_clazz.setStaticCharField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jshort) {
|
||||
_clazz.setStaticShortField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jint) {
|
||||
_clazz.setStaticIntField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jlong) {
|
||||
_clazz.setStaticLongField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jfloat) {
|
||||
_clazz.setStaticFloatField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jdouble) {
|
||||
_clazz.setStaticDoubleField(field.fieldId, value);
|
||||
} else {
|
||||
_clazz.setStaticObjectField(field.fieldId, _dartObjectToJava(value, field.type));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new JavaError('Unable to access ${invocation.memberName}');
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for a JNI object that provides access to the object's fields
|
||||
/// and methods.
|
||||
class JavaObject {
|
||||
JniObject _object;
|
||||
JavaClass _clazz;
|
||||
|
||||
JavaObject(JniObject object) {
|
||||
_object = object;
|
||||
|
||||
_clazz = Java.wrapClassObject(
|
||||
_object.callObjectMethod(_reflect.objectGetClass, []));
|
||||
}
|
||||
|
||||
dynamic noSuchMethod(Invocation invocation) {
|
||||
if (invocation.isMethod) {
|
||||
List<_JavaMethod> overloads = _clazz._methods[invocation.memberName];
|
||||
if (overloads == null)
|
||||
throw new JavaError('Method ${invocation.memberName} not found');
|
||||
|
||||
_JavaMethod method = _findMatchingMethod(overloads, invocation.positionalArguments);
|
||||
List args = _convertArgumentsToJava(invocation.positionalArguments, method);
|
||||
|
||||
if (method.returnType == _JavaPrimitive.jvoid) {
|
||||
return _object.callVoidMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jboolean) {
|
||||
return _object.callBooleanMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jbyte) {
|
||||
return _object.callByteMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jchar) {
|
||||
return _object.callCharMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jshort) {
|
||||
return _object.callShortMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jint) {
|
||||
return _object.callIntMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jlong) {
|
||||
return _object.callLongMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jfloat) {
|
||||
return _object.callFloatMethod(method.methodId, args);
|
||||
} else if (method.returnType == _JavaPrimitive.jdouble) {
|
||||
return _object.callDoubleMethod(method.methodId, args);
|
||||
} else {
|
||||
return _javaObjectToDart(
|
||||
_object.callObjectMethod(method.methodId, args));
|
||||
}
|
||||
}
|
||||
|
||||
if (invocation.isGetter) {
|
||||
_JavaField field = _clazz._fields[invocation.memberName];
|
||||
if (field == null)
|
||||
throw new JavaError('Field ${invocation.memberName} not found');
|
||||
|
||||
if (field.type == _JavaPrimitive.jboolean) {
|
||||
return _object.getBooleanField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jbyte) {
|
||||
return _object.getByteField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jchar) {
|
||||
return _object.getCharField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jshort) {
|
||||
return _object.getShortField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jint) {
|
||||
return _object.getIntField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jlong) {
|
||||
return _object.getLongField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jfloat) {
|
||||
return _object.getFloatField(field.fieldId);
|
||||
} else if (field.type == _JavaPrimitive.jdouble) {
|
||||
return _object.getDoubleField(field.fieldId);
|
||||
} else {
|
||||
return _javaObjectToDart(_object.getObjectField(field.fieldId));
|
||||
}
|
||||
}
|
||||
|
||||
if (invocation.isSetter) {
|
||||
_JavaField field = _clazz._fields[invocation.memberName];
|
||||
if (field == null)
|
||||
throw new JavaError('Field ${invocation.memberName} not found');
|
||||
|
||||
dynamic value = invocation.positionalArguments[0];
|
||||
|
||||
if (field.type == _JavaPrimitive.jboolean) {
|
||||
_object.setBooleanField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jbyte) {
|
||||
_object.setByteField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jchar) {
|
||||
_object.setCharField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jshort) {
|
||||
_object.setShortField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jint) {
|
||||
_object.setIntField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jlong) {
|
||||
_object.setLongField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jfloat) {
|
||||
_object.setFloatField(field.fieldId, value);
|
||||
} else if (field.type == _JavaPrimitive.jdouble) {
|
||||
_object.setDoubleField(field.fieldId, value);
|
||||
} else {
|
||||
_object.setObjectField(field.fieldId, _dartObjectToJava(value, field.type));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new JavaError('Unable to access ${invocation.memberName}');
|
||||
}
|
||||
|
||||
/// Convert this object to a string using the Java object's `toString` method.
|
||||
String toString() {
|
||||
String result = JniString.unwrap(_object.callObjectMethod(_reflect.objectToString, []));
|
||||
if (!result.isEmpty) {
|
||||
return result;
|
||||
} else {
|
||||
return 'JavaObject(${_clazz.className})';
|
||||
}
|
||||
}
|
||||
|
||||
/// A [JavaClass] representing this object's class.
|
||||
JavaClass get javaClass => _clazz;
|
||||
|
||||
/// The raw [JniObject] wrapped by this object.
|
||||
/// This can be used to perform low-level JNI operations that aren't exposed
|
||||
/// by [JavaObject], such as access to specific overloaded methods.
|
||||
JniObject get jniObject => _object;
|
||||
}
|
||||
631
engine/src/flutter/lib/jni/jni_object.cc
Normal file
631
engine/src/flutter/lib/jni/jni_object.cc
Normal file
@@ -0,0 +1,631 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/jni_object.h"
|
||||
|
||||
#include "base/strings/string_util.h"
|
||||
#include "flutter/lib/jni/dart_jni.h"
|
||||
#include "flutter/lib/jni/jni_array.h"
|
||||
#include "flutter/lib/jni/jni_class.h"
|
||||
#include "flutter/lib/jni/jni_string.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniObject);
|
||||
|
||||
JniObject::JniObject(JNIEnv* env, jobject object) : object_(env, object) {}
|
||||
|
||||
JniObject::~JniObject() {}
|
||||
|
||||
ftl::RefPtr<JniObject> JniObject::Create(JNIEnv* env, jobject object) {
|
||||
if (object == nullptr)
|
||||
return nullptr;
|
||||
|
||||
std::string class_name = DartJni::GetObjectClassName(env, object);
|
||||
|
||||
ftl::RefPtr<JniObject> result;
|
||||
|
||||
if (class_name == "java.lang.String") {
|
||||
result = ftl::MakeRefCounted<JniString>(env, static_cast<jstring>(object));
|
||||
} else if (class_name == "java.lang.Class") {
|
||||
result = ftl::MakeRefCounted<JniClass>(env, static_cast<jclass>(object));
|
||||
} else if (base::StartsWith(class_name, "[L", base::CompareCase::SENSITIVE)) {
|
||||
result = ftl::MakeRefCounted<JniObjectArray>(
|
||||
env, static_cast<jobjectArray>(object));
|
||||
} else if (class_name == "[Z") {
|
||||
result = ftl::MakeRefCounted<JniBooleanArray>(
|
||||
env, static_cast<jbooleanArray>(object));
|
||||
} else if (class_name == "[B") {
|
||||
result =
|
||||
ftl::MakeRefCounted<JniByteArray>(env, static_cast<jbyteArray>(object));
|
||||
} else if (class_name == "[C") {
|
||||
result =
|
||||
ftl::MakeRefCounted<JniCharArray>(env, static_cast<jcharArray>(object));
|
||||
} else if (class_name == "[S") {
|
||||
result = ftl::MakeRefCounted<JniShortArray>(
|
||||
env, static_cast<jshortArray>(object));
|
||||
} else if (class_name == "[I") {
|
||||
result =
|
||||
ftl::MakeRefCounted<JniIntArray>(env, static_cast<jintArray>(object));
|
||||
} else if (class_name == "[J") {
|
||||
result =
|
||||
ftl::MakeRefCounted<JniLongArray>(env, static_cast<jlongArray>(object));
|
||||
} else if (class_name == "[F") {
|
||||
result = ftl::MakeRefCounted<JniFloatArray>(
|
||||
env, static_cast<jfloatArray>(object));
|
||||
} else if (class_name == "[D") {
|
||||
result = ftl::MakeRefCounted<JniDoubleArray>(
|
||||
env, static_cast<jdoubleArray>(object));
|
||||
} else {
|
||||
result = ftl::MakeRefCounted<JniObject>(env, object);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniClass> JniObject::GetObjectClass() {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jclass clazz = env->GetObjectClass(java_object());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniClass>(env, clazz);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniObject::GetObjectField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jobject obj = env->GetObjectField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniObject::GetBooleanField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jboolean result = env->GetBooleanField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t JniObject::GetByteField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jbyte result = env->GetByteField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::GetCharField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jchar result = env->GetCharField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::GetShortField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jshort result = env->GetShortField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::GetIntField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jint result = env->GetIntField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::GetLongField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jlong result = env->GetLongField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniObject::GetFloatField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat result = env->GetFloatField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniObject::GetDoubleField(jfieldID fieldId) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jfloat result = env->GetDoubleField(java_object(), fieldId);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void JniObject::SetObjectField(jfieldID fieldId, const JniObject* value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetObjectField(java_object(), fieldId,
|
||||
value ? value->java_object() : nullptr);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetBooleanField(jfieldID fieldId, bool value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetBooleanField(java_object(), fieldId, value ? JNI_TRUE : JNI_FALSE);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetByteField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetByteField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetCharField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetCharField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetShortField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetShortField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetIntField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetIntField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetLongField(jfieldID fieldId, int64_t value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetLongField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetFloatField(jfieldID fieldId, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetFloatField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
void JniObject::SetDoubleField(jfieldID fieldId, double value) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
env->SetDoubleField(java_object(), fieldId, value);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniObject> JniObject::CallObjectMethod(
|
||||
jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jobject obj =
|
||||
env->CallObjectMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return JniObject::Create(env, obj);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool JniObject::CallBooleanMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jboolean result =
|
||||
env->CallBooleanMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result == JNI_TRUE;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t JniObject::CallByteMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jbyte result =
|
||||
env->CallByteMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::CallCharMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jchar result =
|
||||
env->CallCharMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::CallShortMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jshort result =
|
||||
env->CallShortMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::CallIntMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jint result =
|
||||
env->CallIntMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t JniObject::CallLongMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jlong result =
|
||||
env->CallLongMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniObject::CallFloatMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jfloat result =
|
||||
env->CallFloatMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
double JniObject::CallDoubleMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
jdouble result =
|
||||
env->CallDoubleMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void JniObject::CallVoidMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
JniMethodArgs java_args;
|
||||
java_args.Convert(env, args, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
env->CallVoidMethodA(java_object(), methodId, java_args.jvalues());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
return;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
80
engine/src/flutter/lib/jni/jni_object.h
Normal file
80
engine/src/flutter/lib/jni/jni_object.h
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_JNI_OBJECT_H_
|
||||
#define FLUTTER_LIB_JNI_JNI_OBJECT_H_
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "base/android/jni_android.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class JniClass;
|
||||
|
||||
// Wrapper that exposes a JNI jobject to Dart
|
||||
class JniObject : public ftl::RefCountedThreadSafe<JniObject>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(JniObject);
|
||||
|
||||
public:
|
||||
~JniObject() override;
|
||||
|
||||
static ftl::RefPtr<JniObject> Create(JNIEnv* env, jobject object);
|
||||
|
||||
jobject java_object() const { return object_.obj(); }
|
||||
|
||||
ftl::RefPtr<JniClass> GetObjectClass();
|
||||
|
||||
ftl::RefPtr<JniObject> GetObjectField(jfieldID fieldId);
|
||||
bool GetBooleanField(jfieldID fieldId);
|
||||
int64_t GetByteField(jfieldID fieldId);
|
||||
int64_t GetCharField(jfieldID fieldId);
|
||||
int64_t GetShortField(jfieldID fieldId);
|
||||
int64_t GetIntField(jfieldID fieldId);
|
||||
int64_t GetLongField(jfieldID fieldId);
|
||||
double GetFloatField(jfieldID fieldId);
|
||||
double GetDoubleField(jfieldID fieldId);
|
||||
|
||||
void SetObjectField(jfieldID fieldId, const JniObject* value);
|
||||
void SetBooleanField(jfieldID fieldId, bool value);
|
||||
void SetByteField(jfieldID fieldId, int64_t value);
|
||||
void SetCharField(jfieldID fieldId, int64_t value);
|
||||
void SetShortField(jfieldID fieldId, int64_t value);
|
||||
void SetIntField(jfieldID fieldId, int64_t value);
|
||||
void SetLongField(jfieldID fieldId, int64_t value);
|
||||
void SetFloatField(jfieldID fieldId, double value);
|
||||
void SetDoubleField(jfieldID fieldId, double value);
|
||||
|
||||
ftl::RefPtr<JniObject> CallObjectMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
bool CallBooleanMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallByteMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallCharMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallShortMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallIntMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
int64_t CallLongMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
double CallFloatMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
double CallDoubleMethod(jmethodID methodId,
|
||||
const std::vector<Dart_Handle>& args);
|
||||
void CallVoidMethod(jmethodID methodId, const std::vector<Dart_Handle>& args);
|
||||
|
||||
protected:
|
||||
JniObject(JNIEnv* env, jobject object);
|
||||
|
||||
base::android::ScopedJavaGlobalRef<jobject> object_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_JNI_OBJECT_H_
|
||||
326
engine/src/flutter/lib/jni/jni_raw.dart
Normal file
326
engine/src/flutter/lib/jni/jni_raw.dart
Normal file
@@ -0,0 +1,326 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of dart_jni;
|
||||
|
||||
/// A group of methods which invoke Java Native Interface APIs from Dart.
|
||||
class JniApi {
|
||||
/// Return the field ID matching a `java.lang.reflect.Field` object.
|
||||
static int fromReflectedField(JniObject field)
|
||||
native 'JniApi_FromReflectedField';
|
||||
|
||||
/// Return the method ID matching a `java.lang.reflect.Method` object.
|
||||
static int fromReflectedMethod(JniObject method)
|
||||
native 'JniApi_FromReflectedMethod';
|
||||
|
||||
/// The `ApplicationContext` of this Android application.
|
||||
static JniObject getApplicationContext()
|
||||
native 'JniApi_GetApplicationContext';
|
||||
|
||||
/// The application's class loader.
|
||||
static JniObject getClassLoader()
|
||||
native 'JniApi_GetClassLoader';
|
||||
}
|
||||
|
||||
/// Low-level wrapper for a Java object accessed via JNI.
|
||||
/// These methods map directly to the corresponding JNI functions. See the JNI
|
||||
/// documentation for more information.
|
||||
class JniObject extends NativeFieldWrapperClass2 {
|
||||
JniClass getObjectClass()
|
||||
native 'JniObject_GetObjectClass';
|
||||
|
||||
JniObject getObjectField(int fieldId)
|
||||
native 'JniObject_GetObjectField';
|
||||
bool getBooleanField(int fieldId)
|
||||
native 'JniObject_GetBooleanField';
|
||||
int getByteField(int fieldId)
|
||||
native 'JniObject_GetByteField';
|
||||
int getCharField(int fieldId)
|
||||
native 'JniObject_GetCharField';
|
||||
int getShortField(int fieldId)
|
||||
native 'JniObject_GetShortField';
|
||||
int getIntField(int fieldId)
|
||||
native 'JniObject_GetIntField';
|
||||
int getLongField(int fieldId)
|
||||
native 'JniObject_GetLongField';
|
||||
double getFloatField(int fieldId)
|
||||
native 'JniObject_GetFloatField';
|
||||
double getDoubleField(int fieldId)
|
||||
native 'JniObject_GetDoubleField';
|
||||
|
||||
void setObjectField(int fieldId, JniObject value)
|
||||
native 'JniObject_SetObjectField';
|
||||
void setBooleanField(int fieldId, bool value)
|
||||
native 'JniObject_SetBooleanField';
|
||||
void setByteField(int fieldId, int value)
|
||||
native 'JniObject_SetByteField';
|
||||
void setCharField(int fieldId, int value)
|
||||
native 'JniObject_SetCharField';
|
||||
void setShortField(int fieldId, int value)
|
||||
native 'JniObject_SetShortField';
|
||||
void setIntField(int fieldId, int value)
|
||||
native 'JniObject_SetIntField';
|
||||
void setLongField(int fieldId, int value)
|
||||
native 'JniObject_SetLongField';
|
||||
void setFloatField(int fieldId, double value)
|
||||
native 'JniObject_SetFloatField';
|
||||
void setDoubleField(int fieldId, double value)
|
||||
native 'JniObject_SetDoubleField';
|
||||
|
||||
JniObject callObjectMethod(int methodId, List args)
|
||||
native 'JniObject_CallObjectMethod';
|
||||
bool callBooleanMethod(int methodId, List args)
|
||||
native 'JniObject_CallBooleanMethod';
|
||||
int callByteMethod(int methodId, List args)
|
||||
native 'JniObject_CallByteMethod';
|
||||
int callCharMethod(int methodId, List args)
|
||||
native 'JniObject_CallCharMethod';
|
||||
int callShortMethod(int methodId, List args)
|
||||
native 'JniObject_CallShortMethod';
|
||||
int callIntMethod(int methodId, List args)
|
||||
native 'JniObject_CallIntMethod';
|
||||
int callLongMethod(int methodId, List args)
|
||||
native 'JniObject_CallLongMethod';
|
||||
double callFloatMethod(int methodId, List args)
|
||||
native 'JniObject_CallFloatMethod';
|
||||
double callDoubleMethod(int methodId, List args)
|
||||
native 'JniObject_CallDoubleMethod';
|
||||
void callVoidMethod(int methodId, List args)
|
||||
native 'JniObject_CallVoidMethod';
|
||||
}
|
||||
|
||||
/// Low-level wrapper for a Java class accessed via JNI.
|
||||
/// These methods map directly to the corresponding JNI functions. See the JNI
|
||||
/// documentation for more information.
|
||||
class JniClass extends JniObject {
|
||||
/// Loads the Java class with the given fully qualified name.
|
||||
static JniClass fromName(String name)
|
||||
native 'JniClass_FromName';
|
||||
|
||||
/// Returns a wrapper for a `java.lang.Class` object.
|
||||
static JniClass fromClassObject(JniObject classObject)
|
||||
native 'JniClass_FromClassObject';
|
||||
|
||||
/// Returns a field ID for the instance field matching this name and type signature.
|
||||
/// See the JNI reference for explanation of type signatures.
|
||||
int getFieldId(String name, String sig)
|
||||
native 'JniClass_GetFieldId';
|
||||
|
||||
/// Returns a field ID for a static field.
|
||||
int getStaticFieldId(String name, String sig)
|
||||
native 'JniClass_GetStaticFieldId';
|
||||
|
||||
/// Returns a method ID for an instance method.
|
||||
int getMethodId(String name, String sig)
|
||||
native 'JniClass_GetMethodId';
|
||||
|
||||
/// Returns a method ID for a static method.
|
||||
int getStaticMethodId(String name, String sig)
|
||||
native 'JniClass_GetStaticMethodId';
|
||||
|
||||
/// Constructs an instance of the wrapped Java class..
|
||||
/// @param methodId The method ID of the constructor, obtained via getMethodId.
|
||||
/// @param args A list of argument values passed to the constructor. Each value should
|
||||
/// be a Dart number, bool, string, or [JniObject] instance.
|
||||
JniObject newObject(int methodId, List args)
|
||||
native 'JniClass_NewObject';
|
||||
|
||||
/// Returns true if objects of the wrapped Java class can be cast to the
|
||||
/// class described by the argument.
|
||||
bool isAssignable(JniClass clazz)
|
||||
native 'JniClass_IsAssignable';
|
||||
|
||||
JniObject getStaticObjectField(int fieldId)
|
||||
native 'JniClass_GetStaticObjectField';
|
||||
bool getStaticBooleanField(int fieldId)
|
||||
native 'JniClass_GetStaticBooleanField';
|
||||
int getStaticByteField(int fieldId)
|
||||
native 'JniClass_GetStaticByteField';
|
||||
int getStaticCharField(int fieldId)
|
||||
native 'JniClass_GetStaticCharField';
|
||||
int getStaticShortField(int fieldId)
|
||||
native 'JniClass_GetStaticShortField';
|
||||
int getStaticIntField(int fieldId)
|
||||
native 'JniClass_GetStaticIntField';
|
||||
int getStaticLongField(int fieldId)
|
||||
native 'JniClass_GetStaticLongField';
|
||||
double getStaticFloatField(int fieldId)
|
||||
native 'JniClass_GetStaticFloatField';
|
||||
double getStaticDoubleField(int fieldId)
|
||||
native 'JniClass_GetStaticDoubleField';
|
||||
|
||||
void setStaticObjectField(int fieldId, JniObject value)
|
||||
native 'JniClass_SetStaticObjectField';
|
||||
void setStaticBooleanField(int fieldId, bool value)
|
||||
native 'JniClass_SetStaticBooleanField';
|
||||
void setStaticByteField(int fieldId, int value)
|
||||
native 'JniClass_SetStaticByteField';
|
||||
void setStaticCharField(int fieldId, int value)
|
||||
native 'JniClass_SetStaticCharField';
|
||||
void setStaticShortField(int fieldId, int value)
|
||||
native 'JniClass_SetStaticShortField';
|
||||
void setStaticIntField(int fieldId, int value)
|
||||
native 'JniClass_SetStaticIntField';
|
||||
void setStaticLongField(int fieldId, int value)
|
||||
native 'JniClass_SetStaticLongField';
|
||||
void setStaticFloatField(int fieldId, double value)
|
||||
native 'JniClass_SetStaticFloatField';
|
||||
void setStaticDoubleField(int fieldId, double value)
|
||||
native 'JniClass_SetStaticDoubleField';
|
||||
|
||||
JniObject callStaticObjectMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticObjectMethod';
|
||||
bool callStaticBooleanMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticBooleanMethod';
|
||||
int callStaticByteMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticByteMethod';
|
||||
int callStaticCharMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticCharMethod';
|
||||
int callStaticShortMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticShortMethod';
|
||||
int callStaticIntMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticIntMethod';
|
||||
int callStaticLongMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticLongMethod';
|
||||
double callStaticFloatMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticFloatMethod';
|
||||
double callStaticDoubleMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticDoubleMethod';
|
||||
void callStaticVoidMethod(int methodId, List args)
|
||||
native 'JniClass_CallStaticVoidMethod';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java string.
|
||||
class JniString extends JniObject {
|
||||
/// Construct a Java string from a Dart string.
|
||||
static JniString create(String value)
|
||||
native 'JniString_Create';
|
||||
|
||||
/// Retrieve the value of the Java string represented by this object as a Dart string.
|
||||
String get text native 'JniString_GetText';
|
||||
|
||||
/// Convert a JniObject representing a Java string to a Dart string.
|
||||
static String unwrap(JniObject object) => (object as JniString).text;
|
||||
}
|
||||
|
||||
/// Wrapper for a Java array.
|
||||
class JniArray extends JniObject {
|
||||
int get length native 'JniArray_GetLength';
|
||||
|
||||
void set length(int value) { throw new UnsupportedError("Not supported."); }
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `Object` array.
|
||||
class JniObjectArray extends JniArray with ListMixin<JniObject> {
|
||||
static JniObjectArray create(JniClass clazz, int length)
|
||||
native 'JniObjectArray_Create';
|
||||
|
||||
JniObject operator [](int index)
|
||||
native 'JniObjectArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, JniObject value)
|
||||
native 'JniObjectArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `boolean` array.
|
||||
class JniBooleanArray extends JniArray with ListMixin<bool> {
|
||||
static JniBooleanArray create(int length)
|
||||
native 'JniBooleanArray_Create';
|
||||
|
||||
bool operator [](int index)
|
||||
native 'JniBooleanArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, bool value)
|
||||
native 'JniBooleanArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `byte` array.
|
||||
class JniByteArray extends JniArray with ListMixin<int> {
|
||||
static JniByteArray create(int length)
|
||||
native 'JniByteArray_Create';
|
||||
|
||||
int operator [](int index)
|
||||
native 'JniByteArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, int value)
|
||||
native 'JniByteArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `char` array.
|
||||
class JniCharArray extends JniArray with ListMixin<int> {
|
||||
static JniCharArray create(int length)
|
||||
native 'JniCharArray_Create';
|
||||
|
||||
int operator [](int index)
|
||||
native 'JniCharArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, int value)
|
||||
native 'JniCharArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `short` array.
|
||||
class JniShortArray extends JniArray with ListMixin<int> {
|
||||
static JniShortArray create(int length)
|
||||
native 'JniShortArray_Create';
|
||||
|
||||
int operator [](int index)
|
||||
native 'JniShortArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, int value)
|
||||
native 'JniShortArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `int` array.
|
||||
class JniIntArray extends JniArray with ListMixin<int> {
|
||||
static JniIntArray create(int length)
|
||||
native 'JniIntArray_Create';
|
||||
|
||||
int operator [](int index)
|
||||
native 'JniIntArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, int value)
|
||||
native 'JniIntArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `long` array.
|
||||
class JniLongArray extends JniArray with ListMixin<int> {
|
||||
static JniLongArray create(int length)
|
||||
native 'JniLongArray_Create';
|
||||
|
||||
int operator [](int index)
|
||||
native 'JniLongArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, int value)
|
||||
native 'JniLongArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `float` array.
|
||||
class JniFloatArray extends JniArray with ListMixin<double> {
|
||||
static JniFloatArray create(int length)
|
||||
native 'JniFloatArray_Create';
|
||||
|
||||
double operator [](int index)
|
||||
native 'JniFloatArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, double value)
|
||||
native 'JniFloatArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Wrapper for a Java `double` array.
|
||||
class JniDoubleArray extends JniArray with ListMixin<double> {
|
||||
static JniDoubleArray create(int length)
|
||||
native 'JniDoubleArray_Create';
|
||||
|
||||
double operator [](int index)
|
||||
native 'JniDoubleArray_GetArrayElement';
|
||||
|
||||
void operator []=(int index, double value)
|
||||
native 'JniDoubleArray_SetArrayElement';
|
||||
}
|
||||
|
||||
/// Used to pass arguments of type "float" to Java methods.
|
||||
class JniFloat {
|
||||
final double value;
|
||||
JniFloat(this.value);
|
||||
}
|
||||
61
engine/src/flutter/lib/jni/jni_string.cc
Normal file
61
engine/src/flutter/lib/jni/jni_string.cc
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/jni/jni_string.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(jni, JniString);
|
||||
|
||||
JniString::JniString(JNIEnv* env, jstring object) : JniObject(env, object) {}
|
||||
|
||||
JniString::~JniString() {}
|
||||
|
||||
jstring JniString::java_string() {
|
||||
return static_cast<jstring>(java_object());
|
||||
}
|
||||
|
||||
ftl::RefPtr<JniString> JniString::Create(Dart_Handle dart_string) {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jstring java_string =
|
||||
DartJni::DartToJavaString(env, dart_string, &exception);
|
||||
if (exception)
|
||||
goto fail;
|
||||
|
||||
return ftl::MakeRefCounted<JniString>(env, java_string);
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Dart_Handle JniString::GetText() {
|
||||
Dart_Handle exception = nullptr;
|
||||
{
|
||||
ENTER_JNI();
|
||||
|
||||
jsize length = env->GetStringLength(java_string());
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
const jchar* chars = env->GetStringChars(java_string(), NULL);
|
||||
if (CheckJniException(env, &exception))
|
||||
goto fail;
|
||||
|
||||
Dart_Handle result = Dart_NewStringFromUTF16(chars, length);
|
||||
env->ReleaseStringChars(java_string(), chars);
|
||||
|
||||
return result;
|
||||
}
|
||||
fail:
|
||||
Dart_ThrowException(exception);
|
||||
FTL_NOTREACHED();
|
||||
return Dart_Null();
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
33
engine/src/flutter/lib/jni/jni_string.h
Normal file
33
engine/src/flutter/lib/jni/jni_string.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_JNI_JNI_STRING_H_
|
||||
#define FLUTTER_LIB_JNI_JNI_STRING_H_
|
||||
|
||||
#include "flutter/lib/jni/dart_jni.h"
|
||||
#include "flutter/lib/jni/jni_object.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
// Wrapper for a JNI string
|
||||
class JniString : public JniObject {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
friend class JniObject;
|
||||
FRIEND_MAKE_REF_COUNTED(JniString);
|
||||
|
||||
public:
|
||||
~JniString() override;
|
||||
|
||||
static ftl::RefPtr<JniString> Create(Dart_Handle dart_string);
|
||||
Dart_Handle GetText();
|
||||
|
||||
private:
|
||||
JniString(JNIEnv* env, jstring string);
|
||||
|
||||
jstring java_string();
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_JNI_JNI_STRING_H_
|
||||
55
engine/src/flutter/lib/ui/BUILD.gn
Normal file
55
engine/src/flutter/lib/ui/BUILD.gn
Normal file
@@ -0,0 +1,55 @@
|
||||
# Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
source_set("ui") {
|
||||
sources = [
|
||||
"compositing/scene_builder.cc",
|
||||
"compositing/scene_builder.h",
|
||||
"compositing/scene.cc",
|
||||
"compositing/scene.h",
|
||||
"painting/canvas.cc",
|
||||
"painting/canvas.h",
|
||||
"painting/gradient.cc",
|
||||
"painting/gradient.h",
|
||||
"painting/image.cc",
|
||||
"painting/image_decoding.cc",
|
||||
"painting/image_decoding.h",
|
||||
"painting/image_filter.cc",
|
||||
"painting/image_filter.h",
|
||||
"painting/image.h",
|
||||
"painting/image_shader.cc",
|
||||
"painting/image_shader.h",
|
||||
"painting/mask_filter.cc",
|
||||
"painting/mask_filter.h",
|
||||
"painting/matrix.cc",
|
||||
"painting/matrix.h",
|
||||
"painting/paint.cc",
|
||||
"painting/paint.h",
|
||||
"painting/path.cc",
|
||||
"painting/path.h",
|
||||
"painting/picture_recorder.cc",
|
||||
"painting/picture_recorder.h",
|
||||
"painting/picture.cc",
|
||||
"painting/picture.h",
|
||||
"painting/resource_context.cc",
|
||||
"painting/resource_context.h",
|
||||
"painting/rrect.cc",
|
||||
"painting/rrect.h",
|
||||
"painting/shader.cc",
|
||||
"painting/shader.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//flutter/flow",
|
||||
"//flutter/tonic",
|
||||
"//flutter/glue",
|
||||
"//lib/tonic",
|
||||
"//flutter/skia",
|
||||
|
||||
# For image_decoding:
|
||||
"//flutter/sky/engine/platform",
|
||||
"//flutter/sky/engine/wtf",
|
||||
]
|
||||
}
|
||||
230
engine/src/flutter/lib/ui/compositing.dart
Normal file
230
engine/src/flutter/lib/ui/compositing.dart
Normal file
@@ -0,0 +1,230 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
part of dart_ui;
|
||||
|
||||
/// An opaque object representing a composited scene.
|
||||
///
|
||||
/// To create a Scene object, use a [SceneBuilder].
|
||||
///
|
||||
/// Scene objects can be displayed on the screen using the
|
||||
/// [Window.render] method.
|
||||
class Scene extends NativeFieldWrapperClass2 {
|
||||
/// Creates an uninitialized Scene object.
|
||||
///
|
||||
/// Calling the Scene constructor directly will not create a useable
|
||||
/// object. To create a Scene object, use a [SceneBuilder].
|
||||
Scene(); // (this constructor is here just so we can document it)
|
||||
|
||||
/// Releases the resources used by this scene.
|
||||
///
|
||||
/// After calling this function, the scene is cannot be used further.
|
||||
void dispose() native "Scene_dispose";
|
||||
}
|
||||
|
||||
/// Builds a [Scene] containing the given visuals.
|
||||
///
|
||||
/// A [Scene] can then be rendered using [Window.render].
|
||||
///
|
||||
/// To draw graphical operations onto a [Scene], first create a
|
||||
/// [Picture] using a [PictureRecorder] and a [Canvas], and then add
|
||||
/// it to the scene using [addPicture].
|
||||
class SceneBuilder extends NativeFieldWrapperClass2 {
|
||||
/// Creates an empty [SceneBuilder] object.
|
||||
SceneBuilder() { _constructor(); }
|
||||
void _constructor() native "SceneBuilder_constructor";
|
||||
|
||||
/// Pushes a transform operation onto the operation stack.
|
||||
///
|
||||
/// The objects are transformed by the given matrix before rasterization.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushTransform(Float64List matrix4) {
|
||||
if (matrix4 == null)
|
||||
throw new ArgumentError("[matrix4] argument cannot be null");
|
||||
if (matrix4.length != 16)
|
||||
throw new ArgumentError("[matrix4] must have 16 entries.");
|
||||
_pushTransform(matrix4);
|
||||
}
|
||||
void _pushTransform(Float64List matrix4) native "SceneBuilder_pushTransform";
|
||||
|
||||
/// Pushes a rectangular clip operation onto the operation stack.
|
||||
///
|
||||
/// Rasterization outside the given rectangle is discarded.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushClipRect(Rect rect) {
|
||||
_pushClipRect(rect.left, rect.right, rect.top, rect.bottom);
|
||||
}
|
||||
void _pushClipRect(double left,
|
||||
double right,
|
||||
double top,
|
||||
double bottom) native "SceneBuilder_pushClipRect";
|
||||
|
||||
/// Pushes a rounded-rectangular clip operation onto the operation stack.
|
||||
///
|
||||
/// Rasterization outside the given rounded rectangle is discarded.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushClipRRect(RRect rrect) => _pushClipRRect(rrect._value);
|
||||
void _pushClipRRect(Float32List rrect) native "SceneBuilder_pushClipRRect";
|
||||
|
||||
/// Pushes a path clip operation onto the operation stack.
|
||||
///
|
||||
/// Rasterization outside the given path is discarded.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushClipPath(Path path) native "SceneBuilder_pushClipPath";
|
||||
|
||||
/// Pushes an opacity operation onto the operation stack.
|
||||
///
|
||||
/// The given alpha value is blended into the alpha value of the objects'
|
||||
/// rasterization. An alpha value of 0 makes the objects entirely invisible.
|
||||
/// An alpha value of 255 has no effect (i.e., the objects retain the current
|
||||
/// opacity).
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushOpacity(int alpha) native "SceneBuilder_pushOpacity";
|
||||
|
||||
/// Pushes a color filter operation onto the operation stack.
|
||||
///
|
||||
/// The given color is applied to the objects' rasterization using the given
|
||||
/// transfer mode.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushColorFilter(Color color, TransferMode transferMode) {
|
||||
_pushColorFilter(color.value, transferMode.index);
|
||||
}
|
||||
void _pushColorFilter(int color, int transferMode) native "SceneBuilder_pushColorFilter";
|
||||
|
||||
/// Pushes a backdrop filter operation onto the operation stack.
|
||||
///
|
||||
/// The given filter is applied to the current contents of the scene prior to
|
||||
/// rasterizing the given objects.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushBackdropFilter(ImageFilter filter) native "SceneBuilder_pushBackdropFilter";
|
||||
|
||||
/// Pushes a shader mask operation onto the operation stack.
|
||||
///
|
||||
/// The given shader is applied to the object's rasterization in the given
|
||||
/// rectangle using the given transfer mode.
|
||||
///
|
||||
/// See [pop] for details about the operation stack.
|
||||
void pushShaderMask(Shader shader, Rect maskRect, TransferMode transferMode) {
|
||||
_pushShaderMask(shader,
|
||||
maskRect.left,
|
||||
maskRect.right,
|
||||
maskRect.top,
|
||||
maskRect.bottom,
|
||||
transferMode.index);
|
||||
}
|
||||
void _pushShaderMask(Shader shader,
|
||||
double maskRectLeft,
|
||||
double maskRectRight,
|
||||
double maskRectTop,
|
||||
double maskRectBottom,
|
||||
int transferMode) native "SceneBuilder_pushShaderMask";
|
||||
|
||||
/// Ends the effect of the most recently pushed operation.
|
||||
///
|
||||
/// Internally the scene builder maintains a stack of operations. Each of the
|
||||
/// operations in the stack applies to each of the objects added to the scene.
|
||||
/// Calling this function removes the most recently added operation from the
|
||||
/// stack.
|
||||
void pop() native "SceneBuilder_pop";
|
||||
|
||||
/// Adds an object to the scene that displays performance statistics.
|
||||
///
|
||||
/// Useful during development to assess the performance of the application.
|
||||
/// The enabledOptions controls which statistics are displayed. The bounds
|
||||
/// controls where the statistics are displayed.
|
||||
///
|
||||
/// enabledOptions is a bit field with the following bits defined:
|
||||
/// - 0x01: displayRasterizerStatistics - show GPU thread frame time
|
||||
/// - 0x02: visualizeRasterizerStatistics - graph GPU thread frame times
|
||||
/// - 0x04: displayEngineStatistics - show UI thread frame time
|
||||
/// - 0x08: visualizeEngineStatistics - graph UI thread frame times
|
||||
/// Set enabledOptions to 0x0F to enable all the currently defined features.
|
||||
///
|
||||
/// The "UI thread" is the thread that includes all the execution of
|
||||
/// the main Dart isolate (the isolate that can call
|
||||
/// [Window.render]). The UI thread frame time is the total time
|
||||
/// spent executing the [Window.onBeginFrame] callback. The "GPU
|
||||
/// thread" is the thread (running on the CPU) that subsequently
|
||||
/// processes the [Scene] provided by the Dart code to turn it into
|
||||
/// GPU commands and send it to the GPU.
|
||||
///
|
||||
/// See also the [PerformanceOverlayOption] enum in the rendering library.
|
||||
/// for more details.
|
||||
// Values above must match constants in //engine/src/sky/compositor/performance_overlay_layer.h
|
||||
void addPerformanceOverlay(int enabledOptions, Rect bounds) {
|
||||
_addPerformanceOverlay(enabledOptions,
|
||||
bounds.left,
|
||||
bounds.right,
|
||||
bounds.top,
|
||||
bounds.bottom);
|
||||
}
|
||||
void _addPerformanceOverlay(int enabledOptions,
|
||||
double left,
|
||||
double right,
|
||||
double top,
|
||||
double bottom) native "SceneBuilder_addPerformanceOverlay";
|
||||
|
||||
/// Adds a [Picture] to the scene.
|
||||
///
|
||||
/// The picture is rasterized at the given offset.
|
||||
void addPicture(Offset offset, Picture picture, { bool isComplexHint: false, bool willChangeHint: false }) {
|
||||
int hints = 0;
|
||||
if (isComplexHint)
|
||||
hints |= 1;
|
||||
if (willChangeHint)
|
||||
hints |= 2;
|
||||
_addPicture(offset.dx, offset.dy, picture, hints);
|
||||
}
|
||||
void _addPicture(double dx, double dy, Picture picture, int hints) native "SceneBuilder_addPicture";
|
||||
|
||||
/// (mojo-only) Adds a scene rendered by another application to the scene for
|
||||
/// this application.
|
||||
///
|
||||
/// Applications typically obtain scene tokens when embedding other views via
|
||||
/// the Mojo view manager, but this function is agnostic as to the source of
|
||||
/// scene token.
|
||||
void addChildScene(Offset offset,
|
||||
double devicePixelRatio,
|
||||
int physicalWidth,
|
||||
int physicalHeight,
|
||||
int sceneToken) {
|
||||
_addChildScene(offset.dx,
|
||||
offset.dy,
|
||||
devicePixelRatio,
|
||||
physicalWidth,
|
||||
physicalHeight,
|
||||
sceneToken);
|
||||
}
|
||||
void _addChildScene(double dx,
|
||||
double dy,
|
||||
double devicePixelRatio,
|
||||
int physicalWidth,
|
||||
int physicalHeight,
|
||||
int sceneToken) native "SceneBuilder_addChildScene";
|
||||
|
||||
/// Sets a threshold after which additional debugging information should be recorded.
|
||||
///
|
||||
/// Currently this interface is difficult to use by end-developers. If you're
|
||||
/// interested in using this feature, please contact [flutter-dev](https://groups.google.com/forum/#!forum/flutter-dev).
|
||||
/// We'll hopefully be able to figure out how to make this feature more useful
|
||||
/// to you.
|
||||
void setRasterizerTracingThreshold(int frameInterval) native "SceneBuilder_setRasterizerTracingThreshold";
|
||||
|
||||
/// Finishes building the scene.
|
||||
///
|
||||
/// Returns a [Scene] containing the objects that have been added to
|
||||
/// this scene builder. The [Scene] can then be displayed on the
|
||||
/// screen with [Window.render].
|
||||
///
|
||||
/// After calling this function, the scene builder object is invalid and
|
||||
/// cannot be used further.
|
||||
Scene build() native "SceneBuilder_build";
|
||||
}
|
||||
45
engine/src/flutter/lib/ui/compositing/scene.cc
Normal file
45
engine/src/flutter/lib/ui/compositing/scene.cc
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/compositing/scene.h"
|
||||
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "third_party/skia/include/core/SkPictureRecorder.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene);
|
||||
|
||||
#define FOR_EACH_BINDING(V) V(Scene, dispose)
|
||||
|
||||
DART_BIND_ALL(Scene, FOR_EACH_BINDING)
|
||||
|
||||
ftl::RefPtr<Scene> Scene::create(std::unique_ptr<flow::Layer> rootLayer,
|
||||
uint32_t rasterizerTracingThreshold) {
|
||||
return ftl::MakeRefCounted<Scene>(std::move(rootLayer),
|
||||
rasterizerTracingThreshold);
|
||||
}
|
||||
|
||||
Scene::Scene(std::unique_ptr<flow::Layer> rootLayer,
|
||||
uint32_t rasterizerTracingThreshold)
|
||||
: m_layerTree(new flow::LayerTree()) {
|
||||
m_layerTree->set_root_layer(std::move(rootLayer));
|
||||
m_layerTree->set_rasterizer_tracing_threshold(rasterizerTracingThreshold);
|
||||
}
|
||||
|
||||
Scene::~Scene() {}
|
||||
|
||||
void Scene::dispose() {
|
||||
ClearDartWrapper();
|
||||
}
|
||||
|
||||
std::unique_ptr<flow::LayerTree> Scene::takeLayerTree() {
|
||||
return std::move(m_layerTree);
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
46
engine/src/flutter/lib/ui/compositing/scene.h
Normal file
46
engine/src/flutter/lib/ui/compositing/scene.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_COMPOSITING_SCENE_H_
|
||||
#define FLUTTER_LIB_UI_COMPOSITING_SCENE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/flow/layers/layer_tree.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "third_party/skia/include/core/SkPicture.h"
|
||||
|
||||
namespace tonic {
|
||||
class DartLibraryNatives;
|
||||
} // namespace tonic
|
||||
|
||||
namespace blink {
|
||||
|
||||
class Scene : public ftl::RefCountedThreadSafe<Scene>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(Scene);
|
||||
|
||||
public:
|
||||
~Scene() override;
|
||||
static ftl::RefPtr<Scene> create(std::unique_ptr<flow::Layer> rootLayer,
|
||||
uint32_t rasterizerTracingThreshold);
|
||||
|
||||
std::unique_ptr<flow::LayerTree> takeLayerTree();
|
||||
|
||||
void dispose();
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
explicit Scene(std::unique_ptr<flow::Layer> rootLayer,
|
||||
uint32_t rasterizerTracingThreshold);
|
||||
|
||||
std::unique_ptr<flow::LayerTree> m_layerTree;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_COMPOSITING_SCENE_H_
|
||||
206
engine/src/flutter/lib/ui/compositing/scene_builder.cc
Normal file
206
engine/src/flutter/lib/ui/compositing/scene_builder.cc
Normal file
@@ -0,0 +1,206 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/compositing/scene_builder.h"
|
||||
|
||||
#include "flutter/flow/layers/backdrop_filter_layer.h"
|
||||
#include "flutter/flow/layers/child_scene_layer.h"
|
||||
#include "flutter/flow/layers/clip_path_layer.h"
|
||||
#include "flutter/flow/layers/clip_rect_layer.h"
|
||||
#include "flutter/flow/layers/clip_rrect_layer.h"
|
||||
#include "flutter/flow/layers/color_filter_layer.h"
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
#include "flutter/flow/layers/opacity_layer.h"
|
||||
#include "flutter/flow/layers/performance_overlay_layer.h"
|
||||
#include "flutter/flow/layers/picture_layer.h"
|
||||
#include "flutter/flow/layers/shader_mask_layer.h"
|
||||
#include "flutter/flow/layers/transform_layer.h"
|
||||
#include "flutter/lib/ui/painting/matrix.h"
|
||||
#include "flutter/lib/ui/painting/shader.h"
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
#include "third_party/skia/include/core/SkColorFilter.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
static void SceneBuilder_constructor(Dart_NativeArguments args) {
|
||||
DartCallConstructor(&SceneBuilder::create, args);
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, SceneBuilder);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(SceneBuilder, pushTransform) \
|
||||
V(SceneBuilder, pushClipRect) \
|
||||
V(SceneBuilder, pushClipRRect) \
|
||||
V(SceneBuilder, pushClipPath) \
|
||||
V(SceneBuilder, pushOpacity) \
|
||||
V(SceneBuilder, pushColorFilter) \
|
||||
V(SceneBuilder, pushBackdropFilter) \
|
||||
V(SceneBuilder, pushShaderMask) \
|
||||
V(SceneBuilder, pop) \
|
||||
V(SceneBuilder, addPicture) \
|
||||
V(SceneBuilder, addChildScene) \
|
||||
V(SceneBuilder, addPerformanceOverlay) \
|
||||
V(SceneBuilder, setRasterizerTracingThreshold) \
|
||||
V(SceneBuilder, build)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void SceneBuilder::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register(
|
||||
{{"SceneBuilder_constructor", SceneBuilder_constructor, 1, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
SceneBuilder::SceneBuilder()
|
||||
: m_currentLayer(nullptr), m_currentRasterizerTracingThreshold(0) {}
|
||||
|
||||
SceneBuilder::~SceneBuilder() {}
|
||||
|
||||
void SceneBuilder::pushTransform(const tonic::Float64List& matrix4) {
|
||||
SkMatrix sk_matrix = ToSkMatrix(matrix4);
|
||||
std::unique_ptr<flow::TransformLayer> layer(new flow::TransformLayer());
|
||||
layer->set_transform(sk_matrix);
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushClipRect(double left,
|
||||
double right,
|
||||
double top,
|
||||
double bottom) {
|
||||
std::unique_ptr<flow::ClipRectLayer> layer(new flow::ClipRectLayer());
|
||||
layer->set_clip_rect(SkRect::MakeLTRB(left, top, right, bottom));
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushClipRRect(const RRect& rrect) {
|
||||
std::unique_ptr<flow::ClipRRectLayer> layer(new flow::ClipRRectLayer());
|
||||
layer->set_clip_rrect(rrect.sk_rrect);
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushClipPath(const CanvasPath* path) {
|
||||
std::unique_ptr<flow::ClipPathLayer> layer(new flow::ClipPathLayer());
|
||||
layer->set_clip_path(path->path());
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushOpacity(int alpha) {
|
||||
std::unique_ptr<flow::OpacityLayer> layer(new flow::OpacityLayer());
|
||||
layer->set_alpha(alpha);
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushColorFilter(int color, int transferMode) {
|
||||
std::unique_ptr<flow::ColorFilterLayer> layer(new flow::ColorFilterLayer());
|
||||
layer->set_color(static_cast<SkColor>(color));
|
||||
layer->set_transfer_mode(static_cast<SkXfermode::Mode>(transferMode));
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushBackdropFilter(ImageFilter* filter) {
|
||||
std::unique_ptr<flow::BackdropFilterLayer> layer(
|
||||
new flow::BackdropFilterLayer());
|
||||
layer->set_filter(filter->filter());
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::pushShaderMask(Shader* shader,
|
||||
double maskRectLeft,
|
||||
double maskRectRight,
|
||||
double maskRectTop,
|
||||
double maskRectBottom,
|
||||
int transferMode) {
|
||||
std::unique_ptr<flow::ShaderMaskLayer> layer(new flow::ShaderMaskLayer());
|
||||
layer->set_shader(shader->shader());
|
||||
layer->set_mask_rect(SkRect::MakeLTRB(maskRectLeft, maskRectTop,
|
||||
maskRectRight, maskRectBottom));
|
||||
layer->set_transfer_mode(static_cast<SkXfermode::Mode>(transferMode));
|
||||
addLayer(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::addLayer(std::unique_ptr<flow::ContainerLayer> layer) {
|
||||
DCHECK(layer);
|
||||
if (!m_rootLayer) {
|
||||
DCHECK(!m_currentLayer);
|
||||
m_rootLayer = std::move(layer);
|
||||
m_currentLayer = m_rootLayer.get();
|
||||
return;
|
||||
}
|
||||
if (!m_currentLayer)
|
||||
return;
|
||||
flow::ContainerLayer* newLayer = layer.get();
|
||||
m_currentLayer->Add(std::move(layer));
|
||||
m_currentLayer = newLayer;
|
||||
}
|
||||
|
||||
void SceneBuilder::pop() {
|
||||
if (!m_currentLayer)
|
||||
return;
|
||||
m_currentLayer = m_currentLayer->parent();
|
||||
}
|
||||
|
||||
void SceneBuilder::addPicture(double dx,
|
||||
double dy,
|
||||
Picture* picture,
|
||||
int hints) {
|
||||
if (!m_currentLayer)
|
||||
return;
|
||||
std::unique_ptr<flow::PictureLayer> layer(new flow::PictureLayer());
|
||||
layer->set_offset(SkPoint::Make(dx, dy));
|
||||
layer->set_picture(picture->picture());
|
||||
layer->set_is_complex(!!(hints & 1));
|
||||
layer->set_will_change(!!(hints & 2));
|
||||
m_currentLayer->Add(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::addChildScene(double dx,
|
||||
double dy,
|
||||
double devicePixelRatio,
|
||||
int physicalWidth,
|
||||
int physicalHeight,
|
||||
uint32_t sceneToken) {
|
||||
if (!m_currentLayer)
|
||||
return;
|
||||
std::unique_ptr<flow::ChildSceneLayer> layer(new flow::ChildSceneLayer());
|
||||
layer->set_offset(SkPoint::Make(dx, dy));
|
||||
layer->set_device_pixel_ratio(devicePixelRatio);
|
||||
layer->set_physical_size(SkISize::Make(physicalWidth, physicalHeight));
|
||||
mojo::gfx::composition::SceneTokenPtr token =
|
||||
mojo::gfx::composition::SceneToken::New();
|
||||
token->value = sceneToken;
|
||||
layer->set_scene_token(token.Pass());
|
||||
m_currentLayer->Add(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::addPerformanceOverlay(uint64_t enabledOptions,
|
||||
double left,
|
||||
double right,
|
||||
double top,
|
||||
double bottom) {
|
||||
if (!m_currentLayer)
|
||||
return;
|
||||
std::unique_ptr<flow::PerformanceOverlayLayer> layer(
|
||||
new flow::PerformanceOverlayLayer(enabledOptions));
|
||||
layer->set_paint_bounds(SkRect::MakeLTRB(left, top, right, bottom));
|
||||
m_currentLayer->Add(std::move(layer));
|
||||
}
|
||||
|
||||
void SceneBuilder::setRasterizerTracingThreshold(uint32_t frameInterval) {
|
||||
m_currentRasterizerTracingThreshold = frameInterval;
|
||||
}
|
||||
|
||||
ftl::RefPtr<Scene> SceneBuilder::build() {
|
||||
m_currentLayer = nullptr;
|
||||
int32_t threshold = m_currentRasterizerTracingThreshold;
|
||||
m_currentRasterizerTracingThreshold = 0;
|
||||
ftl::RefPtr<Scene> scene = Scene::create(std::move(m_rootLayer), threshold);
|
||||
ClearDartWrapper();
|
||||
return scene;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
82
engine/src/flutter/lib/ui/compositing/scene_builder.h
Normal file
82
engine/src/flutter/lib/ui/compositing/scene_builder.h
Normal file
@@ -0,0 +1,82 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_COMPOSITING_SCENE_BUILDER_H_
|
||||
#define FLUTTER_LIB_UI_COMPOSITING_SCENE_BUILDER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/flow/layers/container_layer.h"
|
||||
#include "flutter/lib/ui/compositing/scene.h"
|
||||
#include "flutter/lib/ui/painting/image_filter.h"
|
||||
#include "flutter/lib/ui/painting/path.h"
|
||||
#include "flutter/lib/ui/painting/picture.h"
|
||||
#include "flutter/lib/ui/painting/rrect.h"
|
||||
#include "flutter/lib/ui/painting/shader.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "lib/tonic/typed_data/float64_list.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class SceneBuilder : public ftl::RefCountedThreadSafe<SceneBuilder>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(SceneBuilder);
|
||||
|
||||
public:
|
||||
static ftl::RefPtr<SceneBuilder> create() {
|
||||
return ftl::MakeRefCounted<SceneBuilder>();
|
||||
}
|
||||
|
||||
~SceneBuilder() override;
|
||||
|
||||
void pushTransform(const tonic::Float64List& matrix4);
|
||||
void pushClipRect(double left, double right, double top, double bottom);
|
||||
void pushClipRRect(const RRect& rrect);
|
||||
void pushClipPath(const CanvasPath* path);
|
||||
void pushOpacity(int alpha);
|
||||
void pushColorFilter(int color, int transferMode);
|
||||
void pushBackdropFilter(ImageFilter* filter);
|
||||
void pushShaderMask(Shader* shader,
|
||||
double maskRectLeft,
|
||||
double maskRectRight,
|
||||
double maskRectTop,
|
||||
double maskRectBottom,
|
||||
int transferMode);
|
||||
void pop();
|
||||
|
||||
void addPerformanceOverlay(uint64_t enabledOptions,
|
||||
double left,
|
||||
double right,
|
||||
double top,
|
||||
double bottom);
|
||||
void addPicture(double dx, double dy, Picture* picture, int hints);
|
||||
void addChildScene(double dx,
|
||||
double dy,
|
||||
double devicePixelRatio,
|
||||
int physicalWidth,
|
||||
int physicalHeight,
|
||||
uint32_t sceneToken);
|
||||
|
||||
void setRasterizerTracingThreshold(uint32_t frameInterval);
|
||||
|
||||
ftl::RefPtr<Scene> build();
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
explicit SceneBuilder();
|
||||
|
||||
void addLayer(std::unique_ptr<flow::ContainerLayer> layer);
|
||||
|
||||
std::unique_ptr<flow::ContainerLayer> m_rootLayer;
|
||||
flow::ContainerLayer* m_currentLayer;
|
||||
int32_t m_currentRasterizerTracingThreshold;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_COMPOSITING_SCENE_BUILDER_H_
|
||||
12
engine/src/flutter/lib/ui/dart_ui.gni
Normal file
12
engine/src/flutter/lib/ui/dart_ui.gni
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
dart_ui_files = [
|
||||
"//flutter/lib/ui/compositing.dart",
|
||||
"//flutter/lib/ui/geometry.dart",
|
||||
"//flutter/lib/ui/painting.dart",
|
||||
"//flutter/lib/ui/ui.dart",
|
||||
]
|
||||
|
||||
dart_ui_path = "//flutter/lib/ui/ui.dart"
|
||||
1362
engine/src/flutter/lib/ui/geometry.dart
Normal file
1362
engine/src/flutter/lib/ui/geometry.dart
Normal file
File diff suppressed because it is too large
Load Diff
1542
engine/src/flutter/lib/ui/painting.dart
Normal file
1542
engine/src/flutter/lib/ui/painting.dart
Normal file
File diff suppressed because it is too large
Load Diff
398
engine/src/flutter/lib/ui/painting/canvas.cc
Normal file
398
engine/src/flutter/lib/ui/painting/canvas.cc
Normal file
@@ -0,0 +1,398 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/painting/canvas.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "flutter/lib/ui/painting/image.h"
|
||||
#include "flutter/lib/ui/painting/matrix.h"
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
static void Canvas_constructor(Dart_NativeArguments args) {
|
||||
DartCallConstructor(&Canvas::Create, args);
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(Canvas, save) \
|
||||
V(Canvas, saveLayerWithoutBounds) \
|
||||
V(Canvas, saveLayer) \
|
||||
V(Canvas, restore) \
|
||||
V(Canvas, getSaveCount) \
|
||||
V(Canvas, translate) \
|
||||
V(Canvas, scale) \
|
||||
V(Canvas, rotate) \
|
||||
V(Canvas, skew) \
|
||||
V(Canvas, transform) \
|
||||
V(Canvas, setMatrix) \
|
||||
V(Canvas, clipRect) \
|
||||
V(Canvas, clipRRect) \
|
||||
V(Canvas, clipPath) \
|
||||
V(Canvas, drawColor) \
|
||||
V(Canvas, drawLine) \
|
||||
V(Canvas, drawPaint) \
|
||||
V(Canvas, drawRect) \
|
||||
V(Canvas, drawRRect) \
|
||||
V(Canvas, drawDRRect) \
|
||||
V(Canvas, drawOval) \
|
||||
V(Canvas, drawCircle) \
|
||||
V(Canvas, drawPath) \
|
||||
V(Canvas, drawImage) \
|
||||
V(Canvas, drawImageRect) \
|
||||
V(Canvas, drawImageNine) \
|
||||
V(Canvas, drawPicture) \
|
||||
V(Canvas, drawPoints) \
|
||||
V(Canvas, drawVertices) \
|
||||
V(Canvas, drawAtlas)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void Canvas::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register({{"Canvas_constructor", Canvas_constructor, 6, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
ftl::RefPtr<Canvas> Canvas::Create(PictureRecorder* recorder,
|
||||
double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom) {
|
||||
FTL_DCHECK(recorder);
|
||||
FTL_DCHECK(!recorder->isRecording());
|
||||
ftl::RefPtr<Canvas> canvas = ftl::MakeRefCounted<Canvas>(
|
||||
recorder->BeginRecording(SkRect::MakeLTRB(left, top, right, bottom)));
|
||||
recorder->set_canvas(canvas);
|
||||
return canvas;
|
||||
}
|
||||
|
||||
Canvas::Canvas(SkCanvas* canvas) : canvas_(canvas) {}
|
||||
|
||||
Canvas::~Canvas() {}
|
||||
|
||||
void Canvas::save() {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->save();
|
||||
}
|
||||
|
||||
void Canvas::saveLayerWithoutBounds(const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->saveLayer(nullptr, paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::saveLayer(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
|
||||
canvas_->saveLayer(&bounds, paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::restore() {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->restore();
|
||||
}
|
||||
|
||||
int Canvas::getSaveCount() {
|
||||
if (!canvas_)
|
||||
return 0;
|
||||
return canvas_->getSaveCount();
|
||||
}
|
||||
|
||||
void Canvas::translate(double dx, double dy) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->translate(dx, dy);
|
||||
}
|
||||
|
||||
void Canvas::scale(double sx, double sy) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->scale(sx, sy);
|
||||
}
|
||||
|
||||
void Canvas::rotate(double radians) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->rotate(radians * 180.0 / M_PI);
|
||||
}
|
||||
|
||||
void Canvas::skew(double sx, double sy) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->skew(sx, sy);
|
||||
}
|
||||
|
||||
void Canvas::transform(const tonic::Float64List& matrix4) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->concat(ToSkMatrix(matrix4));
|
||||
}
|
||||
|
||||
void Canvas::setMatrix(const tonic::Float64List& matrix4) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->setMatrix(ToSkMatrix(matrix4));
|
||||
}
|
||||
|
||||
void Canvas::clipRect(double left, double top, double right, double bottom) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->clipRect(SkRect::MakeLTRB(left, top, right, bottom));
|
||||
}
|
||||
|
||||
void Canvas::clipRRect(const RRect& rrect) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->clipRRect(rrect.sk_rrect, SkRegion::kIntersect_Op);
|
||||
}
|
||||
|
||||
void Canvas::clipPath(const CanvasPath* path) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->clipPath(path->path(), SkRegion::kIntersect_Op);
|
||||
}
|
||||
|
||||
void Canvas::drawColor(SkColor color, SkXfermode::Mode transfer_mode) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawColor(color, transfer_mode);
|
||||
}
|
||||
|
||||
void Canvas::drawLine(double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawLine(x1, y1, x2, y2, *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawPaint(const Paint& paint, const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawPaint(*paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawRect(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawRect(SkRect::MakeLTRB(left, top, right, bottom), *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawRRect(const RRect& rrect,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawRRect(rrect.sk_rrect, *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawDRRect(const RRect& outer,
|
||||
const RRect& inner,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawDRRect(outer.sk_rrect, inner.sk_rrect, *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawOval(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawOval(SkRect::MakeLTRB(left, top, right, bottom), *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawCircle(double x,
|
||||
double y,
|
||||
double radius,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
canvas_->drawCircle(x, y, radius, *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawPath(const CanvasPath* path,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
FTL_DCHECK(path);
|
||||
canvas_->drawPath(path->path(), *paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawImage(const CanvasImage* image,
|
||||
double x,
|
||||
double y,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
FTL_DCHECK(image);
|
||||
canvas_->drawImage(image->image(), x, y, paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawImageRect(const CanvasImage* image,
|
||||
double src_left,
|
||||
double src_top,
|
||||
double src_right,
|
||||
double src_bottom,
|
||||
double dst_left,
|
||||
double dst_top,
|
||||
double dst_right,
|
||||
double dst_bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
FTL_DCHECK(image);
|
||||
SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
|
||||
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
|
||||
canvas_->drawImageRect(image->image(), src, dst, paint.paint(),
|
||||
SkCanvas::kFast_SrcRectConstraint);
|
||||
}
|
||||
|
||||
void Canvas::drawImageNine(const CanvasImage* image,
|
||||
double center_left,
|
||||
double center_top,
|
||||
double center_right,
|
||||
double center_bottom,
|
||||
double dst_left,
|
||||
double dst_top,
|
||||
double dst_right,
|
||||
double dst_bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
FTL_DCHECK(image);
|
||||
SkRect center =
|
||||
SkRect::MakeLTRB(center_left, center_top, center_right, center_bottom);
|
||||
SkIRect icenter;
|
||||
center.round(&icenter);
|
||||
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
|
||||
canvas_->drawImageNine(image->image(), icenter, dst, paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawPicture(Picture* picture) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
FTL_DCHECK(picture);
|
||||
canvas_->drawPicture(picture->picture().get());
|
||||
}
|
||||
|
||||
void Canvas::drawPoints(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
SkCanvas::PointMode point_mode,
|
||||
const tonic::Float32List& points) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
|
||||
static_assert(sizeof(SkPoint) == sizeof(float) * 2,
|
||||
"SkPoint doesn't use floats.");
|
||||
|
||||
canvas_->drawPoints(point_mode,
|
||||
points.num_elements() / 2, // SkPoints have two floats.
|
||||
reinterpret_cast<const SkPoint*>(points.data()),
|
||||
*paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawVertices(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
SkCanvas::VertexMode vertex_mode,
|
||||
const tonic::Float32List& vertices,
|
||||
const tonic::Float32List& texture_coordinates,
|
||||
const tonic::Int32List& colors,
|
||||
SkXfermode::Mode transfer_mode,
|
||||
const tonic::Int32List& indices) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
|
||||
sk_sp<SkXfermode> transfer_mode_ptr = SkXfermode::Make(transfer_mode);
|
||||
|
||||
std::vector<uint16_t> indices16;
|
||||
indices16.reserve(indices.num_elements());
|
||||
for (int i = 0; i < indices.num_elements(); ++i)
|
||||
indices16.push_back(indices.data()[i]);
|
||||
|
||||
static_assert(sizeof(SkPoint) == sizeof(float) * 2,
|
||||
"SkPoint doesn't use floats.");
|
||||
static_assert(sizeof(SkColor) == sizeof(int32_t),
|
||||
"SkColor doesn't use int32_t.");
|
||||
|
||||
canvas_->drawVertices(
|
||||
vertex_mode,
|
||||
vertices.num_elements() / 2, // SkPoints have two floats.
|
||||
reinterpret_cast<const SkPoint*>(vertices.data()),
|
||||
reinterpret_cast<const SkPoint*>(texture_coordinates.data()),
|
||||
reinterpret_cast<const SkColor*>(colors.data()), transfer_mode_ptr.get(),
|
||||
indices16.empty() ? nullptr : indices16.data(), indices16.size(),
|
||||
*paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::drawAtlas(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
CanvasImage* atlas,
|
||||
const tonic::Float32List& transforms,
|
||||
const tonic::Float32List& rects,
|
||||
const tonic::Int32List& colors,
|
||||
SkXfermode::Mode transfer_mode,
|
||||
const tonic::Float32List& cull_rect) {
|
||||
if (!canvas_)
|
||||
return;
|
||||
|
||||
sk_sp<SkImage> skImage = atlas->image();
|
||||
|
||||
static_assert(sizeof(SkRSXform) == sizeof(float) * 4,
|
||||
"SkRSXform doesn't use floats.");
|
||||
static_assert(sizeof(SkRect) == sizeof(float) * 4,
|
||||
"SkRect doesn't use floats.");
|
||||
|
||||
canvas_->drawAtlas(
|
||||
skImage.get(), reinterpret_cast<const SkRSXform*>(transforms.data()),
|
||||
reinterpret_cast<const SkRect*>(rects.data()),
|
||||
reinterpret_cast<const SkColor*>(colors.data()),
|
||||
rects.num_elements() / 4, // SkRect have four floats.
|
||||
transfer_mode, reinterpret_cast<const SkRect*>(cull_rect.data()),
|
||||
paint.paint());
|
||||
}
|
||||
|
||||
void Canvas::Clear() {
|
||||
canvas_ = nullptr;
|
||||
}
|
||||
|
||||
bool Canvas::IsRecording() const {
|
||||
return !!canvas_;
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
180
engine/src/flutter/lib/ui/painting/canvas.h
Normal file
180
engine/src/flutter/lib/ui/painting/canvas.h
Normal file
@@ -0,0 +1,180 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_CANVAS_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_CANVAS_H_
|
||||
|
||||
#include "flutter/lib/ui/painting/paint.h"
|
||||
#include "flutter/lib/ui/painting/path.h"
|
||||
#include "flutter/lib/ui/painting/picture.h"
|
||||
#include "flutter/lib/ui/painting/picture_recorder.h"
|
||||
#include "flutter/lib/ui/painting/rrect.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "lib/tonic/typed_data/float32_list.h"
|
||||
#include "lib/tonic/typed_data/float64_list.h"
|
||||
#include "lib/tonic/typed_data/int32_list.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
|
||||
namespace tonic {
|
||||
class DartLibraryNatives;
|
||||
|
||||
template <>
|
||||
struct DartConverter<SkCanvas::PointMode>
|
||||
: public DartConverterInteger<SkCanvas::PointMode> {};
|
||||
|
||||
template <>
|
||||
struct DartConverter<SkCanvas::VertexMode>
|
||||
: public DartConverterInteger<SkCanvas::VertexMode> {};
|
||||
|
||||
} // namespace tonic
|
||||
|
||||
namespace blink {
|
||||
class CanvasImage;
|
||||
|
||||
class Canvas : public ftl::RefCountedThreadSafe<Canvas>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(Canvas);
|
||||
|
||||
public:
|
||||
static ftl::RefPtr<Canvas> Create(PictureRecorder* recorder,
|
||||
double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom);
|
||||
|
||||
~Canvas() override;
|
||||
|
||||
void save();
|
||||
void saveLayerWithoutBounds(const Paint& paint, const PaintData& paint_data);
|
||||
void saveLayer(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void restore();
|
||||
int getSaveCount();
|
||||
|
||||
void translate(double dx, double dy);
|
||||
void scale(double sx, double sy);
|
||||
void rotate(double radians);
|
||||
void skew(double sx, double sy);
|
||||
void transform(const tonic::Float64List& matrix4);
|
||||
void setMatrix(const tonic::Float64List& matrix4);
|
||||
|
||||
void clipRect(double left, double top, double right, double bottom);
|
||||
void clipRRect(const RRect& rrect);
|
||||
void clipPath(const CanvasPath* path);
|
||||
|
||||
void drawColor(SkColor color, SkXfermode::Mode transfer_mode);
|
||||
void drawLine(double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawPaint(const Paint& paint, const PaintData& paint_data);
|
||||
void drawRect(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawRRect(const RRect& rrect,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawDRRect(const RRect& outer,
|
||||
const RRect& inner,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawOval(double left,
|
||||
double top,
|
||||
double right,
|
||||
double bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawCircle(double x,
|
||||
double y,
|
||||
double radius,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawPath(const CanvasPath* path,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawImage(const CanvasImage* image,
|
||||
double x,
|
||||
double y,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawImageRect(const CanvasImage* image,
|
||||
double src_left,
|
||||
double src_top,
|
||||
double src_right,
|
||||
double src_bottom,
|
||||
double dst_left,
|
||||
double dst_top,
|
||||
double dst_right,
|
||||
double dst_bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawImageNine(const CanvasImage* image,
|
||||
double center_left,
|
||||
double center_top,
|
||||
double center_right,
|
||||
double center_bottom,
|
||||
double dst_left,
|
||||
double dst_top,
|
||||
double dst_right,
|
||||
double dst_bottom,
|
||||
const Paint& paint,
|
||||
const PaintData& paint_data);
|
||||
void drawPicture(Picture* picture);
|
||||
|
||||
// The paint argument is first for the following functions because Paint
|
||||
// unwraps a number of C++ objects. Once we create a view unto a
|
||||
// Float32List, we cannot re-enter the VM to unwrap objects. That means we
|
||||
// either need to process the paint argument first.
|
||||
|
||||
void drawPoints(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
SkCanvas::PointMode point_mode,
|
||||
const tonic::Float32List& points);
|
||||
|
||||
void drawVertices(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
SkCanvas::VertexMode vertex_mode,
|
||||
const tonic::Float32List& vertices,
|
||||
const tonic::Float32List& texture_coordinates,
|
||||
const tonic::Int32List& colors,
|
||||
SkXfermode::Mode transfer_mode,
|
||||
const tonic::Int32List& indices);
|
||||
|
||||
void drawAtlas(const Paint& paint,
|
||||
const PaintData& paint_data,
|
||||
CanvasImage* atlas,
|
||||
const tonic::Float32List& transforms,
|
||||
const tonic::Float32List& rects,
|
||||
const tonic::Int32List& colors,
|
||||
SkXfermode::Mode transfer_mode,
|
||||
const tonic::Float32List& cull_rect);
|
||||
|
||||
SkCanvas* canvas() const { return canvas_; }
|
||||
void Clear();
|
||||
bool IsRecording() const;
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
explicit Canvas(SkCanvas* canvas);
|
||||
|
||||
// The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording,
|
||||
// which does not transfer ownership. For this reason, we hold a raw
|
||||
// pointer and manually set to null in Clear.
|
||||
SkCanvas* canvas_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_CANVAS_H_
|
||||
79
engine/src/flutter/lib/ui/painting/gradient.cc
Normal file
79
engine/src/flutter/lib/ui/painting/gradient.cc
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/painting/gradient.h"
|
||||
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
typedef CanvasGradient
|
||||
Gradient; // Because the C++ name doesn't match the Dart name.
|
||||
|
||||
static void Gradient_constructor(Dart_NativeArguments args) {
|
||||
DartCallConstructor(&CanvasGradient::Create, args);
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, Gradient);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(Gradient, initLinear) \
|
||||
V(Gradient, initRadial)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void CanvasGradient::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register({{"Gradient_constructor", Gradient_constructor, 1, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
ftl::RefPtr<CanvasGradient> CanvasGradient::Create() {
|
||||
return ftl::MakeRefCounted<CanvasGradient>();
|
||||
}
|
||||
|
||||
void CanvasGradient::initLinear(const tonic::Float32List& end_points,
|
||||
const tonic::Int32List& colors,
|
||||
const tonic::Float32List& color_stops,
|
||||
SkShader::TileMode tile_mode) {
|
||||
FTL_DCHECK(end_points.num_elements() == 4);
|
||||
FTL_DCHECK(colors.num_elements() == color_stops.num_elements() ||
|
||||
color_stops.data() == nullptr);
|
||||
|
||||
static_assert(sizeof(SkPoint) == sizeof(float) * 2,
|
||||
"SkPoint doesn't use floats.");
|
||||
static_assert(sizeof(SkColor) == sizeof(int32_t),
|
||||
"SkColor doesn't use int32_t.");
|
||||
|
||||
set_shader(SkGradientShader::MakeLinear(
|
||||
reinterpret_cast<const SkPoint*>(end_points.data()),
|
||||
reinterpret_cast<const SkColor*>(colors.data()), color_stops.data(),
|
||||
colors.num_elements(), tile_mode));
|
||||
}
|
||||
|
||||
void CanvasGradient::initRadial(double center_x,
|
||||
double center_y,
|
||||
double radius,
|
||||
const tonic::Int32List& colors,
|
||||
const tonic::Float32List& color_stops,
|
||||
SkShader::TileMode tile_mode) {
|
||||
FTL_DCHECK(colors.num_elements() * 2 == color_stops.num_elements() ||
|
||||
color_stops.data() == nullptr);
|
||||
|
||||
static_assert(sizeof(SkColor) == sizeof(int32_t),
|
||||
"SkColor doesn't use int32_t.");
|
||||
|
||||
set_shader(SkGradientShader::MakeRadial(
|
||||
SkPoint::Make(center_x, center_y), radius,
|
||||
reinterpret_cast<const SkColor*>(colors.data()), color_stops.data(),
|
||||
colors.num_elements(), tile_mode));
|
||||
}
|
||||
|
||||
CanvasGradient::CanvasGradient() : Shader(nullptr) {}
|
||||
|
||||
CanvasGradient::~CanvasGradient() {}
|
||||
|
||||
} // namespace blink
|
||||
55
engine/src/flutter/lib/ui/painting/gradient.h
Normal file
55
engine/src/flutter/lib/ui/painting/gradient.h
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_GRADIENT_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_GRADIENT_H_
|
||||
|
||||
#include "flutter/lib/ui/painting/shader.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "lib/tonic/typed_data/float32_list.h"
|
||||
#include "lib/tonic/typed_data/int32_list.h"
|
||||
#include "third_party/skia/include/effects/SkGradientShader.h"
|
||||
|
||||
namespace tonic {
|
||||
class DartLibraryNatives;
|
||||
|
||||
template <>
|
||||
struct DartConverter<SkShader::TileMode>
|
||||
: public DartConverterInteger<SkShader::TileMode> {};
|
||||
|
||||
} // namspace tonic
|
||||
|
||||
namespace blink {
|
||||
|
||||
static_assert(SkShader::kTileModeCount == 3, "Need to update tile mode enum");
|
||||
|
||||
class CanvasGradient : public Shader {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(CanvasGradient);
|
||||
|
||||
public:
|
||||
~CanvasGradient() override;
|
||||
static ftl::RefPtr<CanvasGradient> Create();
|
||||
|
||||
void initLinear(const tonic::Float32List& end_points,
|
||||
const tonic::Int32List& colors,
|
||||
const tonic::Float32List& color_stops,
|
||||
SkShader::TileMode tile_mode);
|
||||
|
||||
void initRadial(double center_x,
|
||||
double center_y,
|
||||
double radius,
|
||||
const tonic::Int32List& colors,
|
||||
const tonic::Float32List& color_stops,
|
||||
SkShader::TileMode tile_mode);
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
CanvasGradient();
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_GRADIENT_H_
|
||||
37
engine/src/flutter/lib/ui/painting/image.cc
Normal file
37
engine/src/flutter/lib/ui/painting/image.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2013 The Chromium 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 "flutter/lib/ui/painting/image.h"
|
||||
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
typedef CanvasImage Image;
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, Image);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(Image, width) \
|
||||
V(Image, height) \
|
||||
V(Image, dispose)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void CanvasImage::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
CanvasImage::CanvasImage() {}
|
||||
|
||||
CanvasImage::~CanvasImage() {}
|
||||
|
||||
void CanvasImage::dispose() {
|
||||
ClearDartWrapper();
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
45
engine/src/flutter/lib/ui/painting/image.h
Normal file
45
engine/src/flutter/lib/ui/painting/image.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2013 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_IMAGE_H_
|
||||
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "third_party/skia/include/core/SkImage.h"
|
||||
|
||||
namespace tonic {
|
||||
class DartLibraryNatives;
|
||||
} // namespace tonic
|
||||
|
||||
namespace blink {
|
||||
|
||||
class CanvasImage final : public ftl::RefCountedThreadSafe<CanvasImage>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(CanvasImage);
|
||||
|
||||
public:
|
||||
~CanvasImage() override;
|
||||
static ftl::RefPtr<CanvasImage> Create() {
|
||||
return ftl::MakeRefCounted<CanvasImage>();
|
||||
}
|
||||
|
||||
int width() { return image_->width(); }
|
||||
int height() { return image_->height(); }
|
||||
void dispose();
|
||||
|
||||
const sk_sp<SkImage>& image() const { return image_; }
|
||||
void set_image(sk_sp<SkImage> image) { image_ = std::move(image); }
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
CanvasImage();
|
||||
|
||||
sk_sp<SkImage> image_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_H_
|
||||
151
engine/src/flutter/lib/ui/painting/image_decoding.cc
Normal file
151
engine/src/flutter/lib/ui/painting/image_decoding.cc
Normal file
@@ -0,0 +1,151 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/painting/image_decoding.h"
|
||||
|
||||
#include "flutter/flow/texture_image.h"
|
||||
#include "flutter/lib/ui/painting/image.h"
|
||||
#include "flutter/lib/ui/painting/resource_context.h"
|
||||
#include "flutter/glue/drain_data_pipe_job.h"
|
||||
#include "flutter/glue/movable_wrapper.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
#include "lib/ftl/tasks/task_runner.h"
|
||||
#include "lib/tonic/dart_persistent_value.h"
|
||||
#include "lib/tonic/logging/dart_invoke.h"
|
||||
#include "lib/tonic/mojo_converter.h"
|
||||
#include "lib/tonic/typed_data/uint8_list.h"
|
||||
#include "flutter/sky/engine/public/platform/Platform.h"
|
||||
#include "flutter/sky/engine/wtf/PassOwnPtr.h"
|
||||
#include "third_party/skia/include/core/SkImageGenerator.h"
|
||||
|
||||
using tonic::DartInvoke;
|
||||
using tonic::DartPersistentValue;
|
||||
using tonic::DartState;
|
||||
using tonic::ToDart;
|
||||
|
||||
namespace blink {
|
||||
namespace {
|
||||
|
||||
sk_sp<SkImage> DecodeImage(std::vector<char> buffer) {
|
||||
TRACE_EVENT0("blink", "DecodeImage");
|
||||
|
||||
if (buffer.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sk_sp<SkData> sk_data = SkData::MakeWithoutCopy(buffer.data(), buffer.size());
|
||||
|
||||
if (sk_data == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<SkImageGenerator> generator(
|
||||
SkImageGenerator::NewFromEncoded(sk_data.get()));
|
||||
|
||||
if (generator == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GrContext* context = ResourceContext::Get();
|
||||
|
||||
// First, try to create a texture image from the generator.
|
||||
if (sk_sp<SkImage> image = flow::TextureImageCreate(context, *generator)) {
|
||||
return image;
|
||||
}
|
||||
|
||||
// The, as a fallback, try to create a regular Skia managed image. These
|
||||
// don't require a context ready.
|
||||
return flow::BitmapImageCreate(*generator);
|
||||
}
|
||||
|
||||
void InvokeImageCallback(sk_sp<SkImage> image,
|
||||
PassOwnPtr<DartPersistentValue> callback) {
|
||||
tonic::DartState* dart_state = callback->dart_state().get();
|
||||
if (!dart_state)
|
||||
return;
|
||||
tonic::DartState::Scope scope(dart_state);
|
||||
if (!image) {
|
||||
DartInvoke(callback->value(), {Dart_Null()});
|
||||
} else {
|
||||
ftl::RefPtr<CanvasImage> resultImage = CanvasImage::Create();
|
||||
resultImage->set_image(std::move(image));
|
||||
DartInvoke(callback->value(), {ToDart(resultImage)});
|
||||
}
|
||||
}
|
||||
|
||||
void DecodeImageAndInvokeImageCallback(PassOwnPtr<DartPersistentValue> callback,
|
||||
std::vector<char> buffer) {
|
||||
sk_sp<SkImage> image = DecodeImage(std::move(buffer));
|
||||
Platform::current()->GetUITaskRunner()->PostTask(
|
||||
[callback, image]() { InvokeImageCallback(image, callback); });
|
||||
}
|
||||
|
||||
void DecodeImageFromDataPipe(Dart_NativeArguments args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
|
||||
auto consumer = glue::WrapMovable(
|
||||
tonic::DartConverter<mojo::ScopedDataPipeConsumerHandle>::FromArguments(
|
||||
args, 0, exception));
|
||||
if (exception) {
|
||||
Dart_ThrowException(exception);
|
||||
return;
|
||||
}
|
||||
|
||||
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
|
||||
if (!Dart_IsClosure(callback_handle)) {
|
||||
Dart_ThrowException(ToDart("Callback must be a function"));
|
||||
return;
|
||||
}
|
||||
|
||||
PassOwnPtr<DartPersistentValue> callback =
|
||||
adoptPtr(new DartPersistentValue(DartState::Current(), callback_handle));
|
||||
|
||||
Platform::current()->GetIOTaskRunner()->PostTask(
|
||||
[callback, consumer]() mutable {
|
||||
glue::DrainDataPipeJob* job = nullptr;
|
||||
job = new glue::DrainDataPipeJob(
|
||||
consumer.Unwrap(), [callback, job](std::vector<char> buffer) {
|
||||
delete job;
|
||||
DecodeImageAndInvokeImageCallback(callback, std::move(buffer));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void DecodeImageFromList(Dart_NativeArguments args) {
|
||||
Dart_Handle exception = nullptr;
|
||||
|
||||
tonic::Uint8List list =
|
||||
tonic::DartConverter<tonic::Uint8List>::FromArguments(args, 0, exception);
|
||||
if (exception) {
|
||||
Dart_ThrowException(exception);
|
||||
return;
|
||||
}
|
||||
|
||||
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
|
||||
if (!Dart_IsClosure(callback_handle)) {
|
||||
Dart_ThrowException(ToDart("Callback must be a function"));
|
||||
return;
|
||||
}
|
||||
PassOwnPtr<DartPersistentValue> callback =
|
||||
adoptPtr(new DartPersistentValue(DartState::Current(), callback_handle));
|
||||
|
||||
const char* bytes = reinterpret_cast<const char*>(list.data());
|
||||
PassOwnPtr<std::vector<char>> buffer =
|
||||
adoptPtr(new std::vector<char>(bytes, bytes + list.num_elements()));
|
||||
|
||||
Platform::current()->GetIOTaskRunner()->PostTask([callback, buffer]() {
|
||||
DecodeImageAndInvokeImageCallback(callback, std::move(*buffer));
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ImageDecoding::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register({
|
||||
{"decodeImageFromDataPipe", DecodeImageFromDataPipe, 2, true},
|
||||
{"decodeImageFromList", DecodeImageFromList, 2, true},
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
19
engine/src/flutter/lib/ui/painting/image_decoding.h
Normal file
19
engine/src/flutter/lib/ui/painting/image_decoding.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_
|
||||
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class ImageDecoding {
|
||||
public:
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_DECODING_H_
|
||||
56
engine/src/flutter/lib/ui/painting/image_filter.cc
Normal file
56
engine/src/flutter/lib/ui/painting/image_filter.cc
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2016 The Chromium 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 "flutter/lib/ui/painting/image_filter.h"
|
||||
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
#include "third_party/skia/include/effects/SkBlurImageFilter.h"
|
||||
#include "third_party/skia/include/effects/SkImageSource.h"
|
||||
#include "third_party/skia/include/effects/SkPictureImageFilter.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
static void ImageFilter_constructor(Dart_NativeArguments args) {
|
||||
DartCallConstructor(&ImageFilter::Create, args);
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, ImageFilter);
|
||||
|
||||
#define FOR_EACH_BINDING(V) \
|
||||
V(ImageFilter, initImage) \
|
||||
V(ImageFilter, initPicture) \
|
||||
V(ImageFilter, initBlur)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void ImageFilter::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register(
|
||||
{{"ImageFilter_constructor", ImageFilter_constructor, 1, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
ftl::RefPtr<ImageFilter> ImageFilter::Create() {
|
||||
return ftl::MakeRefCounted<ImageFilter>();
|
||||
}
|
||||
|
||||
ImageFilter::ImageFilter() {}
|
||||
|
||||
ImageFilter::~ImageFilter() {}
|
||||
|
||||
void ImageFilter::initImage(CanvasImage* image) {
|
||||
filter_ = SkImageSource::Make(image->image());
|
||||
}
|
||||
|
||||
void ImageFilter::initPicture(Picture* picture) {
|
||||
filter_ = SkPictureImageFilter::Make(picture->picture());
|
||||
}
|
||||
|
||||
void ImageFilter::initBlur(double sigma_x, double sigma_y) {
|
||||
filter_ = SkBlurImageFilter::Make(sigma_x, sigma_y, nullptr);
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
40
engine/src/flutter/lib/ui/painting/image_filter.h
Normal file
40
engine/src/flutter/lib/ui/painting/image_filter.h
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_FILTER_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_IMAGE_FILTER_H_
|
||||
|
||||
#include "flutter/lib/ui/painting/image.h"
|
||||
#include "flutter/lib/ui/painting/picture.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "third_party/skia/include/core/SkImageFilter.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class ImageFilter : public ftl::RefCountedThreadSafe<ImageFilter>,
|
||||
public tonic::DartWrappable {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(ImageFilter);
|
||||
|
||||
public:
|
||||
~ImageFilter() override;
|
||||
static ftl::RefPtr<ImageFilter> Create();
|
||||
|
||||
void initImage(CanvasImage* image);
|
||||
void initPicture(Picture*);
|
||||
void initBlur(double sigma_x, double sigma_y);
|
||||
|
||||
const sk_sp<SkImageFilter>& filter() { return filter_; }
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
ImageFilter();
|
||||
|
||||
sk_sp<SkImageFilter> filter_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_FILTER_H_
|
||||
50
engine/src/flutter/lib/ui/painting/image_shader.cc
Normal file
50
engine/src/flutter/lib/ui/painting/image_shader.cc
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2015 The Chromium 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 "flutter/lib/ui/painting/image_shader.h"
|
||||
|
||||
#include "lib/tonic/dart_args.h"
|
||||
#include "lib/tonic/dart_binding_macros.h"
|
||||
#include "lib/tonic/converter/dart_converter.h"
|
||||
#include "lib/tonic/dart_library_natives.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
static void ImageShader_constructor(Dart_NativeArguments args) {
|
||||
DartCallConstructor(&ImageShader::Create, args);
|
||||
}
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO(ui, ImageShader);
|
||||
|
||||
#define FOR_EACH_BINDING(V) V(ImageShader, initWithImage)
|
||||
|
||||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
|
||||
|
||||
void ImageShader::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register(
|
||||
{{"ImageShader_constructor", ImageShader_constructor, 1, true},
|
||||
FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
|
||||
}
|
||||
|
||||
ftl::RefPtr<ImageShader> ImageShader::Create() {
|
||||
return ftl::MakeRefCounted<ImageShader>();
|
||||
}
|
||||
|
||||
void ImageShader::initWithImage(CanvasImage* image,
|
||||
SkShader::TileMode tmx,
|
||||
SkShader::TileMode tmy,
|
||||
const tonic::Float64List& matrix4) {
|
||||
FTL_DCHECK(image != NULL);
|
||||
SkMatrix sk_matrix = ToSkMatrix(matrix4);
|
||||
SkBitmap bitmap;
|
||||
image->image()->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
|
||||
|
||||
set_shader(SkShader::MakeBitmapShader(bitmap, tmx, tmy, &sk_matrix));
|
||||
}
|
||||
|
||||
ImageShader::ImageShader() : Shader(nullptr) {}
|
||||
|
||||
ImageShader::~ImageShader() {}
|
||||
|
||||
} // namespace blink
|
||||
44
engine/src/flutter/lib/ui/painting/image_shader.h
Normal file
44
engine/src/flutter/lib/ui/painting/image_shader.h
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_LIB_UI_PAINTING_IMAGE_SHADER_H_
|
||||
#define FLUTTER_LIB_UI_PAINTING_IMAGE_SHADER_H_
|
||||
|
||||
#include "flutter/lib/ui/painting/gradient.h"
|
||||
#include "flutter/lib/ui/painting/image.h"
|
||||
#include "flutter/lib/ui/painting/matrix.h"
|
||||
#include "flutter/lib/ui/painting/shader.h"
|
||||
#include "lib/tonic/dart_wrappable.h"
|
||||
#include "lib/tonic/typed_data/float64_list.h"
|
||||
#include "third_party/skia/include/core/SkMatrix.h"
|
||||
#include "third_party/skia/include/core/SkShader.h"
|
||||
|
||||
namespace tonic {
|
||||
class DartLibraryNatives;
|
||||
} // namespace tonic
|
||||
|
||||
namespace blink {
|
||||
|
||||
class ImageShader : public Shader {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FRIEND_MAKE_REF_COUNTED(ImageShader);
|
||||
|
||||
public:
|
||||
~ImageShader() override;
|
||||
static ftl::RefPtr<ImageShader> Create();
|
||||
|
||||
void initWithImage(CanvasImage* image,
|
||||
SkShader::TileMode tmx,
|
||||
SkShader::TileMode tmy,
|
||||
const tonic::Float64List& matrix4);
|
||||
|
||||
static void RegisterNatives(tonic::DartLibraryNatives* natives);
|
||||
|
||||
private:
|
||||
ImageShader();
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_LIB_UI_PAINTING_IMAGE_SHADER_H_
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user