blob: ca36961b43a4600a2ee3d7a73793736465f39c09 [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
bsalomon682c2692015-05-22 14:01:46 -070031struct GrContextOptions;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000032class SkBitmap;
33class SkCanvas;
kkinnunen9e61bb72014-10-09 05:24:15 -070034class SkGLContext;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000035class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000036
37namespace sk_tools {
38
scroggo@google.comcbcef702012-12-13 22:09:28 +000039class TiledPictureRenderer;
40
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000041class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000042
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000043public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000044 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000045#if SK_ANGLE
46 kAngle_DeviceType,
47#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000048#if SK_MESA
49 kMesa_DeviceType,
50#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000051 kBitmap_DeviceType,
52#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000053 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000054 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000055#endif
56 };
57
junov@chromium.org9313ca42012-11-02 18:11:49 +000058 enum BBoxHierarchyType {
59 kNone_BBoxHierarchyType = 0,
60 kRTree_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000061
mtklein703dd2e2015-01-09 06:41:48 -080062 kLast_BBoxHierarchyType = kRTree_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) {
fmalita50420202015-06-15 11:48:16 -0700230 fHasDrawFilters = false;
caryclark@google.coma3622372012-11-06 21:26:13 +0000231 fDrawFiltersConfig = configName;
fmalita50420202015-06-15 11:48:16 -0700232
233 for (size_t i = 0; i < SK_ARRAY_COUNT(fDrawFilters); ++i) {
234 fDrawFilters[i] = filters[i];
235 fHasDrawFilters |= SkToBool(filters[i]);
236 }
caryclark@google.coma3622372012-11-06 21:26:13 +0000237 }
238
junov@chromium.org9313ca42012-11-02 18:11:49 +0000239 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
240 fBBoxHierarchyType = bbhType;
241 }
242
junov@chromium.orge286e842013-03-13 17:27:16 +0000243 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
244
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000245 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000246 fJsonSummaryPtr = jsonSummaryPtr;
247 }
248
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000249 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000250 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000251 }
252
scroggo@google.com9a412522012-09-07 15:21:18 +0000253 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
254
255 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
256
scroggo@google.com0a049b82012-11-02 22:01:26 +0000257 /**
258 * Reports the configuration of this PictureRenderer.
259 */
260 SkString getConfigName() {
261 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000262 if (!fViewport.isEmpty()) {
263 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
264 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000265 if (fScaleFactor != SK_Scalar1) {
266 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
267 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000268 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
269 config.append("_rtree");
270 }
271#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000272 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000273 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000274 if (fSampleCount) {
275 config.appendf("_msaa%d", fSampleCount);
jvanverth4736e142014-11-07 07:12:46 -0800276 } else if (fUseDFText) {
277 config.append("_gpudft");
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000278 } 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";
kelvinly4d1a3642014-06-26 11:26:40 -0700314 }
315#if SK_SUPPORT_GPU
316 SkString tmp;
317 switch (fDeviceType) {
318 case kGPU_DeviceType:
319 if (0 != fSampleCount) {
320 tmp = "msaa";
321 tmp.appendS32(fSampleCount);
322 result["config"] = tmp.c_str();
jvanverth4736e142014-11-07 07:12:46 -0800323 } else if (fUseDFText) {
324 result["config"] = "gpudft";
kelvinly4d1a3642014-06-26 11:26:40 -0700325 } else {
326 result["config"] = "gpu";
327 }
328 break;
329 case kNVPR_DeviceType:
330 tmp = "nvprmsaa";
331 tmp.appendS32(fSampleCount);
332 result["config"] = tmp.c_str();
333 break;
334#if SK_ANGLE
335 case kAngle_DeviceType:
336 result["config"] = "angle";
337 break;
338#endif
339#if SK_MESA
340 case kMesa_DeviceType:
341 result["config"] = "mesa";
342 break;
343#endif
344 default:
345 // Assume that no extra info means bitmap.
346 break;
347 }
348#endif
349 return result;
350 }
351
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000352#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000353 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000354 switch (fDeviceType) {
355 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000356 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000357 // fall through
358#if SK_ANGLE
359 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000360 // fall through
361#endif
362#if SK_MESA
363 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000364#endif
365 return true;
366 default:
367 return false;
368 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000369 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000370
kkinnunen9e61bb72014-10-09 05:24:15 -0700371 SkGLContext* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000372 GrContextFactory::GLContextType glContextType
373 = GrContextFactory::kNull_GLContextType;
374 switch(fDeviceType) {
375 case kGPU_DeviceType:
376 glContextType = GrContextFactory::kNative_GLContextType;
377 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000378 case kNVPR_DeviceType:
379 glContextType = GrContextFactory::kNVPR_GLContextType;
380 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000381#if SK_ANGLE
382 case kAngle_DeviceType:
383 glContextType = GrContextFactory::kANGLE_GLContextType;
384 break;
385#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000386#if SK_MESA
387 case kMesa_DeviceType:
388 glContextType = GrContextFactory::kMESA_GLContextType;
389 break;
390#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000391 default:
392 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000393 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000394 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000395 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000396
397 GrContext* getGrContext() {
398 return fGrContext;
399 }
krajcevskib1aded82014-08-18 07:52:17 -0700400
bsalomon682c2692015-05-22 14:01:46 -0700401 const GrContextOptions& getGrContextOptions() {
krajcevskib1aded82014-08-18 07:52:17 -0700402 return fGrContextFactory.getGlobalOptions();
403 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000404#endif
405
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000406 SkCanvas* getCanvas() {
407 return fCanvas;
408 }
409
robertphillipsce4dd3d2014-07-07 13:46:35 -0700410 const SkPicture* getPicture() {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000411 return fPicture;
412 }
mtklein2a65a232014-08-26 14:07:04 -0700413
krajcevskib1aded82014-08-18 07:52:17 -0700414#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700415 explicit PictureRenderer(const GrContextOptions &opts)
krajcevskib1aded82014-08-18 07:52:17 -0700416#else
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000417 PictureRenderer()
krajcevskib1aded82014-08-18 07:52:17 -0700418#endif
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000419 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000420 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000421 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000422 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
fmalita50420202015-06-15 11:48:16 -0700423 , fHasDrawFilters(false)
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)
jvanverth4736e142014-11-07 07:12:46 -0800429 , fUseDFText(false)
keyar@chromium.org06125642012-08-20 15:03:33 +0000430#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000431 {
432 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000433 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000434 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000435
scroggo@google.com0556ea02013-02-08 19:38:21 +0000436#if SK_SUPPORT_GPU
437 virtual ~PictureRenderer() {
438 SkSafeUnref(fGrContext);
439 }
440#endif
441
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000442protected:
443 SkAutoTUnref<SkCanvas> fCanvas;
robertphillipsce4dd3d2014-07-07 13:46:35 -0700444 SkAutoTUnref<const SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000445 bool fUseChecksumBasedFilenames;
robertphillips78c71272014-10-09 04:59:19 -0700446 bool fUseMultiPictureDraw;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000447 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000448 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000449 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000450 BBoxHierarchyType fBBoxHierarchyType;
fmalita50420202015-06-15 11:48:16 -0700451 bool fHasDrawFilters;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000452 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;
keyar@chromium.org06125642012-08-20 15:03:33 +0000457
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000458 void buildBBoxHierarchy();
459
460 /**
461 * Return the total width that should be drawn. If the viewport width has been set greater than
462 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
463 */
464 int getViewWidth();
465
466 /**
467 * Return the total height that should be drawn. If the viewport height has been set greater
468 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
469 */
470 int getViewHeight();
471
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000472 /**
473 * Scales the provided canvas to the scale factor set by setScaleFactor.
474 */
475 void scaleToScaleFactor(SkCanvas*);
476
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000477 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700478 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000479 SkCanvas* setupCanvas();
480 virtual SkCanvas* setupCanvas(int width, int height);
481
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000482 /**
483 * Copy src to dest; if src==NULL, set dest to empty string.
484 */
485 static void CopyString(SkString* dest, const SkString* src);
486
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000487private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000488 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000489 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000490#if SK_SUPPORT_GPU
491 GrContextFactory fGrContextFactory;
492 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000493 int fSampleCount;
jvanverth4736e142014-11-07 07:12:46 -0800494 bool fUseDFText;
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
bsalomon682c2692015-05-22 14:01:46 -0700509 RecordPictureRenderer(const GrContextOptions &opts) : INHERITED(opts) { }
krajcevskib1aded82014-08-18 07:52:17 -0700510#endif
511
mtklein36352bf2015-03-25 18:17:31 -0700512 bool render(SkBitmap** out = NULL) override;
scroggo@google.com9a412522012-09-07 15:21:18 +0000513
mtklein36352bf2015-03-25 18:17:31 -0700514 SkString getPerIterTimeFormat() override { return SkString("%.4f"); }
scroggo@google.com9a412522012-09-07 15:21:18 +0000515
mtklein36352bf2015-03-25 18:17:31 -0700516 SkString getNormalTimeFormat() override { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000517
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000518protected:
mtklein36352bf2015-03-25 18:17:31 -0700519 SkCanvas* setupCanvas(int width, int height) override;
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000520
scroggo@google.com0a049b82012-11-02 22:01:26 +0000521private:
mtklein36352bf2015-03-25 18:17:31 -0700522 SkString getConfigNameInternal() 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
bsalomon682c2692015-05-22 14:01:46 -0700530 PipePictureRenderer(const GrContextOptions &opts) : INHERITED(opts) { }
krajcevskib1aded82014-08-18 07:52:17 -0700531#endif
532
mtklein36352bf2015-03-25 18:17:31 -0700533 bool render(SkBitmap** out = NULL) override;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000534
535private:
mtklein36352bf2015-03-25 18:17:31 -0700536 SkString getConfigNameInternal() override;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000537
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
bsalomon682c2692015-05-22 14:01:46 -0700544 SimplePictureRenderer(const GrContextOptions &opts) : INHERITED(opts) { }
krajcevskib1aded82014-08-18 07:52:17 -0700545#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,
robertphillips78c71272014-10-09 04:59:19 -0700551 bool useChecksumBasedFilenames,
mtklein36352bf2015-03-25 18:17:31 -0700552 bool useMultiPictureDraw) override;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000553
mtklein36352bf2015-03-25 18:17:31 -0700554 bool render(SkBitmap** out = NULL) override;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000555
556private:
mtklein36352bf2015-03-25 18:17:31 -0700557 SkString getConfigNameInternal() override;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000558
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000559 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000560};
561
562class TiledPictureRenderer : public PictureRenderer {
563public:
krajcevskib1aded82014-08-18 07:52:17 -0700564#if SK_SUPPORT_GPU
bsalomon682c2692015-05-22 14:01:46 -0700565 TiledPictureRenderer(const GrContextOptions &opts);
krajcevskib1aded82014-08-18 07:52:17 -0700566#else
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000567 TiledPictureRenderer();
krajcevskib1aded82014-08-18 07:52:17 -0700568#endif
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000569
mtklein2a65a232014-08-26 14:07:04 -0700570 virtual void init(const SkPicture* pict,
571 const SkString* writePath,
robertphillipsce4dd3d2014-07-07 13:46:35 -0700572 const SkString* mismatchPath,
mtklein2a65a232014-08-26 14:07:04 -0700573 const SkString* inputFilename,
robertphillips78c71272014-10-09 04:59:19 -0700574 bool useChecksumBasedFilenames,
mtklein36352bf2015-03-25 18:17:31 -0700575 bool useMultiPictureDraw) override;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000576
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000577 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000578 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000579 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000580 * created for each tile, named "path0.png", "path1.png", etc.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000581 */
mtklein36352bf2015-03-25 18:17:31 -0700582 bool render(SkBitmap** out = NULL) override;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000583
mtklein36352bf2015-03-25 18:17:31 -0700584 void end() override;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000585
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000586 void setTileWidth(int width) {
587 fTileWidth = width;
588 }
589
590 int getTileWidth() const {
591 return fTileWidth;
592 }
593
594 void setTileHeight(int height) {
595 fTileHeight = height;
596 }
597
598 int getTileHeight() const {
599 return fTileHeight;
600 }
601
602 void setTileWidthPercentage(double percentage) {
603 fTileWidthPercentage = percentage;
604 }
605
keyar@chromium.org163b5672012-08-01 17:53:29 +0000606 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000607 return fTileWidthPercentage;
608 }
609
610 void setTileHeightPercentage(double percentage) {
611 fTileHeightPercentage = percentage;
612 }
613
keyar@chromium.org163b5672012-08-01 17:53:29 +0000614 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000615 return fTileHeightPercentage;
616 }
617
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000618 void setTileMinPowerOf2Width(int width) {
619 SkASSERT(SkIsPow2(width) && width > 0);
620 if (!SkIsPow2(width) || width <= 0) {
621 return;
622 }
623
624 fTileMinPowerOf2Width = width;
625 }
626
627 int getTileMinPowerOf2Width() const {
628 return fTileMinPowerOf2Width;
629 }
630
mtklein36352bf2015-03-25 18:17:31 -0700631 TiledPictureRenderer* getTiledRenderer() override { return this; }
scroggo@google.comcbcef702012-12-13 22:09:28 +0000632
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000633 virtual bool supportsTimingIndividualTiles() { return true; }
634
scroggo@google.comcbcef702012-12-13 22:09:28 +0000635 /**
636 * Report the number of tiles in the x and y directions. Must not be called before init.
637 * @param x Output parameter identifying the number of tiles in the x direction.
638 * @param y Output parameter identifying the number of tiles in the y direction.
639 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
640 * unmodified.
641 */
642 bool tileDimensions(int& x, int&y);
643
644 /**
645 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
646 * for the first time.
647 * @param i Output parameter identifying the column of the next tile to be drawn on the next
648 * call to drawNextTile.
649 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
650 * to drawNextTile.
651 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
652 * is within the range of tiles. If false, i and j are unmodified.
653 */
654 bool nextTile(int& i, int& j);
655
656 /**
657 * Render one tile. This will draw the same tile each time it is called until nextTile is
658 * called. The tile rendered will depend on how many calls have been made to nextTile.
659 * It is an error to call this without first calling nextTile, or if nextTile returns false.
660 */
661 void drawCurrentTile();
662
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000663protected:
robertphillips78c71272014-10-09 04:59:19 -0700664 SkTDArray<SkIRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000665
mtklein36352bf2015-03-25 18:17:31 -0700666 SkCanvas* setupCanvas(int width, int height) override;
667 SkString getConfigNameInternal() override;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000668
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000669private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000670 int fTileWidth;
671 int fTileHeight;
672 double fTileWidthPercentage;
673 double fTileHeightPercentage;
674 int fTileMinPowerOf2Width;
675
676 // These variables are only used for timing individual tiles.
677 // Next tile to draw in fTileRects.
678 int fCurrentTileOffset;
679 // Number of tiles in the x direction.
680 int fTilesX;
681 // Number of tiles in the y direction.
682 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000683
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000684 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000685 void setupPowerOf2Tiles();
mtklein703dd2e2015-01-09 06:41:48 -0800686 bool postRender(SkCanvas*, const SkIRect& tileRect,
robertphillips78c71272014-10-09 04:59:19 -0700687 SkBitmap* tempBM, SkBitmap** out,
688 int tileNumber);
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
bsalomon682c2692015-05-22 14:01:46 -0700700 PlaybackCreationRenderer(const GrContextOptions &opts) : INHERITED(opts) { }
krajcevskib1aded82014-08-18 07:52:17 -0700701#endif
702
mtklein36352bf2015-03-25 18:17:31 -0700703 void setup() override;
scroggo@google.com9a412522012-09-07 15:21:18 +0000704
mtklein36352bf2015-03-25 18:17:31 -0700705 bool render(SkBitmap** out = NULL) override;
scroggo@google.com9a412522012-09-07 15:21:18 +0000706
mtklein36352bf2015-03-25 18:17:31 -0700707 SkString getPerIterTimeFormat() override { return SkString("%.4f"); }
scroggo@google.com9a412522012-09-07 15:21:18 +0000708
mtklein36352bf2015-03-25 18:17:31 -0700709 SkString getNormalTimeFormat() override { return SkString("%6.4f"); }
scroggo@google.com9a412522012-09-07 15:21:18 +0000710
711private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000712 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000713
mtklein36352bf2015-03-25 18:17:31 -0700714 SkString getConfigNameInternal() override;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000715
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
bsalomon682c2692015-05-22 14:01:46 -0700720extern PictureRenderer* CreateGatherPixelRefsRenderer(const GrContextOptions& opts);
krajcevskib1aded82014-08-18 07:52:17 -0700721#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