The multiwindow model and Resources-per-activity model that came in N puts greater demands on AssetManagers. They are created whenever window dimensions change, which can be frequently. There is a need to be able to cheaply create a new AssetManager for each Activity, which shares a lot of underlying state. In order to make the creation of AssetManagers cheap, we need a new implementation of the native AssetManager and ResTable to support immutable representations of APKs. This new data structure/class is ApkAssets. ApkAssets have the same functionality of an AssetManager, except that they operate on a single APK, and they do not do any caching. Once loaded, they are immutable. ApkAssets will be exposed as a Java object, with its implementation in native code. The existing Java StringBlock will be owned by ApkAssets, which means that Strings can be shared across AssetManagers. ApkAssets can be cached by the ResourcesManager. Creating an AssetManager requires only a list of ApkAssets and a configuration. AssetManager2 (named with the suffix '2' for now while transitioning to the new implementation) caches bags that are accessed. Since ApkAssets are expected to be kept around longer, they do more validation of the resource table, which cause slower load times. Measured on an angler-userdebug, loading the framework assets takes 11ms with ApkAssets, and 2ms with the old AssetManager implementation. The tradeoff is that there does not need to be any security checks once an ApkAssets is loaded, and regular resource retrieval is faster. Measured on an angler-userdebug, accessing resource (android:string/ok) with many locales takes 18us with AssetManager2, and 19us with AssetManager (this is per resource, so these add up). Test: make libandroidfw_tests Change-Id: Id0e57ee828f17008891fe3741935a9be8830b01d
114 lines
3.2 KiB
C++
114 lines
3.2 KiB
C++
/*
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#ifndef CHUNK_H_
|
|
#define CHUNK_H_
|
|
|
|
#include "android-base/logging.h"
|
|
#include "android-base/macros.h"
|
|
#include "utils/ByteOrder.h"
|
|
|
|
#ifdef _WIN32
|
|
#ifdef ERROR
|
|
#undef ERROR
|
|
#endif
|
|
#endif
|
|
|
|
#include "androidfw/ResourceTypes.h"
|
|
|
|
namespace android {
|
|
|
|
// Helpful wrapper around a ResChunk_header that provides getter methods
|
|
// that handle endianness conversions and provide access to the data portion
|
|
// of the chunk.
|
|
class Chunk {
|
|
public:
|
|
explicit Chunk(const ResChunk_header* chunk) : device_chunk_(chunk) {}
|
|
|
|
// Returns the type of the chunk. Caller need not worry about endianness.
|
|
inline int type() const { return dtohs(device_chunk_->type); }
|
|
|
|
// Returns the size of the entire chunk. This can be useful for skipping
|
|
// over the entire chunk. Caller need not worry about endianness.
|
|
inline size_t size() const { return dtohl(device_chunk_->size); }
|
|
|
|
// Returns the size of the header. Caller need not worry about endianness.
|
|
inline size_t header_size() const { return dtohs(device_chunk_->headerSize); }
|
|
|
|
template <typename T>
|
|
inline const T* header() const {
|
|
if (header_size() >= sizeof(T)) {
|
|
return reinterpret_cast<const T*>(device_chunk_);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
inline const void* data_ptr() const {
|
|
return reinterpret_cast<const uint8_t*>(device_chunk_) + header_size();
|
|
}
|
|
|
|
inline size_t data_size() const { return size() - header_size(); }
|
|
|
|
private:
|
|
const ResChunk_header* device_chunk_;
|
|
};
|
|
|
|
// Provides a Java style iterator over an array of ResChunk_header's.
|
|
// Validation is performed while iterating.
|
|
// The caller should check if there was an error during chunk validation
|
|
// by calling HadError() and GetLastError() to get the reason for failure.
|
|
// Example:
|
|
//
|
|
// ChunkIterator iter(data_ptr, data_len);
|
|
// while (iter.HasNext()) {
|
|
// const Chunk chunk = iter.Next();
|
|
// ...
|
|
// }
|
|
//
|
|
// if (iter.HadError()) {
|
|
// LOG(ERROR) << iter.GetLastError();
|
|
// }
|
|
//
|
|
class ChunkIterator {
|
|
public:
|
|
ChunkIterator(const void* data, size_t len)
|
|
: next_chunk_(reinterpret_cast<const ResChunk_header*>(data)),
|
|
len_(len),
|
|
last_error_(nullptr) {
|
|
CHECK(next_chunk_ != nullptr) << "data can't be nullptr";
|
|
VerifyNextChunk();
|
|
}
|
|
|
|
Chunk Next();
|
|
inline bool HasNext() const { return !HadError() && len_ != 0; };
|
|
inline bool HadError() const { return last_error_ != nullptr; }
|
|
inline std::string GetLastError() const { return last_error_; }
|
|
|
|
private:
|
|
DISALLOW_COPY_AND_ASSIGN(ChunkIterator);
|
|
|
|
// Returns false if there was an error.
|
|
bool VerifyNextChunk();
|
|
|
|
const ResChunk_header* next_chunk_;
|
|
size_t len_;
|
|
const char* last_error_;
|
|
};
|
|
|
|
} // namespace android
|
|
|
|
#endif /* CHUNK_H_ */
|