blob: c4b97f2d2679d6b6b1714f9c0e5734a8ac718808 [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,
83 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000084 kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000085 };
86
caryclark@google.coma3622372012-11-06 21:26:13 +000087 // this uses SkPaint::Flags as a base and adds additional flags
88 enum DrawFilterFlags {
89 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000090 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
91 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
92 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000093 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000094 };
95
robertphillips@google.com49149312013-07-03 15:34:35 +000096 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000097 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
98 hinting_flag_must_be_greater);
99 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
100 slight_hinting_flag_must_be_greater);
101
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000102 /**
103 * Called with each new SkPicture to render.
104 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000105 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +0000106
107 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000108 * Set the viewport so that only the portion listed gets drawn.
109 */
110 void setViewport(SkISize size) { fViewport = size; }
111
112 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000113 * Set the scale factor at which draw the picture.
114 */
115 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
116
117 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000118 * Perform any setup that should done prior to each iteration of render() which should not be
119 * timed.
120 */
121 virtual void setup() {}
122
123 /**
124 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
125 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000126 * If path is non-null, subclass implementations should call write().
127 * @param path If non-null, also write the output to the file specified by path. path should
128 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +0000129 * @return bool True if rendering succeeded and, if path is non-null, the output was
130 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000131 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000132 virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000133
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000134 /**
135 * Called once finished with a particular SkPicture, before calling init again, and before
136 * being done with this Renderer.
137 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000138 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000139
scroggo@google.comcbcef702012-12-13 22:09:28 +0000140 /**
141 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
142 * TiledPictureRender so its methods can be called.
143 */
144 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
145
scroggo@google.com08085f82013-01-28 20:40:24 +0000146 /**
147 * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls
148 * flush, and calls finish if callFinish is true.
149 * @param callFinish Whether to call finish.
150 */
151 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000152
scroggo@google.com0556ea02013-02-08 19:38:21 +0000153 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000154 * Remove all decoded textures from the CPU caches and all uploaded textures
155 * from the GPU.
156 */
157 void purgeTextures();
158
159 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000160 * Set the backend type. Returns true on success and false on failure.
161 */
162 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000163 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000164#if SK_SUPPORT_GPU
165 // In case this function is called more than once
166 SkSafeUnref(fGrContext);
167 fGrContext = NULL;
168 // Set to Native so it will have an initial value.
169 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
170#endif
171 switch(deviceType) {
172 case kBitmap_DeviceType:
173 return true;
174#if SK_SUPPORT_GPU
175 case kGPU_DeviceType:
176 // Already set to GrContextFactory::kNative_GLContextType, above.
177 break;
178#if SK_ANGLE
179 case kAngle_DeviceType:
180 glContextType = GrContextFactory::kANGLE_GLContextType;
181 break;
182#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000183#if SK_MESA
184 case kMesa_DeviceType:
185 glContextType = GrContextFactory::kMESA_GLContextType;
186 break;
187#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000188#endif
189 default:
190 // Invalid device type.
191 return false;
192 }
193#if SK_SUPPORT_GPU
194 fGrContext = fGrContextFactory.get(glContextType);
195 if (NULL == fGrContext) {
196 return false;
197 } else {
198 fGrContext->ref();
199 return true;
200 }
201#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000202 }
203
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000204#if SK_SUPPORT_GPU
205 void setSampleCount(int sampleCount) {
206 fSampleCount = sampleCount;
207 }
208#endif
209
caryclark@google.come3e940c2012-11-07 16:42:17 +0000210 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
211 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000212 fDrawFiltersConfig = configName;
213 }
214
junov@chromium.org9313ca42012-11-02 18:11:49 +0000215 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
216 fBBoxHierarchyType = bbhType;
217 }
218
junov@chromium.orge286e842013-03-13 17:27:16 +0000219 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
220
junov@chromium.org7b537062012-11-06 18:58:43 +0000221 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000222 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000223 }
224
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000225 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
226 fJsonSummaryPtr = jsonSummaryPtr;
227 }
228
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000229 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000230 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000231 }
232
scroggo@google.com9a412522012-09-07 15:21:18 +0000233 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
234
235 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
236
scroggo@google.com0a049b82012-11-02 22:01:26 +0000237 /**
238 * Reports the configuration of this PictureRenderer.
239 */
240 SkString getConfigName() {
241 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000242 if (!fViewport.isEmpty()) {
243 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
244 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000245 if (fScaleFactor != SK_Scalar1) {
246 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
247 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000248 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
249 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000250 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
251 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000252 }
253#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000254 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000255 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000256 if (fSampleCount) {
257 config.appendf("_msaa%d", fSampleCount);
258 } else {
259 config.append("_gpu");
260 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000261 break;
262#if SK_ANGLE
263 case kAngle_DeviceType:
264 config.append("_angle");
265 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000266#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000267#if SK_MESA
268 case kMesa_DeviceType:
269 config.append("_mesa");
270 break;
271#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000272 default:
273 // Assume that no extra info means bitmap.
274 break;
275 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000276#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000277 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000278 return config;
279 }
280
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000281#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000282 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000283 switch (fDeviceType) {
284 case kGPU_DeviceType:
285 // fall through
286#if SK_ANGLE
287 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000288 // fall through
289#endif
290#if SK_MESA
291 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000292#endif
293 return true;
294 default:
295 return false;
296 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000297 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000298
robertphillips@google.com6177e692013-02-28 20:16:25 +0000299 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000300 GrContextFactory::GLContextType glContextType
301 = GrContextFactory::kNull_GLContextType;
302 switch(fDeviceType) {
303 case kGPU_DeviceType:
304 glContextType = GrContextFactory::kNative_GLContextType;
305 break;
306#if SK_ANGLE
307 case kAngle_DeviceType:
308 glContextType = GrContextFactory::kANGLE_GLContextType;
309 break;
310#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000311#if SK_MESA
312 case kMesa_DeviceType:
313 glContextType = GrContextFactory::kMESA_GLContextType;
314 break;
315#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000316 default:
317 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000318 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000319 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000320 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000321
322 GrContext* getGrContext() {
323 return fGrContext;
324 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000325#endif
326
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000327 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000328 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000329 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000330 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000331 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000332 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000333#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000334 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000335 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000336#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000337 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000338 fGridInfo.fMargin.setEmpty();
339 fGridInfo.fOffset.setZero();
340 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000341 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000342 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000343 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000344
scroggo@google.com0556ea02013-02-08 19:38:21 +0000345#if SK_SUPPORT_GPU
346 virtual ~PictureRenderer() {
347 SkSafeUnref(fGrContext);
348 }
349#endif
350
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000351protected:
352 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000353 SkPicture* fPicture;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000354 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000355 SkDeviceTypes fDeviceType;
356 BBoxHierarchyType fBBoxHierarchyType;
357 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
358 SkString fDrawFiltersConfig;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000359 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000360
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000361 void buildBBoxHierarchy();
362
363 /**
364 * Return the total width that should be drawn. If the viewport width has been set greater than
365 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
366 */
367 int getViewWidth();
368
369 /**
370 * Return the total height that should be drawn. If the viewport height has been set greater
371 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
372 */
373 int getViewHeight();
374
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000375 /**
376 * Scales the provided canvas to the scale factor set by setScaleFactor.
377 */
378 void scaleToScaleFactor(SkCanvas*);
379
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000380 SkPicture* createPicture();
381 uint32_t recordFlags();
382 SkCanvas* setupCanvas();
383 virtual SkCanvas* setupCanvas(int width, int height);
384
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000385private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000386 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000387 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000388#if SK_SUPPORT_GPU
389 GrContextFactory fGrContextFactory;
390 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000391 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000392#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000393
scroggo@google.com0a049b82012-11-02 22:01:26 +0000394 virtual SkString getConfigNameInternal() = 0;
395
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000396 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000397};
398
scroggo@google.com9a412522012-09-07 15:21:18 +0000399/**
400 * This class does not do any rendering, but its render function executes recording, which we want
401 * to time.
402 */
403class RecordPictureRenderer : public PictureRenderer {
edisonn@google.com84f548c2012-12-18 22:24:03 +0000404 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000405
406 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
407
408 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000409
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000410protected:
411 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
412
scroggo@google.com0a049b82012-11-02 22:01:26 +0000413private:
414 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000415};
416
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000417class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000418public:
edisonn@google.com84f548c2012-12-18 22:24:03 +0000419 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000420
421private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000422 virtual SkString getConfigNameInternal() SK_OVERRIDE;
423
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000424 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000425};
426
427class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000428public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000429 virtual void init(SkPicture* pict) SK_OVERRIDE;
430
edisonn@google.com84f548c2012-12-18 22:24:03 +0000431 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000432
433private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000434 virtual SkString getConfigNameInternal() SK_OVERRIDE;
435
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000436 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000437};
438
439class TiledPictureRenderer : public PictureRenderer {
440public:
441 TiledPictureRenderer();
442
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000443 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000444
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000445 /**
446 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
447 * created for each tile, named "path0.png", "path1.png", etc.
448 * Multithreaded mode currently does not support writing to a file.
449 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000450 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000451
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000452 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000453
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000454 void setTileWidth(int width) {
455 fTileWidth = width;
456 }
457
458 int getTileWidth() const {
459 return fTileWidth;
460 }
461
462 void setTileHeight(int height) {
463 fTileHeight = height;
464 }
465
466 int getTileHeight() const {
467 return fTileHeight;
468 }
469
470 void setTileWidthPercentage(double percentage) {
471 fTileWidthPercentage = percentage;
472 }
473
keyar@chromium.org163b5672012-08-01 17:53:29 +0000474 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000475 return fTileWidthPercentage;
476 }
477
478 void setTileHeightPercentage(double percentage) {
479 fTileHeightPercentage = percentage;
480 }
481
keyar@chromium.org163b5672012-08-01 17:53:29 +0000482 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000483 return fTileHeightPercentage;
484 }
485
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000486 void setTileMinPowerOf2Width(int width) {
487 SkASSERT(SkIsPow2(width) && width > 0);
488 if (!SkIsPow2(width) || width <= 0) {
489 return;
490 }
491
492 fTileMinPowerOf2Width = width;
493 }
494
495 int getTileMinPowerOf2Width() const {
496 return fTileMinPowerOf2Width;
497 }
498
scroggo@google.comcbcef702012-12-13 22:09:28 +0000499 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
500
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000501 virtual bool supportsTimingIndividualTiles() { return true; }
502
scroggo@google.comcbcef702012-12-13 22:09:28 +0000503 /**
504 * Report the number of tiles in the x and y directions. Must not be called before init.
505 * @param x Output parameter identifying the number of tiles in the x direction.
506 * @param y Output parameter identifying the number of tiles in the y direction.
507 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
508 * unmodified.
509 */
510 bool tileDimensions(int& x, int&y);
511
512 /**
513 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
514 * for the first time.
515 * @param i Output parameter identifying the column of the next tile to be drawn on the next
516 * call to drawNextTile.
517 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
518 * to drawNextTile.
519 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
520 * is within the range of tiles. If false, i and j are unmodified.
521 */
522 bool nextTile(int& i, int& j);
523
524 /**
525 * Render one tile. This will draw the same tile each time it is called until nextTile is
526 * called. The tile rendered will depend on how many calls have been made to nextTile.
527 * It is an error to call this without first calling nextTile, or if nextTile returns false.
528 */
529 void drawCurrentTile();
530
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000531protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000532 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000533
scroggo@google.com0a049b82012-11-02 22:01:26 +0000534 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
535 virtual SkString getConfigNameInternal() SK_OVERRIDE;
536
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000537private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000538 int fTileWidth;
539 int fTileHeight;
540 double fTileWidthPercentage;
541 double fTileHeightPercentage;
542 int fTileMinPowerOf2Width;
543
544 // These variables are only used for timing individual tiles.
545 // Next tile to draw in fTileRects.
546 int fCurrentTileOffset;
547 // Number of tiles in the x direction.
548 int fTilesX;
549 // Number of tiles in the y direction.
550 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000551
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000552 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000553 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000554
555 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000556};
557
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000558class CloneData;
559
560class MultiCorePictureRenderer : public TiledPictureRenderer {
561public:
562 explicit MultiCorePictureRenderer(int threadCount);
563
564 ~MultiCorePictureRenderer();
565
566 virtual void init(SkPicture* pict) SK_OVERRIDE;
567
568 /**
569 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
570 */
edisonn@google.com84f548c2012-12-18 22:24:03 +0000571 virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000572
573 virtual void end() SK_OVERRIDE;
574
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000575 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
576
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000577private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000578 virtual SkString getConfigNameInternal() SK_OVERRIDE;
579
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000580 const int fNumThreads;
581 SkTDArray<SkCanvas*> fCanvasPool;
582 SkThreadPool fThreadPool;
583 SkPicture* fPictureClones;
584 CloneData** fCloneData;
585 SkCountdown fCountdown;
586
587 typedef TiledPictureRenderer INHERITED;
588};
589
scroggo@google.com9a412522012-09-07 15:21:18 +0000590/**
591 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
592 * into an SkPicturePlayback, which we want to time.
593 */
594class PlaybackCreationRenderer : public PictureRenderer {
595public:
596 virtual void setup() SK_OVERRIDE;
597
edisonn@google.com84f548c2012-12-18 22:24:03 +0000598 virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000599
600 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
601
602 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
603
604private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000605 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000606
607 virtual SkString getConfigNameInternal() SK_OVERRIDE;
608
scroggo@google.com9a412522012-09-07 15:21:18 +0000609 typedef PictureRenderer INHERITED;
610};
611
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000612extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000613extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000614
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000615}
616
617#endif // PictureRenderer_DEFINED