Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: cc/resources/resource_provider.cc

Issue 1951193002: cc: Add mailbox support to ResourceProvider write locks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@worker_context_stream
Patch Set: vmpstr's review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/resources/resource_provider.h ('k') | cc/resources/resource_provider_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 return; 688 return;
689 } else { 689 } else {
690 DeleteResourceInternal(it, NORMAL); 690 DeleteResourceInternal(it, NORMAL);
691 } 691 }
692 } 692 }
693 693
694 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it, 694 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it,
695 DeleteStyle style) { 695 DeleteStyle style) {
696 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal"); 696 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal");
697 Resource* resource = &it->second; 697 Resource* resource = &it->second;
698 bool lost_resource = resource->lost; 698 DCHECK(resource->exported_count == 0 || style != NORMAL);
699 699
700 DCHECK(resource->exported_count == 0 || style != NORMAL); 700 // Exported resources are lost on shutdown.
701 if (style == FOR_SHUTDOWN && resource->exported_count > 0) 701 bool exported_resource_lost =
702 lost_resource = true; 702 style == FOR_SHUTDOWN && resource->exported_count > 0;
703 // GPU resources are lost when output surface is lost.
704 bool gpu_resource_lost =
705 IsGpuResourceType(resource->type) && lost_output_surface_;
706 bool lost_resource =
707 resource->lost || exported_resource_lost || gpu_resource_lost;
708
709 if (!lost_resource &&
710 resource->synchronization_state() == Resource::NEEDS_WAIT) {
711 DCHECK(resource->allocated);
712 DCHECK(IsGpuResourceType(resource->type));
713 GLES2Interface* gl = ContextGL();
714 DCHECK(gl);
715 resource->WaitSyncToken(gl);
716 }
703 717
704 if (resource->image_id) { 718 if (resource->image_id) {
705 DCHECK(resource->origin == Resource::INTERNAL); 719 DCHECK(resource->origin == Resource::INTERNAL);
706 GLES2Interface* gl = ContextGL(); 720 GLES2Interface* gl = ContextGL();
707 DCHECK(gl); 721 DCHECK(gl);
708 gl->DestroyImageCHROMIUM(resource->image_id); 722 gl->DestroyImageCHROMIUM(resource->image_id);
709 } 723 }
710 if (resource->gl_upload_query_id) { 724 if (resource->gl_upload_query_id) {
711 DCHECK(resource->origin == Resource::INTERNAL); 725 DCHECK(resource->origin == Resource::INTERNAL);
712 GLES2Interface* gl = ContextGL(); 726 GLES2Interface* gl = ContextGL();
(...skipping 10 matching lines...) Expand all
723 DCHECK(resource->origin == Resource::INTERNAL); 737 DCHECK(resource->origin == Resource::INTERNAL);
724 GLES2Interface* gl = ContextGL(); 738 GLES2Interface* gl = ContextGL();
725 DCHECK(gl); 739 DCHECK(gl);
726 gl->DeleteBuffers(1, &resource->gl_pixel_buffer_id); 740 gl->DeleteBuffers(1, &resource->gl_pixel_buffer_id);
727 } 741 }
728 if (resource->origin == Resource::EXTERNAL) { 742 if (resource->origin == Resource::EXTERNAL) {
729 DCHECK(resource->mailbox().IsValid()); 743 DCHECK(resource->mailbox().IsValid());
730 gpu::SyncToken sync_token = resource->mailbox().sync_token(); 744 gpu::SyncToken sync_token = resource->mailbox().sync_token();
731 if (IsGpuResourceType(resource->type)) { 745 if (IsGpuResourceType(resource->type)) {
732 DCHECK(resource->mailbox().IsTexture()); 746 DCHECK(resource->mailbox().IsTexture());
733 lost_resource |= lost_output_surface_;
734 GLES2Interface* gl = ContextGL(); 747 GLES2Interface* gl = ContextGL();
735 DCHECK(gl); 748 DCHECK(gl);
736 if (resource->gl_id) { 749 if (resource->gl_id) {
737 gl->DeleteTextures(1, &resource->gl_id); 750 gl->DeleteTextures(1, &resource->gl_id);
738 resource->gl_id = 0; 751 resource->gl_id = 0;
739 if (!lost_resource) { 752 if (!lost_resource) {
740 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM(); 753 const GLuint64 fence_sync = gl->InsertFenceSyncCHROMIUM();
741 gl->ShallowFlushCHROMIUM(); 754 gl->ShallowFlushCHROMIUM();
742 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); 755 gl->GenSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
743 } 756 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 DCHECK(resource->allocated); 822 DCHECK(resource->allocated);
810 DCHECK_EQ(RGBA_8888, resource->format); 823 DCHECK_EQ(RGBA_8888, resource->format);
811 SkImageInfo source_info = 824 SkImageInfo source_info =
812 SkImageInfo::MakeN32Premul(image_size.width(), image_size.height()); 825 SkImageInfo::MakeN32Premul(image_size.width(), image_size.height());
813 size_t image_stride = image_size.width() * 4; 826 size_t image_stride = image_size.width() * 4;
814 827
815 ScopedWriteLockSoftware lock(this, id); 828 ScopedWriteLockSoftware lock(this, id);
816 SkCanvas dest(lock.sk_bitmap()); 829 SkCanvas dest(lock.sk_bitmap());
817 dest.writePixels(source_info, image, image_stride, 0, 0); 830 dest.writePixels(source_info, image, image_stride, 0, 0);
818 } else { 831 } else {
819 ScopedWriteLockGL lock(this, id); 832 ScopedWriteLockGL lock(this, id, false);
820 DCHECK(lock.texture_id()); 833 unsigned resource_texture_id = lock.texture_id();
834 DCHECK(resource_texture_id);
821 GLES2Interface* gl = ContextGL(); 835 GLES2Interface* gl = ContextGL();
822 DCHECK(gl); 836 DCHECK(gl);
823 gl->BindTexture(resource->target, lock.texture_id()); 837 gl->BindTexture(resource->target, resource_texture_id);
824 if (resource->format == ETC1) { 838 if (resource->format == ETC1) {
825 DCHECK_EQ(resource->target, static_cast<GLenum>(GL_TEXTURE_2D)); 839 DCHECK_EQ(resource->target, static_cast<GLenum>(GL_TEXTURE_2D));
826 int image_bytes = ResourceUtil::CheckedSizeInBytes<int>(image_size, ETC1); 840 int image_bytes = ResourceUtil::CheckedSizeInBytes<int>(image_size, ETC1);
827 gl->CompressedTexImage2D(resource->target, 0, GLInternalFormat(ETC1), 841 gl->CompressedTexImage2D(resource->target, 0, GLInternalFormat(ETC1),
828 image_size.width(), image_size.height(), 0, 842 image_size.width(), image_size.height(), 0,
829 image_bytes, image); 843 image_bytes, image);
830 } else { 844 } else {
831 gl->TexSubImage2D(resource->target, 0, 0, 0, image_size.width(), 845 gl->TexSubImage2D(resource->target, 0, 0, 0, image_size.width(),
832 image_size.height(), GLDataFormat(resource->format), 846 image_size.height(), GLDataFormat(resource->format),
833 GLDataType(resource->format), image); 847 GLDataType(resource->format), image);
834 } 848 }
835 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); 849 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
836 gl->OrderingBarrierCHROMIUM(); 850 gl->OrderingBarrierCHROMIUM();
837 gpu::SyncToken sync_token; 851 gpu::SyncToken sync_token;
838 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); 852 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
839 lock.UpdateResourceSyncToken(sync_token); 853 lock.set_sync_token(sync_token);
854 lock.set_synchronized(true);
840 } 855 }
841 } 856 }
842 857
843 void ResourceProvider::GenerateSyncTokenForResource(ResourceId resource_id) { 858 void ResourceProvider::GenerateSyncTokenForResource(ResourceId resource_id) {
844 Resource* resource = GetResource(resource_id); 859 Resource* resource = GetResource(resource_id);
845 if (!resource->needs_sync_token()) 860 if (!resource->needs_sync_token())
846 return; 861 return;
847 862
848 gpu::SyncToken sync_token; 863 gpu::SyncToken sync_token;
849 GLES2Interface* gl = ContextGL(); 864 GLES2Interface* gl = ContextGL();
850 DCHECK(gl); 865 DCHECK(gl);
851 866
852 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); 867 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
853 gl->OrderingBarrierCHROMIUM(); 868 gl->OrderingBarrierCHROMIUM();
854 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); 869 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
855 870
856 resource->UpdateSyncToken(sync_token); 871 resource->UpdateSyncToken(sync_token);
872 resource->SetSynchronized();
857 } 873 }
858 874
859 void ResourceProvider::GenerateSyncTokenForResources( 875 void ResourceProvider::GenerateSyncTokenForResources(
860 const ResourceIdArray& resource_ids) { 876 const ResourceIdArray& resource_ids) {
861 gpu::SyncToken sync_token; 877 gpu::SyncToken sync_token;
862 bool created_sync_token = false; 878 bool created_sync_token = false;
863 for (ResourceId id : resource_ids) { 879 for (ResourceId id : resource_ids) {
864 Resource* resource = GetResource(id); 880 Resource* resource = GetResource(id);
865 if (resource->needs_sync_token()) { 881 if (resource->needs_sync_token()) {
866 if (!created_sync_token) { 882 if (!created_sync_token) {
867 GLES2Interface* gl = ContextGL(); 883 GLES2Interface* gl = ContextGL();
868 DCHECK(gl); 884 DCHECK(gl);
869 885
870 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); 886 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM();
871 gl->OrderingBarrierCHROMIUM(); 887 gl->OrderingBarrierCHROMIUM();
872 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); 888 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData());
873 created_sync_token = true; 889 created_sync_token = true;
874 } 890 }
875 891
876 resource->UpdateSyncToken(sync_token); 892 resource->UpdateSyncToken(sync_token);
893 resource->SetSynchronized();
877 } 894 }
878 } 895 }
879 } 896 }
880 897
881 ResourceProvider::Resource* ResourceProvider::InsertResource( 898 ResourceProvider::Resource* ResourceProvider::InsertResource(
882 ResourceId id, 899 ResourceId id,
883 Resource resource) { 900 Resource resource) {
884 std::pair<ResourceMap::iterator, bool> result = 901 std::pair<ResourceMap::iterator, bool> result =
885 resources_.insert(ResourceMap::value_type(id, std::move(resource))); 902 resources_.insert(ResourceMap::value_type(id, std::move(resource)));
886 DCHECK(result.second); 903 DCHECK(result.second);
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 ResourceIdArray unused; 992 ResourceIdArray unused;
976 unused.push_back(id); 993 unused.push_back(id);
977 DeleteAndReturnUnusedResourcesToChild(child_it, NORMAL, unused); 994 DeleteAndReturnUnusedResourcesToChild(child_it, NORMAL, unused);
978 } 995 }
979 } 996 }
980 } 997 }
981 998
982 ResourceProvider::Resource* ResourceProvider::LockForWrite(ResourceId id) { 999 ResourceProvider::Resource* ResourceProvider::LockForWrite(ResourceId id) {
983 Resource* resource = GetResource(id); 1000 Resource* resource = GetResource(id);
984 DCHECK(CanLockForWrite(id)); 1001 DCHECK(CanLockForWrite(id));
985 DCHECK_NE(Resource::NEEDS_WAIT, resource->synchronization_state()); 1002 if (resource->allocated)
1003 WaitSyncTokenIfNeeded(id);
986 resource->locked_for_write = true; 1004 resource->locked_for_write = true;
987 resource->SetLocallyUsed(); 1005 resource->SetLocallyUsed();
988 return resource; 1006 return resource;
989 } 1007 }
990 1008
991 bool ResourceProvider::CanLockForWrite(ResourceId id) { 1009 bool ResourceProvider::CanLockForWrite(ResourceId id) {
992 Resource* resource = GetResource(id); 1010 Resource* resource = GetResource(id);
993 return !resource->locked_for_write && !resource->lock_for_read_count && 1011 return !resource->locked_for_write && !resource->lock_for_read_count &&
994 !resource->exported_count && resource->origin == Resource::INTERNAL && 1012 !resource->exported_count && resource->origin == Resource::INTERNAL &&
995 !resource->lost && ReadLockFenceHasPassed(resource); 1013 !resource->lost && ReadLockFenceHasPassed(resource);
996 } 1014 }
997 1015
998 bool ResourceProvider::IsOverlayCandidate(ResourceId id) { 1016 bool ResourceProvider::IsOverlayCandidate(ResourceId id) {
999 Resource* resource = GetResource(id); 1017 Resource* resource = GetResource(id);
1000 return resource->is_overlay_candidate; 1018 return resource->is_overlay_candidate;
1001 } 1019 }
1002 1020
1003 void ResourceProvider::UnlockForWrite(ResourceProvider::Resource* resource) { 1021 void ResourceProvider::UnlockForWrite(Resource* resource) {
1004 DCHECK(resource->locked_for_write); 1022 DCHECK(resource->locked_for_write);
1005 DCHECK_EQ(resource->exported_count, 0); 1023 DCHECK_EQ(resource->exported_count, 0);
1006 DCHECK(resource->origin == Resource::INTERNAL); 1024 DCHECK(resource->origin == Resource::INTERNAL);
1007 resource->locked_for_write = false; 1025 resource->locked_for_write = false;
1008 resource->SetSynchronized();
1009 } 1026 }
1010 1027
1011 void ResourceProvider::EnableReadLockFencesForTesting(ResourceId id) { 1028 void ResourceProvider::EnableReadLockFencesForTesting(ResourceId id) {
1012 Resource* resource = GetResource(id); 1029 Resource* resource = GetResource(id);
1013 DCHECK(resource); 1030 DCHECK(resource);
1014 resource->read_lock_fences_enabled = true; 1031 resource->read_lock_fences_enabled = true;
1015 } 1032 }
1016 1033
1017 ResourceProvider::ScopedReadLockGL::ScopedReadLockGL( 1034 ResourceProvider::ScopedReadLockGL::ScopedReadLockGL(
1018 ResourceProvider* resource_provider, 1035 ResourceProvider* resource_provider,
1019 ResourceId resource_id) 1036 ResourceId resource_id)
1020 : resource_provider_(resource_provider), 1037 : resource_provider_(resource_provider), resource_id_(resource_id) {
1021 resource_id_(resource_id), 1038 const Resource* resource = resource_provider->LockForRead(resource_id, false);
1022 resource_(resource_provider->LockForRead(resource_id, false)) { 1039 texture_id_ = resource->gl_id;
1023 DCHECK(resource_); 1040 target_ = resource->target;
1041 size_ = resource->size;
1024 } 1042 }
1025 1043
1026 ResourceProvider::ScopedReadLockGL::~ScopedReadLockGL() { 1044 ResourceProvider::ScopedReadLockGL::~ScopedReadLockGL() {
1027 resource_provider_->UnlockForRead(resource_id_); 1045 resource_provider_->UnlockForRead(resource_id_);
1028 } 1046 }
1029 1047
1030 ResourceProvider::ScopedSamplerGL::ScopedSamplerGL( 1048 ResourceProvider::ScopedSamplerGL::ScopedSamplerGL(
1031 ResourceProvider* resource_provider, 1049 ResourceProvider* resource_provider,
1032 ResourceId resource_id, 1050 ResourceId resource_id,
1033 GLenum filter) 1051 GLenum filter)
1034 : ScopedReadLockGL(resource_provider, resource_id), 1052 : resource_lock_(resource_provider, resource_id),
1035 unit_(GL_TEXTURE0), 1053 unit_(GL_TEXTURE0),
1036 target_(resource_provider->BindForSampling(resource_id, unit_, filter)) { 1054 target_(resource_provider->BindForSampling(resource_id, unit_, filter)) {}
1037 }
1038 1055
1039 ResourceProvider::ScopedSamplerGL::ScopedSamplerGL( 1056 ResourceProvider::ScopedSamplerGL::ScopedSamplerGL(
1040 ResourceProvider* resource_provider, 1057 ResourceProvider* resource_provider,
1041 ResourceId resource_id, 1058 ResourceId resource_id,
1042 GLenum unit, 1059 GLenum unit,
1043 GLenum filter) 1060 GLenum filter)
1044 : ScopedReadLockGL(resource_provider, resource_id), 1061 : resource_lock_(resource_provider, resource_id),
1045 unit_(unit), 1062 unit_(unit),
1046 target_(resource_provider->BindForSampling(resource_id, unit_, filter)) { 1063 target_(resource_provider->BindForSampling(resource_id, unit_, filter)) {}
1047 }
1048 1064
1049 ResourceProvider::ScopedSamplerGL::~ScopedSamplerGL() { 1065 ResourceProvider::ScopedSamplerGL::~ScopedSamplerGL() {
1050 } 1066 }
1051 1067
1052 ResourceProvider::ScopedWriteLockGL::ScopedWriteLockGL( 1068 ResourceProvider::ScopedWriteLockGL::ScopedWriteLockGL(
1053 ResourceProvider* resource_provider, 1069 ResourceProvider* resource_provider,
1054 ResourceId resource_id) 1070 ResourceId resource_id,
1071 bool create_mailbox)
1055 : resource_provider_(resource_provider), 1072 : resource_provider_(resource_provider),
1056 resource_(resource_provider->LockForWrite(resource_id)), 1073 resource_id_(resource_id),
1057 texture_id_(0), 1074 synchronized_(false) {
1058 set_sync_token_(false) { 1075 DCHECK(thread_checker_.CalledOnValidThread());
1059 resource_provider_->LazyAllocate(resource_); 1076 Resource* resource = resource_provider->LockForWrite(resource_id);
1060 texture_id_ = resource_->gl_id; 1077 resource_provider_->LazyAllocate(resource);
1061 DCHECK(texture_id_); 1078 if (resource->image_id && resource->dirty_image)
1062 if (resource_->image_id && resource_->dirty_image) 1079 resource_provider_->BindImageForSampling(resource);
1063 resource_provider_->BindImageForSampling(resource_); 1080 if (create_mailbox) {
1081 resource_provider_->CreateMailboxAndBindResource(
1082 resource_provider_->ContextGL(), resource);
1083 }
1084 texture_id_ = resource->gl_id;
1085 target_ = resource->target;
1086 format_ = resource->format;
1087 size_ = resource->size;
1088 mailbox_ = resource->mailbox();
1064 } 1089 }
1065 1090
1066 ResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL() { 1091 ResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL() {
1067 if (set_sync_token_) 1092 DCHECK(thread_checker_.CalledOnValidThread());
1068 resource_->UpdateSyncToken(sync_token_); 1093 Resource* resource = resource_provider_->GetResource(resource_id_);
1069 resource_provider_->UnlockForWrite(resource_); 1094 DCHECK(resource->locked_for_write);
1095 if (sync_token_.HasData())
1096 resource->UpdateSyncToken(sync_token_);
1097 if (synchronized_)
1098 resource->SetSynchronized();
1099 resource_provider_->UnlockForWrite(resource);
1100 }
1101
1102 ResourceProvider::ScopedTextureProvider::ScopedTextureProvider(
1103 gpu::gles2::GLES2Interface* gl,
1104 ScopedWriteLockGL* resource_lock,
1105 bool use_mailbox)
1106 : gl_(gl), use_mailbox_(use_mailbox) {
1107 if (use_mailbox_) {
1108 texture_id_ = gl_->CreateAndConsumeTextureCHROMIUM(
1109 resource_lock->target(), resource_lock->mailbox().name());
1110 } else {
1111 texture_id_ = resource_lock->texture_id();
1112 }
1113 DCHECK(texture_id_);
1114 }
1115
1116 ResourceProvider::ScopedTextureProvider::~ScopedTextureProvider() {
1117 if (use_mailbox_)
1118 gl_->DeleteTextures(1, &texture_id_);
1119 }
1120
1121 ResourceProvider::ScopedSkSurfaceProvider::ScopedSkSurfaceProvider(
1122 ContextProvider* context_provider,
1123 ScopedWriteLockGL* resource_lock,
1124 bool use_mailbox,
1125 bool use_distance_field_text,
1126 bool can_use_lcd_text,
1127 int msaa_sample_count)
1128 : texture_provider_(context_provider->ContextGL(),
1129 resource_lock,
1130 use_mailbox) {
1131 GrGLTextureInfo texture_info;
1132 texture_info.fID = texture_provider_.texture_id();
1133 texture_info.fTarget = resource_lock->target();
1134 GrBackendTextureDesc desc;
1135 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
1136 desc.fWidth = resource_lock->size().width();
1137 desc.fHeight = resource_lock->size().height();
1138 desc.fConfig = ToGrPixelConfig(resource_lock->format());
1139 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
1140 desc.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(texture_info);
1141 desc.fSampleCnt = msaa_sample_count;
1142
1143 uint32_t flags =
1144 use_distance_field_text ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
1145 // Use unknown pixel geometry to disable LCD text.
1146 SkSurfaceProps surface_props(flags, kUnknown_SkPixelGeometry);
1147 if (can_use_lcd_text) {
1148 // LegacyFontHost will get LCD text and skia figures out what type to use.
1149 surface_props =
1150 SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1151 }
1152 sk_surface_ = SkSurface::MakeFromBackendTextureAsRenderTarget(
1153 context_provider->GrContext(), desc, &surface_props);
1154 }
1155
1156 ResourceProvider::ScopedSkSurfaceProvider::~ScopedSkSurfaceProvider() {
1157 if (sk_surface_.get()) {
1158 sk_surface_->prepareForExternalIO();
1159 sk_surface_.reset();
1160 }
1070 } 1161 }
1071 1162
1072 void ResourceProvider::PopulateSkBitmapWithResource( 1163 void ResourceProvider::PopulateSkBitmapWithResource(
1073 SkBitmap* sk_bitmap, const Resource* resource) { 1164 SkBitmap* sk_bitmap, const Resource* resource) {
1074 DCHECK_EQ(RGBA_8888, resource->format); 1165 DCHECK_EQ(RGBA_8888, resource->format);
1075 SkImageInfo info = SkImageInfo::MakeN32Premul(resource->size.width(), 1166 SkImageInfo info = SkImageInfo::MakeN32Premul(resource->size.width(),
1076 resource->size.height()); 1167 resource->size.height());
1077 sk_bitmap->installPixels(info, resource->pixels, info.minRowBytes()); 1168 sk_bitmap->installPixels(info, resource->pixels, info.minRowBytes());
1078 } 1169 }
1079 1170
1080 ResourceProvider::ScopedReadLockSoftware::ScopedReadLockSoftware( 1171 ResourceProvider::ScopedReadLockSoftware::ScopedReadLockSoftware(
1081 ResourceProvider* resource_provider, 1172 ResourceProvider* resource_provider,
1082 ResourceId resource_id) 1173 ResourceId resource_id)
1083 : resource_provider_(resource_provider), resource_id_(resource_id) { 1174 : resource_provider_(resource_provider), resource_id_(resource_id) {
1084 const Resource* resource = resource_provider->LockForRead(resource_id, false); 1175 const Resource* resource = resource_provider->LockForRead(resource_id, false);
1085 ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource); 1176 ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource);
1086 } 1177 }
1087 1178
1088 ResourceProvider::ScopedReadLockSoftware::~ScopedReadLockSoftware() { 1179 ResourceProvider::ScopedReadLockSoftware::~ScopedReadLockSoftware() {
1089 resource_provider_->UnlockForRead(resource_id_); 1180 resource_provider_->UnlockForRead(resource_id_);
1090 } 1181 }
1091 1182
1092 ResourceProvider::ScopedWriteLockSoftware::ScopedWriteLockSoftware( 1183 ResourceProvider::ScopedWriteLockSoftware::ScopedWriteLockSoftware(
1093 ResourceProvider* resource_provider, 1184 ResourceProvider* resource_provider,
1094 ResourceId resource_id) 1185 ResourceId resource_id)
1095 : resource_provider_(resource_provider), 1186 : resource_provider_(resource_provider), resource_id_(resource_id) {
1096 resource_(resource_provider->LockForWrite(resource_id)) { 1187 ResourceProvider::PopulateSkBitmapWithResource(
1097 ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource_); 1188 &sk_bitmap_, resource_provider->LockForWrite(resource_id));
1098 DCHECK(valid()); 1189 DCHECK(valid());
1099 } 1190 }
1100 1191
1101 ResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware() { 1192 ResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware() {
1102 DCHECK(thread_checker_.CalledOnValidThread()); 1193 DCHECK(thread_checker_.CalledOnValidThread());
1103 resource_provider_->UnlockForWrite(resource_); 1194 Resource* resource = resource_provider_->GetResource(resource_id_);
1195 DCHECK(resource);
1196 resource->SetSynchronized();
1197 resource_provider_->UnlockForWrite(resource);
1104 } 1198 }
1105 1199
1106 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: 1200 ResourceProvider::ScopedWriteLockGpuMemoryBuffer::
1107 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider, 1201 ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider,
1108 ResourceId resource_id) 1202 ResourceId resource_id)
1109 : resource_provider_(resource_provider), 1203 : resource_provider_(resource_provider), resource_id_(resource_id) {
1110 resource_(resource_provider->LockForWrite(resource_id)) { 1204 Resource* resource = resource_provider->LockForWrite(resource_id);
1111 DCHECK(IsGpuResourceType(resource_->type)); 1205 DCHECK(IsGpuResourceType(resource->type));
1112 gpu_memory_buffer_ = std::move(resource_->gpu_memory_buffer); 1206 format_ = resource->format;
1113 resource_->gpu_memory_buffer = nullptr; 1207 size_ = resource->size;
1208 gpu_memory_buffer_ = std::move(resource->gpu_memory_buffer);
1209 resource->gpu_memory_buffer = nullptr;
1114 } 1210 }
1115 1211
1116 ResourceProvider::ScopedWriteLockGpuMemoryBuffer:: 1212 ResourceProvider::ScopedWriteLockGpuMemoryBuffer::
1117 ~ScopedWriteLockGpuMemoryBuffer() { 1213 ~ScopedWriteLockGpuMemoryBuffer() {
1118 DCHECK(thread_checker_.CalledOnValidThread()); 1214 DCHECK(thread_checker_.CalledOnValidThread());
1119 resource_provider_->UnlockForWrite(resource_); 1215 Resource* resource = resource_provider_->GetResource(resource_id_);
1120 if (!gpu_memory_buffer_) 1216 DCHECK(resource);
1121 return; 1217 if (gpu_memory_buffer_) {
1122 1218 DCHECK(!resource->gpu_memory_buffer);
1123 DCHECK(!resource_->gpu_memory_buffer); 1219 resource_provider_->LazyCreate(resource);
1124 resource_provider_->LazyCreate(resource_); 1220 resource->gpu_memory_buffer = std::move(gpu_memory_buffer_);
1125 resource_->gpu_memory_buffer = std::move(gpu_memory_buffer_); 1221 if (resource->gpu_memory_buffer)
1126 if (resource_->gpu_memory_buffer) 1222 resource->gpu_memory_buffer_id = resource->gpu_memory_buffer->GetId();
1127 resource_->gpu_memory_buffer_id = resource_->gpu_memory_buffer->GetId(); 1223 resource->allocated = true;
1128 resource_->allocated = true; 1224 resource_provider_->LazyCreateImage(resource);
1129 resource_provider_->LazyCreateImage(resource_); 1225 resource->dirty_image = true;
1130 resource_->dirty_image = true; 1226 resource->is_overlay_candidate = true;
1131 resource_->is_overlay_candidate = true; 1227 // GpuMemoryBuffer provides direct access to the memory used by the GPU.
1132 resource_->SetSynchronized(); 1228 // Read lock fences are required to ensure that we're not trying to map a
1133 1229 // buffer that is currently in-use by the GPU.
1134 // GpuMemoryBuffer provides direct access to the memory used by the GPU. 1230 resource->read_lock_fences_enabled = true;
1135 // Read lock fences are required to ensure that we're not trying to map a 1231 }
1136 // buffer that is currently in-use by the GPU. 1232 resource->SetSynchronized();
1137 resource_->read_lock_fences_enabled = true; 1233 resource_provider_->UnlockForWrite(resource);
1138 } 1234 }
1139 1235
1140 gfx::GpuMemoryBuffer* 1236 gfx::GpuMemoryBuffer*
1141 ResourceProvider::ScopedWriteLockGpuMemoryBuffer::GetGpuMemoryBuffer() { 1237 ResourceProvider::ScopedWriteLockGpuMemoryBuffer::GetGpuMemoryBuffer() {
1142 if (!gpu_memory_buffer_) { 1238 if (!gpu_memory_buffer_) {
1143 gpu_memory_buffer_ = 1239 gpu_memory_buffer_ =
1144 resource_provider_->gpu_memory_buffer_manager_->AllocateGpuMemoryBuffer( 1240 resource_provider_->gpu_memory_buffer_manager_->AllocateGpuMemoryBuffer(
1145 resource_->size, BufferFormat(resource_->format), 1241 size_, BufferFormat(format_),
1146 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE, gpu::kNullSurfaceHandle); 1242 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE, gpu::kNullSurfaceHandle);
1147 } 1243 }
1148 return gpu_memory_buffer_.get(); 1244 return gpu_memory_buffer_.get();
1149 } 1245 }
1150 1246
1151 ResourceProvider::ScopedReadLockGpuMemoryBuffer::ScopedReadLockGpuMemoryBuffer( 1247 ResourceProvider::ScopedReadLockGpuMemoryBuffer::ScopedReadLockGpuMemoryBuffer(
1152 ResourceProvider* resource_provider, 1248 ResourceProvider* resource_provider,
1153 ResourceId resource_id) 1249 ResourceId resource_id)
1154 : resource_provider_(resource_provider), 1250 : resource_provider_(resource_provider), resource_id_(resource_id) {
1155 resource_id_(resource_id), 1251 const Resource* resource = resource_provider->LockForRead(resource_id, true);
1156 resource_(resource_provider->LockForRead(resource_id, true)) {} 1252 gpu_memory_buffer_ = resource->gpu_memory_buffer.get();
1253 texture_id_ = resource->gl_id;
1254 }
1157 1255
1158 ResourceProvider::ScopedReadLockGpuMemoryBuffer:: 1256 ResourceProvider::ScopedReadLockGpuMemoryBuffer::
1159 ~ScopedReadLockGpuMemoryBuffer() { 1257 ~ScopedReadLockGpuMemoryBuffer() {
1160 DCHECK(thread_checker_.CalledOnValidThread()); 1258 DCHECK(thread_checker_.CalledOnValidThread());
1161 resource_provider_->UnlockForRead(resource_id_); 1259 resource_provider_->UnlockForRead(resource_id_);
1162 } 1260 }
1163 1261
1164 gfx::GpuMemoryBuffer*
1165 ResourceProvider::ScopedReadLockGpuMemoryBuffer::GetGpuMemoryBuffer() const {
1166 return resource_->gpu_memory_buffer.get();
1167 }
1168
1169 unsigned ResourceProvider::ScopedReadLockGpuMemoryBuffer::GetTextureId() const {
1170 return resource_->gl_id;
1171 }
1172
1173 ResourceId ResourceProvider::ScopedReadLockGpuMemoryBuffer::GetResourceId()
1174 const {
1175 return resource_id_;
1176 }
1177
1178 ResourceProvider::ScopedWriteLockGr::ScopedWriteLockGr(
1179 ResourceProvider* resource_provider,
1180 ResourceId resource_id)
1181 : resource_provider_(resource_provider),
1182 resource_(resource_provider->LockForWrite(resource_id)),
1183 set_sync_token_(false) {
1184 DCHECK(thread_checker_.CalledOnValidThread());
1185 resource_provider_->LazyAllocate(resource_);
1186 if (resource_->image_id && resource_->dirty_image)
1187 resource_provider_->BindImageForSampling(resource_);
1188 }
1189
1190 ResourceProvider::ScopedWriteLockGr::~ScopedWriteLockGr() {
1191 DCHECK(thread_checker_.CalledOnValidThread());
1192 DCHECK(resource_->locked_for_write);
1193 if (set_sync_token_)
1194 resource_->UpdateSyncToken(sync_token_);
1195
1196 resource_provider_->UnlockForWrite(resource_);
1197 }
1198
1199 void ResourceProvider::ScopedWriteLockGr::InitSkSurface(
1200 GrContext* gr_context,
1201 bool use_distance_field_text,
1202 bool can_use_lcd_text,
1203 int msaa_sample_count) {
1204 DCHECK(resource_->locked_for_write);
1205
1206 GrGLTextureInfo texture_info;
1207 texture_info.fID = resource_->gl_id;
1208 texture_info.fTarget = resource_->target;
1209 GrBackendTextureDesc desc;
1210 desc.fFlags = kRenderTarget_GrBackendTextureFlag;
1211 desc.fWidth = resource_->size.width();
1212 desc.fHeight = resource_->size.height();
1213 desc.fConfig = ToGrPixelConfig(resource_->format);
1214 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
1215 desc.fTextureHandle = skia::GrGLTextureInfoToGrBackendObject(texture_info);
1216 desc.fSampleCnt = msaa_sample_count;
1217
1218 uint32_t flags =
1219 use_distance_field_text ? SkSurfaceProps::kUseDistanceFieldFonts_Flag : 0;
1220 // Use unknown pixel geometry to disable LCD text.
1221 SkSurfaceProps surface_props(flags, kUnknown_SkPixelGeometry);
1222 if (can_use_lcd_text) {
1223 // LegacyFontHost will get LCD text and skia figures out what type to use.
1224 surface_props =
1225 SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
1226 }
1227 sk_surface_ = SkSurface::MakeFromBackendTextureAsRenderTarget(
1228 gr_context, desc, &surface_props);
1229 }
1230
1231 void ResourceProvider::ScopedWriteLockGr::ReleaseSkSurface() {
1232 DCHECK(sk_surface_);
1233 sk_surface_->prepareForExternalIO();
1234 sk_surface_.reset();
1235 }
1236
1237 ResourceProvider::SynchronousFence::SynchronousFence( 1262 ResourceProvider::SynchronousFence::SynchronousFence(
1238 gpu::gles2::GLES2Interface* gl) 1263 gpu::gles2::GLES2Interface* gl)
1239 : gl_(gl), has_synchronized_(true) { 1264 : gl_(gl), has_synchronized_(true) {
1240 } 1265 }
1241 1266
1242 ResourceProvider::SynchronousFence::~SynchronousFence() { 1267 ResourceProvider::SynchronousFence::~SynchronousFence() {
1243 } 1268 }
1244 1269
1245 void ResourceProvider::SynchronousFence::Set() { 1270 void ResourceProvider::SynchronousFence::Set() {
1246 has_synchronized_ = false; 1271 has_synchronized_ = false;
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
1783 if (resource->type == RESOURCE_TYPE_GPU_MEMORY_BUFFER) { 1808 if (resource->type == RESOURCE_TYPE_GPU_MEMORY_BUFFER) {
1784 resource->gpu_memory_buffer = 1809 resource->gpu_memory_buffer =
1785 gpu_memory_buffer_manager_->AllocateGpuMemoryBuffer( 1810 gpu_memory_buffer_manager_->AllocateGpuMemoryBuffer(
1786 size, BufferFormat(format), 1811 size, BufferFormat(format),
1787 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE, gpu::kNullSurfaceHandle); 1812 gfx::BufferUsage::GPU_READ_CPU_READ_WRITE, gpu::kNullSurfaceHandle);
1788 if (resource->gpu_memory_buffer) 1813 if (resource->gpu_memory_buffer)
1789 resource->gpu_memory_buffer_id = resource->gpu_memory_buffer->GetId(); 1814 resource->gpu_memory_buffer_id = resource->gpu_memory_buffer->GetId();
1790 LazyCreateImage(resource); 1815 LazyCreateImage(resource);
1791 resource->dirty_image = true; 1816 resource->dirty_image = true;
1792 resource->is_overlay_candidate = true; 1817 resource->is_overlay_candidate = true;
1818 // GpuMemoryBuffer provides direct access to the memory used by the GPU.
1819 // Read lock fences are required to ensure that we're not trying to map a
1820 // buffer that is currently in-use by the GPU.
1821 resource->read_lock_fences_enabled = true;
1793 } else if (use_texture_storage_ext_ && 1822 } else if (use_texture_storage_ext_ &&
1794 IsFormatSupportedForStorage(format, use_texture_format_bgra_) && 1823 IsFormatSupportedForStorage(format, use_texture_format_bgra_) &&
1795 (resource->hint & TEXTURE_HINT_IMMUTABLE)) { 1824 (resource->hint & TEXTURE_HINT_IMMUTABLE)) {
1796 GLenum storage_format = TextureToStorageFormat(format); 1825 GLenum storage_format = TextureToStorageFormat(format);
1797 gl->TexStorage2DEXT(resource->target, 1, storage_format, size.width(), 1826 gl->TexStorage2DEXT(resource->target, 1, storage_format, size.width(),
1798 size.height()); 1827 size.height());
1799 } else { 1828 } else {
1800 // ETC1 does not support preallocation. 1829 // ETC1 does not support preallocation.
1801 if (format != ETC1) { 1830 if (format != ETC1) {
1802 gl->TexImage2D(resource->target, 0, GLInternalFormat(format), 1831 gl->TexImage2D(resource->target, 0, GLInternalFormat(format),
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1959 1988
1960 const int kImportance = 2; 1989 const int kImportance = 2;
1961 pmd->CreateSharedGlobalAllocatorDump(guid); 1990 pmd->CreateSharedGlobalAllocatorDump(guid);
1962 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); 1991 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance);
1963 } 1992 }
1964 1993
1965 return true; 1994 return true;
1966 } 1995 }
1967 1996
1968 } // namespace cc 1997 } // namespace cc
OLDNEW
« no previous file with comments | « cc/resources/resource_provider.h ('k') | cc/resources/resource_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698