blob: 5d30e5b73362041d15e501a1e5624d464ecc58a6 [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,
junov@chromium.org9313ca42012-11-02 18:11:49 +000087 };
88
caryclark@google.coma3622372012-11-06 21:26:13 +000089 // this uses SkPaint::Flags as a base and adds additional flags
90 enum DrawFilterFlags {
91 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000092 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
93 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
94 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000095 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000096 };
97
robertphillips@google.com49149312013-07-03 15:34:35 +000098 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000099 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
100 hinting_flag_must_be_greater);
101 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
102 slight_hinting_flag_must_be_greater);
103
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000104 /**
105 * Called with each new SkPicture to render.
106 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000107 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +0000108
109 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000110 * Set the viewport so that only the portion listed gets drawn.
111 */
112 void setViewport(SkISize size) { fViewport = size; }
113
114 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000115 * Set the scale factor at which draw the picture.
116 */
117 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
118
119 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000120 * Perform any setup that should done prior to each iteration of render() which should not be
121 * timed.
122 */
123 virtual void setup() {}
124
125 /**
126 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
127 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000128 * If path is non-null, subclass implementations should call write().
129 * @param path If non-null, also write the output to the file specified by path. path should
130 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000131 * @return bool True if rendering succeeded and, if path is non-null, the output was
132 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000133 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000134 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000135
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000136 /**
137 * Called once finished with a particular SkPicture, before calling init again, and before
138 * being done with this Renderer.
139 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000140 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000141
scroggo@google.comcbcef702012-12-13 22:09:28 +0000142 /**
143 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
144 * TiledPictureRender so its methods can be called.
145 */
146 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
147
scroggo@google.com08085f82013-01-28 20:40:24 +0000148 /**
149 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
150 * flush, and calls finish if callFinish is true.
151 * @param callFinish Whether to call finish.
152 */
153 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000154
scroggo@google.com0556ea02013-02-08 19:38:21 +0000155 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000156 * Remove all decoded textures from the CPU caches and all uploaded textures
157 * from the GPU.
158 */
159 void purgeTextures();
160
161 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000162 * Set the backend type. Returns true on success and false on failure.
163 */
164 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000165 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000166#if SK_SUPPORT_GPU
167 // In case this function is called more than once
168 SkSafeUnref(fGrContext);
169 fGrContext = NULL;
170 // Set to Native so it will have an initial value.
171 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
172#endif
173 switch(deviceType) {
174 case kBitmap_DeviceType:
175 return true;
176#if SK_SUPPORT_GPU
177 case kGPU_DeviceType:
178 // Already set to GrContextFactory::kNative_GLContextType, above.
179 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000180 case kNVPR_DeviceType:
181 glContextType = GrContextFactory::kNVPR_GLContextType;
182 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000183#if SK_ANGLE
184 case kAngle_DeviceType:
185 glContextType = GrContextFactory::kANGLE_GLContextType;
186 break;
187#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000188#if SK_MESA
189 case kMesa_DeviceType:
190 glContextType = GrContextFactory::kMESA_GLContextType;
191 break;
192#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000193#endif
194 default:
195 // Invalid device type.
196 return false;
197 }
198#if SK_SUPPORT_GPU
199 fGrContext = fGrContextFactory.get(glContextType);
200 if (NULL == fGrContext) {
201 return false;
202 } else {
203 fGrContext->ref();
204 return true;
205 }
206#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000207 }
208
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000209#if SK_SUPPORT_GPU
210 void setSampleCount(int sampleCount) {
211 fSampleCount = sampleCount;
212 }
213#endif
214
caryclark@google.come3e940c2012-11-07 16:42:17 +0000215 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
216 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000217 fDrawFiltersConfig = configName;
218 }
219
junov@chromium.org9313ca42012-11-02 18:11:49 +0000220 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
221 fBBoxHierarchyType = bbhType;
222 }
223
junov@chromium.orge286e842013-03-13 17:27:16 +0000224 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
225
junov@chromium.org7b537062012-11-06 18:58:43 +0000226 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000227 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000228 }
229
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000230 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
231 fJsonSummaryPtr = jsonSummaryPtr;
232 }
233
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000234 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000235 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000236 }
237
scroggo@google.com9a412522012-09-07 15:21:18 +0000238 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
239
240 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
241
scroggo@google.com0a049b82012-11-02 22:01:26 +0000242 /**
243 * Reports the configuration of this PictureRenderer.
244 */
245 SkString getConfigName() {
246 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000247 if (!fViewport.isEmpty()) {
248 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
249 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000250 if (fScaleFactor != SK_Scalar1) {
251 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
252 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000253 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
254 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000255 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
256 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000257 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
258 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000259 }
260#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000261 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000262 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000263 if (fSampleCount) {
264 config.appendf("_msaa%d", fSampleCount);
265 } else {
266 config.append("_gpu");
267 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000268 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000269 case kNVPR_DeviceType:
270 config.appendf("_nvprmsaa%d", fSampleCount);
271 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000272#if SK_ANGLE
273 case kAngle_DeviceType:
274 config.append("_angle");
275 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000276#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000277#if SK_MESA
278 case kMesa_DeviceType:
279 config.append("_mesa");
280 break;
281#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000282 default:
283 // Assume that no extra info means bitmap.
284 break;
285 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000286#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000287 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000288 return config;
289 }
290
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000291#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000292 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000293 switch (fDeviceType) {
294 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000295 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000296 // fall through
297#if SK_ANGLE
298 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000299 // fall through
300#endif
301#if SK_MESA
302 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000303#endif
304 return true;
305 default:
306 return false;
307 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000308 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000309
robertphillips@google.com6177e692013-02-28 20:16:25 +0000310 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000311 GrContextFactory::GLContextType glContextType
312 = GrContextFactory::kNull_GLContextType;
313 switch(fDeviceType) {
314 case kGPU_DeviceType:
315 glContextType = GrContextFactory::kNative_GLContextType;
316 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000317 case kNVPR_DeviceType:
318 glContextType = GrContextFactory::kNVPR_GLContextType;
319 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000320#if SK_ANGLE
321 case kAngle_DeviceType:
322 glContextType = GrContextFactory::kANGLE_GLContextType;
323 break;
324#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000325#if SK_MESA
326 case kMesa_DeviceType:
327 glContextType = GrContextFactory::kMESA_GLContextType;
328 break;
329#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000330 default:
331 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000332 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000333 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000334 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000335
336 GrContext* getGrContext() {
337 return fGrContext;
338 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000339#endif
340
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000341 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000342 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000343 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000344 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000345 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000346 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000347#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000348 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000349 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000350#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000351 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000352 fGridInfo.fMargin.setEmpty();
353 fGridInfo.fOffset.setZero();
354 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000355 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000356 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000357 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000358
scroggo@google.com0556ea02013-02-08 19:38:21 +0000359#if SK_SUPPORT_GPU
360 virtual ~PictureRenderer() {
361 SkSafeUnref(fGrContext);
362 }
363#endif
364
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000365protected:
366 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000367 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000368 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000369 SkDeviceTypes fDeviceType;
370 BBoxHierarchyType fBBoxHierarchyType;
371 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
372 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000373 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000374
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000375 void buildBBoxHierarchy();
376
377 /**
378 * Return the total width that should be drawn. If the viewport width has been set greater than
379 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
380 */
381 int getViewWidth();
382
383 /**
384 * Return the total height that should be drawn. If the viewport height has been set greater
385 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
386 */
387 int getViewHeight();
388
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000389 /**
390 * Scales the provided canvas to the scale factor set by setScaleFactor.
391 */
392 void scaleToScaleFactor(SkCanvas*);
393
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000394 SkPicture* createPicture();
395 uint32_t recordFlags();
396 SkCanvas* setupCanvas();
397 virtual SkCanvas* setupCanvas(int width, int height);
398
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000399private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000400 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000401 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000402#if SK_SUPPORT_GPU
403 GrContextFactory fGrContextFactory;
404 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000405 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000406#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000407
scroggo@google.com0a049b82012-11-02 22:01:26 +0000408 virtual SkString getConfigNameInternal() = 0;
409
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000410 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000411};
412
scroggo@google.com9a412522012-09-07 15:21:18 +0000413/**
414 * This class does not do any rendering, but its render function executes recording, which we want
415 * to time.
416 */
417class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000418 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000419
420 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
421
422 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000423
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000424protected:
425 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
426
scroggo@google.com0a049b82012-11-02 22:01:26 +0000427private:
428 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000429};
430
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000431class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000432public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000433 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000434
435private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000436 virtual SkString getConfigNameInternal() SK_OVERRIDE;
437
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000438 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000439};
440
441class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000442public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000443 virtual void init(SkPicture* pict) SK_OVERRIDE;
444
edisonn@google.com84f548c2012-12-18 22:24:03 +0000445 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000446
447private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000448 virtual SkString getConfigNameInternal() SK_OVERRIDE;
449
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000450 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000451};
452
453class TiledPictureRenderer : public PictureRenderer {
454public:
455 TiledPictureRenderer();
456
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000457 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000458
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000459 /**
460 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
461 * created for each tile, named "path0.png", "path1.png", etc.
462 * Multithreaded mode currently does not support writing to a file.
463 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000464 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000465
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000466 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000467
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000468 void setTileWidth(int width) {
469 fTileWidth = width;
470 }
471
472 int getTileWidth() const {
473 return fTileWidth;
474 }
475
476 void setTileHeight(int height) {
477 fTileHeight = height;
478 }
479
480 int getTileHeight() const {
481 return fTileHeight;
482 }
483
484 void setTileWidthPercentage(double percentage) {
485 fTileWidthPercentage = percentage;
486 }
487
keyar@chromium.org163b5672012-08-01 17:53:29 +0000488 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000489 return fTileWidthPercentage;
490 }
491
492 void setTileHeightPercentage(double percentage) {
493 fTileHeightPercentage = percentage;
494 }
495
keyar@chromium.org163b5672012-08-01 17:53:29 +0000496 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000497 return fTileHeightPercentage;
498 }
499
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000500 void setTileMinPowerOf2Width(int width) {
501 SkASSERT(SkIsPow2(width) && width > 0);
502 if (!SkIsPow2(width) || width <= 0) {
503 return;
504 }
505
506 fTileMinPowerOf2Width = width;
507 }
508
509 int getTileMinPowerOf2Width() const {
510 return fTileMinPowerOf2Width;
511 }
512
scroggo@google.comcbcef702012-12-13 22:09:28 +0000513 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
514
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000515 virtual bool supportsTimingIndividualTiles() { return true; }
516
scroggo@google.comcbcef702012-12-13 22:09:28 +0000517 /**
518 * Report the number of tiles in the x and y directions. Must not be called before init.
519 * @param x Output parameter identifying the number of tiles in the x direction.
520 * @param y Output parameter identifying the number of tiles in the y direction.
521 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
522 * unmodified.
523 */
524 bool tileDimensions(int& x, int&y);
525
526 /**
527 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
528 * for the first time.
529 * @param i Output parameter identifying the column of the next tile to be drawn on the next
530 * call to drawNextTile.
531 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
532 * to drawNextTile.
533 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
534 * is within the range of tiles. If false, i and j are unmodified.
535 */
536 bool nextTile(int& i, int& j);
537
538 /**
539 * Render one tile. This will draw the same tile each time it is called until nextTile is
540 * called. The tile rendered will depend on how many calls have been made to nextTile.
541 * It is an error to call this without first calling nextTile, or if nextTile returns false.
542 */
543 void drawCurrentTile();
544
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000545protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000546 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000547
scroggo@google.com0a049b82012-11-02 22:01:26 +0000548 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
549 virtual SkString getConfigNameInternal() SK_OVERRIDE;
550
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000551private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000552 int fTileWidth;
553 int fTileHeight;
554 double fTileWidthPercentage;
555 double fTileHeightPercentage;
556 int fTileMinPowerOf2Width;
557
558 // These variables are only used for timing individual tiles.
559 // Next tile to draw in fTileRects.
560 int fCurrentTileOffset;
561 // Number of tiles in the x direction.
562 int fTilesX;
563 // Number of tiles in the y direction.
564 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000565
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000566 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000567 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000568
569 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000570};
571
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000572class CloneData;
573
574class MultiCorePictureRenderer : public TiledPictureRenderer {
575public:
576 explicit MultiCorePictureRenderer(int threadCount);
577
578 ~MultiCorePictureRenderer();
579
580 virtual void init(SkPicture* pict) SK_OVERRIDE;
581
582 /**
583 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
584 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000585 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000586
587 virtual void end() SK_OVERRIDE;
588
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000589 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
590
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000591private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000592 virtual SkString getConfigNameInternal() SK_OVERRIDE;
593
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000594 const int fNumThreads;
595 SkTDArray<SkCanvas*> fCanvasPool;
596 SkThreadPool fThreadPool;
597 SkPicture* fPictureClones;
598 CloneData** fCloneData;
599 SkCountdown fCountdown;
600
601 typedef TiledPictureRenderer INHERITED;
602};
603
scroggo@google.com9a412522012-09-07 15:21:18 +0000604/**
605 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
606 * into an SkPicturePlayback, which we want to time.
607 */
608class PlaybackCreationRenderer : public PictureRenderer {
609public:
610 virtual void setup() SK_OVERRIDE;
611
edisonn@google.com84f548c2012-12-18 22:24:03 +0000612 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000613
614 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
615
616 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
617
618private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000619 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000620
621 virtual SkString getConfigNameInternal() SK_OVERRIDE;
622
scroggo@google.com9a412522012-09-07 15:21:18 +0000623 typedef PictureRenderer INHERITED;
624};
625
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000626extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000627extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000628
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000629}
630
631#endif // PictureRenderer_DEFINED