blob: 1298d43f0f42f3e8fd63e08c792e5a82e2d64639 [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"
13#include "SkPicture.h"
14#include "SkPixelRef.h"
15#include "SkRect.h"
16#include "SkString.h"
17
18namespace 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.com84f548c2012-12-18 22:24:03 +000037 bool CopyTilesRenderer::render(const SkString* path, SkBitmap** out) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000038 int i = 0;
39 bool success = true;
40 SkBitmap dst;
scroggo@google.comc0d5e542012-12-13 21:40:48 +000041 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
42 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000043 SkAutoCanvasRestore autoRestore(fCanvas, true);
scroggo@google.com82ec0b02012-12-17 19:25:54 +000044 // 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.com4a26d9d2012-11-07 18:01:46 +000050 // 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(&copy, &subset);
71 } else {
72#endif
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000073 dst.copyTo(&copy);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000074#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}