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