blob: bc1e32ec1112664bb5dabd7284e7462b427755da [file] [log] [blame]
scroggo@google.com4a26d9d2012-11-07 18:01:46 +00001/*
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"
robertphillips78c71272014-10-09 04:59:19 -070013#include "SkMultiPictureDraw.h"
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000014#include "SkPicture.h"
15#include "SkPixelRef.h"
16#include "SkRect.h"
17#include "SkString.h"
18
19namespace sk_tools {
krajcevskib1aded82014-08-18 07:52:17 -070020#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -070021 CopyTilesRenderer::CopyTilesRenderer(const GrContextOptions& opts, int x, int y)
krajcevskib1aded82014-08-18 07:52:17 -070022 : INHERITED(opts)
23 , fXTilesPerLargeTile(x)
24 , fYTilesPerLargeTile(y) { }
25#else
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000026 CopyTilesRenderer::CopyTilesRenderer(int x, int y)
27 : fXTilesPerLargeTile(x)
krajcevskib1aded82014-08-18 07:52:17 -070028 , fYTilesPerLargeTile(y) { }
29#endif
robertphillipsce4dd3d2014-07-07 13:46:35 -070030 void CopyTilesRenderer::init(const SkPicture* pict, const SkString* writePath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000031 const SkString* mismatchPath, const SkString* inputFilename,
robertphillips78c71272014-10-09 04:59:19 -070032 bool useChecksumBasedFilenames, bool useMultiPictureDraw) {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000033 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
34 // not used by bench_pictures.
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000035 SkASSERT(pict != NULL);
36 // Only work with absolute widths (as opposed to percentages).
37 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000038 fPicture.reset(pict)->ref();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000039 this->CopyString(&fWritePath, writePath);
40 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000041 this->CopyString(&fInputFilename, inputFilename);
42 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
robertphillips78c71272014-10-09 04:59:19 -070043 fUseMultiPictureDraw = useMultiPictureDraw;
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000044 this->buildBBoxHierarchy();
45 // In order to avoid allocating a large canvas (particularly important for GPU), create one
46 // canvas that is a multiple of the tile size, and draw portions of the picture.
47 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
48 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
49 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
50 }
51
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000052 bool CopyTilesRenderer::render(SkBitmap** out) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000053 int i = 0;
54 bool success = true;
55 SkBitmap dst;
scroggo@google.comc0d5e542012-12-13 21:40:48 +000056 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
57 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000058 SkAutoCanvasRestore autoRestore(fCanvas, true);
scroggo@google.com82ec0b02012-12-17 19:25:54 +000059 // Translate so that we draw the correct portion of the picture.
60 // Perform a postTranslate so that the scaleFactor does not interfere with the
61 // positioning.
62 SkMatrix mat(fCanvas->getTotalMatrix());
63 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
64 fCanvas->setMatrix(mat);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000065 // Draw the picture
robertphillips78c71272014-10-09 04:59:19 -070066 if (fUseMultiPictureDraw) {
67 SkMultiPictureDraw mpd;
68
69 mpd.add(fCanvas, fPicture);
70
71 mpd.draw();
72 } else {
73 fCanvas->drawPicture(fPicture);
74 }
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000075 // Now extract the picture into tiles
reed52d9ac62014-06-30 09:05:34 -070076 SkBitmap baseBitmap;
77 fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000078 SkIRect subset;
79 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
80 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
81 subset.set(tileX, tileY, tileX + this->getTileWidth(),
82 tileY + this->getTileHeight());
83 SkDEBUGCODE(bool extracted =)
84 baseBitmap.extractSubset(&dst, subset);
85 SkASSERT(extracted);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000086 if (!fWritePath.isEmpty()) {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000087 // Similar to write() in PictureRenderer.cpp, but just encodes
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000088 // a bitmap directly.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000089 // TODO: Share more common code with write() to do this, to properly
90 // write out the JSON summary, etc.
tfarinaa8e2e152014-07-28 19:26:58 -070091 SkString pathWithNumber = SkOSPath::Join(fWritePath.c_str(),
92 fInputFilename.c_str());
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000093 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000094 pathWithNumber.appendf("%i.png", i++);
95 SkBitmap copy;
96#if SK_SUPPORT_GPU
97 if (isUsingGpuDevice()) {
98 dst.pixelRef()->readPixels(&copy, &subset);
99 } else {
100#endif
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000101 dst.copyTo(&copy);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +0000102#if SK_SUPPORT_GPU
103 }
104#endif
105 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
106 SkImageEncoder::kPNG_Type, 100);
107 }
108 }
109 }
110 }
111 }
112 return success;
113 }
114
115 SkString CopyTilesRenderer::getConfigNameInternal() {
116 return SkString("copy_tiles");
117 }
118}