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