blob: b81f0587302d2d09c7ae4f9a4737ea218cc943cb [file] [log] [blame]
keyar@chromium.orgb3fb7c12012-08-20 21:02:49 +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
keyar@chromium.org451bb9f2012-07-26 17:27:57 +00008#include "PictureRenderer.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +00009#include "picture_utils.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000010#include "SamplePipeControllers.h"
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000011#include "SkBitmapHasher.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000012#include "SkCanvas.h"
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000013#include "SkData.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000014#include "SkDevice.h"
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000015#include "SkDiscardableMemoryPool.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000016#include "SkGPipe.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +000017#if SK_SUPPORT_GPU
robertphillips@google.comfe1b5362013-02-07 19:45:46 +000018#include "gl/GrGLDefines.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +000019#include "SkGpuDevice.h"
20#endif
21#include "SkGraphics.h"
22#include "SkImageEncoder.h"
caryclark@google.coma3622372012-11-06 21:26:13 +000023#include "SkMaskFilter.h"
keyar@chromium.orgea826952012-08-23 15:24:13 +000024#include "SkMatrix.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000025#include "SkPicture.h"
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000026#include "SkPictureUtils.h"
27#include "SkPixelRef.h"
junov@chromium.org9313ca42012-11-02 18:11:49 +000028#include "SkRTree.h"
keyar@chromium.orgea826952012-08-23 15:24:13 +000029#include "SkScalar.h"
scroggo@google.coma9e3a362012-11-07 17:52:48 +000030#include "SkStream.h"
keyar@chromium.org9299ede2012-08-21 19:05:08 +000031#include "SkString.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +000032#include "SkTemplates.h"
junov@chromium.org3cb834b2012-12-13 16:39:53 +000033#include "SkTileGridPicture.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000034#include "SkTDArray.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +000035#include "SkThreadUtils.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000036#include "SkTypes.h"
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000037
reed@google.come15b2f52013-12-18 04:59:26 +000038static inline SkScalar scalar_log2(SkScalar x) {
39 static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2));
skia.committer@gmail.com3b85deb2013-12-18 07:01:56 +000040
reed@google.come15b2f52013-12-18 04:59:26 +000041 return SkScalarLog(x) * log2_conversion_factor;
42}
43
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000044namespace sk_tools {
45
46enum {
47 kDefaultTileWidth = 256,
48 kDefaultTileHeight = 256
49};
50
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000051/* TODO(epoger): These constants are already maintained in 2 other places:
52 * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place.
53 * Figure out a way to share the definitions instead.
54 */
55const static char kJsonKey_ActualResults[] = "actual-results";
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000056const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison";
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000057const static char kJsonKey_Hashtype_Bitmap_64bitMD5[] = "bitmap-64bitMD5";
58
59void ImageResultsSummary::add(const char *testName, const SkBitmap& bitmap) {
60 uint64_t hash;
61 SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash));
62 Json::Value jsonTypeValuePair;
63 jsonTypeValuePair.append(Json::Value(kJsonKey_Hashtype_Bitmap_64bitMD5));
64 jsonTypeValuePair.append(Json::UInt64(hash));
65 fActualResultsNoComparison[testName] = jsonTypeValuePair;
66}
67
68void ImageResultsSummary::writeToFile(const char *filename) {
69 Json::Value actualResults;
70 actualResults[kJsonKey_ActualResults_NoComparison] = fActualResultsNoComparison;
71 Json::Value root;
72 root[kJsonKey_ActualResults] = actualResults;
73 std::string jsonStdString = root.toStyledString();
74 SkFILEWStream stream(filename);
75 stream.write(jsonStdString.c_str(), jsonStdString.length());
76}
77
keyar@chromium.org9d696c02012-08-07 17:11:33 +000078void PictureRenderer::init(SkPicture* pict) {
keyar@chromium.org78a35c52012-08-20 15:03:44 +000079 SkASSERT(NULL == fPicture);
80 SkASSERT(NULL == fCanvas.get());
81 if (fPicture != NULL || NULL != fCanvas.get()) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000082 return;
83 }
84
85 SkASSERT(pict != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000086 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000087 return;
88 }
89
90 fPicture = pict;
junov@chromium.org9313ca42012-11-02 18:11:49 +000091 fPicture->ref();
keyar@chromium.orga474ce32012-08-20 15:03:57 +000092 fCanvas.reset(this->setupCanvas());
93}
94
caryclark@google.coma3622372012-11-06 21:26:13 +000095class FlagsDrawFilter : public SkDrawFilter {
96public:
97 FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) :
98 fFlags(flags) {}
99
reed@google.com971aca72012-11-26 20:26:54 +0000100 virtual bool filter(SkPaint* paint, Type t) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000101 paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags);
robertphillips@google.com49149312013-07-03 15:34:35 +0000102 if (PictureRenderer::kMaskFilter_DrawFilterFlag & fFlags[t]) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000103 SkMaskFilter* maskFilter = paint->getMaskFilter();
robertphillips@google.com49149312013-07-03 15:34:35 +0000104 if (NULL != maskFilter) {
reed@google.com457d8a72012-12-18 18:20:44 +0000105 paint->setMaskFilter(NULL);
caryclark@google.coma3622372012-11-06 21:26:13 +0000106 }
107 }
108 if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) {
109 paint->setHinting(SkPaint::kNo_Hinting);
110 } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) {
111 paint->setHinting(SkPaint::kSlight_Hinting);
112 }
reed@google.com971aca72012-11-26 20:26:54 +0000113 return true;
caryclark@google.coma3622372012-11-06 21:26:13 +0000114 }
115
116private:
117 PictureRenderer::DrawFilterFlags* fFlags;
118};
119
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000120static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000121 if (drawFilters && !canvas->getDrawFilter()) {
122 canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref();
caryclark@google.come3e940c2012-11-07 16:42:17 +0000123 if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) {
124 canvas->setAllowSoftClip(false);
125 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000126 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000127}
128
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000129SkCanvas* PictureRenderer::setupCanvas() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000130 const int width = this->getViewWidth();
131 const int height = this->getViewHeight();
132 return this->setupCanvas(width, height);
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000133}
134
135SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000136 SkCanvas* canvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000137 switch(fDeviceType) {
138 case kBitmap_DeviceType: {
139 SkBitmap bitmap;
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000140 sk_tools::setup_bitmap(&bitmap, width, height);
caryclark@google.coma3622372012-11-06 21:26:13 +0000141 canvas = SkNEW_ARGS(SkCanvas, (bitmap));
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000142 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000143 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000144#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000145#if SK_ANGLE
146 case kAngle_DeviceType:
147 // fall through
148#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000149#if SK_MESA
150 case kMesa_DeviceType:
151 // fall through
152#endif
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000153 case kGPU_DeviceType: {
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000154 SkAutoTUnref<GrSurface> target;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000155 if (fGrContext) {
156 // create a render target to back the device
157 GrTextureDesc desc;
158 desc.fConfig = kSkia8888_GrPixelConfig;
159 desc.fFlags = kRenderTarget_GrTextureFlagBit;
160 desc.fWidth = width;
161 desc.fHeight = height;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000162 desc.fSampleCnt = fSampleCount;
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000163 target.reset(fGrContext->createUncachedTexture(desc, NULL, 0));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000164 }
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000165 if (NULL == target.get()) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000166 SkASSERT(0);
167 return NULL;
168 }
169
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000170 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target));
caryclark@google.coma3622372012-11-06 21:26:13 +0000171 canvas = SkNEW_ARGS(SkCanvas, (device.get()));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000172 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000173 }
174#endif
175 default:
176 SkASSERT(0);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000177 return NULL;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000178 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000179 setUpFilter(canvas, fDrawFilters);
180 this->scaleToScaleFactor(canvas);
commit-bot@chromium.org17cc3ea2014-01-15 14:51:25 +0000181
182 // Pictures often lie about their extent (i.e., claim to be 100x100 but
183 // only ever draw to 90x100). Clear here so the undrawn portion will have
184 // a consistent color
185 canvas->clear(SK_ColorTRANSPARENT);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000186 return canvas;
187}
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000188
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000189void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) {
190 SkASSERT(canvas != NULL);
191 if (fScaleFactor != SK_Scalar1) {
192 canvas->scale(fScaleFactor, fScaleFactor);
193 }
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000194}
195
196void PictureRenderer::end() {
scroggo@google.com08085f82013-01-28 20:40:24 +0000197 this->resetState(true);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000198 SkSafeUnref(fPicture);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000199 fPicture = NULL;
200 fCanvas.reset(NULL);
201}
202
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000203int PictureRenderer::getViewWidth() {
204 SkASSERT(fPicture != NULL);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000205 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000206 if (fViewport.width() > 0) {
207 width = SkMin32(width, fViewport.width());
208 }
209 return width;
210}
211
212int PictureRenderer::getViewHeight() {
213 SkASSERT(fPicture != NULL);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000214 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000215 if (fViewport.height() > 0) {
216 height = SkMin32(height, fViewport.height());
217 }
218 return height;
219}
220
junov@chromium.org9313ca42012-11-02 18:11:49 +0000221/** Converts fPicture to a picture that uses a BBoxHierarchy.
222 * PictureRenderer subclasses that are used to test picture playback
223 * should call this method during init.
224 */
225void PictureRenderer::buildBBoxHierarchy() {
226 SkASSERT(NULL != fPicture);
227 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) {
228 SkPicture* newPicture = this->createPicture();
229 SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPicture->height(),
230 this->recordFlags());
231 fPicture->draw(recorder);
232 newPicture->endRecording();
233 fPicture->unref();
234 fPicture = newPicture;
235 }
236}
237
scroggo@google.com08085f82013-01-28 20:40:24 +0000238void PictureRenderer::resetState(bool callFinish) {
keyar@chromium.org28136b32012-08-20 15:04:15 +0000239#if SK_SUPPORT_GPU
robertphillips@google.com6177e692013-02-28 20:16:25 +0000240 SkGLContextHelper* glContext = this->getGLContext();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000241 if (NULL == glContext) {
242 SkASSERT(kBitmap_DeviceType == fDeviceType);
243 return;
244 }
keyar@chromium.org28136b32012-08-20 15:04:15 +0000245
scroggo@google.com0556ea02013-02-08 19:38:21 +0000246 fGrContext->flush();
247 if (callFinish) {
248 SK_GL(*glContext, Finish());
keyar@chromium.org77a55222012-08-20 15:03:47 +0000249 }
keyar@chromium.orga40c20d2012-08-20 15:04:12 +0000250#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +0000251}
252
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000253void PictureRenderer::purgeTextures() {
254 SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool();
255
256 pool->dumpPool();
257
258#if SK_SUPPORT_GPU
259 SkGLContextHelper* glContext = this->getGLContext();
260 if (NULL == glContext) {
261 SkASSERT(kBitmap_DeviceType == fDeviceType);
262 return;
263 }
264
265 // resetState should've already done this
266 fGrContext->flush();
267
268 fGrContext->purgeAllUnlockedResources();
269#endif
270}
271
junov@chromium.org9313ca42012-11-02 18:11:49 +0000272uint32_t PictureRenderer::recordFlags() {
junov@chromium.org100b1c52013-01-16 20:12:22 +0000273 return ((kNone_BBoxHierarchyType == fBBoxHierarchyType) ? 0 :
274 SkPicture::kOptimizeForClippedPlayback_RecordingFlag) |
275 SkPicture::kUsePathBoundsForClip_RecordingFlag;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000276}
277
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000278/**
279 * Write the canvas to the specified path.
280 * @param canvas Must be non-null. Canvas to be written to a file.
281 * @param path Path for the file to be written. Should have no extension; write() will append
282 * an appropriate one. Passed in by value so it can be modified.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000283 * @param jsonSummaryPtr If not null, add image results to this summary.
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000284 * @return bool True if the Canvas is written to a file.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000285 *
286 * TODO(epoger): Right now, all canvases must pass through this function in order to be appended
287 * to the ImageResultsSummary. We need some way to add bitmaps to the ImageResultsSummary
288 * even if --writePath has not been specified (and thus this function is not called).
289 *
290 * One fix would be to pass in these path elements separately, and allow this function to be
291 * called even if --writePath was not specified...
292 * const char *outputDir // NULL if we don't want to write image files to disk
293 * const char *filename // name we use within JSON summary, and as the filename within outputDir
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000294 */
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000295static bool write(SkCanvas* canvas, const SkString* path, ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000296 SkASSERT(canvas != NULL);
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000297 if (NULL == canvas) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000298 return false;
299 }
300
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000301 SkASSERT(path != NULL); // TODO(epoger): we want to remove this constraint, as noted above
302 SkString fullPathname(*path);
303 fullPathname.append(".png");
304
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000305 SkBitmap bitmap;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000306 SkISize size = canvas->getDeviceSize();
307 sk_tools::setup_bitmap(&bitmap, size.width(), size.height());
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000308
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000309 canvas->readPixels(&bitmap, 0, 0);
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000310 sk_tools::force_all_opaque(bitmap);
311
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000312 if (NULL != jsonSummaryPtr) {
313 // EPOGER: This is a hacky way of constructing the filename associated with the
314 // image checksum; we assume that outputDir is not NULL, and we remove outputDir
315 // from fullPathname.
316 //
317 // EPOGER: what about including the config type within hashFilename? That way,
318 // we could combine results of different config types without conflicting filenames.
319 SkString hashFilename;
320 sk_tools::get_basename(&hashFilename, fullPathname);
321 jsonSummaryPtr->add(hashFilename.c_str(), bitmap);
322 }
323
324 return SkImageEncoder::EncodeFile(fullPathname.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000325}
326
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000327/**
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000328 * If path is non NULL, append number to it, and call write() to write the
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000329 * provided canvas to a file. Returns true if path is NULL or if write() succeeds.
330 */
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000331static bool writeAppendNumber(SkCanvas* canvas, const SkString* path, int number,
332 ImageResultsSummary *jsonSummaryPtr) {
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000333 if (NULL == path) {
334 return true;
335 }
336 SkString pathWithNumber(*path);
337 pathWithNumber.appendf("%i", number);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000338 return write(canvas, &pathWithNumber, jsonSummaryPtr);
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000339}
340
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000341///////////////////////////////////////////////////////////////////////////////////////////////
342
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000343SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) {
344 // defer the canvas setup until the render step
345 return NULL;
346}
347
reed@google.com672588b2014-01-08 15:42:01 +0000348// the size_t* parameter is deprecated, so we ignore it
349static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000350 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000351}
352
edisonn@google.com84f548c2012-12-18 22:24:03 +0000353bool RecordPictureRenderer::render(const SkString* path, SkBitmap** out) {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000354 SkAutoTUnref<SkPicture> replayer(this->createPicture());
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000355 SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->getViewHeight(),
junov@chromium.org9313ca42012-11-02 18:11:49 +0000356 this->recordFlags());
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000357 this->scaleToScaleFactor(recorder);
scroggo@google.com9a412522012-09-07 15:21:18 +0000358 fPicture->draw(recorder);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000359 replayer->endRecording();
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000360 if (path != NULL) {
361 // Record the new picture as a new SKP with PNG encoded bitmaps.
362 SkString skpPath(*path);
363 // ".skp" was removed from 'path' before being passed in here.
364 skpPath.append(".skp");
365 SkFILEWStream stream(skpPath.c_str());
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000366 replayer->serialize(&stream, &encode_bitmap_to_data);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000367 return true;
368 }
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000369 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +0000370}
371
scroggo@google.com0a049b82012-11-02 22:01:26 +0000372SkString RecordPictureRenderer::getConfigNameInternal() {
373 return SkString("record");
374}
375
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000376///////////////////////////////////////////////////////////////////////////////////////////////
377
edisonn@google.com84f548c2012-12-18 22:24:03 +0000378bool PipePictureRenderer::render(const SkString* path, SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000379 SkASSERT(fCanvas.get() != NULL);
380 SkASSERT(fPicture != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000381 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000382 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000383 }
384
385 PipeController pipeController(fCanvas.get());
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000386 SkGPipeWriter writer;
387 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000388 pipeCanvas->drawPicture(*fPicture);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000389 writer.endRecording();
scroggo@google.com9a412522012-09-07 15:21:18 +0000390 fCanvas->flush();
borenet@google.com070d3542012-10-26 13:26:55 +0000391 if (NULL != path) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000392 return write(fCanvas, path, fJsonSummaryPtr);
borenet@google.com070d3542012-10-26 13:26:55 +0000393 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000394 if (NULL != out) {
395 *out = SkNEW(SkBitmap);
396 setup_bitmap(*out, fPicture->width(), fPicture->height());
397 fCanvas->readPixels(*out, 0, 0);
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000398 }
borenet@google.com070d3542012-10-26 13:26:55 +0000399 return true;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000400}
401
scroggo@google.com0a049b82012-11-02 22:01:26 +0000402SkString PipePictureRenderer::getConfigNameInternal() {
403 return SkString("pipe");
404}
405
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000406///////////////////////////////////////////////////////////////////////////////////////////////
407
junov@chromium.org9313ca42012-11-02 18:11:49 +0000408void SimplePictureRenderer::init(SkPicture* picture) {
409 INHERITED::init(picture);
410 this->buildBBoxHierarchy();
411}
412
edisonn@google.com84f548c2012-12-18 22:24:03 +0000413bool SimplePictureRenderer::render(const SkString* path, SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000414 SkASSERT(fCanvas.get() != NULL);
415 SkASSERT(fPicture != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000416 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000417 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000418 }
419
420 fCanvas->drawPicture(*fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000421 fCanvas->flush();
borenet@google.com070d3542012-10-26 13:26:55 +0000422 if (NULL != path) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000423 return write(fCanvas, path, fJsonSummaryPtr);
borenet@google.com070d3542012-10-26 13:26:55 +0000424 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000425
edisonn@google.com84f548c2012-12-18 22:24:03 +0000426 if (NULL != out) {
427 *out = SkNEW(SkBitmap);
428 setup_bitmap(*out, fPicture->width(), fPicture->height());
429 fCanvas->readPixels(*out, 0, 0);
430 }
431
borenet@google.com070d3542012-10-26 13:26:55 +0000432 return true;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000433}
434
scroggo@google.com0a049b82012-11-02 22:01:26 +0000435SkString SimplePictureRenderer::getConfigNameInternal() {
436 return SkString("simple");
437}
438
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000439///////////////////////////////////////////////////////////////////////////////////////////////
440
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000441TiledPictureRenderer::TiledPictureRenderer()
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000442 : fTileWidth(kDefaultTileWidth)
rileya@google.comb947b912012-08-29 17:35:07 +0000443 , fTileHeight(kDefaultTileHeight)
rileya@google.coma04dc022012-09-10 19:01:38 +0000444 , fTileWidthPercentage(0.0)
rileya@google.comb947b912012-08-29 17:35:07 +0000445 , fTileHeightPercentage(0.0)
scroggo@google.comcbcef702012-12-13 22:09:28 +0000446 , fTileMinPowerOf2Width(0)
447 , fCurrentTileOffset(-1)
448 , fTilesX(0)
449 , fTilesY(0) { }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000450
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000451void TiledPictureRenderer::init(SkPicture* pict) {
452 SkASSERT(pict != NULL);
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000453 SkASSERT(0 == fTileRects.count());
454 if (NULL == pict || fTileRects.count() != 0) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000455 return;
456 }
457
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000458 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
459 // used by bench_pictures.
460 fPicture = pict;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000461 fPicture->ref();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000462 this->buildBBoxHierarchy();
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000463
464 if (fTileWidthPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000465 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000466 }
467 if (fTileHeightPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000468 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000469 }
470
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000471 if (fTileMinPowerOf2Width > 0) {
472 this->setupPowerOf2Tiles();
473 } else {
474 this->setupTiles();
475 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000476 fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight));
477 // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the
478 // first call to drawCurrentTile.
479 fCurrentTileOffset = -1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000480}
481
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000482void TiledPictureRenderer::end() {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000483 fTileRects.reset();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000484 this->INHERITED::end();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000485}
486
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000487void TiledPictureRenderer::setupTiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000488 // Only use enough tiles to cover the viewport
489 const int width = this->getViewWidth();
490 const int height = this->getViewHeight();
491
scroggo@google.comcbcef702012-12-13 22:09:28 +0000492 fTilesX = fTilesY = 0;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000493 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000494 fTilesY++;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000495 for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000496 if (0 == tile_y_start) {
497 // Only count tiles in the X direction on the first pass.
498 fTilesX++;
499 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000500 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
501 SkIntToScalar(tile_y_start),
502 SkIntToScalar(fTileWidth),
503 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000504 }
505 }
506}
507
scroggo@google.comcbcef702012-12-13 22:09:28 +0000508bool TiledPictureRenderer::tileDimensions(int &x, int &y) {
509 if (fTileRects.count() == 0 || NULL == fPicture) {
510 return false;
511 }
512 x = fTilesX;
513 y = fTilesY;
514 return true;
515}
516
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000517// The goal of the powers of two tiles is to minimize the amount of wasted tile
518// space in the width-wise direction and then minimize the number of tiles. The
519// constraints are that every tile must have a pixel width that is a power of
520// two and also be of some minimal width (that is also a power of two).
521//
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000522// This is solved by first taking our picture size and rounding it up to the
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000523// multiple of the minimal width. The binary representation of this rounded
524// value gives us the tiles we need: a bit of value one means we need a tile of
525// that size.
526void TiledPictureRenderer::setupPowerOf2Tiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000527 // Only use enough tiles to cover the viewport
528 const int width = this->getViewWidth();
529 const int height = this->getViewHeight();
530
531 int rounded_value = width;
532 if (width % fTileMinPowerOf2Width != 0) {
533 rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000534 }
535
reed@google.come15b2f52013-12-18 04:59:26 +0000536 int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width)));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000537 int largest_possible_tile_size = 1 << num_bits;
538
scroggo@google.comcbcef702012-12-13 22:09:28 +0000539 fTilesX = fTilesY = 0;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000540 // The tile height is constant for a particular picture.
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000541 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000542 fTilesY++;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000543 int tile_x_start = 0;
544 int current_width = largest_possible_tile_size;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000545 // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
546 // to draw each tile.
547 fTileWidth = current_width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000548
549 while (current_width >= fTileMinPowerOf2Width) {
550 // It is very important this is a bitwise AND.
551 if (current_width & rounded_value) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000552 if (0 == tile_y_start) {
553 // Only count tiles in the X direction on the first pass.
554 fTilesX++;
555 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000556 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
557 SkIntToScalar(tile_y_start),
558 SkIntToScalar(current_width),
559 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000560 tile_x_start += current_width;
561 }
562
563 current_width >>= 1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000564 }
565 }
566}
567
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000568/**
569 * Draw the specified playback to the canvas translated to rectangle provided, so that this mini
570 * canvas represents the rectangle's portion of the overall picture.
571 * Saves and restores so that the initial clip and matrix return to their state before this function
572 * is called.
573 */
574template<class T>
575static void DrawTileToCanvas(SkCanvas* canvas, const SkRect& tileRect, T* playback) {
576 int saveCount = canvas->save();
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000577 // Translate so that we draw the correct portion of the picture.
578 // Perform a postTranslate so that the scaleFactor does not interfere with the positioning.
579 SkMatrix mat(canvas->getTotalMatrix());
580 mat.postTranslate(-tileRect.fLeft, -tileRect.fTop);
581 canvas->setMatrix(mat);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000582 playback->draw(canvas);
583 canvas->restoreToCount(saveCount);
584 canvas->flush();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000585}
586
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000587///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000588
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000589/**
590 * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap.
591 * If the src bitmap is too large to fit within the dst bitmap after the x and y
592 * offsets have been applied, any excess will be ignored (so only the top-left portion of the
593 * src bitmap will be copied).
594 *
595 * @param src source bitmap
596 * @param dst destination bitmap
597 * @param xOffset x-offset within destination bitmap
598 * @param yOffset y-offset within destination bitmap
599 */
600static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst,
601 int xOffset, int yOffset) {
602 for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) {
603 for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) {
604 *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000605 }
606 }
607}
608
scroggo@google.comcbcef702012-12-13 22:09:28 +0000609bool TiledPictureRenderer::nextTile(int &i, int &j) {
610 if (++fCurrentTileOffset < fTileRects.count()) {
611 i = fCurrentTileOffset % fTilesX;
612 j = fCurrentTileOffset / fTilesX;
613 return true;
614 }
615 return false;
616}
617
618void TiledPictureRenderer::drawCurrentTile() {
619 SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count());
620 DrawTileToCanvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture);
621}
622
edisonn@google.com84f548c2012-12-18 22:24:03 +0000623bool TiledPictureRenderer::render(const SkString* path, SkBitmap** out) {
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000624 SkASSERT(fPicture != NULL);
625 if (NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000626 return false;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000627 }
628
edisonn@google.com84f548c2012-12-18 22:24:03 +0000629 SkBitmap bitmap;
630 if (out){
631 *out = SkNEW(SkBitmap);
632 setup_bitmap(*out, fPicture->width(), fPicture->height());
633 setup_bitmap(&bitmap, fTileWidth, fTileHeight);
634 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000635 bool success = true;
636 for (int i = 0; i < fTileRects.count(); ++i) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000637 DrawTileToCanvas(fCanvas, fTileRects[i], fPicture);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000638 if (NULL != path) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000639 success &= writeAppendNumber(fCanvas, path, i, fJsonSummaryPtr);
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000640 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000641 if (NULL != out) {
642 if (fCanvas->readPixels(&bitmap, 0, 0)) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000643 // Add this tile to the entire bitmap.
644 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()),
645 SkScalarFloorToInt(fTileRects[i].top()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000646 } else {
647 success = false;
648 }
649 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000650 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000651 return success;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000652}
653
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000654SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
655 SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
656 SkASSERT(fPicture != NULL);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000657 // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This
658 // is mostly important for tiles on the right and bottom edges as they may go over this area and
659 // the picture may have some commands that draw outside of this area and so should not actually
660 // be written.
661 // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set
662 // by INHERITED::setupCanvas.
663 SkRegion clipRegion;
664 clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight());
665 canvas->clipRegion(clipRegion);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000666 return canvas;
667}
scroggo@google.com0a049b82012-11-02 22:01:26 +0000668
669SkString TiledPictureRenderer::getConfigNameInternal() {
670 SkString name;
671 if (fTileMinPowerOf2Width > 0) {
672 name.append("pow2tile_");
673 name.appendf("%i", fTileMinPowerOf2Width);
674 } else {
675 name.append("tile_");
676 if (fTileWidthPercentage > 0) {
677 name.appendf("%.f%%", fTileWidthPercentage);
678 } else {
679 name.appendf("%i", fTileWidth);
680 }
681 }
682 name.append("x");
683 if (fTileHeightPercentage > 0) {
684 name.appendf("%.f%%", fTileHeightPercentage);
685 } else {
686 name.appendf("%i", fTileHeight);
687 }
688 return name;
689}
690
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000691///////////////////////////////////////////////////////////////////////////////////////////////
692
693// Holds all of the information needed to draw a set of tiles.
694class CloneData : public SkRunnable {
695
696public:
697 CloneData(SkPicture* clone, SkCanvas* canvas, SkTDArray<SkRect>& rects, int start, int end,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000698 SkRunnable* done, ImageResultsSummary* jsonSummaryPtr)
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000699 : fClone(clone)
700 , fCanvas(canvas)
701 , fPath(NULL)
702 , fRects(rects)
703 , fStart(start)
704 , fEnd(end)
705 , fSuccess(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000706 , fDone(done)
707 , fJsonSummaryPtr(jsonSummaryPtr) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000708 SkASSERT(fDone != NULL);
709 }
710
711 virtual void run() SK_OVERRIDE {
712 SkGraphics::SetTLSFontCacheLimit(1024 * 1024);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000713
714 SkBitmap bitmap;
715 if (fBitmap != NULL) {
716 // All tiles are the same size.
jvanverth@google.com9c4e5ac2013-01-07 18:41:28 +0000717 setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScalarFloorToInt(fRects[0].height()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000718 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000719
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000720 for (int i = fStart; i < fEnd; i++) {
721 DrawTileToCanvas(fCanvas, fRects[i], fClone);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000722 if ((fPath != NULL) && !writeAppendNumber(fCanvas, fPath, i, fJsonSummaryPtr)
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000723 && fSuccess != NULL) {
724 *fSuccess = false;
725 // If one tile fails to write to a file, do not continue drawing the rest.
726 break;
727 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000728 if (fBitmap != NULL) {
729 if (fCanvas->readPixels(&bitmap, 0, 0)) {
730 SkAutoLockPixels alp(*fBitmap);
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000731 bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRects[i].left()),
732 SkScalarFloorToInt(fRects[i].top()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000733 } else {
734 *fSuccess = false;
735 // If one tile fails to read pixels, do not continue drawing the rest.
736 break;
737 }
738 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000739 }
740 fDone->run();
741 }
742
743 void setPathAndSuccess(const SkString* path, bool* success) {
744 fPath = path;
745 fSuccess = success;
746 }
747
edisonn@google.com84f548c2012-12-18 22:24:03 +0000748 void setBitmap(SkBitmap* bitmap) {
749 fBitmap = bitmap;
750 }
751
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000752private:
753 // All pointers unowned.
754 SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which
755 // is threadsafe.
756 SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile.
757 const SkString* fPath; // If non-null, path to write the result to as a PNG.
758 SkTDArray<SkRect>& fRects; // All tiles of the picture.
759 const int fStart; // Range of tiles drawn by this thread.
760 const int fEnd;
761 bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads,
762 // and only set to false upon failure to write to a PNG.
763 SkRunnable* fDone;
edisonn@google.com84f548c2012-12-18 22:24:03 +0000764 SkBitmap* fBitmap;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000765 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000766};
767
768MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount)
769: fNumThreads(threadCount)
770, fThreadPool(threadCount)
771, fCountdown(threadCount) {
772 // Only need to create fNumThreads - 1 clones, since one thread will use the base
773 // picture.
774 fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1);
775 fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads);
776}
777
778void MultiCorePictureRenderer::init(SkPicture *pict) {
779 // Set fPicture and the tiles.
780 this->INHERITED::init(pict);
781 for (int i = 0; i < fNumThreads; ++i) {
782 *fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight());
783 }
784 // Only need to create fNumThreads - 1 clones, since one thread will use the base picture.
785 fPicture->clone(fPictureClones, fNumThreads - 1);
786 // Populate each thread with the appropriate data.
787 // Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all.
788 const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads;
789
790 for (int i = 0; i < fNumThreads; i++) {
791 SkPicture* pic;
792 if (i == fNumThreads-1) {
793 // The last set will use the original SkPicture.
794 pic = fPicture;
795 } else {
796 pic = &fPictureClones[i];
797 }
798 const int start = i * chunkSize;
799 const int end = SkMin32(start + chunkSize, fTileRects.count());
800 fCloneData[i] = SkNEW_ARGS(CloneData,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000801 (pic, fCanvasPool[i], fTileRects, start, end, &fCountdown,
802 fJsonSummaryPtr));
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000803 }
804}
805
edisonn@google.com84f548c2012-12-18 22:24:03 +0000806bool MultiCorePictureRenderer::render(const SkString *path, SkBitmap** out) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000807 bool success = true;
808 if (path != NULL) {
809 for (int i = 0; i < fNumThreads-1; i++) {
810 fCloneData[i]->setPathAndSuccess(path, &success);
811 }
812 }
813
edisonn@google.com84f548c2012-12-18 22:24:03 +0000814 if (NULL != out) {
815 *out = SkNEW(SkBitmap);
816 setup_bitmap(*out, fPicture->width(), fPicture->height());
817 for (int i = 0; i < fNumThreads; i++) {
818 fCloneData[i]->setBitmap(*out);
819 }
820 } else {
821 for (int i = 0; i < fNumThreads; i++) {
822 fCloneData[i]->setBitmap(NULL);
823 }
824 }
825
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000826 fCountdown.reset(fNumThreads);
827 for (int i = 0; i < fNumThreads; i++) {
828 fThreadPool.add(fCloneData[i]);
829 }
830 fCountdown.wait();
831
832 return success;
833}
834
835void MultiCorePictureRenderer::end() {
836 for (int i = 0; i < fNumThreads - 1; i++) {
837 SkDELETE(fCloneData[i]);
838 fCloneData[i] = NULL;
839 }
840
841 fCanvasPool.unrefAll();
842
843 this->INHERITED::end();
844}
845
846MultiCorePictureRenderer::~MultiCorePictureRenderer() {
847 // Each individual CloneData was deleted in end.
848 SkDELETE_ARRAY(fCloneData);
849 SkDELETE_ARRAY(fPictureClones);
850}
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000851
scroggo@google.com0a049b82012-11-02 22:01:26 +0000852SkString MultiCorePictureRenderer::getConfigNameInternal() {
853 SkString name = this->INHERITED::getConfigNameInternal();
854 name.appendf("_multi_%i_threads", fNumThreads);
855 return name;
856}
857
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000858///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.com9a412522012-09-07 15:21:18 +0000859
860void PlaybackCreationRenderer::setup() {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000861 fReplayer.reset(this->createPicture());
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000862 SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->getViewHeight(),
junov@chromium.org9313ca42012-11-02 18:11:49 +0000863 this->recordFlags());
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000864 this->scaleToScaleFactor(recorder);
scroggo@google.com9a412522012-09-07 15:21:18 +0000865 fPicture->draw(recorder);
866}
867
edisonn@google.com84f548c2012-12-18 22:24:03 +0000868bool PlaybackCreationRenderer::render(const SkString*, SkBitmap** out) {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000869 fReplayer->endRecording();
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000870 // Since this class does not actually render, return false.
871 return false;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000872}
873
scroggo@google.com0a049b82012-11-02 22:01:26 +0000874SkString PlaybackCreationRenderer::getConfigNameInternal() {
875 return SkString("playback_creation");
876}
877
junov@chromium.org9313ca42012-11-02 18:11:49 +0000878///////////////////////////////////////////////////////////////////////////////////////////////
879// SkPicture variants for each BBoxHierarchy type
880
881class RTreePicture : public SkPicture {
882public:
883 virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{
884 static const int kRTreeMinChildren = 6;
885 static const int kRTreeMaxChildren = 11;
886 SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth),
887 SkIntToScalar(fHeight));
sglez@google.com8c902122013-08-30 17:27:47 +0000888 bool sortDraws = false;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000889 return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren,
sglez@google.com8c902122013-08-30 17:27:47 +0000890 aspectRatio, sortDraws);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000891 }
892};
893
894SkPicture* PictureRenderer::createPicture() {
895 switch (fBBoxHierarchyType) {
896 case kNone_BBoxHierarchyType:
897 return SkNEW(SkPicture);
898 case kRTree_BBoxHierarchyType:
899 return SkNEW(RTreePicture);
junov@chromium.org7b537062012-11-06 18:58:43 +0000900 case kTileGrid_BBoxHierarchyType:
junov@chromium.org29b19e52013-02-27 18:35:16 +0000901 return SkNEW_ARGS(SkTileGridPicture, (fPicture->width(),
902 fPicture->height(), fGridInfo));
junov@chromium.org9313ca42012-11-02 18:11:49 +0000903 }
904 SkASSERT(0); // invalid bbhType
905 return NULL;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000906}
junov@chromium.org9313ca42012-11-02 18:11:49 +0000907
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000908///////////////////////////////////////////////////////////////////////////////
909
910class GatherRenderer : public PictureRenderer {
911public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000912 virtual bool render(const SkString* path, SkBitmap** out = NULL)
913 SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000914 SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
915 SkIntToScalar(fPicture->height()));
916 SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds);
917 SkSafeUnref(data);
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000918
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000919 return NULL == path; // we don't have anything to write
920 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000921
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000922private:
923 virtual SkString getConfigNameInternal() SK_OVERRIDE {
924 return SkString("gather_pixelrefs");
925 }
926};
927
928PictureRenderer* CreateGatherPixelRefsRenderer() {
929 return SkNEW(GatherRenderer);
930}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000931
reed@google.com5a34fd32012-12-10 16:05:09 +0000932///////////////////////////////////////////////////////////////////////////////
933
934class PictureCloneRenderer : public PictureRenderer {
935public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000936 virtual bool render(const SkString* path, SkBitmap** out = NULL)
937 SK_OVERRIDE {
reed@google.com5a34fd32012-12-10 16:05:09 +0000938 for (int i = 0; i < 100; ++i) {
939 SkPicture* clone = fPicture->clone();
940 SkSafeUnref(clone);
941 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000942
reed@google.com5a34fd32012-12-10 16:05:09 +0000943 return NULL == path; // we don't have anything to write
944 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000945
reed@google.com5a34fd32012-12-10 16:05:09 +0000946private:
947 virtual SkString getConfigNameInternal() SK_OVERRIDE {
948 return SkString("picture_clone");
949 }
950};
951
952PictureRenderer* CreatePictureCloneRenderer() {
953 return SkNEW(PictureCloneRenderer);
954}
955
junov@chromium.org9313ca42012-11-02 18:11:49 +0000956} // namespace sk_tools