blob: ed5bdfe6125e697e5babbb545081f9ffe5dd25a0 [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
mtklein2a65a232014-08-26 14:07:04 -070051void PictureRenderer::init(const SkPicture* pict,
52 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -070053 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -070054 const SkString* inputFilename,
robertphillipsce4dd3d2014-07-07 13:46:35 -070055 bool useChecksumBasedFilenames) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000056 this->CopyString(&fWritePath, writePath);
57 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000058 this->CopyString(&fInputFilename, inputFilename);
59 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
60
keyar@chromium.org78a35c52012-08-20 15:03:44 +000061 SkASSERT(NULL == fPicture);
62 SkASSERT(NULL == fCanvas.get());
robertphillips@google.com84b18c72014-04-13 19:09:42 +000063 if (NULL != fPicture || NULL != fCanvas.get()) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000064 return;
65 }
66
67 SkASSERT(pict != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +000068 if (NULL == pict) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +000069 return;
70 }
71
robertphillips@google.com84b18c72014-04-13 19:09:42 +000072 fPicture.reset(pict)->ref();
keyar@chromium.orga474ce32012-08-20 15:03:57 +000073 fCanvas.reset(this->setupCanvas());
74}
75
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000076void PictureRenderer::CopyString(SkString* dest, const SkString* src) {
77 if (NULL != src) {
78 dest->set(*src);
79 } else {
80 dest->reset();
81 }
82}
83
caryclark@google.coma3622372012-11-06 21:26:13 +000084class FlagsDrawFilter : public SkDrawFilter {
85public:
86 FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) :
87 fFlags(flags) {}
88
reed@google.com971aca72012-11-26 20:26:54 +000089 virtual bool filter(SkPaint* paint, Type t) {
caryclark@google.coma3622372012-11-06 21:26:13 +000090 paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags);
robertphillips@google.com49149312013-07-03 15:34:35 +000091 if (PictureRenderer::kMaskFilter_DrawFilterFlag & fFlags[t]) {
caryclark@google.coma3622372012-11-06 21:26:13 +000092 SkMaskFilter* maskFilter = paint->getMaskFilter();
robertphillips@google.com49149312013-07-03 15:34:35 +000093 if (NULL != maskFilter) {
reed@google.com457d8a72012-12-18 18:20:44 +000094 paint->setMaskFilter(NULL);
caryclark@google.coma3622372012-11-06 21:26:13 +000095 }
96 }
97 if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) {
98 paint->setHinting(SkPaint::kNo_Hinting);
99 } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) {
100 paint->setHinting(SkPaint::kSlight_Hinting);
101 }
reed@google.com971aca72012-11-26 20:26:54 +0000102 return true;
caryclark@google.coma3622372012-11-06 21:26:13 +0000103 }
104
105private:
106 PictureRenderer::DrawFilterFlags* fFlags;
107};
108
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000109static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000110 if (drawFilters && !canvas->getDrawFilter()) {
111 canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref();
caryclark@google.come3e940c2012-11-07 16:42:17 +0000112 if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) {
113 canvas->setAllowSoftClip(false);
114 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000115 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000116}
117
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000118SkCanvas* PictureRenderer::setupCanvas() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000119 const int width = this->getViewWidth();
120 const int height = this->getViewHeight();
121 return this->setupCanvas(width, height);
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000122}
123
124SkCanvas* PictureRenderer::setupCanvas(int width, int height) {
caryclark@google.coma3622372012-11-06 21:26:13 +0000125 SkCanvas* canvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000126 switch(fDeviceType) {
127 case kBitmap_DeviceType: {
128 SkBitmap bitmap;
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000129 sk_tools::setup_bitmap(&bitmap, width, height);
caryclark@google.coma3622372012-11-06 21:26:13 +0000130 canvas = SkNEW_ARGS(SkCanvas, (bitmap));
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000131 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000132 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000133#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000134#if SK_ANGLE
135 case kAngle_DeviceType:
136 // fall through
137#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000138#if SK_MESA
139 case kMesa_DeviceType:
140 // fall through
141#endif
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000142 case kGPU_DeviceType:
143 case kNVPR_DeviceType: {
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000144 SkAutoTUnref<GrSurface> target;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000145 if (fGrContext) {
146 // create a render target to back the device
147 GrTextureDesc desc;
148 desc.fConfig = kSkia8888_GrPixelConfig;
149 desc.fFlags = kRenderTarget_GrTextureFlagBit;
150 desc.fWidth = width;
151 desc.fHeight = height;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000152 desc.fSampleCnt = fSampleCount;
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000153 target.reset(fGrContext->createUncachedTexture(desc, NULL, 0));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000154 }
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000155 if (NULL == target.get()) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000156 SkASSERT(0);
157 return NULL;
158 }
159
commit-bot@chromium.orgae403b92013-04-10 17:27:30 +0000160 SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target));
caryclark@google.coma3622372012-11-06 21:26:13 +0000161 canvas = SkNEW_ARGS(SkCanvas, (device.get()));
scroggo@google.com0556ea02013-02-08 19:38:21 +0000162 break;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000163 }
164#endif
165 default:
166 SkASSERT(0);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000167 return NULL;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000168 }
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000169 setUpFilter(canvas, fDrawFilters);
170 this->scaleToScaleFactor(canvas);
commit-bot@chromium.org17cc3ea2014-01-15 14:51:25 +0000171
172 // Pictures often lie about their extent (i.e., claim to be 100x100 but
173 // only ever draw to 90x100). Clear here so the undrawn portion will have
174 // a consistent color
175 canvas->clear(SK_ColorTRANSPARENT);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000176 return canvas;
177}
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000178
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000179void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) {
180 SkASSERT(canvas != NULL);
181 if (fScaleFactor != SK_Scalar1) {
182 canvas->scale(fScaleFactor, fScaleFactor);
183 }
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000184}
185
186void PictureRenderer::end() {
scroggo@google.com08085f82013-01-28 20:40:24 +0000187 this->resetState(true);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000188 fPicture.reset(NULL);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000189 fCanvas.reset(NULL);
190}
191
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000192int PictureRenderer::getViewWidth() {
193 SkASSERT(fPicture != NULL);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700194 int width = SkScalarCeilToInt(fPicture->cullRect().width() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000195 if (fViewport.width() > 0) {
196 width = SkMin32(width, fViewport.width());
197 }
198 return width;
199}
200
201int PictureRenderer::getViewHeight() {
202 SkASSERT(fPicture != NULL);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700203 int height = SkScalarCeilToInt(fPicture->cullRect().height() * fScaleFactor);
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000204 if (fViewport.height() > 0) {
205 height = SkMin32(height, fViewport.height());
206 }
207 return height;
208}
209
junov@chromium.org9313ca42012-11-02 18:11:49 +0000210/** Converts fPicture to a picture that uses a BBoxHierarchy.
211 * PictureRenderer subclasses that are used to test picture playback
212 * should call this method during init.
213 */
214void PictureRenderer::buildBBoxHierarchy() {
215 SkASSERT(NULL != fPicture);
216 if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000217 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
218 SkPictureRecorder recorder;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700219 SkCanvas* canvas = recorder.beginRecording(fPicture->cullRect().width(),
220 fPicture->cullRect().height(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000221 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000222 this->recordFlags());
223 fPicture->draw(canvas);
224 fPicture.reset(recorder.endRecording());
junov@chromium.org9313ca42012-11-02 18:11:49 +0000225 }
226}
227
scroggo@google.com08085f82013-01-28 20:40:24 +0000228void PictureRenderer::resetState(bool callFinish) {
keyar@chromium.org28136b32012-08-20 15:04:15 +0000229#if SK_SUPPORT_GPU
robertphillips@google.com6177e692013-02-28 20:16:25 +0000230 SkGLContextHelper* glContext = this->getGLContext();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000231 if (NULL == glContext) {
232 SkASSERT(kBitmap_DeviceType == fDeviceType);
233 return;
234 }
keyar@chromium.org28136b32012-08-20 15:04:15 +0000235
scroggo@google.com0556ea02013-02-08 19:38:21 +0000236 fGrContext->flush();
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000237 glContext->swapBuffers();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000238 if (callFinish) {
239 SK_GL(*glContext, Finish());
keyar@chromium.org77a55222012-08-20 15:03:47 +0000240 }
keyar@chromium.orga40c20d2012-08-20 15:04:12 +0000241#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +0000242}
243
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000244void PictureRenderer::purgeTextures() {
245 SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool();
246
247 pool->dumpPool();
248
249#if SK_SUPPORT_GPU
250 SkGLContextHelper* glContext = this->getGLContext();
251 if (NULL == glContext) {
252 SkASSERT(kBitmap_DeviceType == fDeviceType);
253 return;
254 }
255
256 // resetState should've already done this
257 fGrContext->flush();
258
259 fGrContext->purgeAllUnlockedResources();
260#endif
261}
262
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000263/**
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000264 * Write the canvas to an image file and/or JSON summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000265 *
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000266 * @param canvas Must be non-null. Canvas to be written to a file.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000267 * @param writePath If nonempty, write the binary image to a file within this directory.
268 * @param mismatchPath If nonempty, write the binary image to a file within this directory,
269 * but only if the image does not match expectations.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000270 * @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 +0000271 * @param jsonSummaryPtr If not null, add image results (checksum) to this summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000272 * @param useChecksumBasedFilenames If true, use checksum-based filenames when writing to disk.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000273 * @param tileNumberPtr If not null, which tile number this image contains.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000274 *
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000275 * @return bool True if the operation completed successfully.
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000276 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000277static bool write(SkCanvas* canvas, const SkString& writePath, const SkString& mismatchPath,
278 const SkString& inputFilename, ImageResultsAndExpectations *jsonSummaryPtr,
279 bool useChecksumBasedFilenames, const int* tileNumberPtr=NULL) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000280 SkASSERT(canvas != NULL);
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000281 if (NULL == canvas) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000282 return false;
283 }
284
285 SkBitmap bitmap;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000286 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000287 setup_bitmap(&bitmap, size.width(), size.height());
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000288
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000289 canvas->readPixels(&bitmap, 0, 0);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000290 force_all_opaque(bitmap);
291 BitmapAndDigest bitmapAndDigest(bitmap);
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000292
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000293 SkString escapedInputFilename(inputFilename);
294 replace_char(&escapedInputFilename, '.', '_');
295
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000296 // TODO(epoger): what about including the config type within outputFilename? That way,
297 // we could combine results of different config types without conflicting filenames.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000298 SkString outputFilename;
299 const char *outputSubdirPtr = NULL;
300 if (useChecksumBasedFilenames) {
epoger85b438d2014-08-21 23:21:32 -0700301 ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000302 outputSubdirPtr = escapedInputFilename.c_str();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000303 outputFilename.set(imageDigestPtr->getHashType());
304 outputFilename.append("_");
305 outputFilename.appendU64(imageDigestPtr->getHashValue());
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000306 } else {
307 outputFilename.set(escapedInputFilename);
308 if (NULL != tileNumberPtr) {
309 outputFilename.append("-tile");
310 outputFilename.appendS32(*tileNumberPtr);
311 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000312 }
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000313 outputFilename.append(".png");
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000314
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000315 if (NULL != jsonSummaryPtr) {
epoger85b438d2014-08-21 23:21:32 -0700316 ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000317 SkString outputRelativePath;
318 if (outputSubdirPtr) {
319 outputRelativePath.set(outputSubdirPtr);
320 outputRelativePath.append("/"); // always use "/", even on Windows
321 outputRelativePath.append(outputFilename);
322 } else {
323 outputRelativePath.set(outputFilename);
324 }
325
326 jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(),
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000327 *imageDigestPtr, tileNumberPtr);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000328 if (!mismatchPath.isEmpty() &&
epoger85b438d2014-08-21 23:21:32 -0700329 !jsonSummaryPtr->getExpectation(inputFilename.c_str(),
330 tileNumberPtr).matches(*imageDigestPtr)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000331 if (!write_bitmap_to_disk(bitmap, mismatchPath, outputSubdirPtr, outputFilename)) {
332 return false;
333 }
334 }
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000335 }
336
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000337 if (writePath.isEmpty()) {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000338 return true;
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000339 } else {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000340 return write_bitmap_to_disk(bitmap, writePath, outputSubdirPtr, outputFilename);
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000341 }
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000342}
343
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000344///////////////////////////////////////////////////////////////////////////////////////////////
345
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000346SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) {
347 // defer the canvas setup until the render step
348 return NULL;
349}
350
reed@google.com672588b2014-01-08 15:42:01 +0000351// the size_t* parameter is deprecated, so we ignore it
352static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000353 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000354}
355
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000356bool RecordPictureRenderer::render(SkBitmap** out) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000357 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
358 SkPictureRecorder recorder;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700359 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(this->getViewWidth()),
360 SkIntToScalar(this->getViewHeight()),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000361 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000362 this->recordFlags());
363 this->scaleToScaleFactor(canvas);
364 fPicture->draw(canvas);
365 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000366 if (!fWritePath.isEmpty()) {
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000367 // Record the new picture as a new SKP with PNG encoded bitmaps.
tfarinaa8e2e152014-07-28 19:26:58 -0700368 SkString skpPath = SkOSPath::Join(fWritePath.c_str(), fInputFilename.c_str());
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000369 SkFILEWStream stream(skpPath.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000370 picture->serialize(&stream, &encode_bitmap_to_data);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000371 return true;
372 }
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000373 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +0000374}
375
scroggo@google.com0a049b82012-11-02 22:01:26 +0000376SkString RecordPictureRenderer::getConfigNameInternal() {
377 return SkString("record");
378}
379
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000380///////////////////////////////////////////////////////////////////////////////////////////////
381
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000382bool PipePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000383 SkASSERT(fCanvas.get() != NULL);
384 SkASSERT(fPicture != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000385 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000386 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000387 }
388
389 PipeController pipeController(fCanvas.get());
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000390 SkGPipeWriter writer;
391 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
robertphillips9b14f262014-06-04 05:40:44 -0700392 pipeCanvas->drawPicture(fPicture);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000393 writer.endRecording();
scroggo@google.com9a412522012-09-07 15:21:18 +0000394 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000395 if (NULL != out) {
396 *out = SkNEW(SkBitmap);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700397 setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()),
398 SkScalarCeilToInt(fPicture->cullRect().height()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000399 fCanvas->readPixels(*out, 0, 0);
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000400 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000401 if (fEnableWrites) {
402 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
403 fUseChecksumBasedFilenames);
404 } else {
405 return true;
406 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000407}
408
scroggo@google.com0a049b82012-11-02 22:01:26 +0000409SkString PipePictureRenderer::getConfigNameInternal() {
410 return SkString("pipe");
411}
412
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000413///////////////////////////////////////////////////////////////////////////////////////////////
414
robertphillipsce4dd3d2014-07-07 13:46:35 -0700415void SimplePictureRenderer::init(const SkPicture* picture, const SkString* writePath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000416 const SkString* mismatchPath, const SkString* inputFilename,
417 bool useChecksumBasedFilenames) {
418 INHERITED::init(picture, writePath, mismatchPath, inputFilename, useChecksumBasedFilenames);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000419 this->buildBBoxHierarchy();
420}
421
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000422bool SimplePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000423 SkASSERT(fCanvas.get() != NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000424 SkASSERT(NULL != fPicture);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000425 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000426 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000427 }
428
robertphillips9b14f262014-06-04 05:40:44 -0700429 fCanvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000430 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000431 if (NULL != out) {
432 *out = SkNEW(SkBitmap);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700433 setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()),
434 SkScalarCeilToInt(fPicture->cullRect().height()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000435 fCanvas->readPixels(*out, 0, 0);
436 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000437 if (fEnableWrites) {
438 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
439 fUseChecksumBasedFilenames);
440 } else {
441 return true;
442 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000443}
444
scroggo@google.com0a049b82012-11-02 22:01:26 +0000445SkString SimplePictureRenderer::getConfigNameInternal() {
446 return SkString("simple");
447}
448
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000449///////////////////////////////////////////////////////////////////////////////////////////////
450
krajcevskib1aded82014-08-18 07:52:17 -0700451#if SK_SUPPORT_GPU
452TiledPictureRenderer::TiledPictureRenderer(const GrContext::Options& opts)
mtklein2a65a232014-08-26 14:07:04 -0700453 : INHERITED(opts)
krajcevskib1aded82014-08-18 07:52:17 -0700454 , fTileWidth(kDefaultTileWidth)
455#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000456TiledPictureRenderer::TiledPictureRenderer()
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000457 : fTileWidth(kDefaultTileWidth)
krajcevskib1aded82014-08-18 07:52:17 -0700458#endif
rileya@google.comb947b912012-08-29 17:35:07 +0000459 , fTileHeight(kDefaultTileHeight)
rileya@google.coma04dc022012-09-10 19:01:38 +0000460 , fTileWidthPercentage(0.0)
rileya@google.comb947b912012-08-29 17:35:07 +0000461 , fTileHeightPercentage(0.0)
scroggo@google.comcbcef702012-12-13 22:09:28 +0000462 , fTileMinPowerOf2Width(0)
463 , fCurrentTileOffset(-1)
464 , fTilesX(0)
465 , fTilesY(0) { }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000466
robertphillipsce4dd3d2014-07-07 13:46:35 -0700467void TiledPictureRenderer::init(const SkPicture* pict, const SkString* writePath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000468 const SkString* mismatchPath, const SkString* inputFilename,
469 bool useChecksumBasedFilenames) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000470 SkASSERT(NULL != pict);
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000471 SkASSERT(0 == fTileRects.count());
472 if (NULL == pict || fTileRects.count() != 0) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000473 return;
474 }
475
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000476 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
477 // used by bench_pictures.
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000478 fPicture.reset(pict)->ref();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000479 this->CopyString(&fWritePath, writePath);
480 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000481 this->CopyString(&fInputFilename, inputFilename);
482 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000483 this->buildBBoxHierarchy();
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000484
485 if (fTileWidthPercentage > 0) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700486 fTileWidth = SkScalarCeilToInt(float(fTileWidthPercentage * fPicture->cullRect().width() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000487 }
488 if (fTileHeightPercentage > 0) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700489 fTileHeight = SkScalarCeilToInt(float(fTileHeightPercentage * fPicture->cullRect().height() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000490 }
491
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000492 if (fTileMinPowerOf2Width > 0) {
493 this->setupPowerOf2Tiles();
494 } else {
495 this->setupTiles();
496 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000497 fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight));
498 // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the
499 // first call to drawCurrentTile.
500 fCurrentTileOffset = -1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000501}
502
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000503void TiledPictureRenderer::end() {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000504 fTileRects.reset();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000505 this->INHERITED::end();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000506}
507
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000508void TiledPictureRenderer::setupTiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000509 // Only use enough tiles to cover the viewport
510 const int width = this->getViewWidth();
511 const int height = this->getViewHeight();
512
scroggo@google.comcbcef702012-12-13 22:09:28 +0000513 fTilesX = fTilesY = 0;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000514 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000515 fTilesY++;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000516 for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000517 if (0 == tile_y_start) {
518 // Only count tiles in the X direction on the first pass.
519 fTilesX++;
520 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000521 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
522 SkIntToScalar(tile_y_start),
523 SkIntToScalar(fTileWidth),
524 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000525 }
526 }
527}
528
scroggo@google.comcbcef702012-12-13 22:09:28 +0000529bool TiledPictureRenderer::tileDimensions(int &x, int &y) {
530 if (fTileRects.count() == 0 || NULL == fPicture) {
531 return false;
532 }
533 x = fTilesX;
534 y = fTilesY;
535 return true;
536}
537
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000538// The goal of the powers of two tiles is to minimize the amount of wasted tile
539// space in the width-wise direction and then minimize the number of tiles. The
540// constraints are that every tile must have a pixel width that is a power of
541// two and also be of some minimal width (that is also a power of two).
542//
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000543// This is solved by first taking our picture size and rounding it up to the
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000544// multiple of the minimal width. The binary representation of this rounded
545// value gives us the tiles we need: a bit of value one means we need a tile of
546// that size.
547void TiledPictureRenderer::setupPowerOf2Tiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000548 // Only use enough tiles to cover the viewport
549 const int width = this->getViewWidth();
550 const int height = this->getViewHeight();
551
552 int rounded_value = width;
553 if (width % fTileMinPowerOf2Width != 0) {
554 rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000555 }
556
reed@google.come15b2f52013-12-18 04:59:26 +0000557 int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width)));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000558 int largest_possible_tile_size = 1 << num_bits;
559
scroggo@google.comcbcef702012-12-13 22:09:28 +0000560 fTilesX = fTilesY = 0;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000561 // The tile height is constant for a particular picture.
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000562 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000563 fTilesY++;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000564 int tile_x_start = 0;
565 int current_width = largest_possible_tile_size;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000566 // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
567 // to draw each tile.
568 fTileWidth = current_width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000569
570 while (current_width >= fTileMinPowerOf2Width) {
571 // It is very important this is a bitwise AND.
572 if (current_width & rounded_value) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000573 if (0 == tile_y_start) {
574 // Only count tiles in the X direction on the first pass.
575 fTilesX++;
576 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000577 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
578 SkIntToScalar(tile_y_start),
579 SkIntToScalar(current_width),
580 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000581 tile_x_start += current_width;
582 }
583
584 current_width >>= 1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000585 }
586 }
587}
588
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000589/**
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000590 * Draw the specified picture to the canvas translated to rectangle provided, so that this mini
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000591 * canvas represents the rectangle's portion of the overall picture.
592 * Saves and restores so that the initial clip and matrix return to their state before this function
593 * is called.
594 */
mtklein2a65a232014-08-26 14:07:04 -0700595static void draw_tile_to_canvas(SkCanvas* canvas,
596 const SkRect& tileRect,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700597 const SkPicture* picture) {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000598 int saveCount = canvas->save();
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000599 // Translate so that we draw the correct portion of the picture.
600 // Perform a postTranslate so that the scaleFactor does not interfere with the positioning.
601 SkMatrix mat(canvas->getTotalMatrix());
602 mat.postTranslate(-tileRect.fLeft, -tileRect.fTop);
603 canvas->setMatrix(mat);
robertphillips9b14f262014-06-04 05:40:44 -0700604 canvas->drawPicture(picture);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000605 canvas->restoreToCount(saveCount);
606 canvas->flush();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000607}
608
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000609///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000610
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000611/**
612 * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap.
613 * If the src bitmap is too large to fit within the dst bitmap after the x and y
614 * offsets have been applied, any excess will be ignored (so only the top-left portion of the
615 * src bitmap will be copied).
616 *
617 * @param src source bitmap
618 * @param dst destination bitmap
619 * @param xOffset x-offset within destination bitmap
620 * @param yOffset y-offset within destination bitmap
621 */
622static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst,
623 int xOffset, int yOffset) {
624 for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) {
625 for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) {
626 *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000627 }
628 }
629}
630
scroggo@google.comcbcef702012-12-13 22:09:28 +0000631bool TiledPictureRenderer::nextTile(int &i, int &j) {
632 if (++fCurrentTileOffset < fTileRects.count()) {
633 i = fCurrentTileOffset % fTilesX;
634 j = fCurrentTileOffset / fTilesX;
635 return true;
636 }
637 return false;
638}
639
640void TiledPictureRenderer::drawCurrentTile() {
641 SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000642 draw_tile_to_canvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000643}
644
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000645bool TiledPictureRenderer::render(SkBitmap** out) {
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000646 SkASSERT(fPicture != NULL);
647 if (NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000648 return false;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000649 }
650
edisonn@google.com84f548c2012-12-18 22:24:03 +0000651 SkBitmap bitmap;
652 if (out){
653 *out = SkNEW(SkBitmap);
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700654 setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()),
655 SkScalarCeilToInt(fPicture->cullRect().height()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000656 setup_bitmap(&bitmap, fTileWidth, fTileHeight);
657 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000658 bool success = true;
659 for (int i = 0; i < fTileRects.count(); ++i) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000660 draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture);
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000661 if (fEnableWrites) {
662 success &= write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
663 fUseChecksumBasedFilenames, &i);
664 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000665 if (NULL != out) {
666 if (fCanvas->readPixels(&bitmap, 0, 0)) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000667 // Add this tile to the entire bitmap.
668 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()),
669 SkScalarFloorToInt(fTileRects[i].top()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000670 } else {
671 success = false;
672 }
673 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000674 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000675 return success;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000676}
677
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000678SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
679 SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000680 SkASSERT(NULL != fPicture);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000681 // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This
682 // is mostly important for tiles on the right and bottom edges as they may go over this area and
683 // the picture may have some commands that draw outside of this area and so should not actually
684 // be written.
685 // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set
686 // by INHERITED::setupCanvas.
687 SkRegion clipRegion;
688 clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight());
689 canvas->clipRegion(clipRegion);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000690 return canvas;
691}
scroggo@google.com0a049b82012-11-02 22:01:26 +0000692
693SkString TiledPictureRenderer::getConfigNameInternal() {
694 SkString name;
695 if (fTileMinPowerOf2Width > 0) {
696 name.append("pow2tile_");
697 name.appendf("%i", fTileMinPowerOf2Width);
698 } else {
699 name.append("tile_");
700 if (fTileWidthPercentage > 0) {
701 name.appendf("%.f%%", fTileWidthPercentage);
702 } else {
703 name.appendf("%i", fTileWidth);
704 }
705 }
706 name.append("x");
707 if (fTileHeightPercentage > 0) {
708 name.appendf("%.f%%", fTileHeightPercentage);
709 } else {
710 name.appendf("%i", fTileHeight);
711 }
712 return name;
713}
714
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000715///////////////////////////////////////////////////////////////////////////////////////////////
716
scroggo@google.com9a412522012-09-07 15:21:18 +0000717void PlaybackCreationRenderer::setup() {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000718 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
719 fRecorder.reset(SkNEW(SkPictureRecorder));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700720 SkCanvas* canvas = fRecorder->beginRecording(SkIntToScalar(this->getViewWidth()),
721 SkIntToScalar(this->getViewHeight()),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000722 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000723 this->recordFlags());
724 this->scaleToScaleFactor(canvas);
robertphillips9b14f262014-06-04 05:40:44 -0700725 canvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000726}
727
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000728bool PlaybackCreationRenderer::render(SkBitmap** out) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000729 fPicture.reset(fRecorder->endRecording());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000730 // Since this class does not actually render, return false.
731 return false;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000732}
733
scroggo@google.com0a049b82012-11-02 22:01:26 +0000734SkString PlaybackCreationRenderer::getConfigNameInternal() {
735 return SkString("playback_creation");
736}
737
junov@chromium.org9313ca42012-11-02 18:11:49 +0000738///////////////////////////////////////////////////////////////////////////////////////////////
739// SkPicture variants for each BBoxHierarchy type
740
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000741SkBBHFactory* PictureRenderer::getFactory() {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000742 switch (fBBoxHierarchyType) {
743 case kNone_BBoxHierarchyType:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000744 return NULL;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000745 case kRTree_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000746 return SkNEW(SkRTreeFactory);
junov@chromium.org7b537062012-11-06 18:58:43 +0000747 case kTileGrid_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000748 return SkNEW_ARGS(SkTileGridFactory, (fGridInfo));
junov@chromium.org9313ca42012-11-02 18:11:49 +0000749 }
750 SkASSERT(0); // invalid bbhType
751 return NULL;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000752}
junov@chromium.org9313ca42012-11-02 18:11:49 +0000753
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000754///////////////////////////////////////////////////////////////////////////////
755
756class GatherRenderer : public PictureRenderer {
757public:
krajcevskib1aded82014-08-18 07:52:17 -0700758#if SK_SUPPORT_GPU
759 GatherRenderer(const GrContext::Options& opts) : INHERITED(opts) { }
760#endif
761
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000762 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700763 SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->cullRect().width()),
764 SkIntToScalar(fPicture->cullRect().height()));
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000765 SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds);
766 SkSafeUnref(data);
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000767
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000768 return (fWritePath.isEmpty()); // we don't have anything to write
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000769 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000770
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000771private:
772 virtual SkString getConfigNameInternal() SK_OVERRIDE {
773 return SkString("gather_pixelrefs");
774 }
krajcevskib1aded82014-08-18 07:52:17 -0700775
776 typedef PictureRenderer INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000777};
778
krajcevskib1aded82014-08-18 07:52:17 -0700779#if SK_SUPPORT_GPU
780PictureRenderer* CreateGatherPixelRefsRenderer(const GrContext::Options& opts) {
781 return SkNEW_ARGS(GatherRenderer, (opts));
782}
783#else
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000784PictureRenderer* CreateGatherPixelRefsRenderer() {
785 return SkNEW(GatherRenderer);
786}
krajcevskib1aded82014-08-18 07:52:17 -0700787#endif
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000788
junov@chromium.org9313ca42012-11-02 18:11:49 +0000789} // namespace sk_tools