Roll src/third_party/skia e66fec2:2bd5d02
[chromium-blink-merge.git] / gpu / skia_runner / sk_picture_rasterizer.cc
blob7087a1369bd0b705218c1edcff9122dbc2a7630a
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"
7 #include <iostream>
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,
16 int max_texture_size)
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 {
30 DCHECK(gr_context_);
32 SkImageInfo info = SkImageInfo::MakeN32Premul(rect.width(), rect.height());
33 uint32_t flags = 0;
34 if (use_distance_field_text_)
35 flags = SkSurfaceProps::kUseDistanceFieldFonts_Flag;
37 SkSurfaceProps surface_props =
38 use_lcd_text_
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)));
45 if (sk_surface) {
46 SkCanvas* canvas = sk_surface->getCanvas();
47 canvas->translate(-rect.left(), -rect.top());
48 canvas->drawPicture(picture);
50 return skia::AdoptRef(sk_surface->newImageSnapshot());
52 return nullptr;
55 skia::RefPtr<SkImage> SkPictureRasterizer::Rasterize(
56 const SkPicture* picture) const {
57 if (!gr_context_)
58 return nullptr;
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);
65 SkImageInfo info =
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) {
75 SkRect tile_rect;
76 tile_rect.fTop = picture_rect.top() + y * max_texture_size_;
77 tile_rect.fBottom =
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_;
81 tile_rect.fRight =
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