OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/resources/gpu_rasterizer.h" | 5 #include "cc/resources/gpu_rasterizer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "cc/debug/devtools_instrumentation.h" | 12 #include "cc/debug/devtools_instrumentation.h" |
13 #include "cc/debug/frame_viewer_instrumentation.h" | 13 #include "cc/debug/frame_viewer_instrumentation.h" |
14 #include "cc/output/context_provider.h" | 14 #include "cc/output/context_provider.h" |
15 #include "cc/resources/raster_buffer.h" | 15 #include "cc/resources/raster_buffer.h" |
16 #include "cc/resources/raster_source.h" | 16 #include "cc/resources/raster_source.h" |
17 #include "cc/resources/resource.h" | 17 #include "cc/resources/resource.h" |
18 #include "cc/resources/resource_provider.h" | 18 #include "cc/resources/resource_provider.h" |
19 #include "cc/resources/scoped_gpu_raster.h" | 19 #include "cc/resources/scoped_gpu_raster.h" |
20 #include "cc/resources/tile_manager.h" | 20 #include "cc/resources/tile_manager.h" |
21 #include "gpu/command_buffer/client/gles2_interface.h" | 21 #include "gpu/command_buffer/client/gles2_interface.h" |
22 #include "third_party/skia/include/core/SkMultiPictureDraw.h" | 22 #include "third_party/skia/include/core/SkMultiPictureDraw.h" |
23 #include "third_party/skia/include/core/SkPictureRecorder.h" | 23 #include "third_party/skia/include/core/SkPictureRecorder.h" |
24 #include "third_party/skia/include/core/SkSurface.h" | 24 #include "third_party/skia/include/core/SkSurface.h" |
25 #include "third_party/skia/include/gpu/GrContext.h" | 25 #include "third_party/skia/include/gpu/GrContext.h" |
26 | 26 |
27 namespace cc { | 27 namespace cc { |
28 | 28 |
29 // static | |
30 scoped_ptr<GpuRasterizer> GpuRasterizer::Create( | |
31 ContextProvider* context_provider, | |
32 ResourceProvider* resource_provider, | |
33 bool use_distance_field_text, | |
34 bool threaded_gpu_rasterization_enabled, | |
35 int msaa_sample_count) { | |
36 return make_scoped_ptr<GpuRasterizer>(new GpuRasterizer( | |
37 context_provider, resource_provider, use_distance_field_text, | |
38 threaded_gpu_rasterization_enabled, msaa_sample_count)); | |
39 } | |
40 | |
41 GpuRasterizer::GpuRasterizer(ContextProvider* context_provider, | 29 GpuRasterizer::GpuRasterizer(ContextProvider* context_provider, |
42 ResourceProvider* resource_provider, | 30 ResourceProvider* resource_provider, |
43 bool use_distance_field_text, | 31 bool use_distance_field_text, |
44 bool threaded_gpu_rasterization_enabled, | |
45 int msaa_sample_count) | 32 int msaa_sample_count) |
46 : resource_provider_(resource_provider), | 33 : resource_provider_(resource_provider), |
47 use_distance_field_text_(use_distance_field_text), | 34 use_distance_field_text_(use_distance_field_text), |
48 threaded_gpu_rasterization_enabled_(threaded_gpu_rasterization_enabled), | |
49 msaa_sample_count_(msaa_sample_count) { | 35 msaa_sample_count_(msaa_sample_count) { |
50 } | 36 } |
51 | 37 |
52 GpuRasterizer::~GpuRasterizer() { | 38 GpuRasterizer::~GpuRasterizer() { |
53 } | 39 } |
54 | 40 |
55 PrepareTilesMode GpuRasterizer::GetPrepareTilesMode() { | |
56 return threaded_gpu_rasterization_enabled_ | |
57 ? PrepareTilesMode::RASTERIZE_PRIORITIZED_TILES | |
58 : PrepareTilesMode::PREPARE_NONE; | |
59 } | |
60 | |
61 ContextProvider* GpuRasterizer::GetContextProvider(bool worker_context) { | |
62 return worker_context | |
63 ? resource_provider_->output_surface()->worker_context_provider() | |
64 : resource_provider_->output_surface()->context_provider(); | |
65 } | |
66 | |
67 void GpuRasterizer::RasterizeTiles( | |
68 const TileVector& tiles, | |
69 ResourcePool* resource_pool, | |
70 ResourceFormat resource_format, | |
71 const UpdateTileDrawInfoCallback& update_tile_draw_info) { | |
72 ScopedGpuRaster gpu_raster(GetContextProvider(false)); | |
73 | |
74 ScopedResourceWriteLocks locks; | |
75 | |
76 for (Tile* tile : tiles) { | |
77 RasterSource::SolidColorAnalysis analysis; | |
78 | |
79 if (tile->use_picture_analysis()) | |
80 PerformSolidColorAnalysis(tile, &analysis); | |
81 | |
82 scoped_ptr<ScopedResource> resource; | |
83 if (!analysis.is_solid_color) { | |
84 resource = resource_pool->AcquireResource(tile->desired_texture_size(), | |
85 resource_format); | |
86 AddToMultiPictureDraw(tile, resource.get(), &locks); | |
87 } | |
88 update_tile_draw_info.Run(tile, resource.Pass(), analysis); | |
89 } | |
90 | |
91 // If MSAA is enabled, tell Skia to resolve each render target after draw. | |
92 multi_picture_draw_.draw(msaa_sample_count_ > 0); | |
93 } | |
94 | |
95 void GpuRasterizer::RasterizeSource( | 41 void GpuRasterizer::RasterizeSource( |
96 bool use_worker_context, | |
97 ResourceProvider::ScopedWriteLockGr* write_lock, | 42 ResourceProvider::ScopedWriteLockGr* write_lock, |
98 const RasterSource* raster_source, | 43 const RasterSource* raster_source, |
99 const gfx::Rect& rect, | 44 const gfx::Rect& rect, |
100 float scale) { | 45 float scale) { |
101 // Play back raster_source into temp SkPicture. | 46 // Play back raster_source into temp SkPicture. |
102 SkPictureRecorder recorder; | 47 SkPictureRecorder recorder; |
103 gfx::Size size = write_lock->resource()->size; | 48 gfx::Size size = write_lock->resource()->size; |
104 const int flags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; | 49 const int flags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; |
105 skia::RefPtr<SkCanvas> canvas = skia::SharePtr( | 50 skia::RefPtr<SkCanvas> canvas = skia::SharePtr( |
106 recorder.beginRecording(size.width(), size.height(), NULL, flags)); | 51 recorder.beginRecording(size.width(), size.height(), NULL, flags)); |
107 canvas->save(); | 52 canvas->save(); |
108 raster_source->PlaybackToCanvas(canvas.get(), rect, scale); | 53 raster_source->PlaybackToCanvas(canvas.get(), rect, scale); |
109 canvas->restore(); | 54 canvas->restore(); |
110 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); | 55 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); |
111 | 56 |
112 // Turn on distance fields for layers that have ever animated. | 57 // Turn on distance fields for layers that have ever animated. |
113 bool use_distance_field_text = | 58 bool use_distance_field_text = |
114 use_distance_field_text_ || | 59 use_distance_field_text_ || |
115 raster_source->ShouldAttemptToUseDistanceFieldText(); | 60 raster_source->ShouldAttemptToUseDistanceFieldText(); |
116 | 61 |
117 // Playback picture into resource. | 62 // Playback picture into resource. |
118 { | 63 { |
119 ScopedGpuRaster gpu_raster(GetContextProvider(use_worker_context)); | 64 ScopedGpuRaster gpu_raster( |
120 write_lock->InitSkSurface(use_worker_context, use_distance_field_text, | 65 resource_provider_->output_surface()->worker_context_provider()); |
| 66 write_lock->InitSkSurface(use_distance_field_text, |
121 raster_source->CanUseLCDText(), | 67 raster_source->CanUseLCDText(), |
122 msaa_sample_count_); | 68 msaa_sample_count_); |
123 | 69 |
124 SkSurface* sk_surface = write_lock->sk_surface(); | 70 SkSurface* sk_surface = write_lock->sk_surface(); |
125 | 71 |
126 // Allocating an SkSurface will fail after a lost context. Pretend we | 72 // Allocating an SkSurface will fail after a lost context. Pretend we |
127 // rasterized, as the contents of the resource don't matter anymore. | 73 // rasterized, as the contents of the resource don't matter anymore. |
128 if (!sk_surface) | 74 if (!sk_surface) |
129 return; | 75 return; |
130 | 76 |
131 SkMultiPictureDraw multi_picture_draw; | 77 SkMultiPictureDraw multi_picture_draw; |
132 multi_picture_draw.add(sk_surface->getCanvas(), picture.get()); | 78 multi_picture_draw.add(sk_surface->getCanvas(), picture.get()); |
133 multi_picture_draw.draw(msaa_sample_count_ > 0); | 79 multi_picture_draw.draw(msaa_sample_count_ > 0); |
134 write_lock->ReleaseSkSurface(); | 80 write_lock->ReleaseSkSurface(); |
135 } | 81 } |
136 } | 82 } |
137 | 83 |
138 void GpuRasterizer::PerformSolidColorAnalysis( | |
139 const Tile* tile, | |
140 RasterSource::SolidColorAnalysis* analysis) { | |
141 const void* tile_id = static_cast<const void*>(tile); | |
142 frame_viewer_instrumentation::ScopedAnalyzeTask analyze_task( | |
143 tile_id, tile->combined_priority().resolution, | |
144 tile->source_frame_number(), tile->layer_id()); | |
145 | |
146 DCHECK(tile->raster_source()); | |
147 | |
148 tile->raster_source()->PerformSolidColorAnalysis( | |
149 tile->content_rect(), tile->contents_scale(), analysis); | |
150 | |
151 // Record the solid color prediction. | |
152 UMA_HISTOGRAM_BOOLEAN("Renderer4.SolidColorTilesAnalyzed", | |
153 analysis->is_solid_color); | |
154 } | |
155 | |
156 void GpuRasterizer::AddToMultiPictureDraw(const Tile* tile, | |
157 const ScopedResource* resource, | |
158 ScopedResourceWriteLocks* locks) { | |
159 const void* tile_id = static_cast<const void*>(tile); | |
160 frame_viewer_instrumentation::ScopedRasterTask raster_task( | |
161 tile_id, tile->combined_priority().resolution, | |
162 tile->source_frame_number(), tile->layer_id()); | |
163 | |
164 DCHECK(tile->raster_source()); | |
165 | |
166 // Turn on distance fields for layers that have ever animated. | |
167 bool use_distance_field_text = | |
168 use_distance_field_text_ || | |
169 tile->raster_source()->ShouldAttemptToUseDistanceFieldText(); | |
170 scoped_ptr<ResourceProvider::ScopedWriteLockGr> lock( | |
171 new ResourceProvider::ScopedWriteLockGr(resource_provider_, | |
172 resource->id())); | |
173 | |
174 lock->InitSkSurface(false, use_distance_field_text, | |
175 tile->raster_source()->CanUseLCDText(), | |
176 msaa_sample_count_); | |
177 | |
178 SkSurface* sk_surface = lock->sk_surface(); | |
179 | |
180 // Allocating an SkSurface will fail after a lost context. Pretend we | |
181 // rasterized, as the contents of the resource don't matter anymore. | |
182 if (!sk_surface) | |
183 return; | |
184 | |
185 locks->push_back(lock.Pass()); | |
186 | |
187 SkRTreeFactory factory; | |
188 SkPictureRecorder recorder; | |
189 gfx::Size size = resource->size(); | |
190 const int flags = SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; | |
191 skia::RefPtr<SkCanvas> canvas = skia::SharePtr( | |
192 recorder.beginRecording(size.width(), size.height(), &factory, flags)); | |
193 | |
194 canvas->save(); | |
195 tile->raster_source()->PlaybackToCanvas(canvas.get(), tile->content_rect(), | |
196 tile->contents_scale()); | |
197 canvas->restore(); | |
198 | |
199 // Add the canvas and recorded picture to |multi_picture_draw_|. | |
200 skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording()); | |
201 multi_picture_draw_.add(sk_surface->getCanvas(), picture.get()); | |
202 } | |
203 | |
204 } // namespace cc | 84 } // namespace cc |
OLD | NEW |