blob: 7c5a9da1f0d2d97f6cb26d02b5edafc8e4f712f5 [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"
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000014#include "SkJSONCPP.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000015#include "SkMath.h"
reed@google.comea6a3062012-11-06 22:14:54 +000016#include "SkPaint.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000017#include "SkPicture.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"
junov@chromium.org29b19e52013-02-27 18:35:16 +000024#include "SkTileGridPicture.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000025#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000026
keyar@chromium.org06125642012-08-20 15:03:33 +000027#if SK_SUPPORT_GPU
28#include "GrContextFactory.h"
29#include "GrContext.h"
30#endif
31
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000032class SkBitmap;
33class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000034class SkGLContextHelper;
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
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000041/**
42 * Class for collecting image results (checksums) as we go.
43 */
44class ImageResultsSummary {
45public:
46 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000047 * Adds this bitmap hash to the summary of results.
48 *
49 * @param testName name of the test
50 * @param hash hash to store
51 */
52 void add(const char *testName, uint64_t hash);
53
54 /**
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000055 * Adds this bitmap's hash to the summary of results.
56 *
57 * @param testName name of the test
58 * @param bitmap bitmap to store the hash of
59 */
60 void add(const char *testName, const SkBitmap& bitmap);
61
62 /**
63 * Writes the summary (as constructed so far) to a file.
64 *
65 * @param filename path to write the summary to
66 */
67 void writeToFile(const char *filename);
68
69private:
70 Json::Value fActualResultsNoComparison;
71};
72
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000073class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000074
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000075public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000076 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000077#if SK_ANGLE
78 kAngle_DeviceType,
79#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000080#if SK_MESA
81 kMesa_DeviceType,
82#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000083 kBitmap_DeviceType,
84#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000085 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000086 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000087#endif
88 };
89
junov@chromium.org9313ca42012-11-02 18:11:49 +000090 enum BBoxHierarchyType {
91 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000092 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000093 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000094 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000095
96 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000097 };
98
caryclark@google.coma3622372012-11-06 21:26:13 +000099 // this uses SkPaint::Flags as a base and adds additional flags
100 enum DrawFilterFlags {
101 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +0000102 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
103 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
104 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +0000105 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +0000106 };
107
robertphillips@google.com49149312013-07-03 15:34:35 +0000108 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +0000109 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
110 hinting_flag_must_be_greater);
111 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
112 slight_hinting_flag_must_be_greater);
113
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000114 /**
115 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000116 *
117 * @param pict The SkPicture to render.
118 * @param outputDir The output directory within which this renderer should write files,
119 * or NULL if this renderer should not write files at all.
120 * @param inputFilename The name of the input file we are rendering.
121 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
122 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000123 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000124 virtual void init(SkPicture* pict, const SkString* outputDir,
125 const SkString* inputFilename, bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +0000126
127 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000128 * Set the viewport so that only the portion listed gets drawn.
129 */
130 void setViewport(SkISize size) { fViewport = size; }
131
132 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000133 * Set the scale factor at which draw the picture.
134 */
135 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
136
137 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000138 * Perform any setup that should done prior to each iteration of render() which should not be
139 * timed.
140 */
141 virtual void setup() {}
142
143 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000144 * Perform the work. If this is being called within the context of bench_pictures,
145 * this is the step that will be timed.
146 *
147 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
148 * it is recording the source SkPicture into another SkPicture.
149 *
150 * If fOutputDir has been specified, the result of the work will be written to that dir.
151 *
152 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
153 * output image into it, and return it here. (Some subclasses ignore this parameter)
154 * @return bool True if rendering succeeded and, if fOutputDir had been specified, the output
155 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000156 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000157 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000158
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000159 /**
160 * Called once finished with a particular SkPicture, before calling init again, and before
161 * being done with this Renderer.
162 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000163 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000164
scroggo@google.comcbcef702012-12-13 22:09:28 +0000165 /**
166 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
167 * TiledPictureRender so its methods can be called.
168 */
169 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
170
scroggo@google.com08085f82013-01-28 20:40:24 +0000171 /**
172 * 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 +0000173 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000174 * @param callFinish Whether to call finish.
175 */
176 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000177
scroggo@google.com0556ea02013-02-08 19:38:21 +0000178 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000179 * Remove all decoded textures from the CPU caches and all uploaded textures
180 * from the GPU.
181 */
182 void purgeTextures();
183
184 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000185 * Set the backend type. Returns true on success and false on failure.
186 */
187 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000188 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000189#if SK_SUPPORT_GPU
190 // In case this function is called more than once
191 SkSafeUnref(fGrContext);
192 fGrContext = NULL;
193 // Set to Native so it will have an initial value.
194 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
195#endif
196 switch(deviceType) {
197 case kBitmap_DeviceType:
198 return true;
199#if SK_SUPPORT_GPU
200 case kGPU_DeviceType:
201 // Already set to GrContextFactory::kNative_GLContextType, above.
202 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000203 case kNVPR_DeviceType:
204 glContextType = GrContextFactory::kNVPR_GLContextType;
205 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000206#if SK_ANGLE
207 case kAngle_DeviceType:
208 glContextType = GrContextFactory::kANGLE_GLContextType;
209 break;
210#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000211#if SK_MESA
212 case kMesa_DeviceType:
213 glContextType = GrContextFactory::kMESA_GLContextType;
214 break;
215#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000216#endif
217 default:
218 // Invalid device type.
219 return false;
220 }
221#if SK_SUPPORT_GPU
222 fGrContext = fGrContextFactory.get(glContextType);
223 if (NULL == fGrContext) {
224 return false;
225 } else {
226 fGrContext->ref();
227 return true;
228 }
229#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000230 }
231
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000232#if SK_SUPPORT_GPU
233 void setSampleCount(int sampleCount) {
234 fSampleCount = sampleCount;
235 }
236#endif
237
caryclark@google.come3e940c2012-11-07 16:42:17 +0000238 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
239 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000240 fDrawFiltersConfig = configName;
241 }
242
junov@chromium.org9313ca42012-11-02 18:11:49 +0000243 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
244 fBBoxHierarchyType = bbhType;
245 }
246
junov@chromium.orge286e842013-03-13 17:27:16 +0000247 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
248
junov@chromium.org7b537062012-11-06 18:58:43 +0000249 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000250 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000251 }
252
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000253 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
254 fJsonSummaryPtr = jsonSummaryPtr;
255 }
256
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000257 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000258 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000259 }
260
scroggo@google.com9a412522012-09-07 15:21:18 +0000261 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
262
263 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
264
scroggo@google.com0a049b82012-11-02 22:01:26 +0000265 /**
266 * Reports the configuration of this PictureRenderer.
267 */
268 SkString getConfigName() {
269 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000270 if (!fViewport.isEmpty()) {
271 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
272 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000273 if (fScaleFactor != SK_Scalar1) {
274 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
275 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000276 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
277 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000278 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
279 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000280 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
281 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000282 }
283#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000284 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000285 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000286 if (fSampleCount) {
287 config.appendf("_msaa%d", fSampleCount);
288 } else {
289 config.append("_gpu");
290 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000291 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000292 case kNVPR_DeviceType:
293 config.appendf("_nvprmsaa%d", fSampleCount);
294 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295#if SK_ANGLE
296 case kAngle_DeviceType:
297 config.append("_angle");
298 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000299#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000300#if SK_MESA
301 case kMesa_DeviceType:
302 config.append("_mesa");
303 break;
304#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000305 default:
306 // Assume that no extra info means bitmap.
307 break;
308 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000309#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000310 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000311 return config;
312 }
313
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000314#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000315 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000316 switch (fDeviceType) {
317 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000318 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000319 // fall through
320#if SK_ANGLE
321 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000322 // fall through
323#endif
324#if SK_MESA
325 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000326#endif
327 return true;
328 default:
329 return false;
330 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000331 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000332
robertphillips@google.com6177e692013-02-28 20:16:25 +0000333 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000334 GrContextFactory::GLContextType glContextType
335 = GrContextFactory::kNull_GLContextType;
336 switch(fDeviceType) {
337 case kGPU_DeviceType:
338 glContextType = GrContextFactory::kNative_GLContextType;
339 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000340 case kNVPR_DeviceType:
341 glContextType = GrContextFactory::kNVPR_GLContextType;
342 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000343#if SK_ANGLE
344 case kAngle_DeviceType:
345 glContextType = GrContextFactory::kANGLE_GLContextType;
346 break;
347#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000348#if SK_MESA
349 case kMesa_DeviceType:
350 glContextType = GrContextFactory::kMESA_GLContextType;
351 break;
352#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000353 default:
354 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000355 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000356 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000357 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000358
359 GrContext* getGrContext() {
360 return fGrContext;
361 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000362#endif
363
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000364 SkCanvas* getCanvas() {
365 return fCanvas;
366 }
367
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000368 SkPicture* getPicture() {
369 return fPicture;
370 }
371
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000372 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000373 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000374 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000375 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000376 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000377 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000378#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000379 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000380 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000381#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000382 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000383 fGridInfo.fMargin.setEmpty();
384 fGridInfo.fOffset.setZero();
385 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000386 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000387 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000388 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000389
scroggo@google.com0556ea02013-02-08 19:38:21 +0000390#if SK_SUPPORT_GPU
391 virtual ~PictureRenderer() {
392 SkSafeUnref(fGrContext);
393 }
394#endif
395
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000396protected:
397 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000398 SkPicture* fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000399 bool fUseChecksumBasedFilenames;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000400 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000401 SkDeviceTypes fDeviceType;
402 BBoxHierarchyType fBBoxHierarchyType;
403 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
404 SkString fDrawFiltersConfig;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000405 SkString fOutputDir;
406 SkString fInputFilename;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000407 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000408
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000409 void buildBBoxHierarchy();
410
411 /**
412 * Return the total width that should be drawn. If the viewport width has been set greater than
413 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
414 */
415 int getViewWidth();
416
417 /**
418 * Return the total height that should be drawn. If the viewport height has been set greater
419 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
420 */
421 int getViewHeight();
422
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000423 /**
424 * Scales the provided canvas to the scale factor set by setScaleFactor.
425 */
426 void scaleToScaleFactor(SkCanvas*);
427
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000428 SkPicture* createPicture();
429 uint32_t recordFlags();
430 SkCanvas* setupCanvas();
431 virtual SkCanvas* setupCanvas(int width, int height);
432
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000433 /**
434 * Copy src to dest; if src==NULL, set dest to empty string.
435 */
436 static void CopyString(SkString* dest, const SkString* src);
437
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000438private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000439 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000440 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000441#if SK_SUPPORT_GPU
442 GrContextFactory fGrContextFactory;
443 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000444 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000445#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000446
scroggo@google.com0a049b82012-11-02 22:01:26 +0000447 virtual SkString getConfigNameInternal() = 0;
448
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000449 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000450};
451
scroggo@google.com9a412522012-09-07 15:21:18 +0000452/**
453 * This class does not do any rendering, but its render function executes recording, which we want
454 * to time.
455 */
456class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000457 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000458
459 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
460
461 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000462
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000463protected:
464 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
465
scroggo@google.com0a049b82012-11-02 22:01:26 +0000466private:
467 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000468};
469
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000470class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000471public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000472 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000473
474private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000475 virtual SkString getConfigNameInternal() SK_OVERRIDE;
476
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000477 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000478};
479
480class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000481public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000482 virtual void init(SkPicture* pict, const SkString* outputDir,
483 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000484
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000485 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000486
487private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000488 virtual SkString getConfigNameInternal() SK_OVERRIDE;
489
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000490 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000491};
492
493class TiledPictureRenderer : public PictureRenderer {
494public:
495 TiledPictureRenderer();
496
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000497 virtual void init(SkPicture* pict, const SkString* outputDir,
498 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000499
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000500 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000501 * Renders to tiles, rather than a single canvas.
502 * If fOutputDir was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000503 * created for each tile, named "path0.png", "path1.png", etc.
504 * Multithreaded mode currently does not support writing to a file.
505 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000506 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000507
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000508 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000509
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000510 void setTileWidth(int width) {
511 fTileWidth = width;
512 }
513
514 int getTileWidth() const {
515 return fTileWidth;
516 }
517
518 void setTileHeight(int height) {
519 fTileHeight = height;
520 }
521
522 int getTileHeight() const {
523 return fTileHeight;
524 }
525
526 void setTileWidthPercentage(double percentage) {
527 fTileWidthPercentage = percentage;
528 }
529
keyar@chromium.org163b5672012-08-01 17:53:29 +0000530 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000531 return fTileWidthPercentage;
532 }
533
534 void setTileHeightPercentage(double percentage) {
535 fTileHeightPercentage = percentage;
536 }
537
keyar@chromium.org163b5672012-08-01 17:53:29 +0000538 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000539 return fTileHeightPercentage;
540 }
541
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000542 void setTileMinPowerOf2Width(int width) {
543 SkASSERT(SkIsPow2(width) && width > 0);
544 if (!SkIsPow2(width) || width <= 0) {
545 return;
546 }
547
548 fTileMinPowerOf2Width = width;
549 }
550
551 int getTileMinPowerOf2Width() const {
552 return fTileMinPowerOf2Width;
553 }
554
scroggo@google.comcbcef702012-12-13 22:09:28 +0000555 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
556
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000557 virtual bool supportsTimingIndividualTiles() { return true; }
558
scroggo@google.comcbcef702012-12-13 22:09:28 +0000559 /**
560 * Report the number of tiles in the x and y directions. Must not be called before init.
561 * @param x Output parameter identifying the number of tiles in the x direction.
562 * @param y Output parameter identifying the number of tiles in the y direction.
563 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
564 * unmodified.
565 */
566 bool tileDimensions(int& x, int&y);
567
568 /**
569 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
570 * for the first time.
571 * @param i Output parameter identifying the column of the next tile to be drawn on the next
572 * call to drawNextTile.
573 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
574 * to drawNextTile.
575 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
576 * is within the range of tiles. If false, i and j are unmodified.
577 */
578 bool nextTile(int& i, int& j);
579
580 /**
581 * Render one tile. This will draw the same tile each time it is called until nextTile is
582 * called. The tile rendered will depend on how many calls have been made to nextTile.
583 * It is an error to call this without first calling nextTile, or if nextTile returns false.
584 */
585 void drawCurrentTile();
586
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000587protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000588 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000589
scroggo@google.com0a049b82012-11-02 22:01:26 +0000590 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
591 virtual SkString getConfigNameInternal() SK_OVERRIDE;
592
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000593private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000594 int fTileWidth;
595 int fTileHeight;
596 double fTileWidthPercentage;
597 double fTileHeightPercentage;
598 int fTileMinPowerOf2Width;
599
600 // These variables are only used for timing individual tiles.
601 // Next tile to draw in fTileRects.
602 int fCurrentTileOffset;
603 // Number of tiles in the x direction.
604 int fTilesX;
605 // Number of tiles in the y direction.
606 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000607
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000608 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000609 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000610
611 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000612};
613
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000614class CloneData;
615
616class MultiCorePictureRenderer : public TiledPictureRenderer {
617public:
618 explicit MultiCorePictureRenderer(int threadCount);
619
620 ~MultiCorePictureRenderer();
621
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000622 virtual void init(SkPicture* pict, const SkString* outputDir,
623 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000624
625 /**
626 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
627 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000628 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000629
630 virtual void end() SK_OVERRIDE;
631
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000632 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
633
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000634private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000635 virtual SkString getConfigNameInternal() SK_OVERRIDE;
636
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000637 const int fNumThreads;
638 SkTDArray<SkCanvas*> fCanvasPool;
639 SkThreadPool fThreadPool;
640 SkPicture* fPictureClones;
641 CloneData** fCloneData;
642 SkCountdown fCountdown;
643
644 typedef TiledPictureRenderer INHERITED;
645};
646
scroggo@google.com9a412522012-09-07 15:21:18 +0000647/**
648 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
649 * into an SkPicturePlayback, which we want to time.
650 */
651class PlaybackCreationRenderer : public PictureRenderer {
652public:
653 virtual void setup() SK_OVERRIDE;
654
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000655 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000656
657 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
658
659 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
660
661private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000662 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000663
664 virtual SkString getConfigNameInternal() SK_OVERRIDE;
665
scroggo@google.com9a412522012-09-07 15:21:18 +0000666 typedef PictureRenderer INHERITED;
667};
668
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000669extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000670extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000671
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000672}
673
674#endif // PictureRenderer_DEFINED