OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "gpu/skia_runner/sk_picture_rasterizer.h" | |
6 | |
7 #include <iostream> | |
8 | |
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" | |
12 | |
13 namespace skia_runner { | |
14 | |
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) { | |
22 } | |
23 | |
24 SkPictureRasterizer::~SkPictureRasterizer() { | |
25 } | |
26 | |
27 skia::RefPtr<SkImage> SkPictureRasterizer::RasterizeTile( | |
28 const SkPicture* picture, | |
29 const SkRect& rect) const { | |
30 DCHECK(gr_context_); | |
31 | |
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; | |
36 | |
37 SkSurfaceProps surface_props = | |
38 use_lcd_text_ | |
39 ? SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType) | |
40 : SkSurfaceProps(flags, kUnknown_SkPixelGeometry); | |
41 | |
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); | |
49 | |
50 return skia::AdoptRef(sk_surface->newImageSnapshot()); | |
51 } | |
52 return nullptr; | |
53 } | |
54 | |
55 skia::RefPtr<SkImage> SkPictureRasterizer::Rasterize( | |
56 const SkPicture* picture) const { | |
57 if (!gr_context_) | |
58 return nullptr; | |
59 | |
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); | |
64 | |
65 SkImageInfo info = | |
66 SkImageInfo::MakeN32Premul(picture_rect.width(), picture_rect.height()); | |
67 | |
68 skia::RefPtr<SkSurface> sk_surface( | |
69 skia::AdoptRef(SkSurface::NewRaster(info))); | |
70 SkCanvas* canvas = sk_surface->getCanvas(); | |
71 | |
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.drawBitmap(tile, tile_rect.left(), tile_rect.top()); | |
85 canvas->drawImage(tile.get(), tile_rect.left(), tile_rect.top()); | |
86 } | |
87 } | |
88 return skia::AdoptRef(sk_surface->newImageSnapshot()); | |
89 } | |
90 | |
91 } // namepsace skia_runner | |
OLD | NEW |