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 | } |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 23 | void CopyTilesRenderer::init(SkPicture* pict, const SkString* outputDir, |
| 24 | const SkString* inputFilename, bool useChecksumBasedFilenames) { |
| 25 | // Do not call INHERITED::init(), which would create a (potentially large) canvas which is |
| 26 | // not used by bench_pictures. |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 27 | SkASSERT(pict != NULL); |
| 28 | // Only work with absolute widths (as opposed to percentages). |
| 29 | SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame^] | 30 | fPicture.reset(pict)->ref(); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 31 | this->CopyString(&fOutputDir, outputDir); |
| 32 | this->CopyString(&fInputFilename, inputFilename); |
| 33 | fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 34 | this->buildBBoxHierarchy(); |
| 35 | // In order to avoid allocating a large canvas (particularly important for GPU), create one |
| 36 | // canvas that is a multiple of the tile size, and draw portions of the picture. |
| 37 | fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth(); |
| 38 | fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight(); |
| 39 | fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight)); |
| 40 | } |
| 41 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 42 | bool CopyTilesRenderer::render(SkBitmap** out) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 43 | int i = 0; |
| 44 | bool success = true; |
| 45 | SkBitmap dst; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 46 | for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) { |
| 47 | for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) { |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 48 | SkAutoCanvasRestore autoRestore(fCanvas, true); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 49 | // Translate so that we draw the correct portion of the picture. |
| 50 | // Perform a postTranslate so that the scaleFactor does not interfere with the |
| 51 | // positioning. |
| 52 | SkMatrix mat(fCanvas->getTotalMatrix()); |
| 53 | mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 54 | fCanvas->setMatrix(mat); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 55 | // Draw the picture |
| 56 | fCanvas->drawPicture(*fPicture); |
| 57 | // Now extract the picture into tiles |
| 58 | const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false); |
| 59 | SkIRect subset; |
| 60 | for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) { |
| 61 | for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) { |
| 62 | subset.set(tileX, tileY, tileX + this->getTileWidth(), |
| 63 | tileY + this->getTileHeight()); |
| 64 | SkDEBUGCODE(bool extracted =) |
| 65 | baseBitmap.extractSubset(&dst, subset); |
| 66 | SkASSERT(extracted); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 67 | if (!fOutputDir.isEmpty()) { |
| 68 | // Similar to write() in PictureRenderer.cpp, but just encodes |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 69 | // a bitmap directly. |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 70 | // TODO: Share more common code with write() to do this, to properly |
| 71 | // write out the JSON summary, etc. |
| 72 | SkString pathWithNumber; |
| 73 | make_filepath(&pathWithNumber, fOutputDir, fInputFilename); |
| 74 | pathWithNumber.remove(pathWithNumber.size() - 4, 4); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 75 | pathWithNumber.appendf("%i.png", i++); |
| 76 | SkBitmap copy; |
| 77 | #if SK_SUPPORT_GPU |
| 78 | if (isUsingGpuDevice()) { |
| 79 | dst.pixelRef()->readPixels(©, &subset); |
| 80 | } else { |
| 81 | #endif |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 82 | dst.copyTo(©); |
scroggo@google.com | 4a26d9d | 2012-11-07 18:01:46 +0000 | [diff] [blame] | 83 | #if SK_SUPPORT_GPU |
| 84 | } |
| 85 | #endif |
| 86 | success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy, |
| 87 | SkImageEncoder::kPNG_Type, 100); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return success; |
| 94 | } |
| 95 | |
| 96 | SkString CopyTilesRenderer::getConfigNameInternal() { |
| 97 | return SkString("copy_tiles"); |
| 98 | } |
| 99 | } |