prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/raster/zero_copy_raster_buffer_provider.h" |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "base/memory/ptr_util.h" |
| 13 | #include "base/strings/stringprintf.h" |
| 14 | #include "base/trace_event/trace_event.h" |
| 15 | #include "base/trace_event/trace_event_argument.h" |
| 16 | #include "cc/debug/traced_value.h" |
| 17 | #include "cc/resources/platform_color.h" |
| 18 | #include "cc/resources/resource.h" |
| 19 | #include "ui/gfx/buffer_format_util.h" |
| 20 | #include "ui/gfx/gpu_memory_buffer.h" |
| 21 | |
| 22 | namespace cc { |
| 23 | namespace { |
| 24 | |
| 25 | class RasterBufferImpl : public RasterBuffer { |
| 26 | public: |
| 27 | RasterBufferImpl(ResourceProvider* resource_provider, |
| 28 | const Resource* resource) |
| 29 | : lock_(resource_provider, resource->id()), resource_(resource) {} |
| 30 | |
| 31 | // Overridden from RasterBuffer: |
| 32 | void Playback( |
| 33 | const RasterSource* raster_source, |
| 34 | const gfx::Rect& raster_full_rect, |
| 35 | const gfx::Rect& raster_dirty_rect, |
| 36 | uint64_t new_content_id, |
vmpstr | 69c1208 | 2017-01-13 01:52:43 | [diff] [blame] | 37 | float scale, |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 38 | const RasterSource::PlaybackSettings& playback_settings) override { |
prashant.n | 60e135b0 | 2016-06-08 04:12:23 | [diff] [blame] | 39 | TRACE_EVENT0("cc", "ZeroCopyRasterBuffer::Playback"); |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 40 | gfx::GpuMemoryBuffer* buffer = lock_.GetGpuMemoryBuffer(); |
| 41 | if (!buffer) |
| 42 | return; |
| 43 | |
| 44 | DCHECK_EQ(1u, gfx::NumberOfPlanesForBufferFormat(buffer->GetFormat())); |
| 45 | bool rv = buffer->Map(); |
| 46 | DCHECK(rv); |
| 47 | DCHECK(buffer->memory(0)); |
| 48 | // RasterBufferProvider::PlaybackToMemory only supports unsigned strides. |
| 49 | DCHECK_GE(buffer->stride(0), 0); |
| 50 | |
ccameron | b7210d3 | 2016-11-30 11:02:40 | [diff] [blame] | 51 | sk_sp<SkColorSpace> raster_color_space = |
| 52 | raster_source->HasImpliedColorSpace() ? nullptr |
| 53 | : lock_.sk_color_space(); |
| 54 | |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 55 | // TODO(danakj): Implement partial raster with raster_dirty_rect. |
| 56 | RasterBufferProvider::PlaybackToMemory( |
| 57 | buffer->memory(0), resource_->format(), resource_->size(), |
| 58 | buffer->stride(0), raster_source, raster_full_rect, raster_full_rect, |
vmpstr | 69c1208 | 2017-01-13 01:52:43 | [diff] [blame] | 59 | scale, raster_color_space, playback_settings); |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 60 | buffer->Unmap(); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | ResourceProvider::ScopedWriteLockGpuMemoryBuffer lock_; |
| 65 | const Resource* resource_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
| 68 | }; |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | // static |
| 73 | std::unique_ptr<RasterBufferProvider> ZeroCopyRasterBufferProvider::Create( |
| 74 | ResourceProvider* resource_provider, |
| 75 | ResourceFormat preferred_tile_format) { |
| 76 | return base::WrapUnique<RasterBufferProvider>( |
| 77 | new ZeroCopyRasterBufferProvider(resource_provider, |
| 78 | preferred_tile_format)); |
| 79 | } |
| 80 | |
| 81 | ZeroCopyRasterBufferProvider::ZeroCopyRasterBufferProvider( |
| 82 | ResourceProvider* resource_provider, |
| 83 | ResourceFormat preferred_tile_format) |
| 84 | : resource_provider_(resource_provider), |
| 85 | preferred_tile_format_(preferred_tile_format) {} |
| 86 | |
| 87 | ZeroCopyRasterBufferProvider::~ZeroCopyRasterBufferProvider() {} |
| 88 | |
| 89 | std::unique_ptr<RasterBuffer> |
| 90 | ZeroCopyRasterBufferProvider::AcquireBufferForRaster( |
| 91 | const Resource* resource, |
| 92 | uint64_t resource_content_id, |
| 93 | uint64_t previous_content_id) { |
| 94 | return base::WrapUnique<RasterBuffer>( |
| 95 | new RasterBufferImpl(resource_provider_, resource)); |
| 96 | } |
| 97 | |
| 98 | void ZeroCopyRasterBufferProvider::ReleaseBufferForRaster( |
| 99 | std::unique_ptr<RasterBuffer> buffer) { |
| 100 | // Nothing to do here. RasterBufferImpl destructor cleans up after itself. |
| 101 | } |
| 102 | |
sunnyps | 12074d3d | 2016-06-21 01:57:48 | [diff] [blame] | 103 | void ZeroCopyRasterBufferProvider::OrderingBarrier() { |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 104 | // No need to sync resources as this provider does not use GL context. |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ResourceFormat ZeroCopyRasterBufferProvider::GetResourceFormat( |
| 108 | bool must_support_alpha) const { |
| 109 | if (resource_provider_->IsResourceFormatSupported(preferred_tile_format_) && |
| 110 | (DoesResourceFormatSupportAlpha(preferred_tile_format_) || |
| 111 | !must_support_alpha)) { |
| 112 | return preferred_tile_format_; |
| 113 | } |
| 114 | |
| 115 | return resource_provider_->best_texture_format(); |
| 116 | } |
| 117 | |
ericrk | eeda5899 | 2016-07-07 02:34:27 | [diff] [blame] | 118 | bool ZeroCopyRasterBufferProvider::IsResourceSwizzleRequired( |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 119 | bool must_support_alpha) const { |
| 120 | return ResourceFormatRequiresSwizzle(GetResourceFormat(must_support_alpha)); |
| 121 | } |
| 122 | |
ericrk | 5ac42f32 | 2016-07-14 01:06:51 | [diff] [blame] | 123 | bool ZeroCopyRasterBufferProvider::CanPartialRasterIntoProvidedResource() |
| 124 | const { |
ericrk | eeda5899 | 2016-07-07 02:34:27 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame^] | 128 | bool ZeroCopyRasterBufferProvider::IsResourceReadyToDraw( |
| 129 | ResourceId resource_id) const { |
| 130 | // Zero-copy resources are immediately ready to draw. |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | uint64_t ZeroCopyRasterBufferProvider::SetReadyToDrawCallback( |
| 135 | const ResourceProvider::ResourceIdArray& resource_ids, |
| 136 | const base::Closure& callback, |
| 137 | uint64_t pending_callback_id) const { |
| 138 | // Zero-copy resources are immediately ready to draw. |
| 139 | return 0; |
| 140 | } |
| 141 | |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 142 | void ZeroCopyRasterBufferProvider::Shutdown() {} |
| 143 | |
| 144 | } // namespace cc |