blob: a22e0b7cd2edc450d62d1f27c07650d7b1e7618f [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 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000237 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
238 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000239 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
240 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000241 }
242#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000243 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000244 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000245 if (fSampleCount) {
246 config.appendf("_msaa%d", fSampleCount);
247 } else {
248 config.append("_gpu");
249 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000250 break;
251#if SK_ANGLE
252 case kAngle_DeviceType:
253 config.append("_angle");
254 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000255#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000256 default:
257 // Assume that no extra info means bitmap.
258 break;
259 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000260#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000261 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000262 return config;
263 }
264
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000265#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000266 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000267 switch (fDeviceType) {
268 case kGPU_DeviceType:
269 // fall through
270#if SK_ANGLE
271 case kAngle_DeviceType:
272#endif
273 return true;
274 default:
275 return false;
276 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000277 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000278
robertphillips@google.com6177e692013-02-28 20:16:25 +0000279 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000280 GrContextFactory::GLContextType glContextType
281 = GrContextFactory::kNull_GLContextType;
282 switch(fDeviceType) {
283 case kGPU_DeviceType:
284 glContextType = GrContextFactory::kNative_GLContextType;
285 break;
286#if SK_ANGLE
287 case kAngle_DeviceType:
288 glContextType = GrContextFactory::kANGLE_GLContextType;
289 break;
290#endif
291 default:
292 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000293 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000294 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000295 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000296
297 GrContext* getGrContext() {
298 return fGrContext;
299 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000300#endif
301
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000302 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000303 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000304 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000305 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000306 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000307 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000308#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000309 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000310 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000311#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000312 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000313 fGridInfo.fMargin.setEmpty();
314 fGridInfo.fOffset.setZero();
315 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000316 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000317 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000318 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000319
scroggo@google.com0556ea02013-02-08 19:38:21 +0000320#if SK_SUPPORT_GPU
321 virtual ~PictureRenderer() {
322 SkSafeUnref(fGrContext);
323 }
324#endif
325
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000326protected:
327 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000328 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000329 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000330 SkDeviceTypes fDeviceType;
331 BBoxHierarchyType fBBoxHierarchyType;
332 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
333 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000334 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000335
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000336 void buildBBoxHierarchy();
337
338 /**
339 * Return the total width that should be drawn. If the viewport width has been set greater than
340 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
341 */
342 int getViewWidth();
343
344 /**
345 * Return the total height that should be drawn. If the viewport height has been set greater
346 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
347 */
348 int getViewHeight();
349
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000350 /**
351 * Scales the provided canvas to the scale factor set by setScaleFactor.
352 */
353 void scaleToScaleFactor(SkCanvas*);
354
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000355 SkPicture* createPicture();
356 uint32_t recordFlags();
357 SkCanvas* setupCanvas();
358 virtual SkCanvas* setupCanvas(int width, int height);
359
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000360private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000361 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000362 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000363#if SK_SUPPORT_GPU
364 GrContextFactory fGrContextFactory;
365 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000366 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000367#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000368
scroggo@google.com0a049b82012-11-02 22:01:26 +0000369 virtual SkString getConfigNameInternal() = 0;
370
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000371 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000372};
373
scroggo@google.com9a412522012-09-07 15:21:18 +0000374/**
375 * This class does not do any rendering, but its render function executes recording, which we want
376 * to time.
377 */
378class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000379 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000380
381 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
382
383 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000384
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000385protected:
386 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
387
scroggo@google.com0a049b82012-11-02 22:01:26 +0000388private:
389 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000390};
391
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000392class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000393public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000394 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000395
396private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000397 virtual SkString getConfigNameInternal() SK_OVERRIDE;
398
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000399 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000400};
401
402class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000403public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000404 virtual void init(SkPicture* pict) SK_OVERRIDE;
405
edisonn@google.com84f548c2012-12-18 22:24:03 +0000406 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000407
408private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000409 virtual SkString getConfigNameInternal() SK_OVERRIDE;
410
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000411 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000412};
413
414class TiledPictureRenderer : public PictureRenderer {
415public:
416 TiledPictureRenderer();
417
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000418 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000419
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000420 /**
421 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
422 * created for each tile, named "path0.png", "path1.png", etc.
423 * Multithreaded mode currently does not support writing to a file.
424 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000425 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000426
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000427 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000428
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000429 void setTileWidth(int width) {
430 fTileWidth = width;
431 }
432
433 int getTileWidth() const {
434 return fTileWidth;
435 }
436
437 void setTileHeight(int height) {
438 fTileHeight = height;
439 }
440
441 int getTileHeight() const {
442 return fTileHeight;
443 }
444
445 void setTileWidthPercentage(double percentage) {
446 fTileWidthPercentage = percentage;
447 }
448
keyar@chromium.org163b5672012-08-01 17:53:29 +0000449 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000450 return fTileWidthPercentage;
451 }
452
453 void setTileHeightPercentage(double percentage) {
454 fTileHeightPercentage = percentage;
455 }
456
keyar@chromium.org163b5672012-08-01 17:53:29 +0000457 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000458 return fTileHeightPercentage;
459 }
460
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000461 void setTileMinPowerOf2Width(int width) {
462 SkASSERT(SkIsPow2(width) && width > 0);
463 if (!SkIsPow2(width) || width <= 0) {
464 return;
465 }
466
467 fTileMinPowerOf2Width = width;
468 }
469
470 int getTileMinPowerOf2Width() const {
471 return fTileMinPowerOf2Width;
472 }
473
scroggo@google.comcbcef702012-12-13 22:09:28 +0000474 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
475
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000476 virtual bool supportsTimingIndividualTiles() { return true; }
477
scroggo@google.comcbcef702012-12-13 22:09:28 +0000478 /**
479 * Report the number of tiles in the x and y directions. Must not be called before init.
480 * @param x Output parameter identifying the number of tiles in the x direction.
481 * @param y Output parameter identifying the number of tiles in the y direction.
482 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
483 * unmodified.
484 */
485 bool tileDimensions(int& x, int&y);
486
487 /**
488 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
489 * for the first time.
490 * @param i Output parameter identifying the column of the next tile to be drawn on the next
491 * call to drawNextTile.
492 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
493 * to drawNextTile.
494 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
495 * is within the range of tiles. If false, i and j are unmodified.
496 */
497 bool nextTile(int& i, int& j);
498
499 /**
500 * Render one tile. This will draw the same tile each time it is called until nextTile is
501 * called. The tile rendered will depend on how many calls have been made to nextTile.
502 * It is an error to call this without first calling nextTile, or if nextTile returns false.
503 */
504 void drawCurrentTile();
505
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000506protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000507 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000508
scroggo@google.com0a049b82012-11-02 22:01:26 +0000509 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
510 virtual SkString getConfigNameInternal() SK_OVERRIDE;
511
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000512private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000513 int fTileWidth;
514 int fTileHeight;
515 double fTileWidthPercentage;
516 double fTileHeightPercentage;
517 int fTileMinPowerOf2Width;
518
519 // These variables are only used for timing individual tiles.
520 // Next tile to draw in fTileRects.
521 int fCurrentTileOffset;
522 // Number of tiles in the x direction.
523 int fTilesX;
524 // Number of tiles in the y direction.
525 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000526
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000527 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000528 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000529
530 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000531};
532
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000533class CloneData;
534
535class MultiCorePictureRenderer : public TiledPictureRenderer {
536public:
537 explicit MultiCorePictureRenderer(int threadCount);
538
539 ~MultiCorePictureRenderer();
540
541 virtual void init(SkPicture* pict) SK_OVERRIDE;
542
543 /**
544 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
545 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000546 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000547
548 virtual void end() SK_OVERRIDE;
549
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000550 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
551
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000552private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000553 virtual SkString getConfigNameInternal() SK_OVERRIDE;
554
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000555 const int fNumThreads;
556 SkTDArray<SkCanvas*> fCanvasPool;
557 SkThreadPool fThreadPool;
558 SkPicture* fPictureClones;
559 CloneData** fCloneData;
560 SkCountdown fCountdown;
561
562 typedef TiledPictureRenderer INHERITED;
563};
564
scroggo@google.com9a412522012-09-07 15:21:18 +0000565/**
566 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
567 * into an SkPicturePlayback, which we want to time.
568 */
569class PlaybackCreationRenderer : public PictureRenderer {
570public:
571 virtual void setup() SK_OVERRIDE;
572
edisonn@google.com84f548c2012-12-18 22:24:03 +0000573 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000574
575 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
576
577 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
578
579private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000580 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000581
582 virtual SkString getConfigNameInternal() SK_OVERRIDE;
583
scroggo@google.com9a412522012-09-07 15:21:18 +0000584 typedef PictureRenderer INHERITED;
585};
586
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000587extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000588extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000589
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000590}
591
592#endif // PictureRenderer_DEFINED