blob: 6d886abfdc52b105f392d8c037a0343bd012362a [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 /**
151 * Set the backend type. Returns true on success and false on failure.
152 */
153 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000154 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000155#if SK_SUPPORT_GPU
156 // In case this function is called more than once
157 SkSafeUnref(fGrContext);
158 fGrContext = NULL;
159 // Set to Native so it will have an initial value.
160 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
161#endif
162 switch(deviceType) {
163 case kBitmap_DeviceType:
164 return true;
165#if SK_SUPPORT_GPU
166 case kGPU_DeviceType:
167 // Already set to GrContextFactory::kNative_GLContextType, above.
168 break;
169#if SK_ANGLE
170 case kAngle_DeviceType:
171 glContextType = GrContextFactory::kANGLE_GLContextType;
172 break;
173#endif
174#endif
175 default:
176 // Invalid device type.
177 return false;
178 }
179#if SK_SUPPORT_GPU
180 fGrContext = fGrContextFactory.get(glContextType);
181 if (NULL == fGrContext) {
182 return false;
183 } else {
184 fGrContext->ref();
185 return true;
186 }
187#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000188 }
189
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000190#if SK_SUPPORT_GPU
191 void setSampleCount(int sampleCount) {
192 fSampleCount = sampleCount;
193 }
194#endif
195
caryclark@google.come3e940c2012-11-07 16:42:17 +0000196 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
197 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000198 fDrawFiltersConfig = configName;
199 }
200
junov@chromium.org9313ca42012-11-02 18:11:49 +0000201 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
202 fBBoxHierarchyType = bbhType;
203 }
204
junov@chromium.orge286e842013-03-13 17:27:16 +0000205 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
206
junov@chromium.org7b537062012-11-06 18:58:43 +0000207 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000208 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000209 }
210
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000211 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
212 fJsonSummaryPtr = jsonSummaryPtr;
213 }
214
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000215 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000216 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000217 }
218
scroggo@google.com9a412522012-09-07 15:21:18 +0000219 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
220
221 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
222
scroggo@google.com0a049b82012-11-02 22:01:26 +0000223 /**
224 * Reports the configuration of this PictureRenderer.
225 */
226 SkString getConfigName() {
227 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000228 if (!fViewport.isEmpty()) {
229 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
230 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000231 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
232 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000233 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
234 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000235 }
236#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000237 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000238 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000239 if (fSampleCount) {
240 config.appendf("_msaa%d", fSampleCount);
241 } else {
242 config.append("_gpu");
243 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000244 break;
245#if SK_ANGLE
246 case kAngle_DeviceType:
247 config.append("_angle");
248 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000249#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000250 default:
251 // Assume that no extra info means bitmap.
252 break;
253 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000254#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000255 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000256 return config;
257 }
258
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000259#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000260 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000261 switch (fDeviceType) {
262 case kGPU_DeviceType:
263 // fall through
264#if SK_ANGLE
265 case kAngle_DeviceType:
266#endif
267 return true;
268 default:
269 return false;
270 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000271 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000272
robertphillips@google.com6177e692013-02-28 20:16:25 +0000273 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000274 GrContextFactory::GLContextType glContextType
275 = GrContextFactory::kNull_GLContextType;
276 switch(fDeviceType) {
277 case kGPU_DeviceType:
278 glContextType = GrContextFactory::kNative_GLContextType;
279 break;
280#if SK_ANGLE
281 case kAngle_DeviceType:
282 glContextType = GrContextFactory::kANGLE_GLContextType;
283 break;
284#endif
285 default:
286 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000287 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000288 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000289 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000290
291 GrContext* getGrContext() {
292 return fGrContext;
293 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000294#endif
295
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000296 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000297 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000298 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000299 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000300 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000301 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000302#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000303 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000304 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000305#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000306 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000307 fGridInfo.fMargin.setEmpty();
308 fGridInfo.fOffset.setZero();
309 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000310 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000311 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000312 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000313
scroggo@google.com0556ea02013-02-08 19:38:21 +0000314#if SK_SUPPORT_GPU
315 virtual ~PictureRenderer() {
316 SkSafeUnref(fGrContext);
317 }
318#endif
319
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000320protected:
321 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000322 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000323 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000324 SkDeviceTypes fDeviceType;
325 BBoxHierarchyType fBBoxHierarchyType;
326 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
327 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000328 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000329
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000330 void buildBBoxHierarchy();
331
332 /**
333 * Return the total width that should be drawn. If the viewport width has been set greater than
334 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
335 */
336 int getViewWidth();
337
338 /**
339 * Return the total height that should be drawn. If the viewport height has been set greater
340 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
341 */
342 int getViewHeight();
343
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000344 /**
345 * Scales the provided canvas to the scale factor set by setScaleFactor.
346 */
347 void scaleToScaleFactor(SkCanvas*);
348
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000349 SkPicture* createPicture();
350 uint32_t recordFlags();
351 SkCanvas* setupCanvas();
352 virtual SkCanvas* setupCanvas(int width, int height);
353
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000354private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000355 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000356 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000357#if SK_SUPPORT_GPU
358 GrContextFactory fGrContextFactory;
359 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000360 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000361#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000362
scroggo@google.com0a049b82012-11-02 22:01:26 +0000363 virtual SkString getConfigNameInternal() = 0;
364
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000365 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000366};
367
scroggo@google.com9a412522012-09-07 15:21:18 +0000368/**
369 * This class does not do any rendering, but its render function executes recording, which we want
370 * to time.
371 */
372class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000373 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000374
375 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
376
377 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000378
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000379protected:
380 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
381
scroggo@google.com0a049b82012-11-02 22:01:26 +0000382private:
383 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000384};
385
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000386class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000387public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000388 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000389
390private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000391 virtual SkString getConfigNameInternal() SK_OVERRIDE;
392
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000393 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000394};
395
396class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000397public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000398 virtual void init(SkPicture* pict) SK_OVERRIDE;
399
edisonn@google.com84f548c2012-12-18 22:24:03 +0000400 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000401
402private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000403 virtual SkString getConfigNameInternal() SK_OVERRIDE;
404
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000405 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000406};
407
408class TiledPictureRenderer : public PictureRenderer {
409public:
410 TiledPictureRenderer();
411
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000412 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000413
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000414 /**
415 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
416 * created for each tile, named "path0.png", "path1.png", etc.
417 * Multithreaded mode currently does not support writing to a file.
418 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000419 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000420
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000421 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000422
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000423 void setTileWidth(int width) {
424 fTileWidth = width;
425 }
426
427 int getTileWidth() const {
428 return fTileWidth;
429 }
430
431 void setTileHeight(int height) {
432 fTileHeight = height;
433 }
434
435 int getTileHeight() const {
436 return fTileHeight;
437 }
438
439 void setTileWidthPercentage(double percentage) {
440 fTileWidthPercentage = percentage;
441 }
442
keyar@chromium.org163b5672012-08-01 17:53:29 +0000443 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000444 return fTileWidthPercentage;
445 }
446
447 void setTileHeightPercentage(double percentage) {
448 fTileHeightPercentage = percentage;
449 }
450
keyar@chromium.org163b5672012-08-01 17:53:29 +0000451 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000452 return fTileHeightPercentage;
453 }
454
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000455 void setTileMinPowerOf2Width(int width) {
456 SkASSERT(SkIsPow2(width) && width > 0);
457 if (!SkIsPow2(width) || width <= 0) {
458 return;
459 }
460
461 fTileMinPowerOf2Width = width;
462 }
463
464 int getTileMinPowerOf2Width() const {
465 return fTileMinPowerOf2Width;
466 }
467
scroggo@google.comcbcef702012-12-13 22:09:28 +0000468 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
469
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000470 virtual bool supportsTimingIndividualTiles() { return true; }
471
scroggo@google.comcbcef702012-12-13 22:09:28 +0000472 /**
473 * Report the number of tiles in the x and y directions. Must not be called before init.
474 * @param x Output parameter identifying the number of tiles in the x direction.
475 * @param y Output parameter identifying the number of tiles in the y direction.
476 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
477 * unmodified.
478 */
479 bool tileDimensions(int& x, int&y);
480
481 /**
482 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
483 * for the first time.
484 * @param i Output parameter identifying the column of the next tile to be drawn on the next
485 * call to drawNextTile.
486 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
487 * to drawNextTile.
488 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
489 * is within the range of tiles. If false, i and j are unmodified.
490 */
491 bool nextTile(int& i, int& j);
492
493 /**
494 * Render one tile. This will draw the same tile each time it is called until nextTile is
495 * called. The tile rendered will depend on how many calls have been made to nextTile.
496 * It is an error to call this without first calling nextTile, or if nextTile returns false.
497 */
498 void drawCurrentTile();
499
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000500protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000501 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000502
scroggo@google.com0a049b82012-11-02 22:01:26 +0000503 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
504 virtual SkString getConfigNameInternal() SK_OVERRIDE;
505
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000506private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000507 int fTileWidth;
508 int fTileHeight;
509 double fTileWidthPercentage;
510 double fTileHeightPercentage;
511 int fTileMinPowerOf2Width;
512
513 // These variables are only used for timing individual tiles.
514 // Next tile to draw in fTileRects.
515 int fCurrentTileOffset;
516 // Number of tiles in the x direction.
517 int fTilesX;
518 // Number of tiles in the y direction.
519 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000520
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000521 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000522 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000523
524 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000525};
526
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000527class CloneData;
528
529class MultiCorePictureRenderer : public TiledPictureRenderer {
530public:
531 explicit MultiCorePictureRenderer(int threadCount);
532
533 ~MultiCorePictureRenderer();
534
535 virtual void init(SkPicture* pict) SK_OVERRIDE;
536
537 /**
538 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
539 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000540 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000541
542 virtual void end() SK_OVERRIDE;
543
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000544 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
545
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000546private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000547 virtual SkString getConfigNameInternal() SK_OVERRIDE;
548
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000549 const int fNumThreads;
550 SkTDArray<SkCanvas*> fCanvasPool;
551 SkThreadPool fThreadPool;
552 SkPicture* fPictureClones;
553 CloneData** fCloneData;
554 SkCountdown fCountdown;
555
556 typedef TiledPictureRenderer INHERITED;
557};
558
scroggo@google.com9a412522012-09-07 15:21:18 +0000559/**
560 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
561 * into an SkPicturePlayback, which we want to time.
562 */
563class PlaybackCreationRenderer : public PictureRenderer {
564public:
565 virtual void setup() SK_OVERRIDE;
566
edisonn@google.com84f548c2012-12-18 22:24:03 +0000567 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000568
569 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
570
571 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
572
573private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000574 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000575
576 virtual SkString getConfigNameInternal() SK_OVERRIDE;
577
scroggo@google.com9a412522012-09-07 15:21:18 +0000578 typedef PictureRenderer INHERITED;
579};
580
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000581extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000582extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000583
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000584}
585
586#endif // PictureRenderer_DEFINED