blob: 68374dfe41a0091899615ce4dcc384ee6a71e9b4 [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
commit-bot@chromium.org51c040e2014-03-11 22:58:00 +0000152 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000153 * @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
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000343 SkCanvas* getCanvas() {
344 return fCanvas;
345 }
346
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000347 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000348 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000349 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000350 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000351 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000352 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000353#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000354 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000355 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000356#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000357 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000358 fGridInfo.fMargin.setEmpty();
359 fGridInfo.fOffset.setZero();
360 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000361 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000362 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000363 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000364
scroggo@google.com0556ea02013-02-08 19:38:21 +0000365#if SK_SUPPORT_GPU
366 virtual ~PictureRenderer() {
367 SkSafeUnref(fGrContext);
368 }
369#endif
370
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000371protected:
372 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000373 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000374 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000375 SkDeviceTypes fDeviceType;
376 BBoxHierarchyType fBBoxHierarchyType;
377 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
378 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000379 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000380
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000381 void buildBBoxHierarchy();
382
383 /**
384 * Return the total width that should be drawn. If the viewport width has been set greater than
385 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
386 */
387 int getViewWidth();
388
389 /**
390 * Return the total height that should be drawn. If the viewport height has been set greater
391 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
392 */
393 int getViewHeight();
394
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000395 /**
396 * Scales the provided canvas to the scale factor set by setScaleFactor.
397 */
398 void scaleToScaleFactor(SkCanvas*);
399
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000400 SkPicture* createPicture();
401 uint32_t recordFlags();
402 SkCanvas* setupCanvas();
403 virtual SkCanvas* setupCanvas(int width, int height);
404
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000405private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000406 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000407 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000408#if SK_SUPPORT_GPU
409 GrContextFactory fGrContextFactory;
410 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000411 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000412#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000413
scroggo@google.com0a049b82012-11-02 22:01:26 +0000414 virtual SkString getConfigNameInternal() = 0;
415
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000416 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000417};
418
scroggo@google.com9a412522012-09-07 15:21:18 +0000419/**
420 * This class does not do any rendering, but its render function executes recording, which we want
421 * to time.
422 */
423class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000424 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000425
426 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
427
428 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000429
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000430protected:
431 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
432
scroggo@google.com0a049b82012-11-02 22:01:26 +0000433private:
434 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000435};
436
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000437class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000438public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000439 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000440
441private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000442 virtual SkString getConfigNameInternal() SK_OVERRIDE;
443
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000444 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000445};
446
447class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000448public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000449 virtual void init(SkPicture* pict) SK_OVERRIDE;
450
edisonn@google.com84f548c2012-12-18 22:24:03 +0000451 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000452
453private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000454 virtual SkString getConfigNameInternal() SK_OVERRIDE;
455
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000456 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000457};
458
459class TiledPictureRenderer : public PictureRenderer {
460public:
461 TiledPictureRenderer();
462
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000463 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000464
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000465 /**
466 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
467 * created for each tile, named "path0.png", "path1.png", etc.
468 * Multithreaded mode currently does not support writing to a file.
469 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000470 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000471
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000472 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000473
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000474 void setTileWidth(int width) {
475 fTileWidth = width;
476 }
477
478 int getTileWidth() const {
479 return fTileWidth;
480 }
481
482 void setTileHeight(int height) {
483 fTileHeight = height;
484 }
485
486 int getTileHeight() const {
487 return fTileHeight;
488 }
489
490 void setTileWidthPercentage(double percentage) {
491 fTileWidthPercentage = percentage;
492 }
493
keyar@chromium.org163b5672012-08-01 17:53:29 +0000494 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000495 return fTileWidthPercentage;
496 }
497
498 void setTileHeightPercentage(double percentage) {
499 fTileHeightPercentage = percentage;
500 }
501
keyar@chromium.org163b5672012-08-01 17:53:29 +0000502 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000503 return fTileHeightPercentage;
504 }
505
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000506 void setTileMinPowerOf2Width(int width) {
507 SkASSERT(SkIsPow2(width) && width > 0);
508 if (!SkIsPow2(width) || width <= 0) {
509 return;
510 }
511
512 fTileMinPowerOf2Width = width;
513 }
514
515 int getTileMinPowerOf2Width() const {
516 return fTileMinPowerOf2Width;
517 }
518
scroggo@google.comcbcef702012-12-13 22:09:28 +0000519 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
520
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000521 virtual bool supportsTimingIndividualTiles() { return true; }
522
scroggo@google.comcbcef702012-12-13 22:09:28 +0000523 /**
524 * Report the number of tiles in the x and y directions. Must not be called before init.
525 * @param x Output parameter identifying the number of tiles in the x direction.
526 * @param y Output parameter identifying the number of tiles in the y direction.
527 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
528 * unmodified.
529 */
530 bool tileDimensions(int& x, int&y);
531
532 /**
533 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
534 * for the first time.
535 * @param i Output parameter identifying the column of the next tile to be drawn on the next
536 * call to drawNextTile.
537 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
538 * to drawNextTile.
539 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
540 * is within the range of tiles. If false, i and j are unmodified.
541 */
542 bool nextTile(int& i, int& j);
543
544 /**
545 * Render one tile. This will draw the same tile each time it is called until nextTile is
546 * called. The tile rendered will depend on how many calls have been made to nextTile.
547 * It is an error to call this without first calling nextTile, or if nextTile returns false.
548 */
549 void drawCurrentTile();
550
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000551protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000552 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000553
scroggo@google.com0a049b82012-11-02 22:01:26 +0000554 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
555 virtual SkString getConfigNameInternal() SK_OVERRIDE;
556
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000557private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000558 int fTileWidth;
559 int fTileHeight;
560 double fTileWidthPercentage;
561 double fTileHeightPercentage;
562 int fTileMinPowerOf2Width;
563
564 // These variables are only used for timing individual tiles.
565 // Next tile to draw in fTileRects.
566 int fCurrentTileOffset;
567 // Number of tiles in the x direction.
568 int fTilesX;
569 // Number of tiles in the y direction.
570 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000571
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000572 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000573 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000574
575 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000576};
577
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000578class CloneData;
579
580class MultiCorePictureRenderer : public TiledPictureRenderer {
581public:
582 explicit MultiCorePictureRenderer(int threadCount);
583
584 ~MultiCorePictureRenderer();
585
586 virtual void init(SkPicture* pict) SK_OVERRIDE;
587
588 /**
589 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
590 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000591 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000592
593 virtual void end() SK_OVERRIDE;
594
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000595 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
596
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000597private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000598 virtual SkString getConfigNameInternal() SK_OVERRIDE;
599
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000600 const int fNumThreads;
601 SkTDArray<SkCanvas*> fCanvasPool;
602 SkThreadPool fThreadPool;
603 SkPicture* fPictureClones;
604 CloneData** fCloneData;
605 SkCountdown fCountdown;
606
607 typedef TiledPictureRenderer INHERITED;
608};
609
scroggo@google.com9a412522012-09-07 15:21:18 +0000610/**
611 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
612 * into an SkPicturePlayback, which we want to time.
613 */
614class PlaybackCreationRenderer : public PictureRenderer {
615public:
616 virtual void setup() SK_OVERRIDE;
617
edisonn@google.com84f548c2012-12-18 22:24:03 +0000618 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000619
620 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
621
622 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
623
624private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000625 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000626
627 virtual SkString getConfigNameInternal() SK_OVERRIDE;
628
scroggo@google.com9a412522012-09-07 15:21:18 +0000629 typedef PictureRenderer INHERITED;
630};
631
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000632extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000633extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000634
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000635}
636
637#endif // PictureRenderer_DEFINED