blob: 468c567b13ce1b35a5a139b5d12b467fcf45a6b8 [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 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +000099 * Set the viewport so that only the portion listed gets drawn.
100 */
101 void setViewport(SkISize size) { fViewport = size; }
102
103 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000104 * Set the scale factor at which draw the picture.
105 */
106 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
107
108 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000109 * Perform any setup that should done prior to each iteration of render() which should not be
110 * timed.
111 */
112 virtual void setup() {}
113
114 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000115 * Perform the work. If this is being called within the context of bench_pictures,
116 * this is the step that will be timed.
117 *
118 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
119 * it is recording the source SkPicture into another SkPicture.
120 *
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000121 * If fWritePath has been specified, the result of the work will be written to that dir.
122 * If fMismatchPath has been specified, and the actual image result differs from its
123 * expectation, the result of the work will be written to that dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000124 *
125 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
126 * output image into it, and return it here. (Some subclasses ignore this parameter)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000127 * @return bool True if rendering succeeded and, if fWritePath had been specified, the output
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000128 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000129 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000130 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000131
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000132 /**
133 * Called once finished with a particular SkPicture, before calling init again, and before
134 * being done with this Renderer.
135 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000136 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000137
scroggo@google.comcbcef702012-12-13 22:09:28 +0000138 /**
139 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
140 * TiledPictureRender so its methods can be called.
141 */
142 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
143
scroggo@google.com08085f82013-01-28 20:40:24 +0000144 /**
145 * 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 +0000146 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000147 * @param callFinish Whether to call finish.
148 */
149 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000150
scroggo@google.com0556ea02013-02-08 19:38:21 +0000151 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000152 * Remove all decoded textures from the CPU caches and all uploaded textures
153 * from the GPU.
154 */
155 void purgeTextures();
156
157 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000158 * Set the backend type. Returns true on success and false on failure.
159 */
160 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000161 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000162#if SK_SUPPORT_GPU
163 // In case this function is called more than once
164 SkSafeUnref(fGrContext);
165 fGrContext = NULL;
166 // Set to Native so it will have an initial value.
167 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
168#endif
169 switch(deviceType) {
170 case kBitmap_DeviceType:
171 return true;
172#if SK_SUPPORT_GPU
173 case kGPU_DeviceType:
174 // Already set to GrContextFactory::kNative_GLContextType, above.
175 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000176 case kNVPR_DeviceType:
177 glContextType = GrContextFactory::kNVPR_GLContextType;
178 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000179#if SK_ANGLE
180 case kAngle_DeviceType:
181 glContextType = GrContextFactory::kANGLE_GLContextType;
182 break;
183#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000184#if SK_MESA
185 case kMesa_DeviceType:
186 glContextType = GrContextFactory::kMESA_GLContextType;
187 break;
188#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000189#endif
190 default:
191 // Invalid device type.
192 return false;
193 }
194#if SK_SUPPORT_GPU
195 fGrContext = fGrContextFactory.get(glContextType);
196 if (NULL == fGrContext) {
197 return false;
198 } else {
199 fGrContext->ref();
200 return true;
201 }
202#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000203 }
204
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000205#if SK_SUPPORT_GPU
206 void setSampleCount(int sampleCount) {
207 fSampleCount = sampleCount;
208 }
209#endif
210
caryclark@google.come3e940c2012-11-07 16:42:17 +0000211 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
212 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000213 fDrawFiltersConfig = configName;
214 }
215
junov@chromium.org9313ca42012-11-02 18:11:49 +0000216 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
217 fBBoxHierarchyType = bbhType;
218 }
219
junov@chromium.orge286e842013-03-13 17:27:16 +0000220 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
221
junov@chromium.org7b537062012-11-06 18:58:43 +0000222 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000223 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000224 }
225
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000226 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000227 fJsonSummaryPtr = jsonSummaryPtr;
228 }
229
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000230 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000231 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000232 }
233
scroggo@google.com9a412522012-09-07 15:21:18 +0000234 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
235
236 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
237
scroggo@google.com0a049b82012-11-02 22:01:26 +0000238 /**
239 * Reports the configuration of this PictureRenderer.
240 */
241 SkString getConfigName() {
242 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000243 if (!fViewport.isEmpty()) {
244 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
245 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000246 if (fScaleFactor != SK_Scalar1) {
247 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
248 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000249 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
250 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000251 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
252 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000253 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
254 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000255 }
256#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000257 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000258 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000259 if (fSampleCount) {
260 config.appendf("_msaa%d", fSampleCount);
261 } else {
262 config.append("_gpu");
263 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000264 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000265 case kNVPR_DeviceType:
266 config.appendf("_nvprmsaa%d", fSampleCount);
267 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000268#if SK_ANGLE
269 case kAngle_DeviceType:
270 config.append("_angle");
271 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000272#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000273#if SK_MESA
274 case kMesa_DeviceType:
275 config.append("_mesa");
276 break;
277#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000278 default:
279 // Assume that no extra info means bitmap.
280 break;
281 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000282#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000283 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000284 return config;
285 }
286
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000287#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000288 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000289 switch (fDeviceType) {
290 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000291 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000292 // fall through
293#if SK_ANGLE
294 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000295 // fall through
296#endif
297#if SK_MESA
298 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000299#endif
300 return true;
301 default:
302 return false;
303 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000304 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000305
robertphillips@google.com6177e692013-02-28 20:16:25 +0000306 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000307 GrContextFactory::GLContextType glContextType
308 = GrContextFactory::kNull_GLContextType;
309 switch(fDeviceType) {
310 case kGPU_DeviceType:
311 glContextType = GrContextFactory::kNative_GLContextType;
312 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000313 case kNVPR_DeviceType:
314 glContextType = GrContextFactory::kNVPR_GLContextType;
315 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000316#if SK_ANGLE
317 case kAngle_DeviceType:
318 glContextType = GrContextFactory::kANGLE_GLContextType;
319 break;
320#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000321#if SK_MESA
322 case kMesa_DeviceType:
323 glContextType = GrContextFactory::kMESA_GLContextType;
324 break;
325#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000326 default:
327 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000328 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000329 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000330 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000331
332 GrContext* getGrContext() {
333 return fGrContext;
334 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000335#endif
336
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000337 SkCanvas* getCanvas() {
338 return fCanvas;
339 }
340
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000341 SkPicture* getPicture() {
342 return fPicture;
343 }
344
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000345 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000346 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000347 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000348 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000349 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000350#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000351 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000352 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000353#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000354 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000355 fGridInfo.fMargin.setEmpty();
356 fGridInfo.fOffset.setZero();
357 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000358 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000359 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000360 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000361
scroggo@google.com0556ea02013-02-08 19:38:21 +0000362#if SK_SUPPORT_GPU
363 virtual ~PictureRenderer() {
364 SkSafeUnref(fGrContext);
365 }
366#endif
367
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000368protected:
369 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000370 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000371 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000372 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000373 SkDeviceTypes fDeviceType;
374 BBoxHierarchyType fBBoxHierarchyType;
375 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
376 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000377 SkString fWritePath;
378 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000379 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000380 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000381
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000382 void buildBBoxHierarchy();
383
384 /**
385 * Return the total width that should be drawn. If the viewport width has been set greater than
386 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
387 */
388 int getViewWidth();
389
390 /**
391 * Return the total height that should be drawn. If the viewport height has been set greater
392 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
393 */
394 int getViewHeight();
395
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000396 /**
397 * Scales the provided canvas to the scale factor set by setScaleFactor.
398 */
399 void scaleToScaleFactor(SkCanvas*);
400
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000401 SkBBHFactory* getFactory();
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000402 uint32_t recordFlags();
403 SkCanvas* setupCanvas();
404 virtual SkCanvas* setupCanvas(int width, int height);
405
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000406 /**
407 * Copy src to dest; if src==NULL, set dest to empty string.
408 */
409 static void CopyString(SkString* dest, const SkString* src);
410
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000411private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000412 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000413 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000414#if SK_SUPPORT_GPU
415 GrContextFactory fGrContextFactory;
416 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000417 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000418#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000419
scroggo@google.com0a049b82012-11-02 22:01:26 +0000420 virtual SkString getConfigNameInternal() = 0;
421
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000422 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000423};
424
scroggo@google.com9a412522012-09-07 15:21:18 +0000425/**
426 * This class does not do any rendering, but its render function executes recording, which we want
427 * to time.
428 */
429class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000430 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000431
432 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
433
434 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000435
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000436protected:
437 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
438
scroggo@google.com0a049b82012-11-02 22:01:26 +0000439private:
440 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000441};
442
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000443class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000444public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000445 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000446
447private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000448 virtual SkString getConfigNameInternal() SK_OVERRIDE;
449
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000450 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000451};
452
453class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000454public:
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000455 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000456 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000457
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000458 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000459
460private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000461 virtual SkString getConfigNameInternal() SK_OVERRIDE;
462
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000463 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000464};
465
466class TiledPictureRenderer : public PictureRenderer {
467public:
468 TiledPictureRenderer();
469
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000470 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000471 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000472
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000473 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000474 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000475 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000476 * created for each tile, named "path0.png", "path1.png", etc.
477 * Multithreaded mode currently does not support writing to a file.
478 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000479 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000480
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000481 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000482
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000483 void setTileWidth(int width) {
484 fTileWidth = width;
485 }
486
487 int getTileWidth() const {
488 return fTileWidth;
489 }
490
491 void setTileHeight(int height) {
492 fTileHeight = height;
493 }
494
495 int getTileHeight() const {
496 return fTileHeight;
497 }
498
499 void setTileWidthPercentage(double percentage) {
500 fTileWidthPercentage = percentage;
501 }
502
keyar@chromium.org163b5672012-08-01 17:53:29 +0000503 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000504 return fTileWidthPercentage;
505 }
506
507 void setTileHeightPercentage(double percentage) {
508 fTileHeightPercentage = percentage;
509 }
510
keyar@chromium.org163b5672012-08-01 17:53:29 +0000511 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000512 return fTileHeightPercentage;
513 }
514
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000515 void setTileMinPowerOf2Width(int width) {
516 SkASSERT(SkIsPow2(width) && width > 0);
517 if (!SkIsPow2(width) || width <= 0) {
518 return;
519 }
520
521 fTileMinPowerOf2Width = width;
522 }
523
524 int getTileMinPowerOf2Width() const {
525 return fTileMinPowerOf2Width;
526 }
527
scroggo@google.comcbcef702012-12-13 22:09:28 +0000528 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
529
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000530 virtual bool supportsTimingIndividualTiles() { return true; }
531
scroggo@google.comcbcef702012-12-13 22:09:28 +0000532 /**
533 * Report the number of tiles in the x and y directions. Must not be called before init.
534 * @param x Output parameter identifying the number of tiles in the x direction.
535 * @param y Output parameter identifying the number of tiles in the y direction.
536 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
537 * unmodified.
538 */
539 bool tileDimensions(int& x, int&y);
540
541 /**
542 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
543 * for the first time.
544 * @param i Output parameter identifying the column of the next tile to be drawn on the next
545 * call to drawNextTile.
546 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
547 * to drawNextTile.
548 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
549 * is within the range of tiles. If false, i and j are unmodified.
550 */
551 bool nextTile(int& i, int& j);
552
553 /**
554 * Render one tile. This will draw the same tile each time it is called until nextTile is
555 * called. The tile rendered will depend on how many calls have been made to nextTile.
556 * It is an error to call this without first calling nextTile, or if nextTile returns false.
557 */
558 void drawCurrentTile();
559
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000560protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000561 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000562
scroggo@google.com0a049b82012-11-02 22:01:26 +0000563 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
564 virtual SkString getConfigNameInternal() SK_OVERRIDE;
565
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000566private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000567 int fTileWidth;
568 int fTileHeight;
569 double fTileWidthPercentage;
570 double fTileHeightPercentage;
571 int fTileMinPowerOf2Width;
572
573 // These variables are only used for timing individual tiles.
574 // Next tile to draw in fTileRects.
575 int fCurrentTileOffset;
576 // Number of tiles in the x direction.
577 int fTilesX;
578 // Number of tiles in the y direction.
579 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000580
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000581 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000582 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000583
584 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000585};
586
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000587class CloneData;
588
589class MultiCorePictureRenderer : public TiledPictureRenderer {
590public:
591 explicit MultiCorePictureRenderer(int threadCount);
592
593 ~MultiCorePictureRenderer();
594
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000595 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000596 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000597
598 /**
599 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
600 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000601 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000602
603 virtual void end() SK_OVERRIDE;
604
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000605 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
606
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000607private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000608 virtual SkString getConfigNameInternal() SK_OVERRIDE;
609
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000610 const int fNumThreads;
611 SkTDArray<SkCanvas*> fCanvasPool;
612 SkThreadPool fThreadPool;
613 SkPicture* fPictureClones;
614 CloneData** fCloneData;
615 SkCountdown fCountdown;
616
617 typedef TiledPictureRenderer INHERITED;
618};
619
scroggo@google.com9a412522012-09-07 15:21:18 +0000620/**
621 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
622 * into an SkPicturePlayback, which we want to time.
623 */
624class PlaybackCreationRenderer : public PictureRenderer {
625public:
626 virtual void setup() SK_OVERRIDE;
627
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000628 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000629
630 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
631
632 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
633
634private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000635 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000636
637 virtual SkString getConfigNameInternal() SK_OVERRIDE;
638
scroggo@google.com9a412522012-09-07 15:21:18 +0000639 typedef PictureRenderer INHERITED;
640};
641
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000642extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000643extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000644
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000645}
646
647#endif // PictureRenderer_DEFINED