1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/android/resources/resource_manager_impl.h"
#include <inttypes.h>
#include <stddef.h>
#include <utility>
#include <vector>
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/debug/dump_without_crashing.h"
#include "base/memory/ptr_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "cc/resources/scoped_ui_resource.h"
#include "cc/resources/ui_resource_manager.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "ui/android/resources/ui_resource_provider.h"
#include "ui/android/window_android.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/geometry/rect.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "ui/android/ui_android_jni_headers/ResourceManager_jni.h"
using base::android::JavaArrayOfIntArrayToIntVector;
using base::android::JavaParamRef;
using base::android::JavaRef;
namespace {
base::trace_event::MemoryAllocatorDump* CreateMemoryDump(
const std::string& name,
size_t memory_usage,
base::trace_event::ProcessMemoryDump* pmd) {
base::trace_event::MemoryAllocatorDump* dump = pmd->CreateAllocatorDump(name);
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
base::trace_event::MemoryAllocatorDump::kUnitsBytes,
memory_usage);
static const char* system_allocator_name =
base::trace_event::MemoryDumpManager::GetInstance()
->system_allocator_pool_name();
if (system_allocator_name)
pmd->AddSuballocation(dump->guid(), system_allocator_name);
return dump;
}
} // namespace
namespace ui {
// static
ResourceManagerImpl* ResourceManagerImpl::FromJavaObject(
const JavaRef<jobject>& jobj) {
return reinterpret_cast<ResourceManagerImpl*>(
Java_ResourceManager_getNativePtr(base::android::AttachCurrentThread(),
jobj));
}
ResourceManagerImpl::ResourceManagerImpl(gfx::NativeWindow native_window)
: ui_resource_manager_(nullptr) {
JNIEnv* env = base::android::AttachCurrentThread();
java_obj_.Reset(
env, Java_ResourceManager_create(env, native_window->GetJavaObject(),
reinterpret_cast<intptr_t>(this))
.obj());
DCHECK(!java_obj_.is_null());
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
this, "android::ResourceManagerImpl",
base::SingleThreadTaskRunner::GetCurrentDefault());
}
ResourceManagerImpl::~ResourceManagerImpl() {
base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
this);
Java_ResourceManager_destroy(base::android::AttachCurrentThread(), java_obj_);
}
void ResourceManagerImpl::Init(cc::UIResourceManager* ui_resource_manager) {
DCHECK(!ui_resource_manager_);
DCHECK(ui_resource_manager);
ui_resource_manager_ = ui_resource_manager;
}
base::android::ScopedJavaLocalRef<jobject>
ResourceManagerImpl::GetJavaObject() {
return base::android::ScopedJavaLocalRef<jobject>(java_obj_);
}
Resource* ResourceManagerImpl::GetResource(AndroidResourceType res_type,
int res_id) {
DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
std::unordered_map<int, std::unique_ptr<Resource>>::iterator item =
resources_[res_type].find(res_id);
if (item == resources_[res_type].end() ||
res_type == ANDROID_RESOURCE_TYPE_DYNAMIC ||
res_type == ANDROID_RESOURCE_TYPE_DYNAMIC_BITMAP) {
RequestResourceFromJava(res_type, res_id);
// Check if the resource has been added (some dynamic may not have been).
item = resources_[res_type].find(res_id);
if (item == resources_[res_type].end())
return nullptr;
}
return item->second.get();
}
void ResourceManagerImpl::RemoveUnusedTints() {
// Iterate over the currently cached tints and remove ones that were not
// used as defined in |used_tints|.
for (auto it = tinted_resources_.cbegin(); it != tinted_resources_.cend();) {
if (used_tints_.find(it->first) == used_tints_.end()) {
it = tinted_resources_.erase(it);
} else {
++it;
}
}
}
void ResourceManagerImpl::OnFrameUpdatesFinished() {
RemoveUnusedTints();
used_tints_.clear();
}
Resource* ResourceManagerImpl::GetStaticResourceWithTint(int res_id,
SkColor tint_color) {
return GetStaticResourceWithTint(res_id, tint_color, false);
}
Resource* ResourceManagerImpl::GetStaticResourceWithTint(
int res_id,
SkColor tint_color,
bool preserve_color_alpha) {
if (tinted_resources_.find(tint_color) == tinted_resources_.end()) {
tinted_resources_[tint_color] = std::make_unique<ResourceMap>();
}
used_tints_.insert(tint_color);
ResourceMap* resource_map = tinted_resources_[tint_color].get();
// If the resource is already cached, use it.
std::unordered_map<int, std::unique_ptr<Resource>>::iterator item =
resource_map->find(res_id);
if (item != resource_map->end())
return item->second.get();
Resource* base_image = GetResource(ANDROID_RESOURCE_TYPE_STATIC, res_id);
DCHECK(base_image);
std::unique_ptr<Resource> tinted_resource = base_image->CreateForCopy();
TRACE_EVENT0("browser", "ResourceManagerImpl::GetStaticResourceWithTint");
SkBitmap tinted_bitmap;
tinted_bitmap.allocPixels(SkImageInfo::MakeN32Premul(
base_image->size().width(), base_image->size().height()));
SkCanvas canvas(tinted_bitmap);
canvas.clear(SK_ColorTRANSPARENT);
// Build a color filter to use on the base resource. This filter ignores
// the original image's RGB components, instead using the components of the
// new color. The alpha of the original image will be conditionally preserved
// based on preserve_color_alpha.
float alpha_multiplier =
preserve_color_alpha ? SkColorGetA(tint_color) * (1.0f / 255) : 1;
float color_matrix[20] = {0, 0, 0, 0, SkColorGetR(tint_color) * (1.0f / 255),
0, 0, 0, 0, SkColorGetG(tint_color) * (1.0f / 255),
0, 0, 0, 0, SkColorGetB(tint_color) * (1.0f / 255),
0, 0, 0, alpha_multiplier, 0};
SkPaint color_filter;
color_filter.setColorFilter(SkColorFilters::Matrix(color_matrix));
// Draw the resource and make it immutable.
base_image->ui_resource()
->GetBitmap(base_image->ui_resource()->id(), false)
.DrawToCanvas(&canvas, &color_filter);
tinted_bitmap.setImmutable();
// Create a UI resource from the new bitmap.
tinted_resource->SetUIResource(
cc::ScopedUIResource::Create(ui_resource_manager_,
cc::UIResourceBitmap(tinted_bitmap)),
base_image->size());
(*resource_map)[res_id].swap(tinted_resource);
return (*resource_map)[res_id].get();
}
void ResourceManagerImpl::ClearTintedResourceCache(JNIEnv* env,
const JavaRef<jobject>& jobj) {
tinted_resources_.clear();
}
void ResourceManagerImpl::PreloadResource(AndroidResourceType res_type,
int res_id) {
DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
// Don't send out a query if the resource is already loaded.
if (resources_[res_type].find(res_id) != resources_[res_type].end())
return;
PreloadResourceFromJava(res_type, res_id);
}
void ResourceManagerImpl::OnResourceReady(JNIEnv* env,
const JavaRef<jobject>& jobj,
jint res_type,
jint res_id,
const JavaRef<jobject>& bitmap,
jint width,
jint height,
jlong native_resource) {
DCHECK_GE(res_type, ANDROID_RESOURCE_TYPE_FIRST);
DCHECK_LE(res_type, ANDROID_RESOURCE_TYPE_LAST);
TRACE_EVENT2("ui", "ResourceManagerImpl::OnResourceReady",
"resource_type", res_type,
"resource_id", res_id);
resources_[res_type][res_id] =
base::WrapUnique(reinterpret_cast<Resource*>(native_resource));
Resource* resource = resources_[res_type][res_id].get();
gfx::JavaBitmap jbitmap(bitmap);
SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(jbitmap);
skbitmap.setImmutable();
resource->SetUIResource(
cc::ScopedUIResource::Create(ui_resource_manager_,
cc::UIResourceBitmap(skbitmap)),
gfx::Size(width, height));
}
void ResourceManagerImpl::RemoveResource(
JNIEnv* env,
const base::android::JavaRef<jobject>& jobj,
jint res_type,
jint res_id) {
resources_[res_type].erase(res_id);
}
void ResourceManagerImpl::DumpIfNoResource(
JNIEnv* env,
const base::android::JavaRef<jobject>& jobj,
jint res_type,
jint res_id) {
if (resources_[res_type].find(res_id) == resources_[res_type].end()) {
base::debug::DumpWithoutCrashing(); // Investigating crbug.com/388600389.
}
}
bool ResourceManagerImpl::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) {
std::string prefix = base::StringPrintf("ui/resource_manager_0x%" PRIXPTR,
reinterpret_cast<uintptr_t>(this));
for (uint32_t type = static_cast<uint32_t>(ANDROID_RESOURCE_TYPE_FIRST);
type <= static_cast<uint32_t>(ANDROID_RESOURCE_TYPE_LAST); ++type) {
size_t usage = base::trace_event::EstimateMemoryUsage(resources_[type]);
auto* dump = CreateMemoryDump(
prefix + base::StringPrintf("/default_resource/0x%u",
static_cast<uint32_t>(type)),
usage, pmd);
dump->AddScalar("resource_count", "objects", resources_[type].size());
}
size_t tinted_resource_usage =
base::trace_event::EstimateMemoryUsage(tinted_resources_);
CreateMemoryDump(prefix + "/tinted_resource", tinted_resource_usage, pmd);
return true;
}
void ResourceManagerImpl::PreloadResourceFromJava(AndroidResourceType res_type,
int res_id) {
TRACE_EVENT2("ui", "ResourceManagerImpl::PreloadResourceFromJava",
"resource_type", res_type,
"resource_id", res_id);
Java_ResourceManager_preloadResource(base::android::AttachCurrentThread(),
java_obj_, res_type, res_id);
}
void ResourceManagerImpl::RequestResourceFromJava(AndroidResourceType res_type,
int res_id) {
TRACE_EVENT2("ui", "ResourceManagerImpl::RequestResourceFromJava",
"resource_type", res_type,
"resource_id", res_id);
Java_ResourceManager_resourceRequested(base::android::AttachCurrentThread(),
java_obj_, res_type, res_id);
}
} // namespace ui
|