blob: e524aa12962fb4b1a954775336236556acba1c78 [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 /**
47 * Adds this bitmap's hash to the summary of results.
48 *
49 * @param testName name of the test
50 * @param bitmap bitmap to store the hash of
51 */
52 void add(const char *testName, const SkBitmap& bitmap);
53
54 /**
55 * Writes the summary (as constructed so far) to a file.
56 *
57 * @param filename path to write the summary to
58 */
59 void writeToFile(const char *filename);
60
61private:
62 Json::Value fActualResultsNoComparison;
63};
64
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000065class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000066
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000067public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000068 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000069#if SK_ANGLE
70 kAngle_DeviceType,
71#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000072 kBitmap_DeviceType,
73#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000074 kGPU_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000075#endif
76 };
77
junov@chromium.org9313ca42012-11-02 18:11:49 +000078 enum BBoxHierarchyType {
79 kNone_BBoxHierarchyType = 0,
80 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000081 kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000082 };
83
caryclark@google.coma3622372012-11-06 21:26:13 +000084 // this uses SkPaint::Flags as a base and adds additional flags
85 enum DrawFilterFlags {
86 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000087 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
88 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
89 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000090 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000091 };
92
robertphillips@google.com49149312013-07-03 15:34:35 +000093 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000094 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
95 hinting_flag_must_be_greater);
96 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
97 slight_hinting_flag_must_be_greater);
98
scroggo@google.coma62da2f2012-11-02 21:28:12 +000099 /**
100 * Called with each new SkPicture to render.
101 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000102 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +0000103
104 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000105 * Set the viewport so that only the portion listed gets drawn.
106 */
107 void setViewport(SkISize size) { fViewport = size; }
108
109 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000110 * Set the scale factor at which draw the picture.
111 */
112 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
113
114 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000115 * Perform any setup that should done prior to each iteration of render() which should not be
116 * timed.
117 */
118 virtual void setup() {}
119
120 /**
121 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
122 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000123 * If path is non-null, subclass implementations should call write().
124 * @param path If non-null, also write the output to the file specified by path. path should
125 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000126 * @return bool True if rendering succeeded and, if path is non-null, the output was
127 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000128 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000129 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000130
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000131 /**
132 * Called once finished with a particular SkPicture, before calling init again, and before
133 * being done with this Renderer.
134 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000135 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000136
scroggo@google.comcbcef702012-12-13 22:09:28 +0000137 /**
138 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
139 * TiledPictureRender so its methods can be called.
140 */
141 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
142
scroggo@google.com08085f82013-01-28 20:40:24 +0000143 /**
144 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
145 * flush, and calls finish if callFinish is true.
146 * @param callFinish Whether to call finish.
147 */
148 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000149
scroggo@google.com0556ea02013-02-08 19:38:21 +0000150 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000151 * Remove all decoded textures from the CPU caches and all uploaded textures
152 * from the GPU.
153 */
154 void purgeTextures();
155
156 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000157 * Set the backend type. Returns true on success and false on failure.
158 */
159 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000160 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000161#if SK_SUPPORT_GPU
162 // In case this function is called more than once
163 SkSafeUnref(fGrContext);
164 fGrContext = NULL;
165 // Set to Native so it will have an initial value.
166 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
167#endif
168 switch(deviceType) {
169 case kBitmap_DeviceType:
170 return true;
171#if SK_SUPPORT_GPU
172 case kGPU_DeviceType:
173 // Already set to GrContextFactory::kNative_GLContextType, above.
174 break;
175#if SK_ANGLE
176 case kAngle_DeviceType:
177 glContextType = GrContextFactory::kANGLE_GLContextType;
178 break;
179#endif
180#endif
181 default:
182 // Invalid device type.
183 return false;
184 }
185#if SK_SUPPORT_GPU
186 fGrContext = fGrContextFactory.get(glContextType);
187 if (NULL == fGrContext) {
188 return false;
189 } else {
190 fGrContext->ref();
191 return true;
192 }
193#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000194 }
195
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000196#if SK_SUPPORT_GPU
197 void setSampleCount(int sampleCount) {
198 fSampleCount = sampleCount;
199 }
200#endif
201
caryclark@google.come3e940c2012-11-07 16:42:17 +0000202 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
203 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000204 fDrawFiltersConfig = configName;
205 }
206
junov@chromium.org9313ca42012-11-02 18:11:49 +0000207 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
208 fBBoxHierarchyType = bbhType;
209 }
210
junov@chromium.orge286e842013-03-13 17:27:16 +0000211 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
212
junov@chromium.org7b537062012-11-06 18:58:43 +0000213 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000214 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000215 }
216
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000217 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
218 fJsonSummaryPtr = jsonSummaryPtr;
219 }
220
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000221 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000222 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000223 }
224
scroggo@google.com9a412522012-09-07 15:21:18 +0000225 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
226
227 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
228
scroggo@google.com0a049b82012-11-02 22:01:26 +0000229 /**
230 * Reports the configuration of this PictureRenderer.
231 */
232 SkString getConfigName() {
233 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000234 if (!fViewport.isEmpty()) {
235 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
236 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000237 if (fScaleFactor != SK_Scalar1) {
238 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
239 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000240 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
241 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000242 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
243 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000244 }
245#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000246 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000247 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000248 if (fSampleCount) {
249 config.appendf("_msaa%d", fSampleCount);
250 } else {
251 config.append("_gpu");
252 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000253 break;
254#if SK_ANGLE
255 case kAngle_DeviceType:
256 config.append("_angle");
257 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000258#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000259 default:
260 // Assume that no extra info means bitmap.
261 break;
262 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000263#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000264 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000265 return config;
266 }
267
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000268#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000269 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000270 switch (fDeviceType) {
271 case kGPU_DeviceType:
272 // fall through
273#if SK_ANGLE
274 case kAngle_DeviceType:
275#endif
276 return true;
277 default:
278 return false;
279 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000280 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000281
robertphillips@google.com6177e692013-02-28 20:16:25 +0000282 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000283 GrContextFactory::GLContextType glContextType
284 = GrContextFactory::kNull_GLContextType;
285 switch(fDeviceType) {
286 case kGPU_DeviceType:
287 glContextType = GrContextFactory::kNative_GLContextType;
288 break;
289#if SK_ANGLE
290 case kAngle_DeviceType:
291 glContextType = GrContextFactory::kANGLE_GLContextType;
292 break;
293#endif
294 default:
295 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000296 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000297 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000298 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000299
300 GrContext* getGrContext() {
301 return fGrContext;
302 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000303#endif
304
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000305 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000306 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000307 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000308 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000309 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000310 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000311#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000312 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000313 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000314#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000315 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000316 fGridInfo.fMargin.setEmpty();
317 fGridInfo.fOffset.setZero();
318 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000319 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000320 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000321 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000322
scroggo@google.com0556ea02013-02-08 19:38:21 +0000323#if SK_SUPPORT_GPU
324 virtual ~PictureRenderer() {
325 SkSafeUnref(fGrContext);
326 }
327#endif
328
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000329protected:
330 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000331 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000332 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000333 SkDeviceTypes fDeviceType;
334 BBoxHierarchyType fBBoxHierarchyType;
335 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
336 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000337 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000338
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000339 void buildBBoxHierarchy();
340
341 /**
342 * Return the total width that should be drawn. If the viewport width has been set greater than
343 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
344 */
345 int getViewWidth();
346
347 /**
348 * Return the total height that should be drawn. If the viewport height has been set greater
349 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
350 */
351 int getViewHeight();
352
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000353 /**
354 * Scales the provided canvas to the scale factor set by setScaleFactor.
355 */
356 void scaleToScaleFactor(SkCanvas*);
357
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000358 SkPicture* createPicture();
359 uint32_t recordFlags();
360 SkCanvas* setupCanvas();
361 virtual SkCanvas* setupCanvas(int width, int height);
362
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000363private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000364 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000365 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000366#if SK_SUPPORT_GPU
367 GrContextFactory fGrContextFactory;
368 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000369 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000370#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000371
scroggo@google.com0a049b82012-11-02 22:01:26 +0000372 virtual SkString getConfigNameInternal() = 0;
373
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000374 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000375};
376
scroggo@google.com9a412522012-09-07 15:21:18 +0000377/**
378 * This class does not do any rendering, but its render function executes recording, which we want
379 * to time.
380 */
381class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000382 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000383
384 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
385
386 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000387
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000388protected:
389 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
390
scroggo@google.com0a049b82012-11-02 22:01:26 +0000391private:
392 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000393};
394
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000395class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000396public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000397 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000398
399private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000400 virtual SkString getConfigNameInternal() SK_OVERRIDE;
401
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000402 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000403};
404
405class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000406public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000407 virtual void init(SkPicture* pict) SK_OVERRIDE;
408
edisonn@google.com84f548c2012-12-18 22:24:03 +0000409 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000410
411private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000412 virtual SkString getConfigNameInternal() SK_OVERRIDE;
413
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000414 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000415};
416
417class TiledPictureRenderer : public PictureRenderer {
418public:
419 TiledPictureRenderer();
420
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000421 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000422
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000423 /**
424 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
425 * created for each tile, named "path0.png", "path1.png", etc.
426 * Multithreaded mode currently does not support writing to a file.
427 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000428 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000429
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000430 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000431
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000432 void setTileWidth(int width) {
433 fTileWidth = width;
434 }
435
436 int getTileWidth() const {
437 return fTileWidth;
438 }
439
440 void setTileHeight(int height) {
441 fTileHeight = height;
442 }
443
444 int getTileHeight() const {
445 return fTileHeight;
446 }
447
448 void setTileWidthPercentage(double percentage) {
449 fTileWidthPercentage = percentage;
450 }
451
keyar@chromium.org163b5672012-08-01 17:53:29 +0000452 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000453 return fTileWidthPercentage;
454 }
455
456 void setTileHeightPercentage(double percentage) {
457 fTileHeightPercentage = percentage;
458 }
459
keyar@chromium.org163b5672012-08-01 17:53:29 +0000460 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000461 return fTileHeightPercentage;
462 }
463
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000464 void setTileMinPowerOf2Width(int width) {
465 SkASSERT(SkIsPow2(width) && width > 0);
466 if (!SkIsPow2(width) || width <= 0) {
467 return;
468 }
469
470 fTileMinPowerOf2Width = width;
471 }
472
473 int getTileMinPowerOf2Width() const {
474 return fTileMinPowerOf2Width;
475 }
476
scroggo@google.comcbcef702012-12-13 22:09:28 +0000477 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
478
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000479 virtual bool supportsTimingIndividualTiles() { return true; }
480
scroggo@google.comcbcef702012-12-13 22:09:28 +0000481 /**
482 * Report the number of tiles in the x and y directions. Must not be called before init.
483 * @param x Output parameter identifying the number of tiles in the x direction.
484 * @param y Output parameter identifying the number of tiles in the y direction.
485 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
486 * unmodified.
487 */
488 bool tileDimensions(int& x, int&y);
489
490 /**
491 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
492 * for the first time.
493 * @param i Output parameter identifying the column of the next tile to be drawn on the next
494 * call to drawNextTile.
495 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
496 * to drawNextTile.
497 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
498 * is within the range of tiles. If false, i and j are unmodified.
499 */
500 bool nextTile(int& i, int& j);
501
502 /**
503 * Render one tile. This will draw the same tile each time it is called until nextTile is
504 * called. The tile rendered will depend on how many calls have been made to nextTile.
505 * It is an error to call this without first calling nextTile, or if nextTile returns false.
506 */
507 void drawCurrentTile();
508
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000509protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000510 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000511
scroggo@google.com0a049b82012-11-02 22:01:26 +0000512 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
513 virtual SkString getConfigNameInternal() SK_OVERRIDE;
514
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000515private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000516 int fTileWidth;
517 int fTileHeight;
518 double fTileWidthPercentage;
519 double fTileHeightPercentage;
520 int fTileMinPowerOf2Width;
521
522 // These variables are only used for timing individual tiles.
523 // Next tile to draw in fTileRects.
524 int fCurrentTileOffset;
525 // Number of tiles in the x direction.
526 int fTilesX;
527 // Number of tiles in the y direction.
528 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000529
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000530 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000531 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000532
533 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000534};
535
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000536class CloneData;
537
538class MultiCorePictureRenderer : public TiledPictureRenderer {
539public:
540 explicit MultiCorePictureRenderer(int threadCount);
541
542 ~MultiCorePictureRenderer();
543
544 virtual void init(SkPicture* pict) SK_OVERRIDE;
545
546 /**
547 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
548 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000549 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000550
551 virtual void end() SK_OVERRIDE;
552
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000553 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
554
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000555private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000556 virtual SkString getConfigNameInternal() SK_OVERRIDE;
557
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000558 const int fNumThreads;
559 SkTDArray<SkCanvas*> fCanvasPool;
560 SkThreadPool fThreadPool;
561 SkPicture* fPictureClones;
562 CloneData** fCloneData;
563 SkCountdown fCountdown;
564
565 typedef TiledPictureRenderer INHERITED;
566};
567
scroggo@google.com9a412522012-09-07 15:21:18 +0000568/**
569 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
570 * into an SkPicturePlayback, which we want to time.
571 */
572class PlaybackCreationRenderer : public PictureRenderer {
573public:
574 virtual void setup() SK_OVERRIDE;
575
edisonn@google.com84f548c2012-12-18 22:24:03 +0000576 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000577
578 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
579
580 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
581
582private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000583 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000584
585 virtual SkString getConfigNameInternal() SK_OVERRIDE;
586
scroggo@google.com9a412522012-09-07 15:21:18 +0000587 typedef PictureRenderer INHERITED;
588};
589
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000590extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000591extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000592
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000593}
594
595#endif // PictureRenderer_DEFINED