blob: 8192cacc0d5d28a6fca8996ab588b2f81d862a57 [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 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000047 * Adds this bitmap hash to the summary of results.
48 *
49 * @param testName name of the test
50 * @param hash hash to store
51 */
52 void add(const char *testName, uint64_t hash);
53
54 /**
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +000055 * Adds this bitmap's hash to the summary of results.
56 *
57 * @param testName name of the test
58 * @param bitmap bitmap to store the hash of
59 */
60 void add(const char *testName, const SkBitmap& bitmap);
61
62 /**
63 * Writes the summary (as constructed so far) to a file.
64 *
65 * @param filename path to write the summary to
66 */
67 void writeToFile(const char *filename);
68
69private:
70 Json::Value fActualResultsNoComparison;
71};
72
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000073class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000074
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000075public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000076 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000077#if SK_ANGLE
78 kAngle_DeviceType,
79#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000080#if SK_MESA
81 kMesa_DeviceType,
82#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000083 kBitmap_DeviceType,
84#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000085 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000086 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000087#endif
88 };
89
junov@chromium.org9313ca42012-11-02 18:11:49 +000090 enum BBoxHierarchyType {
91 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000092 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000093 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000094 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000095
96 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000097 };
98
caryclark@google.coma3622372012-11-06 21:26:13 +000099 // this uses SkPaint::Flags as a base and adds additional flags
100 enum DrawFilterFlags {
101 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +0000102 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
103 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
104 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +0000105 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +0000106 };
107
robertphillips@google.com49149312013-07-03 15:34:35 +0000108 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +0000109 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
110 hinting_flag_must_be_greater);
111 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
112 slight_hinting_flag_must_be_greater);
113
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000114 /**
115 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000116 *
117 * @param pict The SkPicture to render.
118 * @param outputDir The output directory within which this renderer should write files,
119 * or NULL if this renderer should not write files at all.
120 * @param inputFilename The name of the input file we are rendering.
121 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
122 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000123 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000124 virtual void init(SkPicture* pict, const SkString* outputDir,
125 const SkString* inputFilename, bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +0000126
127 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000128 * Set the viewport so that only the portion listed gets drawn.
129 */
130 void setViewport(SkISize size) { fViewport = size; }
131
132 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000133 * Set the scale factor at which draw the picture.
134 */
135 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
136
137 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000138 * Perform any setup that should done prior to each iteration of render() which should not be
139 * timed.
140 */
141 virtual void setup() {}
142
143 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000144 * Perform the work. If this is being called within the context of bench_pictures,
145 * this is the step that will be timed.
146 *
147 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
148 * it is recording the source SkPicture into another SkPicture.
149 *
150 * If fOutputDir has been specified, the result of the work will be written to that dir.
151 *
152 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
153 * output image into it, and return it here. (Some subclasses ignore this parameter)
154 * @return bool True if rendering succeeded and, if fOutputDir had been specified, the output
155 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000156 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000157 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000158
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000159 /**
160 * Called once finished with a particular SkPicture, before calling init again, and before
161 * being done with this Renderer.
162 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000163 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000164
scroggo@google.comcbcef702012-12-13 22:09:28 +0000165 /**
166 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
167 * TiledPictureRender so its methods can be called.
168 */
169 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
170
scroggo@google.com08085f82013-01-28 20:40:24 +0000171 /**
172 * 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 +0000173 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000174 * @param callFinish Whether to call finish.
175 */
176 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000177
scroggo@google.com0556ea02013-02-08 19:38:21 +0000178 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000179 * Remove all decoded textures from the CPU caches and all uploaded textures
180 * from the GPU.
181 */
182 void purgeTextures();
183
184 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000185 * Set the backend type. Returns true on success and false on failure.
186 */
187 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000188 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000189#if SK_SUPPORT_GPU
190 // In case this function is called more than once
191 SkSafeUnref(fGrContext);
192 fGrContext = NULL;
193 // Set to Native so it will have an initial value.
194 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
195#endif
196 switch(deviceType) {
197 case kBitmap_DeviceType:
198 return true;
199#if SK_SUPPORT_GPU
200 case kGPU_DeviceType:
201 // Already set to GrContextFactory::kNative_GLContextType, above.
202 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000203 case kNVPR_DeviceType:
204 glContextType = GrContextFactory::kNVPR_GLContextType;
205 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000206#if SK_ANGLE
207 case kAngle_DeviceType:
208 glContextType = GrContextFactory::kANGLE_GLContextType;
209 break;
210#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000211#if SK_MESA
212 case kMesa_DeviceType:
213 glContextType = GrContextFactory::kMESA_GLContextType;
214 break;
215#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000216#endif
217 default:
218 // Invalid device type.
219 return false;
220 }
221#if SK_SUPPORT_GPU
222 fGrContext = fGrContextFactory.get(glContextType);
223 if (NULL == fGrContext) {
224 return false;
225 } else {
226 fGrContext->ref();
227 return true;
228 }
229#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000230 }
231
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000232#if SK_SUPPORT_GPU
233 void setSampleCount(int sampleCount) {
234 fSampleCount = sampleCount;
235 }
236#endif
237
caryclark@google.come3e940c2012-11-07 16:42:17 +0000238 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
239 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000240 fDrawFiltersConfig = configName;
241 }
242
junov@chromium.org9313ca42012-11-02 18:11:49 +0000243 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
244 fBBoxHierarchyType = bbhType;
245 }
246
junov@chromium.orge286e842013-03-13 17:27:16 +0000247 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
248
junov@chromium.org7b537062012-11-06 18:58:43 +0000249 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000250 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000251 }
252
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000253 void setJsonSummaryPtr(ImageResultsSummary* jsonSummaryPtr) {
254 fJsonSummaryPtr = jsonSummaryPtr;
255 }
256
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000257 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000258 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000259 }
260
scroggo@google.com9a412522012-09-07 15:21:18 +0000261 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
262
263 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
264
scroggo@google.com0a049b82012-11-02 22:01:26 +0000265 /**
266 * Reports the configuration of this PictureRenderer.
267 */
268 SkString getConfigName() {
269 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000270 if (!fViewport.isEmpty()) {
271 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
272 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000273 if (fScaleFactor != SK_Scalar1) {
274 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
275 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000276 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
277 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000278 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
279 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000280 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
281 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000282 }
283#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000284 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000285 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000286 if (fSampleCount) {
287 config.appendf("_msaa%d", fSampleCount);
288 } else {
289 config.append("_gpu");
290 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000291 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000292 case kNVPR_DeviceType:
293 config.appendf("_nvprmsaa%d", fSampleCount);
294 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295#if SK_ANGLE
296 case kAngle_DeviceType:
297 config.append("_angle");
298 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000299#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000300#if SK_MESA
301 case kMesa_DeviceType:
302 config.append("_mesa");
303 break;
304#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000305 default:
306 // Assume that no extra info means bitmap.
307 break;
308 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000309#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000310 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000311 return config;
312 }
313
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000314#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000315 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000316 switch (fDeviceType) {
317 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000318 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000319 // fall through
320#if SK_ANGLE
321 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000322 // fall through
323#endif
324#if SK_MESA
325 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000326#endif
327 return true;
328 default:
329 return false;
330 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000331 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000332
robertphillips@google.com6177e692013-02-28 20:16:25 +0000333 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000334 GrContextFactory::GLContextType glContextType
335 = GrContextFactory::kNull_GLContextType;
336 switch(fDeviceType) {
337 case kGPU_DeviceType:
338 glContextType = GrContextFactory::kNative_GLContextType;
339 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000340 case kNVPR_DeviceType:
341 glContextType = GrContextFactory::kNVPR_GLContextType;
342 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000343#if SK_ANGLE
344 case kAngle_DeviceType:
345 glContextType = GrContextFactory::kANGLE_GLContextType;
346 break;
347#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000348#if SK_MESA
349 case kMesa_DeviceType:
350 glContextType = GrContextFactory::kMESA_GLContextType;
351 break;
352#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000353 default:
354 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000355 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000356 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000357 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000358
359 GrContext* getGrContext() {
360 return fGrContext;
361 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000362#endif
363
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000364 SkCanvas* getCanvas() {
365 return fCanvas;
366 }
367
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000368 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000369 : fPicture(NULL)
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000370 , fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000371 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000372 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000373 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000374#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000375 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000376 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000377#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000378 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000379 fGridInfo.fMargin.setEmpty();
380 fGridInfo.fOffset.setZero();
381 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000382 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000383 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000384 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000385
scroggo@google.com0556ea02013-02-08 19:38:21 +0000386#if SK_SUPPORT_GPU
387 virtual ~PictureRenderer() {
388 SkSafeUnref(fGrContext);
389 }
390#endif
391
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000392protected:
393 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000394 SkPicture* fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000395 bool fUseChecksumBasedFilenames;
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000396 ImageResultsSummary* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000397 SkDeviceTypes fDeviceType;
398 BBoxHierarchyType fBBoxHierarchyType;
399 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
400 SkString fDrawFiltersConfig;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000401 SkString fOutputDir;
402 SkString fInputFilename;
junov@chromium.org29b19e52013-02-27 18:35:16 +0000403 SkTileGridPicture::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000404
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000405 void buildBBoxHierarchy();
406
407 /**
408 * Return the total width that should be drawn. If the viewport width has been set greater than
409 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
410 */
411 int getViewWidth();
412
413 /**
414 * Return the total height that should be drawn. If the viewport height has been set greater
415 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
416 */
417 int getViewHeight();
418
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000419 /**
420 * Scales the provided canvas to the scale factor set by setScaleFactor.
421 */
422 void scaleToScaleFactor(SkCanvas*);
423
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000424 SkPicture* createPicture();
425 uint32_t recordFlags();
426 SkCanvas* setupCanvas();
427 virtual SkCanvas* setupCanvas(int width, int height);
428
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000429 /**
430 * Copy src to dest; if src==NULL, set dest to empty string.
431 */
432 static void CopyString(SkString* dest, const SkString* src);
433
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000434private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000435 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000436 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000437#if SK_SUPPORT_GPU
438 GrContextFactory fGrContextFactory;
439 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000440 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000441#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000442
scroggo@google.com0a049b82012-11-02 22:01:26 +0000443 virtual SkString getConfigNameInternal() = 0;
444
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000445 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000446};
447
scroggo@google.com9a412522012-09-07 15:21:18 +0000448/**
449 * This class does not do any rendering, but its render function executes recording, which we want
450 * to time.
451 */
452class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000453 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000454
455 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
456
457 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000458
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000459protected:
460 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
461
scroggo@google.com0a049b82012-11-02 22:01:26 +0000462private:
463 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000464};
465
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000466class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000467public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000468 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000469
470private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000471 virtual SkString getConfigNameInternal() SK_OVERRIDE;
472
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000473 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000474};
475
476class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000477public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000478 virtual void init(SkPicture* pict, const SkString* outputDir,
479 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000480
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000481 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000482
483private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000484 virtual SkString getConfigNameInternal() SK_OVERRIDE;
485
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000486 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000487};
488
489class TiledPictureRenderer : public PictureRenderer {
490public:
491 TiledPictureRenderer();
492
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000493 virtual void init(SkPicture* pict, const SkString* outputDir,
494 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000495
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000496 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000497 * Renders to tiles, rather than a single canvas.
498 * If fOutputDir was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000499 * created for each tile, named "path0.png", "path1.png", etc.
500 * Multithreaded mode currently does not support writing to a file.
501 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000502 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000503
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000504 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000505
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000506 void setTileWidth(int width) {
507 fTileWidth = width;
508 }
509
510 int getTileWidth() const {
511 return fTileWidth;
512 }
513
514 void setTileHeight(int height) {
515 fTileHeight = height;
516 }
517
518 int getTileHeight() const {
519 return fTileHeight;
520 }
521
522 void setTileWidthPercentage(double percentage) {
523 fTileWidthPercentage = percentage;
524 }
525
keyar@chromium.org163b5672012-08-01 17:53:29 +0000526 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000527 return fTileWidthPercentage;
528 }
529
530 void setTileHeightPercentage(double percentage) {
531 fTileHeightPercentage = percentage;
532 }
533
keyar@chromium.org163b5672012-08-01 17:53:29 +0000534 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000535 return fTileHeightPercentage;
536 }
537
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000538 void setTileMinPowerOf2Width(int width) {
539 SkASSERT(SkIsPow2(width) && width > 0);
540 if (!SkIsPow2(width) || width <= 0) {
541 return;
542 }
543
544 fTileMinPowerOf2Width = width;
545 }
546
547 int getTileMinPowerOf2Width() const {
548 return fTileMinPowerOf2Width;
549 }
550
scroggo@google.comcbcef702012-12-13 22:09:28 +0000551 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
552
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000553 virtual bool supportsTimingIndividualTiles() { return true; }
554
scroggo@google.comcbcef702012-12-13 22:09:28 +0000555 /**
556 * Report the number of tiles in the x and y directions. Must not be called before init.
557 * @param x Output parameter identifying the number of tiles in the x direction.
558 * @param y Output parameter identifying the number of tiles in the y direction.
559 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
560 * unmodified.
561 */
562 bool tileDimensions(int& x, int&y);
563
564 /**
565 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
566 * for the first time.
567 * @param i Output parameter identifying the column of the next tile to be drawn on the next
568 * call to drawNextTile.
569 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
570 * to drawNextTile.
571 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
572 * is within the range of tiles. If false, i and j are unmodified.
573 */
574 bool nextTile(int& i, int& j);
575
576 /**
577 * Render one tile. This will draw the same tile each time it is called until nextTile is
578 * called. The tile rendered will depend on how many calls have been made to nextTile.
579 * It is an error to call this without first calling nextTile, or if nextTile returns false.
580 */
581 void drawCurrentTile();
582
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000583protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000584 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000585
scroggo@google.com0a049b82012-11-02 22:01:26 +0000586 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
587 virtual SkString getConfigNameInternal() SK_OVERRIDE;
588
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000589private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000590 int fTileWidth;
591 int fTileHeight;
592 double fTileWidthPercentage;
593 double fTileHeightPercentage;
594 int fTileMinPowerOf2Width;
595
596 // These variables are only used for timing individual tiles.
597 // Next tile to draw in fTileRects.
598 int fCurrentTileOffset;
599 // Number of tiles in the x direction.
600 int fTilesX;
601 // Number of tiles in the y direction.
602 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000603
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000604 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000605 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000606
607 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000608};
609
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000610class CloneData;
611
612class MultiCorePictureRenderer : public TiledPictureRenderer {
613public:
614 explicit MultiCorePictureRenderer(int threadCount);
615
616 ~MultiCorePictureRenderer();
617
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000618 virtual void init(SkPicture* pict, const SkString* outputDir,
619 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000620
621 /**
622 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
623 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000624 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000625
626 virtual void end() SK_OVERRIDE;
627
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000628 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
629
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000630private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000631 virtual SkString getConfigNameInternal() SK_OVERRIDE;
632
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000633 const int fNumThreads;
634 SkTDArray<SkCanvas*> fCanvasPool;
635 SkThreadPool fThreadPool;
636 SkPicture* fPictureClones;
637 CloneData** fCloneData;
638 SkCountdown fCountdown;
639
640 typedef TiledPictureRenderer INHERITED;
641};
642
scroggo@google.com9a412522012-09-07 15:21:18 +0000643/**
644 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
645 * into an SkPicturePlayback, which we want to time.
646 */
647class PlaybackCreationRenderer : public PictureRenderer {
648public:
649 virtual void setup() SK_OVERRIDE;
650
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000651 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000652
653 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
654
655 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
656
657private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000658 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000659
660 virtual SkString getConfigNameInternal() SK_OVERRIDE;
661
scroggo@google.com9a412522012-09-07 15:21:18 +0000662 typedef PictureRenderer INHERITED;
663};
664
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000665extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000666extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000667
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000668}
669
670#endif // PictureRenderer_DEFINED