blob: b073252199243c23da25f34f6eccc4a4e5e492a8 [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.org7b537062012-11-06 18:58:43 +0000174 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000175 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000176 }
177
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000178 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000179 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000180 }
181
scroggo@google.com9a412522012-09-07 15:21:18 +0000182 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
183
184 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
185
scroggo@google.com0a049b82012-11-02 22:01:26 +0000186 /**
187 * Reports the configuration of this PictureRenderer.
188 */
189 SkString getConfigName() {
190 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000191 if (!fViewport.isEmpty()) {
192 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
193 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000194 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
195 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000196 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
197 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000198 }
199#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000200 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000201 case kGPU_DeviceType:
202 config.append("_gpu");
203 break;
204#if SK_ANGLE
205 case kAngle_DeviceType:
206 config.append("_angle");
207 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000208#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000209 default:
210 // Assume that no extra info means bitmap.
211 break;
212 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000213#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000214 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000215 return config;
216 }
217
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000218#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000219 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000220 switch (fDeviceType) {
221 case kGPU_DeviceType:
222 // fall through
223#if SK_ANGLE
224 case kAngle_DeviceType:
225#endif
226 return true;
227 default:
228 return false;
229 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000230 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000231
robertphillips@google.com6177e692013-02-28 20:16:25 +0000232 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000233 GrContextFactory::GLContextType glContextType
234 = GrContextFactory::kNull_GLContextType;
235 switch(fDeviceType) {
236 case kGPU_DeviceType:
237 glContextType = GrContextFactory::kNative_GLContextType;
238 break;
239#if SK_ANGLE
240 case kAngle_DeviceType:
241 glContextType = GrContextFactory::kANGLE_GLContextType;
242 break;
243#endif
244 default:
245 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000246 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000247 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000248 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000249
250 GrContext* getGrContext() {
251 return fGrContext;
252 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000253#endif
254
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000255 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000256 : fPicture(NULL)
257 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000258 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000259 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000260#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000261 , fGrContext(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000262#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000263 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000264 fGridInfo.fMargin.setEmpty();
265 fGridInfo.fOffset.setZero();
266 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000267 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000268 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000269 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000270
scroggo@google.com0556ea02013-02-08 19:38:21 +0000271#if SK_SUPPORT_GPU
272 virtual ~PictureRenderer() {
273 SkSafeUnref(fGrContext);
274 }
275#endif
276
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000277protected:
278 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000279 SkPicture* fPicture;
280 SkDeviceTypes fDeviceType;
281 BBoxHierarchyType fBBoxHierarchyType;
282 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
283 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000284 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000285
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000286 void buildBBoxHierarchy();
287
288 /**
289 * Return the total width that should be drawn. If the viewport width has been set greater than
290 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
291 */
292 int getViewWidth();
293
294 /**
295 * Return the total height that should be drawn. If the viewport height has been set greater
296 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
297 */
298 int getViewHeight();
299
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000300 /**
301 * Scales the provided canvas to the scale factor set by setScaleFactor.
302 */
303 void scaleToScaleFactor(SkCanvas*);
304
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000305 SkPicture* createPicture();
306 uint32_t recordFlags();
307 SkCanvas* setupCanvas();
308 virtual SkCanvas* setupCanvas(int width, int height);
309
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000310private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000311 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000312 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000313#if SK_SUPPORT_GPU
314 GrContextFactory fGrContextFactory;
315 GrContext* fGrContext;
316#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000317
scroggo@google.com0a049b82012-11-02 22:01:26 +0000318 virtual SkString getConfigNameInternal() = 0;
319
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000320 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000321};
322
scroggo@google.com9a412522012-09-07 15:21:18 +0000323/**
324 * This class does not do any rendering, but its render function executes recording, which we want
325 * to time.
326 */
327class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000328 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000329
330 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
331
332 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000333
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000334protected:
335 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
336
scroggo@google.com0a049b82012-11-02 22:01:26 +0000337private:
338 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000339};
340
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000341class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000342public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000343 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000344
345private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000346 virtual SkString getConfigNameInternal() SK_OVERRIDE;
347
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000348 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000349};
350
351class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000352public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000353 virtual void init(SkPicture* pict) SK_OVERRIDE;
354
edisonn@google.com84f548c2012-12-18 22:24:03 +0000355 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000356
357private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000358 virtual SkString getConfigNameInternal() SK_OVERRIDE;
359
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000360 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000361};
362
363class TiledPictureRenderer : public PictureRenderer {
364public:
365 TiledPictureRenderer();
366
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000367 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000368
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000369 /**
370 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
371 * created for each tile, named "path0.png", "path1.png", etc.
372 * Multithreaded mode currently does not support writing to a file.
373 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000374 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000375
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000376 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000377
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000378 void setTileWidth(int width) {
379 fTileWidth = width;
380 }
381
382 int getTileWidth() const {
383 return fTileWidth;
384 }
385
386 void setTileHeight(int height) {
387 fTileHeight = height;
388 }
389
390 int getTileHeight() const {
391 return fTileHeight;
392 }
393
394 void setTileWidthPercentage(double percentage) {
395 fTileWidthPercentage = percentage;
396 }
397
keyar@chromium.org163b5672012-08-01 17:53:29 +0000398 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000399 return fTileWidthPercentage;
400 }
401
402 void setTileHeightPercentage(double percentage) {
403 fTileHeightPercentage = percentage;
404 }
405
keyar@chromium.org163b5672012-08-01 17:53:29 +0000406 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000407 return fTileHeightPercentage;
408 }
409
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000410 void setTileMinPowerOf2Width(int width) {
411 SkASSERT(SkIsPow2(width) && width > 0);
412 if (!SkIsPow2(width) || width <= 0) {
413 return;
414 }
415
416 fTileMinPowerOf2Width = width;
417 }
418
419 int getTileMinPowerOf2Width() const {
420 return fTileMinPowerOf2Width;
421 }
422
scroggo@google.comcbcef702012-12-13 22:09:28 +0000423 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
424
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000425 virtual bool supportsTimingIndividualTiles() { return true; }
426
scroggo@google.comcbcef702012-12-13 22:09:28 +0000427 /**
428 * Report the number of tiles in the x and y directions. Must not be called before init.
429 * @param x Output parameter identifying the number of tiles in the x direction.
430 * @param y Output parameter identifying the number of tiles in the y direction.
431 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
432 * unmodified.
433 */
434 bool tileDimensions(int& x, int&y);
435
436 /**
437 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
438 * for the first time.
439 * @param i Output parameter identifying the column of the next tile to be drawn on the next
440 * call to drawNextTile.
441 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
442 * to drawNextTile.
443 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
444 * is within the range of tiles. If false, i and j are unmodified.
445 */
446 bool nextTile(int& i, int& j);
447
448 /**
449 * Render one tile. This will draw the same tile each time it is called until nextTile is
450 * called. The tile rendered will depend on how many calls have been made to nextTile.
451 * It is an error to call this without first calling nextTile, or if nextTile returns false.
452 */
453 void drawCurrentTile();
454
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000455protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000456 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000457
scroggo@google.com0a049b82012-11-02 22:01:26 +0000458 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
459 virtual SkString getConfigNameInternal() SK_OVERRIDE;
460
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000461private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000462 int fTileWidth;
463 int fTileHeight;
464 double fTileWidthPercentage;
465 double fTileHeightPercentage;
466 int fTileMinPowerOf2Width;
467
468 // These variables are only used for timing individual tiles.
469 // Next tile to draw in fTileRects.
470 int fCurrentTileOffset;
471 // Number of tiles in the x direction.
472 int fTilesX;
473 // Number of tiles in the y direction.
474 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000475
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000476 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000477 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000478
479 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000480};
481
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000482class CloneData;
483
484class MultiCorePictureRenderer : public TiledPictureRenderer {
485public:
486 explicit MultiCorePictureRenderer(int threadCount);
487
488 ~MultiCorePictureRenderer();
489
490 virtual void init(SkPicture* pict) SK_OVERRIDE;
491
492 /**
493 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
494 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000495 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000496
497 virtual void end() SK_OVERRIDE;
498
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000499 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
500
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000501private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000502 virtual SkString getConfigNameInternal() SK_OVERRIDE;
503
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000504 const int fNumThreads;
505 SkTDArray<SkCanvas*> fCanvasPool;
506 SkThreadPool fThreadPool;
507 SkPicture* fPictureClones;
508 CloneData** fCloneData;
509 SkCountdown fCountdown;
510
511 typedef TiledPictureRenderer INHERITED;
512};
513
scroggo@google.com9a412522012-09-07 15:21:18 +0000514/**
515 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
516 * into an SkPicturePlayback, which we want to time.
517 */
518class PlaybackCreationRenderer : public PictureRenderer {
519public:
520 virtual void setup() SK_OVERRIDE;
521
edisonn@google.com84f548c2012-12-18 22:24:03 +0000522 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000523
524 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
525
526 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
527
528private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000529 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000530
531 virtual SkString getConfigNameInternal() SK_OVERRIDE;
532
scroggo@google.com9a412522012-09-07 15:21:18 +0000533 typedef PictureRenderer INHERITED;
534};
535
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000536extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000537extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000538
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000539}
540
541#endif // PictureRenderer_DEFINED