blob: 6a84130ac6b26a0923ee7b94615d3186118acfa2 [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,
reed@google.com881b10b2013-05-22 14:03:45 +000062 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
63 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
64 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000065 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000066 };
67
robertphillips@google.com49149312013-07-03 15:34:35 +000068 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000069 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
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000165#if SK_SUPPORT_GPU
166 void setSampleCount(int sampleCount) {
167 fSampleCount = sampleCount;
168 }
169#endif
170
caryclark@google.come3e940c2012-11-07 16:42:17 +0000171 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
172 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000173 fDrawFiltersConfig = configName;
174 }
175
junov@chromium.org9313ca42012-11-02 18:11:49 +0000176 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
177 fBBoxHierarchyType = bbhType;
178 }
179
junov@chromium.orge286e842013-03-13 17:27:16 +0000180 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
181
junov@chromium.org7b537062012-11-06 18:58:43 +0000182 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000183 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000184 }
185
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000186 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000187 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000188 }
189
scroggo@google.com9a412522012-09-07 15:21:18 +0000190 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
191
192 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
193
scroggo@google.com0a049b82012-11-02 22:01:26 +0000194 /**
195 * Reports the configuration of this PictureRenderer.
196 */
197 SkString getConfigName() {
198 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000199 if (!fViewport.isEmpty()) {
200 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
201 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000202 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
203 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000204 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
205 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000206 }
207#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000208 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000209 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000210 if (fSampleCount) {
211 config.appendf("_msaa%d", fSampleCount);
212 } else {
213 config.append("_gpu");
214 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000215 break;
216#if SK_ANGLE
217 case kAngle_DeviceType:
218 config.append("_angle");
219 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000220#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000221 default:
222 // Assume that no extra info means bitmap.
223 break;
224 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000225#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000226 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000227 return config;
228 }
229
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000230#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000231 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000232 switch (fDeviceType) {
233 case kGPU_DeviceType:
234 // fall through
235#if SK_ANGLE
236 case kAngle_DeviceType:
237#endif
238 return true;
239 default:
240 return false;
241 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000242 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000243
robertphillips@google.com6177e692013-02-28 20:16:25 +0000244 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000245 GrContextFactory::GLContextType glContextType
246 = GrContextFactory::kNull_GLContextType;
247 switch(fDeviceType) {
248 case kGPU_DeviceType:
249 glContextType = GrContextFactory::kNative_GLContextType;
250 break;
251#if SK_ANGLE
252 case kAngle_DeviceType:
253 glContextType = GrContextFactory::kANGLE_GLContextType;
254 break;
255#endif
256 default:
257 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000258 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000259 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000260 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000261
262 GrContext* getGrContext() {
263 return fGrContext;
264 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000265#endif
266
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000267 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000268 : fPicture(NULL)
269 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000270 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000271 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000272#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000273 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000274 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000275#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000276 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000277 fGridInfo.fMargin.setEmpty();
278 fGridInfo.fOffset.setZero();
279 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000280 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000281 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000282 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000283
scroggo@google.com0556ea02013-02-08 19:38:21 +0000284#if SK_SUPPORT_GPU
285 virtual ~PictureRenderer() {
286 SkSafeUnref(fGrContext);
287 }
288#endif
289
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000290protected:
291 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000292 SkPicture* fPicture;
293 SkDeviceTypes fDeviceType;
294 BBoxHierarchyType fBBoxHierarchyType;
295 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
296 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000297 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000298
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000299 void buildBBoxHierarchy();
300
301 /**
302 * Return the total width that should be drawn. If the viewport width has been set greater than
303 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
304 */
305 int getViewWidth();
306
307 /**
308 * Return the total height that should be drawn. If the viewport height has been set greater
309 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
310 */
311 int getViewHeight();
312
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000313 /**
314 * Scales the provided canvas to the scale factor set by setScaleFactor.
315 */
316 void scaleToScaleFactor(SkCanvas*);
317
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000318 SkPicture* createPicture();
319 uint32_t recordFlags();
320 SkCanvas* setupCanvas();
321 virtual SkCanvas* setupCanvas(int width, int height);
322
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000323private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000324 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000325 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000326#if SK_SUPPORT_GPU
327 GrContextFactory fGrContextFactory;
328 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000329 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000330#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000331
scroggo@google.com0a049b82012-11-02 22:01:26 +0000332 virtual SkString getConfigNameInternal() = 0;
333
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000334 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000335};
336
scroggo@google.com9a412522012-09-07 15:21:18 +0000337/**
338 * This class does not do any rendering, but its render function executes recording, which we want
339 * to time.
340 */
341class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000342 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000343
344 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
345
346 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000347
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000348protected:
349 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
350
scroggo@google.com0a049b82012-11-02 22:01:26 +0000351private:
352 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000353};
354
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000355class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000356public:
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 SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000366public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000367 virtual void init(SkPicture* pict) SK_OVERRIDE;
368
edisonn@google.com84f548c2012-12-18 22:24:03 +0000369 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000370
371private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000372 virtual SkString getConfigNameInternal() SK_OVERRIDE;
373
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000374 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000375};
376
377class TiledPictureRenderer : public PictureRenderer {
378public:
379 TiledPictureRenderer();
380
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000381 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000382
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000383 /**
384 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
385 * created for each tile, named "path0.png", "path1.png", etc.
386 * Multithreaded mode currently does not support writing to a file.
387 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000388 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000389
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000390 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000391
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000392 void setTileWidth(int width) {
393 fTileWidth = width;
394 }
395
396 int getTileWidth() const {
397 return fTileWidth;
398 }
399
400 void setTileHeight(int height) {
401 fTileHeight = height;
402 }
403
404 int getTileHeight() const {
405 return fTileHeight;
406 }
407
408 void setTileWidthPercentage(double percentage) {
409 fTileWidthPercentage = percentage;
410 }
411
keyar@chromium.org163b5672012-08-01 17:53:29 +0000412 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000413 return fTileWidthPercentage;
414 }
415
416 void setTileHeightPercentage(double percentage) {
417 fTileHeightPercentage = percentage;
418 }
419
keyar@chromium.org163b5672012-08-01 17:53:29 +0000420 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000421 return fTileHeightPercentage;
422 }
423
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000424 void setTileMinPowerOf2Width(int width) {
425 SkASSERT(SkIsPow2(width) && width > 0);
426 if (!SkIsPow2(width) || width <= 0) {
427 return;
428 }
429
430 fTileMinPowerOf2Width = width;
431 }
432
433 int getTileMinPowerOf2Width() const {
434 return fTileMinPowerOf2Width;
435 }
436
scroggo@google.comcbcef702012-12-13 22:09:28 +0000437 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
438
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000439 virtual bool supportsTimingIndividualTiles() { return true; }
440
scroggo@google.comcbcef702012-12-13 22:09:28 +0000441 /**
442 * Report the number of tiles in the x and y directions. Must not be called before init.
443 * @param x Output parameter identifying the number of tiles in the x direction.
444 * @param y Output parameter identifying the number of tiles in the y direction.
445 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
446 * unmodified.
447 */
448 bool tileDimensions(int& x, int&y);
449
450 /**
451 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
452 * for the first time.
453 * @param i Output parameter identifying the column of the next tile to be drawn on the next
454 * call to drawNextTile.
455 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
456 * to drawNextTile.
457 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
458 * is within the range of tiles. If false, i and j are unmodified.
459 */
460 bool nextTile(int& i, int& j);
461
462 /**
463 * Render one tile. This will draw the same tile each time it is called until nextTile is
464 * called. The tile rendered will depend on how many calls have been made to nextTile.
465 * It is an error to call this without first calling nextTile, or if nextTile returns false.
466 */
467 void drawCurrentTile();
468
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000469protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000470 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000471
scroggo@google.com0a049b82012-11-02 22:01:26 +0000472 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
473 virtual SkString getConfigNameInternal() SK_OVERRIDE;
474
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000475private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000476 int fTileWidth;
477 int fTileHeight;
478 double fTileWidthPercentage;
479 double fTileHeightPercentage;
480 int fTileMinPowerOf2Width;
481
482 // These variables are only used for timing individual tiles.
483 // Next tile to draw in fTileRects.
484 int fCurrentTileOffset;
485 // Number of tiles in the x direction.
486 int fTilesX;
487 // Number of tiles in the y direction.
488 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000489
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000490 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000491 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000492
493 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000494};
495
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000496class CloneData;
497
498class MultiCorePictureRenderer : public TiledPictureRenderer {
499public:
500 explicit MultiCorePictureRenderer(int threadCount);
501
502 ~MultiCorePictureRenderer();
503
504 virtual void init(SkPicture* pict) SK_OVERRIDE;
505
506 /**
507 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
508 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000509 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000510
511 virtual void end() SK_OVERRIDE;
512
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000513 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
514
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000515private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000516 virtual SkString getConfigNameInternal() SK_OVERRIDE;
517
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000518 const int fNumThreads;
519 SkTDArray<SkCanvas*> fCanvasPool;
520 SkThreadPool fThreadPool;
521 SkPicture* fPictureClones;
522 CloneData** fCloneData;
523 SkCountdown fCountdown;
524
525 typedef TiledPictureRenderer INHERITED;
526};
527
scroggo@google.com9a412522012-09-07 15:21:18 +0000528/**
529 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
530 * into an SkPicturePlayback, which we want to time.
531 */
532class PlaybackCreationRenderer : public PictureRenderer {
533public:
534 virtual void setup() SK_OVERRIDE;
535
edisonn@google.com84f548c2012-12-18 22:24:03 +0000536 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000537
538 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
539
540 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
541
542private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000543 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000544
545 virtual SkString getConfigNameInternal() SK_OVERRIDE;
546
scroggo@google.com9a412522012-09-07 15:21:18 +0000547 typedef PictureRenderer INHERITED;
548};
549
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000550extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000551extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000552
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000553}
554
555#endif // PictureRenderer_DEFINED