blob: f11b198cf3d428187acc13e85a2919ca308e5512 [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"
caryclark@google.coma3622372012-11-06 21:26:13 +000012#include "SkDrawFilter.h"
kelvinly4d1a3642014-06-26 11:26:40 -070013#include "SkJSONCPP.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000014#include "SkMath.h"
reed@google.comea6a3062012-11-06 22:14:54 +000015#include "SkPaint.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000016#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000017#include "SkPictureRecorder.h"
scroggo@google.comacfb30e2012-09-18 14:32:35 +000018#include "SkRect.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000019#include "SkRefCnt.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000020#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000021#include "SkTDArray.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000022#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000023
keyar@chromium.org06125642012-08-20 15:03:33 +000024#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrContext.h"
27#endif
28
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000029#include "image_expectations.h"
30
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000031class SkBitmap;
32class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000033class SkGLContextHelper;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000034class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000035
36namespace sk_tools {
37
scroggo@google.comcbcef702012-12-13 22:09:28 +000038class TiledPictureRenderer;
39
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000040class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000041
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000042public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000043 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000044#if SK_ANGLE
45 kAngle_DeviceType,
46#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000047#if SK_MESA
48 kMesa_DeviceType,
49#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000050 kBitmap_DeviceType,
51#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000052 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000053 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000054#endif
55 };
56
junov@chromium.org9313ca42012-11-02 18:11:49 +000057 enum BBoxHierarchyType {
58 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000059 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000060 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000061 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000062
63 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000064 };
65
caryclark@google.coma3622372012-11-06 21:26:13 +000066 // this uses SkPaint::Flags as a base and adds additional flags
67 enum DrawFilterFlags {
68 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000069 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
70 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
71 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000072 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000073 };
74
robertphillips@google.com49149312013-07-03 15:34:35 +000075 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000076 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
77 hinting_flag_must_be_greater);
78 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
79 slight_hinting_flag_must_be_greater);
80
scroggo@google.coma62da2f2012-11-02 21:28:12 +000081 /**
82 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000083 *
84 * @param pict The SkPicture to render.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000085 * @param writePath The output directory within which this renderer should write all images,
86 * or NULL if this renderer should not write all images.
87 * @param mismatchPath The output directory within which this renderer should write any images
88 * which do not match expectations, or NULL if this renderer should not write mismatches.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000089 * @param inputFilename The name of the input file we are rendering.
90 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
91 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000092 */
robertphillipsce4dd3d2014-07-07 13:46:35 -070093 virtual void init(const SkPicture* pict,
94 const SkString* writePath,
95 const SkString* mismatchPath,
96 const SkString* inputFilename,
97 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 */
kkinnunen80549fc2014-06-30 06:36:31 -0700169#if SK_SUPPORT_GPU
170 bool setDeviceType(SkDeviceTypes deviceType, GrGLStandard gpuAPI = kNone_GrGLStandard) {
171#else
scroggo@google.com0556ea02013-02-08 19:38:21 +0000172 bool setDeviceType(SkDeviceTypes deviceType) {
kkinnunen80549fc2014-06-30 06:36:31 -0700173#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000174 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000175#if SK_SUPPORT_GPU
176 // In case this function is called more than once
177 SkSafeUnref(fGrContext);
178 fGrContext = NULL;
179 // Set to Native so it will have an initial value.
180 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
181#endif
182 switch(deviceType) {
183 case kBitmap_DeviceType:
184 return true;
185#if SK_SUPPORT_GPU
186 case kGPU_DeviceType:
187 // Already set to GrContextFactory::kNative_GLContextType, above.
188 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000189 case kNVPR_DeviceType:
190 glContextType = GrContextFactory::kNVPR_GLContextType;
191 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000192#if SK_ANGLE
193 case kAngle_DeviceType:
194 glContextType = GrContextFactory::kANGLE_GLContextType;
195 break;
196#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000197#if SK_MESA
198 case kMesa_DeviceType:
199 glContextType = GrContextFactory::kMESA_GLContextType;
200 break;
201#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000202#endif
203 default:
204 // Invalid device type.
205 return false;
206 }
207#if SK_SUPPORT_GPU
kkinnunen80549fc2014-06-30 06:36:31 -0700208 fGrContext = fGrContextFactory.get(glContextType, gpuAPI);
scroggo@google.com0556ea02013-02-08 19:38:21 +0000209 if (NULL == fGrContext) {
210 return false;
211 } else {
212 fGrContext->ref();
213 return true;
214 }
215#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000216 }
217
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000218#if SK_SUPPORT_GPU
219 void setSampleCount(int sampleCount) {
220 fSampleCount = sampleCount;
221 }
222#endif
223
caryclark@google.come3e940c2012-11-07 16:42:17 +0000224 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
225 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000226 fDrawFiltersConfig = configName;
227 }
228
junov@chromium.org9313ca42012-11-02 18:11:49 +0000229 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
230 fBBoxHierarchyType = bbhType;
231 }
232
junov@chromium.orge286e842013-03-13 17:27:16 +0000233 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
234
junov@chromium.org7b537062012-11-06 18:58:43 +0000235 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000236 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000237 }
238
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000239 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000240 fJsonSummaryPtr = jsonSummaryPtr;
241 }
242
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000243 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000244 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000245 }
246
scroggo@google.com9a412522012-09-07 15:21:18 +0000247 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
248
249 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
250
scroggo@google.com0a049b82012-11-02 22:01:26 +0000251 /**
252 * Reports the configuration of this PictureRenderer.
253 */
254 SkString getConfigName() {
255 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000256 if (!fViewport.isEmpty()) {
257 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
258 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000259 if (fScaleFactor != SK_Scalar1) {
260 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
261 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000262 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
263 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000264 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
265 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000266 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
267 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700268 config.append("_");
269 config.appendS32(fGridInfo.fTileInterval.width());
270 config.append("x");
271 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000272 }
273#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000274 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000275 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000276 if (fSampleCount) {
277 config.appendf("_msaa%d", fSampleCount);
278 } else {
279 config.append("_gpu");
280 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000281 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000282 case kNVPR_DeviceType:
283 config.appendf("_nvprmsaa%d", fSampleCount);
284 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000285#if SK_ANGLE
286 case kAngle_DeviceType:
287 config.append("_angle");
288 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000289#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000290#if SK_MESA
291 case kMesa_DeviceType:
292 config.append("_mesa");
293 break;
294#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295 default:
296 // Assume that no extra info means bitmap.
297 break;
298 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000299#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000300 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000301 return config;
302 }
303
kelvinly4d1a3642014-06-26 11:26:40 -0700304 Json::Value getJSONConfig() {
305 Json::Value result;
306
307 result["mode"] = this->getConfigNameInternal().c_str();
308 result["scale"] = 1.0f;
309 if (SK_Scalar1 != fScaleFactor) {
310 result["scale"] = SkScalarToFloat(fScaleFactor);
311 }
312 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
313 result["bbh"] = "rtree";
314 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
315 result["bbh"] = "quadtree";
316 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
317 SkString tmp("grid_");
318 tmp.appendS32(fGridInfo.fTileInterval.width());
319 tmp.append("x");
320 tmp.appendS32(fGridInfo.fTileInterval.height());
321 result["bbh"] = tmp.c_str();
322 }
323#if SK_SUPPORT_GPU
324 SkString tmp;
325 switch (fDeviceType) {
326 case kGPU_DeviceType:
327 if (0 != fSampleCount) {
328 tmp = "msaa";
329 tmp.appendS32(fSampleCount);
330 result["config"] = tmp.c_str();
331 } else {
332 result["config"] = "gpu";
333 }
334 break;
335 case kNVPR_DeviceType:
336 tmp = "nvprmsaa";
337 tmp.appendS32(fSampleCount);
338 result["config"] = tmp.c_str();
339 break;
340#if SK_ANGLE
341 case kAngle_DeviceType:
342 result["config"] = "angle";
343 break;
344#endif
345#if SK_MESA
346 case kMesa_DeviceType:
347 result["config"] = "mesa";
348 break;
349#endif
350 default:
351 // Assume that no extra info means bitmap.
352 break;
353 }
354#endif
355 return result;
356 }
357
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000358#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000359 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000360 switch (fDeviceType) {
361 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000362 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000363 // fall through
364#if SK_ANGLE
365 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000366 // fall through
367#endif
368#if SK_MESA
369 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000370#endif
371 return true;
372 default:
373 return false;
374 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000375 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000376
robertphillips@google.com6177e692013-02-28 20:16:25 +0000377 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000378 GrContextFactory::GLContextType glContextType
379 = GrContextFactory::kNull_GLContextType;
380 switch(fDeviceType) {
381 case kGPU_DeviceType:
382 glContextType = GrContextFactory::kNative_GLContextType;
383 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000384 case kNVPR_DeviceType:
385 glContextType = GrContextFactory::kNVPR_GLContextType;
386 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000387#if SK_ANGLE
388 case kAngle_DeviceType:
389 glContextType = GrContextFactory::kANGLE_GLContextType;
390 break;
391#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000392#if SK_MESA
393 case kMesa_DeviceType:
394 glContextType = GrContextFactory::kMESA_GLContextType;
395 break;
396#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000397 default:
398 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000399 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000400 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000401 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000402
403 GrContext* getGrContext() {
404 return fGrContext;
405 }
krajcevskib1aded82014-08-18 07:52:17 -0700406
407 const GrContext::Options& getGrContextOptions() {
408 return fGrContextFactory.getGlobalOptions();
409 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000410#endif
411
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000412 SkCanvas* getCanvas() {
413 return fCanvas;
414 }
415
robertphillipsce4dd3d2014-07-07 13:46:35 -0700416 const SkPicture* getPicture() {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000417 return fPicture;
418 }
krajcevskib1aded82014-08-18 07:52:17 -0700419
420#if SK_SUPPORT_GPU
421 explicit PictureRenderer(const GrContext::Options &opts)
422#else
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000423 PictureRenderer()
krajcevskib1aded82014-08-18 07:52:17 -0700424#endif
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000425 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000426 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000427 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000428 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000429 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000430#if SK_SUPPORT_GPU
krajcevskib1aded82014-08-18 07:52:17 -0700431 , fGrContextFactory(opts)
scroggo@google.com0556ea02013-02-08 19:38:21 +0000432 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000433 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000434#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000435 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000436 fGridInfo.fMargin.setEmpty();
437 fGridInfo.fOffset.setZero();
438 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000439 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000440 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000441 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000442
scroggo@google.com0556ea02013-02-08 19:38:21 +0000443#if SK_SUPPORT_GPU
444 virtual ~PictureRenderer() {
445 SkSafeUnref(fGrContext);
446 }
447#endif
448
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000449protected:
450 SkAutoTUnref<SkCanvas> fCanvas;
robertphillipsce4dd3d2014-07-07 13:46:35 -0700451 SkAutoTUnref<const SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000452 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000453 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000454 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000455 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000456 BBoxHierarchyType fBBoxHierarchyType;
457 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
458 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000459 SkString fWritePath;
460 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000461 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000462 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000463
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000464 void buildBBoxHierarchy();
465
466 /**
467 * Return the total width that should be drawn. If the viewport width has been set greater than
468 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
469 */
470 int getViewWidth();
471
472 /**
473 * Return the total height that should be drawn. If the viewport height has been set greater
474 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
475 */
476 int getViewHeight();
477
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000478 /**
479 * Scales the provided canvas to the scale factor set by setScaleFactor.
480 */
481 void scaleToScaleFactor(SkCanvas*);
482
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000483 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700484 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000485 SkCanvas* setupCanvas();
486 virtual SkCanvas* setupCanvas(int width, int height);
487
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000488 /**
489 * Copy src to dest; if src==NULL, set dest to empty string.
490 */
491 static void CopyString(SkString* dest, const SkString* src);
492
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000493private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000494 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000495 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000496#if SK_SUPPORT_GPU
497 GrContextFactory fGrContextFactory;
498 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000499 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000500#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000501
scroggo@google.com0a049b82012-11-02 22:01:26 +0000502 virtual SkString getConfigNameInternal() = 0;
503
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000504 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000505};
506
scroggo@google.com9a412522012-09-07 15:21:18 +0000507/**
508 * This class does not do any rendering, but its render function executes recording, which we want
509 * to time.
510 */
511class RecordPictureRenderer : public PictureRenderer {
krajcevskib1aded82014-08-18 07:52:17 -0700512public:
513#if SK_SUPPORT_GPU
514 RecordPictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
515#endif
516
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000517 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000518
519 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
520
521 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000522
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000523protected:
524 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
525
scroggo@google.com0a049b82012-11-02 22:01:26 +0000526private:
527 virtual SkString getConfigNameInternal() SK_OVERRIDE;
krajcevskib1aded82014-08-18 07:52:17 -0700528
529 typedef PictureRenderer INHERITED;
scroggo@google.com9a412522012-09-07 15:21:18 +0000530};
531
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000532class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000533public:
krajcevskib1aded82014-08-18 07:52:17 -0700534#if SK_SUPPORT_GPU
535 PipePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
536#endif
537
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000538 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000539
540private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000541 virtual SkString getConfigNameInternal() SK_OVERRIDE;
542
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000543 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000544};
545
546class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000547public:
krajcevskib1aded82014-08-18 07:52:17 -0700548#if SK_SUPPORT_GPU
549 SimplePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
550#endif
551
robertphillipsce4dd3d2014-07-07 13:46:35 -0700552 virtual void init(const SkPicture* pict,
553 const SkString* writePath,
554 const SkString* mismatchPath,
555 const SkString* inputFilename,
556 bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000557
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000558 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000559
560private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000561 virtual SkString getConfigNameInternal() SK_OVERRIDE;
562
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000563 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000564};
565
566class TiledPictureRenderer : public PictureRenderer {
567public:
krajcevskib1aded82014-08-18 07:52:17 -0700568#if SK_SUPPORT_GPU
569 TiledPictureRenderer(const GrContext::Options &opts);
570#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000571 TiledPictureRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700572#endif
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000573
robertphillipsce4dd3d2014-07-07 13:46:35 -0700574 virtual void init(const SkPicture* pict,
575 const SkString* writePath,
576 const SkString* mismatchPath,
577 const SkString* inputFilename,
578 bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000579
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000580 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000581 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000582 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000583 * created for each tile, named "path0.png", "path1.png", etc.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000584 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000585 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000586
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000587 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000588
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000589 void setTileWidth(int width) {
590 fTileWidth = width;
591 }
592
593 int getTileWidth() const {
594 return fTileWidth;
595 }
596
597 void setTileHeight(int height) {
598 fTileHeight = height;
599 }
600
601 int getTileHeight() const {
602 return fTileHeight;
603 }
604
605 void setTileWidthPercentage(double percentage) {
606 fTileWidthPercentage = percentage;
607 }
608
keyar@chromium.org163b5672012-08-01 17:53:29 +0000609 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000610 return fTileWidthPercentage;
611 }
612
613 void setTileHeightPercentage(double percentage) {
614 fTileHeightPercentage = percentage;
615 }
616
keyar@chromium.org163b5672012-08-01 17:53:29 +0000617 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000618 return fTileHeightPercentage;
619 }
620
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000621 void setTileMinPowerOf2Width(int width) {
622 SkASSERT(SkIsPow2(width) && width > 0);
623 if (!SkIsPow2(width) || width <= 0) {
624 return;
625 }
626
627 fTileMinPowerOf2Width = width;
628 }
629
630 int getTileMinPowerOf2Width() const {
631 return fTileMinPowerOf2Width;
632 }
633
scroggo@google.comcbcef702012-12-13 22:09:28 +0000634 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
635
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000636 virtual bool supportsTimingIndividualTiles() { return true; }
637
scroggo@google.comcbcef702012-12-13 22:09:28 +0000638 /**
639 * Report the number of tiles in the x and y directions. Must not be called before init.
640 * @param x Output parameter identifying the number of tiles in the x direction.
641 * @param y Output parameter identifying the number of tiles in the y direction.
642 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
643 * unmodified.
644 */
645 bool tileDimensions(int& x, int&y);
646
647 /**
648 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
649 * for the first time.
650 * @param i Output parameter identifying the column of the next tile to be drawn on the next
651 * call to drawNextTile.
652 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
653 * to drawNextTile.
654 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
655 * is within the range of tiles. If false, i and j are unmodified.
656 */
657 bool nextTile(int& i, int& j);
658
659 /**
660 * Render one tile. This will draw the same tile each time it is called until nextTile is
661 * called. The tile rendered will depend on how many calls have been made to nextTile.
662 * It is an error to call this without first calling nextTile, or if nextTile returns false.
663 */
664 void drawCurrentTile();
665
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000666protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000667 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000668
scroggo@google.com0a049b82012-11-02 22:01:26 +0000669 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
670 virtual SkString getConfigNameInternal() SK_OVERRIDE;
671
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000672private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000673 int fTileWidth;
674 int fTileHeight;
675 double fTileWidthPercentage;
676 double fTileHeightPercentage;
677 int fTileMinPowerOf2Width;
678
679 // These variables are only used for timing individual tiles.
680 // Next tile to draw in fTileRects.
681 int fCurrentTileOffset;
682 // Number of tiles in the x direction.
683 int fTilesX;
684 // Number of tiles in the y direction.
685 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000686
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000687 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000688 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000689
690 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000691};
692
scroggo@google.com9a412522012-09-07 15:21:18 +0000693/**
694 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
695 * into an SkPicturePlayback, which we want to time.
696 */
697class PlaybackCreationRenderer : public PictureRenderer {
698public:
krajcevskib1aded82014-08-18 07:52:17 -0700699#if SK_SUPPORT_GPU
700 PlaybackCreationRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
701#endif
702
scroggo@google.com9a412522012-09-07 15:21:18 +0000703 virtual void setup() SK_OVERRIDE;
704
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000705 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000706
707 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
708
709 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
710
711private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000712 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000713
714 virtual SkString getConfigNameInternal() SK_OVERRIDE;
715
scroggo@google.com9a412522012-09-07 15:21:18 +0000716 typedef PictureRenderer INHERITED;
717};
718
krajcevskib1aded82014-08-18 07:52:17 -0700719#if SK_SUPPORT_GPU
720extern PictureRenderer* CreateGatherPixelRefsRenderer(const GrContext::Options& opts);
721#else
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000722extern PictureRenderer* CreateGatherPixelRefsRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700723#endif
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000724
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000725}
726
727#endif // PictureRenderer_DEFINED