Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ink/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ cc_library(
"//ink/storage/proto:options_cc_proto",
"//ink/storage/proto:stroke_input_batch_cc_proto",
"//ink/types:duration",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/status",
Expand Down
17 changes: 9 additions & 8 deletions ink/storage/brush.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
#include "ink/storage/brush.h"

#include <array>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <variant>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/inlined_vector.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "ink/brush/brush.h"
Expand Down Expand Up @@ -1768,8 +1769,8 @@ absl::StatusOr<BrushFamily> DecodeBrushFamily(
return DecodeBrushFamily(
family_proto,
// LINT.IfChange(decode_brush_family_get_client_texture_id)
[](const std::string& encoded_id, const std::string& bitmap) {
return encoded_id;
[](absl::string_view encoded_id, absl::string_view bitmap) {
return std::string(encoded_id);
},
// LINT.ThenChange(//depot/google3/third_party/ink/storage/brush.h:decode_brush_family_get_client_texture_id)
max_version);
Expand All @@ -1794,11 +1795,11 @@ absl::StatusOr<BrushFamily> DecodeBrushFamily(

// ID map that also serves as a record of the IDs for which we've already
// called `get_client_texture_id`.
std::map<std::string, std::string> old_to_new_id = {};
absl::flat_hash_map<std::string, std::string> old_to_new_id = {};

ClientTextureIdProvider texture_callback =
[&family_proto, &old_to_new_id, &get_client_texture_id](
const std::string& old_id) -> absl::StatusOr<std::string> {
absl::string_view old_id) -> absl::StatusOr<std::string> {
if (auto it = old_to_new_id.find(old_id); it != old_to_new_id.end()) {
// No need to call `get_client_texture_id` again.
return it->second;
Expand All @@ -1814,7 +1815,7 @@ absl::StatusOr<BrushFamily> DecodeBrushFamily(
if (!new_id.ok()) {
return new_id.status();
}
old_to_new_id.insert({old_id, *new_id});
old_to_new_id.insert({std::string(old_id), *new_id});
return *new_id;
};

Expand Down Expand Up @@ -1854,8 +1855,8 @@ absl::StatusOr<Brush> DecodeBrush(const proto::Brush& brush_proto,
return DecodeBrush(
brush_proto,
// LINT.IfChange(decode_brush_get_client_texture_id)
[](const std::string& encoded_id, const std::string& bitmap) {
return encoded_id;
[](absl::string_view encoded_id, absl::string_view bitmap) {
return std::string(encoded_id);
},
// LINT.ThenChange(//depot/google3/third_party/ink/storage/brush.h:decode_brush_get_client_texture_id)
max_version);
Expand Down
25 changes: 13 additions & 12 deletions ink/storage/brush.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "ink/brush/brush.h"
#include "ink/brush/brush_behavior.h"
#include "ink/brush/brush_coat.h"
Expand All @@ -37,32 +38,32 @@ namespace ink {
// returned string is the bytes of the PNG-encoded bitmap, or std::nullopt if
// the bitmap is not available.
using TextureBitmapProvider =
std::function<std::optional<std::string>(const std::string& id)>;
std::function<std::optional<std::string>(absl::string_view id)>;

// Provides a new client texture ID for a given encoded texture ID, and is
// responsible for receiving (e.g. by storing) the corresponding bitmap.
// `bitmap` is the bytes of the PNG-encoded bitmap that the proto associated
// with `encoded_id`, or the empty string if there was no associated bitmap.
using ClientTextureIdProviderAndBitmapReceiver =
std::function<absl::StatusOr<std::string>(const std::string& encoded_id,
const std::string& bitmap)>;
std::function<absl::StatusOr<std::string>(absl::string_view encoded_id,
absl::string_view bitmap)>;

// Provides a new client texture ID for a given encoded texture ID.
using ClientTextureIdProvider =
std::function<absl::StatusOr<std::string>(const std::string& encoded_id)>;
std::function<absl::StatusOr<std::string>(absl::string_view encoded_id)>;

// Populates the given proto by encoding the given brush object.
//
// The proto need not be empty before calling these; they will effectively clear
// the proto first.
void EncodeBrush(
const Brush& brush, proto::Brush& brush_proto_out,
TextureBitmapProvider get_bitmap = [](const std::string& id) {
TextureBitmapProvider get_bitmap = [](absl::string_view id) {
return std::nullopt;
});
void EncodeBrushFamily(
const BrushFamily& family, proto::BrushFamily& family_proto_out,
TextureBitmapProvider get_bitmap = [](const std::string& id) {
TextureBitmapProvider get_bitmap = [](absl::string_view id) {
return std::nullopt;
});
void EncodeBrushFamilyTextureMap(
Expand All @@ -84,8 +85,8 @@ absl::StatusOr<Brush> DecodeBrush(
const proto::Brush& brush_proto,
// LINT.IfChange(decode_brush_get_client_texture_id)
ClientTextureIdProviderAndBitmapReceiver get_client_texture_id =
[](const std::string& encoded_id, const std::string& bitmap) {
return encoded_id;
[](absl::string_view encoded_id, absl::string_view bitmap) {
return std::string(encoded_id);
},
// LINT.ThenChange(//depot/google3/third_party/ink/storage/brush.cc:decode_brush_get_client_texture_id)
Version max_version = Version::kMaxSupported());
Expand All @@ -95,8 +96,8 @@ absl::StatusOr<BrushFamily> DecodeBrushFamily(
const proto::BrushFamily& family_proto,
// LINT.IfChange(decode_brush_family_get_client_texture_id)
ClientTextureIdProviderAndBitmapReceiver get_client_texture_id =
[](const std::string& encoded_id, const std::string& bitmap) {
return encoded_id;
[](absl::string_view encoded_id, absl::string_view bitmap) {
return std::string(encoded_id);
},
// LINT.ThenChange(//depot/google3/third_party/ink/storage/brush.cc:decode_brush_family_get_client_texture_id)
Version max_version = Version::kMaxSupported());
Expand All @@ -105,11 +106,11 @@ absl::StatusOr<BrushFamily> DecodeBrushFamily(
absl::StatusOr<BrushCoat> DecodeBrushCoat(
const proto::BrushCoat& coat_proto,
ClientTextureIdProvider get_client_texture_id =
[](const std::string& encoded_id) { return encoded_id; });
[](absl::string_view encoded_id) { return std::string(encoded_id); });
absl::StatusOr<BrushPaint> DecodeBrushPaint(
const proto::BrushPaint& paint_proto,
ClientTextureIdProvider get_client_texture_id =
[](const std::string& encoded_id) { return encoded_id; });
[](absl::string_view encoded_id) { return std::string(encoded_id); });
absl::StatusOr<BrushTip> DecodeBrushTip(const proto::BrushTip& tip_proto);
absl::StatusOr<BrushBehavior::Node> DecodeBrushBehaviorNode(
const proto::BrushBehavior::Node& node_proto);
Expand Down
28 changes: 14 additions & 14 deletions ink/storage/brush_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ TEST(BrushTest, DecodeBrushProto) {

std::map<std::string, std::string> decoded_bitmaps = {};
ClientTextureIdProviderAndBitmapReceiver callback =
[&decoded_bitmaps](const std::string& id,
const std::string& bitmap) -> std::string {
[&decoded_bitmaps](absl::string_view id,
absl::string_view bitmap) -> std::string {
std::string new_id = "";
if (id == kTestTextureId1) {
new_id = kTestTextureId1Decoded;
Expand All @@ -191,7 +191,7 @@ TEST(BrushTest, DecodeBrushProto) {
}
if (!bitmap.empty()) {
if (decoded_bitmaps.find(new_id) == decoded_bitmaps.end()) {
decoded_bitmaps.insert({new_id, bitmap});
decoded_bitmaps.insert({new_id, std::string(bitmap)});
}
}
return new_id;
Expand Down Expand Up @@ -417,7 +417,7 @@ TEST(BrushTest, EncodeBrushWithoutTextureMap) {
ASSERT_EQ(brush.status(), absl::OkStatus());
proto::Brush brush_proto_out;
int callback_count = 0;
TextureBitmapProvider callback = [&callback_count](const std::string& id) {
TextureBitmapProvider callback = [&callback_count](absl::string_view id) {
callback_count++;
return std::nullopt;
};
Expand Down Expand Up @@ -496,7 +496,7 @@ TEST(BrushTest, EncodeBrushWithTextureMap) {
proto::Brush brush_proto_out;
int callback_count = 0;
TextureBitmapProvider callback =
[&callback_count](const std::string& id) -> std::optional<std::string> {
[&callback_count](absl::string_view id) -> std::optional<std::string> {
callback_count++;
if (kTestTextureId1 == id) {
return TestPngBytes1x1();
Expand Down Expand Up @@ -590,7 +590,7 @@ TEST(BrushTest, EncodeBrushFamilyTextureMap) {
int distinct_texture_ids_count = 0;
TextureBitmapProvider callback =
[&distinct_texture_ids_count](
const std::string& id) -> std::optional<std::string> {
absl::string_view id) -> std::optional<std::string> {
distinct_texture_ids_count++;
if (kTestTextureId1 == id) {
return TestPngBytes1x1();
Expand Down Expand Up @@ -621,7 +621,7 @@ TEST(BrushTest, EncodeBrushFamilyTextureMapWithNonEmptyProto) {

int callback_count = 0;
TextureBitmapProvider callback =
[&callback_count](const std::string& id) -> std::optional<std::string> {
[&callback_count](absl::string_view id) -> std::optional<std::string> {
callback_count++;
return std::nullopt;
};
Expand Down Expand Up @@ -667,8 +667,8 @@ TEST(BrushTest, DecodeBrushFamilyWithNoInputModel) {
TEST(BrushTest, DecodeBrushFamilyReturnsErrorStatusFromCallback) {
absl::Status error_status = absl::InternalError("test error");
ClientTextureIdProviderAndBitmapReceiver callback =
[&error_status](const std::string&,
const std::string&) -> absl::StatusOr<std::string> {
[&error_status](absl::string_view,
absl::string_view) -> absl::StatusOr<std::string> {
return error_status;
};
proto::BrushFamily family_proto;
Expand All @@ -685,7 +685,7 @@ TEST(BrushTest, DecodeBrushPaintReturnsErrorStatusFromCallback) {
absl::Status error_status = absl::InternalError("test error");
ClientTextureIdProvider callback =
[&error_status](
const std::string& encoded_id) -> absl::StatusOr<std::string> {
absl::string_view encoded_id) -> absl::StatusOr<std::string> {
return error_status;
};
proto::BrushPaint paint_proto;
Expand Down Expand Up @@ -817,16 +817,16 @@ void EncodeDecodeBrushRoundTrip(const Brush& brush_in) {
int decode_callback_count = 0;
TextureBitmapProvider encode_callback =
[&encode_callback_count](
const std::string& id) -> std::optional<std::string> {
absl::string_view id) -> std::optional<std::string> {
encode_callback_count++;
return std::nullopt;
};
ClientTextureIdProviderAndBitmapReceiver decode_callback =
[&decode_callback_count](
const std::string& id,
const std::string& bitmap) -> absl::StatusOr<std::string> {
absl::string_view id,
absl::string_view bitmap) -> absl::StatusOr<std::string> {
decode_callback_count++;
return id;
return std::string(id);
};
proto::Brush brush_proto_in;
EncodeBrush(brush_in, brush_proto_in, encode_callback);
Expand Down
2 changes: 2 additions & 0 deletions ink/storage/internal/jni/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ cc_library(
"//ink/storage:brush",
"//ink/storage/proto:brush_cc_proto",
"//ink/storage/proto:brush_family_cc_proto",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
] + select({
"@platforms//os:android": [],
"//conditions:default": [
Expand Down
17 changes: 10 additions & 7 deletions ink/storage/internal/jni/brush_serialization_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

#include <jni.h>

#include <map>
#include <optional>
#include <string>
#include <utility>

#include "absl/container/flat_hash_map.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "ink/brush/brush.h"
#include "ink/brush/brush_behavior.h"
#include "ink/brush/brush_coat.h"
Expand Down Expand Up @@ -90,7 +91,7 @@ JNI_METHOD(storage, BrushSerializationNative, jbyteArray, serializeBrush)
JNI_METHOD(storage, BrushSerializationNative, jbyteArray, serializeBrushFamily)
(JNIEnv* env, jobject object, jlong brush_family_native_pointer,
jobjectArray texture_map_keys, jobjectArray texture_map_values) {
std::map<std::string, std::string> texture_map = {};
absl::flat_hash_map<std::string, std::string> texture_map = {};

jsize key_length = env->GetArrayLength(texture_map_keys);
jsize value_length = env->GetArrayLength(texture_map_values);
Expand All @@ -112,7 +113,7 @@ JNI_METHOD(storage, BrushSerializationNative, jbyteArray, serializeBrushFamily)

ink::TextureBitmapProvider texture_bitmap_provider =
[&texture_map](
const std::string& texture_id) -> std::optional<std::string> {
absl::string_view texture_id) -> std::optional<std::string> {
if (auto it = texture_map.find(texture_id); it != texture_map.end()) {
return it->second;
}
Expand Down Expand Up @@ -199,14 +200,16 @@ JNI_METHOD(storage, BrushSerializationNative, jlong,

ink::ClientTextureIdProviderAndBitmapReceiver decode_texture_jni_wrapper =
[env, callback, on_decode_texture_method](
const std::string& encoded_id,
const std::string& bitmap) -> absl::StatusOr<std::string> {
absl::string_view encoded_id,
absl::string_view bitmap) -> absl::StatusOr<std::string> {
if (env->ExceptionCheck()) {
return absl::InternalError("Previously encountered exception in JVM.");
}
jstring encoded_id_jstring = env->NewStringUTF(encoded_id.c_str());
jstring encoded_id_jstring =
env->NewStringUTF(std::string(encoded_id).c_str());
jbyteArray pixel_data_jarray =
bitmap.empty() ? nullptr : StdStringToJByteArray(env, bitmap);
bitmap.empty() ? nullptr
: StdStringToJByteArray(env, std::string(bitmap));

jstring new_id_jstring = static_cast<jstring>(
env->CallObjectMethod(callback, on_decode_texture_method,
Expand Down
Loading