blob: 3fa7a3c3c41ba716af5da1366f1aa79182f40421 [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"
scroggo@google.comacfb30e2012-09-18 14:32:35 +000017#include "SkRect.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000018#include "SkRefCnt.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000019#include "SkRunnable.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000020#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000021#include "SkTDArray.h"
22#include "SkThreadPool.h"
junov@chromium.org29b19e52013-02-27 18:35:16 +000023#include "SkTileGridPicture.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000024#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
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000031class SkBitmap;
32class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000033class SkGLContextHelper;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000034class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000035
36namespace sk_tools {
37
scroggo@google.comcbcef702012-12-13 22:09:28 +000038class TiledPictureRenderer;
39
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000040class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000041
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000042public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000043 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000044#if SK_ANGLE
45 kAngle_DeviceType,
46#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000047 kBitmap_DeviceType,
48#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000049 kGPU_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000050#endif
51 };
52
junov@chromium.org9313ca42012-11-02 18:11:49 +000053 enum BBoxHierarchyType {
54 kNone_BBoxHierarchyType = 0,
55 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000056 kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000057 };
58
caryclark@google.coma3622372012-11-06 21:26:13 +000059 // this uses SkPaint::Flags as a base and adds additional flags
60 enum DrawFilterFlags {
61 kNone_DrawFilterFlag = 0,
caryclark@google.come3e940c2012-11-07 16:42:17 +000062 kBlur_DrawFilterFlag = 0x4000, // toggles between blur and no blur
reed@google.com457d8a72012-12-18 18:20:44 +000063 kHinting_DrawFilterFlag = 0x8000, // toggles between no hinting and normal hinting
64 kSlightHinting_DrawFilterFlag = 0x10000, // toggles between slight and normal hinting
65 kAAClip_DrawFilterFlag = 0x20000, // toggles between soft and hard clip
caryclark@google.coma3622372012-11-06 21:26:13 +000066 };
67
68 SK_COMPILE_ASSERT(!(kBlur_DrawFilterFlag & SkPaint::kAllFlags), blur_flag_must_be_greater);
69 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
70 hinting_flag_must_be_greater);
71 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
72 slight_hinting_flag_must_be_greater);
73
scroggo@google.coma62da2f2012-11-02 21:28:12 +000074 /**
75 * Called with each new SkPicture to render.
76 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +000077 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +000078
79 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +000080 * Set the viewport so that only the portion listed gets drawn.
81 */
82 void setViewport(SkISize size) { fViewport = size; }
83
84 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +000085 * Set the scale factor at which draw the picture.
86 */
87 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
88
89 /**
scroggo@google.com9a412522012-09-07 15:21:18 +000090 * Perform any setup that should done prior to each iteration of render() which should not be
91 * timed.
92 */
93 virtual void setup() {}
94
95 /**
96 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
97 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000098 * If path is non-null, subclass implementations should call write().
99 * @param path If non-null, also write the output to the file specified by path. path should
100 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000101 * @return bool True if rendering succeeded and, if path is non-null, the output was
102 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000103 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000104 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000105
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000106 /**
107 * Called once finished with a particular SkPicture, before calling init again, and before
108 * being done with this Renderer.
109 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000110 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000111
scroggo@google.comcbcef702012-12-13 22:09:28 +0000112 /**
113 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
114 * TiledPictureRender so its methods can be called.
115 */
116 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
117
scroggo@google.com08085f82013-01-28 20:40:24 +0000118 /**
119 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
120 * flush, and calls finish if callFinish is true.
121 * @param callFinish Whether to call finish.
122 */
123 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000124
scroggo@google.com0556ea02013-02-08 19:38:21 +0000125 /**
126 * Set the backend type. Returns true on success and false on failure.
127 */
128 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000129 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000130#if SK_SUPPORT_GPU
131 // In case this function is called more than once
132 SkSafeUnref(fGrContext);
133 fGrContext = NULL;
134 // Set to Native so it will have an initial value.
135 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
136#endif
137 switch(deviceType) {
138 case kBitmap_DeviceType:
139 return true;
140#if SK_SUPPORT_GPU
141 case kGPU_DeviceType:
142 // Already set to GrContextFactory::kNative_GLContextType, above.
143 break;
144#if SK_ANGLE
145 case kAngle_DeviceType:
146 glContextType = GrContextFactory::kANGLE_GLContextType;
147 break;
148#endif
149#endif
150 default:
151 // Invalid device type.
152 return false;
153 }
154#if SK_SUPPORT_GPU
155 fGrContext = fGrContextFactory.get(glContextType);
156 if (NULL == fGrContext) {
157 return false;
158 } else {
159 fGrContext->ref();
160 return true;
161 }
162#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000163 }
164
caryclark@google.come3e940c2012-11-07 16:42:17 +0000165 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
166 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000167 fDrawFiltersConfig = configName;
168 }
169
junov@chromium.org9313ca42012-11-02 18:11:49 +0000170 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
171 fBBoxHierarchyType = bbhType;
172 }
173
junov@chromium.orge286e842013-03-13 17:27:16 +0000174 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
175
junov@chromium.org7b537062012-11-06 18:58:43 +0000176 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000177 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000178 }
179
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000180 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000181 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000182 }
183
scroggo@google.com9a412522012-09-07 15:21:18 +0000184 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
185
186 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
187
scroggo@google.com0a049b82012-11-02 22:01:26 +0000188 /**
189 * Reports the configuration of this PictureRenderer.
190 */
191 SkString getConfigName() {
192 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000193 if (!fViewport.isEmpty()) {
194 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
195 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000196 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
197 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000198 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
199 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000200 }
201#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000202 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000203 case kGPU_DeviceType:
204 config.append("_gpu");
205 break;
206#if SK_ANGLE
207 case kAngle_DeviceType:
208 config.append("_angle");
209 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000210#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000211 default:
212 // Assume that no extra info means bitmap.
213 break;
214 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000215#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000216 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000217 return config;
218 }
219
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000220#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000221 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000222 switch (fDeviceType) {
223 case kGPU_DeviceType:
224 // fall through
225#if SK_ANGLE
226 case kAngle_DeviceType:
227#endif
228 return true;
229 default:
230 return false;
231 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000232 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000233
robertphillips@google.com6177e692013-02-28 20:16:25 +0000234 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000235 GrContextFactory::GLContextType glContextType
236 = GrContextFactory::kNull_GLContextType;
237 switch(fDeviceType) {
238 case kGPU_DeviceType:
239 glContextType = GrContextFactory::kNative_GLContextType;
240 break;
241#if SK_ANGLE
242 case kAngle_DeviceType:
243 glContextType = GrContextFactory::kANGLE_GLContextType;
244 break;
245#endif
246 default:
247 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000248 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000249 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000250 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000251
252 GrContext* getGrContext() {
253 return fGrContext;
254 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000255#endif
256
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000257 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000258 : fPicture(NULL)
259 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000260 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000261 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000262#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000263 , fGrContext(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000264#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000265 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000266 fGridInfo.fMargin.setEmpty();
267 fGridInfo.fOffset.setZero();
268 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000269 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000270 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000271 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000272
scroggo@google.com0556ea02013-02-08 19:38:21 +0000273#if SK_SUPPORT_GPU
274 virtual ~PictureRenderer() {
275 SkSafeUnref(fGrContext);
276 }
277#endif
278
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000279protected:
280 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000281 SkPicture* fPicture;
282 SkDeviceTypes fDeviceType;
283 BBoxHierarchyType fBBoxHierarchyType;
284 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
285 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000286 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000287
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000288 void buildBBoxHierarchy();
289
290 /**
291 * Return the total width that should be drawn. If the viewport width has been set greater than
292 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
293 */
294 int getViewWidth();
295
296 /**
297 * Return the total height that should be drawn. If the viewport height has been set greater
298 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
299 */
300 int getViewHeight();
301
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000302 /**
303 * Scales the provided canvas to the scale factor set by setScaleFactor.
304 */
305 void scaleToScaleFactor(SkCanvas*);
306
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000307 SkPicture* createPicture();
308 uint32_t recordFlags();
309 SkCanvas* setupCanvas();
310 virtual SkCanvas* setupCanvas(int width, int height);
311
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000312private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000313 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000314 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000315#if SK_SUPPORT_GPU
316 GrContextFactory fGrContextFactory;
317 GrContext* fGrContext;
318#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000319
scroggo@google.com0a049b82012-11-02 22:01:26 +0000320 virtual SkString getConfigNameInternal() = 0;
321
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000322 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000323};
324
scroggo@google.com9a412522012-09-07 15:21:18 +0000325/**
326 * This class does not do any rendering, but its render function executes recording, which we want
327 * to time.
328 */
329class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000330 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000331
332 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
333
334 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000335
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000336protected:
337 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
338
scroggo@google.com0a049b82012-11-02 22:01:26 +0000339private:
340 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000341};
342
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000343class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000344public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000345 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000346
347private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000348 virtual SkString getConfigNameInternal() SK_OVERRIDE;
349
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000350 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000351};
352
353class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000354public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000355 virtual void init(SkPicture* pict) SK_OVERRIDE;
356
edisonn@google.com84f548c2012-12-18 22:24:03 +0000357 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000358
359private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000360 virtual SkString getConfigNameInternal() SK_OVERRIDE;
361
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000362 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000363};
364
365class TiledPictureRenderer : public PictureRenderer {
366public:
367 TiledPictureRenderer();
368
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000369 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000370
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000371 /**
372 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
373 * created for each tile, named "path0.png", "path1.png", etc.
374 * Multithreaded mode currently does not support writing to a file.
375 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000376 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000377
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000378 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000379
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000380 void setTileWidth(int width) {
381 fTileWidth = width;
382 }
383
384 int getTileWidth() const {
385 return fTileWidth;
386 }
387
388 void setTileHeight(int height) {
389 fTileHeight = height;
390 }
391
392 int getTileHeight() const {
393 return fTileHeight;
394 }
395
396 void setTileWidthPercentage(double percentage) {
397 fTileWidthPercentage = percentage;
398 }
399
keyar@chromium.org163b5672012-08-01 17:53:29 +0000400 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000401 return fTileWidthPercentage;
402 }
403
404 void setTileHeightPercentage(double percentage) {
405 fTileHeightPercentage = percentage;
406 }
407
keyar@chromium.org163b5672012-08-01 17:53:29 +0000408 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000409 return fTileHeightPercentage;
410 }
411
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000412 void setTileMinPowerOf2Width(int width) {
413 SkASSERT(SkIsPow2(width) && width > 0);
414 if (!SkIsPow2(width) || width <= 0) {
415 return;
416 }
417
418 fTileMinPowerOf2Width = width;
419 }
420
421 int getTileMinPowerOf2Width() const {
422 return fTileMinPowerOf2Width;
423 }
424
scroggo@google.comcbcef702012-12-13 22:09:28 +0000425 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
426
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000427 virtual bool supportsTimingIndividualTiles() { return true; }
428
scroggo@google.comcbcef702012-12-13 22:09:28 +0000429 /**
430 * Report the number of tiles in the x and y directions. Must not be called before init.
431 * @param x Output parameter identifying the number of tiles in the x direction.
432 * @param y Output parameter identifying the number of tiles in the y direction.
433 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
434 * unmodified.
435 */
436 bool tileDimensions(int& x, int&y);
437
438 /**
439 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
440 * for the first time.
441 * @param i Output parameter identifying the column of the next tile to be drawn on the next
442 * call to drawNextTile.
443 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
444 * to drawNextTile.
445 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
446 * is within the range of tiles. If false, i and j are unmodified.
447 */
448 bool nextTile(int& i, int& j);
449
450 /**
451 * Render one tile. This will draw the same tile each time it is called until nextTile is
452 * called. The tile rendered will depend on how many calls have been made to nextTile.
453 * It is an error to call this without first calling nextTile, or if nextTile returns false.
454 */
455 void drawCurrentTile();
456
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000457protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000458 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000459
scroggo@google.com0a049b82012-11-02 22:01:26 +0000460 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
461 virtual SkString getConfigNameInternal() SK_OVERRIDE;
462
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000463private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000464 int fTileWidth;
465 int fTileHeight;
466 double fTileWidthPercentage;
467 double fTileHeightPercentage;
468 int fTileMinPowerOf2Width;
469
470 // These variables are only used for timing individual tiles.
471 // Next tile to draw in fTileRects.
472 int fCurrentTileOffset;
473 // Number of tiles in the x direction.
474 int fTilesX;
475 // Number of tiles in the y direction.
476 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000477
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000478 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000479 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000480
481 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000482};
483
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000484class CloneData;
485
486class MultiCorePictureRenderer : public TiledPictureRenderer {
487public:
488 explicit MultiCorePictureRenderer(int threadCount);
489
490 ~MultiCorePictureRenderer();
491
492 virtual void init(SkPicture* pict) SK_OVERRIDE;
493
494 /**
495 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
496 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000497 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000498
499 virtual void end() SK_OVERRIDE;
500
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000501 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
502
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000503private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000504 virtual SkString getConfigNameInternal() SK_OVERRIDE;
505
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000506 const int fNumThreads;
507 SkTDArray<SkCanvas*> fCanvasPool;
508 SkThreadPool fThreadPool;
509 SkPicture* fPictureClones;
510 CloneData** fCloneData;
511 SkCountdown fCountdown;
512
513 typedef TiledPictureRenderer INHERITED;
514};
515
scroggo@google.com9a412522012-09-07 15:21:18 +0000516/**
517 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
518 * into an SkPicturePlayback, which we want to time.
519 */
520class PlaybackCreationRenderer : public PictureRenderer {
521public:
522 virtual void setup() SK_OVERRIDE;
523
edisonn@google.com84f548c2012-12-18 22:24:03 +0000524 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000525
526 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
527
528 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
529
530private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000531 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000532
533 virtual SkString getConfigNameInternal() SK_OVERRIDE;
534
scroggo@google.com9a412522012-09-07 15:21:18 +0000535 typedef PictureRenderer INHERITED;
536};
537
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000538extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000539extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000540
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000541}
542
543#endif // PictureRenderer_DEFINED