| Index: gpu/skia_runner/sk_picture_rasterizer.cc
|
| diff --git a/gpu/skia_runner/sk_picture_rasterizer.cc b/gpu/skia_runner/sk_picture_rasterizer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..e5ffc1de0041ed6b38bede365e8b81025c376133
|
| --- /dev/null
|
| +++ b/gpu/skia_runner/sk_picture_rasterizer.cc
|
| @@ -0,0 +1,101 @@
|
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "gpu/skia_runner/sk_picture_rasterizer.h"
|
| +
|
| +#include <iostream>
|
| +
|
| +#include "third_party/skia/include/core/SkCanvas.h"
|
| +#include "third_party/skia/include/core/SkSurface.h"
|
| +#include "third_party/skia/include/core/SkSurfaceProps.h"
|
| +
|
| +namespace skia_runner {
|
| +
|
| +SkPictureRasterizer::SkPictureRasterizer(skia::RefPtr<GrContext> gr_context,
|
| + int max_texture_size)
|
| + : use_lcd_text_(false),
|
| + use_distance_field_text_(false),
|
| + msaa_sample_count_(0),
|
| + max_texture_size_(max_texture_size),
|
| + gr_context_(gr_context) {
|
| +}
|
| +
|
| +SkPictureRasterizer::~SkPictureRasterizer() {
|
| +}
|
| +
|
| +SkBitmap SkPictureRasterizer::RasterizeTile(const SkPicture* picture,
|
| + const SkRect& rect) const {
|
| + DCHECK(gr_context_);
|
| +
|
| + GrSurfaceDesc desc;
|
| + desc.fFlags = kRenderTarget_GrSurfaceFlag;
|
| + desc.fWidth = rect.width();
|
| + desc.fHeight = rect.height();
|
| + desc.fConfig = kBGRA_8888_GrPixelConfig;
|
| + desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
| + desc.fSampleCnt = msaa_sample_count_;
|
| + DCHECK(desc.fSampleCnt == 0 || desc.fSampleCnt == 2 || desc.fSampleCnt == 4 ||
|
| + desc.fSampleCnt == 8 || desc.fSampleCnt == 16);
|
| +
|
| + skia::RefPtr<GrTexture> gr_texture(skia::AdoptRef(
|
| + gr_context_->textureProvider()->createTexture(desc, false, nullptr, 0)));
|
| +
|
| + if (gr_texture) {
|
| + uint32_t flags = use_distance_field_text_
|
| + ? SkSurfaceProps::kUseDistanceFieldFonts_Flag
|
| + : 0;
|
| + SkSurfaceProps surface_props =
|
| + use_lcd_text_
|
| + ? SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType)
|
| + : SkSurfaceProps(flags, kUnknown_SkPixelGeometry);
|
| +
|
| + skia::RefPtr<SkSurface> sk_surface(
|
| + skia::AdoptRef(SkSurface::NewRenderTargetDirect(
|
| + gr_texture->asRenderTarget(), &surface_props)));
|
| + SkCanvas* canvas = sk_surface->getCanvas();
|
| + canvas->translate(-rect.left(), -rect.top());
|
| + canvas->drawPicture(picture);
|
| +
|
| + SkBitmap bitmap;
|
| + bitmap.setInfo(canvas->imageInfo());
|
| + if (canvas->readPixels(&bitmap, 0, 0)) {
|
| + return bitmap;
|
| + }
|
| + }
|
| + return SkBitmap();
|
| +}
|
| +
|
| +SkBitmap SkPictureRasterizer::Rasterize(const SkPicture* picture) const {
|
| + if (!gr_context_)
|
| + return SkBitmap();
|
| +
|
| + SkRect picture_rect = picture->cullRect();
|
| + if (picture_rect.width() <= max_texture_size_ &&
|
| + picture_rect.height() <= max_texture_size_)
|
| + return RasterizeTile(picture, picture_rect);
|
| +
|
| + // If the picture doesn't fit within a single texture, we need to generate
|
| + // tiles and assemble into a final bitmap.
|
| + SkBitmap bitmap;
|
| + bitmap.allocN32Pixels(picture_rect.width(), picture_rect.height());
|
| + SkCanvas canvas(bitmap);
|
| + int num_tiles_x = picture_rect.width() / max_texture_size_ + 1;
|
| + int num_tiles_y = picture_rect.height() / max_texture_size_ + 1;
|
| + for (int y = 0; y < num_tiles_y; ++y) {
|
| + SkRect tile_rect;
|
| + tile_rect.fTop = picture_rect.top() + y * max_texture_size_;
|
| + tile_rect.fBottom =
|
| + std::min(tile_rect.fTop + max_texture_size_, picture_rect.bottom());
|
| + for (int x = 0; x < num_tiles_x; ++x) {
|
| + tile_rect.fLeft = picture_rect.left() + x * max_texture_size_;
|
| + tile_rect.fRight =
|
| + std::min(tile_rect.fLeft + max_texture_size_, picture_rect.right());
|
| + SkBitmap tile = RasterizeTile(picture, tile_rect);
|
| + canvas.drawBitmap(tile, tile_rect.left(), tile_rect.top());
|
| + }
|
| + }
|
| + return bitmap;
|
| +}
|
| +
|
| +} // namepsace skia_runner
|
|
|