From 203678a81ac3097a83088934434fdefe66d5b8ef Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 23 Jan 2015 21:20:14 -0800 Subject: [PATCH] Sky shouldn't crash on device rotate There were two problems (both fixed in this CL): 1) When we were resized by the view manager, we forgot to deflate by the device-pixel-ratio when converting to engine types. That caused use to allocate a backing texture that was 9x what we needed. 2) When the surfaces system returned textures to us for re-use, we'd put them into the cache even if they were the old size. That caused us to thrash the texture cache. In this CL, we make the size of the textures in the cache explicit. R=eseidel@chromium.org BUG=449001 Review URL: https://codereview.chromium.org/868263002 --- engine/src/flutter/compositor/BUILD.gn | 2 + .../flutter/compositor/resource_manager.cc | 17 +++------ .../src/flutter/compositor/resource_manager.h | 6 +-- .../src/flutter/compositor/texture_cache.cc | 37 +++++++++++++++++++ engine/src/flutter/compositor/texture_cache.h | 35 ++++++++++++++++++ 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 engine/src/flutter/compositor/texture_cache.cc create mode 100644 engine/src/flutter/compositor/texture_cache.h diff --git a/engine/src/flutter/compositor/BUILD.gn b/engine/src/flutter/compositor/BUILD.gn index fdb5167827..27f59b0a1d 100644 --- a/engine/src/flutter/compositor/BUILD.gn +++ b/engine/src/flutter/compositor/BUILD.gn @@ -26,6 +26,8 @@ source_set("compositor") { "surface_allocator.h", "surface_holder.cc", "surface_holder.h", + "texture_cache.cc", + "texture_cache.h", ] deps = [ diff --git a/engine/src/flutter/compositor/resource_manager.cc b/engine/src/flutter/compositor/resource_manager.cc index 0169380c98..9a5eecd249 100644 --- a/engine/src/flutter/compositor/resource_manager.cc +++ b/engine/src/flutter/compositor/resource_manager.cc @@ -31,16 +31,9 @@ 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(); - } - + scoped_ptr texture = texture_cache_.GetTexture(size); + if (texture) + return texture.Pass(); gl_context_->MakeCurrent(); return make_scoped_ptr(new mojo::GLTexture( gl_context_, mojo::TypeConverter::Convert(size))); @@ -89,11 +82,11 @@ void ResourceManager::ReturnResources( auto iter = resource_to_texture_map_.find(resource->id); if (iter == resource_to_texture_map_.end()) continue; - mojo::GLTexture* texture = iter->second; + scoped_ptr texture(iter->second); DCHECK_NE(0u, texture->texture_id()); resource_to_texture_map_.erase(iter); glWaitSyncPointCHROMIUM(resource->sync_point); - available_textures_.push_back(texture); + texture_cache_.PutTexture(texture.Pass()); } } diff --git a/engine/src/flutter/compositor/resource_manager.h b/engine/src/flutter/compositor/resource_manager.h index cd1ebdde7e..e6fd329dd4 100644 --- a/engine/src/flutter/compositor/resource_manager.h +++ b/engine/src/flutter/compositor/resource_manager.h @@ -10,6 +10,7 @@ #include "base/memory/scoped_vector.h" #include "base/memory/weak_ptr.h" #include "mojo/services/surfaces/public/interfaces/surfaces.mojom.h" +#include "sky/compositor/texture_cache.h" namespace gfx { class Size; @@ -38,12 +39,11 @@ class ResourceManager { base::WeakPtr gl_context_; uint32_t next_resource_id_; base::hash_map resource_to_texture_map_; - - ScopedVector available_textures_; + TextureCache texture_cache_; DISALLOW_COPY_AND_ASSIGN(ResourceManager); }; -} // namespace examples +} // namespace sky #endif // SKY_COMPOSITOR_RESOURCE_MANAGER_H_ diff --git a/engine/src/flutter/compositor/texture_cache.cc b/engine/src/flutter/compositor/texture_cache.cc new file mode 100644 index 0000000000..05fa7d58eb --- /dev/null +++ b/engine/src/flutter/compositor/texture_cache.cc @@ -0,0 +1,37 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/compositor/texture_cache.h" + +#include "mojo/converters/geometry/geometry_type_converters.h" +#include "mojo/gpu/gl_texture.h" + +namespace sky { + +TextureCache::TextureCache() { +} + +TextureCache::~TextureCache() { +} + +scoped_ptr TextureCache::GetTexture(const gfx::Size& size) { + if (size != size_) { + available_textures_.clear(); + size_ = size; + } + if (available_textures_.empty()) + return nullptr; + scoped_ptr texture(available_textures_.back()); + available_textures_.back() = nullptr; + available_textures_.pop_back(); + return texture.Pass(); +} + +void TextureCache::PutTexture(scoped_ptr texture) { + if (texture->size() != size_) + return; + available_textures_.push_back(texture.release()); +} + +} // namespace sky diff --git a/engine/src/flutter/compositor/texture_cache.h b/engine/src/flutter/compositor/texture_cache.h new file mode 100644 index 0000000000..056112e0fa --- /dev/null +++ b/engine/src/flutter/compositor/texture_cache.h @@ -0,0 +1,35 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_COMPOSITOR_TEXTURE_CACHE_H_ +#define SKY_COMPOSITOR_TEXTURE_CACHE_H_ + +#include "base/memory/scoped_ptr.h" +#include "base/memory/scoped_vector.h" +#include "ui/gfx/geometry/size.h" + +namespace mojo { +class GLTexture; +} + +namespace sky { + +class TextureCache { + public: + TextureCache(); + ~TextureCache(); + + scoped_ptr GetTexture(const gfx::Size& size); + void PutTexture(scoped_ptr texture); + + private: + gfx::Size size_; + ScopedVector available_textures_; + + DISALLOW_COPY_AND_ASSIGN(TextureCache); +}; + +} // namespace sky + +#endif // SKY_COMPOSITOR_TEXTURE_CACHE_H_