reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 1 | // Copyright 2014 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 | |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 5 | #include "cc/raster/gpu_raster_buffer_provider.h" |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 6 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
ernstm | 69fedbd | 2014-09-18 01:23:41 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
avi | 02a4d17 | 2015-12-21 06:14:36 | [diff] [blame] | 11 | #include "base/macros.h" |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
ericrk | e402731 | 2016-06-30 00:12:42 | [diff] [blame] | 13 | #include "base/metrics/histogram_macros.h" |
ssid | 904ce3b | 2015-01-27 15:20:16 | [diff] [blame] | 14 | #include "base/trace_event/trace_event.h" |
ericrk | c7c9e3f | 2016-07-01 17:30:16 | [diff] [blame] | 15 | #include "cc/base/histograms.h" |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 16 | #include "cc/paint/display_item_list.h" |
enne | 5a963036 | 2017-02-24 23:41:03 | [diff] [blame] | 17 | #include "cc/paint/paint_canvas.h" |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 18 | #include "cc/paint/paint_recorder.h" |
chrishtr | ac41ff9 | 2017-03-17 05:07:30 | [diff] [blame] | 19 | #include "cc/raster/raster_source.h" |
danakj | 92015685 | 2015-05-18 20:22:29 | [diff] [blame] | 20 | #include "cc/raster/scoped_gpu_raster.h" |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 21 | #include "cc/resources/resource.h" |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 22 | #include "components/viz/common/gpu/context_provider.h" |
| 23 | #include "components/viz/common/gpu/raster_context_provider.h" |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 24 | #include "components/viz/common/gpu/texture_allocation.h" |
danakj | bf124ca | 2018-01-04 16:35:51 | [diff] [blame] | 25 | #include "components/viz/common/resources/resource_format_utils.h" |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 26 | #include "gpu/GLES2/gl2extchromium.h" |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 27 | #include "gpu/command_buffer/client/context_support.h" |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 28 | #include "gpu/command_buffer/client/gles2_interface.h" |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 29 | #include "gpu/command_buffer/client/raster_interface.h" |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 30 | #include "gpu/command_buffer/common/gpu_memory_buffer_support.h" |
hendrikw | 04cea97 | 2014-09-23 20:50:53 | [diff] [blame] | 31 | #include "third_party/skia/include/core/SkMultiPictureDraw.h" |
| 32 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
reveman | 47560ab | 2014-09-18 19:39:21 | [diff] [blame] | 33 | #include "third_party/skia/include/core/SkSurface.h" |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 34 | #include "third_party/skia/include/gpu/GrContext.h" |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 35 | #include "ui/gfx/geometry/axis_transform2d.h" |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 36 | #include "ui/gl/trace_util.h" |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 37 | |
| 38 | namespace cc { |
reveman | 47560ab | 2014-09-18 19:39:21 | [diff] [blame] | 39 | namespace { |
| 40 | |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 41 | static void RasterizeSourceOOP( |
| 42 | const RasterSource* raster_source, |
| 43 | bool resource_has_previous_content, |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 44 | const gpu::Mailbox& mailbox, |
| 45 | GLenum texture_target, |
| 46 | bool texture_is_overlay_candidate, |
| 47 | bool texture_storage_allocated, |
| 48 | const gfx::Size& resource_size, |
| 49 | viz::ResourceFormat resource_format, |
| 50 | const gfx::ColorSpace& color_space, |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 51 | const gfx::Rect& raster_full_rect, |
| 52 | const gfx::Rect& playback_rect, |
| 53 | const gfx::AxisTransform2d& transform, |
| 54 | const RasterSource::PlaybackSettings& playback_settings, |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 55 | viz::RasterContextProvider* context_provider, |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 56 | bool use_distance_field_text, |
| 57 | int msaa_sample_count) { |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 58 | gpu::raster::RasterInterface* ri = context_provider->RasterInterface(); |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 59 | GLuint texture_id = ri->CreateAndConsumeTextureCHROMIUM(mailbox.name); |
| 60 | if (!texture_storage_allocated) { |
| 61 | viz::TextureAllocation alloc = {texture_id, texture_target, |
| 62 | texture_is_overlay_candidate}; |
| 63 | viz::TextureAllocation::AllocateStorage( |
| 64 | ri, context_provider->ContextCapabilities(), resource_format, |
| 65 | resource_size, alloc, color_space); |
| 66 | } |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 67 | |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 68 | // TODO(enne): Use the |texture_target|? GpuMemoryBuffer backed textures don't |
| 69 | // use GL_TEXTURE_2D. |
| 70 | ri->BeginRasterCHROMIUM( |
| 71 | texture_id, raster_source->background_color(), msaa_sample_count, |
| 72 | playback_settings.use_lcd_text, use_distance_field_text, |
| 73 | viz::ResourceFormatToClosestSkColorType(resource_format), |
| 74 | playback_settings.raster_color_space); |
Adrienne Walker | bc5cd82e | 2017-10-31 21:58:15 | [diff] [blame] | 75 | float recording_to_raster_scale = |
| 76 | transform.scale() / raster_source->recording_scale_factor(); |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 77 | gfx::Size content_size = raster_source->GetContentSize(transform.scale()); |
| 78 | // TODO(enne): could skip the clear on new textures, as the service side has |
| 79 | // to do that anyway. resource_has_previous_content implies that the texture |
| 80 | // is not new, but the reverse does not hold, so more plumbing is needed. |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 81 | ri->RasterCHROMIUM(raster_source->GetDisplayItemList().get(), |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 82 | playback_settings.image_provider, content_size, |
| 83 | raster_full_rect, playback_rect, transform.translation(), |
| 84 | recording_to_raster_scale, |
| 85 | raster_source->requires_clear()); |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 86 | ri->EndRasterCHROMIUM(); |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 87 | |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 88 | ri->DeleteTextures(1, &texture_id); |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 89 | } |
| 90 | |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 91 | // The following class is needed to correctly reset GL state when rendering to |
| 92 | // SkCanvases with a GrContext on a RasterInterface enabled context. |
| 93 | class ScopedGrContextAccess { |
| 94 | public: |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 95 | explicit ScopedGrContextAccess(viz::RasterContextProvider* context_provider) |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 96 | : context_provider_(context_provider) { |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 97 | gpu::raster::RasterInterface* ri = context_provider_->RasterInterface(); |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 98 | ri->BeginGpuRaster(); |
| 99 | |
| 100 | class GrContext* gr_context = context_provider_->GrContext(); |
| 101 | gr_context->resetContext(); |
| 102 | } |
| 103 | ~ScopedGrContextAccess() { |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 104 | gpu::raster::RasterInterface* ri = context_provider_->RasterInterface(); |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 105 | ri->EndGpuRaster(); |
| 106 | } |
| 107 | |
| 108 | private: |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 109 | viz::RasterContextProvider* context_provider_; |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 110 | }; |
| 111 | |
vmiura | f7c765c | 2016-12-03 21:02:32 | [diff] [blame] | 112 | static void RasterizeSource( |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 113 | const RasterSource* raster_source, |
| 114 | bool resource_has_previous_content, |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 115 | const gpu::Mailbox& mailbox, |
| 116 | GLenum texture_target, |
| 117 | bool texture_is_overlay_candidate, |
| 118 | bool texture_storage_allocated, |
| 119 | const gfx::Size& resource_size, |
| 120 | viz::ResourceFormat resource_format, |
| 121 | const gfx::ColorSpace& color_space, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 122 | const gfx::Rect& raster_full_rect, |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 123 | const gfx::Rect& playback_rect, |
trchen | 178ac91 | 2017-04-04 10:11:10 | [diff] [blame] | 124 | const gfx::AxisTransform2d& transform, |
vmiura | f7c765c | 2016-12-03 21:02:32 | [diff] [blame] | 125 | const RasterSource::PlaybackSettings& playback_settings, |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 126 | viz::RasterContextProvider* context_provider, |
vmiura | f7c765c | 2016-12-03 21:02:32 | [diff] [blame] | 127 | bool use_distance_field_text, |
| 128 | int msaa_sample_count) { |
Victor Miura | 406891a | 2017-12-16 03:58:30 | [diff] [blame] | 129 | ScopedGrContextAccess gr_context_access(context_provider); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 130 | |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 131 | gpu::raster::RasterInterface* ri = context_provider->RasterInterface(); |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 132 | GLuint texture_id = ri->CreateAndConsumeTextureCHROMIUM(mailbox.name); |
| 133 | if (!texture_storage_allocated) { |
| 134 | viz::TextureAllocation alloc = {texture_id, texture_target, |
| 135 | texture_is_overlay_candidate}; |
| 136 | viz::TextureAllocation::AllocateStorage( |
| 137 | ri, context_provider->ContextCapabilities(), resource_format, |
| 138 | resource_size, alloc, color_space); |
| 139 | } |
Sunny Sachanandani | 0cb875e | 2017-08-15 02:54:52 | [diff] [blame] | 140 | |
| 141 | { |
Xu Xing | 4d36381 | 2018-01-11 01:59:34 | [diff] [blame] | 142 | LayerTreeResourceProvider::ScopedSkSurface scoped_surface( |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 143 | context_provider->GrContext(), texture_id, texture_target, |
| 144 | resource_size, resource_format, use_distance_field_text, |
Sunny Sachanandani | 0cb875e | 2017-08-15 02:54:52 | [diff] [blame] | 145 | playback_settings.use_lcd_text, msaa_sample_count); |
| 146 | |
| 147 | SkSurface* surface = scoped_surface.surface(); |
| 148 | |
| 149 | // Allocating an SkSurface will fail after a lost context. Pretend we |
| 150 | // rasterized, as the contents of the resource don't matter anymore. |
| 151 | if (!surface) { |
| 152 | DLOG(ERROR) << "Failed to allocate raster surface"; |
| 153 | return; |
| 154 | } |
| 155 | |
Sunny Sachanandani | 0cb875e | 2017-08-15 02:54:52 | [diff] [blame] | 156 | SkCanvas* canvas = surface->getCanvas(); |
| 157 | |
| 158 | // As an optimization, inform Skia to discard when not doing partial raster. |
| 159 | if (raster_full_rect == playback_rect) |
| 160 | canvas->discard(); |
| 161 | |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 162 | gfx::Size content_size = raster_source->GetContentSize(transform.scale()); |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 163 | raster_source->PlaybackToCanvas(canvas, color_space, content_size, |
| 164 | raster_full_rect, playback_rect, transform, |
| 165 | playback_settings); |
ccameron | 22094236 | 2017-02-06 20:29:19 | [diff] [blame] | 166 | } |
vmiura | f7c765c | 2016-12-03 21:02:32 | [diff] [blame] | 167 | |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 168 | ri->DeleteTextures(1, &texture_id); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 169 | } |
reveman | 47560ab | 2014-09-18 19:39:21 | [diff] [blame] | 170 | |
| 171 | } // namespace |
vmiura | 78b6928 | 2015-02-14 00:01:17 | [diff] [blame] | 172 | |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 173 | // Subclass for InUsePoolResource that holds ownership of a gpu-rastered backing |
| 174 | // and does cleanup of the backing when destroyed. |
| 175 | class GpuRasterBufferProvider::GpuRasterBacking |
| 176 | : public ResourcePool::GpuBacking { |
| 177 | public: |
| 178 | ~GpuRasterBacking() override { |
| 179 | gpu::gles2::GLES2Interface* gl = compositor_context_provider->ContextGL(); |
| 180 | if (returned_sync_token.HasData()) |
| 181 | gl->WaitSyncTokenCHROMIUM(returned_sync_token.GetConstData()); |
| 182 | if (mailbox_sync_token.HasData()) |
| 183 | gl->WaitSyncTokenCHROMIUM(mailbox_sync_token.GetConstData()); |
| 184 | if (texture_id) |
| 185 | gl->DeleteTextures(1, &texture_id); |
| 186 | } |
| 187 | |
| 188 | base::trace_event::MemoryAllocatorDumpGuid MemoryDumpGuid( |
| 189 | uint64_t tracing_process_id) override { |
| 190 | return gl::GetGLTextureClientGUIDForTracing( |
| 191 | compositor_context_provider->ContextSupport()->ShareGroupTracingGUID(), |
| 192 | texture_id); |
| 193 | } |
| 194 | base::UnguessableToken SharedMemoryGuid() override { return {}; } |
| 195 | |
| 196 | // The ContextProvider used to clean up the texture id. |
| 197 | viz::ContextProvider* compositor_context_provider = nullptr; |
| 198 | // The texture backing of the resource. |
| 199 | GLuint texture_id = 0; |
| 200 | // The allocation of storage for the |texture_id| is deferred, and this tracks |
| 201 | // if it has been done. |
| 202 | bool storage_allocated = false; |
| 203 | }; |
| 204 | |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 205 | GpuRasterBufferProvider::RasterBufferImpl::RasterBufferImpl( |
| 206 | GpuRasterBufferProvider* client, |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 207 | const ResourcePool::InUsePoolResource& in_use_resource, |
| 208 | GpuRasterBacking* backing, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 209 | bool resource_has_previous_content) |
| 210 | : client_(client), |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 211 | backing_(backing), |
| 212 | resource_size_(in_use_resource.size()), |
| 213 | resource_format_(in_use_resource.format()), |
| 214 | color_space_(in_use_resource.color_space()), |
| 215 | resource_has_previous_content_(resource_has_previous_content), |
| 216 | returned_sync_token_(backing->returned_sync_token), |
| 217 | mailbox_(backing->mailbox), |
| 218 | texture_target_(backing->texture_target), |
| 219 | texture_is_overlay_candidate_(backing->overlay_candidate), |
| 220 | texture_storage_allocated_(backing->storage_allocated) { |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 221 | client_->pending_raster_buffers_.insert(this); |
| 222 | } |
| 223 | |
| 224 | GpuRasterBufferProvider::RasterBufferImpl::~RasterBufferImpl() { |
| 225 | client_->pending_raster_buffers_.erase(this); |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 226 | |
| 227 | // This SyncToken was created on the worker context after rastering the |
| 228 | // texture content. |
| 229 | backing_->mailbox_sync_token = after_raster_sync_token_; |
| 230 | if (after_raster_sync_token_.HasData()) { |
| 231 | // The returned SyncToken was waited on in Playback. We know Playback |
| 232 | // happened if the |after_raster_sync_token_| was set. |
| 233 | backing_->returned_sync_token = gpu::SyncToken(); |
| 234 | } |
| 235 | backing_->storage_allocated = texture_storage_allocated_; |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void GpuRasterBufferProvider::RasterBufferImpl::Playback( |
| 239 | const RasterSource* raster_source, |
| 240 | const gfx::Rect& raster_full_rect, |
| 241 | const gfx::Rect& raster_dirty_rect, |
| 242 | uint64_t new_content_id, |
trchen | 178ac91 | 2017-04-04 10:11:10 | [diff] [blame] | 243 | const gfx::AxisTransform2d& transform, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 244 | const RasterSource::PlaybackSettings& playback_settings) { |
| 245 | TRACE_EVENT0("cc", "GpuRasterBuffer::Playback"); |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 246 | // Either a SyncToken had to be made in OrderingBarrier, because a texture |
| 247 | // was made in AcquireBufferForRaster, or a texture already existed and we're |
| 248 | // reusing it. In that case a SyncToken was on the GpuBacking that we must |
| 249 | // wait on befor using the texture. |
| 250 | if (returned_sync_token_.HasData()) |
| 251 | before_raster_sync_token_ = returned_sync_token_; |
| 252 | // The |sync_token_| passed in here was created on the compositor thread, or |
| 253 | // given back with the texture for reuse. This call returns another SyncToken |
| 254 | // generated on the worker thread to synchronize with after the raster is |
| 255 | // complete. |
| 256 | after_raster_sync_token_ = client_->PlaybackOnWorkerThread( |
| 257 | mailbox_, texture_target_, texture_is_overlay_candidate_, |
| 258 | texture_storage_allocated_, before_raster_sync_token_, resource_size_, |
| 259 | resource_format_, color_space_, resource_has_previous_content_, |
| 260 | raster_source, raster_full_rect, raster_dirty_rect, new_content_id, |
| 261 | transform, playback_settings); |
| 262 | texture_storage_allocated_ = true; |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 263 | } |
| 264 | |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 265 | GpuRasterBufferProvider::GpuRasterBufferProvider( |
Xu Xing | 3254916 | 2017-07-17 22:25:43 | [diff] [blame] | 266 | viz::ContextProvider* compositor_context_provider, |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 267 | viz::RasterContextProvider* worker_context_provider, |
Xu Xing | b57edf3 | 2017-08-22 04:18:47 | [diff] [blame] | 268 | LayerTreeResourceProvider* resource_provider, |
hendrikw | 59d44948 | 2015-04-04 00:44:32 | [diff] [blame] | 269 | bool use_distance_field_text, |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 270 | bool use_gpu_memory_buffer_resources, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 271 | int gpu_rasterization_msaa_sample_count, |
Fady Samuel | 555c8d1 | 2017-07-07 23:14:09 | [diff] [blame] | 272 | viz::ResourceFormat preferred_tile_format, |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 273 | bool enable_oop_rasterization) |
danakj | 0de0c95a | 2016-05-25 01:42:49 | [diff] [blame] | 274 | : compositor_context_provider_(compositor_context_provider), |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 275 | worker_context_provider_(worker_context_provider), |
| 276 | resource_provider_(resource_provider), |
| 277 | use_distance_field_text_(use_distance_field_text), |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 278 | use_gpu_memory_buffer_resources_(use_gpu_memory_buffer_resources), |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 279 | msaa_sample_count_(gpu_rasterization_msaa_sample_count), |
ericrk | d03c005d | 2017-03-24 18:48:52 | [diff] [blame] | 280 | preferred_tile_format_(preferred_tile_format), |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 281 | enable_oop_rasterization_(enable_oop_rasterization) { |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 282 | DCHECK(compositor_context_provider); |
| 283 | DCHECK(worker_context_provider); |
danakj | 0de0c95a | 2016-05-25 01:42:49 | [diff] [blame] | 284 | } |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 285 | |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 286 | GpuRasterBufferProvider::~GpuRasterBufferProvider() { |
| 287 | DCHECK(pending_raster_buffers_.empty()); |
| 288 | } |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 289 | |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 290 | std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster( |
danakj | 4e871d8 | 2018-01-18 21:56:57 | [diff] [blame] | 291 | const ResourcePool::InUsePoolResource& resource, |
danakj | 510822aa | 2015-06-01 20:23:02 | [diff] [blame] | 292 | uint64_t resource_content_id, |
danakj | e0b5478 | 2015-05-29 22:47:52 | [diff] [blame] | 293 | uint64_t previous_content_id) { |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 294 | if (!resource.gpu_backing()) { |
| 295 | auto backing = std::make_unique<GpuRasterBacking>(); |
| 296 | backing->compositor_context_provider = compositor_context_provider_; |
| 297 | |
| 298 | gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL(); |
| 299 | const auto& caps = compositor_context_provider_->ContextCapabilities(); |
| 300 | |
| 301 | viz::TextureAllocation alloc = viz::TextureAllocation::MakeTextureId( |
| 302 | gl, caps, resource.format(), use_gpu_memory_buffer_resources_, |
| 303 | /*for_framebuffer_attachment=*/true); |
| 304 | backing->texture_id = alloc.texture_id; |
| 305 | backing->texture_target = alloc.texture_target; |
| 306 | backing->overlay_candidate = alloc.overlay_candidate; |
| 307 | backing->mailbox = gpu::Mailbox::Generate(); |
| 308 | // The SyncToken in order to use this mailbox on another context will be |
| 309 | // generated later by OrderingBarrier(), once for all mailboxes made at a |
| 310 | // given time. |
| 311 | gl->ProduceTextureDirectCHROMIUM(backing->texture_id, |
| 312 | backing->mailbox.name); |
| 313 | |
| 314 | resource.set_gpu_backing(std::move(backing)); |
| 315 | } |
| 316 | GpuRasterBacking* backing = |
| 317 | static_cast<GpuRasterBacking*>(resource.gpu_backing()); |
| 318 | |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 319 | bool resource_has_previous_content = |
| 320 | resource_content_id && resource_content_id == previous_content_id; |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 321 | return std::make_unique<RasterBufferImpl>(this, resource, backing, |
danakj | 4e871d8 | 2018-01-18 21:56:57 | [diff] [blame] | 322 | resource_has_previous_content); |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 323 | } |
| 324 | |
sunnyps | 12074d3d | 2016-06-21 01:57:48 | [diff] [blame] | 325 | void GpuRasterBufferProvider::OrderingBarrier() { |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 326 | TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier"); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 327 | |
| 328 | gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL(); |
Xu Xing | 4d36381 | 2018-01-11 01:59:34 | [diff] [blame] | 329 | gpu::SyncToken sync_token = |
| 330 | LayerTreeResourceProvider::GenerateSyncTokenHelper(gl); |
Victor Miura | ff648861 | 2017-12-21 04:16:15 | [diff] [blame] | 331 | for (RasterBufferImpl* buffer : pending_raster_buffers_) |
| 332 | buffer->set_sync_token(sync_token); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 333 | pending_raster_buffers_.clear(); |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 334 | } |
| 335 | |
Sunny Sachanandani | 5f5419e2 | 2017-05-12 20:35:30 | [diff] [blame] | 336 | void GpuRasterBufferProvider::Flush() { |
Victor Miura | ff648861 | 2017-12-21 04:16:15 | [diff] [blame] | 337 | compositor_context_provider_->ContextSupport()->FlushPendingWork(); |
Sunny Sachanandani | 5f5419e2 | 2017-05-12 20:35:30 | [diff] [blame] | 338 | } |
| 339 | |
Fady Samuel | 555c8d1 | 2017-07-07 23:14:09 | [diff] [blame] | 340 | viz::ResourceFormat GpuRasterBufferProvider::GetResourceFormat( |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 341 | bool must_support_alpha) const { |
ericrk | d03c005d | 2017-03-24 18:48:52 | [diff] [blame] | 342 | if (resource_provider_->IsRenderBufferFormatSupported( |
| 343 | preferred_tile_format_) && |
| 344 | (DoesResourceFormatSupportAlpha(preferred_tile_format_) || |
| 345 | !must_support_alpha)) { |
| 346 | return preferred_tile_format_; |
| 347 | } |
| 348 | |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 349 | return resource_provider_->best_render_buffer_format(); |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 350 | } |
| 351 | |
ericrk | eeda5899 | 2016-07-07 02:34:27 | [diff] [blame] | 352 | bool GpuRasterBufferProvider::IsResourceSwizzleRequired( |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 353 | bool must_support_alpha) const { |
| 354 | // This doesn't require a swizzle because we rasterize to the correct format. |
| 355 | return false; |
| 356 | } |
| 357 | |
ericrk | 5ac42f32 | 2016-07-14 01:06:51 | [diff] [blame] | 358 | bool GpuRasterBufferProvider::CanPartialRasterIntoProvidedResource() const { |
ericrk | c9ed083 | 2016-07-20 19:42:50 | [diff] [blame] | 359 | // Partial raster doesn't support MSAA, as the MSAA resolve is unaware of clip |
| 360 | // rects. |
| 361 | // TODO(crbug.com/629683): See if we can work around this limitation. |
| 362 | return msaa_sample_count_ == 0; |
ericrk | eeda5899 | 2016-07-07 02:34:27 | [diff] [blame] | 363 | } |
| 364 | |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 365 | bool GpuRasterBufferProvider::IsResourceReadyToDraw( |
danakj | 4e871d8 | 2018-01-18 21:56:57 | [diff] [blame] | 366 | const ResourcePool::InUsePoolResource& resource) const { |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 367 | const gpu::SyncToken& sync_token = resource.gpu_backing()->mailbox_sync_token; |
| 368 | // This SyncToken() should have been set by calling OrderingBarrier() before |
| 369 | // calling this. |
| 370 | DCHECK(sync_token.HasData()); |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 371 | |
sunnyps | 7499629 | 2017-03-15 02:35:48 | [diff] [blame] | 372 | // IsSyncTokenSignaled is thread-safe, no need for worker context lock. |
| 373 | return worker_context_provider_->ContextSupport()->IsSyncTokenSignaled( |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 374 | sync_token); |
| 375 | } |
| 376 | |
| 377 | uint64_t GpuRasterBufferProvider::SetReadyToDrawCallback( |
danakj | 4e871d8 | 2018-01-18 21:56:57 | [diff] [blame] | 378 | const std::vector<const ResourcePool::InUsePoolResource*>& resources, |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 379 | const base::Closure& callback, |
| 380 | uint64_t pending_callback_id) const { |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 381 | gpu::SyncToken latest_sync_token; |
| 382 | for (const auto* in_use : resources) { |
| 383 | const gpu::SyncToken& sync_token = |
| 384 | in_use->gpu_backing()->mailbox_sync_token; |
| 385 | if (sync_token.release_count() > latest_sync_token.release_count()) |
| 386 | latest_sync_token = sync_token; |
| 387 | } |
| 388 | uint64_t callback_id = latest_sync_token.release_count(); |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 389 | DCHECK_NE(callback_id, 0u); |
| 390 | |
| 391 | // If the callback is different from the one the caller is already waiting on, |
sunnyps | 31c92fe | 2017-02-10 23:46:55 | [diff] [blame] | 392 | // pass the callback through to SignalSyncToken. Otherwise the request is |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 393 | // redundant. |
| 394 | if (callback_id != pending_callback_id) { |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 395 | // Use the compositor context because we want this callback on the |
| 396 | // compositor thread. |
| 397 | compositor_context_provider_->ContextSupport()->SignalSyncToken( |
| 398 | latest_sync_token, callback); |
ericrk | 7f6a27f | 2017-01-31 22:34:32 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | return callback_id; |
| 402 | } |
| 403 | |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 404 | void GpuRasterBufferProvider::Shutdown() { |
| 405 | pending_raster_buffers_.clear(); |
| 406 | } |
| 407 | |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 408 | gpu::SyncToken GpuRasterBufferProvider::PlaybackOnWorkerThread( |
| 409 | const gpu::Mailbox& mailbox, |
| 410 | GLenum texture_target, |
| 411 | bool texture_is_overlay_candidate, |
| 412 | bool texture_storage_allocated, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 413 | const gpu::SyncToken& sync_token, |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 414 | const gfx::Size& resource_size, |
| 415 | viz::ResourceFormat resource_format, |
| 416 | const gfx::ColorSpace& color_space, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 417 | bool resource_has_previous_content, |
| 418 | const RasterSource* raster_source, |
| 419 | const gfx::Rect& raster_full_rect, |
| 420 | const gfx::Rect& raster_dirty_rect, |
| 421 | uint64_t new_content_id, |
trchen | 178ac91 | 2017-04-04 10:11:10 | [diff] [blame] | 422 | const gfx::AxisTransform2d& transform, |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 423 | const RasterSource::PlaybackSettings& playback_settings) { |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 424 | viz::RasterContextProvider::ScopedRasterContextLock scoped_context( |
Xu Xing | 3254916 | 2017-07-17 22:25:43 | [diff] [blame] | 425 | worker_context_provider_); |
Victor Miura | 29b7ea3d | 2017-12-19 20:23:59 | [diff] [blame] | 426 | gpu::raster::RasterInterface* ri = scoped_context.RasterInterface(); |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 427 | DCHECK(ri); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 428 | |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 429 | // Wait on the SyncToken that was created on the compositor thread after |
| 430 | // making the mailbox. This ensures that the mailbox we consume here is valid |
| 431 | // by the time the consume command executes. |
Victor Miura | 3a4ad4f8 | 2017-12-13 06:03:45 | [diff] [blame] | 432 | ri->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 433 | |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 434 | gfx::Rect playback_rect = raster_full_rect; |
| 435 | if (resource_has_previous_content) { |
| 436 | playback_rect.Intersect(raster_dirty_rect); |
| 437 | } |
| 438 | DCHECK(!playback_rect.IsEmpty()) |
| 439 | << "Why are we rastering a tile that's not dirty?"; |
| 440 | |
| 441 | // Log a histogram of the percentage of pixels that were saved due to |
| 442 | // partial raster. |
| 443 | const char* client_name = GetClientNameForMetrics(); |
| 444 | float full_rect_size = raster_full_rect.size().GetArea(); |
| 445 | if (full_rect_size > 0 && client_name) { |
| 446 | float fraction_partial_rastered = |
| 447 | static_cast<float>(playback_rect.size().GetArea()) / full_rect_size; |
| 448 | float fraction_saved = 1.0f - fraction_partial_rastered; |
| 449 | UMA_HISTOGRAM_PERCENTAGE( |
| 450 | base::StringPrintf("Renderer4.%s.PartialRasterPercentageSaved.Gpu", |
| 451 | client_name), |
| 452 | 100.0f * fraction_saved); |
| 453 | } |
| 454 | |
| 455 | if (enable_oop_rasterization_) { |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 456 | RasterizeSourceOOP( |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 457 | raster_source, resource_has_previous_content, mailbox, texture_target, |
| 458 | texture_is_overlay_candidate, texture_storage_allocated, resource_size, |
| 459 | resource_format, color_space, raster_full_rect, playback_rect, |
| 460 | transform, playback_settings, worker_context_provider_, |
| 461 | use_distance_field_text_, msaa_sample_count_); |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 462 | } else { |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 463 | RasterizeSource(raster_source, resource_has_previous_content, mailbox, |
| 464 | texture_target, texture_is_overlay_candidate, |
| 465 | texture_storage_allocated, resource_size, resource_format, |
| 466 | color_space, raster_full_rect, playback_rect, transform, |
| 467 | playback_settings, worker_context_provider_, |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 468 | use_distance_field_text_, msaa_sample_count_); |
Adrienne Walker | 436a775 | 2017-08-28 23:33:09 | [diff] [blame] | 469 | } |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 470 | |
Sunny Sachanandani | 0cb875e | 2017-08-15 02:54:52 | [diff] [blame] | 471 | // Generate sync token for cross context synchronization. |
danakj | af3170e | 2018-02-09 17:31:58 | [diff] [blame^] | 472 | return LayerTreeResourceProvider::GenerateSyncTokenHelper(ri); |
sunnyps | 5d6ff0d0 | 2016-06-28 00:40:11 | [diff] [blame] | 473 | } |
prashant.n | b4d4f49 | 2016-04-29 12:51:28 | [diff] [blame] | 474 | |
reveman@chromium.org | b5641b9 | 2014-02-15 14:21:58 | [diff] [blame] | 475 | } // namespace cc |