OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/raster/gpu_raster_buffer_provider.h" | 5 #include "cc/raster/gpu_raster_buffer_provider.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
14 #include "cc/playback/raster_source.h" | 14 #include "cc/playback/raster_source.h" |
15 #include "cc/raster/gpu_rasterizer.h" | |
16 #include "cc/raster/scoped_gpu_raster.h" | 15 #include "cc/raster/scoped_gpu_raster.h" |
17 #include "cc/resources/resource.h" | 16 #include "cc/resources/resource.h" |
18 #include "gpu/command_buffer/client/gles2_interface.h" | 17 #include "gpu/command_buffer/client/gles2_interface.h" |
19 #include "third_party/skia/include/core/SkMultiPictureDraw.h" | 18 #include "third_party/skia/include/core/SkMultiPictureDraw.h" |
20 #include "third_party/skia/include/core/SkPictureRecorder.h" | 19 #include "third_party/skia/include/core/SkPictureRecorder.h" |
21 #include "third_party/skia/include/core/SkSurface.h" | 20 #include "third_party/skia/include/core/SkSurface.h" |
22 #include "third_party/skia/include/gpu/GrContext.h" | 21 #include "third_party/skia/include/gpu/GrContext.h" |
23 | 22 |
24 namespace cc { | 23 namespace cc { |
25 namespace { | 24 namespace { |
26 | 25 |
27 class RasterBufferImpl : public RasterBuffer { | 26 class RasterBufferImpl : public RasterBuffer { |
28 public: | 27 public: |
29 RasterBufferImpl(GpuRasterizer* rasterizer, | 28 RasterBufferImpl(GpuRasterBufferProvider* client, |
30 const Resource* resource, | 29 ResourceProvider* resource_provider, |
31 uint64_t resource_content_id, | 30 ResourceId resource_id, |
32 uint64_t previous_content_id) | 31 bool async_worker_context_enabled, |
33 : rasterizer_(rasterizer), | 32 bool resource_has_previous_content) |
34 lock_(rasterizer->resource_provider(), resource->id()), | 33 : client_(client), |
35 resource_has_previous_content_( | 34 lock_(resource_provider, resource_id, async_worker_context_enabled), |
36 resource_content_id && resource_content_id == previous_content_id) { | 35 resource_has_previous_content_(resource_has_previous_content) {} |
37 } | |
38 | 36 |
39 // Overridden from RasterBuffer: | 37 // Overridden from RasterBuffer: |
40 void Playback( | 38 void Playback( |
41 const RasterSource* raster_source, | 39 const RasterSource* raster_source, |
42 const gfx::Rect& raster_full_rect, | 40 const gfx::Rect& raster_full_rect, |
43 const gfx::Rect& raster_dirty_rect, | 41 const gfx::Rect& raster_dirty_rect, |
44 uint64_t new_content_id, | 42 uint64_t new_content_id, |
45 float scale, | 43 float scale, |
46 const RasterSource::PlaybackSettings& playback_settings) override { | 44 const RasterSource::PlaybackSettings& playback_settings) override { |
47 TRACE_EVENT0("cc", "RasterBufferImpl::Playback"); | 45 TRACE_EVENT0("cc", "RasterBufferImpl::Playback"); |
48 // GPU raster doesn't do low res tiles, so should always include images. | 46 client_->PlaybackOnWorkerThread( |
49 DCHECK(!playback_settings.skip_images); | 47 &lock_, sync_token_, resource_has_previous_content_, raster_source, |
| 48 raster_full_rect, raster_dirty_rect, new_content_id, scale, |
| 49 playback_settings); |
| 50 } |
50 | 51 |
51 ContextProvider::ScopedContextLock scoped_context( | 52 void SetSyncToken(const gpu::SyncToken& sync_token) override { |
52 rasterizer_->worker_context_provider()); | 53 sync_token_ = sync_token; |
53 | |
54 gfx::Rect playback_rect = raster_full_rect; | |
55 if (resource_has_previous_content_) { | |
56 playback_rect.Intersect(raster_dirty_rect); | |
57 } | |
58 DCHECK(!playback_rect.IsEmpty()) | |
59 << "Why are we rastering a tile that's not dirty?"; | |
60 | |
61 // TODO(danakj): Implement partial raster with raster_dirty_rect. | |
62 // Rasterize source into resource. | |
63 rasterizer_->RasterizeSource(&lock_, raster_source, raster_full_rect, | |
64 playback_rect, scale, playback_settings); | |
65 | |
66 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL(); | |
67 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
68 | |
69 // Barrier to sync worker context output to cc context. | |
70 gl->OrderingBarrierCHROMIUM(); | |
71 | |
72 // Generate sync token after the barrier for cross context synchronization. | |
73 gpu::SyncToken sync_token; | |
74 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
75 lock_.UpdateResourceSyncToken(sync_token); | |
76 } | 54 } |
77 | 55 |
78 private: | 56 private: |
79 GpuRasterizer* rasterizer_; | 57 GpuRasterBufferProvider* const client_; |
80 ResourceProvider::ScopedWriteLockGr lock_; | 58 ResourceProvider::ScopedWriteLockGL lock_; |
81 bool resource_has_previous_content_; | 59 const bool resource_has_previous_content_; |
| 60 |
| 61 gpu::SyncToken sync_token_; |
82 | 62 |
83 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); | 63 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
84 }; | 64 }; |
85 | 65 |
| 66 static sk_sp<SkPicture> PlaybackToPicture( |
| 67 const RasterSource* raster_source, |
| 68 bool resource_has_previous_content, |
| 69 const gfx::Size& resource_size, |
| 70 const gfx::Rect& raster_full_rect, |
| 71 const gfx::Rect& raster_dirty_rect, |
| 72 float scale, |
| 73 const RasterSource::PlaybackSettings& playback_settings) { |
| 74 // GPU raster doesn't do low res tiles, so should always include images. |
| 75 DCHECK(!playback_settings.skip_images); |
| 76 |
| 77 gfx::Rect playback_rect = raster_full_rect; |
| 78 if (resource_has_previous_content) { |
| 79 playback_rect.Intersect(raster_dirty_rect); |
| 80 } |
| 81 DCHECK(!playback_rect.IsEmpty()) |
| 82 << "Why are we rastering a tile that's not dirty?"; |
| 83 |
| 84 // Play back raster_source into temp SkPicture. |
| 85 SkPictureRecorder recorder; |
| 86 const int flags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; |
| 87 sk_sp<SkCanvas> canvas = sk_ref_sp(recorder.beginRecording( |
| 88 resource_size.width(), resource_size.height(), NULL, flags)); |
| 89 canvas->save(); |
| 90 raster_source->PlaybackToCanvas(canvas.get(), raster_full_rect, playback_rect, |
| 91 scale, playback_settings); |
| 92 canvas->restore(); |
| 93 return recorder.finishRecordingAsPicture(); |
| 94 } |
| 95 |
| 96 static void RasterizePicture(SkPicture* picture, |
| 97 ContextProvider* context_provider, |
| 98 ResourceProvider::ScopedWriteLockGL* resource_lock, |
| 99 bool async_worker_context_enabled, |
| 100 bool use_distance_field_text, |
| 101 bool can_use_lcd_text, |
| 102 int msaa_sample_count) { |
| 103 ScopedGpuRaster gpu_raster(context_provider); |
| 104 |
| 105 ResourceProvider::ScopedSkSurfaceProvider scoped_surface( |
| 106 context_provider, resource_lock, async_worker_context_enabled, |
| 107 use_distance_field_text, can_use_lcd_text, msaa_sample_count); |
| 108 SkSurface* sk_surface = scoped_surface.sk_surface(); |
| 109 // Allocating an SkSurface will fail after a lost context. Pretend we |
| 110 // rasterized, as the contents of the resource don't matter anymore. |
| 111 if (!sk_surface) |
| 112 return; |
| 113 |
| 114 SkMultiPictureDraw multi_picture_draw; |
| 115 multi_picture_draw.add(sk_surface->getCanvas(), picture); |
| 116 multi_picture_draw.draw(false); |
| 117 } |
| 118 |
86 } // namespace | 119 } // namespace |
87 | 120 |
88 GpuRasterBufferProvider::GpuRasterBufferProvider( | 121 GpuRasterBufferProvider::GpuRasterBufferProvider( |
89 ContextProvider* compositor_context_provider, | 122 ContextProvider* compositor_context_provider, |
90 ContextProvider* worker_context_provider, | 123 ContextProvider* worker_context_provider, |
91 ResourceProvider* resource_provider, | 124 ResourceProvider* resource_provider, |
92 bool use_distance_field_text, | 125 bool use_distance_field_text, |
93 int gpu_rasterization_msaa_sample_count) | 126 int gpu_rasterization_msaa_sample_count, |
| 127 bool async_worker_context_enabled) |
94 : compositor_context_provider_(compositor_context_provider), | 128 : compositor_context_provider_(compositor_context_provider), |
95 rasterizer_(new GpuRasterizer(worker_context_provider, | 129 worker_context_provider_(worker_context_provider), |
96 resource_provider, | 130 resource_provider_(resource_provider), |
97 use_distance_field_text, | 131 use_distance_field_text_(use_distance_field_text), |
98 gpu_rasterization_msaa_sample_count)) { | 132 msaa_sample_count_(gpu_rasterization_msaa_sample_count), |
99 DCHECK(compositor_context_provider_); | 133 async_worker_context_enabled_(async_worker_context_enabled) { |
| 134 DCHECK(compositor_context_provider); |
| 135 DCHECK(worker_context_provider); |
100 } | 136 } |
101 | 137 |
102 GpuRasterBufferProvider::~GpuRasterBufferProvider() {} | 138 GpuRasterBufferProvider::~GpuRasterBufferProvider() { |
| 139 DCHECK(pending_raster_buffers_.empty()); |
| 140 } |
103 | 141 |
104 std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster( | 142 std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster( |
105 const Resource* resource, | 143 const Resource* resource, |
106 uint64_t resource_content_id, | 144 uint64_t resource_content_id, |
107 uint64_t previous_content_id) { | 145 uint64_t previous_content_id) { |
108 return std::unique_ptr<RasterBuffer>(new RasterBufferImpl( | 146 bool resource_has_previous_content = |
109 rasterizer_.get(), resource, resource_content_id, previous_content_id)); | 147 resource_content_id && resource_content_id == previous_content_id; |
| 148 std::unique_ptr<RasterBuffer> buffer(new RasterBufferImpl( |
| 149 this, resource_provider_, resource->id(), async_worker_context_enabled_, |
| 150 resource_has_previous_content)); |
| 151 pending_raster_buffers_.insert(buffer.get()); |
| 152 return buffer; |
110 } | 153 } |
111 | 154 |
112 void GpuRasterBufferProvider::ReleaseBufferForRaster( | 155 void GpuRasterBufferProvider::ReleaseBufferForRaster( |
113 std::unique_ptr<RasterBuffer> buffer) { | 156 std::unique_ptr<RasterBuffer> buffer) { |
114 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. | 157 pending_raster_buffers_.erase(buffer.get()); |
115 } | 158 } |
116 | 159 |
117 void GpuRasterBufferProvider::OrderingBarrier() { | 160 void GpuRasterBufferProvider::OrderingBarrier() { |
118 TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier"); | 161 TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier"); |
119 compositor_context_provider_->ContextGL()->OrderingBarrierCHROMIUM(); | 162 |
| 163 gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL(); |
| 164 GLuint64 fence = gl->InsertFenceSyncCHROMIUM(); |
| 165 gl->OrderingBarrierCHROMIUM(); |
| 166 |
| 167 gpu::SyncToken sync_token; |
| 168 gl->GenUnverifiedSyncTokenCHROMIUM(fence, sync_token.GetData()); |
| 169 |
| 170 for (RasterBuffer* buffer : pending_raster_buffers_) |
| 171 buffer->SetSyncToken(sync_token); |
| 172 pending_raster_buffers_.clear(); |
120 } | 173 } |
121 | 174 |
122 ResourceFormat GpuRasterBufferProvider::GetResourceFormat( | 175 ResourceFormat GpuRasterBufferProvider::GetResourceFormat( |
123 bool must_support_alpha) const { | 176 bool must_support_alpha) const { |
124 return rasterizer_->resource_provider()->best_render_buffer_format(); | 177 return resource_provider_->best_render_buffer_format(); |
125 } | 178 } |
126 | 179 |
127 bool GpuRasterBufferProvider::GetResourceRequiresSwizzle( | 180 bool GpuRasterBufferProvider::GetResourceRequiresSwizzle( |
128 bool must_support_alpha) const { | 181 bool must_support_alpha) const { |
129 // This doesn't require a swizzle because we rasterize to the correct format. | 182 // This doesn't require a swizzle because we rasterize to the correct format. |
130 return false; | 183 return false; |
131 } | 184 } |
132 | 185 |
133 void GpuRasterBufferProvider::Shutdown() {} | 186 void GpuRasterBufferProvider::Shutdown() { |
| 187 pending_raster_buffers_.clear(); |
| 188 } |
| 189 |
| 190 void GpuRasterBufferProvider::PlaybackOnWorkerThread( |
| 191 ResourceProvider::ScopedWriteLockGL* resource_lock, |
| 192 const gpu::SyncToken& sync_token, |
| 193 bool resource_has_previous_content, |
| 194 const RasterSource* raster_source, |
| 195 const gfx::Rect& raster_full_rect, |
| 196 const gfx::Rect& raster_dirty_rect, |
| 197 uint64_t new_content_id, |
| 198 float scale, |
| 199 const RasterSource::PlaybackSettings& playback_settings) { |
| 200 ContextProvider::ScopedContextLock scoped_context(worker_context_provider_); |
| 201 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL(); |
| 202 |
| 203 // Synchronize with compositor if worker context is async. |
| 204 DCHECK(sync_token.HasData()); |
| 205 gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 206 |
| 207 sk_sp<SkPicture> picture = PlaybackToPicture( |
| 208 raster_source, resource_has_previous_content, resource_lock->size(), |
| 209 raster_full_rect, raster_dirty_rect, scale, playback_settings); |
| 210 |
| 211 // Turn on distance fields for layers that have ever animated. |
| 212 bool use_distance_field_text = |
| 213 use_distance_field_text_ || |
| 214 raster_source->ShouldAttemptToUseDistanceFieldText(); |
| 215 |
| 216 RasterizePicture(picture.get(), worker_context_provider_, resource_lock, |
| 217 async_worker_context_enabled_, use_distance_field_text, |
| 218 raster_source->CanUseLCDText(), msaa_sample_count_); |
| 219 |
| 220 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); |
| 221 |
| 222 // Barrier to sync worker context output to cc context. |
| 223 gl->OrderingBarrierCHROMIUM(); |
| 224 |
| 225 // Generate sync token after the barrier for cross context synchronization. |
| 226 gpu::SyncToken resource_sync_token; |
| 227 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, resource_sync_token.GetData()); |
| 228 resource_lock->set_sync_token(resource_sync_token); |
| 229 } |
134 | 230 |
135 } // namespace cc | 231 } // namespace cc |
OLD | NEW |