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 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 37 | bool CopyTilesRenderer::render(const SkString* path, SkBitmap** out) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 38 | int i = 0; |
| 39 | bool success = true; |
| 40 | SkBitmap dst; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 41 | for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) { |
| 42 | for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 43 | SkAutoCanvasRestore autoRestore(fCanvas, true); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 44 | // Translate so that we draw the correct portion of the picture. |
| 45 | // Perform a postTranslate so that the scaleFactor does not interfere with the |
| 46 | // positioning. |
| 47 | SkMatrix mat(fCanvas->getTotalMatrix()); |
| 48 | mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 49 | fCanvas->setMatrix(mat); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 50 | // Draw the picture |
| 51 | fCanvas->drawPicture(*fPicture); |
| 52 | // Now extract the picture into tiles |
| 53 | const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false); |
| 54 | SkIRect subset; |
| 55 | for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) { |
| 56 | for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) { |
| 57 | subset.set(tileX, tileY, tileX + this->getTileWidth(), |
| 58 | tileY + this->getTileHeight()); |
| 59 | SkDEBUGCODE(bool extracted =) |
| 60 | baseBitmap.extractSubset(&dst, subset); |
| 61 | SkASSERT(extracted); |
| 62 | if (path != NULL) { |
| 63 | // Similar to writeAppendNumber in PictureRenderer.cpp, but just encodes |
| 64 | // a bitmap directly. |
| 65 | SkString pathWithNumber(*path); |
| 66 | pathWithNumber.appendf("%i.png", i++); |
| 67 | SkBitmap copy; |
| 68 | #if SK_SUPPORT_GPU |
| 69 | if (isUsingGpuDevice()) { |
| 70 | dst.pixelRef()->readPixels(©, &subset); |
| 71 | } else { |
| 72 | #endif |
| 73 | dst.copyTo(©, dst.config()); |
| 74 | #if SK_SUPPORT_GPU |
| 75 | } |
| 76 | #endif |
| 77 | success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy, |
| 78 | SkImageEncoder::kPNG_Type, 100); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return success; |
| 85 | } |
| 86 | |
| 87 | SkString CopyTilesRenderer::getConfigNameInternal() { |
| 88 | return SkString("copy_tiles"); |
| 89 | } |
| 90 | } |