blob: ac639e59e8dfed46ca356d8dc3efa7d8db5f4148 [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
robertphillipsce4dd3d2014-07-07 13:46:35 -070051void PictureRenderer::init(const SkPicture* pict,
52 const SkString* writePath,
53 const SkString* mismatchPath,
54 const SkString* inputFilename,
55 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);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000194 int width = SkScalarCeilToInt(fPicture->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);
robertphillips@google.com8ac811e2013-02-07 00:13:34 +0000203 int height = SkScalarCeilToInt(fPicture->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;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000219 SkCanvas* canvas = recorder.beginRecording(fPicture->width(), fPicture->height(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000220 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000221 this->recordFlags());
222 fPicture->draw(canvas);
223 fPicture.reset(recorder.endRecording());
junov@chromium.org9313ca42012-11-02 18:11:49 +0000224 }
225}
226
scroggo@google.com08085f82013-01-28 20:40:24 +0000227void PictureRenderer::resetState(bool callFinish) {
keyar@chromium.org28136b32012-08-20 15:04:15 +0000228#if SK_SUPPORT_GPU
robertphillips@google.com6177e692013-02-28 20:16:25 +0000229 SkGLContextHelper* glContext = this->getGLContext();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000230 if (NULL == glContext) {
231 SkASSERT(kBitmap_DeviceType == fDeviceType);
232 return;
233 }
keyar@chromium.org28136b32012-08-20 15:04:15 +0000234
scroggo@google.com0556ea02013-02-08 19:38:21 +0000235 fGrContext->flush();
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000236 glContext->swapBuffers();
scroggo@google.com0556ea02013-02-08 19:38:21 +0000237 if (callFinish) {
238 SK_GL(*glContext, Finish());
keyar@chromium.org77a55222012-08-20 15:03:47 +0000239 }
keyar@chromium.orga40c20d2012-08-20 15:04:12 +0000240#endif
keyar@chromium.org77a55222012-08-20 15:03:47 +0000241}
242
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000243void PictureRenderer::purgeTextures() {
244 SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool();
245
246 pool->dumpPool();
247
248#if SK_SUPPORT_GPU
249 SkGLContextHelper* glContext = this->getGLContext();
250 if (NULL == glContext) {
251 SkASSERT(kBitmap_DeviceType == fDeviceType);
252 return;
253 }
254
255 // resetState should've already done this
256 fGrContext->flush();
257
258 fGrContext->purgeAllUnlockedResources();
259#endif
260}
261
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000262/**
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000263 * Write the canvas to an image file and/or JSON summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000264 *
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000265 * @param canvas Must be non-null. Canvas to be written to a file.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000266 * @param writePath If nonempty, write the binary image to a file within this directory.
267 * @param mismatchPath If nonempty, write the binary image to a file within this directory,
268 * but only if the image does not match expectations.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000269 * @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 +0000270 * @param jsonSummaryPtr If not null, add image results (checksum) to this summary.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000271 * @param useChecksumBasedFilenames If true, use checksum-based filenames when writing to disk.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000272 * @param tileNumberPtr If not null, which tile number this image contains.
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000273 *
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000274 * @return bool True if the operation completed successfully.
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000275 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000276static bool write(SkCanvas* canvas, const SkString& writePath, const SkString& mismatchPath,
277 const SkString& inputFilename, ImageResultsAndExpectations *jsonSummaryPtr,
278 bool useChecksumBasedFilenames, const int* tileNumberPtr=NULL) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000279 SkASSERT(canvas != NULL);
scroggo@google.comb6e806b2012-10-03 17:32:33 +0000280 if (NULL == canvas) {
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000281 return false;
282 }
283
284 SkBitmap bitmap;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000285 SkISize size = canvas->getDeviceSize();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000286 setup_bitmap(&bitmap, size.width(), size.height());
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000287
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000288 canvas->readPixels(&bitmap, 0, 0);
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000289 force_all_opaque(bitmap);
290 BitmapAndDigest bitmapAndDigest(bitmap);
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000291
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000292 SkString escapedInputFilename(inputFilename);
293 replace_char(&escapedInputFilename, '.', '_');
294
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000295 // TODO(epoger): what about including the config type within outputFilename? That way,
296 // we could combine results of different config types without conflicting filenames.
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000297 SkString outputFilename;
298 const char *outputSubdirPtr = NULL;
299 if (useChecksumBasedFilenames) {
epoger85b438d2014-08-21 23:21:32 -0700300 ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000301 outputSubdirPtr = escapedInputFilename.c_str();
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000302 outputFilename.set(imageDigestPtr->getHashType());
303 outputFilename.append("_");
304 outputFilename.appendU64(imageDigestPtr->getHashValue());
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000305 } else {
306 outputFilename.set(escapedInputFilename);
307 if (NULL != tileNumberPtr) {
308 outputFilename.append("-tile");
309 outputFilename.appendS32(*tileNumberPtr);
310 }
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000311 }
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000312 outputFilename.append(".png");
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000313
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000314 if (NULL != jsonSummaryPtr) {
epoger85b438d2014-08-21 23:21:32 -0700315 ImageDigest *imageDigestPtr = bitmapAndDigest.getImageDigestPtr();
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000316 SkString outputRelativePath;
317 if (outputSubdirPtr) {
318 outputRelativePath.set(outputSubdirPtr);
319 outputRelativePath.append("/"); // always use "/", even on Windows
320 outputRelativePath.append(outputFilename);
321 } else {
322 outputRelativePath.set(outputFilename);
323 }
324
325 jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(),
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000326 *imageDigestPtr, tileNumberPtr);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000327 if (!mismatchPath.isEmpty() &&
epoger85b438d2014-08-21 23:21:32 -0700328 !jsonSummaryPtr->getExpectation(inputFilename.c_str(),
329 tileNumberPtr).matches(*imageDigestPtr)) {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000330 if (!write_bitmap_to_disk(bitmap, mismatchPath, outputSubdirPtr, outputFilename)) {
331 return false;
332 }
333 }
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000334 }
335
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000336 if (writePath.isEmpty()) {
commit-bot@chromium.org4610a462014-04-29 19:39:22 +0000337 return true;
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000338 } else {
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000339 return write_bitmap_to_disk(bitmap, writePath, outputSubdirPtr, outputFilename);
commit-bot@chromium.org24c568c2014-04-10 15:39:02 +0000340 }
keyar@chromium.org9299ede2012-08-21 19:05:08 +0000341}
342
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000343///////////////////////////////////////////////////////////////////////////////////////////////
344
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000345SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) {
346 // defer the canvas setup until the render step
347 return NULL;
348}
349
reed@google.com672588b2014-01-08 15:42:01 +0000350// the size_t* parameter is deprecated, so we ignore it
351static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) {
scroggo@google.com1b1bcc32013-05-21 20:31:23 +0000352 return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000353}
354
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000355bool RecordPictureRenderer::render(SkBitmap** out) {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000356 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
357 SkPictureRecorder recorder;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000358 SkCanvas* canvas = recorder.beginRecording(this->getViewWidth(), this->getViewHeight(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000359 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000360 this->recordFlags());
361 this->scaleToScaleFactor(canvas);
362 fPicture->draw(canvas);
363 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000364 if (!fWritePath.isEmpty()) {
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000365 // Record the new picture as a new SKP with PNG encoded bitmaps.
tfarinaa8e2e152014-07-28 19:26:58 -0700366 SkString skpPath = SkOSPath::Join(fWritePath.c_str(), fInputFilename.c_str());
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000367 SkFILEWStream stream(skpPath.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000368 picture->serialize(&stream, &encode_bitmap_to_data);
scroggo@google.coma9e3a362012-11-07 17:52:48 +0000369 return true;
370 }
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000371 return false;
scroggo@google.com9a412522012-09-07 15:21:18 +0000372}
373
scroggo@google.com0a049b82012-11-02 22:01:26 +0000374SkString RecordPictureRenderer::getConfigNameInternal() {
375 return SkString("record");
376}
377
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000378///////////////////////////////////////////////////////////////////////////////////////////////
379
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000380bool PipePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000381 SkASSERT(fCanvas.get() != NULL);
382 SkASSERT(fPicture != NULL);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000383 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000384 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000385 }
386
387 PipeController pipeController(fCanvas.get());
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000388 SkGPipeWriter writer;
389 SkCanvas* pipeCanvas = writer.startRecording(&pipeController);
robertphillips9b14f262014-06-04 05:40:44 -0700390 pipeCanvas->drawPicture(fPicture);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000391 writer.endRecording();
scroggo@google.com9a412522012-09-07 15:21:18 +0000392 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000393 if (NULL != out) {
394 *out = SkNEW(SkBitmap);
395 setup_bitmap(*out, fPicture->width(), fPicture->height());
396 fCanvas->readPixels(*out, 0, 0);
skia.committer@gmail.coma7d8e3e2012-12-19 02:01:38 +0000397 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000398 if (fEnableWrites) {
399 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
400 fUseChecksumBasedFilenames);
401 } else {
402 return true;
403 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000404}
405
scroggo@google.com0a049b82012-11-02 22:01:26 +0000406SkString PipePictureRenderer::getConfigNameInternal() {
407 return SkString("pipe");
408}
409
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000410///////////////////////////////////////////////////////////////////////////////////////////////
411
robertphillipsce4dd3d2014-07-07 13:46:35 -0700412void SimplePictureRenderer::init(const SkPicture* picture, const SkString* writePath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000413 const SkString* mismatchPath, const SkString* inputFilename,
414 bool useChecksumBasedFilenames) {
415 INHERITED::init(picture, writePath, mismatchPath, inputFilename, useChecksumBasedFilenames);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000416 this->buildBBoxHierarchy();
417}
418
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000419bool SimplePictureRenderer::render(SkBitmap** out) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000420 SkASSERT(fCanvas.get() != NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000421 SkASSERT(NULL != fPicture);
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000422 if (NULL == fCanvas.get() || NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000423 return false;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000424 }
425
robertphillips9b14f262014-06-04 05:40:44 -0700426 fCanvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000427 fCanvas->flush();
edisonn@google.com84f548c2012-12-18 22:24:03 +0000428 if (NULL != out) {
429 *out = SkNEW(SkBitmap);
430 setup_bitmap(*out, fPicture->width(), fPicture->height());
431 fCanvas->readPixels(*out, 0, 0);
432 }
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000433 if (fEnableWrites) {
434 return write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
435 fUseChecksumBasedFilenames);
436 } else {
437 return true;
438 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000439}
440
scroggo@google.com0a049b82012-11-02 22:01:26 +0000441SkString SimplePictureRenderer::getConfigNameInternal() {
442 return SkString("simple");
443}
444
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000445///////////////////////////////////////////////////////////////////////////////////////////////
446
krajcevskib1aded82014-08-18 07:52:17 -0700447#if SK_SUPPORT_GPU
448TiledPictureRenderer::TiledPictureRenderer(const GrContext::Options& opts)
449 : INHERITED(opts)
450 , fTileWidth(kDefaultTileWidth)
451#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000452TiledPictureRenderer::TiledPictureRenderer()
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000453 : fTileWidth(kDefaultTileWidth)
krajcevskib1aded82014-08-18 07:52:17 -0700454#endif
rileya@google.comb947b912012-08-29 17:35:07 +0000455 , fTileHeight(kDefaultTileHeight)
rileya@google.coma04dc022012-09-10 19:01:38 +0000456 , fTileWidthPercentage(0.0)
rileya@google.comb947b912012-08-29 17:35:07 +0000457 , fTileHeightPercentage(0.0)
scroggo@google.comcbcef702012-12-13 22:09:28 +0000458 , fTileMinPowerOf2Width(0)
459 , fCurrentTileOffset(-1)
460 , fTilesX(0)
461 , fTilesY(0) { }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000462
robertphillipsce4dd3d2014-07-07 13:46:35 -0700463void TiledPictureRenderer::init(const SkPicture* pict, const SkString* writePath,
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000464 const SkString* mismatchPath, const SkString* inputFilename,
465 bool useChecksumBasedFilenames) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000466 SkASSERT(NULL != pict);
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000467 SkASSERT(0 == fTileRects.count());
468 if (NULL == pict || fTileRects.count() != 0) {
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000469 return;
470 }
471
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000472 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not
473 // used by bench_pictures.
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000474 fPicture.reset(pict)->ref();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000475 this->CopyString(&fWritePath, writePath);
476 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000477 this->CopyString(&fInputFilename, inputFilename);
478 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000479 this->buildBBoxHierarchy();
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000480
481 if (fTileWidthPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000482 fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000483 }
484 if (fTileHeightPercentage > 0) {
robertphillips@google.com5d8d1862012-08-15 14:36:41 +0000485 fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100));
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000486 }
487
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000488 if (fTileMinPowerOf2Width > 0) {
489 this->setupPowerOf2Tiles();
490 } else {
491 this->setupTiles();
492 }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000493 fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight));
494 // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the
495 // first call to drawCurrentTile.
496 fCurrentTileOffset = -1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000497}
498
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000499void TiledPictureRenderer::end() {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000500 fTileRects.reset();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000501 this->INHERITED::end();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000502}
503
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000504void TiledPictureRenderer::setupTiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000505 // Only use enough tiles to cover the viewport
506 const int width = this->getViewWidth();
507 const int height = this->getViewHeight();
508
scroggo@google.comcbcef702012-12-13 22:09:28 +0000509 fTilesX = fTilesY = 0;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000510 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000511 fTilesY++;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000512 for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000513 if (0 == tile_y_start) {
514 // Only count tiles in the X direction on the first pass.
515 fTilesX++;
516 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000517 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
518 SkIntToScalar(tile_y_start),
519 SkIntToScalar(fTileWidth),
520 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000521 }
522 }
523}
524
scroggo@google.comcbcef702012-12-13 22:09:28 +0000525bool TiledPictureRenderer::tileDimensions(int &x, int &y) {
526 if (fTileRects.count() == 0 || NULL == fPicture) {
527 return false;
528 }
529 x = fTilesX;
530 y = fTilesY;
531 return true;
532}
533
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000534// The goal of the powers of two tiles is to minimize the amount of wasted tile
535// space in the width-wise direction and then minimize the number of tiles. The
536// constraints are that every tile must have a pixel width that is a power of
537// two and also be of some minimal width (that is also a power of two).
538//
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000539// This is solved by first taking our picture size and rounding it up to the
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000540// multiple of the minimal width. The binary representation of this rounded
541// value gives us the tiles we need: a bit of value one means we need a tile of
542// that size.
543void TiledPictureRenderer::setupPowerOf2Tiles() {
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000544 // Only use enough tiles to cover the viewport
545 const int width = this->getViewWidth();
546 const int height = this->getViewHeight();
547
548 int rounded_value = width;
549 if (width % fTileMinPowerOf2Width != 0) {
550 rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000551 }
552
reed@google.come15b2f52013-12-18 04:59:26 +0000553 int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width)));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000554 int largest_possible_tile_size = 1 << num_bits;
555
scroggo@google.comcbcef702012-12-13 22:09:28 +0000556 fTilesX = fTilesY = 0;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000557 // The tile height is constant for a particular picture.
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000558 for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000559 fTilesY++;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000560 int tile_x_start = 0;
561 int current_width = largest_possible_tile_size;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000562 // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough
563 // to draw each tile.
564 fTileWidth = current_width;
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000565
566 while (current_width >= fTileMinPowerOf2Width) {
567 // It is very important this is a bitwise AND.
568 if (current_width & rounded_value) {
scroggo@google.comcbcef702012-12-13 22:09:28 +0000569 if (0 == tile_y_start) {
570 // Only count tiles in the X direction on the first pass.
571 fTilesX++;
572 }
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000573 *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start),
574 SkIntToScalar(tile_y_start),
575 SkIntToScalar(current_width),
576 SkIntToScalar(fTileHeight));
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000577 tile_x_start += current_width;
578 }
579
580 current_width >>= 1;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000581 }
582 }
583}
584
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000585/**
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000586 * Draw the specified picture to the canvas translated to rectangle provided, so that this mini
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000587 * canvas represents the rectangle's portion of the overall picture.
588 * Saves and restores so that the initial clip and matrix return to their state before this function
589 * is called.
590 */
robertphillipsce4dd3d2014-07-07 13:46:35 -0700591static void draw_tile_to_canvas(SkCanvas* canvas,
592 const SkRect& tileRect,
593 const SkPicture* picture) {
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000594 int saveCount = canvas->save();
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000595 // Translate so that we draw the correct portion of the picture.
596 // Perform a postTranslate so that the scaleFactor does not interfere with the positioning.
597 SkMatrix mat(canvas->getTotalMatrix());
598 mat.postTranslate(-tileRect.fLeft, -tileRect.fTop);
599 canvas->setMatrix(mat);
robertphillips9b14f262014-06-04 05:40:44 -0700600 canvas->drawPicture(picture);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000601 canvas->restoreToCount(saveCount);
602 canvas->flush();
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000603}
604
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000605///////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000606
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000607/**
608 * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap.
609 * If the src bitmap is too large to fit within the dst bitmap after the x and y
610 * offsets have been applied, any excess will be ignored (so only the top-left portion of the
611 * src bitmap will be copied).
612 *
613 * @param src source bitmap
614 * @param dst destination bitmap
615 * @param xOffset x-offset within destination bitmap
616 * @param yOffset y-offset within destination bitmap
617 */
618static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst,
619 int xOffset, int yOffset) {
620 for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) {
621 for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) {
622 *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y);
edisonn@google.com84f548c2012-12-18 22:24:03 +0000623 }
624 }
625}
626
scroggo@google.comcbcef702012-12-13 22:09:28 +0000627bool TiledPictureRenderer::nextTile(int &i, int &j) {
628 if (++fCurrentTileOffset < fTileRects.count()) {
629 i = fCurrentTileOffset % fTilesX;
630 j = fCurrentTileOffset / fTilesX;
631 return true;
632 }
633 return false;
634}
635
636void TiledPictureRenderer::drawCurrentTile() {
637 SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count());
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000638 draw_tile_to_canvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture);
scroggo@google.comcbcef702012-12-13 22:09:28 +0000639}
640
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000641bool TiledPictureRenderer::render(SkBitmap** out) {
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000642 SkASSERT(fPicture != NULL);
643 if (NULL == fPicture) {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000644 return false;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000645 }
646
edisonn@google.com84f548c2012-12-18 22:24:03 +0000647 SkBitmap bitmap;
648 if (out){
649 *out = SkNEW(SkBitmap);
650 setup_bitmap(*out, fPicture->width(), fPicture->height());
651 setup_bitmap(&bitmap, fTileWidth, fTileHeight);
652 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000653 bool success = true;
654 for (int i = 0; i < fTileRects.count(); ++i) {
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000655 draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture);
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000656 if (fEnableWrites) {
657 success &= write(fCanvas, fWritePath, fMismatchPath, fInputFilename, fJsonSummaryPtr,
658 fUseChecksumBasedFilenames, &i);
659 }
edisonn@google.com84f548c2012-12-18 22:24:03 +0000660 if (NULL != out) {
661 if (fCanvas->readPixels(&bitmap, 0, 0)) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000662 // Add this tile to the entire bitmap.
663 bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()),
664 SkScalarFloorToInt(fTileRects[i].top()));
edisonn@google.com84f548c2012-12-18 22:24:03 +0000665 } else {
666 success = false;
667 }
668 }
keyar@chromium.org163b5672012-08-01 17:53:29 +0000669 }
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000670 return success;
keyar@chromium.org163b5672012-08-01 17:53:29 +0000671}
672
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000673SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) {
674 SkCanvas* canvas = this->INHERITED::setupCanvas(width, height);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000675 SkASSERT(NULL != fPicture);
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000676 // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This
677 // is mostly important for tiles on the right and bottom edges as they may go over this area and
678 // the picture may have some commands that draw outside of this area and so should not actually
679 // be written.
680 // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set
681 // by INHERITED::setupCanvas.
682 SkRegion clipRegion;
683 clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight());
684 canvas->clipRegion(clipRegion);
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000685 return canvas;
686}
scroggo@google.com0a049b82012-11-02 22:01:26 +0000687
688SkString TiledPictureRenderer::getConfigNameInternal() {
689 SkString name;
690 if (fTileMinPowerOf2Width > 0) {
691 name.append("pow2tile_");
692 name.appendf("%i", fTileMinPowerOf2Width);
693 } else {
694 name.append("tile_");
695 if (fTileWidthPercentage > 0) {
696 name.appendf("%.f%%", fTileWidthPercentage);
697 } else {
698 name.appendf("%i", fTileWidth);
699 }
700 }
701 name.append("x");
702 if (fTileHeightPercentage > 0) {
703 name.appendf("%.f%%", fTileHeightPercentage);
704 } else {
705 name.appendf("%i", fTileHeight);
706 }
707 return name;
708}
709
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000710///////////////////////////////////////////////////////////////////////////////////////////////
711
scroggo@google.com9a412522012-09-07 15:21:18 +0000712void PlaybackCreationRenderer::setup() {
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000713 SkAutoTDelete<SkBBHFactory> factory(this->getFactory());
714 fRecorder.reset(SkNEW(SkPictureRecorder));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000715 SkCanvas* canvas = fRecorder->beginRecording(this->getViewWidth(), this->getViewHeight(),
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000716 factory.get(),
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000717 this->recordFlags());
718 this->scaleToScaleFactor(canvas);
robertphillips9b14f262014-06-04 05:40:44 -0700719 canvas->drawPicture(fPicture);
scroggo@google.com9a412522012-09-07 15:21:18 +0000720}
721
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000722bool PlaybackCreationRenderer::render(SkBitmap** out) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000723 fPicture.reset(fRecorder->endRecording());
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000724 // Since this class does not actually render, return false.
725 return false;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000726}
727
scroggo@google.com0a049b82012-11-02 22:01:26 +0000728SkString PlaybackCreationRenderer::getConfigNameInternal() {
729 return SkString("playback_creation");
730}
731
junov@chromium.org9313ca42012-11-02 18:11:49 +0000732///////////////////////////////////////////////////////////////////////////////////////////////
733// SkPicture variants for each BBoxHierarchy type
734
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000735SkBBHFactory* PictureRenderer::getFactory() {
junov@chromium.org9313ca42012-11-02 18:11:49 +0000736 switch (fBBoxHierarchyType) {
737 case kNone_BBoxHierarchyType:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000738 return NULL;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000739 case kQuadTree_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000740 return SkNEW(SkQuadTreeFactory);
junov@chromium.org9313ca42012-11-02 18:11:49 +0000741 case kRTree_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000742 return SkNEW(SkRTreeFactory);
junov@chromium.org7b537062012-11-06 18:58:43 +0000743 case kTileGrid_BBoxHierarchyType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000744 return SkNEW_ARGS(SkTileGridFactory, (fGridInfo));
junov@chromium.org9313ca42012-11-02 18:11:49 +0000745 }
746 SkASSERT(0); // invalid bbhType
747 return NULL;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000748}
junov@chromium.org9313ca42012-11-02 18:11:49 +0000749
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000750///////////////////////////////////////////////////////////////////////////////
751
752class GatherRenderer : public PictureRenderer {
753public:
krajcevskib1aded82014-08-18 07:52:17 -0700754#if SK_SUPPORT_GPU
755 GatherRenderer(const GrContext::Options& opts) : INHERITED(opts) { }
756#endif
757
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000758 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE {
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000759 SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()),
760 SkIntToScalar(fPicture->height()));
761 SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds);
762 SkSafeUnref(data);
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000763
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000764 return (fWritePath.isEmpty()); // we don't have anything to write
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000765 }
skia.committer@gmail.comc7b4be72012-12-11 02:01:20 +0000766
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000767private:
768 virtual SkString getConfigNameInternal() SK_OVERRIDE {
769 return SkString("gather_pixelrefs");
770 }
krajcevskib1aded82014-08-18 07:52:17 -0700771
772 typedef PictureRenderer INHERITED;
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000773};
774
krajcevskib1aded82014-08-18 07:52:17 -0700775#if SK_SUPPORT_GPU
776PictureRenderer* CreateGatherPixelRefsRenderer(const GrContext::Options& opts) {
777 return SkNEW_ARGS(GatherRenderer, (opts));
778}
779#else
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000780PictureRenderer* CreateGatherPixelRefsRenderer() {
781 return SkNEW(GatherRenderer);
782}
krajcevskib1aded82014-08-18 07:52:17 -0700783#endif
skia.committer@gmail.comc3d7d902012-11-30 02:01:24 +0000784
junov@chromium.org9313ca42012-11-02 18:11:49 +0000785} // namespace sk_tools