blob: b4e08aafeba704d362da6523de107cc2e8040649 [file] [log] [blame]
caryclark@google.com411bb722012-11-06 21:29:16 +00001/*
keyar@chromium.org451bb9f2012-07-26 17:27:57 +00002 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef PictureRenderer_DEFINED
9#define PictureRenderer_DEFINED
scroggo@google.coma62da2f2012-11-02 21:28:12 +000010
scroggo@google.com161e1ba2013-03-04 16:41:06 +000011#include "SkCanvas.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000012#include "SkCountdown.h"
caryclark@google.coma3622372012-11-06 21:26:13 +000013#include "SkDrawFilter.h"
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.coma62da2f2012-11-02 21:28:12 +000020#include "SkRunnable.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000021#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000022#include "SkTDArray.h"
23#include "SkThreadPool.h"
24#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000025
keyar@chromium.org06125642012-08-20 15:03:33 +000026#if SK_SUPPORT_GPU
27#include "GrContextFactory.h"
28#include "GrContext.h"
29#endif
30
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000031#include "image_expectations.h"
32
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000033class SkBitmap;
34class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000035class SkGLContextHelper;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000036class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000037
38namespace sk_tools {
39
scroggo@google.comcbcef702012-12-13 22:09:28 +000040class TiledPictureRenderer;
41
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000042class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000043
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000044public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000045 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000046#if SK_ANGLE
47 kAngle_DeviceType,
48#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000049#if SK_MESA
50 kMesa_DeviceType,
51#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000052 kBitmap_DeviceType,
53#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000054 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000055 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000056#endif
57 };
58
junov@chromium.org9313ca42012-11-02 18:11:49 +000059 enum BBoxHierarchyType {
60 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000061 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000062 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000063 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000064
65 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000066 };
67
caryclark@google.coma3622372012-11-06 21:26:13 +000068 // this uses SkPaint::Flags as a base and adds additional flags
69 enum DrawFilterFlags {
70 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000071 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
72 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
73 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000074 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000075 };
76
robertphillips@google.com49149312013-07-03 15:34:35 +000077 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000078 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
79 hinting_flag_must_be_greater);
80 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
81 slight_hinting_flag_must_be_greater);
82
scroggo@google.coma62da2f2012-11-02 21:28:12 +000083 /**
84 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000085 *
86 * @param pict The SkPicture to render.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000087 * @param writePath The output directory within which this renderer should write all images,
88 * or NULL if this renderer should not write all images.
89 * @param mismatchPath The output directory within which this renderer should write any images
90 * which do not match expectations, or NULL if this renderer should not write mismatches.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000091 * @param inputFilename The name of the input file we are rendering.
92 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
93 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000094 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000095 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000096 const SkString* inputFilename, 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 */
kkinnunen74fc7272014-06-22 22:56:53 -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) {
kkinnunen74fc7272014-06-22 22:56:53 -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
kkinnunen74fc7272014-06-22 22:56:53 -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");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000263 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
264 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000265 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
266 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700267 config.append("_");
268 config.appendS32(fGridInfo.fTileInterval.width());
269 config.append("x");
270 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000271 }
272#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000273 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000274 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000275 if (fSampleCount) {
276 config.appendf("_msaa%d", fSampleCount);
277 } else {
278 config.append("_gpu");
279 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000280 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000281 case kNVPR_DeviceType:
282 config.appendf("_nvprmsaa%d", fSampleCount);
283 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000284#if SK_ANGLE
285 case kAngle_DeviceType:
286 config.append("_angle");
287 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000288#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000289#if SK_MESA
290 case kMesa_DeviceType:
291 config.append("_mesa");
292 break;
293#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000294 default:
295 // Assume that no extra info means bitmap.
296 break;
297 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000298#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000299 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000300 return config;
301 }
302
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000303#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000304 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000305 switch (fDeviceType) {
306 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000307 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000308 // fall through
309#if SK_ANGLE
310 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000311 // fall through
312#endif
313#if SK_MESA
314 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000315#endif
316 return true;
317 default:
318 return false;
319 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000320 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000321
robertphillips@google.com6177e692013-02-28 20:16:25 +0000322 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000323 GrContextFactory::GLContextType glContextType
324 = GrContextFactory::kNull_GLContextType;
325 switch(fDeviceType) {
326 case kGPU_DeviceType:
327 glContextType = GrContextFactory::kNative_GLContextType;
328 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000329 case kNVPR_DeviceType:
330 glContextType = GrContextFactory::kNVPR_GLContextType;
331 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000332#if SK_ANGLE
333 case kAngle_DeviceType:
334 glContextType = GrContextFactory::kANGLE_GLContextType;
335 break;
336#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000337#if SK_MESA
338 case kMesa_DeviceType:
339 glContextType = GrContextFactory::kMESA_GLContextType;
340 break;
341#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000342 default:
343 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000344 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000345 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000346 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000347
348 GrContext* getGrContext() {
349 return fGrContext;
350 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000351#endif
352
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000353 SkCanvas* getCanvas() {
354 return fCanvas;
355 }
356
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000357 SkPicture* getPicture() {
358 return fPicture;
359 }
360
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000361 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000362 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000363 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000364 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000365 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000366 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000367#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000368 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000369 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000370#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000371 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000372 fGridInfo.fMargin.setEmpty();
373 fGridInfo.fOffset.setZero();
374 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000375 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000376 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000377 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000378
scroggo@google.com0556ea02013-02-08 19:38:21 +0000379#if SK_SUPPORT_GPU
380 virtual ~PictureRenderer() {
381 SkSafeUnref(fGrContext);
382 }
383#endif
384
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000385protected:
386 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000387 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000388 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000389 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000390 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000391 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000392 BBoxHierarchyType fBBoxHierarchyType;
393 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
394 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000395 SkString fWritePath;
396 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000397 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000398 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000399
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000400 void buildBBoxHierarchy();
401
402 /**
403 * Return the total width that should be drawn. If the viewport width has been set greater than
404 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
405 */
406 int getViewWidth();
407
408 /**
409 * Return the total height that should be drawn. If the viewport height has been set greater
410 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
411 */
412 int getViewHeight();
413
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000414 /**
415 * Scales the provided canvas to the scale factor set by setScaleFactor.
416 */
417 void scaleToScaleFactor(SkCanvas*);
418
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000419 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700420 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000421 SkCanvas* setupCanvas();
422 virtual SkCanvas* setupCanvas(int width, int height);
423
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000424 /**
425 * Copy src to dest; if src==NULL, set dest to empty string.
426 */
427 static void CopyString(SkString* dest, const SkString* src);
428
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000429private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000430 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000431 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000432#if SK_SUPPORT_GPU
433 GrContextFactory fGrContextFactory;
434 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000435 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000436#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000437
scroggo@google.com0a049b82012-11-02 22:01:26 +0000438 virtual SkString getConfigNameInternal() = 0;
439
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000440 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000441};
442
scroggo@google.com9a412522012-09-07 15:21:18 +0000443/**
444 * This class does not do any rendering, but its render function executes recording, which we want
445 * to time.
446 */
447class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000448 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000449
450 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
451
452 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000453
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000454protected:
455 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
456
scroggo@google.com0a049b82012-11-02 22:01:26 +0000457private:
458 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000459};
460
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000461class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000462public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000463 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000464
465private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000466 virtual SkString getConfigNameInternal() SK_OVERRIDE;
467
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000468 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000469};
470
471class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000472public:
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000473 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000474 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000475
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000476 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000477
478private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000479 virtual SkString getConfigNameInternal() SK_OVERRIDE;
480
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000481 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000482};
483
484class TiledPictureRenderer : public PictureRenderer {
485public:
486 TiledPictureRenderer();
487
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000488 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000489 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000490
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000491 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000492 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000493 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000494 * created for each tile, named "path0.png", "path1.png", etc.
495 * Multithreaded mode currently does not support writing to a file.
496 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000497 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000498
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000499 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000500
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000501 void setTileWidth(int width) {
502 fTileWidth = width;
503 }
504
505 int getTileWidth() const {
506 return fTileWidth;
507 }
508
509 void setTileHeight(int height) {
510 fTileHeight = height;
511 }
512
513 int getTileHeight() const {
514 return fTileHeight;
515 }
516
517 void setTileWidthPercentage(double percentage) {
518 fTileWidthPercentage = percentage;
519 }
520
keyar@chromium.org163b5672012-08-01 17:53:29 +0000521 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000522 return fTileWidthPercentage;
523 }
524
525 void setTileHeightPercentage(double percentage) {
526 fTileHeightPercentage = percentage;
527 }
528
keyar@chromium.org163b5672012-08-01 17:53:29 +0000529 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000530 return fTileHeightPercentage;
531 }
532
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000533 void setTileMinPowerOf2Width(int width) {
534 SkASSERT(SkIsPow2(width) && width > 0);
535 if (!SkIsPow2(width) || width <= 0) {
536 return;
537 }
538
539 fTileMinPowerOf2Width = width;
540 }
541
542 int getTileMinPowerOf2Width() const {
543 return fTileMinPowerOf2Width;
544 }
545
scroggo@google.comcbcef702012-12-13 22:09:28 +0000546 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
547
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000548 virtual bool supportsTimingIndividualTiles() { return true; }
549
scroggo@google.comcbcef702012-12-13 22:09:28 +0000550 /**
551 * Report the number of tiles in the x and y directions. Must not be called before init.
552 * @param x Output parameter identifying the number of tiles in the x direction.
553 * @param y Output parameter identifying the number of tiles in the y direction.
554 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
555 * unmodified.
556 */
557 bool tileDimensions(int& x, int&y);
558
559 /**
560 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
561 * for the first time.
562 * @param i Output parameter identifying the column of the next tile to be drawn on the next
563 * call to drawNextTile.
564 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
565 * to drawNextTile.
566 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
567 * is within the range of tiles. If false, i and j are unmodified.
568 */
569 bool nextTile(int& i, int& j);
570
571 /**
572 * Render one tile. This will draw the same tile each time it is called until nextTile is
573 * called. The tile rendered will depend on how many calls have been made to nextTile.
574 * It is an error to call this without first calling nextTile, or if nextTile returns false.
575 */
576 void drawCurrentTile();
577
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000578protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000579 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000580
scroggo@google.com0a049b82012-11-02 22:01:26 +0000581 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
582 virtual SkString getConfigNameInternal() SK_OVERRIDE;
583
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000584private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000585 int fTileWidth;
586 int fTileHeight;
587 double fTileWidthPercentage;
588 double fTileHeightPercentage;
589 int fTileMinPowerOf2Width;
590
591 // These variables are only used for timing individual tiles.
592 // Next tile to draw in fTileRects.
593 int fCurrentTileOffset;
594 // Number of tiles in the x direction.
595 int fTilesX;
596 // Number of tiles in the y direction.
597 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000598
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000599 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000600 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000601
602 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000603};
604
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000605class CloneData;
606
607class MultiCorePictureRenderer : public TiledPictureRenderer {
608public:
609 explicit MultiCorePictureRenderer(int threadCount);
610
611 ~MultiCorePictureRenderer();
612
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000613 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000614 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000615
616 /**
617 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
618 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000619 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000620
621 virtual void end() SK_OVERRIDE;
622
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000623 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
624
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000625private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000626 virtual SkString getConfigNameInternal() SK_OVERRIDE;
627
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000628 const int fNumThreads;
629 SkTDArray<SkCanvas*> fCanvasPool;
630 SkThreadPool fThreadPool;
631 SkPicture* fPictureClones;
632 CloneData** fCloneData;
633 SkCountdown fCountdown;
634
635 typedef TiledPictureRenderer INHERITED;
636};
637
scroggo@google.com9a412522012-09-07 15:21:18 +0000638/**
639 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
640 * into an SkPicturePlayback, which we want to time.
641 */
642class PlaybackCreationRenderer : public PictureRenderer {
643public:
644 virtual void setup() SK_OVERRIDE;
645
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000646 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000647
648 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
649
650 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
651
652private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000653 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000654
655 virtual SkString getConfigNameInternal() SK_OVERRIDE;
656
scroggo@google.com9a412522012-09-07 15:21:18 +0000657 typedef PictureRenderer INHERITED;
658};
659
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000660extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000661extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000662
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000663}
664
665#endif // PictureRenderer_DEFINED