From 2219ef50b92db50f67ba5949e5a9ab4027e5db21 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 15 Jan 2015 11:09:56 -0800 Subject: [PATCH] The Sky compositor should recycle textures Previously, we were creating a new texture for every frame. Using tracing on a Nexus 5, that appears to cost about 6ms per frame on the GPU thread. After this CL, we keep a cache of recently used textures and only allocate a new one when either (1) we don't have one free or (2) our size requirements have changed. If our size requirements change, we dump the whole texture cache. In the future, we'll probably need something fancier if we ever need more than one size texture per frame. R=jamesr@chromium.org Review URL: https://codereview.chromium.org/845963006 --- engine/src/flutter/compositor/resource_manager.cc | 13 +++++++++++-- engine/src/flutter/compositor/resource_manager.h | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/compositor/resource_manager.cc b/engine/src/flutter/compositor/resource_manager.cc index fe6dc0ec8c..0169380c98 100644 --- a/engine/src/flutter/compositor/resource_manager.cc +++ b/engine/src/flutter/compositor/resource_manager.cc @@ -31,6 +31,16 @@ ResourceManager::~ResourceManager() { scoped_ptr ResourceManager::CreateTexture( const gfx::Size& size) { + if (!available_textures_.empty()) { + scoped_ptr texture(available_textures_.back()); + available_textures_.back() = nullptr; + available_textures_.pop_back(); + if (texture->size() == size) + return texture.Pass(); + // Currently we only support caching textures of a constant size. + available_textures_.clear(); + } + gl_context_->MakeCurrent(); return make_scoped_ptr(new mojo::GLTexture( gl_context_, mojo::TypeConverter::Convert(size))); @@ -82,9 +92,8 @@ void ResourceManager::ReturnResources( mojo::GLTexture* texture = iter->second; DCHECK_NE(0u, texture->texture_id()); resource_to_texture_map_.erase(iter); - // TODO(abarth): Consider recycling the texture. glWaitSyncPointCHROMIUM(resource->sync_point); - delete texture; + available_textures_.push_back(texture); } } diff --git a/engine/src/flutter/compositor/resource_manager.h b/engine/src/flutter/compositor/resource_manager.h index 9c9b5c97ec..cd1ebdde7e 100644 --- a/engine/src/flutter/compositor/resource_manager.h +++ b/engine/src/flutter/compositor/resource_manager.h @@ -6,6 +6,8 @@ #define SKY_COMPOSITOR_RESOURCE_MANAGER_H_ #include "base/containers/hash_tables.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/scoped_vector.h" #include "base/memory/weak_ptr.h" #include "mojo/services/surfaces/public/interfaces/surfaces.mojom.h" @@ -37,6 +39,8 @@ class ResourceManager { uint32_t next_resource_id_; base::hash_map resource_to_texture_map_; + ScopedVector available_textures_; + DISALLOW_COPY_AND_ASSIGN(ResourceManager); };