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