blob: c8462d9f037c213d1b9bc718c6ac524100be5f69 [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;
kkinnunen9e61bb72014-10-09 05:24:15 -070033class SkGLContext;
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,
59 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000060 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000061
62 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000063 };
64
caryclark@google.coma3622372012-11-06 21:26:13 +000065 // this uses SkPaint::Flags as a base and adds additional flags
66 enum DrawFilterFlags {
67 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000068 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
69 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
70 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000071 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000072 };
73
robertphillips@google.com49149312013-07-03 15:34:35 +000074 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000075 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
76 hinting_flag_must_be_greater);
77 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
78 slight_hinting_flag_must_be_greater);
79
scroggo@google.coma62da2f2012-11-02 21:28:12 +000080 /**
81 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000082 *
83 * @param pict The SkPicture to render.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000084 * @param writePath The output directory within which this renderer should write all images,
85 * or NULL if this renderer should not write all images.
86 * @param mismatchPath The output directory within which this renderer should write any images
87 * which do not match expectations, or NULL if this renderer should not write mismatches.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000088 * @param inputFilename The name of the input file we are rendering.
89 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
90 * bitmap images to disk.
robertphillips78c71272014-10-09 04:59:19 -070091 * @param useMultiPictureDraw true if MultiPictureDraw should be used for rendering
scroggo@google.coma62da2f2012-11-02 21:28:12 +000092 */
mtklein2a65a232014-08-26 14:07:04 -070093 virtual void init(const SkPicture* pict,
94 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -070095 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -070096 const SkString* inputFilename,
robertphillips78c71272014-10-09 04:59:19 -070097 bool useChecksumBasedFilenames,
98 bool useMultiPictureDraw);
scroggo@google.com9a412522012-09-07 15:21:18 +000099
100 /**
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000101 * TODO(epoger): Temporary hack, while we work on http://skbug.com/2584 ('bench_pictures is
102 * timing reading pixels and writing json files'), such that:
103 * - render_pictures can call this method and continue to work
104 * - any other callers (bench_pictures) will skip calls to write() by default
105 */
106 void enableWrites() { fEnableWrites = true; }
107
108 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000109 * Set the viewport so that only the portion listed gets drawn.
110 */
111 void setViewport(SkISize size) { fViewport = size; }
112
113 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000114 * Set the scale factor at which draw the picture.
115 */
116 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
117
118 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000119 * Perform any setup that should done prior to each iteration of render() which should not be
120 * timed.
121 */
122 virtual void setup() {}
123
124 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000125 * Perform the work. If this is being called within the context of bench_pictures,
126 * this is the step that will be timed.
127 *
128 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
129 * it is recording the source SkPicture into another SkPicture.
130 *
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000131 * If fWritePath has been specified, the result of the work will be written to that dir.
132 * If fMismatchPath has been specified, and the actual image result differs from its
133 * expectation, the result of the work will be written to that dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000134 *
135 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
136 * output image into it, and return it here. (Some subclasses ignore this parameter)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000137 * @return bool True if rendering succeeded and, if fWritePath had been specified, the output
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000138 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000139 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000140 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000141
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000142 /**
143 * Called once finished with a particular SkPicture, before calling init again, and before
144 * being done with this Renderer.
145 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000146 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000147
scroggo@google.comcbcef702012-12-13 22:09:28 +0000148 /**
149 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
150 * TiledPictureRender so its methods can be called.
151 */
152 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
153
scroggo@google.com08085f82013-01-28 20:40:24 +0000154 /**
155 * 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 +0000156 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000157 * @param callFinish Whether to call finish.
158 */
159 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000160
scroggo@google.com0556ea02013-02-08 19:38:21 +0000161 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000162 * Remove all decoded textures from the CPU caches and all uploaded textures
163 * from the GPU.
164 */
165 void purgeTextures();
166
167 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000168 * Set the backend type. Returns true on success and false on failure.
169 */
kkinnunen80549fc2014-06-30 06:36:31 -0700170#if SK_SUPPORT_GPU
171 bool setDeviceType(SkDeviceTypes deviceType, GrGLStandard gpuAPI = kNone_GrGLStandard) {
172#else
scroggo@google.com0556ea02013-02-08 19:38:21 +0000173 bool setDeviceType(SkDeviceTypes deviceType) {
kkinnunen80549fc2014-06-30 06:36:31 -0700174#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000175 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000176#if SK_SUPPORT_GPU
177 // In case this function is called more than once
178 SkSafeUnref(fGrContext);
179 fGrContext = NULL;
180 // Set to Native so it will have an initial value.
181 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
182#endif
183 switch(deviceType) {
184 case kBitmap_DeviceType:
185 return true;
186#if SK_SUPPORT_GPU
187 case kGPU_DeviceType:
188 // Already set to GrContextFactory::kNative_GLContextType, above.
189 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000190 case kNVPR_DeviceType:
191 glContextType = GrContextFactory::kNVPR_GLContextType;
192 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000193#if SK_ANGLE
194 case kAngle_DeviceType:
195 glContextType = GrContextFactory::kANGLE_GLContextType;
196 break;
197#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000198#if SK_MESA
199 case kMesa_DeviceType:
200 glContextType = GrContextFactory::kMESA_GLContextType;
201 break;
202#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000203#endif
204 default:
205 // Invalid device type.
206 return false;
207 }
208#if SK_SUPPORT_GPU
kkinnunen80549fc2014-06-30 06:36:31 -0700209 fGrContext = fGrContextFactory.get(glContextType, gpuAPI);
scroggo@google.com0556ea02013-02-08 19:38:21 +0000210 if (NULL == fGrContext) {
211 return false;
212 } else {
213 fGrContext->ref();
214 return true;
215 }
216#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000217 }
218
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000219#if SK_SUPPORT_GPU
220 void setSampleCount(int sampleCount) {
221 fSampleCount = sampleCount;
222 }
jvanverth4736e142014-11-07 07:12:46 -0800223
224 void setUseDFText(bool useDFText) {
225 fUseDFText = useDFText;
226 }
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000227#endif
228
caryclark@google.come3e940c2012-11-07 16:42:17 +0000229 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
230 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000231 fDrawFiltersConfig = configName;
232 }
233
junov@chromium.org9313ca42012-11-02 18:11:49 +0000234 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
235 fBBoxHierarchyType = bbhType;
236 }
237
junov@chromium.orge286e842013-03-13 17:27:16 +0000238 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
239
junov@chromium.org7b537062012-11-06 18:58:43 +0000240 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000241 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000242 }
243
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000244 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000245 fJsonSummaryPtr = jsonSummaryPtr;
246 }
247
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000248 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000249 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000250 }
251
scroggo@google.com9a412522012-09-07 15:21:18 +0000252 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
253
254 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
255
scroggo@google.com0a049b82012-11-02 22:01:26 +0000256 /**
257 * Reports the configuration of this PictureRenderer.
258 */
259 SkString getConfigName() {
260 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000261 if (!fViewport.isEmpty()) {
262 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
263 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000264 if (fScaleFactor != SK_Scalar1) {
265 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
266 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000267 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
268 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000269 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
270 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700271 config.append("_");
272 config.appendS32(fGridInfo.fTileInterval.width());
273 config.append("x");
274 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000275 }
276#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000277 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000278 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000279 if (fSampleCount) {
280 config.appendf("_msaa%d", fSampleCount);
jvanverth4736e142014-11-07 07:12:46 -0800281 } else if (fUseDFText) {
282 config.append("_gpudft");
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000283 } else {
284 config.append("_gpu");
285 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000286 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000287 case kNVPR_DeviceType:
288 config.appendf("_nvprmsaa%d", fSampleCount);
289 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000290#if SK_ANGLE
291 case kAngle_DeviceType:
292 config.append("_angle");
293 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000294#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000295#if SK_MESA
296 case kMesa_DeviceType:
297 config.append("_mesa");
298 break;
299#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000300 default:
301 // Assume that no extra info means bitmap.
302 break;
303 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000304#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000305 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000306 return config;
307 }
308
kelvinly4d1a3642014-06-26 11:26:40 -0700309 Json::Value getJSONConfig() {
310 Json::Value result;
311
312 result["mode"] = this->getConfigNameInternal().c_str();
313 result["scale"] = 1.0f;
314 if (SK_Scalar1 != fScaleFactor) {
315 result["scale"] = SkScalarToFloat(fScaleFactor);
316 }
317 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
318 result["bbh"] = "rtree";
kelvinly4d1a3642014-06-26 11:26:40 -0700319 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
320 SkString tmp("grid_");
321 tmp.appendS32(fGridInfo.fTileInterval.width());
322 tmp.append("x");
323 tmp.appendS32(fGridInfo.fTileInterval.height());
324 result["bbh"] = tmp.c_str();
325 }
326#if SK_SUPPORT_GPU
327 SkString tmp;
328 switch (fDeviceType) {
329 case kGPU_DeviceType:
330 if (0 != fSampleCount) {
331 tmp = "msaa";
332 tmp.appendS32(fSampleCount);
333 result["config"] = tmp.c_str();
jvanverth4736e142014-11-07 07:12:46 -0800334 } else if (fUseDFText) {
335 result["config"] = "gpudft";
kelvinly4d1a3642014-06-26 11:26:40 -0700336 } else {
337 result["config"] = "gpu";
338 }
339 break;
340 case kNVPR_DeviceType:
341 tmp = "nvprmsaa";
342 tmp.appendS32(fSampleCount);
343 result["config"] = tmp.c_str();
344 break;
345#if SK_ANGLE
346 case kAngle_DeviceType:
347 result["config"] = "angle";
348 break;
349#endif
350#if SK_MESA
351 case kMesa_DeviceType:
352 result["config"] = "mesa";
353 break;
354#endif
355 default:
356 // Assume that no extra info means bitmap.
357 break;
358 }
359#endif
360 return result;
361 }
362
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000363#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000364 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000365 switch (fDeviceType) {
366 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000367 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000368 // fall through
369#if SK_ANGLE
370 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000371 // fall through
372#endif
373#if SK_MESA
374 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000375#endif
376 return true;
377 default:
378 return false;
379 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000380 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000381
kkinnunen9e61bb72014-10-09 05:24:15 -0700382 SkGLContext* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000383 GrContextFactory::GLContextType glContextType
384 = GrContextFactory::kNull_GLContextType;
385 switch(fDeviceType) {
386 case kGPU_DeviceType:
387 glContextType = GrContextFactory::kNative_GLContextType;
388 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000389 case kNVPR_DeviceType:
390 glContextType = GrContextFactory::kNVPR_GLContextType;
391 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000392#if SK_ANGLE
393 case kAngle_DeviceType:
394 glContextType = GrContextFactory::kANGLE_GLContextType;
395 break;
396#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000397#if SK_MESA
398 case kMesa_DeviceType:
399 glContextType = GrContextFactory::kMESA_GLContextType;
400 break;
401#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000402 default:
403 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000404 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000405 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000406 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000407
408 GrContext* getGrContext() {
409 return fGrContext;
410 }
krajcevskib1aded82014-08-18 07:52:17 -0700411
412 const GrContext::Options& getGrContextOptions() {
413 return fGrContextFactory.getGlobalOptions();
414 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000415#endif
416
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000417 SkCanvas* getCanvas() {
418 return fCanvas;
419 }
420
robertphillipsce4dd3d2014-07-07 13:46:35 -0700421 const SkPicture* getPicture() {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000422 return fPicture;
423 }
mtklein2a65a232014-08-26 14:07:04 -0700424
krajcevskib1aded82014-08-18 07:52:17 -0700425#if SK_SUPPORT_GPU
426 explicit PictureRenderer(const GrContext::Options &opts)
427#else
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000428 PictureRenderer()
krajcevskib1aded82014-08-18 07:52:17 -0700429#endif
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000430 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000431 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000432 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000433 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000434 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000435#if SK_SUPPORT_GPU
krajcevskib1aded82014-08-18 07:52:17 -0700436 , fGrContextFactory(opts)
scroggo@google.com0556ea02013-02-08 19:38:21 +0000437 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000438 , fSampleCount(0)
jvanverth4736e142014-11-07 07:12:46 -0800439 , fUseDFText(false)
keyar@chromium.org06125642012-08-20 15:03:33 +0000440#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000441 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000442 fGridInfo.fMargin.setEmpty();
443 fGridInfo.fOffset.setZero();
444 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000445 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000446 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000447 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000448
scroggo@google.com0556ea02013-02-08 19:38:21 +0000449#if SK_SUPPORT_GPU
450 virtual ~PictureRenderer() {
451 SkSafeUnref(fGrContext);
452 }
453#endif
454
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000455protected:
456 SkAutoTUnref<SkCanvas> fCanvas;
robertphillipsce4dd3d2014-07-07 13:46:35 -0700457 SkAutoTUnref<const SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000458 bool fUseChecksumBasedFilenames;
robertphillips78c71272014-10-09 04:59:19 -0700459 bool fUseMultiPictureDraw;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000460 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000461 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000462 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000463 BBoxHierarchyType fBBoxHierarchyType;
464 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
465 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000466 SkString fWritePath;
467 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000468 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000469 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000470
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000471 void buildBBoxHierarchy();
472
473 /**
474 * Return the total width that should be drawn. If the viewport width has been set greater than
475 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
476 */
477 int getViewWidth();
478
479 /**
480 * Return the total height that should be drawn. If the viewport height has been set greater
481 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
482 */
483 int getViewHeight();
484
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000485 /**
486 * Scales the provided canvas to the scale factor set by setScaleFactor.
487 */
488 void scaleToScaleFactor(SkCanvas*);
489
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000490 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700491 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000492 SkCanvas* setupCanvas();
493 virtual SkCanvas* setupCanvas(int width, int height);
494
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000495 /**
496 * Copy src to dest; if src==NULL, set dest to empty string.
497 */
498 static void CopyString(SkString* dest, const SkString* src);
499
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000500private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000501 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000502 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000503#if SK_SUPPORT_GPU
504 GrContextFactory fGrContextFactory;
505 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000506 int fSampleCount;
jvanverth4736e142014-11-07 07:12:46 -0800507 bool fUseDFText;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000508#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000509
scroggo@google.com0a049b82012-11-02 22:01:26 +0000510 virtual SkString getConfigNameInternal() = 0;
511
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000512 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000513};
514
scroggo@google.com9a412522012-09-07 15:21:18 +0000515/**
516 * This class does not do any rendering, but its render function executes recording, which we want
517 * to time.
518 */
519class RecordPictureRenderer : public PictureRenderer {
krajcevskib1aded82014-08-18 07:52:17 -0700520public:
521#if SK_SUPPORT_GPU
522 RecordPictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
523#endif
524
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000525 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000526
527 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
528
529 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000530
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000531protected:
532 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
533
scroggo@google.com0a049b82012-11-02 22:01:26 +0000534private:
535 virtual SkString getConfigNameInternal() SK_OVERRIDE;
krajcevskib1aded82014-08-18 07:52:17 -0700536
537 typedef PictureRenderer INHERITED;
scroggo@google.com9a412522012-09-07 15:21:18 +0000538};
539
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000540class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000541public:
krajcevskib1aded82014-08-18 07:52:17 -0700542#if SK_SUPPORT_GPU
543 PipePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
544#endif
545
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000546 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000547
548private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000549 virtual SkString getConfigNameInternal() SK_OVERRIDE;
550
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000551 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000552};
553
554class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000555public:
krajcevskib1aded82014-08-18 07:52:17 -0700556#if SK_SUPPORT_GPU
557 SimplePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
558#endif
559
robertphillipsce4dd3d2014-07-07 13:46:35 -0700560 virtual void init(const SkPicture* pict,
mtklein2a65a232014-08-26 14:07:04 -0700561 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700562 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -0700563 const SkString* inputFilename,
robertphillips78c71272014-10-09 04:59:19 -0700564 bool useChecksumBasedFilenames,
565 bool useMultiPictureDraw) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000566
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000567 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000568
569private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000570 virtual SkString getConfigNameInternal() SK_OVERRIDE;
571
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000572 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000573};
574
575class TiledPictureRenderer : public PictureRenderer {
576public:
krajcevskib1aded82014-08-18 07:52:17 -0700577#if SK_SUPPORT_GPU
578 TiledPictureRenderer(const GrContext::Options &opts);
579#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000580 TiledPictureRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700581#endif
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000582
mtklein2a65a232014-08-26 14:07:04 -0700583 virtual void init(const SkPicture* pict,
584 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700585 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -0700586 const SkString* inputFilename,
robertphillips78c71272014-10-09 04:59:19 -0700587 bool useChecksumBasedFilenames,
588 bool useMultiPictureDraw) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000589
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000590 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000591 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000592 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000593 * created for each tile, named "path0.png", "path1.png", etc.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000594 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000595 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000596
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000597 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000598
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000599 void setTileWidth(int width) {
600 fTileWidth = width;
601 }
602
603 int getTileWidth() const {
604 return fTileWidth;
605 }
606
607 void setTileHeight(int height) {
608 fTileHeight = height;
609 }
610
611 int getTileHeight() const {
612 return fTileHeight;
613 }
614
615 void setTileWidthPercentage(double percentage) {
616 fTileWidthPercentage = percentage;
617 }
618
keyar@chromium.org163b5672012-08-01 17:53:29 +0000619 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000620 return fTileWidthPercentage;
621 }
622
623 void setTileHeightPercentage(double percentage) {
624 fTileHeightPercentage = percentage;
625 }
626
keyar@chromium.org163b5672012-08-01 17:53:29 +0000627 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000628 return fTileHeightPercentage;
629 }
630
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000631 void setTileMinPowerOf2Width(int width) {
632 SkASSERT(SkIsPow2(width) && width > 0);
633 if (!SkIsPow2(width) || width <= 0) {
634 return;
635 }
636
637 fTileMinPowerOf2Width = width;
638 }
639
640 int getTileMinPowerOf2Width() const {
641 return fTileMinPowerOf2Width;
642 }
643
scroggo@google.comcbcef702012-12-13 22:09:28 +0000644 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
645
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000646 virtual bool supportsTimingIndividualTiles() { return true; }
647
scroggo@google.comcbcef702012-12-13 22:09:28 +0000648 /**
649 * Report the number of tiles in the x and y directions. Must not be called before init.
650 * @param x Output parameter identifying the number of tiles in the x direction.
651 * @param y Output parameter identifying the number of tiles in the y direction.
652 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
653 * unmodified.
654 */
655 bool tileDimensions(int& x, int&y);
656
657 /**
658 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
659 * for the first time.
660 * @param i Output parameter identifying the column of the next tile to be drawn on the next
661 * call to drawNextTile.
662 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
663 * to drawNextTile.
664 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
665 * is within the range of tiles. If false, i and j are unmodified.
666 */
667 bool nextTile(int& i, int& j);
668
669 /**
670 * Render one tile. This will draw the same tile each time it is called until nextTile is
671 * called. The tile rendered will depend on how many calls have been made to nextTile.
672 * It is an error to call this without first calling nextTile, or if nextTile returns false.
673 */
674 void drawCurrentTile();
675
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000676protected:
robertphillips78c71272014-10-09 04:59:19 -0700677 SkTDArray<SkIRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000678
scroggo@google.com0a049b82012-11-02 22:01:26 +0000679 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
680 virtual SkString getConfigNameInternal() SK_OVERRIDE;
681
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000682private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000683 int fTileWidth;
684 int fTileHeight;
685 double fTileWidthPercentage;
686 double fTileHeightPercentage;
687 int fTileMinPowerOf2Width;
688
689 // These variables are only used for timing individual tiles.
690 // Next tile to draw in fTileRects.
691 int fCurrentTileOffset;
692 // Number of tiles in the x direction.
693 int fTilesX;
694 // Number of tiles in the y direction.
695 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000696
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000697 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000698 void setupPowerOf2Tiles();
robertphillips78c71272014-10-09 04:59:19 -0700699 bool postRender(SkCanvas*, const SkIRect& tileRect,
700 SkBitmap* tempBM, SkBitmap** out,
701 int tileNumber);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000702
703 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000704};
705
scroggo@google.com9a412522012-09-07 15:21:18 +0000706/**
707 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
708 * into an SkPicturePlayback, which we want to time.
709 */
710class PlaybackCreationRenderer : public PictureRenderer {
711public:
krajcevskib1aded82014-08-18 07:52:17 -0700712#if SK_SUPPORT_GPU
713 PlaybackCreationRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
714#endif
715
scroggo@google.com9a412522012-09-07 15:21:18 +0000716 virtual void setup() SK_OVERRIDE;
717
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000718 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000719
720 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
721
722 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
723
724private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000725 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000726
727 virtual SkString getConfigNameInternal() SK_OVERRIDE;
728
scroggo@google.com9a412522012-09-07 15:21:18 +0000729 typedef PictureRenderer INHERITED;
730};
731
krajcevskib1aded82014-08-18 07:52:17 -0700732#if SK_SUPPORT_GPU
733extern PictureRenderer* CreateGatherPixelRefsRenderer(const GrContext::Options& opts);
734#else
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000735extern PictureRenderer* CreateGatherPixelRefsRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700736#endif
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000737
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000738}
739
740#endif // PictureRenderer_DEFINED