blob: 67392774ba192b421b9b5432474961e5d3598221 [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,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000078#endif
79 };
80
junov@chromium.org9313ca42012-11-02 18:11:49 +000081 enum BBoxHierarchyType {
82 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000083 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000084 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000085 kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000086 };
87
caryclark@google.coma3622372012-11-06 21:26:13 +000088 // this uses SkPaint::Flags as a base and adds additional flags
89 enum DrawFilterFlags {
90 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000091 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
92 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
93 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000094 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000095 };
96
robertphillips@google.com49149312013-07-03 15:34:35 +000097 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000098 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
99 hinting_flag_must_be_greater);
100 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
101 slight_hinting_flag_must_be_greater);
102
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000103 /**
104 * Called with each new SkPicture to render.
105 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000106 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +0000107
108 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000109 * Set the viewport so that only the portion listed gets drawn.
110 */
111 void setViewport(SkISize size) { fViewport = size; }
112
113 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000114 * Set the scale factor at which draw the picture.
115 */
116 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
117
118 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000119 * Perform any setup that should done prior to each iteration of render() which should not be
120 * timed.
121 */
122 virtual void setup() {}
123
124 /**
125 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
126 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000127 * If path is non-null, subclass implementations should call write().
128 * @param path If non-null, also write the output to the file specified by path. path should
129 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000130 * @return bool True if rendering succeeded and, if path is non-null, the output was
131 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000132 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000133 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000134
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000135 /**
136 * Called once finished with a particular SkPicture, before calling init again, and before
137 * being done with this Renderer.
138 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000139 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000140
scroggo@google.comcbcef702012-12-13 22:09:28 +0000141 /**
142 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
143 * TiledPictureRender so its methods can be called.
144 */
145 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
146
scroggo@google.com08085f82013-01-28 20:40:24 +0000147 /**
148 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
149 * flush, and calls finish if callFinish is true.
150 * @param callFinish Whether to call finish.
151 */
152 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000153
scroggo@google.com0556ea02013-02-08 19:38:21 +0000154 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000155 * Remove all decoded textures from the CPU caches and all uploaded textures
156 * from the GPU.
157 */
158 void purgeTextures();
159
160 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000161 * Set the backend type. Returns true on success and false on failure.
162 */
163 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000164 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000165#if SK_SUPPORT_GPU
166 // In case this function is called more than once
167 SkSafeUnref(fGrContext);
168 fGrContext = NULL;
169 // Set to Native so it will have an initial value.
170 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
171#endif
172 switch(deviceType) {
173 case kBitmap_DeviceType:
174 return true;
175#if SK_SUPPORT_GPU
176 case kGPU_DeviceType:
177 // Already set to GrContextFactory::kNative_GLContextType, above.
178 break;
179#if SK_ANGLE
180 case kAngle_DeviceType:
181 glContextType = GrContextFactory::kANGLE_GLContextType;
182 break;
183#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000184#if SK_MESA
185 case kMesa_DeviceType:
186 glContextType = GrContextFactory::kMESA_GLContextType;
187 break;
188#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000189#endif
190 default:
191 // Invalid device type.
192 return false;
193 }
194#if SK_SUPPORT_GPU
195 fGrContext = fGrContextFactory.get(glContextType);
196 if (NULL == fGrContext) {
197 return false;
198 } else {
199 fGrContext->ref();
200 return true;
201 }
202#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000203 }
204
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000205#if SK_SUPPORT_GPU
206 void setSampleCount(int sampleCount) {
207 fSampleCount = sampleCount;
208 }
209#endif
210
caryclark@google.come3e940c2012-11-07 16:42:17 +0000211 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
212 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000213 fDrawFiltersConfig = configName;
214 }
215
junov@chromium.org9313ca42012-11-02 18:11:49 +0000216 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
217 fBBoxHierarchyType = bbhType;
218 }
219
junov@chromium.orge286e842013-03-13 17:27:16 +0000220 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
221
junov@chromium.org7b537062012-11-06 18:58:43 +0000222 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000223 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000224 }
225
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000226 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
227 fJsonSummaryPtr = jsonSummaryPtr;
228 }
229
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000230 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000231 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000232 }
233
scroggo@google.com9a412522012-09-07 15:21:18 +0000234 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
235
236 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
237
scroggo@google.com0a049b82012-11-02 22:01:26 +0000238 /**
239 * Reports the configuration of this PictureRenderer.
240 */
241 SkString getConfigName() {
242 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000243 if (!fViewport.isEmpty()) {
244 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
245 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000246 if (fScaleFactor != SK_Scalar1) {
247 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
248 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000249 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
250 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000251 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
252 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000253 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
254 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000255 }
256#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000257 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000258 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000259 if (fSampleCount) {
260 config.appendf("_msaa%d", fSampleCount);
261 } else {
262 config.append("_gpu");
263 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000264 break;
265#if SK_ANGLE
266 case kAngle_DeviceType:
267 config.append("_angle");
268 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000269#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000270#if SK_MESA
271 case kMesa_DeviceType:
272 config.append("_mesa");
273 break;
274#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000275 default:
276 // Assume that no extra info means bitmap.
277 break;
278 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000279#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000280 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000281 return config;
282 }
283
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000284#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000285 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000286 switch (fDeviceType) {
287 case kGPU_DeviceType:
288 // fall through
289#if SK_ANGLE
290 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000291 // fall through
292#endif
293#if SK_MESA
294 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295#endif
296 return true;
297 default:
298 return false;
299 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000300 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000301
robertphillips@google.com6177e692013-02-28 20:16:25 +0000302 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000303 GrContextFactory::GLContextType glContextType
304 = GrContextFactory::kNull_GLContextType;
305 switch(fDeviceType) {
306 case kGPU_DeviceType:
307 glContextType = GrContextFactory::kNative_GLContextType;
308 break;
309#if SK_ANGLE
310 case kAngle_DeviceType:
311 glContextType = GrContextFactory::kANGLE_GLContextType;
312 break;
313#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000314#if SK_MESA
315 case kMesa_DeviceType:
316 glContextType = GrContextFactory::kMESA_GLContextType;
317 break;
318#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000319 default:
320 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000321 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000322 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000323 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000324
325 GrContext* getGrContext() {
326 return fGrContext;
327 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000328#endif
329
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000330 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000331 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000332 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000333 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000334 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000335 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000336#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000337 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000338 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000339#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000340 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000341 fGridInfo.fMargin.setEmpty();
342 fGridInfo.fOffset.setZero();
343 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000344 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000345 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000346 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000347
scroggo@google.com0556ea02013-02-08 19:38:21 +0000348#if SK_SUPPORT_GPU
349 virtual ~PictureRenderer() {
350 SkSafeUnref(fGrContext);
351 }
352#endif
353
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000354protected:
355 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000356 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000357 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000358 SkDeviceTypes fDeviceType;
359 BBoxHierarchyType fBBoxHierarchyType;
360 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
361 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000362 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000363
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000364 void buildBBoxHierarchy();
365
366 /**
367 * Return the total width that should be drawn. If the viewport width has been set greater than
368 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
369 */
370 int getViewWidth();
371
372 /**
373 * Return the total height that should be drawn. If the viewport height has been set greater
374 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
375 */
376 int getViewHeight();
377
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000378 /**
379 * Scales the provided canvas to the scale factor set by setScaleFactor.
380 */
381 void scaleToScaleFactor(SkCanvas*);
382
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000383 SkPicture* createPicture();
384 uint32_t recordFlags();
385 SkCanvas* setupCanvas();
386 virtual SkCanvas* setupCanvas(int width, int height);
387
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000388private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000389 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000390 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000391#if SK_SUPPORT_GPU
392 GrContextFactory fGrContextFactory;
393 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000394 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000395#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000396
scroggo@google.com0a049b82012-11-02 22:01:26 +0000397 virtual SkString getConfigNameInternal() = 0;
398
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000399 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000400};
401
scroggo@google.com9a412522012-09-07 15:21:18 +0000402/**
403 * This class does not do any rendering, but its render function executes recording, which we want
404 * to time.
405 */
406class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000407 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000408
409 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
410
411 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000412
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000413protected:
414 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
415
scroggo@google.com0a049b82012-11-02 22:01:26 +0000416private:
417 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000418};
419
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000420class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000421public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000422 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000423
424private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000425 virtual SkString getConfigNameInternal() SK_OVERRIDE;
426
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000427 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000428};
429
430class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000431public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000432 virtual void init(SkPicture* pict) SK_OVERRIDE;
433
edisonn@google.com84f548c2012-12-18 22:24:03 +0000434 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000435
436private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000437 virtual SkString getConfigNameInternal() SK_OVERRIDE;
438
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000439 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000440};
441
442class TiledPictureRenderer : public PictureRenderer {
443public:
444 TiledPictureRenderer();
445
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000446 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000447
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000448 /**
449 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
450 * created for each tile, named "path0.png", "path1.png", etc.
451 * Multithreaded mode currently does not support writing to a file.
452 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000453 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000454
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000455 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000456
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000457 void setTileWidth(int width) {
458 fTileWidth = width;
459 }
460
461 int getTileWidth() const {
462 return fTileWidth;
463 }
464
465 void setTileHeight(int height) {
466 fTileHeight = height;
467 }
468
469 int getTileHeight() const {
470 return fTileHeight;
471 }
472
473 void setTileWidthPercentage(double percentage) {
474 fTileWidthPercentage = percentage;
475 }
476
keyar@chromium.org163b5672012-08-01 17:53:29 +0000477 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000478 return fTileWidthPercentage;
479 }
480
481 void setTileHeightPercentage(double percentage) {
482 fTileHeightPercentage = percentage;
483 }
484
keyar@chromium.org163b5672012-08-01 17:53:29 +0000485 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000486 return fTileHeightPercentage;
487 }
488
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000489 void setTileMinPowerOf2Width(int width) {
490 SkASSERT(SkIsPow2(width) && width > 0);
491 if (!SkIsPow2(width) || width <= 0) {
492 return;
493 }
494
495 fTileMinPowerOf2Width = width;
496 }
497
498 int getTileMinPowerOf2Width() const {
499 return fTileMinPowerOf2Width;
500 }
501
scroggo@google.comcbcef702012-12-13 22:09:28 +0000502 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
503
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000504 virtual bool supportsTimingIndividualTiles() { return true; }
505
scroggo@google.comcbcef702012-12-13 22:09:28 +0000506 /**
507 * Report the number of tiles in the x and y directions. Must not be called before init.
508 * @param x Output parameter identifying the number of tiles in the x direction.
509 * @param y Output parameter identifying the number of tiles in the y direction.
510 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
511 * unmodified.
512 */
513 bool tileDimensions(int& x, int&y);
514
515 /**
516 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
517 * for the first time.
518 * @param i Output parameter identifying the column of the next tile to be drawn on the next
519 * call to drawNextTile.
520 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
521 * to drawNextTile.
522 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
523 * is within the range of tiles. If false, i and j are unmodified.
524 */
525 bool nextTile(int& i, int& j);
526
527 /**
528 * Render one tile. This will draw the same tile each time it is called until nextTile is
529 * called. The tile rendered will depend on how many calls have been made to nextTile.
530 * It is an error to call this without first calling nextTile, or if nextTile returns false.
531 */
532 void drawCurrentTile();
533
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000534protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000535 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000536
scroggo@google.com0a049b82012-11-02 22:01:26 +0000537 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
538 virtual SkString getConfigNameInternal() SK_OVERRIDE;
539
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000540private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000541 int fTileWidth;
542 int fTileHeight;
543 double fTileWidthPercentage;
544 double fTileHeightPercentage;
545 int fTileMinPowerOf2Width;
546
547 // These variables are only used for timing individual tiles.
548 // Next tile to draw in fTileRects.
549 int fCurrentTileOffset;
550 // Number of tiles in the x direction.
551 int fTilesX;
552 // Number of tiles in the y direction.
553 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000554
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000555 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000556 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000557
558 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000559};
560
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000561class CloneData;
562
563class MultiCorePictureRenderer : public TiledPictureRenderer {
564public:
565 explicit MultiCorePictureRenderer(int threadCount);
566
567 ~MultiCorePictureRenderer();
568
569 virtual void init(SkPicture* pict) SK_OVERRIDE;
570
571 /**
572 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
573 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000574 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000575
576 virtual void end() SK_OVERRIDE;
577
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000578 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
579
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000580private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000581 virtual SkString getConfigNameInternal() SK_OVERRIDE;
582
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000583 const int fNumThreads;
584 SkTDArray<SkCanvas*> fCanvasPool;
585 SkThreadPool fThreadPool;
586 SkPicture* fPictureClones;
587 CloneData** fCloneData;
588 SkCountdown fCountdown;
589
590 typedef TiledPictureRenderer INHERITED;
591};
592
scroggo@google.com9a412522012-09-07 15:21:18 +0000593/**
594 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
595 * into an SkPicturePlayback, which we want to time.
596 */
597class PlaybackCreationRenderer : public PictureRenderer {
598public:
599 virtual void setup() SK_OVERRIDE;
600
edisonn@google.com84f548c2012-12-18 22:24:03 +0000601 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000602
603 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
604
605 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
606
607private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000608 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000609
610 virtual SkString getConfigNameInternal() SK_OVERRIDE;
611
scroggo@google.com9a412522012-09-07 15:21:18 +0000612 typedef PictureRenderer INHERITED;
613};
614
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000615extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000616extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000617
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000618}
619
620#endif // PictureRenderer_DEFINED