| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/resources/resource_provider.h" | 5 #include "cc/resources/resource_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 : gl_(gl), query_id_(query_id) {} | 206 : gl_(gl), query_id_(query_id) {} |
| 207 | 207 |
| 208 // Overridden from ResourceProvider::Fence: | 208 // Overridden from ResourceProvider::Fence: |
| 209 void Set() override {} | 209 void Set() override {} |
| 210 bool HasPassed() override { | 210 bool HasPassed() override { |
| 211 unsigned available = 1; | 211 unsigned available = 1; |
| 212 gl_->GetQueryObjectuivEXT( | 212 gl_->GetQueryObjectuivEXT( |
| 213 query_id_, GL_QUERY_RESULT_AVAILABLE_EXT, &available); | 213 query_id_, GL_QUERY_RESULT_AVAILABLE_EXT, &available); |
| 214 return !!available; | 214 return !!available; |
| 215 } | 215 } |
| 216 void Wait() override { |
| 217 unsigned result = 0; |
| 218 gl_->GetQueryObjectuivEXT(query_id_, GL_QUERY_RESULT_EXT, &result); |
| 219 } |
| 216 | 220 |
| 217 private: | 221 private: |
| 218 ~QueryFence() override {} | 222 ~QueryFence() override {} |
| 219 | 223 |
| 220 gpu::gles2::GLES2Interface* gl_; | 224 gpu::gles2::GLES2Interface* gl_; |
| 221 unsigned query_id_; | 225 unsigned query_id_; |
| 222 | 226 |
| 223 DISALLOW_COPY_AND_ASSIGN(QueryFence); | 227 DISALLOW_COPY_AND_ASSIGN(QueryFence); |
| 224 }; | 228 }; |
| 225 | 229 |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 DCHECK_EQ(Bitmap, resource->type); | 721 DCHECK_EQ(Bitmap, resource->type); |
| 718 delete resource->shared_bitmap; | 722 delete resource->shared_bitmap; |
| 719 resource->pixels = NULL; | 723 resource->pixels = NULL; |
| 720 } | 724 } |
| 721 if (resource->pixels) { | 725 if (resource->pixels) { |
| 722 DCHECK(resource->origin == Resource::Internal); | 726 DCHECK(resource->origin == Resource::Internal); |
| 723 delete[] resource->pixels; | 727 delete[] resource->pixels; |
| 724 resource->pixels = NULL; | 728 resource->pixels = NULL; |
| 725 } | 729 } |
| 726 if (resource->gpu_memory_buffer) { | 730 if (resource->gpu_memory_buffer) { |
| 727 DCHECK(resource->origin != Resource::External); | 731 DCHECK(resource->origin == Resource::Internal); |
| 728 delete resource->gpu_memory_buffer; | 732 delete resource->gpu_memory_buffer; |
| 729 resource->gpu_memory_buffer = NULL; | 733 resource->gpu_memory_buffer = NULL; |
| 730 } | 734 } |
| 731 resources_.erase(it); | 735 resources_.erase(it); |
| 732 } | 736 } |
| 733 | 737 |
| 734 ResourceProvider::ResourceType ResourceProvider::GetResourceType( | 738 ResourceProvider::ResourceType ResourceProvider::GetResourceType( |
| 735 ResourceId id) { | 739 ResourceId id) { |
| 736 return GetResource(id)->type; | 740 return GetResource(id)->type; |
| 737 } | 741 } |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 ResourceProvider* resource_provider, | 1023 ResourceProvider* resource_provider, |
| 1020 ResourceProvider::ResourceId resource_id) | 1024 ResourceProvider::ResourceId resource_id) |
| 1021 : resource_provider_(resource_provider), | 1025 : resource_provider_(resource_provider), |
| 1022 resource_(resource_provider->LockForWrite(resource_id)) { | 1026 resource_(resource_provider->LockForWrite(resource_id)) { |
| 1023 ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource_); | 1027 ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource_); |
| 1024 DCHECK(valid()); | 1028 DCHECK(valid()); |
| 1025 sk_canvas_.reset(new SkCanvas(sk_bitmap_)); | 1029 sk_canvas_.reset(new SkCanvas(sk_bitmap_)); |
| 1026 } | 1030 } |
| 1027 | 1031 |
| 1028 ResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware() { | 1032 ResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware() { |
| 1033 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1029 resource_provider_->UnlockForWrite(resource_); | 1034 resource_provider_->UnlockForWrite(resource_); |
| 1030 } | 1035 } |
| 1031 | 1036 |
| 1032 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: | 1037 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: |
| 1033 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider, | 1038 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider, |
| 1034 ResourceProvider::ResourceId resource_id) | 1039 ResourceProvider::ResourceId resource_id) |
| 1035 : resource_provider_(resource_provider), | 1040 : resource_provider_(resource_provider), |
| 1036 resource_(resource_provider->LockForWrite(resource_id)), | 1041 resource_(resource_provider->LockForWrite(resource_id)), |
| 1037 gpu_memory_buffer_manager_(resource_provider->gpu_memory_buffer_manager_), | 1042 gpu_memory_buffer_manager_(resource_provider->gpu_memory_buffer_manager_), |
| 1038 gpu_memory_buffer_(nullptr), | 1043 gpu_memory_buffer_(nullptr), |
| 1039 size_(resource_->size), | 1044 size_(resource_->size), |
| 1040 format_(resource_->format) { | 1045 format_(resource_->format) { |
| 1041 DCHECK_EQ(GLTexture, resource_->type); | 1046 DCHECK_EQ(GLTexture, resource_->type); |
| 1042 std::swap(gpu_memory_buffer_, resource_->gpu_memory_buffer); | 1047 std::swap(gpu_memory_buffer_, resource_->gpu_memory_buffer); |
| 1043 } | 1048 } |
| 1044 | 1049 |
| 1045 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: | 1050 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: |
| 1046 ~ScopedWriteLockGpuMemoryBuffer() { | 1051 ~ScopedWriteLockGpuMemoryBuffer() { |
| 1052 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1047 resource_provider_->UnlockForWrite(resource_); | 1053 resource_provider_->UnlockForWrite(resource_); |
| 1048 if (!gpu_memory_buffer_) | 1054 if (!gpu_memory_buffer_) |
| 1049 return; | 1055 return; |
| 1050 | 1056 |
| 1051 if (!resource_->image_id) { | 1057 if (!resource_->image_id) { |
| 1052 GLES2Interface* gl = resource_provider_->ContextGL(); | 1058 GLES2Interface* gl = resource_provider_->ContextGL(); |
| 1053 DCHECK(gl); | 1059 DCHECK(gl); |
| 1054 | 1060 |
| 1055 resource_->image_id = | 1061 resource_->image_id = |
| 1056 gl->CreateImageCHROMIUM(gpu_memory_buffer_->AsClientBuffer(), | 1062 gl->CreateImageCHROMIUM(gpu_memory_buffer_->AsClientBuffer(), |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1082 } | 1088 } |
| 1083 | 1089 |
| 1084 ResourceProvider::ScopedWriteLockGr::ScopedWriteLockGr( | 1090 ResourceProvider::ScopedWriteLockGr::ScopedWriteLockGr( |
| 1085 ResourceProvider* resource_provider, | 1091 ResourceProvider* resource_provider, |
| 1086 ResourceProvider::ResourceId resource_id) | 1092 ResourceProvider::ResourceId resource_id) |
| 1087 : resource_provider_(resource_provider), | 1093 : resource_provider_(resource_provider), |
| 1088 resource_(resource_provider->LockForWrite(resource_id)) { | 1094 resource_(resource_provider->LockForWrite(resource_id)) { |
| 1089 } | 1095 } |
| 1090 | 1096 |
| 1091 ResourceProvider::ScopedWriteLockGr::~ScopedWriteLockGr() { | 1097 ResourceProvider::ScopedWriteLockGr::~ScopedWriteLockGr() { |
| 1098 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1092 resource_provider_->UnlockForWrite(resource_); | 1099 resource_provider_->UnlockForWrite(resource_); |
| 1093 } | 1100 } |
| 1094 | 1101 |
| 1095 SkSurface* ResourceProvider::ScopedWriteLockGr::GetSkSurface( | 1102 SkSurface* ResourceProvider::ScopedWriteLockGr::GetSkSurface( |
| 1096 bool use_distance_field_text) { | 1103 bool use_distance_field_text) { |
| 1104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1097 DCHECK(resource_->locked_for_write); | 1105 DCHECK(resource_->locked_for_write); |
| 1098 | 1106 |
| 1099 // If the surface doesn't exist, or doesn't have the correct dff setting, | 1107 // If the surface doesn't exist, or doesn't have the correct dff setting, |
| 1100 // recreate the surface within the resource. | 1108 // recreate the surface within the resource. |
| 1101 if (!resource_->sk_surface || | 1109 if (!resource_->sk_surface || |
| 1102 use_distance_field_text != | 1110 use_distance_field_text != |
| 1103 resource_->sk_surface->props().isUseDistanceFieldFonts()) { | 1111 resource_->sk_surface->props().isUseDistanceFieldFonts()) { |
| 1104 class GrContext* gr_context = resource_provider_->GrContext(); | 1112 class GrContext* gr_context = resource_provider_->GrContext(); |
| 1105 // TODO(alokp): Implement TestContextProvider::GrContext(). | 1113 // TODO(alokp): Implement TestContextProvider::GrContext(). |
| 1106 if (!gr_context) | 1114 if (!gr_context) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1119 skia::AdoptRef(gr_context->wrapBackendTexture(desc)); | 1127 skia::AdoptRef(gr_context->wrapBackendTexture(desc)); |
| 1120 SkSurface::TextRenderMode text_render_mode = | 1128 SkSurface::TextRenderMode text_render_mode = |
| 1121 use_distance_field_text ? SkSurface::kDistanceField_TextRenderMode | 1129 use_distance_field_text ? SkSurface::kDistanceField_TextRenderMode |
| 1122 : SkSurface::kStandard_TextRenderMode; | 1130 : SkSurface::kStandard_TextRenderMode; |
| 1123 resource_->sk_surface = skia::AdoptRef(SkSurface::NewRenderTargetDirect( | 1131 resource_->sk_surface = skia::AdoptRef(SkSurface::NewRenderTargetDirect( |
| 1124 gr_texture->asRenderTarget(), text_render_mode)); | 1132 gr_texture->asRenderTarget(), text_render_mode)); |
| 1125 } | 1133 } |
| 1126 return resource_->sk_surface.get(); | 1134 return resource_->sk_surface.get(); |
| 1127 } | 1135 } |
| 1128 | 1136 |
| 1137 ResourceProvider::SynchronousFence::SynchronousFence( |
| 1138 gpu::gles2::GLES2Interface* gl) |
| 1139 : gl_(gl), has_synchronized_(true) { |
| 1140 } |
| 1141 |
| 1142 ResourceProvider::SynchronousFence::~SynchronousFence() { |
| 1143 } |
| 1144 |
| 1145 void ResourceProvider::SynchronousFence::Set() { |
| 1146 has_synchronized_ = false; |
| 1147 } |
| 1148 |
| 1149 bool ResourceProvider::SynchronousFence::HasPassed() { |
| 1150 if (!has_synchronized_) { |
| 1151 has_synchronized_ = true; |
| 1152 Synchronize(); |
| 1153 } |
| 1154 return true; |
| 1155 } |
| 1156 |
| 1157 void ResourceProvider::SynchronousFence::Wait() { |
| 1158 HasPassed(); |
| 1159 } |
| 1160 |
| 1161 void ResourceProvider::SynchronousFence::Synchronize() { |
| 1162 TRACE_EVENT0("cc", "ResourceProvider::SynchronousFence::Synchronize"); |
| 1163 gl_->Finish(); |
| 1164 } |
| 1165 |
| 1129 ResourceProvider::ResourceProvider( | 1166 ResourceProvider::ResourceProvider( |
| 1130 OutputSurface* output_surface, | 1167 OutputSurface* output_surface, |
| 1131 SharedBitmapManager* shared_bitmap_manager, | 1168 SharedBitmapManager* shared_bitmap_manager, |
| 1132 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, | 1169 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager, |
| 1133 BlockingTaskRunner* blocking_main_thread_task_runner, | 1170 BlockingTaskRunner* blocking_main_thread_task_runner, |
| 1134 int highp_threshold_min, | 1171 int highp_threshold_min, |
| 1135 bool use_rgba_4444_texture_format, | 1172 bool use_rgba_4444_texture_format, |
| 1136 size_t id_allocation_chunk_size) | 1173 size_t id_allocation_chunk_size) |
| 1137 : output_surface_(output_surface), | 1174 : output_surface_(output_surface), |
| 1138 shared_bitmap_manager_(shared_bitmap_manager), | 1175 shared_bitmap_manager_(shared_bitmap_manager), |
| (...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2012 DCHECK_EQ(source_resource->type, dest_resource->type); | 2049 DCHECK_EQ(source_resource->type, dest_resource->type); |
| 2013 DCHECK_EQ(source_resource->format, dest_resource->format); | 2050 DCHECK_EQ(source_resource->format, dest_resource->format); |
| 2014 DCHECK(source_resource->size == dest_resource->size); | 2051 DCHECK(source_resource->size == dest_resource->size); |
| 2015 | 2052 |
| 2016 GLES2Interface* gl = ContextGL(); | 2053 GLES2Interface* gl = ContextGL(); |
| 2017 DCHECK(gl); | 2054 DCHECK(gl); |
| 2018 if (source_resource->image_id && source_resource->dirty_image) { | 2055 if (source_resource->image_id && source_resource->dirty_image) { |
| 2019 gl->BindTexture(source_resource->target, source_resource->gl_id); | 2056 gl->BindTexture(source_resource->target, source_resource->gl_id); |
| 2020 BindImageForSampling(source_resource); | 2057 BindImageForSampling(source_resource); |
| 2021 } | 2058 } |
| 2022 DCHECK(use_sync_query_) << "CHROMIUM_sync_query extension missing"; | 2059 if (use_sync_query_) { |
| 2023 if (!source_resource->gl_read_lock_query_id) | 2060 if (!source_resource->gl_read_lock_query_id) |
| 2024 gl->GenQueriesEXT(1, &source_resource->gl_read_lock_query_id); | 2061 gl->GenQueriesEXT(1, &source_resource->gl_read_lock_query_id); |
| 2025 gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, | 2062 gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM, |
| 2026 source_resource->gl_read_lock_query_id); | 2063 source_resource->gl_read_lock_query_id); |
| 2064 } |
| 2027 DCHECK(!dest_resource->image_id); | 2065 DCHECK(!dest_resource->image_id); |
| 2028 dest_resource->allocated = true; | 2066 dest_resource->allocated = true; |
| 2029 gl->CopyTextureCHROMIUM(dest_resource->target, | 2067 gl->CopyTextureCHROMIUM(dest_resource->target, |
| 2030 source_resource->gl_id, | 2068 source_resource->gl_id, |
| 2031 dest_resource->gl_id, | 2069 dest_resource->gl_id, |
| 2032 0, | 2070 0, |
| 2033 GLInternalFormat(dest_resource->format), | 2071 GLInternalFormat(dest_resource->format), |
| 2034 GLDataType(dest_resource->format)); | 2072 GLDataType(dest_resource->format)); |
| 2035 // End query and create a read lock fence that will prevent access to | 2073 if (source_resource->gl_read_lock_query_id) { |
| 2036 // source resource until CopyTextureCHROMIUM command has completed. | 2074 // End query and create a read lock fence that will prevent access to |
| 2037 gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM); | 2075 // source resource until CopyTextureCHROMIUM command has completed. |
| 2038 source_resource->read_lock_fence = make_scoped_refptr( | 2076 gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM); |
| 2039 new QueryFence(gl, source_resource->gl_read_lock_query_id)); | 2077 source_resource->read_lock_fence = make_scoped_refptr( |
| 2078 new QueryFence(gl, source_resource->gl_read_lock_query_id)); |
| 2079 } else { |
| 2080 // Create a SynchronousFence when CHROMIUM_sync_query extension is missing. |
| 2081 // Try to use one synchronous fence for as many CopyResource operations as |
| 2082 // possible as that reduce the number of times we have to synchronize with |
| 2083 // the GL. |
| 2084 if (!synchronous_fence_.get() || synchronous_fence_->has_synchronized()) |
| 2085 synchronous_fence_ = make_scoped_refptr(new SynchronousFence(gl)); |
| 2086 source_resource->read_lock_fence = synchronous_fence_; |
| 2087 source_resource->read_lock_fence->Set(); |
| 2088 } |
| 2040 } | 2089 } |
| 2041 | 2090 |
| 2042 void ResourceProvider::WaitSyncPointIfNeeded(ResourceId id) { | 2091 void ResourceProvider::WaitSyncPointIfNeeded(ResourceId id) { |
| 2043 Resource* resource = GetResource(id); | 2092 Resource* resource = GetResource(id); |
| 2044 DCHECK_EQ(resource->exported_count, 0); | 2093 DCHECK_EQ(resource->exported_count, 0); |
| 2045 DCHECK(resource->allocated); | 2094 DCHECK(resource->allocated); |
| 2046 if (resource->type != GLTexture || resource->gl_id) | 2095 if (resource->type != GLTexture || resource->gl_id) |
| 2047 return; | 2096 return; |
| 2048 if (!resource->mailbox.sync_point()) | 2097 if (!resource->mailbox.sync_point()) |
| 2049 return; | 2098 return; |
| 2050 DCHECK(resource->mailbox.IsValid()); | 2099 DCHECK(resource->mailbox.IsValid()); |
| 2051 GLES2Interface* gl = ContextGL(); | 2100 GLES2Interface* gl = ContextGL(); |
| 2052 DCHECK(gl); | 2101 DCHECK(gl); |
| 2053 GLC(gl, gl->WaitSyncPointCHROMIUM(resource->mailbox.sync_point())); | 2102 GLC(gl, gl->WaitSyncPointCHROMIUM(resource->mailbox.sync_point())); |
| 2054 resource->mailbox.set_sync_point(0); | 2103 resource->mailbox.set_sync_point(0); |
| 2055 } | 2104 } |
| 2056 | 2105 |
| 2106 void ResourceProvider::WaitReadLockIfNeeded(ResourceId id) { |
| 2107 Resource* resource = GetResource(id); |
| 2108 DCHECK_EQ(resource->exported_count, 0); |
| 2109 if (!resource->read_lock_fence.get()) |
| 2110 return; |
| 2111 |
| 2112 resource->read_lock_fence->Wait(); |
| 2113 } |
| 2114 |
| 2057 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { | 2115 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { |
| 2058 GLint active_unit = 0; | 2116 GLint active_unit = 0; |
| 2059 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); | 2117 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); |
| 2060 return active_unit; | 2118 return active_unit; |
| 2061 } | 2119 } |
| 2062 | 2120 |
| 2063 GLES2Interface* ResourceProvider::ContextGL() const { | 2121 GLES2Interface* ResourceProvider::ContextGL() const { |
| 2064 ContextProvider* context_provider = output_surface_->context_provider(); | 2122 ContextProvider* context_provider = output_surface_->context_provider(); |
| 2065 return context_provider ? context_provider->ContextGL() : NULL; | 2123 return context_provider ? context_provider->ContextGL() : NULL; |
| 2066 } | 2124 } |
| 2067 | 2125 |
| 2068 class GrContext* ResourceProvider::GrContext() const { | 2126 class GrContext* ResourceProvider::GrContext() const { |
| 2069 ContextProvider* context_provider = output_surface_->context_provider(); | 2127 ContextProvider* context_provider = output_surface_->context_provider(); |
| 2070 return context_provider ? context_provider->GrContext() : NULL; | 2128 return context_provider ? context_provider->GrContext() : NULL; |
| 2071 } | 2129 } |
| 2072 | 2130 |
| 2073 } // namespace cc | 2131 } // namespace cc |
| OLD | NEW |