blob: 4bfd2c9c2ade534a9feb93751af2856c1de15c74 [file] [log] [blame]
// 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.
#ifndef UI_ANDROID_RESOURCES_RESOURCE_MANAGER_IMPL_H_
#define UI_ANDROID_RESOURCES_RESOURCE_MANAGER_IMPL_H_
#include <array>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "base/trace_event/memory_dump_provider.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_set.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/android/resources/resource_manager.h"
#include "ui/android/ui_android_export.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/native_ui_types.h"
namespace cc {
class UIResourceManager;
} // namespace cc
namespace ui {
class UI_ANDROID_EXPORT ResourceManagerImpl
: public ResourceManager,
public base::trace_event::MemoryDumpProvider {
public:
static ResourceManagerImpl* FromJavaObject(
const base::android::JavaRef<jobject>& jobj);
explicit ResourceManagerImpl(gfx::NativeWindow native_window);
ResourceManagerImpl(const ResourceManagerImpl&) = delete;
ResourceManagerImpl& operator=(const ResourceManagerImpl&) = delete;
~ResourceManagerImpl() override;
void Init(cc::UIResourceManager* ui_resource_manager);
// ResourceManager implementation.
base::android::ScopedJavaLocalRef<jobject> GetJavaObject() override;
Resource* GetResource(AndroidResourceType res_type, int res_id) override;
Resource* GetStaticResourceWithTint(
int res_id, SkColor tint_color) override;
Resource* GetStaticResourceWithTint(int res_id,
SkColor tint_color,
bool preserve_color_alpha) override;
Resource* GetAndRetainStaticResourceWithTint(int res_id,
SkColor tint_color) override;
void ReleaseStaticResource(int res_id) override;
void PreloadResource(AndroidResourceType res_type, int res_id) override;
void OnFrameUpdatesFinished() override;
// Called from Java
// ----------------------------------------------------------
void OnResourceReady(JNIEnv* env,
int32_t res_type,
int32_t res_id,
const base::android::JavaRef<jobject>& bitmap,
int32_t width,
int32_t height,
jlong native_resource);
void RemoveResource(JNIEnv* env, int32_t res_type, int32_t res_id);
void ClearTintedResourceCache(JNIEnv* env);
void AssertResourceExists(JNIEnv* env, int32_t res_type, int32_t res_id);
// base::trace_event::MemoryDumpProvider implementation.
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
base::WeakPtr<ResourceManager> GetWeakPtr() override;
private:
friend class TestResourceManagerImpl;
// Start loading the resource. virtual for testing.
virtual void PreloadResourceFromJava(AndroidResourceType res_type,
int res_id);
virtual void RequestResourceFromJava(AndroidResourceType res_type,
int res_id);
// Remove tints that were unused in the current frame being built. This
// function takes a set |used_tints| and removes all the tints not in the set
// from the cache.
void RemoveUnusedTints();
using ResourceMap = std::unordered_map<int, std::unique_ptr<Resource>>;
using TintedResourceMap =
std::unordered_map<SkColor, std::unique_ptr<ResourceMap>>;
raw_ptr<cc::UIResourceManager> ui_resource_manager_;
std::array<ResourceMap, ANDROID_RESOURCE_TYPE_COUNT> resources_;
TintedResourceMap tinted_resources_;
// The set of tints that are used for resources in the current frame.
absl::flat_hash_set<SkColor> used_tints_;
absl::flat_hash_map<int, SkColor> tinted_resources_to_keep_;
base::android::ScopedJavaGlobalRef<jobject> java_obj_;
base::WeakPtrFactory<ResourceManagerImpl> weak_ptr_factory_{this};
};
} // namespace ui
#endif // UI_ANDROID_RESOURCES_RESOURCE_MANAGER_IMPL_H_