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 : context_provider_(context_provider
),
47 resource_provider_(resource_provider
),
48 use_distance_field_text_(use_distance_field_text
),
49 threaded_gpu_rasterization_enabled_(threaded_gpu_rasterization_enabled
),
50 msaa_sample_count_(msaa_sample_count
) {
51 DCHECK(context_provider_
);
54 GpuRasterizer::~GpuRasterizer() {
57 PrepareTilesMode
GpuRasterizer::GetPrepareTilesMode() {
58 return PrepareTilesMode::PREPARE_NONE
;
61 void GpuRasterizer::RasterizeTiles(
62 const TileVector
& tiles
,
63 ResourcePool
* resource_pool
,
64 ResourceFormat resource_format
,
65 const UpdateTileDrawInfoCallback
& update_tile_draw_info
) {
66 ScopedGpuRaster
gpu_raster(context_provider_
);
68 ScopedResourceWriteLocks locks
;
70 for (Tile
* tile
: tiles
) {
71 RasterSource::SolidColorAnalysis analysis
;
73 if (tile
->use_picture_analysis())
74 PerformSolidColorAnalysis(tile
, &analysis
);
76 scoped_ptr
<ScopedResource
> resource
;
77 if (!analysis
.is_solid_color
) {
78 resource
= resource_pool
->AcquireResource(tile
->desired_texture_size(),
80 AddToMultiPictureDraw(tile
, resource
.get(), &locks
);
82 update_tile_draw_info
.Run(tile
, resource
.Pass(), analysis
);
85 // If MSAA is enabled, tell Skia to resolve each render target after draw.
86 multi_picture_draw_
.draw(msaa_sample_count_
> 0);
89 void GpuRasterizer::PerformSolidColorAnalysis(
91 RasterSource::SolidColorAnalysis
* analysis
) {
92 const void* tile_id
= static_cast<const void*>(tile
);
93 frame_viewer_instrumentation::ScopedAnalyzeTask
analyze_task(
94 tile_id
, tile
->combined_priority().resolution
,
95 tile
->source_frame_number(), tile
->layer_id());
97 DCHECK(tile
->raster_source());
99 tile
->raster_source()->PerformSolidColorAnalysis(
100 tile
->content_rect(), tile
->contents_scale(), analysis
);
102 // Record the solid color prediction.
103 UMA_HISTOGRAM_BOOLEAN("Renderer4.SolidColorTilesAnalyzed",
104 analysis
->is_solid_color
);
107 void GpuRasterizer::AddToMultiPictureDraw(const Tile
* tile
,
108 const ScopedResource
* resource
,
109 ScopedResourceWriteLocks
* locks
) {
110 const void* tile_id
= static_cast<const void*>(tile
);
111 frame_viewer_instrumentation::ScopedRasterTask
raster_task(
112 tile_id
, tile
->combined_priority().resolution
,
113 tile
->source_frame_number(), tile
->layer_id());
115 DCHECK(tile
->raster_source());
117 // Turn on distance fields for layers that have ever animated.
118 bool use_distance_field_text
=
119 use_distance_field_text_
||
120 tile
->raster_source()->ShouldAttemptToUseDistanceFieldText();
121 scoped_ptr
<ResourceProvider::ScopedWriteLockGr
> lock(
122 new ResourceProvider::ScopedWriteLockGr(
123 resource_provider_
, resource
->id(), use_distance_field_text
,
124 tile
->raster_source()->CanUseLCDText(), msaa_sample_count_
));
126 SkSurface
* sk_surface
= lock
->get_sk_surface();
130 locks
->push_back(lock
.Pass());
132 SkRTreeFactory factory
;
133 SkPictureRecorder recorder
;
134 gfx::Size size
= resource
->size();
135 const int flags
= SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
;
136 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
137 recorder
.beginRecording(size
.width(), size
.height(), &factory
, flags
));
140 tile
->raster_source()->PlaybackToCanvas(canvas
.get(), tile
->content_rect(),
141 tile
->contents_scale());
144 // Add the canvas and recorded picture to |multi_picture_draw_|.
145 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
146 multi_picture_draw_
.add(sk_surface
->getCanvas(), picture
.get());