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

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

Issue 23097005: cc: return resources via a ReturnedResource struct rather than TransferableResource (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: post-rebase fixes Created 7 years, 4 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 | Annotate | Revision Log
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 <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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 181
182 CleanUpGLIfNeeded(); 182 CleanUpGLIfNeeded();
183 } 183 }
184 184
185 WebGraphicsContext3D* ResourceProvider::GraphicsContext3D() { 185 WebGraphicsContext3D* ResourceProvider::GraphicsContext3D() {
186 DCHECK(thread_checker_.CalledOnValidThread()); 186 DCHECK(thread_checker_.CalledOnValidThread());
187 return output_surface_->context3d(); 187 return output_surface_->context3d();
188 } 188 }
189 189
190 bool ResourceProvider::InUseByConsumer(ResourceId id) { 190 bool ResourceProvider::InUseByConsumer(ResourceId id) {
191 DCHECK(thread_checker_.CalledOnValidThread()); 191 Resource* resource = GetResource(id);
192 ResourceMap::iterator it = resources_.find(id);
193 CHECK(it != resources_.end());
194 Resource* resource = &it->second;
195 return resource->lock_for_read_count > 0 || resource->exported_count > 0; 192 return resource->lock_for_read_count > 0 || resource->exported_count > 0;
196 } 193 }
197 194
198 ResourceProvider::ResourceId ResourceProvider::CreateResource( 195 ResourceProvider::ResourceId ResourceProvider::CreateResource(
199 gfx::Size size, GLenum format, TextureUsageHint hint) { 196 gfx::Size size, GLenum format, TextureUsageHint hint) {
200 DCHECK(!size.IsEmpty()); 197 DCHECK(!size.IsEmpty());
201 switch (default_resource_type_) { 198 switch (default_resource_type_) {
202 case GLTexture: 199 case GLTexture:
203 return CreateGLTexture( 200 return CreateGLTexture(
204 size, format, GL_TEXTURE_POOL_UNMANAGED_CHROMIUM, hint); 201 size, format, GL_TEXTURE_POOL_UNMANAGED_CHROMIUM, hint);
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 if (resource->pixels) 372 if (resource->pixels)
376 delete[] resource->pixels; 373 delete[] resource->pixels;
377 if (resource->pixel_buffer) 374 if (resource->pixel_buffer)
378 delete[] resource->pixel_buffer; 375 delete[] resource->pixel_buffer;
379 376
380 resources_.erase(it); 377 resources_.erase(it);
381 } 378 }
382 379
383 ResourceProvider::ResourceType ResourceProvider::GetResourceType( 380 ResourceProvider::ResourceType ResourceProvider::GetResourceType(
384 ResourceId id) { 381 ResourceId id) {
385 ResourceMap::iterator it = resources_.find(id); 382 return GetResource(id)->type;
386 CHECK(it != resources_.end());
387 Resource* resource = &it->second;
388 return resource->type;
389 } 383 }
390 384
391 void ResourceProvider::SetPixels(ResourceId id, 385 void ResourceProvider::SetPixels(ResourceId id,
392 const uint8_t* image, 386 const uint8_t* image,
393 gfx::Rect image_rect, 387 gfx::Rect image_rect,
394 gfx::Rect source_rect, 388 gfx::Rect source_rect,
395 gfx::Vector2d dest_offset) { 389 gfx::Vector2d dest_offset) {
396 DCHECK(thread_checker_.CalledOnValidThread()); 390 Resource* resource = GetResource(id);
397 ResourceMap::iterator it = resources_.find(id);
398 CHECK(it != resources_.end());
399 Resource* resource = &it->second;
400 DCHECK(!resource->locked_for_write); 391 DCHECK(!resource->locked_for_write);
401 DCHECK(!resource->lock_for_read_count); 392 DCHECK(!resource->lock_for_read_count);
402 DCHECK(!resource->external); 393 DCHECK(!resource->external);
403 DCHECK_EQ(resource->exported_count, 0); 394 DCHECK_EQ(resource->exported_count, 0);
404 DCHECK(ReadLockFenceHasPassed(resource)); 395 DCHECK(ReadLockFenceHasPassed(resource));
405 LazyAllocate(resource); 396 LazyAllocate(resource);
406 397
407 if (resource->gl_id) { 398 if (resource->gl_id) {
408 DCHECK(!resource->pending_set_pixels); 399 DCHECK(!resource->pending_set_pixels);
409 WebGraphicsContext3D* context3d = output_surface_->context3d(); 400 WebGraphicsContext3D* context3d = output_surface_->context3d();
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 bool ResourceProvider::ShallowFlushIfSupported() { 482 bool ResourceProvider::ShallowFlushIfSupported() {
492 DCHECK(thread_checker_.CalledOnValidThread()); 483 DCHECK(thread_checker_.CalledOnValidThread());
493 WebGraphicsContext3D* context3d = output_surface_->context3d(); 484 WebGraphicsContext3D* context3d = output_surface_->context3d();
494 if (!context3d || !use_shallow_flush_) 485 if (!context3d || !use_shallow_flush_)
495 return false; 486 return false;
496 487
497 context3d->shallowFlushCHROMIUM(); 488 context3d->shallowFlushCHROMIUM();
498 return true; 489 return true;
499 } 490 }
500 491
501 const ResourceProvider::Resource* ResourceProvider::LockForRead(ResourceId id) { 492 ResourceProvider::Resource* ResourceProvider::GetResource(ResourceId id) {
502 DCHECK(thread_checker_.CalledOnValidThread()); 493 DCHECK(thread_checker_.CalledOnValidThread());
503 ResourceMap::iterator it = resources_.find(id); 494 ResourceMap::iterator it = resources_.find(id);
504 CHECK(it != resources_.end()); 495 CHECK(it != resources_.end());
505 Resource* resource = &it->second; 496 return &it->second;
497 }
498
499 const ResourceProvider::Resource* ResourceProvider::LockForRead(ResourceId id) {
500 Resource* resource = GetResource(id);
506 DCHECK(!resource->locked_for_write || 501 DCHECK(!resource->locked_for_write ||
507 resource->set_pixels_completion_forced) << 502 resource->set_pixels_completion_forced) <<
508 "locked for write: " << resource->locked_for_write << 503 "locked for write: " << resource->locked_for_write <<
509 " pixels completion forced: " << resource->set_pixels_completion_forced; 504 " pixels completion forced: " << resource->set_pixels_completion_forced;
510 DCHECK_EQ(resource->exported_count, 0); 505 DCHECK_EQ(resource->exported_count, 0);
511 // Uninitialized! Call SetPixels or LockForWrite first. 506 // Uninitialized! Call SetPixels or LockForWrite first.
512 DCHECK(resource->allocated); 507 DCHECK(resource->allocated);
513 508
514 LazyCreate(resource); 509 LazyCreate(resource);
515 510
(...skipping 15 matching lines...) Expand all
531 } 526 }
532 527
533 resource->lock_for_read_count++; 528 resource->lock_for_read_count++;
534 if (resource->enable_read_lock_fences) 529 if (resource->enable_read_lock_fences)
535 resource->read_lock_fence = current_read_lock_fence_; 530 resource->read_lock_fence = current_read_lock_fence_;
536 531
537 return resource; 532 return resource;
538 } 533 }
539 534
540 void ResourceProvider::UnlockForRead(ResourceId id) { 535 void ResourceProvider::UnlockForRead(ResourceId id) {
541 DCHECK(thread_checker_.CalledOnValidThread()); 536 Resource* resource = GetResource(id);
542 ResourceMap::iterator it = resources_.find(id);
543 CHECK(it != resources_.end());
544 Resource* resource = &it->second;
545 DCHECK_GT(resource->lock_for_read_count, 0); 537 DCHECK_GT(resource->lock_for_read_count, 0);
546 DCHECK_EQ(resource->exported_count, 0); 538 DCHECK_EQ(resource->exported_count, 0);
547 resource->lock_for_read_count--; 539 resource->lock_for_read_count--;
548 } 540 }
549 541
550 const ResourceProvider::Resource* ResourceProvider::LockForWrite( 542 const ResourceProvider::Resource* ResourceProvider::LockForWrite(
551 ResourceId id) { 543 ResourceId id) {
552 DCHECK(thread_checker_.CalledOnValidThread()); 544 Resource* resource = GetResource(id);
553 ResourceMap::iterator it = resources_.find(id);
554 CHECK(it != resources_.end());
555 Resource* resource = &it->second;
556 DCHECK(!resource->locked_for_write); 545 DCHECK(!resource->locked_for_write);
557 DCHECK(!resource->lock_for_read_count); 546 DCHECK(!resource->lock_for_read_count);
558 DCHECK_EQ(resource->exported_count, 0); 547 DCHECK_EQ(resource->exported_count, 0);
559 DCHECK(!resource->external); 548 DCHECK(!resource->external);
560 DCHECK(ReadLockFenceHasPassed(resource)); 549 DCHECK(ReadLockFenceHasPassed(resource));
561 LazyAllocate(resource); 550 LazyAllocate(resource);
562 551
563 resource->locked_for_write = true; 552 resource->locked_for_write = true;
564 return resource; 553 return resource;
565 } 554 }
566 555
567 bool ResourceProvider::CanLockForWrite(ResourceId id) { 556 bool ResourceProvider::CanLockForWrite(ResourceId id) {
568 DCHECK(thread_checker_.CalledOnValidThread()); 557 Resource* resource = GetResource(id);
569 ResourceMap::iterator it = resources_.find(id);
570 CHECK(it != resources_.end());
571 Resource* resource = &it->second;
572 return !resource->locked_for_write && 558 return !resource->locked_for_write &&
573 !resource->lock_for_read_count && 559 !resource->lock_for_read_count &&
574 !resource->exported_count && 560 !resource->exported_count &&
575 !resource->external && 561 !resource->external &&
576 ReadLockFenceHasPassed(resource); 562 ReadLockFenceHasPassed(resource);
577 } 563 }
578 564
579 void ResourceProvider::UnlockForWrite(ResourceId id) { 565 void ResourceProvider::UnlockForWrite(ResourceId id) {
580 DCHECK(thread_checker_.CalledOnValidThread()); 566 Resource* resource = GetResource(id);
581 ResourceMap::iterator it = resources_.find(id);
582 CHECK(it != resources_.end());
583 Resource* resource = &it->second;
584 DCHECK(resource->locked_for_write); 567 DCHECK(resource->locked_for_write);
585 DCHECK_EQ(resource->exported_count, 0); 568 DCHECK_EQ(resource->exported_count, 0);
586 DCHECK(!resource->external); 569 DCHECK(!resource->external);
587 resource->locked_for_write = false; 570 resource->locked_for_write = false;
588 } 571 }
589 572
590 ResourceProvider::ScopedReadLockGL::ScopedReadLockGL( 573 ResourceProvider::ScopedReadLockGL::ScopedReadLockGL(
591 ResourceProvider* resource_provider, 574 ResourceProvider* resource_provider,
592 ResourceProvider::ResourceId resource_id) 575 ResourceProvider::ResourceId resource_id)
593 : resource_provider_(resource_provider), 576 : resource_provider_(resource_provider),
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 unsigned int sync_point = context3d->insertSyncPoint(); 799 unsigned int sync_point = context3d->insertSyncPoint();
817 for (TransferableResourceArray::iterator it = list->begin(); 800 for (TransferableResourceArray::iterator it = list->begin();
818 it != list->end(); 801 it != list->end();
819 ++it) { 802 ++it) {
820 if (!it->sync_point) 803 if (!it->sync_point)
821 it->sync_point = sync_point; 804 it->sync_point = sync_point;
822 } 805 }
823 } 806 }
824 } 807 }
825 808
826 void ResourceProvider::PrepareSendToChild(int child, 809 void ResourceProvider::PrepareSendToChild(int child,
danakj 2013/08/16 23:33:35 Maybe we could rename this to PrepareSendReturnsTo
piman 2013/08/17 01:54:31 Done.
827 const ResourceIdArray& resources, 810 const ResourceIdArray& resources,
828 TransferableResourceArray* list) { 811 ReturnedResourceArray* list) {
829 DCHECK(thread_checker_.CalledOnValidThread()); 812 DCHECK(thread_checker_.CalledOnValidThread());
830 WebGraphicsContext3D* context3d = output_surface_->context3d(); 813 WebGraphicsContext3D* context3d = output_surface_->context3d();
831 if (!context3d || !context3d->makeContextCurrent()) { 814 if (!context3d || !context3d->makeContextCurrent()) {
832 // TODO(skaslev): Implement this path for software compositing. 815 // TODO(skaslev): Implement this path for software compositing.
833 return; 816 return;
834 } 817 }
835 Child& child_info = children_.find(child)->second; 818 Child& child_info = children_.find(child)->second;
836 bool need_sync_point = false; 819 bool need_sync_point = false;
837 for (ResourceIdArray::const_iterator it = resources.begin(); 820 for (ResourceIdArray::const_iterator it = resources.begin();
838 it != resources.end(); 821 it != resources.end(); ++it) {
839 ++it) { 822 Resource* resource = GetResource(*it);
840 TransferableResource resource; 823 DCHECK(!resource->locked_for_write);
841 TransferResource(context3d, *it, &resource); 824 DCHECK(!resource->lock_for_read_count);
842 if (!resource.sync_point)
843 need_sync_point = true;
844 DCHECK(child_info.parent_to_child_map.find(*it) != 825 DCHECK(child_info.parent_to_child_map.find(*it) !=
845 child_info.parent_to_child_map.end()); 826 child_info.parent_to_child_map.end());
846 resource.id = child_info.parent_to_child_map[*it]; 827
828 ReturnedResource returned;
829 returned.id = child_info.parent_to_child_map[*it];
830 returned.filter = resource->filter;
831 returned.sync_point = resource->mailbox.sync_point();
832 if (!returned.sync_point)
833 need_sync_point = true;
834 returned.count = resource->imported_count;
835 list->push_back(returned);
836
847 child_info.parent_to_child_map.erase(*it); 837 child_info.parent_to_child_map.erase(*it);
848 child_info.child_to_parent_map.erase(resource.id); 838 child_info.child_to_parent_map.erase(returned.id);
849 for (int i = 0; i < resources_[*it].imported_count; ++i)
850 list->push_back(resource);
851 resources_[*it].imported_count = 0; 839 resources_[*it].imported_count = 0;
852 DeleteResource(*it); 840 DeleteResource(*it);
853 } 841 }
854 if (need_sync_point) { 842 if (need_sync_point) {
855 unsigned int sync_point = context3d->insertSyncPoint(); 843 unsigned int sync_point = context3d->insertSyncPoint();
856 for (TransferableResourceArray::iterator it = list->begin(); 844 for (ReturnedResourceArray::iterator it = list->begin();
857 it != list->end(); 845 it != list->end(); ++it) {
858 ++it) {
859 if (!it->sync_point) 846 if (!it->sync_point)
860 it->sync_point = sync_point; 847 it->sync_point = sync_point;
861 } 848 }
862 } 849 }
863 } 850 }
864 851
865 void ResourceProvider::ReceiveFromChild( 852 void ResourceProvider::ReceiveFromChild(
866 int child, const TransferableResourceArray& resources) { 853 int child, const TransferableResourceArray& resources) {
867 DCHECK(thread_checker_.CalledOnValidThread()); 854 DCHECK(thread_checker_.CalledOnValidThread());
868 WebGraphicsContext3D* context3d = output_surface_->context3d(); 855 WebGraphicsContext3D* context3d = output_surface_->context3d();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 // Don't allocate a texture for a child. 887 // Don't allocate a texture for a child.
901 resource.allocated = true; 888 resource.allocated = true;
902 resource.imported_count = 1; 889 resource.imported_count = 1;
903 resources_[id] = resource; 890 resources_[id] = resource;
904 child_info.parent_to_child_map[id] = it->id; 891 child_info.parent_to_child_map[id] = it->id;
905 child_info.child_to_parent_map[it->id] = id; 892 child_info.child_to_parent_map[it->id] = id;
906 } 893 }
907 } 894 }
908 895
909 void ResourceProvider::ReceiveFromParent( 896 void ResourceProvider::ReceiveFromParent(
910 const TransferableResourceArray& resources) { 897 const ReturnedResourceArray& resources) {
911 DCHECK(thread_checker_.CalledOnValidThread()); 898 DCHECK(thread_checker_.CalledOnValidThread());
912 WebGraphicsContext3D* context3d = output_surface_->context3d(); 899 WebGraphicsContext3D* context3d = output_surface_->context3d();
913 if (!context3d || !context3d->makeContextCurrent()) { 900 if (!context3d || !context3d->makeContextCurrent()) {
914 // TODO(skaslev): Implement this path for software compositing. 901 // TODO(skaslev): Implement this path for software compositing.
915 return; 902 return;
916 } 903 }
917 for (TransferableResourceArray::const_iterator it = resources.begin(); 904 for (ReturnedResourceArray::const_iterator it = resources.begin();
918 it != resources.end(); 905 it != resources.end();
919 ++it) { 906 ++it) {
920 ResourceMap::iterator map_iterator = resources_.find(it->id); 907 ResourceMap::iterator map_iterator = resources_.find(it->id);
921 DCHECK(map_iterator != resources_.end()); 908 DCHECK(map_iterator != resources_.end());
922 Resource* resource = &map_iterator->second; 909 Resource* resource = &map_iterator->second;
923 DCHECK_GT(resource->exported_count, 0); 910 CHECK_GE(resource->exported_count, it->count);
924 --resource->exported_count; 911 resource->exported_count -= it->count;
925 if (resource->exported_count) 912 if (resource->exported_count)
926 continue; 913 continue;
927 resource->filter = it->filter; 914 resource->filter = it->filter;
928 DCHECK(resource->mailbox.ContainsMailbox(it->mailbox));
929 if (resource->gl_id) { 915 if (resource->gl_id) {
930 if (it->sync_point) 916 if (it->sync_point)
931 GLC(context3d, context3d->waitSyncPoint(it->sync_point)); 917 GLC(context3d, context3d->waitSyncPoint(it->sync_point));
932 } else { 918 } else {
933 resource->mailbox = TextureMailbox(resource->mailbox.name(), 919 resource->mailbox = TextureMailbox(resource->mailbox.name(),
934 resource->mailbox.callback(), 920 resource->mailbox.callback(),
935 it->sync_point); 921 it->sync_point);
936 } 922 }
937 if (resource->marked_for_deletion) 923 if (resource->marked_for_deletion)
938 DeleteResourceInternal(map_iterator, Normal); 924 DeleteResourceInternal(map_iterator, Normal);
939 } 925 }
940 } 926 }
941 927
942 void ResourceProvider::TransferResource(WebGraphicsContext3D* context, 928 void ResourceProvider::TransferResource(WebGraphicsContext3D* context,
943 ResourceId id, 929 ResourceId id,
944 TransferableResource* resource) { 930 TransferableResource* resource) {
945 DCHECK(thread_checker_.CalledOnValidThread()); 931 Resource* source = GetResource(id);
946 ResourceMap::iterator it = resources_.find(id);
947 CHECK(it != resources_.end());
948 Resource* source = &it->second;
949 DCHECK(!source->locked_for_write); 932 DCHECK(!source->locked_for_write);
950 DCHECK(!source->lock_for_read_count); 933 DCHECK(!source->lock_for_read_count);
951 DCHECK(!source->external || (source->external && source->mailbox.IsValid())); 934 DCHECK(!source->external || (source->external && source->mailbox.IsValid()));
952 DCHECK(source->allocated); 935 DCHECK(source->allocated);
953 resource->id = id; 936 resource->id = id;
954 resource->format = source->format; 937 resource->format = source->format;
955 resource->filter = source->filter; 938 resource->filter = source->filter;
956 resource->size = source->size; 939 resource->size = source->size;
957 940
958 // TODO(skaslev) Implement this path for shared memory resources. 941 // TODO(skaslev) Implement this path for shared memory resources.
(...skipping 11 matching lines...) Expand all
970 } else { 953 } else {
971 // This is either an external resource, or a compositor resource that we 954 // This is either an external resource, or a compositor resource that we
972 // already exported. Make sure to forward the sync point that we were given. 955 // already exported. Make sure to forward the sync point that we were given.
973 resource->mailbox = source->mailbox.name(); 956 resource->mailbox = source->mailbox.name();
974 resource->sync_point = source->mailbox.sync_point(); 957 resource->sync_point = source->mailbox.sync_point();
975 source->mailbox.ResetSyncPoint(); 958 source->mailbox.ResetSyncPoint();
976 } 959 }
977 } 960 }
978 961
979 void ResourceProvider::AcquirePixelBuffer(ResourceId id) { 962 void ResourceProvider::AcquirePixelBuffer(ResourceId id) {
980 DCHECK(thread_checker_.CalledOnValidThread()); 963 Resource* resource = GetResource(id);
981 ResourceMap::iterator it = resources_.find(id);
982 CHECK(it != resources_.end());
983 Resource* resource = &it->second;
984 DCHECK(!resource->external); 964 DCHECK(!resource->external);
985 DCHECK_EQ(resource->exported_count, 0); 965 DCHECK_EQ(resource->exported_count, 0);
986 DCHECK(!resource->image_id); 966 DCHECK(!resource->image_id);
987 967
988 if (resource->type == GLTexture) { 968 if (resource->type == GLTexture) {
989 WebGraphicsContext3D* context3d = output_surface_->context3d(); 969 WebGraphicsContext3D* context3d = output_surface_->context3d();
990 DCHECK(context3d); 970 DCHECK(context3d);
991 if (!resource->gl_pixel_buffer_id) 971 if (!resource->gl_pixel_buffer_id)
992 resource->gl_pixel_buffer_id = context3d->createBuffer(); 972 resource->gl_pixel_buffer_id = context3d->createBuffer();
993 context3d->bindBuffer( 973 context3d->bindBuffer(
994 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 974 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
995 resource->gl_pixel_buffer_id); 975 resource->gl_pixel_buffer_id);
996 context3d->bufferData( 976 context3d->bufferData(
997 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 977 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
998 4 * resource->size.GetArea(), 978 4 * resource->size.GetArea(),
999 NULL, 979 NULL,
1000 GL_DYNAMIC_DRAW); 980 GL_DYNAMIC_DRAW);
1001 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); 981 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
1002 } 982 }
1003 983
1004 if (resource->pixels) { 984 if (resource->pixels) {
1005 if (resource->pixel_buffer) 985 if (resource->pixel_buffer)
1006 return; 986 return;
1007 987
1008 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; 988 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()];
1009 } 989 }
1010 } 990 }
1011 991
1012 void ResourceProvider::ReleasePixelBuffer(ResourceId id) { 992 void ResourceProvider::ReleasePixelBuffer(ResourceId id) {
1013 DCHECK(thread_checker_.CalledOnValidThread()); 993 Resource* resource = GetResource(id);
1014 ResourceMap::iterator it = resources_.find(id);
1015 CHECK(it != resources_.end());
1016 Resource* resource = &it->second;
1017 DCHECK(!resource->external); 994 DCHECK(!resource->external);
1018 DCHECK_EQ(resource->exported_count, 0); 995 DCHECK_EQ(resource->exported_count, 0);
1019 DCHECK(!resource->image_id); 996 DCHECK(!resource->image_id);
1020 997
1021 // The pixel buffer can be released while there is a pending "set pixels" 998 // The pixel buffer can be released while there is a pending "set pixels"
1022 // if completion has been forced. Any shared memory associated with this 999 // if completion has been forced. Any shared memory associated with this
1023 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM 1000 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM
1024 // command has been processed on the service side. It is also safe to 1001 // command has been processed on the service side. It is also safe to
1025 // reuse any query id associated with this resource before they complete 1002 // reuse any query id associated with this resource before they complete
1026 // as each new query has a unique submit count. 1003 // as each new query has a unique submit count.
(...skipping 21 matching lines...) Expand all
1048 1025
1049 if (resource->pixels) { 1026 if (resource->pixels) {
1050 if (!resource->pixel_buffer) 1027 if (!resource->pixel_buffer)
1051 return; 1028 return;
1052 delete[] resource->pixel_buffer; 1029 delete[] resource->pixel_buffer;
1053 resource->pixel_buffer = NULL; 1030 resource->pixel_buffer = NULL;
1054 } 1031 }
1055 } 1032 }
1056 1033
1057 uint8_t* ResourceProvider::MapPixelBuffer(ResourceId id) { 1034 uint8_t* ResourceProvider::MapPixelBuffer(ResourceId id) {
1058 DCHECK(thread_checker_.CalledOnValidThread()); 1035 Resource* resource = GetResource(id);
1059 ResourceMap::iterator it = resources_.find(id);
1060 CHECK(it != resources_.end());
1061 Resource* resource = &it->second;
1062 DCHECK(!resource->external); 1036 DCHECK(!resource->external);
1063 DCHECK_EQ(resource->exported_count, 0); 1037 DCHECK_EQ(resource->exported_count, 0);
1064 DCHECK(!resource->image_id); 1038 DCHECK(!resource->image_id);
1065 1039
1066 if (resource->type == GLTexture) { 1040 if (resource->type == GLTexture) {
1067 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1041 WebGraphicsContext3D* context3d = output_surface_->context3d();
1068 DCHECK(context3d); 1042 DCHECK(context3d);
1069 DCHECK(resource->gl_pixel_buffer_id); 1043 DCHECK(resource->gl_pixel_buffer_id);
1070 context3d->bindBuffer( 1044 context3d->bindBuffer(
1071 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 1045 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
1072 resource->gl_pixel_buffer_id); 1046 resource->gl_pixel_buffer_id);
1073 uint8_t* image = static_cast<uint8_t*>( 1047 uint8_t* image = static_cast<uint8_t*>(
1074 context3d->mapBufferCHROMIUM( 1048 context3d->mapBufferCHROMIUM(
1075 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY)); 1049 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY));
1076 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); 1050 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
1077 // Buffer is required to be 4-byte aligned. 1051 // Buffer is required to be 4-byte aligned.
1078 CHECK(!(reinterpret_cast<intptr_t>(image) & 3)); 1052 CHECK(!(reinterpret_cast<intptr_t>(image) & 3));
1079 return image; 1053 return image;
1080 } 1054 }
1081 1055
1082 if (resource->pixels) 1056 if (resource->pixels)
1083 return resource->pixel_buffer; 1057 return resource->pixel_buffer;
1084 1058
1085 return NULL; 1059 return NULL;
1086 } 1060 }
1087 1061
1088 void ResourceProvider::UnmapPixelBuffer(ResourceId id) { 1062 void ResourceProvider::UnmapPixelBuffer(ResourceId id) {
1089 DCHECK(thread_checker_.CalledOnValidThread()); 1063 Resource* resource = GetResource(id);
1090 ResourceMap::iterator it = resources_.find(id);
1091 CHECK(it != resources_.end());
1092 Resource* resource = &it->second;
1093 DCHECK(!resource->external); 1064 DCHECK(!resource->external);
1094 DCHECK_EQ(resource->exported_count, 0); 1065 DCHECK_EQ(resource->exported_count, 0);
1095 DCHECK(!resource->image_id); 1066 DCHECK(!resource->image_id);
1096 1067
1097 if (resource->type == GLTexture) { 1068 if (resource->type == GLTexture) {
1098 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1069 WebGraphicsContext3D* context3d = output_surface_->context3d();
1099 DCHECK(context3d); 1070 DCHECK(context3d);
1100 DCHECK(resource->gl_pixel_buffer_id); 1071 DCHECK(resource->gl_pixel_buffer_id);
1101 context3d->bindBuffer( 1072 context3d->bindBuffer(
1102 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 1073 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 DCHECK_EQ(GL_TEXTURE0, GetActiveTextureUnit(context3d)); 1125 DCHECK_EQ(GL_TEXTURE0, GetActiveTextureUnit(context3d));
1155 if (unit != GL_TEXTURE0) 1126 if (unit != GL_TEXTURE0)
1156 GLC(context3d, context3d->activeTexture(unit)); 1127 GLC(context3d, context3d->activeTexture(unit));
1157 context3d->releaseTexImage2DCHROMIUM(target, resource->image_id); 1128 context3d->releaseTexImage2DCHROMIUM(target, resource->image_id);
1158 // Active unit being GL_TEXTURE0 is effectively the ground state. 1129 // Active unit being GL_TEXTURE0 is effectively the ground state.
1159 if (unit != GL_TEXTURE0) 1130 if (unit != GL_TEXTURE0)
1160 GLC(context3d, context3d->activeTexture(GL_TEXTURE0)); 1131 GLC(context3d, context3d->activeTexture(GL_TEXTURE0));
1161 } 1132 }
1162 1133
1163 void ResourceProvider::BeginSetPixels(ResourceId id) { 1134 void ResourceProvider::BeginSetPixels(ResourceId id) {
1164 DCHECK(thread_checker_.CalledOnValidThread()); 1135 Resource* resource = GetResource(id);
1165 ResourceMap::iterator it = resources_.find(id);
1166 CHECK(it != resources_.end());
1167 Resource* resource = &it->second;
1168 DCHECK(!resource->pending_set_pixels); 1136 DCHECK(!resource->pending_set_pixels);
1169 1137
1170 LazyCreate(resource); 1138 LazyCreate(resource);
1171 DCHECK(resource->gl_id || resource->allocated); 1139 DCHECK(resource->gl_id || resource->allocated);
1172 DCHECK(ReadLockFenceHasPassed(resource)); 1140 DCHECK(ReadLockFenceHasPassed(resource));
1173 DCHECK(!resource->image_id); 1141 DCHECK(!resource->image_id);
1174 1142
1175 bool allocate = !resource->allocated; 1143 bool allocate = !resource->allocated;
1176 resource->allocated = true; 1144 resource->allocated = true;
1177 LockForWrite(id); 1145 LockForWrite(id);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 std::swap(resource->pixels, resource->pixel_buffer); 1190 std::swap(resource->pixels, resource->pixel_buffer);
1223 delete[] resource->pixel_buffer; 1191 delete[] resource->pixel_buffer;
1224 resource->pixel_buffer = NULL; 1192 resource->pixel_buffer = NULL;
1225 } 1193 }
1226 1194
1227 resource->pending_set_pixels = true; 1195 resource->pending_set_pixels = true;
1228 resource->set_pixels_completion_forced = false; 1196 resource->set_pixels_completion_forced = false;
1229 } 1197 }
1230 1198
1231 void ResourceProvider::ForceSetPixelsToComplete(ResourceId id) { 1199 void ResourceProvider::ForceSetPixelsToComplete(ResourceId id) {
1232 DCHECK(thread_checker_.CalledOnValidThread()); 1200 Resource* resource = GetResource(id);
1233 ResourceMap::iterator it = resources_.find(id);
1234 CHECK(it != resources_.end());
1235 Resource* resource = &it->second;
1236 DCHECK(resource->locked_for_write); 1201 DCHECK(resource->locked_for_write);
1237 DCHECK(resource->pending_set_pixels); 1202 DCHECK(resource->pending_set_pixels);
1238 DCHECK(!resource->set_pixels_completion_forced); 1203 DCHECK(!resource->set_pixels_completion_forced);
1239 1204
1240 if (resource->gl_id) { 1205 if (resource->gl_id) {
1241 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1206 WebGraphicsContext3D* context3d = output_surface_->context3d();
1242 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, resource->gl_id)); 1207 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, resource->gl_id));
1243 GLC(context3d, context3d->waitAsyncTexImage2DCHROMIUM(GL_TEXTURE_2D)); 1208 GLC(context3d, context3d->waitAsyncTexImage2DCHROMIUM(GL_TEXTURE_2D));
1244 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, 0)); 1209 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, 0));
1245 } 1210 }
1246 1211
1247 resource->set_pixels_completion_forced = true; 1212 resource->set_pixels_completion_forced = true;
1248 } 1213 }
1249 1214
1250 bool ResourceProvider::DidSetPixelsComplete(ResourceId id) { 1215 bool ResourceProvider::DidSetPixelsComplete(ResourceId id) {
1251 DCHECK(thread_checker_.CalledOnValidThread()); 1216 Resource* resource = GetResource(id);
1252 ResourceMap::iterator it = resources_.find(id);
1253 CHECK(it != resources_.end());
1254 Resource* resource = &it->second;
1255 DCHECK(resource->locked_for_write); 1217 DCHECK(resource->locked_for_write);
1256 DCHECK(resource->pending_set_pixels); 1218 DCHECK(resource->pending_set_pixels);
1257 1219
1258 if (resource->gl_id) { 1220 if (resource->gl_id) {
1259 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1221 WebGraphicsContext3D* context3d = output_surface_->context3d();
1260 DCHECK(context3d); 1222 DCHECK(context3d);
1261 DCHECK(resource->gl_upload_query_id); 1223 DCHECK(resource->gl_upload_query_id);
1262 unsigned complete = 1; 1224 unsigned complete = 1;
1263 context3d->getQueryObjectuivEXT( 1225 context3d->getQueryObjectuivEXT(
1264 resource->gl_upload_query_id, 1226 resource->gl_upload_query_id,
1265 GL_QUERY_RESULT_AVAILABLE_EXT, 1227 GL_QUERY_RESULT_AVAILABLE_EXT,
1266 &complete); 1228 &complete);
1267 if (!complete) 1229 if (!complete)
1268 return false; 1230 return false;
1269 } 1231 }
1270 1232
1271 resource->pending_set_pixels = false; 1233 resource->pending_set_pixels = false;
1272 UnlockForWrite(id); 1234 UnlockForWrite(id);
1273 1235
1274 return true; 1236 return true;
1275 } 1237 }
1276 1238
1277 void ResourceProvider::CreateForTesting(ResourceId id) { 1239 void ResourceProvider::CreateForTesting(ResourceId id) {
1278 ResourceMap::iterator it = resources_.find(id); 1240 LazyCreate(GetResource(id));
1279 CHECK(it != resources_.end());
1280 Resource* resource = &it->second;
1281 LazyCreate(resource);
1282 } 1241 }
1283 1242
1284 void ResourceProvider::LazyCreate(Resource* resource) { 1243 void ResourceProvider::LazyCreate(Resource* resource) {
1285 if (resource->type != GLTexture || resource->gl_id != 0) 1244 if (resource->type != GLTexture || resource->gl_id != 0)
1286 return; 1245 return;
1287 1246
1288 // Early out for resources that don't require texture creation. 1247 // Early out for resources that don't require texture creation.
1289 if (resource->texture_pool == 0) 1248 if (resource->texture_pool == 0)
1290 return; 1249 return;
1291 1250
1292 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1251 WebGraphicsContext3D* context3d = output_surface_->context3d();
1293 DCHECK(context3d); 1252 DCHECK(context3d);
1294 // Create and set texture properties. Allocation is delayed until needed. 1253 // Create and set texture properties. Allocation is delayed until needed.
1295 resource->gl_id = CreateTextureId(context3d); 1254 resource->gl_id = CreateTextureId(context3d);
1296 GLC(context3d, context3d->texParameteri(GL_TEXTURE_2D, 1255 GLC(context3d, context3d->texParameteri(GL_TEXTURE_2D,
1297 GL_TEXTURE_POOL_CHROMIUM, 1256 GL_TEXTURE_POOL_CHROMIUM,
1298 resource->texture_pool)); 1257 resource->texture_pool));
1299 if (use_texture_usage_hint_ && resource->hint == TextureUsageFramebuffer) { 1258 if (use_texture_usage_hint_ && resource->hint == TextureUsageFramebuffer) {
1300 GLC(context3d, context3d->texParameteri(GL_TEXTURE_2D, 1259 GLC(context3d, context3d->texParameteri(GL_TEXTURE_2D,
1301 GL_TEXTURE_USAGE_ANGLE, 1260 GL_TEXTURE_USAGE_ANGLE,
1302 GL_FRAMEBUFFER_ATTACHMENT_ANGLE)); 1261 GL_FRAMEBUFFER_ATTACHMENT_ANGLE));
1303 } 1262 }
1304 } 1263 }
1305 1264
1306 void ResourceProvider::AllocateForTesting(ResourceId id) { 1265 void ResourceProvider::AllocateForTesting(ResourceId id) {
1307 ResourceMap::iterator it = resources_.find(id); 1266 LazyAllocate(GetResource(id));
1308 CHECK(it != resources_.end());
1309 Resource* resource = &it->second;
1310 LazyAllocate(resource);
1311 } 1267 }
1312 1268
1313 void ResourceProvider::LazyAllocate(Resource* resource) { 1269 void ResourceProvider::LazyAllocate(Resource* resource) {
1314 DCHECK(resource); 1270 DCHECK(resource);
1315 LazyCreate(resource); 1271 LazyCreate(resource);
1316 1272
1317 DCHECK(resource->gl_id || resource->allocated); 1273 DCHECK(resource->gl_id || resource->allocated);
1318 if (resource->allocated || !resource->gl_id) 1274 if (resource->allocated || !resource->gl_id)
1319 return; 1275 return;
1320 resource->allocated = true; 1276 resource->allocated = true;
(...skipping 16 matching lines...) Expand all
1337 size.height(), 1293 size.height(),
1338 0, 1294 0,
1339 format, 1295 format,
1340 GL_UNSIGNED_BYTE, 1296 GL_UNSIGNED_BYTE,
1341 NULL)); 1297 NULL));
1342 } 1298 }
1343 } 1299 }
1344 1300
1345 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id, 1301 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id,
1346 bool enable) { 1302 bool enable) {
1347 DCHECK(thread_checker_.CalledOnValidThread()); 1303 Resource* resource = GetResource(id);
1348 ResourceMap::iterator it = resources_.find(id);
1349 CHECK(it != resources_.end());
1350 Resource* resource = &it->second;
1351 resource->enable_read_lock_fences = enable; 1304 resource->enable_read_lock_fences = enable;
1352 } 1305 }
1353 1306
1354 void ResourceProvider::AcquireImage(ResourceId id) { 1307 void ResourceProvider::AcquireImage(ResourceId id) {
1355 DCHECK(thread_checker_.CalledOnValidThread()); 1308 Resource* resource = GetResource(id);
1356 ResourceMap::iterator it = resources_.find(id);
1357 CHECK(it != resources_.end());
1358 Resource* resource = &it->second;
1359
1360 DCHECK(!resource->external); 1309 DCHECK(!resource->external);
1361 DCHECK_EQ(resource->exported_count, 0); 1310 DCHECK_EQ(resource->exported_count, 0);
1362 1311
1363 if (resource->type != GLTexture) 1312 if (resource->type != GLTexture)
1364 return; 1313 return;
1365 1314
1366 if (resource->image_id) 1315 if (resource->image_id)
1367 return; 1316 return;
1368 1317
1369 resource->allocated = true; 1318 resource->allocated = true;
1370 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1319 WebGraphicsContext3D* context3d = output_surface_->context3d();
1371 DCHECK(context3d); 1320 DCHECK(context3d);
1372 resource->image_id = context3d->createImageCHROMIUM( 1321 resource->image_id = context3d->createImageCHROMIUM(
1373 resource->size.width(), resource->size.height(), GL_RGBA8_OES); 1322 resource->size.width(), resource->size.height(), GL_RGBA8_OES);
1374 DCHECK(resource->image_id); 1323 DCHECK(resource->image_id);
1375 } 1324 }
1376 1325
1377 void ResourceProvider::ReleaseImage(ResourceId id) { 1326 void ResourceProvider::ReleaseImage(ResourceId id) {
1378 DCHECK(thread_checker_.CalledOnValidThread()); 1327 Resource* resource = GetResource(id);
1379 ResourceMap::iterator it = resources_.find(id);
1380 CHECK(it != resources_.end());
1381 Resource* resource = &it->second;
1382
1383 DCHECK(!resource->external); 1328 DCHECK(!resource->external);
1384 DCHECK_EQ(resource->exported_count, 0); 1329 DCHECK_EQ(resource->exported_count, 0);
1385 1330
1386 if (!resource->image_id) 1331 if (!resource->image_id)
1387 return; 1332 return;
1388 1333
1389 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1334 WebGraphicsContext3D* context3d = output_surface_->context3d();
1390 DCHECK(context3d); 1335 DCHECK(context3d);
1391 context3d->destroyImageCHROMIUM(resource->image_id); 1336 context3d->destroyImageCHROMIUM(resource->image_id);
1392 resource->image_id = 0; 1337 resource->image_id = 0;
1393 resource->allocated = false; 1338 resource->allocated = false;
1394 } 1339 }
1395 1340
1396 uint8_t* ResourceProvider::MapImage(ResourceId id) { 1341 uint8_t* ResourceProvider::MapImage(ResourceId id) {
1397 DCHECK(thread_checker_.CalledOnValidThread()); 1342 Resource* resource = GetResource(id);
1398 ResourceMap::iterator it = resources_.find(id);
1399 CHECK(it != resources_.end());
1400 Resource* resource = &it->second;
1401
1402 DCHECK(ReadLockFenceHasPassed(resource)); 1343 DCHECK(ReadLockFenceHasPassed(resource));
1403 DCHECK(!resource->external); 1344 DCHECK(!resource->external);
1404 DCHECK_EQ(resource->exported_count, 0); 1345 DCHECK_EQ(resource->exported_count, 0);
1405 1346
1406 if (resource->image_id) { 1347 if (resource->image_id) {
1407 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1348 WebGraphicsContext3D* context3d = output_surface_->context3d();
1408 DCHECK(context3d); 1349 DCHECK(context3d);
1409 return static_cast<uint8_t*>( 1350 return static_cast<uint8_t*>(
1410 context3d->mapImageCHROMIUM(resource->image_id, GL_READ_WRITE)); 1351 context3d->mapImageCHROMIUM(resource->image_id, GL_READ_WRITE));
1411 } 1352 }
1412 1353
1413 if (resource->pixels) 1354 if (resource->pixels)
1414 return resource->pixels; 1355 return resource->pixels;
1415 1356
1416 return NULL; 1357 return NULL;
1417 } 1358 }
1418 1359
1419 void ResourceProvider::UnmapImage(ResourceId id) { 1360 void ResourceProvider::UnmapImage(ResourceId id) {
1420 DCHECK(thread_checker_.CalledOnValidThread()); 1361 Resource* resource = GetResource(id);
1421 ResourceMap::iterator it = resources_.find(id);
1422 CHECK(it != resources_.end());
1423 Resource* resource = &it->second;
1424
1425 DCHECK(!resource->external); 1362 DCHECK(!resource->external);
1426 DCHECK_EQ(resource->exported_count, 0); 1363 DCHECK_EQ(resource->exported_count, 0);
1427 1364
1428 if (resource->image_id) { 1365 if (resource->image_id) {
1429 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1366 WebGraphicsContext3D* context3d = output_surface_->context3d();
1430 DCHECK(context3d); 1367 DCHECK(context3d);
1431 context3d->unmapImageCHROMIUM(resource->image_id); 1368 context3d->unmapImageCHROMIUM(resource->image_id);
1432 } 1369 }
1433 } 1370 }
1434 1371
1435 int ResourceProvider::GetImageStride(ResourceId id) { 1372 int ResourceProvider::GetImageStride(ResourceId id) {
1436 DCHECK(thread_checker_.CalledOnValidThread()); 1373 Resource* resource = GetResource(id);
1437 ResourceMap::iterator it = resources_.find(id);
1438 CHECK(it != resources_.end());
1439 Resource* resource = &it->second;
1440
1441 DCHECK(!resource->external); 1374 DCHECK(!resource->external);
1442 DCHECK_EQ(resource->exported_count, 0); 1375 DCHECK_EQ(resource->exported_count, 0);
1443 1376
1444 int stride = 0; 1377 int stride = 0;
1445 1378
1446 if (resource->image_id) { 1379 if (resource->image_id) {
1447 WebGraphicsContext3D* context3d = output_surface_->context3d(); 1380 WebGraphicsContext3D* context3d = output_surface_->context3d();
1448 DCHECK(context3d); 1381 DCHECK(context3d);
1449 context3d->getImageParameterivCHROMIUM( 1382 context3d->getImageParameterivCHROMIUM(
1450 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride); 1383 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride);
1451 } 1384 }
1452 1385
1453 return stride; 1386 return stride;
1454 } 1387 }
1455 1388
1456 GLint ResourceProvider::GetActiveTextureUnit(WebGraphicsContext3D* context) { 1389 GLint ResourceProvider::GetActiveTextureUnit(WebGraphicsContext3D* context) {
1457 GLint active_unit = 0; 1390 GLint active_unit = 0;
1458 context->getIntegerv(GL_ACTIVE_TEXTURE, &active_unit); 1391 context->getIntegerv(GL_ACTIVE_TEXTURE, &active_unit);
1459 return active_unit; 1392 return active_unit;
1460 } 1393 }
1461 1394
1462 } // namespace cc 1395 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698