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
3 // found in the LICENSE file.
5 #include "cc/resources/gpu_rasterizer.h"
10 #include "base/metrics/histogram.h"
11 #include "base/trace_event/trace_event.h"
12 #include "cc/debug/devtools_instrumentation.h"
13 #include "cc/debug/frame_viewer_instrumentation.h"
14 #include "cc/output/context_provider.h"
15 #include "cc/resources/raster_buffer.h"
16 #include "cc/resources/raster_source.h"
17 #include "cc/resources/resource.h"
18 #include "cc/resources/resource_provider.h"
19 #include "cc/resources/scoped_gpu_raster.h"
20 #include "cc/resources/tile_manager.h"
21 #include "gpu/command_buffer/client/gles2_interface.h"
22 #include "third_party/skia/include/core/SkMultiPictureDraw.h"
23 #include "third_party/skia/include/core/SkPictureRecorder.h"
24 #include "third_party/skia/include/core/SkSurface.h"
25 #include "third_party/skia/include/gpu/GrContext.h"
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
));
41 GpuRasterizer::GpuRasterizer(ContextProvider
* context_provider
,
42 ResourceProvider
* resource_provider
,
43 bool use_distance_field_text
,
44 bool threaded_gpu_rasterization_enabled
,
45 int msaa_sample_count
)
46 : resource_provider_(resource_provider
),
47 use_distance_field_text_(use_distance_field_text
),
48 threaded_gpu_rasterization_enabled_(threaded_gpu_rasterization_enabled
),
49 msaa_sample_count_(msaa_sample_count
) {
52 GpuRasterizer::~GpuRasterizer() {
55 PrepareTilesMode
GpuRasterizer::GetPrepareTilesMode() {
56 return threaded_gpu_rasterization_enabled_
57 ? PrepareTilesMode::RASTERIZE_PRIORITIZED_TILES
58 : PrepareTilesMode::PREPARE_NONE
;
61 ContextProvider
* GpuRasterizer::GetContextProvider(bool worker_context
) {
63 ? resource_provider_
->output_surface()->worker_context_provider()
64 : resource_provider_
->output_surface()->context_provider();
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));
74 ScopedResourceWriteLocks locks
;
76 for (Tile
* tile
: tiles
) {
77 RasterSource::SolidColorAnalysis analysis
;
79 if (tile
->use_picture_analysis())
80 PerformSolidColorAnalysis(tile
, &analysis
);
82 scoped_ptr
<ScopedResource
> resource
;
83 if (!analysis
.is_solid_color
) {
84 resource
= resource_pool
->AcquireResource(tile
->desired_texture_size(),
86 AddToMultiPictureDraw(tile
, resource
.get(), &locks
);
88 update_tile_draw_info
.Run(tile
, resource
.Pass(), analysis
);
91 // If MSAA is enabled, tell Skia to resolve each render target after draw.
92 multi_picture_draw_
.draw(msaa_sample_count_
> 0);
95 void GpuRasterizer::RasterizeSource(
96 bool use_worker_context
,
97 ResourceProvider::ScopedWriteLockGr
* write_lock
,
98 const RasterSource
* raster_source
,
99 const gfx::Rect
& rect
,
101 // Play back raster_source into temp SkPicture.
102 SkPictureRecorder recorder
;
103 gfx::Size size
= write_lock
->resource()->size
;
104 const int flags
= SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
;
105 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
106 recorder
.beginRecording(size
.width(), size
.height(), NULL
, flags
));
108 raster_source
->PlaybackToCanvas(canvas
.get(), rect
, scale
);
110 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
112 // Turn on distance fields for layers that have ever animated.
113 bool use_distance_field_text
=
114 use_distance_field_text_
||
115 raster_source
->ShouldAttemptToUseDistanceFieldText();
117 // Playback picture into resource.
119 ScopedGpuRaster
gpu_raster(GetContextProvider(use_worker_context
));
120 write_lock
->InitSkSurface(use_worker_context
, use_distance_field_text
,
121 raster_source
->CanUseLCDText(),
123 SkMultiPictureDraw multi_picture_draw
;
124 multi_picture_draw
.add(write_lock
->sk_surface()->getCanvas(),
126 multi_picture_draw
.draw(msaa_sample_count_
> 0);
127 write_lock
->ReleaseSkSurface();
131 void GpuRasterizer::PerformSolidColorAnalysis(
133 RasterSource::SolidColorAnalysis
* analysis
) {
134 const void* tile_id
= static_cast<const void*>(tile
);
135 frame_viewer_instrumentation::ScopedAnalyzeTask
analyze_task(
136 tile_id
, tile
->combined_priority().resolution
,
137 tile
->source_frame_number(), tile
->layer_id());
139 DCHECK(tile
->raster_source());
141 tile
->raster_source()->PerformSolidColorAnalysis(
142 tile
->content_rect(), tile
->contents_scale(), analysis
);
144 // Record the solid color prediction.
145 UMA_HISTOGRAM_BOOLEAN("Renderer4.SolidColorTilesAnalyzed",
146 analysis
->is_solid_color
);
149 void GpuRasterizer::AddToMultiPictureDraw(const Tile
* tile
,
150 const ScopedResource
* resource
,
151 ScopedResourceWriteLocks
* locks
) {
152 const void* tile_id
= static_cast<const void*>(tile
);
153 frame_viewer_instrumentation::ScopedRasterTask
raster_task(
154 tile_id
, tile
->combined_priority().resolution
,
155 tile
->source_frame_number(), tile
->layer_id());
157 DCHECK(tile
->raster_source());
159 // Turn on distance fields for layers that have ever animated.
160 bool use_distance_field_text
=
161 use_distance_field_text_
||
162 tile
->raster_source()->ShouldAttemptToUseDistanceFieldText();
163 scoped_ptr
<ResourceProvider::ScopedWriteLockGr
> lock(
164 new ResourceProvider::ScopedWriteLockGr(resource_provider_
,
167 lock
->InitSkSurface(false, use_distance_field_text
,
168 tile
->raster_source()->CanUseLCDText(),
171 SkSurface
* sk_surface
= lock
->sk_surface();
175 locks
->push_back(lock
.Pass());
177 SkRTreeFactory factory
;
178 SkPictureRecorder recorder
;
179 gfx::Size size
= resource
->size();
180 const int flags
= SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
;
181 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
182 recorder
.beginRecording(size
.width(), size
.height(), &factory
, flags
));
185 tile
->raster_source()->PlaybackToCanvas(canvas
.get(), tile
->content_rect(),
186 tile
->contents_scale());
189 // Add the canvas and recorded picture to |multi_picture_draw_|.
190 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
191 multi_picture_draw_
.add(sk_surface
->getCanvas(), picture
.get());