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_, resource_has_previous_content_, raster_source, raster_full_rect, |
50 | 48 raster_dirty_rect, new_content_id, scale, playback_settings); |
51 ContextProvider::ScopedContextLock scoped_context( | |
52 rasterizer_->worker_context_provider()); | |
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 } | 49 } |
77 | 50 |
78 private: | 51 private: |
79 GpuRasterizer* rasterizer_; | 52 GpuRasterBufferProvider* client_; |
80 ResourceProvider::ScopedWriteLockGr lock_; | 53 ResourceProvider::ScopedWriteLockGL lock_; |
81 bool resource_has_previous_content_; | 54 bool resource_has_previous_content_; |
82 | 55 |
83 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); | 56 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); |
84 }; | 57 }; |
85 | 58 |
59 static sk_sp<SkPicture> PlaybackToPicture( | |
60 const RasterSource* raster_source, | |
61 bool resource_has_previous_content, | |
62 const gfx::Size& resource_size, | |
63 const gfx::Rect& raster_full_rect, | |
64 const gfx::Rect& raster_dirty_rect, | |
65 float scale, | |
66 const RasterSource::PlaybackSettings& playback_settings) { | |
67 // GPU raster doesn't do low res tiles, so should always include images. | |
68 DCHECK(!playback_settings.skip_images); | |
69 | |
70 gfx::Rect playback_rect = raster_full_rect; | |
71 if (resource_has_previous_content) { | |
72 playback_rect.Intersect(raster_dirty_rect); | |
73 } | |
74 DCHECK(!playback_rect.IsEmpty()) | |
75 << "Why are we rastering a tile that's not dirty?"; | |
76 | |
77 // Play back raster_source into temp SkPicture. | |
78 SkPictureRecorder recorder; | |
79 const int flags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; | |
80 sk_sp<SkCanvas> canvas = sk_ref_sp(recorder.beginRecording( | |
81 resource_size.width(), resource_size.height(), NULL, flags)); | |
82 canvas->save(); | |
83 raster_source->PlaybackToCanvas(canvas.get(), raster_full_rect, playback_rect, | |
84 scale, playback_settings); | |
85 canvas->restore(); | |
86 return recorder.finishRecordingAsPicture(); | |
87 } | |
88 | |
89 static void RasterizePicture(SkPicture* picture, | |
90 ContextProvider* context_provider, | |
91 ResourceProvider::ScopedWriteLockGL* resource_lock, | |
92 bool async_worker_context_enabled, | |
93 bool use_distance_field_text, | |
94 bool can_use_lcd_text, | |
95 int msaa_sample_count) { | |
96 ScopedGpuRaster gpu_raster(context_provider); | |
97 | |
98 ResourceProvider::ScopedSkSurfaceProvider scoped_surface( | |
99 context_provider, resource_lock, async_worker_context_enabled, | |
100 use_distance_field_text, can_use_lcd_text, msaa_sample_count); | |
101 SkSurface* sk_surface = scoped_surface.sk_surface(); | |
102 // Allocating an SkSurface will fail after a lost context. Pretend we | |
103 // rasterized, as the contents of the resource don't matter anymore. | |
104 if (!sk_surface) | |
105 return; | |
106 | |
107 SkMultiPictureDraw multi_picture_draw; | |
108 multi_picture_draw.add(sk_surface->getCanvas(), picture); | |
109 multi_picture_draw.draw(false); | |
110 } | |
111 | |
86 } // namespace | 112 } // namespace |
87 | 113 |
88 GpuRasterBufferProvider::GpuRasterBufferProvider( | 114 GpuRasterBufferProvider::GpuRasterBufferProvider( |
89 ContextProvider* compositor_context_provider, | 115 ContextProvider* compositor_context_provider, |
90 ContextProvider* worker_context_provider, | 116 ContextProvider* worker_context_provider, |
91 ResourceProvider* resource_provider, | 117 ResourceProvider* resource_provider, |
92 bool use_distance_field_text, | 118 bool use_distance_field_text, |
93 int gpu_rasterization_msaa_sample_count) | 119 int gpu_rasterization_msaa_sample_count, |
120 bool async_worker_context_enabled) | |
94 : compositor_context_provider_(compositor_context_provider), | 121 : compositor_context_provider_(compositor_context_provider), |
95 rasterizer_(new GpuRasterizer(worker_context_provider, | 122 worker_context_provider_(worker_context_provider), |
96 resource_provider, | 123 resource_provider_(resource_provider), |
97 use_distance_field_text, | 124 use_distance_field_text_(use_distance_field_text), |
98 gpu_rasterization_msaa_sample_count)) { | 125 msaa_sample_count_(gpu_rasterization_msaa_sample_count), |
99 DCHECK(compositor_context_provider_); | 126 async_worker_context_enabled_(async_worker_context_enabled) { |
127 DCHECK(compositor_context_provider); | |
128 DCHECK(worker_context_provider); | |
100 } | 129 } |
101 | 130 |
102 GpuRasterBufferProvider::~GpuRasterBufferProvider() {} | 131 GpuRasterBufferProvider::~GpuRasterBufferProvider() {} |
103 | 132 |
104 std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster( | 133 std::unique_ptr<RasterBuffer> GpuRasterBufferProvider::AcquireBufferForRaster( |
105 const Resource* resource, | 134 const Resource* resource, |
106 uint64_t resource_content_id, | 135 uint64_t resource_content_id, |
107 uint64_t previous_content_id) { | 136 uint64_t previous_content_id) { |
137 bool resource_has_previous_content = | |
138 resource_content_id && resource_content_id == previous_content_id; | |
108 return std::unique_ptr<RasterBuffer>(new RasterBufferImpl( | 139 return std::unique_ptr<RasterBuffer>(new RasterBufferImpl( |
109 rasterizer_.get(), resource, resource_content_id, previous_content_id)); | 140 this, resource_provider_, resource->id(), async_worker_context_enabled_, |
141 resource_has_previous_content)); | |
110 } | 142 } |
111 | 143 |
112 void GpuRasterBufferProvider::ReleaseBufferForRaster( | 144 void GpuRasterBufferProvider::ReleaseBufferForRaster( |
113 std::unique_ptr<RasterBuffer> buffer) { | 145 std::unique_ptr<RasterBuffer> buffer) { |
114 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. | 146 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. |
115 } | 147 } |
116 | 148 |
117 void GpuRasterBufferProvider::OrderingBarrier() { | 149 void GpuRasterBufferProvider::OrderingBarrier() { |
118 TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier"); | 150 TRACE_EVENT0("cc", "GpuRasterBufferProvider::OrderingBarrier"); |
119 compositor_context_provider_->ContextGL()->OrderingBarrierCHROMIUM(); | 151 gpu::gles2::GLES2Interface* gl = compositor_context_provider_->ContextGL(); |
152 // Synchronize with compositor. | |
153 GLuint64 fence = gl->InsertFenceSyncCHROMIUM(); | |
154 gl->OrderingBarrierCHROMIUM(); | |
155 gl->GenUnverifiedSyncTokenCHROMIUM(fence, sync_token_.GetData()); | |
120 } | 156 } |
121 | 157 |
122 ResourceFormat GpuRasterBufferProvider::GetResourceFormat( | 158 ResourceFormat GpuRasterBufferProvider::GetResourceFormat( |
123 bool must_support_alpha) const { | 159 bool must_support_alpha) const { |
124 return rasterizer_->resource_provider()->best_render_buffer_format(); | 160 return resource_provider_->best_render_buffer_format(); |
125 } | 161 } |
126 | 162 |
127 bool GpuRasterBufferProvider::GetResourceRequiresSwizzle( | 163 bool GpuRasterBufferProvider::GetResourceRequiresSwizzle( |
128 bool must_support_alpha) const { | 164 bool must_support_alpha) const { |
129 // This doesn't require a swizzle because we rasterize to the correct format. | 165 // This doesn't require a swizzle because we rasterize to the correct format. |
130 return false; | 166 return false; |
131 } | 167 } |
132 | 168 |
133 void GpuRasterBufferProvider::Shutdown() {} | 169 void GpuRasterBufferProvider::Shutdown() {} |
134 | 170 |
171 void GpuRasterBufferProvider::PlaybackOnWorkerThread( | |
172 ResourceProvider::ScopedWriteLockGL* resource_lock, | |
173 bool resource_has_previous_content, | |
174 const RasterSource* raster_source, | |
175 const gfx::Rect& raster_full_rect, | |
176 const gfx::Rect& raster_dirty_rect, | |
177 uint64_t new_content_id, | |
178 float scale, | |
179 const RasterSource::PlaybackSettings& playback_settings) { | |
180 ContextProvider::ScopedContextLock scoped_context(worker_context_provider_); | |
181 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL(); | |
182 | |
183 // Synchronize with compositor if worker context is async. | |
184 DCHECK(sync_token_.HasData()); | |
piman
2016/05/26 00:01:10
If I understand correctly, OrderingBarrier() is ca
sunnyps
2016/05/26 01:39:27
You're right. I overlooked the data race here. We
| |
185 gl->WaitSyncTokenCHROMIUM(sync_token_.GetData()); | |
186 | |
187 sk_sp<SkPicture> picture = PlaybackToPicture( | |
188 raster_source, resource_has_previous_content, resource_lock->size(), | |
189 raster_full_rect, raster_dirty_rect, scale, playback_settings); | |
190 | |
191 // Turn on distance fields for layers that have ever animated. | |
192 bool use_distance_field_text = | |
193 use_distance_field_text_ || | |
194 raster_source->ShouldAttemptToUseDistanceFieldText(); | |
195 | |
196 RasterizePicture(picture.get(), worker_context_provider_, resource_lock, | |
197 async_worker_context_enabled_, use_distance_field_text, | |
198 raster_source->CanUseLCDText(), msaa_sample_count_); | |
199 | |
200 const uint64_t fence_sync = gl->InsertFenceSyncCHROMIUM(); | |
201 | |
202 // Barrier to sync worker context output to cc context. | |
203 gl->OrderingBarrierCHROMIUM(); | |
204 | |
205 // Generate sync token after the barrier for cross context synchronization. | |
206 gpu::SyncToken sync_token; | |
207 gl->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | |
208 resource_lock->set_sync_token(sync_token); | |
209 } | |
210 | |
135 } // namespace cc | 211 } // namespace cc |
OLD | NEW |