| scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "picture_utils.h" |
| 9 | #include "CopyTilesRenderer.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkPicture.h" |
| 14 | #include "SkPixelRef.h" |
| 15 | #include "SkRect.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | namespace sk_tools { |
| 19 | CopyTilesRenderer::CopyTilesRenderer(int x, int y) |
| 20 | : fXTilesPerLargeTile(x) |
| 21 | , fYTilesPerLargeTile(y) { |
| 22 | } |
| 23 | void CopyTilesRenderer::init(SkPicture* pict) { |
| 24 | SkASSERT(pict != NULL); |
| 25 | // Only work with absolute widths (as opposed to percentages). |
| 26 | SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0); |
| 27 | fPicture = pict; |
| 28 | fPicture->ref(); |
| 29 | this->buildBBoxHierarchy(); |
| 30 | // In order to avoid allocating a large canvas (particularly important for GPU), create one |
| 31 | // canvas that is a multiple of the tile size, and draw portions of the picture. |
| 32 | fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth(); |
| 33 | fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight(); |
| 34 | fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight)); |
| 35 | } |
| 36 | |
| 37 | bool CopyTilesRenderer::render(const SkString* path) { |
| 38 | int i = 0; |
| 39 | bool success = true; |
| 40 | SkBitmap dst; |
| 41 | for (int x = 0; x < fPicture->width(); x += fLargeTileWidth) { |
| 42 | for (int y = 0; y < fPicture->height(); y += fLargeTileHeight) { |
| 43 | SkAutoCanvasRestore autoRestore(fCanvas, true); |
| 44 | fCanvas->translate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 45 | // Draw the picture |
| 46 | fCanvas->drawPicture(*fPicture); |
| 47 | // Now extract the picture into tiles |
| 48 | const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false); |
| 49 | SkIRect subset; |
| 50 | for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) { |
| 51 | for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) { |
| 52 | subset.set(tileX, tileY, tileX + this->getTileWidth(), |
| 53 | tileY + this->getTileHeight()); |
| 54 | SkDEBUGCODE(bool extracted =) |
| 55 | baseBitmap.extractSubset(&dst, subset); |
| 56 | SkASSERT(extracted); |
| 57 | if (path != NULL) { |
| 58 | // Similar to writeAppendNumber in PictureRenderer.cpp, but just encodes |
| 59 | // a bitmap directly. |
| 60 | SkString pathWithNumber(*path); |
| 61 | pathWithNumber.appendf("%i.png", i++); |
| 62 | SkBitmap copy; |
| 63 | #if SK_SUPPORT_GPU |
| 64 | if (isUsingGpuDevice()) { |
| 65 | dst.pixelRef()->readPixels(©, &subset); |
| 66 | } else { |
| 67 | #endif |
| 68 | dst.copyTo(©, dst.config()); |
| 69 | #if SK_SUPPORT_GPU |
| 70 | } |
| 71 | #endif |
| 72 | success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy, |
| 73 | SkImageEncoder::kPNG_Type, 100); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return success; |
| 80 | } |
| 81 | |
| 82 | SkString CopyTilesRenderer::getConfigNameInternal() { |
| 83 | return SkString("copy_tiles"); |
| 84 | } |
| 85 | } |