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 SkBitmap SkPictureRasterizer::RasterizeTile(const SkPicture* picture, |
| 28 const SkRect& rect) const { |
| 29 DCHECK(gr_context_); |
| 30 |
| 31 GrSurfaceDesc desc; |
| 32 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 33 desc.fWidth = rect.width(); |
| 34 desc.fHeight = rect.height(); |
| 35 desc.fConfig = kBGRA_8888_GrPixelConfig; |
| 36 desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 37 desc.fSampleCnt = msaa_sample_count_; |
| 38 DCHECK(desc.fSampleCnt == 0 || desc.fSampleCnt == 2 || desc.fSampleCnt == 4 || |
| 39 desc.fSampleCnt == 8 || desc.fSampleCnt == 16); |
| 40 |
| 41 skia::RefPtr<GrTexture> gr_texture(skia::AdoptRef( |
| 42 gr_context_->textureProvider()->createTexture(desc, false, nullptr, 0))); |
| 43 |
| 44 if (gr_texture) { |
| 45 uint32_t flags = use_distance_field_text_ |
| 46 ? SkSurfaceProps::kUseDistanceFieldFonts_Flag |
| 47 : 0; |
| 48 SkSurfaceProps surface_props = |
| 49 use_lcd_text_ |
| 50 ? SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType) |
| 51 : SkSurfaceProps(flags, kUnknown_SkPixelGeometry); |
| 52 |
| 53 skia::RefPtr<SkSurface> sk_surface( |
| 54 skia::AdoptRef(SkSurface::NewRenderTargetDirect( |
| 55 gr_texture->asRenderTarget(), &surface_props))); |
| 56 SkCanvas* canvas = sk_surface->getCanvas(); |
| 57 canvas->translate(-rect.left(), -rect.top()); |
| 58 canvas->drawPicture(picture); |
| 59 |
| 60 SkBitmap bitmap; |
| 61 bitmap.setInfo(canvas->imageInfo()); |
| 62 if (canvas->readPixels(&bitmap, 0, 0)) { |
| 63 return bitmap; |
| 64 } |
| 65 } |
| 66 return SkBitmap(); |
| 67 } |
| 68 |
| 69 SkBitmap SkPictureRasterizer::Rasterize(const SkPicture* picture) const { |
| 70 if (!gr_context_) |
| 71 return SkBitmap(); |
| 72 |
| 73 SkRect picture_rect = picture->cullRect(); |
| 74 if (picture_rect.width() <= max_texture_size_ && |
| 75 picture_rect.height() <= max_texture_size_) |
| 76 return RasterizeTile(picture, picture_rect); |
| 77 |
| 78 // If the picture doesn't fit within a single texture, we need to generate |
| 79 // tiles and assemble into a final bitmap. |
| 80 SkBitmap bitmap; |
| 81 bitmap.allocN32Pixels(picture_rect.width(), picture_rect.height()); |
| 82 SkCanvas canvas(bitmap); |
| 83 int num_tiles_x = picture_rect.width() / max_texture_size_ + 1; |
| 84 int num_tiles_y = picture_rect.height() / max_texture_size_ + 1; |
| 85 for (int y = 0; y < num_tiles_y; ++y) { |
| 86 SkRect tile_rect; |
| 87 tile_rect.fTop = picture_rect.top() + y * max_texture_size_; |
| 88 tile_rect.fBottom = |
| 89 std::min(tile_rect.fTop + max_texture_size_, picture_rect.bottom()); |
| 90 for (int x = 0; x < num_tiles_x; ++x) { |
| 91 tile_rect.fLeft = picture_rect.left() + x * max_texture_size_; |
| 92 tile_rect.fRight = |
| 93 std::min(tile_rect.fLeft + max_texture_size_, picture_rect.right()); |
| 94 SkBitmap tile = RasterizeTile(picture, tile_rect); |
| 95 canvas.drawBitmap(tile, tile_rect.left(), tile_rect.top()); |
| 96 } |
| 97 } |
| 98 return bitmap; |
| 99 } |
| 100 |
| 101 } // namepsace skia_runner |
OLD | NEW |