blob: e6f4020dfd091f73560ae31b34184afe08eea65b [file] [log] [blame]
caryclark@google.com411bb722012-11-06 21:29:16 +00001/*
keyar@chromium.org451bb9f2012-07-26 17:27:57 +00002 * 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
8#ifndef PictureRenderer_DEFINED
9#define PictureRenderer_DEFINED
scroggo@google.coma62da2f2012-11-02 21:28:12 +000010
scroggo@google.com161e1ba2013-03-04 16:41:06 +000011#include "SkCanvas.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000012#include "SkCountdown.h"
caryclark@google.coma3622372012-11-06 21:26:13 +000013#include "SkDrawFilter.h"
kelvinly4d1a3642014-06-26 11:26:40 -070014#include "SkJSONCPP.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000015#include "SkMath.h"
reed@google.comea6a3062012-11-06 22:14:54 +000016#include "SkPaint.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000017#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000018#include "SkPictureRecorder.h"
scroggo@google.comacfb30e2012-09-18 14:32:35 +000019#include "SkRect.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000020#include "SkRefCnt.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000021#include "SkRunnable.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000022#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000023#include "SkTDArray.h"
24#include "SkThreadPool.h"
25#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000026
keyar@chromium.org06125642012-08-20 15:03:33 +000027#if SK_SUPPORT_GPU
28#include "GrContextFactory.h"
29#include "GrContext.h"
30#endif
31
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000032#include "image_expectations.h"
33
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000034class SkBitmap;
35class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000036class SkGLContextHelper;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000037class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000038
39namespace sk_tools {
40
scroggo@google.comcbcef702012-12-13 22:09:28 +000041class TiledPictureRenderer;
42
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000043class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000044
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000045public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000046 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000047#if SK_ANGLE
48 kAngle_DeviceType,
49#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000050#if SK_MESA
51 kMesa_DeviceType,
52#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000053 kBitmap_DeviceType,
54#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000055 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000056 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000057#endif
58 };
59
junov@chromium.org9313ca42012-11-02 18:11:49 +000060 enum BBoxHierarchyType {
61 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000062 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000063 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000064 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000065
66 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000067 };
68
caryclark@google.coma3622372012-11-06 21:26:13 +000069 // this uses SkPaint::Flags as a base and adds additional flags
70 enum DrawFilterFlags {
71 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000072 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
73 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
74 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000075 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000076 };
77
robertphillips@google.com49149312013-07-03 15:34:35 +000078 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000079 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
80 hinting_flag_must_be_greater);
81 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
82 slight_hinting_flag_must_be_greater);
83
scroggo@google.coma62da2f2012-11-02 21:28:12 +000084 /**
85 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000086 *
87 * @param pict The SkPicture to render.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000088 * @param writePath The output directory within which this renderer should write all images,
89 * or NULL if this renderer should not write all images.
90 * @param mismatchPath The output directory within which this renderer should write any images
91 * which do not match expectations, or NULL if this renderer should not write mismatches.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000092 * @param inputFilename The name of the input file we are rendering.
93 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
94 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000095 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000096 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000097 const SkString* inputFilename, bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +000098
99 /**
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000100 * TODO(epoger): Temporary hack, while we work on http://skbug.com/2584 ('bench_pictures is
101 * timing reading pixels and writing json files'), such that:
102 * - render_pictures can call this method and continue to work
103 * - any other callers (bench_pictures) will skip calls to write() by default
104 */
105 void enableWrites() { fEnableWrites = true; }
106
107 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000108 * Set the viewport so that only the portion listed gets drawn.
109 */
110 void setViewport(SkISize size) { fViewport = size; }
111
112 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000113 * Set the scale factor at which draw the picture.
114 */
115 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
116
117 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000118 * Perform any setup that should done prior to each iteration of render() which should not be
119 * timed.
120 */
121 virtual void setup() {}
122
123 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000124 * Perform the work. If this is being called within the context of bench_pictures,
125 * this is the step that will be timed.
126 *
127 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
128 * it is recording the source SkPicture into another SkPicture.
129 *
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000130 * If fWritePath has been specified, the result of the work will be written to that dir.
131 * If fMismatchPath has been specified, and the actual image result differs from its
132 * expectation, the result of the work will be written to that dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000133 *
134 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
135 * output image into it, and return it here. (Some subclasses ignore this parameter)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000136 * @return bool True if rendering succeeded and, if fWritePath had been specified, the output
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000137 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000138 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000139 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000140
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000141 /**
142 * Called once finished with a particular SkPicture, before calling init again, and before
143 * being done with this Renderer.
144 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000145 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000146
scroggo@google.comcbcef702012-12-13 22:09:28 +0000147 /**
148 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
149 * TiledPictureRender so its methods can be called.
150 */
151 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
152
scroggo@google.com08085f82013-01-28 20:40:24 +0000153 /**
154 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000155 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000156 * @param callFinish Whether to call finish.
157 */
158 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000159
scroggo@google.com0556ea02013-02-08 19:38:21 +0000160 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000161 * Remove all decoded textures from the CPU caches and all uploaded textures
162 * from the GPU.
163 */
164 void purgeTextures();
165
166 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000167 * Set the backend type. Returns true on success and false on failure.
168 */
169 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000170 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000171#if SK_SUPPORT_GPU
172 // In case this function is called more than once
173 SkSafeUnref(fGrContext);
174 fGrContext = NULL;
175 // Set to Native so it will have an initial value.
176 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
177#endif
178 switch(deviceType) {
179 case kBitmap_DeviceType:
180 return true;
181#if SK_SUPPORT_GPU
182 case kGPU_DeviceType:
183 // Already set to GrContextFactory::kNative_GLContextType, above.
184 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000185 case kNVPR_DeviceType:
186 glContextType = GrContextFactory::kNVPR_GLContextType;
187 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000188#if SK_ANGLE
189 case kAngle_DeviceType:
190 glContextType = GrContextFactory::kANGLE_GLContextType;
191 break;
192#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000193#if SK_MESA
194 case kMesa_DeviceType:
195 glContextType = GrContextFactory::kMESA_GLContextType;
196 break;
197#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000198#endif
199 default:
200 // Invalid device type.
201 return false;
202 }
203#if SK_SUPPORT_GPU
rmistry05ead8a2014-06-23 06:13:46 -0700204 fGrContext = fGrContextFactory.get(glContextType);
scroggo@google.com0556ea02013-02-08 19:38:21 +0000205 if (NULL == fGrContext) {
206 return false;
207 } else {
208 fGrContext->ref();
209 return true;
210 }
211#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000212 }
213
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000214#if SK_SUPPORT_GPU
215 void setSampleCount(int sampleCount) {
216 fSampleCount = sampleCount;
217 }
218#endif
219
caryclark@google.come3e940c2012-11-07 16:42:17 +0000220 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
221 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000222 fDrawFiltersConfig = configName;
223 }
224
junov@chromium.org9313ca42012-11-02 18:11:49 +0000225 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
226 fBBoxHierarchyType = bbhType;
227 }
228
junov@chromium.orge286e842013-03-13 17:27:16 +0000229 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
230
junov@chromium.org7b537062012-11-06 18:58:43 +0000231 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000232 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000233 }
234
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000235 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000236 fJsonSummaryPtr = jsonSummaryPtr;
237 }
238
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000239 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000240 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000241 }
242
scroggo@google.com9a412522012-09-07 15:21:18 +0000243 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
244
245 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
246
scroggo@google.com0a049b82012-11-02 22:01:26 +0000247 /**
248 * Reports the configuration of this PictureRenderer.
249 */
250 SkString getConfigName() {
251 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000252 if (!fViewport.isEmpty()) {
253 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
254 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000255 if (fScaleFactor != SK_Scalar1) {
256 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
257 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000258 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
259 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000260 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
261 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000262 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
263 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700264 config.append("_");
265 config.appendS32(fGridInfo.fTileInterval.width());
266 config.append("x");
267 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000268 }
269#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000270 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000271 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000272 if (fSampleCount) {
273 config.appendf("_msaa%d", fSampleCount);
274 } else {
275 config.append("_gpu");
276 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000277 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000278 case kNVPR_DeviceType:
279 config.appendf("_nvprmsaa%d", fSampleCount);
280 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000281#if SK_ANGLE
282 case kAngle_DeviceType:
283 config.append("_angle");
284 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000285#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000286#if SK_MESA
287 case kMesa_DeviceType:
288 config.append("_mesa");
289 break;
290#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000291 default:
292 // Assume that no extra info means bitmap.
293 break;
294 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000295#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000296 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000297 return config;
298 }
299
kelvinly4d1a3642014-06-26 11:26:40 -0700300 Json::Value getJSONConfig() {
301 Json::Value result;
302
303 result["mode"] = this->getConfigNameInternal().c_str();
304 result["scale"] = 1.0f;
305 if (SK_Scalar1 != fScaleFactor) {
306 result["scale"] = SkScalarToFloat(fScaleFactor);
307 }
308 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
309 result["bbh"] = "rtree";
310 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
311 result["bbh"] = "quadtree";
312 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
313 SkString tmp("grid_");
314 tmp.appendS32(fGridInfo.fTileInterval.width());
315 tmp.append("x");
316 tmp.appendS32(fGridInfo.fTileInterval.height());
317 result["bbh"] = tmp.c_str();
318 }
319#if SK_SUPPORT_GPU
320 SkString tmp;
321 switch (fDeviceType) {
322 case kGPU_DeviceType:
323 if (0 != fSampleCount) {
324 tmp = "msaa";
325 tmp.appendS32(fSampleCount);
326 result["config"] = tmp.c_str();
327 } else {
328 result["config"] = "gpu";
329 }
330 break;
331 case kNVPR_DeviceType:
332 tmp = "nvprmsaa";
333 tmp.appendS32(fSampleCount);
334 result["config"] = tmp.c_str();
335 break;
336#if SK_ANGLE
337 case kAngle_DeviceType:
338 result["config"] = "angle";
339 break;
340#endif
341#if SK_MESA
342 case kMesa_DeviceType:
343 result["config"] = "mesa";
344 break;
345#endif
346 default:
347 // Assume that no extra info means bitmap.
348 break;
349 }
350#endif
351 return result;
352 }
353
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000354#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000355 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000356 switch (fDeviceType) {
357 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000358 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000359 // fall through
360#if SK_ANGLE
361 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000362 // fall through
363#endif
364#if SK_MESA
365 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000366#endif
367 return true;
368 default:
369 return false;
370 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000371 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000372
robertphillips@google.com6177e692013-02-28 20:16:25 +0000373 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000374 GrContextFactory::GLContextType glContextType
375 = GrContextFactory::kNull_GLContextType;
376 switch(fDeviceType) {
377 case kGPU_DeviceType:
378 glContextType = GrContextFactory::kNative_GLContextType;
379 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000380 case kNVPR_DeviceType:
381 glContextType = GrContextFactory::kNVPR_GLContextType;
382 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000383#if SK_ANGLE
384 case kAngle_DeviceType:
385 glContextType = GrContextFactory::kANGLE_GLContextType;
386 break;
387#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000388#if SK_MESA
389 case kMesa_DeviceType:
390 glContextType = GrContextFactory::kMESA_GLContextType;
391 break;
392#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000393 default:
394 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000395 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000396 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000397 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000398
399 GrContext* getGrContext() {
400 return fGrContext;
401 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000402#endif
403
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000404 SkCanvas* getCanvas() {
405 return fCanvas;
406 }
407
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000408 SkPicture* getPicture() {
409 return fPicture;
410 }
411
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000412 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000413 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000414 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000415 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000416 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000417 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000418#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000419 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000420 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000421#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000422 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000423 fGridInfo.fMargin.setEmpty();
424 fGridInfo.fOffset.setZero();
425 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000426 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000427 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000428 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000429
scroggo@google.com0556ea02013-02-08 19:38:21 +0000430#if SK_SUPPORT_GPU
431 virtual ~PictureRenderer() {
432 SkSafeUnref(fGrContext);
433 }
434#endif
435
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000436protected:
437 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000438 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000439 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000440 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000441 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000442 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000443 BBoxHierarchyType fBBoxHierarchyType;
444 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
445 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000446 SkString fWritePath;
447 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000448 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000449 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000450
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000451 void buildBBoxHierarchy();
452
453 /**
454 * Return the total width that should be drawn. If the viewport width has been set greater than
455 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
456 */
457 int getViewWidth();
458
459 /**
460 * Return the total height that should be drawn. If the viewport height has been set greater
461 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
462 */
463 int getViewHeight();
464
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000465 /**
466 * Scales the provided canvas to the scale factor set by setScaleFactor.
467 */
468 void scaleToScaleFactor(SkCanvas*);
469
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000470 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700471 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000472 SkCanvas* setupCanvas();
473 virtual SkCanvas* setupCanvas(int width, int height);
474
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000475 /**
476 * Copy src to dest; if src==NULL, set dest to empty string.
477 */
478 static void CopyString(SkString* dest, const SkString* src);
479
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000480private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000481 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000482 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000483#if SK_SUPPORT_GPU
484 GrContextFactory fGrContextFactory;
485 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000486 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000487#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000488
scroggo@google.com0a049b82012-11-02 22:01:26 +0000489 virtual SkString getConfigNameInternal() = 0;
490
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000491 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000492};
493
scroggo@google.com9a412522012-09-07 15:21:18 +0000494/**
495 * This class does not do any rendering, but its render function executes recording, which we want
496 * to time.
497 */
498class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000499 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000500
501 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
502
503 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000504
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000505protected:
506 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
507
scroggo@google.com0a049b82012-11-02 22:01:26 +0000508private:
509 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000510};
511
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000512class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000513public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000514 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000515
516private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000517 virtual SkString getConfigNameInternal() SK_OVERRIDE;
518
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000519 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000520};
521
522class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000523public:
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000524 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000525 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000526
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000527 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000528
529private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000530 virtual SkString getConfigNameInternal() SK_OVERRIDE;
531
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000532 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000533};
534
535class TiledPictureRenderer : public PictureRenderer {
536public:
537 TiledPictureRenderer();
538
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000539 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000540 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000541
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000542 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000543 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000544 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000545 * created for each tile, named "path0.png", "path1.png", etc.
546 * Multithreaded mode currently does not support writing to a file.
547 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000548 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000549
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000550 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000551
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000552 void setTileWidth(int width) {
553 fTileWidth = width;
554 }
555
556 int getTileWidth() const {
557 return fTileWidth;
558 }
559
560 void setTileHeight(int height) {
561 fTileHeight = height;
562 }
563
564 int getTileHeight() const {
565 return fTileHeight;
566 }
567
568 void setTileWidthPercentage(double percentage) {
569 fTileWidthPercentage = percentage;
570 }
571
keyar@chromium.org163b5672012-08-01 17:53:29 +0000572 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000573 return fTileWidthPercentage;
574 }
575
576 void setTileHeightPercentage(double percentage) {
577 fTileHeightPercentage = percentage;
578 }
579
keyar@chromium.org163b5672012-08-01 17:53:29 +0000580 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000581 return fTileHeightPercentage;
582 }
583
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000584 void setTileMinPowerOf2Width(int width) {
585 SkASSERT(SkIsPow2(width) && width > 0);
586 if (!SkIsPow2(width) || width <= 0) {
587 return;
588 }
589
590 fTileMinPowerOf2Width = width;
591 }
592
593 int getTileMinPowerOf2Width() const {
594 return fTileMinPowerOf2Width;
595 }
596
scroggo@google.comcbcef702012-12-13 22:09:28 +0000597 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
598
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000599 virtual bool supportsTimingIndividualTiles() { return true; }
600
scroggo@google.comcbcef702012-12-13 22:09:28 +0000601 /**
602 * Report the number of tiles in the x and y directions. Must not be called before init.
603 * @param x Output parameter identifying the number of tiles in the x direction.
604 * @param y Output parameter identifying the number of tiles in the y direction.
605 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
606 * unmodified.
607 */
608 bool tileDimensions(int& x, int&y);
609
610 /**
611 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
612 * for the first time.
613 * @param i Output parameter identifying the column of the next tile to be drawn on the next
614 * call to drawNextTile.
615 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
616 * to drawNextTile.
617 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
618 * is within the range of tiles. If false, i and j are unmodified.
619 */
620 bool nextTile(int& i, int& j);
621
622 /**
623 * Render one tile. This will draw the same tile each time it is called until nextTile is
624 * called. The tile rendered will depend on how many calls have been made to nextTile.
625 * It is an error to call this without first calling nextTile, or if nextTile returns false.
626 */
627 void drawCurrentTile();
628
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000629protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000630 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000631
scroggo@google.com0a049b82012-11-02 22:01:26 +0000632 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
633 virtual SkString getConfigNameInternal() SK_OVERRIDE;
634
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000635private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000636 int fTileWidth;
637 int fTileHeight;
638 double fTileWidthPercentage;
639 double fTileHeightPercentage;
640 int fTileMinPowerOf2Width;
641
642 // These variables are only used for timing individual tiles.
643 // Next tile to draw in fTileRects.
644 int fCurrentTileOffset;
645 // Number of tiles in the x direction.
646 int fTilesX;
647 // Number of tiles in the y direction.
648 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000649
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000650 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000651 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000652
653 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000654};
655
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000656class CloneData;
657
658class MultiCorePictureRenderer : public TiledPictureRenderer {
659public:
660 explicit MultiCorePictureRenderer(int threadCount);
661
662 ~MultiCorePictureRenderer();
663
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000664 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000665 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000666
667 /**
668 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
669 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000670 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000671
672 virtual void end() SK_OVERRIDE;
673
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000674 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
675
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000676private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000677 virtual SkString getConfigNameInternal() SK_OVERRIDE;
678
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000679 const int fNumThreads;
680 SkTDArray<SkCanvas*> fCanvasPool;
681 SkThreadPool fThreadPool;
682 SkPicture* fPictureClones;
683 CloneData** fCloneData;
684 SkCountdown fCountdown;
685
686 typedef TiledPictureRenderer INHERITED;
687};
688
scroggo@google.com9a412522012-09-07 15:21:18 +0000689/**
690 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
691 * into an SkPicturePlayback, which we want to time.
692 */
693class PlaybackCreationRenderer : public PictureRenderer {
694public:
695 virtual void setup() SK_OVERRIDE;
696
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000697 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000698
699 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
700
701 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
702
703private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000704 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000705
706 virtual SkString getConfigNameInternal() SK_OVERRIDE;
707
scroggo@google.com9a412522012-09-07 15:21:18 +0000708 typedef PictureRenderer INHERITED;
709};
710
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000711extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000712extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000713
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000714}
715
716#endif // PictureRenderer_DEFINED