blob: 94912c73adbbad40ee31fd11aecabedd5ccf6821 [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
rmistry@google.com6ab96732014-01-06 18:37:24 +000072#if SK_MESA
73 kMesa_DeviceType,
74#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000075 kBitmap_DeviceType,
76#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000077 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000078 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000079#endif
80 };
81
junov@chromium.org9313ca42012-11-02 18:11:49 +000082 enum BBoxHierarchyType {
83 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000084 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000085 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000086 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000087
88 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000089 };
90
caryclark@google.coma3622372012-11-06 21:26:13 +000091 // this uses SkPaint::Flags as a base and adds additional flags
92 enum DrawFilterFlags {
93 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000094 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
95 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
96 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000097 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000098 };
99
robertphillips@google.com49149312013-07-03 15:34:35 +0000100 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +0000101 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
102 hinting_flag_must_be_greater);
103 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
104 slight_hinting_flag_must_be_greater);
105
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000106 /**
107 * Called with each new SkPicture to render.
108 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000109 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +0000110
111 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000112 * Set the viewport so that only the portion listed gets drawn.
113 */
114 void setViewport(SkISize size) { fViewport = size; }
115
116 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000117 * Set the scale factor at which draw the picture.
118 */
119 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
120
121 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000122 * Perform any setup that should done prior to each iteration of render() which should not be
123 * timed.
124 */
125 virtual void setup() {}
126
127 /**
128 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
129 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000130 * If path is non-null, subclass implementations should call write().
131 * @param path If non-null, also write the output to the file specified by path. path should
132 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000133 * @return bool True if rendering succeeded and, if path is non-null, the output was
134 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000135 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000136 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000137
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000138 /**
139 * Called once finished with a particular SkPicture, before calling init again, and before
140 * being done with this Renderer.
141 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000142 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000143
scroggo@google.comcbcef702012-12-13 22:09:28 +0000144 /**
145 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
146 * TiledPictureRender so its methods can be called.
147 */
148 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
149
scroggo@google.com08085f82013-01-28 20:40:24 +0000150 /**
151 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
152 * flush, and calls finish if callFinish is true.
153 * @param callFinish Whether to call finish.
154 */
155 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000156
scroggo@google.com0556ea02013-02-08 19:38:21 +0000157 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000158 * Remove all decoded textures from the CPU caches and all uploaded textures
159 * from the GPU.
160 */
161 void purgeTextures();
162
163 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000164 * Set the backend type. Returns true on success and false on failure.
165 */
166 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000167 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000168#if SK_SUPPORT_GPU
169 // In case this function is called more than once
170 SkSafeUnref(fGrContext);
171 fGrContext = NULL;
172 // Set to Native so it will have an initial value.
173 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
174#endif
175 switch(deviceType) {
176 case kBitmap_DeviceType:
177 return true;
178#if SK_SUPPORT_GPU
179 case kGPU_DeviceType:
180 // Already set to GrContextFactory::kNative_GLContextType, above.
181 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000182 case kNVPR_DeviceType:
183 glContextType = GrContextFactory::kNVPR_GLContextType;
184 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000185#if SK_ANGLE
186 case kAngle_DeviceType:
187 glContextType = GrContextFactory::kANGLE_GLContextType;
188 break;
189#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000190#if SK_MESA
191 case kMesa_DeviceType:
192 glContextType = GrContextFactory::kMESA_GLContextType;
193 break;
194#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000195#endif
196 default:
197 // Invalid device type.
198 return false;
199 }
200#if SK_SUPPORT_GPU
201 fGrContext = fGrContextFactory.get(glContextType);
202 if (NULL == fGrContext) {
203 return false;
204 } else {
205 fGrContext->ref();
206 return true;
207 }
208#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000209 }
210
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000211#if SK_SUPPORT_GPU
212 void setSampleCount(int sampleCount) {
213 fSampleCount = sampleCount;
214 }
215#endif
216
caryclark@google.come3e940c2012-11-07 16:42:17 +0000217 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
218 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000219 fDrawFiltersConfig = configName;
220 }
221
junov@chromium.org9313ca42012-11-02 18:11:49 +0000222 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
223 fBBoxHierarchyType = bbhType;
224 }
225
junov@chromium.orge286e842013-03-13 17:27:16 +0000226 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
227
junov@chromium.org7b537062012-11-06 18:58:43 +0000228 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000229 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000230 }
231
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000232 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
233 fJsonSummaryPtr = jsonSummaryPtr;
234 }
235
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000236 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000237 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000238 }
239
scroggo@google.com9a412522012-09-07 15:21:18 +0000240 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
241
242 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
243
scroggo@google.com0a049b82012-11-02 22:01:26 +0000244 /**
245 * Reports the configuration of this PictureRenderer.
246 */
247 SkString getConfigName() {
248 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000249 if (!fViewport.isEmpty()) {
250 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
251 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000252 if (fScaleFactor != SK_Scalar1) {
253 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
254 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000255 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
256 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000257 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
258 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000259 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
260 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000261 }
262#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000263 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000264 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000265 if (fSampleCount) {
266 config.appendf("_msaa%d", fSampleCount);
267 } else {
268 config.append("_gpu");
269 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000270 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000271 case kNVPR_DeviceType:
272 config.appendf("_nvprmsaa%d", fSampleCount);
273 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000274#if SK_ANGLE
275 case kAngle_DeviceType:
276 config.append("_angle");
277 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000278#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000279#if SK_MESA
280 case kMesa_DeviceType:
281 config.append("_mesa");
282 break;
283#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000284 default:
285 // Assume that no extra info means bitmap.
286 break;
287 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000288#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000289 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000290 return config;
291 }
292
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000293#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000294 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295 switch (fDeviceType) {
296 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000297 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000298 // fall through
299#if SK_ANGLE
300 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000301 // fall through
302#endif
303#if SK_MESA
304 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000305#endif
306 return true;
307 default:
308 return false;
309 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000310 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000311
robertphillips@google.com6177e692013-02-28 20:16:25 +0000312 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000313 GrContextFactory::GLContextType glContextType
314 = GrContextFactory::kNull_GLContextType;
315 switch(fDeviceType) {
316 case kGPU_DeviceType:
317 glContextType = GrContextFactory::kNative_GLContextType;
318 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000319 case kNVPR_DeviceType:
320 glContextType = GrContextFactory::kNVPR_GLContextType;
321 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000322#if SK_ANGLE
323 case kAngle_DeviceType:
324 glContextType = GrContextFactory::kANGLE_GLContextType;
325 break;
326#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000327#if SK_MESA
328 case kMesa_DeviceType:
329 glContextType = GrContextFactory::kMESA_GLContextType;
330 break;
331#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000332 default:
333 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000334 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000335 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000336 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000337
338 GrContext* getGrContext() {
339 return fGrContext;
340 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000341#endif
342
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000343 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000344 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000345 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000346 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000347 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000348 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000349#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000350 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000351 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000352#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000353 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000354 fGridInfo.fMargin.setEmpty();
355 fGridInfo.fOffset.setZero();
356 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000357 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000358 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000359 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000360
scroggo@google.com0556ea02013-02-08 19:38:21 +0000361#if SK_SUPPORT_GPU
362 virtual ~PictureRenderer() {
363 SkSafeUnref(fGrContext);
364 }
365#endif
366
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000367protected:
368 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000369 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000370 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000371 SkDeviceTypes fDeviceType;
372 BBoxHierarchyType fBBoxHierarchyType;
373 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
374 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000375 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000376
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000377 void buildBBoxHierarchy();
378
379 /**
380 * Return the total width that should be drawn. If the viewport width has been set greater than
381 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
382 */
383 int getViewWidth();
384
385 /**
386 * Return the total height that should be drawn. If the viewport height has been set greater
387 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
388 */
389 int getViewHeight();
390
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000391 /**
392 * Scales the provided canvas to the scale factor set by setScaleFactor.
393 */
394 void scaleToScaleFactor(SkCanvas*);
395
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000396 SkPicture* createPicture();
397 uint32_t recordFlags();
398 SkCanvas* setupCanvas();
399 virtual SkCanvas* setupCanvas(int width, int height);
400
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000401private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000402 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000403 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000404#if SK_SUPPORT_GPU
405 GrContextFactory fGrContextFactory;
406 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000407 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000408#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000409
scroggo@google.com0a049b82012-11-02 22:01:26 +0000410 virtual SkString getConfigNameInternal() = 0;
411
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000412 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000413};
414
scroggo@google.com9a412522012-09-07 15:21:18 +0000415/**
416 * This class does not do any rendering, but its render function executes recording, which we want
417 * to time.
418 */
419class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000420 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000421
422 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
423
424 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000425
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000426protected:
427 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
428
scroggo@google.com0a049b82012-11-02 22:01:26 +0000429private:
430 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000431};
432
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000433class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000434public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000435 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000436
437private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000438 virtual SkString getConfigNameInternal() SK_OVERRIDE;
439
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000440 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000441};
442
443class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000444public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000445 virtual void init(SkPicture* pict) SK_OVERRIDE;
446
edisonn@google.com84f548c2012-12-18 22:24:03 +0000447 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000448
449private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000450 virtual SkString getConfigNameInternal() SK_OVERRIDE;
451
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000452 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000453};
454
455class TiledPictureRenderer : public PictureRenderer {
456public:
457 TiledPictureRenderer();
458
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000459 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000460
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000461 /**
462 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
463 * created for each tile, named "path0.png", "path1.png", etc.
464 * Multithreaded mode currently does not support writing to a file.
465 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000466 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000467
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000468 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000469
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000470 void setTileWidth(int width) {
471 fTileWidth = width;
472 }
473
474 int getTileWidth() const {
475 return fTileWidth;
476 }
477
478 void setTileHeight(int height) {
479 fTileHeight = height;
480 }
481
482 int getTileHeight() const {
483 return fTileHeight;
484 }
485
486 void setTileWidthPercentage(double percentage) {
487 fTileWidthPercentage = percentage;
488 }
489
keyar@chromium.org163b5672012-08-01 17:53:29 +0000490 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000491 return fTileWidthPercentage;
492 }
493
494 void setTileHeightPercentage(double percentage) {
495 fTileHeightPercentage = percentage;
496 }
497
keyar@chromium.org163b5672012-08-01 17:53:29 +0000498 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000499 return fTileHeightPercentage;
500 }
501
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000502 void setTileMinPowerOf2Width(int width) {
503 SkASSERT(SkIsPow2(width) && width > 0);
504 if (!SkIsPow2(width) || width <= 0) {
505 return;
506 }
507
508 fTileMinPowerOf2Width = width;
509 }
510
511 int getTileMinPowerOf2Width() const {
512 return fTileMinPowerOf2Width;
513 }
514
scroggo@google.comcbcef702012-12-13 22:09:28 +0000515 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
516
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000517 virtual bool supportsTimingIndividualTiles() { return true; }
518
scroggo@google.comcbcef702012-12-13 22:09:28 +0000519 /**
520 * Report the number of tiles in the x and y directions. Must not be called before init.
521 * @param x Output parameter identifying the number of tiles in the x direction.
522 * @param y Output parameter identifying the number of tiles in the y direction.
523 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
524 * unmodified.
525 */
526 bool tileDimensions(int& x, int&y);
527
528 /**
529 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
530 * for the first time.
531 * @param i Output parameter identifying the column of the next tile to be drawn on the next
532 * call to drawNextTile.
533 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
534 * to drawNextTile.
535 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
536 * is within the range of tiles. If false, i and j are unmodified.
537 */
538 bool nextTile(int& i, int& j);
539
540 /**
541 * Render one tile. This will draw the same tile each time it is called until nextTile is
542 * called. The tile rendered will depend on how many calls have been made to nextTile.
543 * It is an error to call this without first calling nextTile, or if nextTile returns false.
544 */
545 void drawCurrentTile();
546
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000547protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000548 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000549
scroggo@google.com0a049b82012-11-02 22:01:26 +0000550 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
551 virtual SkString getConfigNameInternal() SK_OVERRIDE;
552
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000553private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000554 int fTileWidth;
555 int fTileHeight;
556 double fTileWidthPercentage;
557 double fTileHeightPercentage;
558 int fTileMinPowerOf2Width;
559
560 // These variables are only used for timing individual tiles.
561 // Next tile to draw in fTileRects.
562 int fCurrentTileOffset;
563 // Number of tiles in the x direction.
564 int fTilesX;
565 // Number of tiles in the y direction.
566 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000567
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000568 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000569 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000570
571 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000572};
573
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000574class CloneData;
575
576class MultiCorePictureRenderer : public TiledPictureRenderer {
577public:
578 explicit MultiCorePictureRenderer(int threadCount);
579
580 ~MultiCorePictureRenderer();
581
582 virtual void init(SkPicture* pict) SK_OVERRIDE;
583
584 /**
585 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
586 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000587 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000588
589 virtual void end() SK_OVERRIDE;
590
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000591 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
592
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000593private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000594 virtual SkString getConfigNameInternal() SK_OVERRIDE;
595
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000596 const int fNumThreads;
597 SkTDArray<SkCanvas*> fCanvasPool;
598 SkThreadPool fThreadPool;
599 SkPicture* fPictureClones;
600 CloneData** fCloneData;
601 SkCountdown fCountdown;
602
603 typedef TiledPictureRenderer INHERITED;
604};
605
scroggo@google.com9a412522012-09-07 15:21:18 +0000606/**
607 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
608 * into an SkPicturePlayback, which we want to time.
609 */
610class PlaybackCreationRenderer : public PictureRenderer {
611public:
612 virtual void setup() SK_OVERRIDE;
613
edisonn@google.com84f548c2012-12-18 22:24:03 +0000614 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000615
616 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
617
618 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
619
620private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000621 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000622
623 virtual SkString getConfigNameInternal() SK_OVERRIDE;
624
scroggo@google.com9a412522012-09-07 15:21:18 +0000625 typedef PictureRenderer INHERITED;
626};
627
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000628extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000629extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000630
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000631}
632
633#endif // PictureRenderer_DEFINED