OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "picture_utils.h" | 8 #include "picture_utils.h" |
9 #include "CopyTilesRenderer.h" | 9 #include "CopyTilesRenderer.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkImageEncoder.h" | 12 #include "SkImageEncoder.h" |
13 #include "SkPicture.h" | 13 #include "SkPicture.h" |
14 #include "SkPixelRef.h" | 14 #include "SkPixelRef.h" |
15 #include "SkRect.h" | 15 #include "SkRect.h" |
16 #include "SkString.h" | 16 #include "SkString.h" |
17 | 17 |
18 namespace sk_tools { | 18 namespace sk_tools { |
19 CopyTilesRenderer::CopyTilesRenderer(int x, int y) | 19 CopyTilesRenderer::CopyTilesRenderer(int x, int y) |
20 : fXTilesPerLargeTile(x) | 20 : fXTilesPerLargeTile(x) |
21 , fYTilesPerLargeTile(y) { | 21 , fYTilesPerLargeTile(y) { |
22 } | 22 } |
23 void CopyTilesRenderer::init(SkPicture* pict) { | 23 void CopyTilesRenderer::init(SkPicture* pict, const SkString& outputDir, |
24 const SkString& inputFilename, bool useChecksum BasedFilenames) { | |
robertphillips
2014/03/19 14:42:36
I think this should call INHERITED::init
epoger
2014/03/19 15:20:29
I disagree, but I'm not 100% certain about it.
IN
robertphillips
2014/03/19 16:06:50
You're right. We probably want a comment like the
epoger
2014/03/19 16:48:26
Done.
| |
24 SkASSERT(pict != NULL); | 25 SkASSERT(pict != NULL); |
25 // Only work with absolute widths (as opposed to percentages). | 26 // Only work with absolute widths (as opposed to percentages). |
26 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0); | 27 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0); |
27 fPicture = pict; | 28 fPicture = pict; |
29 fOutputDir.set(outputDir); | |
30 fInputFilename.set(inputFilename); | |
31 fUseChecksumBasedFilenames = useChecksumBasedFilenames; | |
28 fPicture->ref(); | 32 fPicture->ref(); |
29 this->buildBBoxHierarchy(); | 33 this->buildBBoxHierarchy(); |
30 // In order to avoid allocating a large canvas (particularly important f or GPU), create one | 34 // In order to avoid allocating a large canvas (particularly important f or GPU), create one |
31 // canvas that is a multiple of the tile size, and draw portions of the picture. | 35 // canvas that is a multiple of the tile size, and draw portions of the picture. |
32 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth(); | 36 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth(); |
33 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight(); | 37 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight(); |
34 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHe ight)); | 38 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHe ight)); |
35 } | 39 } |
36 | 40 |
37 bool CopyTilesRenderer::render(const SkString* path, SkBitmap** out) { | 41 bool CopyTilesRenderer::render(SkBitmap** out) { |
38 int i = 0; | 42 int i = 0; |
39 bool success = true; | 43 bool success = true; |
40 SkBitmap dst; | 44 SkBitmap dst; |
41 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) { | 45 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) { |
42 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) { | 46 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) { |
43 SkAutoCanvasRestore autoRestore(fCanvas, true); | 47 SkAutoCanvasRestore autoRestore(fCanvas, true); |
44 // Translate so that we draw the correct portion of the picture. | 48 // Translate so that we draw the correct portion of the picture. |
45 // Perform a postTranslate so that the scaleFactor does not inte rfere with the | 49 // Perform a postTranslate so that the scaleFactor does not inte rfere with the |
46 // positioning. | 50 // positioning. |
47 SkMatrix mat(fCanvas->getTotalMatrix()); | 51 SkMatrix mat(fCanvas->getTotalMatrix()); |
48 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); | 52 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
49 fCanvas->setMatrix(mat); | 53 fCanvas->setMatrix(mat); |
50 // Draw the picture | 54 // Draw the picture |
51 fCanvas->drawPicture(*fPicture); | 55 fCanvas->drawPicture(*fPicture); |
52 // Now extract the picture into tiles | 56 // Now extract the picture into tiles |
53 const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap( false); | 57 const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap( false); |
54 SkIRect subset; | 58 SkIRect subset; |
55 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->get TileHeight()) { | 59 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->get TileHeight()) { |
56 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this-> getTileWidth()) { | 60 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this-> getTileWidth()) { |
57 subset.set(tileX, tileY, tileX + this->getTileWidth(), | 61 subset.set(tileX, tileY, tileX + this->getTileWidth(), |
58 tileY + this->getTileHeight()); | 62 tileY + this->getTileHeight()); |
59 SkDEBUGCODE(bool extracted =) | 63 SkDEBUGCODE(bool extracted =) |
60 baseBitmap.extractSubset(&dst, subset); | 64 baseBitmap.extractSubset(&dst, subset); |
61 SkASSERT(extracted); | 65 SkASSERT(extracted); |
62 if (path != NULL) { | 66 if (!fOutputDir.isEmpty()) { |
63 // Similar to writeAppendNumber in PictureRenderer.c pp, but just encodes | 67 // Similar to write() in PictureRenderer.cpp, but ju st encodes |
64 // a bitmap directly. | 68 // a bitmap directly. |
65 SkString pathWithNumber(*path); | 69 // TODO: Share more common code with write() to do t his, to properly |
70 // write out the JSON summary, etc. | |
71 SkString pathWithNumber; | |
72 make_filepath(&pathWithNumber, fOutputDir, fInputFil ename); | |
73 pathWithNumber.remove(pathWithNumber.size() - 4, 4); | |
66 pathWithNumber.appendf("%i.png", i++); | 74 pathWithNumber.appendf("%i.png", i++); |
67 SkBitmap copy; | 75 SkBitmap copy; |
68 #if SK_SUPPORT_GPU | 76 #if SK_SUPPORT_GPU |
69 if (isUsingGpuDevice()) { | 77 if (isUsingGpuDevice()) { |
70 dst.pixelRef()->readPixels(©, &subset); | 78 dst.pixelRef()->readPixels(©, &subset); |
71 } else { | 79 } else { |
72 #endif | 80 #endif |
73 dst.copyTo(©); | 81 dst.copyTo(©); |
74 #if SK_SUPPORT_GPU | 82 #if SK_SUPPORT_GPU |
75 } | 83 } |
76 #endif | 84 #endif |
77 success &= SkImageEncoder::EncodeFile(pathWithNumber .c_str(), copy, | 85 success &= SkImageEncoder::EncodeFile(pathWithNumber .c_str(), copy, |
78 SkImageEncoder ::kPNG_Type, 100); | 86 SkImageEncoder ::kPNG_Type, 100); |
79 } | 87 } |
80 } | 88 } |
81 } | 89 } |
82 } | 90 } |
83 } | 91 } |
84 return success; | 92 return success; |
85 } | 93 } |
86 | 94 |
87 SkString CopyTilesRenderer::getConfigNameInternal() { | 95 SkString CopyTilesRenderer::getConfigNameInternal() { |
88 return SkString("copy_tiles"); | 96 return SkString("copy_tiles"); |
89 } | 97 } |
90 } | 98 } |
OLD | NEW |