1 // Copyright (c) 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 "gpu/skia_runner/sk_picture_rasterizer.h"
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkSurface.h"
11 #include "third_party/skia/include/core/SkSurfaceProps.h"
13 namespace skia_runner
{
15 SkPictureRasterizer::SkPictureRasterizer(skia::RefPtr
<GrContext
> gr_context
,
17 : use_lcd_text_(false),
18 use_distance_field_text_(false),
19 msaa_sample_count_(0),
20 max_texture_size_(max_texture_size
),
21 gr_context_(gr_context
) {
24 SkPictureRasterizer::~SkPictureRasterizer() {
27 skia::RefPtr
<SkImage
> SkPictureRasterizer::RasterizeTile(
28 const SkPicture
* picture
,
29 const SkRect
& rect
) const {
32 SkImageInfo info
= SkImageInfo::MakeN32Premul(rect
.width(), rect
.height());
34 if (use_distance_field_text_
)
35 flags
= SkSurfaceProps::kUseDistanceFieldFonts_Flag
;
37 SkSurfaceProps surface_props
=
39 ? SkSurfaceProps(flags
, SkSurfaceProps::kLegacyFontHost_InitType
)
40 : SkSurfaceProps(flags
, kUnknown_SkPixelGeometry
);
42 skia::RefPtr
<SkSurface
> sk_surface(skia::AdoptRef(
43 SkSurface::NewRenderTarget(gr_context_
.get(), SkSurface::kYes_Budgeted
,
44 info
, msaa_sample_count_
, &surface_props
)));
46 SkCanvas
* canvas
= sk_surface
->getCanvas();
47 canvas
->translate(-rect
.left(), -rect
.top());
48 canvas
->drawPicture(picture
);
50 return skia::AdoptRef(sk_surface
->newImageSnapshot());
55 skia::RefPtr
<SkImage
> SkPictureRasterizer::Rasterize(
56 const SkPicture
* picture
) const {
60 SkRect picture_rect
= picture
->cullRect();
61 if (picture_rect
.width() <= max_texture_size_
&&
62 picture_rect
.height() <= max_texture_size_
)
63 return RasterizeTile(picture
, picture_rect
);
66 SkImageInfo::MakeN32Premul(picture_rect
.width(), picture_rect
.height());
68 skia::RefPtr
<SkSurface
> sk_surface(
69 skia::AdoptRef(SkSurface::NewRaster(info
)));
70 SkCanvas
* canvas
= sk_surface
->getCanvas();
72 int num_tiles_x
= picture_rect
.width() / max_texture_size_
+ 1;
73 int num_tiles_y
= picture_rect
.height() / max_texture_size_
+ 1;
74 for (int y
= 0; y
< num_tiles_y
; ++y
) {
76 tile_rect
.fTop
= picture_rect
.top() + y
* max_texture_size_
;
78 std::min(tile_rect
.fTop
+ max_texture_size_
, picture_rect
.bottom());
79 for (int x
= 0; x
< num_tiles_x
; ++x
) {
80 tile_rect
.fLeft
= picture_rect
.left() + x
* max_texture_size_
;
82 std::min(tile_rect
.fLeft
+ max_texture_size_
, picture_rect
.right());
83 skia::RefPtr
<SkImage
> tile(RasterizeTile(picture
, tile_rect
));
84 canvas
->drawImage(tile
.get(), tile_rect
.left(), tile_rect
.top());
87 return skia::AdoptRef(sk_surface
->newImageSnapshot());
90 } // namepsace skia_runner