blob: 04ac20fcb609e0acf16a12961daef83173a0e57d [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;
bsalomon10805962014-10-08 04:45:09 -070033class 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,
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.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000091 */
mtklein2a65a232014-08-26 14:07:04 -070092 virtual void init(const SkPicture* pict,
93 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -070094 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -070095 const SkString* inputFilename,
robertphillipsce4dd3d2014-07-07 13:46:35 -070096 bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +000097
98 /**
commit-bot@chromium.org8991c672014-05-22 00:36:05 +000099 * TODO(epoger): Temporary hack, while we work on http://skbug.com/2584 ('bench_pictures is
100 * timing reading pixels and writing json files'), such that:
101 * - render_pictures can call this method and continue to work
102 * - any other callers (bench_pictures) will skip calls to write() by default
103 */
104 void enableWrites() { fEnableWrites = true; }
105
106 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000107 * Set the viewport so that only the portion listed gets drawn.
108 */
109 void setViewport(SkISize size) { fViewport = size; }
110
111 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000112 * Set the scale factor at which draw the picture.
113 */
114 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
115
116 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000117 * Perform any setup that should done prior to each iteration of render() which should not be
118 * timed.
119 */
120 virtual void setup() {}
121
122 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000123 * Perform the work. If this is being called within the context of bench_pictures,
124 * this is the step that will be timed.
125 *
126 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
127 * it is recording the source SkPicture into another SkPicture.
128 *
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000129 * If fWritePath has been specified, the result of the work will be written to that dir.
130 * If fMismatchPath has been specified, and the actual image result differs from its
131 * expectation, the result of the work will be written to that dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000132 *
133 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
134 * output image into it, and return it here. (Some subclasses ignore this parameter)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000135 * @return bool True if rendering succeeded and, if fWritePath had been specified, the output
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000136 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000137 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000138 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000139
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000140 /**
141 * Called once finished with a particular SkPicture, before calling init again, and before
142 * being done with this Renderer.
143 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000144 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000145
scroggo@google.comcbcef702012-12-13 22:09:28 +0000146 /**
147 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
148 * TiledPictureRender so its methods can be called.
149 */
150 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
151
scroggo@google.com08085f82013-01-28 20:40:24 +0000152 /**
153 * 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 +0000154 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000155 * @param callFinish Whether to call finish.
156 */
157 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000158
scroggo@google.com0556ea02013-02-08 19:38:21 +0000159 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000160 * Remove all decoded textures from the CPU caches and all uploaded textures
161 * from the GPU.
162 */
163 void purgeTextures();
164
165 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000166 * Set the backend type. Returns true on success and false on failure.
167 */
kkinnunen80549fc2014-06-30 06:36:31 -0700168#if SK_SUPPORT_GPU
169 bool setDeviceType(SkDeviceTypes deviceType, GrGLStandard gpuAPI = kNone_GrGLStandard) {
170#else
scroggo@google.com0556ea02013-02-08 19:38:21 +0000171 bool setDeviceType(SkDeviceTypes deviceType) {
kkinnunen80549fc2014-06-30 06:36:31 -0700172#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000173 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000174#if SK_SUPPORT_GPU
175 // In case this function is called more than once
176 SkSafeUnref(fGrContext);
177 fGrContext = NULL;
178 // Set to Native so it will have an initial value.
179 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
180#endif
181 switch(deviceType) {
182 case kBitmap_DeviceType:
183 return true;
184#if SK_SUPPORT_GPU
185 case kGPU_DeviceType:
186 // Already set to GrContextFactory::kNative_GLContextType, above.
187 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000188 case kNVPR_DeviceType:
189 glContextType = GrContextFactory::kNVPR_GLContextType;
190 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000191#if SK_ANGLE
192 case kAngle_DeviceType:
193 glContextType = GrContextFactory::kANGLE_GLContextType;
194 break;
195#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000196#if SK_MESA
197 case kMesa_DeviceType:
198 glContextType = GrContextFactory::kMESA_GLContextType;
199 break;
200#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000201#endif
202 default:
203 // Invalid device type.
204 return false;
205 }
206#if SK_SUPPORT_GPU
kkinnunen80549fc2014-06-30 06:36:31 -0700207 fGrContext = fGrContextFactory.get(glContextType, gpuAPI);
scroggo@google.com0556ea02013-02-08 19:38:21 +0000208 if (NULL == fGrContext) {
209 return false;
210 } else {
211 fGrContext->ref();
212 return true;
213 }
214#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000215 }
216
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000217#if SK_SUPPORT_GPU
218 void setSampleCount(int sampleCount) {
219 fSampleCount = sampleCount;
220 }
221#endif
222
caryclark@google.come3e940c2012-11-07 16:42:17 +0000223 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
224 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000225 fDrawFiltersConfig = configName;
226 }
227
junov@chromium.org9313ca42012-11-02 18:11:49 +0000228 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
229 fBBoxHierarchyType = bbhType;
230 }
231
junov@chromium.orge286e842013-03-13 17:27:16 +0000232 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
233
junov@chromium.org7b537062012-11-06 18:58:43 +0000234 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000235 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000236 }
237
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000238 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000239 fJsonSummaryPtr = jsonSummaryPtr;
240 }
241
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000242 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000243 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000244 }
245
scroggo@google.com9a412522012-09-07 15:21:18 +0000246 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
247
248 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
249
scroggo@google.com0a049b82012-11-02 22:01:26 +0000250 /**
251 * Reports the configuration of this PictureRenderer.
252 */
253 SkString getConfigName() {
254 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000255 if (!fViewport.isEmpty()) {
256 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
257 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000258 if (fScaleFactor != SK_Scalar1) {
259 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
260 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000261 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
262 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000263 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
264 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700265 config.append("_");
266 config.appendS32(fGridInfo.fTileInterval.width());
267 config.append("x");
268 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000269 }
270#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000271 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000272 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000273 if (fSampleCount) {
274 config.appendf("_msaa%d", fSampleCount);
275 } else {
276 config.append("_gpu");
277 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000278 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000279 case kNVPR_DeviceType:
280 config.appendf("_nvprmsaa%d", fSampleCount);
281 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000282#if SK_ANGLE
283 case kAngle_DeviceType:
284 config.append("_angle");
285 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000286#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000287#if SK_MESA
288 case kMesa_DeviceType:
289 config.append("_mesa");
290 break;
291#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000292 default:
293 // Assume that no extra info means bitmap.
294 break;
295 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000296#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000297 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000298 return config;
299 }
300
kelvinly4d1a3642014-06-26 11:26:40 -0700301 Json::Value getJSONConfig() {
302 Json::Value result;
303
304 result["mode"] = this->getConfigNameInternal().c_str();
305 result["scale"] = 1.0f;
306 if (SK_Scalar1 != fScaleFactor) {
307 result["scale"] = SkScalarToFloat(fScaleFactor);
308 }
309 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
310 result["bbh"] = "rtree";
kelvinly4d1a3642014-06-26 11:26:40 -0700311 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
312 SkString tmp("grid_");
313 tmp.appendS32(fGridInfo.fTileInterval.width());
314 tmp.append("x");
315 tmp.appendS32(fGridInfo.fTileInterval.height());
316 result["bbh"] = tmp.c_str();
317 }
318#if SK_SUPPORT_GPU
319 SkString tmp;
320 switch (fDeviceType) {
321 case kGPU_DeviceType:
322 if (0 != fSampleCount) {
323 tmp = "msaa";
324 tmp.appendS32(fSampleCount);
325 result["config"] = tmp.c_str();
326 } else {
327 result["config"] = "gpu";
328 }
329 break;
330 case kNVPR_DeviceType:
331 tmp = "nvprmsaa";
332 tmp.appendS32(fSampleCount);
333 result["config"] = tmp.c_str();
334 break;
335#if SK_ANGLE
336 case kAngle_DeviceType:
337 result["config"] = "angle";
338 break;
339#endif
340#if SK_MESA
341 case kMesa_DeviceType:
342 result["config"] = "mesa";
343 break;
344#endif
345 default:
346 // Assume that no extra info means bitmap.
347 break;
348 }
349#endif
350 return result;
351 }
352
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000353#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000354 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000355 switch (fDeviceType) {
356 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000357 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000358 // fall through
359#if SK_ANGLE
360 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000361 // fall through
362#endif
363#if SK_MESA
364 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000365#endif
366 return true;
367 default:
368 return false;
369 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000370 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000371
bsalomon10805962014-10-08 04:45:09 -0700372 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000373 GrContextFactory::GLContextType glContextType
374 = GrContextFactory::kNull_GLContextType;
375 switch(fDeviceType) {
376 case kGPU_DeviceType:
377 glContextType = GrContextFactory::kNative_GLContextType;
378 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000379 case kNVPR_DeviceType:
380 glContextType = GrContextFactory::kNVPR_GLContextType;
381 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000382#if SK_ANGLE
383 case kAngle_DeviceType:
384 glContextType = GrContextFactory::kANGLE_GLContextType;
385 break;
386#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000387#if SK_MESA
388 case kMesa_DeviceType:
389 glContextType = GrContextFactory::kMESA_GLContextType;
390 break;
391#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000392 default:
393 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000394 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000395 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000396 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000397
398 GrContext* getGrContext() {
399 return fGrContext;
400 }
krajcevskib1aded82014-08-18 07:52:17 -0700401
402 const GrContext::Options& getGrContextOptions() {
403 return fGrContextFactory.getGlobalOptions();
404 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000405#endif
406
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000407 SkCanvas* getCanvas() {
408 return fCanvas;
409 }
410
robertphillipsce4dd3d2014-07-07 13:46:35 -0700411 const SkPicture* getPicture() {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000412 return fPicture;
413 }
mtklein2a65a232014-08-26 14:07:04 -0700414
krajcevskib1aded82014-08-18 07:52:17 -0700415#if SK_SUPPORT_GPU
416 explicit PictureRenderer(const GrContext::Options &opts)
417#else
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000418 PictureRenderer()
krajcevskib1aded82014-08-18 07:52:17 -0700419#endif
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000420 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000421 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000422 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000423 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000424 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000425#if SK_SUPPORT_GPU
krajcevskib1aded82014-08-18 07:52:17 -0700426 , fGrContextFactory(opts)
scroggo@google.com0556ea02013-02-08 19:38:21 +0000427 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000428 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000429#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000430 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000431 fGridInfo.fMargin.setEmpty();
432 fGridInfo.fOffset.setZero();
433 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000434 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000435 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000436 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000437
scroggo@google.com0556ea02013-02-08 19:38:21 +0000438#if SK_SUPPORT_GPU
439 virtual ~PictureRenderer() {
440 SkSafeUnref(fGrContext);
441 }
442#endif
443
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000444protected:
445 SkAutoTUnref<SkCanvas> fCanvas;
robertphillipsce4dd3d2014-07-07 13:46:35 -0700446 SkAutoTUnref<const SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000447 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000448 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000449 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000450 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000451 BBoxHierarchyType fBBoxHierarchyType;
452 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
453 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000454 SkString fWritePath;
455 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000456 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000457 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000458
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000459 void buildBBoxHierarchy();
460
461 /**
462 * Return the total width that should be drawn. If the viewport width has been set greater than
463 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
464 */
465 int getViewWidth();
466
467 /**
468 * Return the total height that should be drawn. If the viewport height has been set greater
469 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
470 */
471 int getViewHeight();
472
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000473 /**
474 * Scales the provided canvas to the scale factor set by setScaleFactor.
475 */
476 void scaleToScaleFactor(SkCanvas*);
477
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000478 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700479 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000480 SkCanvas* setupCanvas();
481 virtual SkCanvas* setupCanvas(int width, int height);
482
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000483 /**
484 * Copy src to dest; if src==NULL, set dest to empty string.
485 */
486 static void CopyString(SkString* dest, const SkString* src);
487
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000488private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000489 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000490 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000491#if SK_SUPPORT_GPU
492 GrContextFactory fGrContextFactory;
493 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000494 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000495#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000496
scroggo@google.com0a049b82012-11-02 22:01:26 +0000497 virtual SkString getConfigNameInternal() = 0;
498
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000499 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000500};
501
scroggo@google.com9a412522012-09-07 15:21:18 +0000502/**
503 * This class does not do any rendering, but its render function executes recording, which we want
504 * to time.
505 */
506class RecordPictureRenderer : public PictureRenderer {
krajcevskib1aded82014-08-18 07:52:17 -0700507public:
508#if SK_SUPPORT_GPU
509 RecordPictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
510#endif
511
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000512 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000513
514 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
515
516 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000517
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000518protected:
519 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
520
scroggo@google.com0a049b82012-11-02 22:01:26 +0000521private:
522 virtual SkString getConfigNameInternal() SK_OVERRIDE;
krajcevskib1aded82014-08-18 07:52:17 -0700523
524 typedef PictureRenderer INHERITED;
scroggo@google.com9a412522012-09-07 15:21:18 +0000525};
526
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000527class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000528public:
krajcevskib1aded82014-08-18 07:52:17 -0700529#if SK_SUPPORT_GPU
530 PipePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
531#endif
532
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000533 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000534
535private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000536 virtual SkString getConfigNameInternal() SK_OVERRIDE;
537
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000538 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000539};
540
541class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000542public:
krajcevskib1aded82014-08-18 07:52:17 -0700543#if SK_SUPPORT_GPU
544 SimplePictureRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
545#endif
546
robertphillipsce4dd3d2014-07-07 13:46:35 -0700547 virtual void init(const SkPicture* pict,
mtklein2a65a232014-08-26 14:07:04 -0700548 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700549 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -0700550 const SkString* inputFilename,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700551 bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000552
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000553 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000554
555private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000556 virtual SkString getConfigNameInternal() SK_OVERRIDE;
557
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000558 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000559};
560
561class TiledPictureRenderer : public PictureRenderer {
562public:
krajcevskib1aded82014-08-18 07:52:17 -0700563#if SK_SUPPORT_GPU
564 TiledPictureRenderer(const GrContext::Options &opts);
565#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000566 TiledPictureRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700567#endif
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000568
mtklein2a65a232014-08-26 14:07:04 -0700569 virtual void init(const SkPicture* pict,
570 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700571 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -0700572 const SkString* inputFilename,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700573 bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000574
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000575 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000576 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000577 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000578 * created for each tile, named "path0.png", "path1.png", etc.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000579 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000580 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000581
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000582 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000583
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000584 void setTileWidth(int width) {
585 fTileWidth = width;
586 }
587
588 int getTileWidth() const {
589 return fTileWidth;
590 }
591
592 void setTileHeight(int height) {
593 fTileHeight = height;
594 }
595
596 int getTileHeight() const {
597 return fTileHeight;
598 }
599
600 void setTileWidthPercentage(double percentage) {
601 fTileWidthPercentage = percentage;
602 }
603
keyar@chromium.org163b5672012-08-01 17:53:29 +0000604 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000605 return fTileWidthPercentage;
606 }
607
608 void setTileHeightPercentage(double percentage) {
609 fTileHeightPercentage = percentage;
610 }
611
keyar@chromium.org163b5672012-08-01 17:53:29 +0000612 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000613 return fTileHeightPercentage;
614 }
615
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000616 void setTileMinPowerOf2Width(int width) {
617 SkASSERT(SkIsPow2(width) && width > 0);
618 if (!SkIsPow2(width) || width <= 0) {
619 return;
620 }
621
622 fTileMinPowerOf2Width = width;
623 }
624
625 int getTileMinPowerOf2Width() const {
626 return fTileMinPowerOf2Width;
627 }
628
scroggo@google.comcbcef702012-12-13 22:09:28 +0000629 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
630
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000631 virtual bool supportsTimingIndividualTiles() { return true; }
632
scroggo@google.comcbcef702012-12-13 22:09:28 +0000633 /**
634 * Report the number of tiles in the x and y directions. Must not be called before init.
635 * @param x Output parameter identifying the number of tiles in the x direction.
636 * @param y Output parameter identifying the number of tiles in the y direction.
637 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
638 * unmodified.
639 */
640 bool tileDimensions(int& x, int&y);
641
642 /**
643 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
644 * for the first time.
645 * @param i Output parameter identifying the column of the next tile to be drawn on the next
646 * call to drawNextTile.
647 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
648 * to drawNextTile.
649 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
650 * is within the range of tiles. If false, i and j are unmodified.
651 */
652 bool nextTile(int& i, int& j);
653
654 /**
655 * Render one tile. This will draw the same tile each time it is called until nextTile is
656 * called. The tile rendered will depend on how many calls have been made to nextTile.
657 * It is an error to call this without first calling nextTile, or if nextTile returns false.
658 */
659 void drawCurrentTile();
660
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000661protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000662 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000663
scroggo@google.com0a049b82012-11-02 22:01:26 +0000664 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
665 virtual SkString getConfigNameInternal() SK_OVERRIDE;
666
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000667private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000668 int fTileWidth;
669 int fTileHeight;
670 double fTileWidthPercentage;
671 double fTileHeightPercentage;
672 int fTileMinPowerOf2Width;
673
674 // These variables are only used for timing individual tiles.
675 // Next tile to draw in fTileRects.
676 int fCurrentTileOffset;
677 // Number of tiles in the x direction.
678 int fTilesX;
679 // Number of tiles in the y direction.
680 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000681
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000682 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000683 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000684
685 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000686};
687
scroggo@google.com9a412522012-09-07 15:21:18 +0000688/**
689 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
690 * into an SkPicturePlayback, which we want to time.
691 */
692class PlaybackCreationRenderer : public PictureRenderer {
693public:
krajcevskib1aded82014-08-18 07:52:17 -0700694#if SK_SUPPORT_GPU
695 PlaybackCreationRenderer(const GrContext::Options &opts) : INHERITED(opts) { }
696#endif
697
scroggo@google.com9a412522012-09-07 15:21:18 +0000698 virtual void setup() SK_OVERRIDE;
699
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000700 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000701
702 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
703
704 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
705
706private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000707 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000708
709 virtual SkString getConfigNameInternal() SK_OVERRIDE;
710
scroggo@google.com9a412522012-09-07 15:21:18 +0000711 typedef PictureRenderer INHERITED;
712};
713
krajcevskib1aded82014-08-18 07:52:17 -0700714#if SK_SUPPORT_GPU
715extern PictureRenderer* CreateGatherPixelRefsRenderer(const GrContext::Options& opts);
716#else
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000717extern PictureRenderer* CreateGatherPixelRefsRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700718#endif
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000719
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000720}
721
722#endif // PictureRenderer_DEFINED