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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 wrap_mode(wrap_mode), | 315 wrap_mode(wrap_mode), |
275 lost(false), | 316 lost(false), |
276 hint(TextureUsageAny), | 317 hint(TextureUsageAny), |
277 type(Bitmap), | 318 type(Bitmap), |
278 format(RGBA_8888), | 319 format(RGBA_8888), |
279 shared_bitmap(bitmap) { | 320 shared_bitmap(bitmap) { |
280 DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); | 321 DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT); |
281 DCHECK(origin == Delegated || pixels); | 322 DCHECK(origin == Delegated || pixels); |
282 } | 323 } |
283 | 324 |
| 325 ResourceProvider::RasterBuffer::RasterBuffer( |
| 326 const Resource* resource, |
| 327 ResourceProvider* resource_provider) |
| 328 : resource_(resource), |
| 329 resource_provider_(resource_provider), |
| 330 locked_canvas_(NULL), |
| 331 canvas_save_count_(0) { |
| 332 DCHECK(resource_); |
| 333 DCHECK(resource_provider_); |
| 334 } |
| 335 |
| 336 ResourceProvider::RasterBuffer::~RasterBuffer() {} |
| 337 |
| 338 SkCanvas* ResourceProvider::RasterBuffer::LockForWrite() { |
| 339 DCHECK(!locked_canvas_); |
| 340 |
| 341 locked_canvas_ = DoLockForWrite(); |
| 342 canvas_save_count_ = locked_canvas_ ? locked_canvas_->save() : 0; |
| 343 return locked_canvas_; |
| 344 } |
| 345 |
| 346 void ResourceProvider::RasterBuffer::UnlockForWrite() { |
| 347 if (locked_canvas_) { |
| 348 locked_canvas_->restoreToCount(canvas_save_count_); |
| 349 locked_canvas_ = NULL; |
| 350 } |
| 351 DoUnlockForWrite(); |
| 352 } |
| 353 |
| 354 ResourceProvider::DirectRasterBuffer::DirectRasterBuffer( |
| 355 const Resource* resource, |
| 356 ResourceProvider* resource_provider) |
| 357 : RasterBuffer(resource, resource_provider) {} |
| 358 |
| 359 ResourceProvider::DirectRasterBuffer::~DirectRasterBuffer() {} |
| 360 |
| 361 SkCanvas* ResourceProvider::DirectRasterBuffer::DoLockForWrite() { |
| 362 if (!surface_) |
| 363 surface_ = CreateSurface(); |
| 364 return surface_ ? surface_->getCanvas() : NULL; |
| 365 } |
| 366 |
| 367 void ResourceProvider::DirectRasterBuffer::DoUnlockForWrite() {} |
| 368 |
| 369 skia::RefPtr<SkSurface> ResourceProvider::DirectRasterBuffer::CreateSurface() { |
| 370 skia::RefPtr<SkSurface> surface; |
| 371 switch (resource()->type) { |
| 372 case GLTexture: { |
| 373 DCHECK(resource()->gl_id); |
| 374 class GrContext* gr_context = resource_provider()->GrContext(); |
| 375 if (gr_context) { |
| 376 GrBackendTextureDesc desc; |
| 377 desc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 378 desc.fWidth = resource()->size.width(); |
| 379 desc.fHeight = resource()->size.height(); |
| 380 desc.fConfig = ToGrPixelConfig(resource()->format); |
| 381 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 382 desc.fTextureHandle = resource()->gl_id; |
| 383 skia::RefPtr<GrTexture> gr_texture = |
| 384 skia::AdoptRef(gr_context->wrapBackendTexture(desc)); |
| 385 surface = skia::AdoptRef( |
| 386 SkSurface::NewRenderTargetDirect(gr_texture->asRenderTarget())); |
| 387 } |
| 388 break; |
| 389 } |
| 390 case Bitmap: { |
| 391 DCHECK(resource()->pixels); |
| 392 DCHECK_EQ(RGBA_8888, resource()->format); |
| 393 SkImageInfo image_info = SkImageInfo::MakeN32Premul( |
| 394 resource()->size.width(), resource()->size.height()); |
| 395 size_t row_bytes = SkBitmap::ComputeRowBytes(SkBitmap::kARGB_8888_Config, |
| 396 resource()->size.width()); |
| 397 surface = skia::AdoptRef(SkSurface::NewRasterDirect( |
| 398 image_info, resource()->pixels, row_bytes)); |
| 399 break; |
| 400 } |
| 401 default: |
| 402 NOTREACHED(); |
| 403 } |
| 404 return surface; |
| 405 } |
| 406 |
| 407 ResourceProvider::BitmapRasterBuffer::BitmapRasterBuffer( |
| 408 const Resource* resource, |
| 409 ResourceProvider* resource_provider) |
| 410 : RasterBuffer(resource, resource_provider), mapped_buffer_(NULL) {} |
| 411 |
| 412 ResourceProvider::BitmapRasterBuffer::~BitmapRasterBuffer() {} |
| 413 |
| 414 SkCanvas* ResourceProvider::BitmapRasterBuffer::DoLockForWrite() { |
| 415 DCHECK(!mapped_buffer_); |
| 416 DCHECK(!raster_canvas_); |
| 417 |
| 418 int stride = 0; |
| 419 mapped_buffer_ = MapBuffer(&stride); |
| 420 if (!mapped_buffer_) |
| 421 return NULL; |
| 422 |
| 423 switch (resource()->format) { |
| 424 case RGBA_4444: |
| 425 // Use the default stride if we will eventually convert this |
| 426 // bitmap to 4444. |
| 427 raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 428 resource()->size.width(), |
| 429 resource()->size.height()); |
| 430 raster_bitmap_.allocPixels(); |
| 431 break; |
| 432 case RGBA_8888: |
| 433 case BGRA_8888: |
| 434 raster_bitmap_.setConfig(SkBitmap::kARGB_8888_Config, |
| 435 resource()->size.width(), |
| 436 resource()->size.height(), |
| 437 stride); |
| 438 raster_bitmap_.setPixels(mapped_buffer_); |
| 439 break; |
| 440 case LUMINANCE_8: |
| 441 case RGB_565: |
| 442 case ETC1: |
| 443 NOTREACHED(); |
| 444 break; |
| 445 } |
| 446 skia::RefPtr<SkBitmapDevice> device = |
| 447 skia::AdoptRef(new SkBitmapDevice(raster_bitmap_)); |
| 448 raster_canvas_ = skia::AdoptRef(new SkCanvas(device.get())); |
| 449 return raster_canvas_.get(); |
| 450 } |
| 451 |
| 452 void ResourceProvider::BitmapRasterBuffer::DoUnlockForWrite() { |
| 453 raster_canvas_.clear(); |
| 454 |
| 455 SkBitmap::Config buffer_config = SkBitmapConfig(resource()->format); |
| 456 if (mapped_buffer_ && (buffer_config != raster_bitmap_.config())) |
| 457 CopyBitmap(raster_bitmap_, mapped_buffer_, buffer_config); |
| 458 raster_bitmap_.reset(); |
| 459 |
| 460 UnmapBuffer(); |
| 461 mapped_buffer_ = NULL; |
| 462 } |
| 463 |
| 464 ResourceProvider::ImageRasterBuffer::ImageRasterBuffer( |
| 465 const Resource* resource, |
| 466 ResourceProvider* resource_provider) |
| 467 : BitmapRasterBuffer(resource, resource_provider) {} |
| 468 |
| 469 ResourceProvider::ImageRasterBuffer::~ImageRasterBuffer() {} |
| 470 |
| 471 uint8_t* ResourceProvider::ImageRasterBuffer::MapBuffer(int* stride) { |
| 472 return resource_provider()->MapImage(resource(), stride); |
| 473 } |
| 474 |
| 475 void ResourceProvider::ImageRasterBuffer::UnmapBuffer() { |
| 476 resource_provider()->UnmapImage(resource()); |
| 477 } |
| 478 |
| 479 ResourceProvider::PixelRasterBuffer::PixelRasterBuffer( |
| 480 const Resource* resource, |
| 481 ResourceProvider* resource_provider) |
| 482 : BitmapRasterBuffer(resource, resource_provider) {} |
| 483 |
| 484 ResourceProvider::PixelRasterBuffer::~PixelRasterBuffer() {} |
| 485 |
| 486 uint8_t* ResourceProvider::PixelRasterBuffer::MapBuffer(int* stride) { |
| 487 return resource_provider()->MapPixelBuffer(resource(), stride); |
| 488 } |
| 489 |
| 490 void ResourceProvider::PixelRasterBuffer::UnmapBuffer() { |
| 491 resource_provider()->UnmapPixelBuffer(resource()); |
| 492 } |
| 493 |
284 ResourceProvider::Child::Child() : marked_for_deletion(false) {} | 494 ResourceProvider::Child::Child() : marked_for_deletion(false) {} |
285 | 495 |
286 ResourceProvider::Child::~Child() {} | 496 ResourceProvider::Child::~Child() {} |
287 | 497 |
288 scoped_ptr<ResourceProvider> ResourceProvider::Create( | 498 scoped_ptr<ResourceProvider> ResourceProvider::Create( |
289 OutputSurface* output_surface, | 499 OutputSurface* output_surface, |
290 SharedBitmapManager* shared_bitmap_manager, | 500 SharedBitmapManager* shared_bitmap_manager, |
291 int highp_threshold_min, | 501 int highp_threshold_min, |
292 bool use_rgba_4444_texture_format, | 502 bool use_rgba_4444_texture_format, |
293 size_t id_allocation_chunk_size) { | 503 size_t id_allocation_chunk_size) { |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it, | 741 void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it, |
532 DeleteStyle style) { | 742 DeleteStyle style) { |
533 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal"); | 743 TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal"); |
534 Resource* resource = &it->second; | 744 Resource* resource = &it->second; |
535 bool lost_resource = resource->lost; | 745 bool lost_resource = resource->lost; |
536 | 746 |
537 DCHECK(resource->exported_count == 0 || style != Normal); | 747 DCHECK(resource->exported_count == 0 || style != Normal); |
538 if (style == ForShutdown && resource->exported_count > 0) | 748 if (style == ForShutdown && resource->exported_count > 0) |
539 lost_resource = true; | 749 lost_resource = true; |
540 | 750 |
| 751 resource->direct_raster_buffer.reset(); |
| 752 resource->image_raster_buffer.reset(); |
| 753 resource->pixel_raster_buffer.reset(); |
| 754 |
541 if (resource->image_id) { | 755 if (resource->image_id) { |
542 DCHECK(resource->origin == Resource::Internal); | 756 DCHECK(resource->origin == Resource::Internal); |
543 GLES2Interface* gl = ContextGL(); | 757 GLES2Interface* gl = ContextGL(); |
544 DCHECK(gl); | 758 DCHECK(gl); |
545 GLC(gl, gl->DestroyImageCHROMIUM(resource->image_id)); | 759 GLC(gl, gl->DestroyImageCHROMIUM(resource->image_id)); |
546 } | 760 } |
547 | 761 |
548 if (resource->gl_upload_query_id) { | 762 if (resource->gl_upload_query_id) { |
549 DCHECK(resource->origin == Resource::Internal); | 763 DCHECK(resource->origin == Resource::Internal); |
550 GLES2Interface* gl = ContextGL(); | 764 GLES2Interface* gl = ContextGL(); |
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 if (!to_return.empty()) | 1632 if (!to_return.empty()) |
1419 child_info->return_callback.Run(to_return); | 1633 child_info->return_callback.Run(to_return); |
1420 | 1634 |
1421 if (child_info->marked_for_deletion && | 1635 if (child_info->marked_for_deletion && |
1422 child_info->parent_to_child_map.empty()) { | 1636 child_info->parent_to_child_map.empty()) { |
1423 DCHECK(child_info->child_to_parent_map.empty()); | 1637 DCHECK(child_info->child_to_parent_map.empty()); |
1424 children_.erase(child_it); | 1638 children_.erase(child_it); |
1425 } | 1639 } |
1426 } | 1640 } |
1427 | 1641 |
1428 void ResourceProvider::AcquirePixelBuffer(ResourceId id) { | 1642 SkCanvas* ResourceProvider::MapDirectRasterBuffer(ResourceId id) { |
| 1643 // Resource needs to be locked for write since DirectRasterBuffer writes |
| 1644 // directly to it. |
| 1645 LockForWrite(id); |
1429 Resource* resource = GetResource(id); | 1646 Resource* resource = GetResource(id); |
| 1647 if (!resource->direct_raster_buffer.get()) { |
| 1648 resource->direct_raster_buffer.reset( |
| 1649 new DirectRasterBuffer(resource, this)); |
| 1650 } |
| 1651 return resource->direct_raster_buffer->LockForWrite(); |
| 1652 } |
| 1653 |
| 1654 void ResourceProvider::UnmapDirectRasterBuffer(ResourceId id) { |
| 1655 Resource* resource = GetResource(id); |
| 1656 DCHECK(resource->direct_raster_buffer.get()); |
| 1657 resource->direct_raster_buffer->UnlockForWrite(); |
| 1658 UnlockForWrite(id); |
| 1659 } |
| 1660 |
| 1661 SkCanvas* ResourceProvider::MapImageRasterBuffer(ResourceId id) { |
| 1662 Resource* resource = GetResource(id); |
| 1663 AcquireImage(resource); |
| 1664 if (!resource->image_raster_buffer.get()) |
| 1665 resource->image_raster_buffer.reset(new ImageRasterBuffer(resource, this)); |
| 1666 return resource->image_raster_buffer->LockForWrite(); |
| 1667 } |
| 1668 |
| 1669 void ResourceProvider::UnmapImageRasterBuffer(ResourceId id) { |
| 1670 Resource* resource = GetResource(id); |
| 1671 resource->image_raster_buffer->UnlockForWrite(); |
| 1672 resource->dirty_image = true; |
| 1673 } |
| 1674 |
| 1675 void ResourceProvider::AcquirePixelRasterBuffer(ResourceId id) { |
| 1676 Resource* resource = GetResource(id); |
| 1677 AcquirePixelBuffer(resource); |
| 1678 resource->pixel_raster_buffer.reset(new PixelRasterBuffer(resource, this)); |
| 1679 } |
| 1680 |
| 1681 void ResourceProvider::ReleasePixelRasterBuffer(ResourceId id) { |
| 1682 Resource* resource = GetResource(id); |
| 1683 resource->pixel_raster_buffer.reset(); |
| 1684 ReleasePixelBuffer(resource); |
| 1685 } |
| 1686 |
| 1687 SkCanvas* ResourceProvider::MapPixelRasterBuffer(ResourceId id) { |
| 1688 Resource* resource = GetResource(id); |
| 1689 DCHECK(resource->pixel_raster_buffer.get()); |
| 1690 return resource->pixel_raster_buffer->LockForWrite(); |
| 1691 } |
| 1692 |
| 1693 void ResourceProvider::UnmapPixelRasterBuffer(ResourceId id) { |
| 1694 Resource* resource = GetResource(id); |
| 1695 DCHECK(resource->pixel_raster_buffer.get()); |
| 1696 resource->pixel_raster_buffer->UnlockForWrite(); |
| 1697 } |
| 1698 |
| 1699 void ResourceProvider::AcquirePixelBuffer(Resource* resource) { |
1430 DCHECK(resource->origin == Resource::Internal); | 1700 DCHECK(resource->origin == Resource::Internal); |
1431 DCHECK_EQ(resource->exported_count, 0); | 1701 DCHECK_EQ(resource->exported_count, 0); |
1432 DCHECK(!resource->image_id); | 1702 DCHECK(!resource->image_id); |
1433 DCHECK_NE(ETC1, resource->format); | 1703 DCHECK_NE(ETC1, resource->format); |
1434 | 1704 |
1435 if (resource->type == GLTexture) { | 1705 if (resource->type == GLTexture) { |
1436 GLES2Interface* gl = ContextGL(); | 1706 GLES2Interface* gl = ContextGL(); |
1437 DCHECK(gl); | 1707 DCHECK(gl); |
1438 if (!resource->gl_pixel_buffer_id) | 1708 if (!resource->gl_pixel_buffer_id) |
1439 resource->gl_pixel_buffer_id = buffer_id_allocator_->NextId(); | 1709 resource->gl_pixel_buffer_id = buffer_id_allocator_->NextId(); |
1440 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1710 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1441 resource->gl_pixel_buffer_id); | 1711 resource->gl_pixel_buffer_id); |
1442 unsigned bytes_per_pixel = BitsPerPixel(resource->format) / 8; | 1712 unsigned bytes_per_pixel = BitsPerPixel(resource->format) / 8; |
1443 gl->BufferData(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1713 gl->BufferData(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1444 resource->size.height() * | 1714 resource->size.height() * |
1445 RoundUp(bytes_per_pixel * resource->size.width(), 4u), | 1715 RoundUp(bytes_per_pixel * resource->size.width(), 4u), |
1446 NULL, | 1716 NULL, |
1447 GL_DYNAMIC_DRAW); | 1717 GL_DYNAMIC_DRAW); |
1448 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1718 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1449 } else { | 1719 } else { |
1450 DCHECK_EQ(Bitmap, resource->type); | 1720 DCHECK_EQ(Bitmap, resource->type); |
1451 if (resource->pixel_buffer) | 1721 if (resource->pixel_buffer) |
1452 return; | 1722 return; |
1453 | 1723 |
1454 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; | 1724 resource->pixel_buffer = new uint8_t[4 * resource->size.GetArea()]; |
1455 } | 1725 } |
1456 } | 1726 } |
1457 | 1727 |
1458 void ResourceProvider::ReleasePixelBuffer(ResourceId id) { | 1728 void ResourceProvider::ReleasePixelBuffer(Resource* resource) { |
1459 Resource* resource = GetResource(id); | |
1460 DCHECK(resource->origin == Resource::Internal); | 1729 DCHECK(resource->origin == Resource::Internal); |
1461 DCHECK_EQ(resource->exported_count, 0); | 1730 DCHECK_EQ(resource->exported_count, 0); |
1462 DCHECK(!resource->image_id); | 1731 DCHECK(!resource->image_id); |
1463 | 1732 |
1464 // The pixel buffer can be released while there is a pending "set pixels" | 1733 // The pixel buffer can be released while there is a pending "set pixels" |
1465 // if completion has been forced. Any shared memory associated with this | 1734 // if completion has been forced. Any shared memory associated with this |
1466 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM | 1735 // pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM |
1467 // command has been processed on the service side. It is also safe to | 1736 // command has been processed on the service side. It is also safe to |
1468 // reuse any query id associated with this resource before they complete | 1737 // reuse any query id associated with this resource before they complete |
1469 // as each new query has a unique submit count. | 1738 // as each new query has a unique submit count. |
1470 if (resource->pending_set_pixels) { | 1739 if (resource->pending_set_pixels) { |
1471 DCHECK(resource->set_pixels_completion_forced); | 1740 DCHECK(resource->set_pixels_completion_forced); |
1472 resource->pending_set_pixels = false; | 1741 resource->pending_set_pixels = false; |
1473 UnlockForWrite(id); | 1742 resource->locked_for_write = false; |
1474 } | 1743 } |
1475 | 1744 |
1476 if (resource->type == GLTexture) { | 1745 if (resource->type == GLTexture) { |
1477 if (!resource->gl_pixel_buffer_id) | 1746 if (!resource->gl_pixel_buffer_id) |
1478 return; | 1747 return; |
1479 GLES2Interface* gl = ContextGL(); | 1748 GLES2Interface* gl = ContextGL(); |
1480 DCHECK(gl); | 1749 DCHECK(gl); |
1481 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1750 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1482 resource->gl_pixel_buffer_id); | 1751 resource->gl_pixel_buffer_id); |
1483 gl->BufferData( | 1752 gl->BufferData( |
1484 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0, NULL, GL_DYNAMIC_DRAW); | 1753 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0, NULL, GL_DYNAMIC_DRAW); |
1485 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1754 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1486 } else { | 1755 } else { |
1487 DCHECK_EQ(Bitmap, resource->type); | 1756 DCHECK_EQ(Bitmap, resource->type); |
1488 if (!resource->pixel_buffer) | 1757 if (!resource->pixel_buffer) |
1489 return; | 1758 return; |
1490 delete[] resource->pixel_buffer; | 1759 delete[] resource->pixel_buffer; |
1491 resource->pixel_buffer = NULL; | 1760 resource->pixel_buffer = NULL; |
1492 } | 1761 } |
1493 } | 1762 } |
1494 | 1763 |
1495 uint8_t* ResourceProvider::MapPixelBuffer(ResourceId id) { | 1764 uint8_t* ResourceProvider::MapPixelBuffer(const Resource* resource, |
1496 Resource* resource = GetResource(id); | 1765 int* stride) { |
1497 DCHECK(resource->origin == Resource::Internal); | 1766 DCHECK(resource->origin == Resource::Internal); |
1498 DCHECK_EQ(resource->exported_count, 0); | 1767 DCHECK_EQ(resource->exported_count, 0); |
1499 DCHECK(!resource->image_id); | 1768 DCHECK(!resource->image_id); |
1500 | 1769 |
| 1770 *stride = 0; |
1501 if (resource->type == GLTexture) { | 1771 if (resource->type == GLTexture) { |
1502 GLES2Interface* gl = ContextGL(); | 1772 GLES2Interface* gl = ContextGL(); |
1503 DCHECK(gl); | 1773 DCHECK(gl); |
1504 DCHECK(resource->gl_pixel_buffer_id); | 1774 DCHECK(resource->gl_pixel_buffer_id); |
1505 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1775 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1506 resource->gl_pixel_buffer_id); | 1776 resource->gl_pixel_buffer_id); |
1507 uint8_t* image = static_cast<uint8_t*>(gl->MapBufferCHROMIUM( | 1777 uint8_t* image = static_cast<uint8_t*>(gl->MapBufferCHROMIUM( |
1508 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY)); | 1778 GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY)); |
1509 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); | 1779 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0); |
1510 // Buffer is required to be 4-byte aligned. | 1780 // Buffer is required to be 4-byte aligned. |
1511 CHECK(!(reinterpret_cast<intptr_t>(image) & 3)); | 1781 CHECK(!(reinterpret_cast<intptr_t>(image) & 3)); |
1512 return image; | 1782 return image; |
1513 } | 1783 } |
1514 DCHECK_EQ(Bitmap, resource->type); | 1784 DCHECK_EQ(Bitmap, resource->type); |
1515 return resource->pixel_buffer; | 1785 return resource->pixel_buffer; |
1516 } | 1786 } |
1517 | 1787 |
1518 void ResourceProvider::UnmapPixelBuffer(ResourceId id) { | 1788 void ResourceProvider::UnmapPixelBuffer(const Resource* resource) { |
1519 Resource* resource = GetResource(id); | |
1520 DCHECK(resource->origin == Resource::Internal); | 1789 DCHECK(resource->origin == Resource::Internal); |
1521 DCHECK_EQ(resource->exported_count, 0); | 1790 DCHECK_EQ(resource->exported_count, 0); |
1522 DCHECK(!resource->image_id); | 1791 DCHECK(!resource->image_id); |
1523 | 1792 |
1524 if (resource->type == GLTexture) { | 1793 if (resource->type == GLTexture) { |
1525 GLES2Interface* gl = ContextGL(); | 1794 GLES2Interface* gl = ContextGL(); |
1526 DCHECK(gl); | 1795 DCHECK(gl); |
1527 DCHECK(resource->gl_pixel_buffer_id); | 1796 DCHECK(resource->gl_pixel_buffer_id); |
1528 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, | 1797 gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, |
1529 resource->gl_pixel_buffer_id); | 1798 resource->gl_pixel_buffer_id); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1761 resource->bound_image_id = resource->image_id; | 2030 resource->bound_image_id = resource->image_id; |
1762 resource->dirty_image = false; | 2031 resource->dirty_image = false; |
1763 } | 2032 } |
1764 | 2033 |
1765 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id, | 2034 void ResourceProvider::EnableReadLockFences(ResourceProvider::ResourceId id, |
1766 bool enable) { | 2035 bool enable) { |
1767 Resource* resource = GetResource(id); | 2036 Resource* resource = GetResource(id); |
1768 resource->enable_read_lock_fences = enable; | 2037 resource->enable_read_lock_fences = enable; |
1769 } | 2038 } |
1770 | 2039 |
1771 void ResourceProvider::AcquireImage(ResourceId id) { | 2040 void ResourceProvider::AcquireImage(Resource* resource) { |
1772 Resource* resource = GetResource(id); | |
1773 DCHECK(resource->origin == Resource::Internal); | 2041 DCHECK(resource->origin == Resource::Internal); |
1774 DCHECK_EQ(resource->exported_count, 0); | 2042 DCHECK_EQ(resource->exported_count, 0); |
1775 | 2043 |
1776 if (resource->type != GLTexture) | 2044 if (resource->type != GLTexture) |
1777 return; | 2045 return; |
1778 | 2046 |
1779 if (resource->image_id) | 2047 if (resource->image_id) |
1780 return; | 2048 return; |
1781 | 2049 |
1782 resource->allocated = true; | 2050 resource->allocated = true; |
1783 GLES2Interface* gl = ContextGL(); | 2051 GLES2Interface* gl = ContextGL(); |
1784 DCHECK(gl); | 2052 DCHECK(gl); |
1785 resource->image_id = | 2053 resource->image_id = |
1786 gl->CreateImageCHROMIUM(resource->size.width(), | 2054 gl->CreateImageCHROMIUM(resource->size.width(), |
1787 resource->size.height(), | 2055 resource->size.height(), |
1788 TextureToStorageFormat(resource->format)); | 2056 TextureToStorageFormat(resource->format)); |
1789 DCHECK(resource->image_id); | 2057 DCHECK(resource->image_id); |
1790 } | 2058 } |
1791 | 2059 |
1792 void ResourceProvider::ReleaseImage(ResourceId id) { | 2060 void ResourceProvider::ReleaseImage(Resource* resource) { |
1793 Resource* resource = GetResource(id); | |
1794 DCHECK(resource->origin == Resource::Internal); | 2061 DCHECK(resource->origin == Resource::Internal); |
1795 DCHECK_EQ(resource->exported_count, 0); | 2062 DCHECK_EQ(resource->exported_count, 0); |
1796 | 2063 |
1797 if (!resource->image_id) | 2064 if (!resource->image_id) |
1798 return; | 2065 return; |
1799 | 2066 |
1800 GLES2Interface* gl = ContextGL(); | 2067 GLES2Interface* gl = ContextGL(); |
1801 DCHECK(gl); | 2068 DCHECK(gl); |
1802 gl->DestroyImageCHROMIUM(resource->image_id); | 2069 gl->DestroyImageCHROMIUM(resource->image_id); |
1803 resource->image_id = 0; | 2070 resource->image_id = 0; |
1804 resource->bound_image_id = 0; | 2071 resource->bound_image_id = 0; |
1805 resource->dirty_image = false; | 2072 resource->dirty_image = false; |
1806 resource->allocated = false; | 2073 resource->allocated = false; |
1807 } | 2074 } |
1808 | 2075 |
1809 uint8_t* ResourceProvider::MapImage(ResourceId id) { | 2076 uint8_t* ResourceProvider::MapImage(const Resource* resource, int* stride) { |
1810 Resource* resource = GetResource(id); | |
1811 DCHECK(ReadLockFenceHasPassed(resource)); | 2077 DCHECK(ReadLockFenceHasPassed(resource)); |
1812 DCHECK(resource->origin == Resource::Internal); | 2078 DCHECK(resource->origin == Resource::Internal); |
1813 DCHECK_EQ(resource->exported_count, 0); | 2079 DCHECK_EQ(resource->exported_count, 0); |
1814 | 2080 |
1815 if (resource->type == GLTexture) { | 2081 if (resource->type == GLTexture) { |
1816 DCHECK(resource->image_id); | 2082 DCHECK(resource->image_id); |
1817 GLES2Interface* gl = ContextGL(); | 2083 GLES2Interface* gl = ContextGL(); |
1818 DCHECK(gl); | 2084 DCHECK(gl); |
| 2085 gl->GetImageParameterivCHROMIUM( |
| 2086 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, stride); |
1819 return static_cast<uint8_t*>( | 2087 return static_cast<uint8_t*>( |
1820 gl->MapImageCHROMIUM(resource->image_id, GL_READ_WRITE)); | 2088 gl->MapImageCHROMIUM(resource->image_id, GL_READ_WRITE)); |
1821 } | 2089 } |
1822 DCHECK_EQ(Bitmap, resource->type); | 2090 DCHECK_EQ(Bitmap, resource->type); |
| 2091 *stride = 0; |
1823 return resource->pixels; | 2092 return resource->pixels; |
1824 } | 2093 } |
1825 | 2094 |
1826 void ResourceProvider::UnmapImage(ResourceId id) { | 2095 void ResourceProvider::UnmapImage(const Resource* resource) { |
1827 Resource* resource = GetResource(id); | |
1828 DCHECK(resource->origin == Resource::Internal); | 2096 DCHECK(resource->origin == Resource::Internal); |
1829 DCHECK_EQ(resource->exported_count, 0); | 2097 DCHECK_EQ(resource->exported_count, 0); |
1830 | 2098 |
1831 if (resource->image_id) { | 2099 if (resource->image_id) { |
1832 GLES2Interface* gl = ContextGL(); | 2100 GLES2Interface* gl = ContextGL(); |
1833 DCHECK(gl); | 2101 DCHECK(gl); |
1834 gl->UnmapImageCHROMIUM(resource->image_id); | 2102 gl->UnmapImageCHROMIUM(resource->image_id); |
1835 resource->dirty_image = true; | |
1836 } | 2103 } |
1837 } | 2104 } |
1838 | 2105 |
1839 int ResourceProvider::GetImageStride(ResourceId id) { | |
1840 Resource* resource = GetResource(id); | |
1841 DCHECK(resource->origin == Resource::Internal); | |
1842 DCHECK_EQ(resource->exported_count, 0); | |
1843 | |
1844 int stride = 0; | |
1845 | |
1846 if (resource->image_id) { | |
1847 GLES2Interface* gl = ContextGL(); | |
1848 DCHECK(gl); | |
1849 gl->GetImageParameterivCHROMIUM( | |
1850 resource->image_id, GL_IMAGE_ROWBYTES_CHROMIUM, &stride); | |
1851 } | |
1852 | |
1853 return stride; | |
1854 } | |
1855 | |
1856 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { | 2106 GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) { |
1857 GLint active_unit = 0; | 2107 GLint active_unit = 0; |
1858 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); | 2108 gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit); |
1859 return active_unit; | 2109 return active_unit; |
1860 } | 2110 } |
1861 | 2111 |
1862 GLES2Interface* ResourceProvider::ContextGL() const { | 2112 GLES2Interface* ResourceProvider::ContextGL() const { |
1863 ContextProvider* context_provider = output_surface_->context_provider(); | 2113 ContextProvider* context_provider = output_surface_->context_provider(); |
1864 return context_provider ? context_provider->ContextGL() : NULL; | 2114 return context_provider ? context_provider->ContextGL() : NULL; |
1865 } | 2115 } |
1866 | 2116 |
| 2117 class GrContext* ResourceProvider::GrContext() const { |
| 2118 ContextProvider* context_provider = output_surface_->context_provider(); |
| 2119 return context_provider ? context_provider->GrContext() : NULL; |
| 2120 } |
| 2121 |
1867 } // namespace cc | 2122 } // namespace cc |
OLD | NEW |