OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/resource_provider.h" | 5 #include "cc/resources/resource_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "cc/base/util.h" | 15 #include "cc/base/util.h" |
16 #include "cc/output/gl_renderer.h" // For the GLC() macro. | 16 #include "cc/output/gl_renderer.h" // For the GLC() macro. |
17 #include "cc/resources/platform_color.h" | 17 #include "cc/resources/platform_color.h" |
18 #include "cc/resources/returned_resource.h" | 18 #include "cc/resources/returned_resource.h" |
19 #include "cc/resources/shared_bitmap_manager.h" | 19 #include "cc/resources/shared_bitmap_manager.h" |
20 #include "cc/resources/texture_uploader.h" | 20 #include "cc/resources/texture_uploader.h" |
21 #include "cc/resources/transferable_resource.h" | 21 #include "cc/resources/transferable_resource.h" |
22 #include "gpu/GLES2/gl2extchromium.h" | 22 #include "gpu/GLES2/gl2extchromium.h" |
23 #include "gpu/command_buffer/client/gles2_interface.h" | 23 #include "gpu/command_buffer/client/gles2_interface.h" |
24 #include "third_party/khronos/GLES2/gl2.h" | 24 #include "third_party/khronos/GLES2/gl2.h" |
25 #include "third_party/khronos/GLES2/gl2ext.h" | 25 #include "third_party/khronos/GLES2/gl2ext.h" |
| 26 #include "third_party/skia/include/core/SkSurface.h" |
| 27 #include "third_party/skia/include/gpu/GrContext.h" |
| 28 #include "third_party/skia/include/gpu/SkGpuDevice.h" |
26 #include "ui/gfx/frame_time.h" | 29 #include "ui/gfx/frame_time.h" |
27 #include "ui/gfx/rect.h" | 30 #include "ui/gfx/rect.h" |
28 #include "ui/gfx/vector2d.h" | 31 #include "ui/gfx/vector2d.h" |
29 | 32 |
30 using gpu::gles2::GLES2Interface; | 33 using gpu::gles2::GLES2Interface; |
31 | 34 |
32 namespace cc { | 35 namespace cc { |
33 | 36 |
34 class IdAllocator { | 37 class IdAllocator { |
35 public: | 38 public: |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 return true; | 87 return true; |
85 case RGBA_4444: | 88 case RGBA_4444: |
86 case LUMINANCE_8: | 89 case LUMINANCE_8: |
87 case RGB_565: | 90 case RGB_565: |
88 case ETC1: | 91 case ETC1: |
89 return false; | 92 return false; |
90 } | 93 } |
91 return false; | 94 return false; |
92 } | 95 } |
93 | 96 |
| 97 GrPixelConfig ToGrPixelConfig(ResourceFormat format) { |
| 98 switch (format) { |
| 99 case RGBA_8888: |
| 100 return kRGBA_8888_GrPixelConfig; |
| 101 case BGRA_8888: |
| 102 return kBGRA_8888_GrPixelConfig; |
| 103 case RGBA_4444: |
| 104 return kRGBA_4444_GrPixelConfig; |
| 105 default: |
| 106 break; |
| 107 } |
| 108 DCHECK(false) << "Unsupported resource format."; |
| 109 return kSkia8888_GrPixelConfig; |
| 110 } |
| 111 |
| 112 class IdentityAllocator : public SkBitmap::Allocator { |
| 113 public: |
| 114 explicit IdentityAllocator(void* buffer) : buffer_(buffer) {} |
| 115 virtual bool allocPixelRef(SkBitmap* dst, SkColorTable*) OVERRIDE { |
| 116 dst->setPixels(buffer_); |
| 117 return true; |
| 118 } |
| 119 |
| 120 private: |
| 121 void* buffer_; |
| 122 }; |
| 123 |
| 124 void CopyBitmap(const SkBitmap& src, |
| 125 uint8_t* dst, |
| 126 SkBitmap::Config dst_config) { |
| 127 SkBitmap dst_bitmap; |
| 128 IdentityAllocator allocator(dst); |
| 129 src.copyTo(&dst_bitmap, dst_config, &allocator); |
| 130 // TODO(kaanb): The GL pipeline assumes a 4-byte alignment for the |
| 131 // bitmap data. This check will be removed once crbug.com/293728 is fixed. |
| 132 CHECK_EQ(0u, dst_bitmap.rowBytes() % 4); |
| 133 } |
| 134 |
94 class ScopedSetActiveTexture { | 135 class ScopedSetActiveTexture { |
95 public: | 136 public: |
96 ScopedSetActiveTexture(GLES2Interface* gl, GLenum unit) | 137 ScopedSetActiveTexture(GLES2Interface* gl, GLenum unit) |
97 : gl_(gl), unit_(unit) { | 138 : gl_(gl), unit_(unit) { |
98 DCHECK_EQ(GL_TEXTURE0, ResourceProvider::GetActiveTextureUnit(gl_)); | 139 DCHECK_EQ(GL_TEXTURE0, ResourceProvider::GetActiveTextureUnit(gl_)); |
99 | 140 |
100 if (unit_ != GL_TEXTURE0) | 141 if (unit_ != GL_TEXTURE0) |
101 GLC(gl_, gl_->ActiveTexture(unit_)); | 142 GLC(gl_, gl_->ActiveTexture(unit_)); |
102 } | 143 } |
103 | 144 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 lost(false), | 361 lost(false), |
321 hint(TextureUsageAny), | 362 hint(TextureUsageAny), |
322 type(Bitmap), | 363 type(Bitmap), |
323 format(RGBA_8888), | 364 format(RGBA_8888), |
324 has_shared_bitmap_id(true), | 365 has_shared_bitmap_id(true), |
325 shared_bitmap_id(bitmap_id), | 366 shared_bitmap_id(bitmap_id), |
326 shared_bitmap(NULL) { | 367 shared_bitmap(NULL) { |
327 DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); | 368 DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); |
328 } | 369 } |
329 | 370 |
| 371 ResourceProvider::RasterBuffer::RasterBuffer( |
| 372 const Resource* resource, |
| 373 ResourceProvider* resource_provider) |
| 374 : resource_(resource), |
| 375 resource_provider_(resource_provider), |
| 376 locked_canvas_(NULL), |
| 377 canvas_save_count_(0) { |
| 378 DCHECK(resource_); |
| 379 DCHECK(resource_provider_); |
| 380 } |
| 381 |
| 382 ResourceProvider::RasterBuffer::~RasterBuffer() {} |
| 383 |
| 384 SkCanvas* ResourceProvider::RasterBuffer::LockForWrite() { |
| 385 DCHECK(!locked_canvas_); |
| 386 |
| 387 locked_canvas_ = DoLockForWrite(); |
| 388 canvas_save_count_ = locked_canvas_ ? locked_canvas_->save() : 0; |
| 389 return locked_canvas_; |
| 390 } |
| 391 |
| 392 void ResourceProvider::RasterBuffer::UnlockForWrite() { |
| 393 if (locked_canvas_) { |
| 394 locked_canvas_->restoreToCount(canvas_save_count_); |
| 395 locked_canvas_ = NULL; |
| 396 } |
| 397 DoUnlockForWrite(); |
| 398 } |
| 399 |
| 400 ResourceProvider::DirectRasterBuffer::DirectRasterBuffer( |
| 401 const Resource* resource, |
| 402 ResourceProvider* resource_provider) |
| 403 : RasterBuffer(resource, resource_provider) {} |
| 404 |
| 405 ResourceProvider::DirectRasterBuffer::~DirectRasterBuffer() {} |
| 406 |
| 407 SkCanvas* ResourceProvider::DirectRasterBuffer::DoLockForWrite() { |
| 408 if (!surface_) |
| 409 surface_ = CreateSurface(); |
| 410 return surface_ ? surface_->getCanvas() : NULL; |
| 411 } |
| 412 |
| 413 void ResourceProvider::DirectRasterBuffer::DoUnlockForWrite() {} |
| 414 |
| 415 skia::RefPtr<SkSurface> ResourceProvider::DirectRasterBuffer::CreateSurface() { |
| 416 skia::RefPtr<SkSurface> surface; |
| 417 switch (resource()->type) { |
| 418 case GLTexture: { |
| 419 DCHECK(resource()->gl_id); |
| 420 class GrContext* gr_context = resource_provider()->GrContext(); |
| 421 if (gr_context) { |
| 422 GrBackendTextureDesc desc; |
| 423 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 424 desc.fWidth = resource()->size.width(); |
| 425 desc.fHeight = resource()->size.height(); |
| 426 desc.fConfig = ToGrPixelConfig(resource()->format); |
| 427 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 428 desc.fTextureHandle = resource()->gl_id; |
| 429 skia::RefPtr<GrTexture> gr_texture = |
| 430 skia::AdoptRef(gr_context->wrapBackendTexture(desc)); |
| 431 surface = skia::AdoptRef( |
| 432 SkSurface::NewRenderTargetDirect(gr_texture->asRenderTarget())); |
| 433 } |
| 434 break; |
| 435 } |
| 436 case Bitmap: { |
| 437 DCHECK(resource()->pixels); |
| 438 DCHECK_EQ(RGBA_8888, resource()->format); |
| 439 SkImageInfo image_info = SkImageInfo::MakeN32Premul( |
| 440 resource()->size.width(), resource()->size.height()); |
| 441 size_t row_bytes = SkBitmap::ComputeRowBytes(SkBitmap::kARGB_8888_Config, |
| 442 resource()->size.width()); |
| 443 surface = skia::AdoptRef(SkSurface::NewRasterDirect( |
| 444 image_info, resource()->pixels, row_bytes)); |
| 445 break; |
| 446 } |
| 447 default: |
| 448 NOTREACHED(); |
| 449 } |
| 450 return surface; |
| 451 } |
| 452 |
| 453 ResourceProvider::BitmapRasterBuffer::BitmapRasterBuffer( |
| 454 const Resource* resource, |
| 455 ResourceProvider* resource_provider) |
| 456 : RasterBuffer(resource, resource_provider), mapped_buffer_(NULL) {} |
| 457 |
| 458 ResourceProvider::BitmapRasterBuffer::~BitmapRasterBuffer() {} |
| 459 |
| 460 SkCanvas* ResourceProvider::BitmapRasterBuffer::DoLockForWrite() { |
| 461 DCHECK(!mapped_buffer_); |
| 462 DCHECK(!raster_canvas_); |
| 463 |
| 464 int stride = 0; |
| 465 mapped_buffer_ = MapBuffer(&stride); |
| 466 if (!mapped_buffer_) |
| 467 return NULL; |
| 468 |
| 469 switch (resource()->format) { |
| 470 case RGBA_4444: |
| 471 // Use the default stride if we will eventually convert this |
| 472 // bitmap to 4444. |
| 473 raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 474 resource()->size.width(), |
| 475 resource()->size.height()); |
| 476 raster_bitmap_.allocPixels(); |
| 477 break; |
| 478 case RGBA_8888: |
| 479 case BGRA_8888: |
| 480 raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 481 resource()->size.width(), |
| 482 resource()->size.height(), |
| 483 stride); |
| 484 raster_bitmap_.setPixels(mapped_buffer_); |
| 485 break; |
| 486 case LUMINANCE_8: |
| 487 case RGB_565: |
| 488 case ETC1: |
| 489 NOTREACHED(); |
| 490 break; |
| 491 } |
| 492 skia::RefPtr<SkBitmapDevice> device = |
| 493 skia::AdoptRef(new SkBitmapDevice(raster_bitmap_)); |
| 494 raster_canvas_ = skia::AdoptRef(new SkCanvas(device.get())); |
| 495 return raster_canvas_.get(); |
| 496 } |
| 497 |
| 498 void ResourceProvider::BitmapRasterBuffer::DoUnlockForWrite() { |
| 499 raster_canvas_.clear(); |
| 500 |
| 501 SkBitmap::Config buffer_config = SkBitmapConfig(resource()->format); |
| 502 if (mapped_buffer_ && (buffer_config != raster_bitmap_.config())) |
| 503 CopyBitmap(raster_bitmap_, mapped_buffer_, buffer_config); |
| 504 raster_bitmap_.reset(); |
| 505 |
| 506 UnmapBuffer(); |
| 507 mapped_buffer_ = NULL; |
| 508 } |
| 509 |
| 510 ResourceProvider::ImageRasterBuffer::ImageRasterBuffer( |
| 511 const Resource* resource, |
| 512 ResourceProvider* resource_provider) |
| 513 : BitmapRasterBuffer(resource, resource_provider) {} |
| 514 |
| 515 ResourceProvider::ImageRasterBuffer::~ImageRasterBuffer() {} |
| 516 |
| 517 uint8_t* ResourceProvider::ImageRasterBuffer::MapBuffer(int* stride) { |
| 518 return resource_provider()->MapImage(resource(), stride); |
| 519 } |
| 520 |
| 521 void ResourceProvider::ImageRasterBuffer::UnmapBuffer() { |
| 522 resource_provider()->UnmapImage(resource()); |
| 523 } |
| 524 |
| 525 ResourceProvider::PixelRasterBuffer::PixelRasterBuffer( |
| 526 const Resource* resource, |
| 527 ResourceProvider* resource_provider) |
| 528 : BitmapRasterBuffer(resource, resource_provider) {} |
| 529 |
| 530 ResourceProvider::PixelRasterBuffer::~PixelRasterBuffer() {} |
| 531 |
| 532 uint8_t* ResourceProvider::PixelRasterBuffer::MapBuffer(int* stride) { |
| 533 return resource_provider()->MapPixelBuffer(resource(), stride); |
| 534 } |
| 535 |
| 536 void ResourceProvider::PixelRasterBuffer::UnmapBuffer() { |
| 537 resource_provider()->UnmapPixelBuffer(resource()); |
| 538 } |
| 539 |
330 ResourceProvider::Child::Child() : marked_for_deletion(false) {} | 540 ResourceProvider::Child::Child() : marked_for_deletion(false) {} |
331 | 541 |
332 ResourceProvider::Child::~Child() {} | 542 ResourceProvider::Child::~Child() {} |
333 | 543 |
334 scoped_ptr<ResourceProvider> ResourceProvider::Create( | 544 scoped_ptr<ResourceProvider> ResourceProvider::Create( |
335 OutputSurface* output_surface, | 545 OutputSurface* output_surface, |
336 SharedBitmapManager* shared_bitmap_manager, | 546 SharedBitmapManager* shared_bitmap_manager, |
337 int highp_threshold_min, | 547 int highp_threshold_min, |
338 bool use_rgba_4444_texture_format, | 548 bool use_rgba_4444_texture_format, |
339 size_t id_allocation_chunk_size) { | 549 size_t id_allocation_chunk_size) { |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it, | 787 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it, |
578 DeleteStyle style) { | 788 DeleteStyle style) { |
579 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal"); | 789 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal"); |
580 Resource* resource = &it->second; | 790 Resource* resource = &it->second; |
581 bool lost_resource = resource->lost; | 791 bool lost_resource = resource->lost; |
582 | 792 |
583 DCHECK(resource->exported_count == 0 || style != Normal); | 793 DCHECK(resource->exported_count == 0 || style != Normal); |
584 if (style == ForShutdown && resource->exported_count > 0) | 794 if (style == ForShutdown && resource->exported_count > 0) |
585 lost_resource = true; | 795 lost_resource = true; |
586 | 796 |
| 797 resource->direct_raster_buffer.reset(); |
| 798 resource->image_raster_buffer.reset(); |
| 799 resource->pixel_raster_buffer.reset(); |
| 800 |
587 if (resource->image_id) { | 801 if (resource->image_id) { |
588 DCHECK(resource->origin == Resource::Internal); | 802 DCHECK(resource->origin == Resource::Internal); |
589 GLES2Interface* gl = ContextGL(); | 803 GLES2Interface* gl = ContextGL(); |
590 DCHECK(gl); | 804 DCHECK(gl); |
591 GLC(gl, gl->DestroyImageCHROMIUM(resource->image_id)); | 805 GLC(gl, gl->DestroyImageCHROMIUM(resource->image_id)); |
592 } | 806 } |
593 | 807 |
594 if (resource->gl_upload_query_id) { | 808 if (resource->gl_upload_query_id) { |
595 DCHECK(resource->origin == Resource::Internal); | 809 DCHECK(resource->origin == Resource::Internal); |
596 GLES2Interface* gl = ContextGL(); | 810 GLES2Interface* gl = ContextGL(); |
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1464 if (!to_return.empty()) | 1678 if (!to_return.empty()) |
1465 child_info->return_callback.Run(to_return); | 1679 child_info->return_callback.Run(to_return); |
1466 | 1680 |
1467 if (child_info->marked_for_deletion && | 1681 if (child_info->marked_for_deletion && |
1468 child_info->parent_to_child_map.empty()) { | 1682 child_info->parent_to_child_map.empty()) { |
1469 DCHECK(child_info->child_to_parent_map.empty()); | 1683 DCHECK(child_info->child_to_parent_map.empty()); |
1470 children_.erase(child_it); | 1684 children_.erase(child_it); |
1471 } | 1685 } |
1472 } | 1686 } |
1473 | 1687 |
1474 void ResourceProvider::AcquirePixelBuffer(ResourceId id) { | 1688 SkCanvas* ResourceProvider::MapDirectRasterBuffer(ResourceId id) { |
| 1689 // Resource needs to be locked for write since DirectRasterBuffer writes |
| 1690 // directly to it. |
| 1691 LockForWrite(id); |
1475 Resource* resource = GetResource(id); | 1692 Resource* resource = GetResource(id); |
| 1693 if (!resource->direct_raster_buffer.get()) { |
| 1694 resource->direct_raster_buffer.reset( |
| 1695 new DirectRasterBuffer(resource, this)); |
| 1696 } |
| 1697 return resource->direct_raster_buffer->LockForWrite(); |
| 1698 } |
| 1699 |
| 1700 void ResourceProvider::UnmapDirectRasterBuffer(ResourceId id) { |
| 1701 Resource* resource = GetResource(id); |
| 1702 DCHECK(resource->direct_raster_buffer.get()); |
| 1703 resource->direct_raster_buffer->UnlockForWrite(); |
| 1704 UnlockForWrite(id); |
| 1705 } |
| 1706 |
| 1707 SkCanvas* ResourceProvider::MapImageRasterBuffer(ResourceId id) { |
| 1708 Resource* resource = GetResource(id); |
| 1709 AcquireImage(resource); |
| 1710 if (!resource->image_raster_buffer.get()) |
| 1711 resource->image_raster_buffer.reset(new ImageRasterBuffer(resource, this)); |
| 1712 return resource->image_raster_buffer->LockForWrite(); |
| 1713 } |
| 1714 |
| 1715 void ResourceProvider::UnmapImageRasterBuffer(ResourceId id) { |
| 1716 Resource* resource = GetResource(id); |
| 1717 resource->image_raster_buffer->UnlockForWrite(); |
| 1718 resource->dirty_image = true; |
| 1719 } |
| 1720 |
| 1721 void ResourceProvider::AcquirePixelRasterBuffer(ResourceId id) { |
| 1722 Resource* resource = GetResource(id); |
| 1723 AcquirePixelBuffer(resource); |
| 1724 resource->pixel_raster_buffer.reset(new PixelRasterBuffer(resource, this)); |
| 1725 } |
| 1726 |
| 1727 void ResourceProvider::ReleasePixelRasterBuffer(ResourceId id) { |
| 1728 Resource* resource = GetResource(id); |
| 1729 resource->pixel_raster_buffer.reset(); |
| 1730 ReleasePixelBuffer(resource); |
| 1731 } |
| 1732 |
| 1733 SkCanvas* ResourceProvider::MapPixelRasterBuffer(ResourceId id) { |
| 1734 Resource* resource = GetResource(id); |
| 1735 DCHECK(resource->pixel_raster_buffer.get()); |
| 1736 return resource->pixel_raster_buffer->LockForWrite(); |
| 1737 } |
| 1738 |
| 1739 void ResourceProvider::UnmapPixelRasterBuffer(ResourceId id) { |
| 1740 Resource* resource = GetResource(id); |
| 1741 DCHECK(resource->pixel_raster_buffer.get()); |
| 1742 resource->pixel_raster_buffer->UnlockForWrite(); |
| 1743 } |
| 1744 |
| 1745 void ResourceProvider::AcquirePixelBuffer(Resource* resource) { |
1476 DCHECK(resource->origin == Resource::Internal); | 1746 DCHECK(resource->origin == Resource::Internal); |
1477 DCHECK_EQ(resource->exported_count, 0); | 1747 DCHECK_EQ(resource->exported_count, 0); |
1478 DCHECK(!resource->image_id); | 1748 DCHECK(!resource->image_id); |
1479 DCHECK_NE(ETC1, resource->format); | 1749 DCHECK_NE(ETC1, resource->format); |
1480 | 1750 |
1481 if (resource->type == GLTexture) { | 1751 if (resource->type == GLTexture) { |
1482 GLES2Interface* gl = ContextGL(); | 1752 GLES2Interface* gl = ContextGL(); |
1483 DCHECK(gl); | 1753 DCHECK(gl); |
1484 if (!resource->gl_pixel_buffer_id) | 1754 if (!resource->gl_pixel_buffer_id) |
1485 resource->gl_pixel_buffer_id = buffer_id_allocator_->NextId(); | 1755 resource->gl_pixel_buffer_id = buffer_id_allocator_->NextId(); |
1486 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1756 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1487 resource->gl_pixel_buffer_id); | 1757 resource->gl_pixel_buffer_id); |
1488 unsigned bytes_per_pixel = BitsPerPixel(resource->format) / 8; | 1758 unsigned bytes_per_pixel = BitsPerPixel(resource->format) / 8; |
1489 gl->BufferData(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1759 gl->BufferData(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1490 resource->size.height() * | 1760 resource->size.height() * |
1491 RoundUp(bytes_per_pixel * resource->size.width(), 4u), | 1761 RoundUp(bytes_per_pixel * resource->size.width(), 4u), |
1492 NULL, | 1762 NULL, |
1493 GL_DYNAMIC_DRAW); | 1763 GL_DYNAMIC_DRAW); |
1494 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1764 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1495 } else { | 1765 } else { |
1496 DCHECK_EQ(Bitmap, resource->type); | 1766 DCHECK_EQ(Bitmap, resource->type); |
1497 if (resource->pixel_buffer) | 1767 if (resource->pixel_buffer) |
1498 return; | 1768 return; |
1499 | 1769 |
1500 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; | 1770 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; |
1501 } | 1771 } |
1502 } | 1772 } |
1503 | 1773 |
1504 void ResourceProvider::ReleasePixelBuffer(ResourceId id) { | 1774 void ResourceProvider::ReleasePixelBuffer(Resource* resource) { |
1505 Resource* resource = GetResource(id); | |
1506 DCHECK(resource->origin == Resource::Internal); | 1775 DCHECK(resource->origin == Resource::Internal); |
1507 DCHECK_EQ(resource->exported_count, 0); | 1776 DCHECK_EQ(resource->exported_count, 0); |
1508 DCHECK(!resource->image_id); | 1777 DCHECK(!resource->image_id); |
1509 | 1778 |
1510 // The pixel buffer can be released while there is a pending "set pixels" | 1779 // The pixel buffer can be released while there is a pending "set pixels" |
1511 // if completion has been forced. Any shared memory associated with this | 1780 // if completion has been forced. Any shared memory associated with this |
1512 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM | 1781 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM |
1513 // command has been processed on the service side. It is also safe to | 1782 // command has been processed on the service side. It is also safe to |
1514 // reuse any query id associated with this resource before they complete | 1783 // reuse any query id associated with this resource before they complete |
1515 // as each new query has a unique submit count. | 1784 // as each new query has a unique submit count. |
1516 if (resource->pending_set_pixels) { | 1785 if (resource->pending_set_pixels) { |
1517 DCHECK(resource->set_pixels_completion_forced); | 1786 DCHECK(resource->set_pixels_completion_forced); |
1518 resource->pending_set_pixels = false; | 1787 resource->pending_set_pixels = false; |
1519 UnlockForWrite(id); | 1788 resource->locked_for_write = false; |
1520 } | 1789 } |
1521 | 1790 |
1522 if (resource->type == GLTexture) { | 1791 if (resource->type == GLTexture) { |
1523 if (!resource->gl_pixel_buffer_id) | 1792 if (!resource->gl_pixel_buffer_id) |
1524 return; | 1793 return; |
1525 GLES2Interface* gl = ContextGL(); | 1794 GLES2Interface* gl = ContextGL(); |
1526 DCHECK(gl); | 1795 DCHECK(gl); |
1527 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1796 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1528 resource->gl_pixel_buffer_id); | 1797 resource->gl_pixel_buffer_id); |
1529 gl->BufferData( | 1798 gl->BufferData( |
1530 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0, NULL, GL_DYNAMIC_DRAW); | 1799 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0, NULL, GL_DYNAMIC_DRAW); |
1531 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1800 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1532 } else { | 1801 } else { |
1533 DCHECK_EQ(Bitmap, resource->type); | 1802 DCHECK_EQ(Bitmap, resource->type); |
1534 if (!resource->pixel_buffer) | 1803 if (!resource->pixel_buffer) |
1535 return; | 1804 return; |
1536 delete[] resource->pixel_buffer; | 1805 delete[] resource->pixel_buffer; |
1537 resource->pixel_buffer = NULL; | 1806 resource->pixel_buffer = NULL; |
1538 } | 1807 } |
1539 } | 1808 } |
1540 | 1809 |
1541 uint8_t* ResourceProvider::MapPixelBuffer(ResourceId id) { | 1810 uint8_t* ResourceProvider::MapPixelBuffer(const Resource* resource, |
1542 Resource* resource = GetResource(id); | 1811 int* stride) { |
1543 DCHECK(resource->origin == Resource::Internal); | 1812 DCHECK(resource->origin == Resource::Internal); |
1544 DCHECK_EQ(resource->exported_count, 0); | 1813 DCHECK_EQ(resource->exported_count, 0); |
1545 DCHECK(!resource->image_id); | 1814 DCHECK(!resource->image_id); |
1546 | 1815 |
| 1816 *stride = 0; |
1547 if (resource->type == GLTexture) { | 1817 if (resource->type == GLTexture) { |
1548 GLES2Interface* gl = ContextGL(); | 1818 GLES2Interface* gl = ContextGL(); |
1549 DCHECK(gl); | 1819 DCHECK(gl); |
1550 DCHECK(resource->gl_pixel_buffer_id); | 1820 DCHECK(resource->gl_pixel_buffer_id); |
1551 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1821 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1552 resource->gl_pixel_buffer_id); | 1822 resource->gl_pixel_buffer_id); |
1553 uint8_t* image = static_cast<uint8_t*>(gl->MapBufferCHROMIUM( | 1823 uint8_t* image = static_cast<uint8_t*>(gl->MapBufferCHROMIUM( |
1554 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY)); | 1824 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY)); |
1555 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1825 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1556 // Buffer is required to be 4-byte aligned. | 1826 // Buffer is required to be 4-byte aligned. |
1557 CHECK(!(reinterpret_cast<intptr_t>(image) & 3)); | 1827 CHECK(!(reinterpret_cast<intptr_t>(image) & 3)); |
1558 return image; | 1828 return image; |
1559 } | 1829 } |
1560 DCHECK_EQ(Bitmap, resource->type); | 1830 DCHECK_EQ(Bitmap, resource->type); |
1561 return resource->pixel_buffer; | 1831 return resource->pixel_buffer; |
1562 } | 1832 } |
1563 | 1833 |
1564 void ResourceProvider::UnmapPixelBuffer(ResourceId id) { | 1834 void ResourceProvider::UnmapPixelBuffer(const Resource* resource) { |
1565 Resource* resource = GetResource(id); | |
1566 DCHECK(resource->origin == Resource::Internal); | 1835 DCHECK(resource->origin == Resource::Internal); |
1567 DCHECK_EQ(resource->exported_count, 0); | 1836 DCHECK_EQ(resource->exported_count, 0); |
1568 DCHECK(!resource->image_id); | 1837 DCHECK(!resource->image_id); |
1569 | 1838 |
1570 if (resource->type == GLTexture) { | 1839 if (resource->type == GLTexture) { |
1571 GLES2Interface* gl = ContextGL(); | 1840 GLES2Interface* gl = ContextGL(); |
1572 DCHECK(gl); | 1841 DCHECK(gl); |
1573 DCHECK(resource->gl_pixel_buffer_id); | 1842 DCHECK(resource->gl_pixel_buffer_id); |
1574 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1843 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1575 resource->gl_pixel_buffer_id); | 1844 resource->gl_pixel_buffer_id); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1807 resource->bound_image_id = resource->image_id; | 2076 resource->bound_image_id = resource->image_id; |
1808 resource->dirty_image = false; | 2077 resource->dirty_image = false; |
1809 } | 2078 } |
1810 | 2079 |
1811 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id, | 2080 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id, |
1812 bool enable) { | 2081 bool enable) { |
1813 Resource* resource = GetResource(id); | 2082 Resource* resource = GetResource(id); |
1814 resource->enable_read_lock_fences = enable; | 2083 resource->enable_read_lock_fences = enable; |
1815 } | 2084 } |
1816 | 2085 |
1817 void ResourceProvider::AcquireImage(ResourceId id) { | 2086 void ResourceProvider::AcquireImage(Resource* resource) { |
1818 Resource* resource = GetResource(id); | |
1819 DCHECK(resource->origin == Resource::Internal); | 2087 DCHECK(resource->origin == Resource::Internal); |
1820 DCHECK_EQ(resource->exported_count, 0); | 2088 DCHECK_EQ(resource->exported_count, 0); |
1821 | 2089 |
1822 if (resource->type != GLTexture) | 2090 if (resource->type != GLTexture) |
1823 return; | 2091 return; |
1824 | 2092 |
1825 if (resource->image_id) | 2093 if (resource->image_id) |
1826 return; | 2094 return; |
1827 | 2095 |
1828 resource->allocated = true; | 2096 resource->allocated = true; |
1829 GLES2Interface* gl = ContextGL(); | 2097 GLES2Interface* gl = ContextGL(); |
1830 DCHECK(gl); | 2098 DCHECK(gl); |
1831 resource->image_id = | 2099 resource->image_id = |
1832 gl->CreateImageCHROMIUM(resource->size.width(), | 2100 gl->CreateImageCHROMIUM(resource->size.width(), |
1833 resource->size.height(), | 2101 resource->size.height(), |
1834 TextureToStorageFormat(resource->format)); | 2102 TextureToStorageFormat(resource->format)); |
1835 DCHECK(resource->image_id); | 2103 DCHECK(resource->image_id); |
1836 } | 2104 } |
1837 | 2105 |
1838 void ResourceProvider::ReleaseImage(ResourceId id) { | 2106 void ResourceProvider::ReleaseImage(Resource* resource) { |
1839 Resource* resource = GetResource(id); | |
1840 DCHECK(resource->origin == Resource::Internal); | 2107 DCHECK(resource->origin == Resource::Internal); |
1841 DCHECK_EQ(resource->exported_count, 0); | 2108 DCHECK_EQ(resource->exported_count, 0); |
1842 | 2109 |
1843 if (!resource->image_id) | 2110 if (!resource->image_id) |
1844 return; | 2111 return; |
1845 | 2112 |
1846 GLES2Interface* gl = ContextGL(); | 2113 GLES2Interface* gl = ContextGL(); |
1847 DCHECK(gl); | 2114 DCHECK(gl); |
1848 gl->DestroyImageCHROMIUM(resource->image_id); | 2115 gl->DestroyImageCHROMIUM(resource->image_id); |
1849 resource->image_id = 0; | 2116 resource->image_id = 0; |
1850 resource->bound_image_id = 0; | 2117 resource->bound_image_id = 0; |
1851 resource->dirty_image = false; | 2118 resource->dirty_image = false; |
1852 resource->allocated = false; | 2119 resource->allocated = false; |
1853 } | 2120 } |
1854 | 2121 |
1855 uint8_t* ResourceProvider::MapImage(ResourceId id) { | 2122 uint8_t* ResourceProvider::MapImage(const Resource* resource, int* stride) { |
1856 Resource* resource = GetResource(id); | |
1857 DCHECK(ReadLockFenceHasPassed(resource)); | 2123 DCHECK(ReadLockFenceHasPassed(resource)); |
1858 DCHECK(resource->origin == Resource::Internal); | 2124 DCHECK(resource->origin == Resource::Internal); |
1859 DCHECK_EQ(resource->exported_count, 0); | 2125 DCHECK_EQ(resource->exported_count, 0); |
1860 | 2126 |
1861 if (resource->type == GLTexture) { | 2127 if (resource->type == GLTexture) { |
1862 DCHECK(resource->image_id); | 2128 DCHECK(resource->image_id); |
1863 GLES2Interface* gl = ContextGL(); | 2129 GLES2Interface* gl = ContextGL(); |
1864 DCHECK(gl); | 2130 DCHECK(gl); |
| 2131 gl->GetImageParameterivCHROMIUM( |
| 2132 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, stride); |
1865 return static_cast<uint8_t*>( | 2133 return static_cast<uint8_t*>( |
1866 gl->MapImageCHROMIUM(resource->image_id, GL_READ_WRITE)); | 2134 gl->MapImageCHROMIUM(resource->image_id, GL_READ_WRITE)); |
1867 } | 2135 } |
1868 DCHECK_EQ(Bitmap, resource->type); | 2136 DCHECK_EQ(Bitmap, resource->type); |
| 2137 *stride = 0; |
1869 return resource->pixels; | 2138 return resource->pixels; |
1870 } | 2139 } |
1871 | 2140 |
1872 void ResourceProvider::UnmapImage(ResourceId id) { | 2141 void ResourceProvider::UnmapImage(const Resource* resource) { |
1873 Resource* resource = GetResource(id); | |
1874 DCHECK(resource->origin == Resource::Internal); | 2142 DCHECK(resource->origin == Resource::Internal); |
1875 DCHECK_EQ(resource->exported_count, 0); | 2143 DCHECK_EQ(resource->exported_count, 0); |
1876 | 2144 |
1877 if (resource->image_id) { | 2145 if (resource->image_id) { |
1878 GLES2Interface* gl = ContextGL(); | 2146 GLES2Interface* gl = ContextGL(); |
1879 DCHECK(gl); | 2147 DCHECK(gl); |
1880 gl->UnmapImageCHROMIUM(resource->image_id); | 2148 gl->UnmapImageCHROMIUM(resource->image_id); |
1881 resource->dirty_image = true; | |
1882 } | 2149 } |
1883 } | 2150 } |
1884 | 2151 |
1885 int ResourceProvider::GetImageStride(ResourceId id) { | |
1886 Resource* resource = GetResource(id); | |
1887 DCHECK(resource->origin == Resource::Internal); | |
1888 DCHECK_EQ(resource->exported_count, 0); | |
1889 | |
1890 int stride = 0; | |
1891 | |
1892 if (resource->image_id) { | |
1893 GLES2Interface* gl = ContextGL(); | |
1894 DCHECK(gl); | |
1895 gl->GetImageParameterivCHROMIUM( | |
1896 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride); | |
1897 } | |
1898 | |
1899 return stride; | |
1900 } | |
1901 | |
1902 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { | 2152 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { |
1903 GLint active_unit = 0; | 2153 GLint active_unit = 0; |
1904 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); | 2154 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); |
1905 return active_unit; | 2155 return active_unit; |
1906 } | 2156 } |
1907 | 2157 |
1908 GLES2Interface* ResourceProvider::ContextGL() const { | 2158 GLES2Interface* ResourceProvider::ContextGL() const { |
1909 ContextProvider* context_provider = output_surface_->context_provider(); | 2159 ContextProvider* context_provider = output_surface_->context_provider(); |
1910 return context_provider ? context_provider->ContextGL() : NULL; | 2160 return context_provider ? context_provider->ContextGL() : NULL; |
1911 } | 2161 } |
1912 | 2162 |
| 2163 class GrContext* ResourceProvider::GrContext() const { |
| 2164 ContextProvider* context_provider = output_surface_->context_provider(); |
| 2165 return context_provider ? context_provider->GrContext() : NULL; |
| 2166 } |
| 2167 |
1913 } // namespace cc | 2168 } // namespace cc |
OLD | NEW |