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/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 ResourceId id = next_id_++; | 229 ResourceId id = next_id_++; |
230 Resource resource(texture_id, size, format, GL_LINEAR); | 230 Resource resource(texture_id, size, format, GL_LINEAR); |
231 resource.allocated = false; | 231 resource.allocated = false; |
232 resources_[id] = resource; | 232 resources_[id] = resource; |
233 return id; | 233 return id; |
234 } | 234 } |
235 | 235 |
236 ResourceProvider::ResourceId ResourceProvider::CreateBitmap(gfx::Size size) { | 236 ResourceProvider::ResourceId ResourceProvider::CreateBitmap(gfx::Size size) { |
237 DCHECK(thread_checker_.CalledOnValidThread()); | 237 DCHECK(thread_checker_.CalledOnValidThread()); |
238 | 238 |
239 uint8_t* pixels = new uint8_t[size.width() * size.height() * 4]; | 239 uint8_t* pixels = new uint8_t[4 * size.GetArea()]; |
240 | 240 |
241 ResourceId id = next_id_++; | 241 ResourceId id = next_id_++; |
242 Resource resource(pixels, size, GL_RGBA, GL_LINEAR); | 242 Resource resource(pixels, size, GL_RGBA, GL_LINEAR); |
243 resource.allocated = true; | 243 resource.allocated = true; |
244 resources_[id] = resource; | 244 resources_[id] = resource; |
245 return id; | 245 return id; |
246 } | 246 } |
247 | 247 |
248 ResourceProvider::ResourceId | 248 ResourceProvider::ResourceId |
249 ResourceProvider::CreateResourceFromExternalTexture( | 249 ResourceProvider::CreateResourceFromExternalTexture( |
(...skipping 19 matching lines...) Expand all Loading... | |
269 resource.allocated = true; | 269 resource.allocated = true; |
270 resources_[id] = resource; | 270 resources_[id] = resource; |
271 return id; | 271 return id; |
272 } | 272 } |
273 | 273 |
274 ResourceProvider::ResourceId ResourceProvider::CreateResourceFromTextureMailbox( | 274 ResourceProvider::ResourceId ResourceProvider::CreateResourceFromTextureMailbox( |
275 const TextureMailbox& mailbox) { | 275 const TextureMailbox& mailbox) { |
276 DCHECK(thread_checker_.CalledOnValidThread()); | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
277 // Just store the information. Mailbox will be consumed in LockForRead(). | 277 // Just store the information. Mailbox will be consumed in LockForRead(). |
278 ResourceId id = next_id_++; | 278 ResourceId id = next_id_++; |
279 unsigned texture_id = 0; | 279 DCHECK(mailbox.IsValid()); |
280 Resource resource(texture_id, gfx::Size(), 0, GL_LINEAR); | 280 if (mailbox.IsTexture()) { |
281 resource.external = true; | 281 unsigned texture_id = 0; |
282 resource.allocated = true; | 282 Resource resource(texture_id, gfx::Size(), 0, GL_LINEAR); |
283 resource.mailbox = mailbox; | 283 resource.external = true; |
284 resources_[id] = resource; | 284 resource.allocated = true; |
285 resource.mailbox = mailbox; | |
286 resources_[id] = resource; | |
287 } else { | |
288 DCHECK(mailbox.IsSharedMemory()); | |
289 uint8_t *pixels = NULL; | |
290 Resource resource(pixels, mailbox.shared_memory_size(), GL_RGBA, GL_LINEAR); | |
291 resource.external = true; | |
292 resource.allocated = true; | |
293 resource.mailbox = mailbox; | |
294 resources_[id] = resource; | |
piman
2013/06/07 01:26:28
nit: can we share the setting of external/allocate
slavi
2013/06/07 21:49:21
Done.
| |
295 } | |
285 return id; | 296 return id; |
286 } | 297 } |
287 | 298 |
288 void ResourceProvider::DeleteResource(ResourceId id) { | 299 void ResourceProvider::DeleteResource(ResourceId id) { |
289 DCHECK(thread_checker_.CalledOnValidThread()); | 300 DCHECK(thread_checker_.CalledOnValidThread()); |
290 ResourceMap::iterator it = resources_.find(id); | 301 ResourceMap::iterator it = resources_.find(id); |
291 CHECK(it != resources_.end()); | 302 CHECK(it != resources_.end()); |
292 Resource* resource = &it->second; | 303 Resource* resource = &it->second; |
293 DCHECK(!resource->lock_for_read_count); | 304 DCHECK(!resource->lock_for_read_count); |
294 DCHECK(!resource->marked_for_deletion); | 305 DCHECK(!resource->marked_for_deletion); |
(...skipping 30 matching lines...) Expand all Loading... | |
325 if (resource->gl_upload_query_id) { | 336 if (resource->gl_upload_query_id) { |
326 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 337 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
327 DCHECK(context3d); | 338 DCHECK(context3d); |
328 GLC(context3d, context3d->deleteQueryEXT(resource->gl_upload_query_id)); | 339 GLC(context3d, context3d->deleteQueryEXT(resource->gl_upload_query_id)); |
329 } | 340 } |
330 if (resource->gl_pixel_buffer_id) { | 341 if (resource->gl_pixel_buffer_id) { |
331 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 342 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
332 DCHECK(context3d); | 343 DCHECK(context3d); |
333 GLC(context3d, context3d->deleteBuffer(resource->gl_pixel_buffer_id)); | 344 GLC(context3d, context3d->deleteBuffer(resource->gl_pixel_buffer_id)); |
334 } | 345 } |
335 if (!resource->mailbox.IsEmpty() && resource->external) { | 346 if (resource->mailbox.IsValid() && resource->external) { |
336 WebGraphicsContext3D* context3d = output_surface_->context3d(); | |
337 DCHECK(context3d); | |
338 unsigned sync_point = resource->mailbox.sync_point(); | 347 unsigned sync_point = resource->mailbox.sync_point(); |
339 if (!lost_resource && resource->gl_id) { | 348 if (resource->mailbox.IsTexture()) { |
340 GLC(context3d, context3d->bindTexture( | 349 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
341 resource->mailbox.target(), resource->gl_id)); | 350 DCHECK(context3d); |
342 GLC(context3d, context3d->produceTextureCHROMIUM( | 351 if (!lost_resource && resource->gl_id) { |
343 resource->mailbox.target(), resource->mailbox.data())); | 352 GLC(context3d, context3d->bindTexture( |
353 resource->mailbox.target(), resource->gl_id)); | |
354 GLC(context3d, context3d->produceTextureCHROMIUM( | |
355 resource->mailbox.target(), resource->mailbox.data())); | |
356 } | |
357 if (resource->gl_id) | |
358 GLC(context3d, context3d->deleteTexture(resource->gl_id)); | |
359 if (!lost_resource && resource->gl_id) | |
360 sync_point = context3d->insertSyncPoint(); | |
361 } else { | |
362 DCHECK(resource->mailbox.IsSharedMemory()); | |
363 base::SharedMemory* shared_memory = resource->mailbox.shared_memory(); | |
364 if (resource->pixels && shared_memory) { | |
365 DCHECK(shared_memory->memory() == resource->pixels); | |
366 resource->pixels = NULL; | |
367 } | |
344 } | 368 } |
345 if (resource->gl_id) | |
346 GLC(context3d, context3d->deleteTexture(resource->gl_id)); | |
347 if (!lost_resource && resource->gl_id) | |
348 sync_point = context3d->insertSyncPoint(); | |
349 resource->mailbox.RunReleaseCallback(sync_point, lost_resource); | 369 resource->mailbox.RunReleaseCallback(sync_point, lost_resource); |
350 } | 370 } |
351 if (resource->pixels) | 371 if (resource->pixels) |
352 delete[] resource->pixels; | 372 delete[] resource->pixels; |
353 if (resource->pixel_buffer) | 373 if (resource->pixel_buffer) |
354 delete[] resource->pixel_buffer; | 374 delete[] resource->pixel_buffer; |
355 | 375 |
356 resources_.erase(it); | 376 resources_.erase(it); |
357 } | 377 } |
358 | 378 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 CHECK(it != resources_.end()); | 500 CHECK(it != resources_.end()); |
481 Resource* resource = &it->second; | 501 Resource* resource = &it->second; |
482 DCHECK(!resource->locked_for_write || | 502 DCHECK(!resource->locked_for_write || |
483 resource->set_pixels_completion_forced) << | 503 resource->set_pixels_completion_forced) << |
484 "locked for write: " << resource->locked_for_write << | 504 "locked for write: " << resource->locked_for_write << |
485 " pixels completion forced: " << resource->set_pixels_completion_forced; | 505 " pixels completion forced: " << resource->set_pixels_completion_forced; |
486 DCHECK(!resource->exported); | 506 DCHECK(!resource->exported); |
487 // Uninitialized! Call SetPixels or LockForWrite first. | 507 // Uninitialized! Call SetPixels or LockForWrite first. |
488 DCHECK(resource->allocated); | 508 DCHECK(resource->allocated); |
489 | 509 |
490 if (!resource->gl_id && resource->external && !resource->mailbox.IsEmpty()) { | 510 if (resource->external && resource->mailbox.IsValid()) { |
491 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 511 bool is_texture_mailbox = resource->mailbox.IsTexture(); |
492 DCHECK(context3d); | 512 if (!resource->gl_id && is_texture_mailbox) { |
493 if (resource->mailbox.sync_point()) { | 513 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
494 GLC(context3d, context3d->waitSyncPoint(resource->mailbox.sync_point())); | 514 DCHECK(context3d); |
495 resource->mailbox.ResetSyncPoint(); | 515 if (resource->mailbox.sync_point()) { |
516 GLC(context3d, | |
517 context3d->waitSyncPoint(resource->mailbox.sync_point())); | |
518 resource->mailbox.ResetSyncPoint(); | |
519 } | |
520 resource->gl_id = context3d->createTexture(); | |
521 GLC(context3d, context3d->bindTexture( | |
522 resource->mailbox.target(), resource->gl_id)); | |
523 GLC(context3d, context3d->consumeTextureCHROMIUM( | |
524 resource->mailbox.target(), resource->mailbox.data())); | |
525 } else if (!resource->pixels && !is_texture_mailbox) { | |
526 DCHECK(resource->mailbox.IsSharedMemory()); | |
527 base::SharedMemory* shared_memory = resource->mailbox.shared_memory(); | |
528 DCHECK(shared_memory->memory()); | |
529 resource->pixels = reinterpret_cast<uint8_t*>(shared_memory->memory()); | |
piman
2013/06/07 01:26:28
nit: we could set resource->pixels directly in Cre
slavi
2013/06/07 21:49:21
Done.
| |
496 } | 530 } |
497 resource->gl_id = context3d->createTexture(); | |
498 GLC(context3d, context3d->bindTexture( | |
499 resource->mailbox.target(), resource->gl_id)); | |
500 GLC(context3d, context3d->consumeTextureCHROMIUM( | |
501 resource->mailbox.target(), resource->mailbox.data())); | |
502 } | 531 } |
503 | 532 |
504 resource->lock_for_read_count++; | 533 resource->lock_for_read_count++; |
505 if (resource->enable_read_lock_fences) | 534 if (resource->enable_read_lock_fences) |
506 resource->read_lock_fence = current_read_lock_fence_; | 535 resource->read_lock_fence = current_read_lock_fence_; |
507 | 536 |
508 return resource; | 537 return resource; |
509 } | 538 } |
510 | 539 |
511 void ResourceProvider::UnlockForRead(ResourceId id) { | 540 void ResourceProvider::UnlockForRead(ResourceId id) { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
833 } | 862 } |
834 for (TransferableResourceArray::const_iterator it = resources.begin(); | 863 for (TransferableResourceArray::const_iterator it = resources.begin(); |
835 it != resources.end(); | 864 it != resources.end(); |
836 ++it) { | 865 ++it) { |
837 ResourceMap::iterator map_iterator = resources_.find(it->id); | 866 ResourceMap::iterator map_iterator = resources_.find(it->id); |
838 DCHECK(map_iterator != resources_.end()); | 867 DCHECK(map_iterator != resources_.end()); |
839 Resource* resource = &map_iterator->second; | 868 Resource* resource = &map_iterator->second; |
840 DCHECK(resource->exported); | 869 DCHECK(resource->exported); |
841 resource->exported = false; | 870 resource->exported = false; |
842 resource->filter = it->filter; | 871 resource->filter = it->filter; |
843 DCHECK(resource->mailbox.Equals(it->mailbox)); | 872 DCHECK(resource->mailbox.ContainsMailbox(it->mailbox)); |
844 if (resource->gl_id) { | 873 if (resource->gl_id) { |
845 if (it->sync_point) | 874 if (it->sync_point) |
846 GLC(context3d, context3d->waitSyncPoint(it->sync_point)); | 875 GLC(context3d, context3d->waitSyncPoint(it->sync_point)); |
847 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, resource->gl_id)); | 876 GLC(context3d, context3d->bindTexture(GL_TEXTURE_2D, resource->gl_id)); |
848 GLC(context3d, context3d->consumeTextureCHROMIUM(GL_TEXTURE_2D, | 877 GLC(context3d, context3d->consumeTextureCHROMIUM(GL_TEXTURE_2D, |
849 it->mailbox.name)); | 878 it->mailbox.name)); |
850 } else { | 879 } else { |
851 resource->mailbox = TextureMailbox(resource->mailbox.name(), | 880 resource->mailbox = TextureMailbox(resource->mailbox.name(), |
852 resource->mailbox.callback(), | 881 resource->mailbox.callback(), |
853 it->sync_point); | 882 it->sync_point); |
854 } | 883 } |
855 if (resource->marked_for_deletion) | 884 if (resource->marked_for_deletion) |
856 DeleteResourceInternal(map_iterator, Normal); | 885 DeleteResourceInternal(map_iterator, Normal); |
857 } | 886 } |
858 } | 887 } |
859 | 888 |
860 bool ResourceProvider::TransferResource(WebGraphicsContext3D* context, | 889 bool ResourceProvider::TransferResource(WebGraphicsContext3D* context, |
861 ResourceId id, | 890 ResourceId id, |
862 TransferableResource* resource) { | 891 TransferableResource* resource) { |
863 DCHECK(thread_checker_.CalledOnValidThread()); | 892 DCHECK(thread_checker_.CalledOnValidThread()); |
864 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 893 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
865 ResourceMap::iterator it = resources_.find(id); | 894 ResourceMap::iterator it = resources_.find(id); |
866 CHECK(it != resources_.end()); | 895 CHECK(it != resources_.end()); |
867 Resource* source = &it->second; | 896 Resource* source = &it->second; |
868 DCHECK(!source->locked_for_write); | 897 DCHECK(!source->locked_for_write); |
869 DCHECK(!source->lock_for_read_count); | 898 DCHECK(!source->lock_for_read_count); |
870 DCHECK(!source->external || (source->external && !source->mailbox.IsEmpty())); | 899 DCHECK(!source->external || (source->external && source->mailbox.IsValid())); |
871 DCHECK(source->allocated); | 900 DCHECK(source->allocated); |
872 if (source->exported) | 901 if (source->exported) |
873 return false; | 902 return false; |
874 resource->id = id; | 903 resource->id = id; |
875 resource->format = source->format; | 904 resource->format = source->format; |
876 resource->filter = source->filter; | 905 resource->filter = source->filter; |
877 resource->size = source->size; | 906 resource->size = source->size; |
878 | 907 |
879 if (source->mailbox.IsEmpty()) { | 908 // TODO(skaslev) Implement this path for shared memory resources. |
909 DCHECK(!source->mailbox.IsSharedMemory()); | |
910 | |
911 if (!source->mailbox.IsTexture()) { | |
danakj
2013/06/07 14:56:42
This if looks awkward now, as the else implicitly
slavi
2013/06/07 21:49:21
I agree, hence the DCHECK and TODO comment above.
| |
880 GLC(context3d, context3d->genMailboxCHROMIUM(resource->mailbox.name)); | 912 GLC(context3d, context3d->genMailboxCHROMIUM(resource->mailbox.name)); |
881 source->mailbox.SetName(resource->mailbox); | 913 source->mailbox.SetName(resource->mailbox); |
882 } else { | 914 } else { |
883 resource->mailbox = source->mailbox.name(); | 915 resource->mailbox = source->mailbox.name(); |
884 } | 916 } |
885 | 917 |
886 if (source->gl_id) { | 918 if (source->gl_id) { |
887 GLC(context, context->bindTexture(GL_TEXTURE_2D, source->gl_id)); | 919 GLC(context, context->bindTexture(GL_TEXTURE_2D, source->gl_id)); |
888 GLC(context, context->produceTextureCHROMIUM(GL_TEXTURE_2D, | 920 GLC(context, context->produceTextureCHROMIUM(GL_TEXTURE_2D, |
889 resource->mailbox.name)); | 921 resource->mailbox.name)); |
(...skipping 16 matching lines...) Expand all Loading... | |
906 if (resource->gl_id) { | 938 if (resource->gl_id) { |
907 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 939 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
908 DCHECK(context3d); | 940 DCHECK(context3d); |
909 if (!resource->gl_pixel_buffer_id) | 941 if (!resource->gl_pixel_buffer_id) |
910 resource->gl_pixel_buffer_id = context3d->createBuffer(); | 942 resource->gl_pixel_buffer_id = context3d->createBuffer(); |
911 context3d->bindBuffer( | 943 context3d->bindBuffer( |
912 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 944 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
913 resource->gl_pixel_buffer_id); | 945 resource->gl_pixel_buffer_id); |
914 context3d->bufferData( | 946 context3d->bufferData( |
915 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 947 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
916 resource->size.width() * resource->size.height() * 4, | 948 4 * resource->size.GetArea(), |
917 NULL, | 949 NULL, |
918 GL_DYNAMIC_DRAW); | 950 GL_DYNAMIC_DRAW); |
919 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 951 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
920 } | 952 } |
921 | 953 |
922 if (resource->pixels) { | 954 if (resource->pixels) { |
923 if (resource->pixel_buffer) | 955 if (resource->pixel_buffer) |
924 return; | 956 return; |
925 | 957 |
926 resource->pixel_buffer = new uint8_t[ | 958 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; |
927 resource->size.width() * resource->size.height() * 4]; | |
928 } | 959 } |
929 } | 960 } |
930 | 961 |
931 void ResourceProvider::ReleasePixelBuffer(ResourceId id) { | 962 void ResourceProvider::ReleasePixelBuffer(ResourceId id) { |
932 DCHECK(thread_checker_.CalledOnValidThread()); | 963 DCHECK(thread_checker_.CalledOnValidThread()); |
933 ResourceMap::iterator it = resources_.find(id); | 964 ResourceMap::iterator it = resources_.find(id); |
934 CHECK(it != resources_.end()); | 965 CHECK(it != resources_.end()); |
935 Resource* resource = &it->second; | 966 Resource* resource = &it->second; |
936 DCHECK(!resource->external); | 967 DCHECK(!resource->external); |
937 DCHECK(!resource->exported); | 968 DCHECK(!resource->exported); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1042 resource->size.height(), | 1073 resource->size.height(), |
1043 resource->format, | 1074 resource->format, |
1044 GL_UNSIGNED_BYTE, | 1075 GL_UNSIGNED_BYTE, |
1045 NULL); | 1076 NULL); |
1046 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1077 context3d->bindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1047 context3d->deleteBuffer(resource->gl_pixel_buffer_id); | 1078 context3d->deleteBuffer(resource->gl_pixel_buffer_id); |
1048 resource->gl_pixel_buffer_id = 0; | 1079 resource->gl_pixel_buffer_id = 0; |
1049 } | 1080 } |
1050 | 1081 |
1051 if (resource->pixels) { | 1082 if (resource->pixels) { |
1083 DCHECK(!resource->mailbox.IsValid()); | |
1052 DCHECK(resource->pixel_buffer); | 1084 DCHECK(resource->pixel_buffer); |
1053 DCHECK(resource->format == GL_RGBA); | 1085 DCHECK(resource->format == GL_RGBA); |
1054 | 1086 |
1055 ScopedWriteLockSoftware lock(this, id); | 1087 ScopedWriteLockSoftware lock(this, id); |
1056 std::swap(resource->pixels, resource->pixel_buffer); | 1088 std::swap(resource->pixels, resource->pixel_buffer); |
1057 delete[] resource->pixel_buffer; | 1089 delete[] resource->pixel_buffer; |
1058 resource->pixel_buffer = NULL; | 1090 resource->pixel_buffer = NULL; |
1059 } | 1091 } |
1060 } | 1092 } |
1061 | 1093 |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1369 WebGraphicsContext3D* context3d = output_surface_->context3d(); | 1401 WebGraphicsContext3D* context3d = output_surface_->context3d(); |
1370 DCHECK(context3d); | 1402 DCHECK(context3d); |
1371 int stride = 0; | 1403 int stride = 0; |
1372 context3d->getImageParameterivCHROMIUM( | 1404 context3d->getImageParameterivCHROMIUM( |
1373 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride); | 1405 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride); |
1374 return stride; | 1406 return stride; |
1375 } | 1407 } |
1376 | 1408 |
1377 | 1409 |
1378 } // namespace cc | 1410 } // namespace cc |
OLD | NEW |