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/debug/trace_event.h"
11 #include "base/metrics/histogram.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 tile_prepare_enabled
) {
35 return make_scoped_ptr
<GpuRasterizer
>(
36 new GpuRasterizer(context_provider
, resource_provider
,
37 use_distance_field_text
, tile_prepare_enabled
));
40 GpuRasterizer::GpuRasterizer(ContextProvider
* context_provider
,
41 ResourceProvider
* resource_provider
,
42 bool use_distance_field_text
,
43 bool tile_prepare_enabled
)
44 : context_provider_(context_provider
),
45 resource_provider_(resource_provider
),
46 use_distance_field_text_(use_distance_field_text
),
47 tile_prepare_enabled_(tile_prepare_enabled
) {
48 DCHECK(context_provider_
);
51 GpuRasterizer::~GpuRasterizer() {
54 PrepareTilesMode
GpuRasterizer::GetPrepareTilesMode() {
55 return tile_prepare_enabled_
? PrepareTilesMode::PREPARE_PRIORITIZED_TILES
56 : PrepareTilesMode::PREPARE_NONE
;
59 void GpuRasterizer::RasterizeTiles(
60 const TileVector
& tiles
,
61 ResourcePool
* resource_pool
,
62 const UpdateTileDrawInfoCallback
& update_tile_draw_info
) {
63 ScopedGpuRaster
gpu_raster(context_provider_
);
65 ScopedResourceWriteLocks locks
;
67 for (Tile
* tile
: tiles
) {
68 // TODO(hendrikw): Don't create resources for solid color tiles.
69 // See crbug.com/445919
70 scoped_ptr
<ScopedResource
> resource
=
71 resource_pool
->AcquireResource(tile
->desired_texture_size());
72 const ScopedResource
* const_resource
= resource
.get();
74 RasterSource::SolidColorAnalysis analysis
;
76 if (tile
->use_picture_analysis())
77 PerformSolidColorAnalysis(tile
, &analysis
);
79 if (!analysis
.is_solid_color
)
80 AddToMultiPictureDraw(tile
, const_resource
, &locks
);
82 update_tile_draw_info
.Run(tile
, resource
.Pass(), analysis
);
85 multi_picture_draw_
.draw();
88 void GpuRasterizer::PerformSolidColorAnalysis(
90 RasterSource::SolidColorAnalysis
* analysis
) {
91 const void* tile_id
= static_cast<const void*>(tile
);
92 frame_viewer_instrumentation::ScopedAnalyzeTask
analyze_task(
93 tile_id
, tile
->combined_priority().resolution
,
94 tile
->source_frame_number(), tile
->layer_id());
96 DCHECK(tile
->raster_source());
98 tile
->raster_source()->PerformSolidColorAnalysis(
99 tile
->content_rect(), tile
->contents_scale(), analysis
);
101 // Record the solid color prediction.
102 UMA_HISTOGRAM_BOOLEAN("Renderer4.SolidColorTilesAnalyzed",
103 analysis
->is_solid_color
);
106 void GpuRasterizer::AddToMultiPictureDraw(const Tile
* tile
,
107 const ScopedResource
* resource
,
108 ScopedResourceWriteLocks
* locks
) {
109 const void* tile_id
= static_cast<const void*>(tile
);
110 frame_viewer_instrumentation::ScopedRasterTask
raster_task(
111 tile_id
, tile
->combined_priority().resolution
,
112 tile
->source_frame_number(), tile
->layer_id());
114 DCHECK(tile
->raster_source());
116 // Turn on distance fields for layers that have ever animated.
117 bool use_distance_field_text
=
118 use_distance_field_text_
||
119 tile
->raster_source()->ShouldAttemptToUseDistanceFieldText();
120 scoped_ptr
<ResourceProvider::ScopedWriteLockGr
> lock(
121 new ResourceProvider::ScopedWriteLockGr(resource_provider_
,
123 SkSurface
* sk_surface
= lock
->GetSkSurface(
124 use_distance_field_text
, tile
->raster_source()->CanUseLCDText());
126 locks
->push_back(lock
.Pass());
131 SkRTreeFactory factory
;
132 SkPictureRecorder recorder
;
133 gfx::Size size
= resource
->size();
134 const int flags
= SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
;
135 skia::RefPtr
<SkCanvas
> canvas
= skia::SharePtr(
136 recorder
.beginRecording(size
.width(), size
.height(), &factory
, flags
));
139 tile
->raster_source()->PlaybackToCanvas(canvas
.get(), tile
->content_rect(),
140 tile
->contents_scale());
143 // Add the canvas and recorded picture to |multi_picture_draw_|.
144 skia::RefPtr
<SkPicture
> picture
= skia::AdoptRef(recorder
.endRecording());
145 multi_picture_draw_
.add(sk_surface
->getCanvas(), picture
.get());