blob: d337a5ebb7df295c0828ed3bf2f6bcc0d8c36b34 [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"
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +000025#include "SkOSFile.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000026#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000027#include "SkPictureRecorder.h"
scroggo@google.com1b1bcc32013-05-21 20:31:23 +000028#include "SkPictureUtils.h"
29#include "SkPixelRef.h"
keyar@chromium.orgea826952012-08-23 15:24:13 +000030#include "SkScalar.h"
scroggo@google.coma9e3a362012-11-07 17:52:48 +000031#include "SkStream.h"
keyar@chromium.org9299ede2012-08-21 19:05:08 +000032#include "SkString.h"
scroggo@google.com58b4ead2012-08-31 16:15:22 +000033#include "SkTemplates.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.org3f045172014-05-15 15:10:48 +000051void PictureRenderer::init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000052 const SkString* inputFilename, bool useChecksumBasedFilenames) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000053 this->CopyString(&fWritePath, writePath);
54 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000055 this->CopyString(&fInputFilename, inputFilename);
56 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
57
keyar@chromium.org78a35c52012-08-20 15:03:44 +000058 SkASSERT(NULL == fPicture);
59 SkASSERT(NULL == fCanvas.get());
robertphillips@google.com84b18c72014-04-13 19:09:42 +000060 if (NULL != fPicture || NULL != fCanvas.get()) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000061 return;
62 }
63
64 SkASSERT(pict != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000065 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000066 return;
67 }
68
robertphillips@google.com84b18c72014-04-13 19:09:42 +000069 fPicture.reset(pict)->ref();
keyar@chromium.orga474ce32012-08-20 15:03:57 +000070 fCanvas.reset(this->setupCanvas());
71}
72
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000073void PictureRenderer::CopyString(SkString* dest, const SkString* src) {
74 if (NULL != src) {
75 dest->set(*src);
76 } else {
77 dest->reset();
78 }
79}
80
caryclark@google.coma3622372012-11-06 21:26:13 +000081class FlagsDrawFilter : public SkDrawFilter {
82public:
83 FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) :
84 fFlags(flags) {}
85
reed@google.com971aca72012-11-26 20:26:54 +000086 virtual bool filter(SkPaint* paint, Type t) {
caryclark@google.coma3622372012-11-06 21:26:13 +000087 paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags);
robertphillips@google.com49149312013-07-03 15:34:35 +000088 if (PictureRenderer::kMaskFilter_DrawFilterFlag & fFlags[t]) {
caryclark@google.coma3622372012-11-06 21:26:13 +000089 SkMaskFilter* maskFilter = paint->getMaskFilter();
robertphillips@google.com49149312013-07-03 15:34:35 +000090 if (NULL != maskFilter) {
reed@google.com457d8a72012-12-18 18:20:44 +000091 paint->setMaskFilter(NULL);
caryclark@google.coma3622372012-11-06 21:26:13 +000092 }
93 }
94 if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) {
95 paint->setHinting(SkPaint::kNo_Hinting);
96 } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) {
97 paint->setHinting(SkPaint::kSlight_Hinting);
98 }
reed@google.com971aca72012-11-26 20:26:54 +000099 return true;
caryclark@google.coma3622372012-11-06 21:26:13 +0000100 }
101
102private:
103 PictureRenderer::DrawFilterFlags* fFlags;
104};
105
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000106static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000107 if (drawFilters && !canvas->getDrawFilter()) {
108 canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref();
caryclark@google.come3e940c2012-11-07 16:42:17 +0000109 if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) {
110 canvas->setAllowSoftClip(false);
111 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000112 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000113}
114
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000115SkCanvas* PictureRenderer::setupCanvas() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000116 const int width = this->getViewWidth();
117 const int height = this->getViewHeight();
118 return this->setupCanvas(width, height);
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000119}
120
121SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000122 SkCanvas* canvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000123 switch(fDeviceType) {
124 case kBitmap_DeviceType: {
125 SkBitmap bitmap;
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000126 sk_tools::setup_bitmap(&bitmap, width, height);
caryclark@google.coma3622372012-11-06 21:26:13 +0000127 canvas = SkNEW_ARGS(SkCanvas, (bitmap));
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000128 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000129 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000130#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000131#if SK_ANGLE
132 case kAngle_DeviceType:
133 // fall through
134#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000135#if SK_MESA
136 case kMesa_DeviceType:
137 // fall through
138#endif
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000139 case kGPU_DeviceType:
140 case kNVPR_DeviceType: {
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000141 SkAutoTUnref<GrSurface> target;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000142 if (fGrContext) {
143 // create a render target to back the device
144 GrTextureDesc desc;
145 desc.fConfig = kSkia8888_GrPixelConfig;
146 desc.fFlags = kRenderTarget_GrTextureFlagBit;
147 desc.fWidth = width;
148 desc.fHeight = height;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000149 desc.fSampleCnt = fSampleCount;
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000150 target.reset(fGrContext->createUncachedTexture(desc, NULL, 0));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000151 }
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000152 if (NULL == target.get()) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000153 SkASSERT(0);
154 return NULL;
155 }
156
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000157 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target));
caryclark@google.coma3622372012-11-06 21:26:13 +0000158 canvas = SkNEW_ARGS(SkCanvas, (device.get()));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000159 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000160 }
161#endif
162 default:
163 SkASSERT(0);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000164 return NULL;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000165 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000166 setUpFilter(canvas, fDrawFilters);
167 this->scaleToScaleFactor(canvas);
commit-bot@chromium.org17cc3ea2014-01-15 14:51:25 +0000168
169 // Pictures often lie about their extent (i.e., claim to be 100x100 but
170 // only ever draw to 90x100). Clear here so the undrawn portion will have
171 // a consistent color
172 canvas->clear(SK_ColorTRANSPARENT);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000173 return canvas;
174}
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000175
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000176void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) {
177 SkASSERT(canvas != NULL);
178 if (fScaleFactor != SK_Scalar1) {
179 canvas->scale(fScaleFactor, fScaleFactor);
180 }
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000181}
182
183void PictureRenderer::end() {
scroggo@google.com08085f82013-01-28 20:40:24 +0000184 this->resetState(true);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000185 fPicture.reset(NULL);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000186 fCanvas.reset(NULL);
187}
188
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000189int PictureRenderer::getViewWidth() {
190 SkASSERT(fPicture != NULL);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000191 int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000192 if (fViewport.width() > 0) {
193 width = SkMin32(width, fViewport.width());
194 }
195 return width;
196}
197
198int PictureRenderer::getViewHeight() {
199 SkASSERT(fPicture != NULL);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000200 int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000201 if (fViewport.height() > 0) {
202 height = SkMin32(height, fViewport.height());
203 }
204 return height;
205}
206
junov@chromium.org9313ca42012-11-02 18:11:49 +0000207/** Converts fPicture to a picture that uses a BBoxHierarchy.
208 * PictureRenderer subclasses that are used to test picture playback
209 * should call this method during init.
210 */
211void PictureRenderer::buildBBoxHierarchy() {
212 SkASSERT(NULL != fPicture);
213 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000214 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
215 SkPictureRecorder recorder;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000216 SkCanvas* canvas = recorder.beginRecording(fPicture->width(), fPicture->height(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000217 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000218 this->recordFlags());
219 fPicture->draw(canvas);
220 fPicture.reset(recorder.endRecording());
junov@chromium.org9313ca42012-11-02 18:11:49 +0000221 }
222}
223
scroggo@google.com08085f82013-01-28 20:40:24 +0000224void PictureRenderer::resetState(bool callFinish) {
keyar@chromium.org28136b32012-08-20 15:04:15 +0000225#if SK_SUPPORT_GPU
robertphillips@google.com6177e692013-02-28 20:16:25 +0000226 SkGLContextHelper* glContext = this->getGLContext();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000227 if (NULL == glContext) {
228 SkASSERT(kBitmap_DeviceType == fDeviceType);
229 return;
230 }
keyar@chromium.org28136b32012-08-20 15:04:15 +0000231
scroggo@google.com0556ea02013-02-08 19:38:21 +0000232 fGrContext->flush();
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000233 glContext->swapBuffers();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000234 if (callFinish) {
235 SK_GL(*glContext, Finish());
keyar@chromium.org77a55222012-08-20 15:03:47 +0000236 }
keyar@chromium.orga40c20d2012-08-20 15:04:12 +0000237#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +0000238}
239
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000240void PictureRenderer::purgeTextures() {
241 SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool();
242
243 pool->dumpPool();
244
245#if SK_SUPPORT_GPU
246 SkGLContextHelper* glContext = this->getGLContext();
247 if (NULL == glContext) {
248 SkASSERT(kBitmap_DeviceType == fDeviceType);
249 return;
250 }
251
252 // resetState should've already done this
253 fGrContext->flush();
254
255 fGrContext->purgeAllUnlockedResources();
256#endif
257}
258
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000259/**
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000260 * Write the canvas to an image file and/or JSON summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000261 *
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000262 * @param canvas Must be non-null. Canvas to be written to a file.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000263 * @param writePath If nonempty, write the binary image to a file within this directory.
264 * @param mismatchPath If nonempty, write the binary image to a file within this directory,
265 * but only if the image does not match expectations.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000266 * @param inputFilename If we are writing out a binary image, use this to build its filename.
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000267 * @param jsonSummaryPtr If not null, add image results (checksum) to this summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000268 * @param useChecksumBasedFilenames If true, use checksum-based filenames when writing to disk.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000269 * @param tileNumberPtr If not null, which tile number this image contains.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000270 *
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000271 * @return bool True if the operation completed successfully.
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000272 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000273static bool write(SkCanvas* canvas, const SkString& writePath, const SkString& mismatchPath,
274 const SkString& inputFilename, ImageResultsAndExpectations *jsonSummaryPtr,
275 bool useChecksumBasedFilenames, const int* tileNumberPtr=NULL) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000276 SkASSERT(canvas != NULL);
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000277 if (NULL == canvas) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000278 return false;
279 }
280
281 SkBitmap bitmap;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000282 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000283 setup_bitmap(&bitmap, size.width(), size.height());
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000284
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000285 canvas->readPixels(&bitmap, 0, 0);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000286 force_all_opaque(bitmap);
287 BitmapAndDigest bitmapAndDigest(bitmap);
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000288
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000289 SkString escapedInputFilename(inputFilename);
290 replace_char(&escapedInputFilename, '.', '_');
291
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000292 // TODO(epoger): what about including the config type within outputFilename? That way,
293 // we could combine results of different config types without conflicting filenames.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000294 SkString outputFilename;
295 const char *outputSubdirPtr = NULL;
296 if (useChecksumBasedFilenames) {
commit-bot@chromium.org661832c2014-05-21 11:57:16 +0000297 const ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000298 outputSubdirPtr = escapedInputFilename.c_str();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000299 outputFilename.set(imageDigestPtr->getHashType());
300 outputFilename.append("_");
301 outputFilename.appendU64(imageDigestPtr->getHashValue());
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000302 } else {
303 outputFilename.set(escapedInputFilename);
304 if (NULL != tileNumberPtr) {
305 outputFilename.append("-tile");
306 outputFilename.appendS32(*tileNumberPtr);
307 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000308 }
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000309 outputFilename.append(".png");
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000310
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000311 if (NULL != jsonSummaryPtr) {
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000312 const ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000313 SkString outputRelativePath;
314 if (outputSubdirPtr) {
315 outputRelativePath.set(outputSubdirPtr);
316 outputRelativePath.append("/"); // always use "/", even on Windows
317 outputRelativePath.append(outputFilename);
318 } else {
319 outputRelativePath.set(outputFilename);
320 }
321
322 jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(),
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000323 *imageDigestPtr, tileNumberPtr);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000324 if (!mismatchPath.isEmpty() &&
325 !jsonSummaryPtr->matchesExpectation(inputFilename.c_str(), *imageDigestPtr,
326 tileNumberPtr)) {
327 if (!write_bitmap_to_disk(bitmap, mismatchPath, outputSubdirPtr, outputFilename)) {
328 return false;
329 }
330 }
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000331 }
332
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000333 if (writePath.isEmpty()) {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000334 return true;
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000335 } else {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000336 return write_bitmap_to_disk(bitmap, writePath, outputSubdirPtr, outputFilename);
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000337 }
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000338}
339
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000340///////////////////////////////////////////////////////////////////////////////////////////////
341
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000342SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) {
343 // defer the canvas setup until the render step
344 return NULL;
345}
346
reed@google.com672588b2014-01-08 15:42:01 +0000347// the size_t* parameter is deprecated, so we ignore it
348static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000349 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000350}
351
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000352bool RecordPictureRenderer::render(SkBitmap** out) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000353 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
354 SkPictureRecorder recorder;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000355 SkCanvas* canvas = recorder.beginRecording(this->getViewWidth(), this->getViewHeight(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000356 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000357 this->recordFlags());
358 this->scaleToScaleFactor(canvas);
359 fPicture->draw(canvas);
360 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000361 if (!fWritePath.isEmpty()) {
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000362 // Record the new picture as a new SKP with PNG encoded bitmaps.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000363 SkString skpPath = SkOSPath::SkPathJoin(fWritePath.c_str(), fInputFilename.c_str());
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000364 SkFILEWStream stream(skpPath.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000365 picture->serialize(&stream, &encode_bitmap_to_data);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000366 return true;
367 }
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000368 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +0000369}
370
scroggo@google.com0a049b82012-11-02 22:01:26 +0000371SkString RecordPictureRenderer::getConfigNameInternal() {
372 return SkString("record");
373}
374
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000375///////////////////////////////////////////////////////////////////////////////////////////////
376
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000377bool PipePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000378 SkASSERT(fCanvas.get() != NULL);
379 SkASSERT(fPicture != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000380 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000381 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000382 }
383
384 PipeController pipeController(fCanvas.get());
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000385 SkGPipeWriter writer;
386 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
robertphillips9b14f262014-06-04 05:40:44 -0700387 pipeCanvas->drawPicture(fPicture);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000388 writer.endRecording();
scroggo@google.com9a412522012-09-07 15:21:18 +0000389 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000390 if (NULL != out) {
391 *out = SkNEW(SkBitmap);
392 setup_bitmap(*out, fPicture->width(), fPicture->height());
393 fCanvas->readPixels(*out, 0, 0);
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000394 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000395 if (fEnableWrites) {
396 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
397 fUseChecksumBasedFilenames);
398 } else {
399 return true;
400 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000401}
402
scroggo@google.com0a049b82012-11-02 22:01:26 +0000403SkString PipePictureRenderer::getConfigNameInternal() {
404 return SkString("pipe");
405}
406
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000407///////////////////////////////////////////////////////////////////////////////////////////////
408
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000409void SimplePictureRenderer::init(SkPicture* picture, const SkString* writePath,
410 const SkString* mismatchPath, const SkString* inputFilename,
411 bool useChecksumBasedFilenames) {
412 INHERITED::init(picture, writePath, mismatchPath, inputFilename, useChecksumBasedFilenames);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000413 this->buildBBoxHierarchy();
414}
415
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000416bool SimplePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000417 SkASSERT(fCanvas.get() != NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000418 SkASSERT(NULL != fPicture);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000419 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000420 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000421 }
422
robertphillips9b14f262014-06-04 05:40:44 -0700423 fCanvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000424 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000425 if (NULL != out) {
426 *out = SkNEW(SkBitmap);
427 setup_bitmap(*out, fPicture->width(), fPicture->height());
428 fCanvas->readPixels(*out, 0, 0);
429 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000430 if (fEnableWrites) {
431 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
432 fUseChecksumBasedFilenames);
433 } else {
434 return true;
435 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000436}
437
scroggo@google.com0a049b82012-11-02 22:01:26 +0000438SkString SimplePictureRenderer::getConfigNameInternal() {
439 return SkString("simple");
440}
441
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000442///////////////////////////////////////////////////////////////////////////////////////////////
443
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000444TiledPictureRenderer::TiledPictureRenderer()
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000445 : fTileWidth(kDefaultTileWidth)
rileya@google.comb947b912012-08-29 17:35:07 +0000446 , fTileHeight(kDefaultTileHeight)
rileya@google.coma04dc022012-09-10 19:01:38 +0000447 , fTileWidthPercentage(0.0)
rileya@google.comb947b912012-08-29 17:35:07 +0000448 , fTileHeightPercentage(0.0)
scroggo@google.comcbcef702012-12-13 22:09:28 +0000449 , fTileMinPowerOf2Width(0)
450 , fCurrentTileOffset(-1)
451 , fTilesX(0)
452 , fTilesY(0) { }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000453
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000454void TiledPictureRenderer::init(SkPicture* pict, const SkString* writePath,
455 const SkString* mismatchPath, const SkString* inputFilename,
456 bool useChecksumBasedFilenames) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000457 SkASSERT(NULL != pict);
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000458 SkASSERT(0 == fTileRects.count());
459 if (NULL == pict || fTileRects.count() != 0) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000460 return;
461 }
462
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000463 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
464 // used by bench_pictures.
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000465 fPicture.reset(pict)->ref();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000466 this->CopyString(&fWritePath, writePath);
467 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000468 this->CopyString(&fInputFilename, inputFilename);
469 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000470 this->buildBBoxHierarchy();
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000471
472 if (fTileWidthPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000473 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000474 }
475 if (fTileHeightPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000476 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000477 }
478
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000479 if (fTileMinPowerOf2Width > 0) {
480 this->setupPowerOf2Tiles();
481 } else {
482 this->setupTiles();
483 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000484 fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight));
485 // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the
486 // first call to drawCurrentTile.
487 fCurrentTileOffset = -1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000488}
489
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000490void TiledPictureRenderer::end() {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000491 fTileRects.reset();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000492 this->INHERITED::end();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000493}
494
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000495void TiledPictureRenderer::setupTiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000496 // Only use enough tiles to cover the viewport
497 const int width = this->getViewWidth();
498 const int height = this->getViewHeight();
499
scroggo@google.comcbcef702012-12-13 22:09:28 +0000500 fTilesX = fTilesY = 0;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000501 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000502 fTilesY++;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000503 for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000504 if (0 == tile_y_start) {
505 // Only count tiles in the X direction on the first pass.
506 fTilesX++;
507 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000508 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
509 SkIntToScalar(tile_y_start),
510 SkIntToScalar(fTileWidth),
511 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000512 }
513 }
514}
515
scroggo@google.comcbcef702012-12-13 22:09:28 +0000516bool TiledPictureRenderer::tileDimensions(int &x, int &y) {
517 if (fTileRects.count() == 0 || NULL == fPicture) {
518 return false;
519 }
520 x = fTilesX;
521 y = fTilesY;
522 return true;
523}
524
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000525// The goal of the powers of two tiles is to minimize the amount of wasted tile
526// space in the width-wise direction and then minimize the number of tiles. The
527// constraints are that every tile must have a pixel width that is a power of
528// two and also be of some minimal width (that is also a power of two).
529//
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000530// This is solved by first taking our picture size and rounding it up to the
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000531// multiple of the minimal width. The binary representation of this rounded
532// value gives us the tiles we need: a bit of value one means we need a tile of
533// that size.
534void TiledPictureRenderer::setupPowerOf2Tiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000535 // Only use enough tiles to cover the viewport
536 const int width = this->getViewWidth();
537 const int height = this->getViewHeight();
538
539 int rounded_value = width;
540 if (width % fTileMinPowerOf2Width != 0) {
541 rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000542 }
543
reed@google.come15b2f52013-12-18 04:59:26 +0000544 int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width)));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000545 int largest_possible_tile_size = 1 << num_bits;
546
scroggo@google.comcbcef702012-12-13 22:09:28 +0000547 fTilesX = fTilesY = 0;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000548 // The tile height is constant for a particular picture.
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000549 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000550 fTilesY++;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000551 int tile_x_start = 0;
552 int current_width = largest_possible_tile_size;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000553 // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
554 // to draw each tile.
555 fTileWidth = current_width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000556
557 while (current_width >= fTileMinPowerOf2Width) {
558 // It is very important this is a bitwise AND.
559 if (current_width & rounded_value) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000560 if (0 == tile_y_start) {
561 // Only count tiles in the X direction on the first pass.
562 fTilesX++;
563 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000564 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
565 SkIntToScalar(tile_y_start),
566 SkIntToScalar(current_width),
567 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000568 tile_x_start += current_width;
569 }
570
571 current_width >>= 1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000572 }
573 }
574}
575
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000576/**
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000577 * Draw the specified picture to the canvas translated to rectangle provided, so that this mini
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000578 * canvas represents the rectangle's portion of the overall picture.
579 * Saves and restores so that the initial clip and matrix return to their state before this function
580 * is called.
581 */
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000582static void draw_tile_to_canvas(SkCanvas* canvas, const SkRect& tileRect, SkPicture* picture) {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000583 int saveCount = canvas->save();
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000584 // Translate so that we draw the correct portion of the picture.
585 // Perform a postTranslate so that the scaleFactor does not interfere with the positioning.
586 SkMatrix mat(canvas->getTotalMatrix());
587 mat.postTranslate(-tileRect.fLeft, -tileRect.fTop);
588 canvas->setMatrix(mat);
robertphillips9b14f262014-06-04 05:40:44 -0700589 canvas->drawPicture(picture);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000590 canvas->restoreToCount(saveCount);
591 canvas->flush();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000592}
593
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000594///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000595
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000596/**
597 * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap.
598 * If the src bitmap is too large to fit within the dst bitmap after the x and y
599 * offsets have been applied, any excess will be ignored (so only the top-left portion of the
600 * src bitmap will be copied).
601 *
602 * @param src source bitmap
603 * @param dst destination bitmap
604 * @param xOffset x-offset within destination bitmap
605 * @param yOffset y-offset within destination bitmap
606 */
607static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst,
608 int xOffset, int yOffset) {
609 for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) {
610 for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) {
611 *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000612 }
613 }
614}
615
scroggo@google.comcbcef702012-12-13 22:09:28 +0000616bool TiledPictureRenderer::nextTile(int &i, int &j) {
617 if (++fCurrentTileOffset < fTileRects.count()) {
618 i = fCurrentTileOffset % fTilesX;
619 j = fCurrentTileOffset / fTilesX;
620 return true;
621 }
622 return false;
623}
624
625void TiledPictureRenderer::drawCurrentTile() {
626 SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000627 draw_tile_to_canvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000628}
629
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000630bool TiledPictureRenderer::render(SkBitmap** out) {
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000631 SkASSERT(fPicture != NULL);
632 if (NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000633 return false;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000634 }
635
edisonn@google.com84f548c2012-12-18 22:24:03 +0000636 SkBitmap bitmap;
637 if (out){
638 *out = SkNEW(SkBitmap);
639 setup_bitmap(*out, fPicture->width(), fPicture->height());
640 setup_bitmap(&bitmap, fTileWidth, fTileHeight);
641 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000642 bool success = true;
643 for (int i = 0; i < fTileRects.count(); ++i) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000644 draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture);
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000645 if (fEnableWrites) {
646 success &= write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
647 fUseChecksumBasedFilenames, &i);
648 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000649 if (NULL != out) {
650 if (fCanvas->readPixels(&bitmap, 0, 0)) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000651 // Add this tile to the entire bitmap.
652 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()),
653 SkScalarFloorToInt(fTileRects[i].top()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000654 } else {
655 success = false;
656 }
657 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000658 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000659 return success;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000660}
661
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000662SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
663 SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000664 SkASSERT(NULL != fPicture);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000665 // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This
666 // is mostly important for tiles on the right and bottom edges as they may go over this area and
667 // the picture may have some commands that draw outside of this area and so should not actually
668 // be written.
669 // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set
670 // by INHERITED::setupCanvas.
671 SkRegion clipRegion;
672 clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight());
673 canvas->clipRegion(clipRegion);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000674 return canvas;
675}
scroggo@google.com0a049b82012-11-02 22:01:26 +0000676
677SkString TiledPictureRenderer::getConfigNameInternal() {
678 SkString name;
679 if (fTileMinPowerOf2Width > 0) {
680 name.append("pow2tile_");
681 name.appendf("%i", fTileMinPowerOf2Width);
682 } else {
683 name.append("tile_");
684 if (fTileWidthPercentage > 0) {
685 name.appendf("%.f%%", fTileWidthPercentage);
686 } else {
687 name.appendf("%i", fTileWidth);
688 }
689 }
690 name.append("x");
691 if (fTileHeightPercentage > 0) {
692 name.appendf("%.f%%", fTileHeightPercentage);
693 } else {
694 name.appendf("%i", fTileHeight);
695 }
696 return name;
697}
698
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000699///////////////////////////////////////////////////////////////////////////////////////////////
700
701// Holds all of the information needed to draw a set of tiles.
702class CloneData : public SkRunnable {
703
704public:
705 CloneData(SkPicture* clone, SkCanvas* canvas, SkTDArray<SkRect>& rects, int start, int end,
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000706 SkRunnable* done, ImageResultsAndExpectations* jsonSummaryPtr,
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000707 bool useChecksumBasedFilenames, bool enableWrites)
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000708 : fClone(clone)
709 , fCanvas(canvas)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000710 , fEnableWrites(enableWrites)
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000711 , fRects(rects)
712 , fStart(start)
713 , fEnd(end)
714 , fSuccess(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000715 , fDone(done)
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000716 , fJsonSummaryPtr(jsonSummaryPtr)
717 , fUseChecksumBasedFilenames(useChecksumBasedFilenames) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000718 SkASSERT(fDone != NULL);
719 }
720
721 virtual void run() SK_OVERRIDE {
722 SkGraphics::SetTLSFontCacheLimit(1024 * 1024);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000723
724 SkBitmap bitmap;
725 if (fBitmap != NULL) {
726 // All tiles are the same size.
jvanverth@google.com9c4e5ac2013-01-07 18:41:28 +0000727 setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScalarFloorToInt(fRects[0].height()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000728 }
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000729
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000730 for (int i = fStart; i < fEnd; i++) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000731 draw_tile_to_canvas(fCanvas, fRects[i], fClone);
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000732 if (fEnableWrites) {
733 if (!write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
734 fUseChecksumBasedFilenames, &i)
735 && fSuccess != NULL) {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000736 *fSuccess = false;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000737 // If one tile fails to write to a file, do not continue drawing the rest.
edisonn@google.com84f548c2012-12-18 22:24:03 +0000738 break;
739 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000740 if (fBitmap != NULL) {
741 if (fCanvas->readPixels(&bitmap, 0, 0)) {
742 SkAutoLockPixels alp(*fBitmap);
743 bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRects[i].left()),
744 SkScalarFloorToInt(fRects[i].top()));
745 } else {
746 *fSuccess = false;
747 // If one tile fails to read pixels, do not continue drawing the rest.
748 break;
749 }
750 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000751 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000752 }
753 fDone->run();
754 }
755
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000756 void setPathsAndSuccess(const SkString& writePath, const SkString& mismatchPath,
757 const SkString& inputFilename, bool* success) {
758 fWritePath.set(writePath);
759 fMismatchPath.set(mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000760 fInputFilename.set(inputFilename);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000761 fSuccess = success;
762 }
763
edisonn@google.com84f548c2012-12-18 22:24:03 +0000764 void setBitmap(SkBitmap* bitmap) {
765 fBitmap = bitmap;
766 }
767
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000768private:
769 // All pointers unowned.
770 SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which
771 // is threadsafe.
772 SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile.
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000773 bool fEnableWrites; // TODO(epoger): Temporary hack; see declaration of
774 // fEnableWrites in PictureRenderer.h.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000775 SkString fWritePath; // If not empty, write all results into this directory.
776 SkString fMismatchPath; // If not empty, write all unexpected results into this dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000777 SkString fInputFilename; // Filename of input SkPicture file.
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000778 SkTDArray<SkRect>& fRects; // All tiles of the picture.
779 const int fStart; // Range of tiles drawn by this thread.
780 const int fEnd;
781 bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads,
782 // and only set to false upon failure to write to a PNG.
783 SkRunnable* fDone;
edisonn@google.com84f548c2012-12-18 22:24:03 +0000784 SkBitmap* fBitmap;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000785 ImageResultsAndExpectations* fJsonSummaryPtr;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000786 bool fUseChecksumBasedFilenames;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000787};
788
789MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount)
790: fNumThreads(threadCount)
791, fThreadPool(threadCount)
792, fCountdown(threadCount) {
793 // Only need to create fNumThreads - 1 clones, since one thread will use the base
794 // picture.
795 fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1);
796 fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads);
797}
798
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000799void MultiCorePictureRenderer::init(SkPicture *pict, const SkString* writePath,
800 const SkString* mismatchPath, const SkString* inputFilename,
801 bool useChecksumBasedFilenames) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000802 // Set fPicture and the tiles.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000803 this->INHERITED::init(pict, writePath, mismatchPath, inputFilename, useChecksumBasedFilenames);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000804 for (int i = 0; i < fNumThreads; ++i) {
805 *fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight());
806 }
807 // Only need to create fNumThreads - 1 clones, since one thread will use the base picture.
808 fPicture->clone(fPictureClones, fNumThreads - 1);
809 // Populate each thread with the appropriate data.
810 // Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all.
811 const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads;
812
813 for (int i = 0; i < fNumThreads; i++) {
814 SkPicture* pic;
815 if (i == fNumThreads-1) {
816 // The last set will use the original SkPicture.
817 pic = fPicture;
818 } else {
819 pic = &fPictureClones[i];
820 }
821 const int start = i * chunkSize;
822 const int end = SkMin32(start + chunkSize, fTileRects.count());
823 fCloneData[i] = SkNEW_ARGS(CloneData,
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000824 (pic, fCanvasPool[i], fTileRects, start, end, &fCountdown,
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000825 fJsonSummaryPtr, useChecksumBasedFilenames, fEnableWrites));
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000826 }
827}
828
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000829bool MultiCorePictureRenderer::render(SkBitmap** out) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000830 bool success = true;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000831 if (!fWritePath.isEmpty() || !fMismatchPath.isEmpty()) {
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000832 for (int i = 0; i < fNumThreads-1; i++) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000833 fCloneData[i]->setPathsAndSuccess(fWritePath, fMismatchPath, fInputFilename, &success);
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000834 }
835 }
836
edisonn@google.com84f548c2012-12-18 22:24:03 +0000837 if (NULL != out) {
838 *out = SkNEW(SkBitmap);
839 setup_bitmap(*out, fPicture->width(), fPicture->height());
840 for (int i = 0; i < fNumThreads; i++) {
841 fCloneData[i]->setBitmap(*out);
842 }
843 } else {
844 for (int i = 0; i < fNumThreads; i++) {
845 fCloneData[i]->setBitmap(NULL);
846 }
847 }
848
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000849 fCountdown.reset(fNumThreads);
850 for (int i = 0; i < fNumThreads; i++) {
851 fThreadPool.add(fCloneData[i]);
852 }
853 fCountdown.wait();
854
855 return success;
856}
857
858void MultiCorePictureRenderer::end() {
859 for (int i = 0; i < fNumThreads - 1; i++) {
860 SkDELETE(fCloneData[i]);
861 fCloneData[i] = NULL;
862 }
863
864 fCanvasPool.unrefAll();
865
866 this->INHERITED::end();
867}
868
869MultiCorePictureRenderer::~MultiCorePictureRenderer() {
870 // Each individual CloneData was deleted in end.
871 SkDELETE_ARRAY(fCloneData);
872 SkDELETE_ARRAY(fPictureClones);
873}
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000874
scroggo@google.com0a049b82012-11-02 22:01:26 +0000875SkString MultiCorePictureRenderer::getConfigNameInternal() {
876 SkString name = this->INHERITED::getConfigNameInternal();
877 name.appendf("_multi_%i_threads", fNumThreads);
878 return name;
879}
880
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000881///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.com9a412522012-09-07 15:21:18 +0000882
883void PlaybackCreationRenderer::setup() {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000884 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
885 fRecorder.reset(SkNEW(SkPictureRecorder));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000886 SkCanvas* canvas = fRecorder->beginRecording(this->getViewWidth(), this->getViewHeight(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000887 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000888 this->recordFlags());
889 this->scaleToScaleFactor(canvas);
robertphillips9b14f262014-06-04 05:40:44 -0700890 canvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000891}
892
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000893bool PlaybackCreationRenderer::render(SkBitmap** out) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000894 fPicture.reset(fRecorder->endRecording());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000895 // Since this class does not actually render, return false.
896 return false;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000897}
898
scroggo@google.com0a049b82012-11-02 22:01:26 +0000899SkString PlaybackCreationRenderer::getConfigNameInternal() {
900 return SkString("playback_creation");
901}
902
junov@chromium.org9313ca42012-11-02 18:11:49 +0000903///////////////////////////////////////////////////////////////////////////////////////////////
904// SkPicture variants for each BBoxHierarchy type
905
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000906SkBBHFactory* PictureRenderer::getFactory() {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000907 switch (fBBoxHierarchyType) {
908 case kNone_BBoxHierarchyType:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000909 return NULL;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000910 case kQuadTree_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000911 return SkNEW(SkQuadTreeFactory);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000912 case kRTree_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000913 return SkNEW(SkRTreeFactory);
junov@chromium.org7b537062012-11-06 18:58:43 +0000914 case kTileGrid_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000915 return SkNEW_ARGS(SkTileGridFactory, (fGridInfo));
junov@chromium.org9313ca42012-11-02 18:11:49 +0000916 }
917 SkASSERT(0); // invalid bbhType
918 return NULL;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000919}
junov@chromium.org9313ca42012-11-02 18:11:49 +0000920
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000921///////////////////////////////////////////////////////////////////////////////
922
923class GatherRenderer : public PictureRenderer {
924public:
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000925 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000926 SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
927 SkIntToScalar(fPicture->height()));
928 SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds);
929 SkSafeUnref(data);
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000930
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000931 return (fWritePath.isEmpty()); // we don't have anything to write
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000932 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000933
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000934private:
935 virtual SkString getConfigNameInternal() SK_OVERRIDE {
936 return SkString("gather_pixelrefs");
937 }
938};
939
940PictureRenderer* CreateGatherPixelRefsRenderer() {
941 return SkNEW(GatherRenderer);
942}
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000943
reed@google.com5a34fd32012-12-10 16:05:09 +0000944///////////////////////////////////////////////////////////////////////////////
945
946class PictureCloneRenderer : public PictureRenderer {
947public:
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000948 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE {
reed@google.com5a34fd32012-12-10 16:05:09 +0000949 for (int i = 0; i < 100; ++i) {
950 SkPicture* clone = fPicture->clone();
951 SkSafeUnref(clone);
952 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000953
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000954 return (fWritePath.isEmpty()); // we don't have anything to write
reed@google.com5a34fd32012-12-10 16:05:09 +0000955 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000956
reed@google.com5a34fd32012-12-10 16:05:09 +0000957private:
958 virtual SkString getConfigNameInternal() SK_OVERRIDE {
959 return SkString("picture_clone");
960 }
961};
962
963PictureRenderer* CreatePictureCloneRenderer() {
964 return SkNEW(PictureCloneRenderer);
965}
966
junov@chromium.org9313ca42012-11-02 18:11:49 +0000967} // namespace sk_tools