blob: efe118ff08f48701e53c89c19c6f9fdca01bd607 [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"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000014#include "SkMath.h"
reed@google.comea6a3062012-11-06 22:14:54 +000015#include "SkPaint.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000016#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000017#include "SkPictureRecorder.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"
24#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000025
keyar@chromium.org06125642012-08-20 15:03:33 +000026#if SK_SUPPORT_GPU
27#include "GrContextFactory.h"
28#include "GrContext.h"
29#endif
30
commit-bot@chromium.org90c0fbd2014-05-09 03:18:41 +000031#include "image_expectations.h"
32
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000033class SkBitmap;
34class SkCanvas;
robertphillips@google.com6177e692013-02-28 20:16:25 +000035class SkGLContextHelper;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000036class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000037
38namespace sk_tools {
39
scroggo@google.comcbcef702012-12-13 22:09:28 +000040class TiledPictureRenderer;
41
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000042class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000043
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000044public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000045 enum SkDeviceTypes {
scroggo@google.com0556ea02013-02-08 19:38:21 +000046#if SK_ANGLE
47 kAngle_DeviceType,
48#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +000049#if SK_MESA
50 kMesa_DeviceType,
51#endif
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000052 kBitmap_DeviceType,
53#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +000054 kGPU_DeviceType,
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +000055 kNVPR_DeviceType,
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000056#endif
57 };
58
junov@chromium.org9313ca42012-11-02 18:11:49 +000059 enum BBoxHierarchyType {
60 kNone_BBoxHierarchyType = 0,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000061 kQuadTree_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000062 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000063 kTileGrid_BBoxHierarchyType,
commit-bot@chromium.orgcdd0f922014-03-11 17:27:07 +000064
65 kLast_BBoxHierarchyType = kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000066 };
67
caryclark@google.coma3622372012-11-06 21:26:13 +000068 // this uses SkPaint::Flags as a base and adds additional flags
69 enum DrawFilterFlags {
70 kNone_DrawFilterFlag = 0,
reed@google.com881b10b2013-05-22 14:03:45 +000071 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
72 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
73 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
humper@google.com387db0a2013-07-09 14:13:04 +000074 kMaskFilter_DrawFilterFlag = 0x80000, // toggles on/off mask filters (e.g., blurs)
caryclark@google.coma3622372012-11-06 21:26:13 +000075 };
76
robertphillips@google.com49149312013-07-03 15:34:35 +000077 SK_COMPILE_ASSERT(!(kMaskFilter_DrawFilterFlag & SkPaint::kAllFlags), maskfilter_flag_must_be_greater);
caryclark@google.coma3622372012-11-06 21:26:13 +000078 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
79 hinting_flag_must_be_greater);
80 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
81 slight_hinting_flag_must_be_greater);
82
scroggo@google.coma62da2f2012-11-02 21:28:12 +000083 /**
84 * Called with each new SkPicture to render.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000085 *
86 * @param pict The SkPicture to render.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000087 * @param writePath The output directory within which this renderer should write all images,
88 * or NULL if this renderer should not write all images.
89 * @param mismatchPath The output directory within which this renderer should write any images
90 * which do not match expectations, or NULL if this renderer should not write mismatches.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000091 * @param inputFilename The name of the input file we are rendering.
92 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
93 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000094 */
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000095 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000096 const SkString* inputFilename, bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +000097
98 /**
commit-bot@chromium.org8991c672014-05-22 00:36:05 +000099 * TODO(epoger): Temporary hack, while we work on http://skbug.com/2584 ('bench_pictures is
100 * timing reading pixels and writing json files'), such that:
101 * - render_pictures can call this method and continue to work
102 * - any other callers (bench_pictures) will skip calls to write() by default
103 */
104 void enableWrites() { fEnableWrites = true; }
105
106 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000107 * Set the viewport so that only the portion listed gets drawn.
108 */
109 void setViewport(SkISize size) { fViewport = size; }
110
111 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000112 * Set the scale factor at which draw the picture.
113 */
114 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
115
116 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000117 * Perform any setup that should done prior to each iteration of render() which should not be
118 * timed.
119 */
120 virtual void setup() {}
121
122 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000123 * Perform the work. If this is being called within the context of bench_pictures,
124 * this is the step that will be timed.
125 *
126 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
127 * it is recording the source SkPicture into another SkPicture.
128 *
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000129 * If fWritePath has been specified, the result of the work will be written to that dir.
130 * If fMismatchPath has been specified, and the actual image result differs from its
131 * expectation, the result of the work will be written to that dir.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000132 *
133 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
134 * output image into it, and return it here. (Some subclasses ignore this parameter)
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000135 * @return bool True if rendering succeeded and, if fWritePath had been specified, the output
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000136 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000137 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000138 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000139
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000140 /**
141 * Called once finished with a particular SkPicture, before calling init again, and before
142 * being done with this Renderer.
143 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000144 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000145
scroggo@google.comcbcef702012-12-13 22:09:28 +0000146 /**
147 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
148 * TiledPictureRender so its methods can be called.
149 */
150 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
151
scroggo@google.com08085f82013-01-28 20:40:24 +0000152 /**
153 * 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 +0000154 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000155 * @param callFinish Whether to call finish.
156 */
157 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000158
scroggo@google.com0556ea02013-02-08 19:38:21 +0000159 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000160 * Remove all decoded textures from the CPU caches and all uploaded textures
161 * from the GPU.
162 */
163 void purgeTextures();
164
165 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000166 * Set the backend type. Returns true on success and false on failure.
167 */
168 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000169 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000170#if SK_SUPPORT_GPU
171 // In case this function is called more than once
172 SkSafeUnref(fGrContext);
173 fGrContext = NULL;
174 // Set to Native so it will have an initial value.
175 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
176#endif
177 switch(deviceType) {
178 case kBitmap_DeviceType:
179 return true;
180#if SK_SUPPORT_GPU
181 case kGPU_DeviceType:
182 // Already set to GrContextFactory::kNative_GLContextType, above.
183 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000184 case kNVPR_DeviceType:
185 glContextType = GrContextFactory::kNVPR_GLContextType;
186 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000187#if SK_ANGLE
188 case kAngle_DeviceType:
189 glContextType = GrContextFactory::kANGLE_GLContextType;
190 break;
191#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000192#if SK_MESA
193 case kMesa_DeviceType:
194 glContextType = GrContextFactory::kMESA_GLContextType;
195 break;
196#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000197#endif
198 default:
199 // Invalid device type.
200 return false;
201 }
202#if SK_SUPPORT_GPU
rmistry05ead8a2014-06-23 06:13:46 -0700203 fGrContext = fGrContextFactory.get(glContextType);
scroggo@google.com0556ea02013-02-08 19:38:21 +0000204 if (NULL == fGrContext) {
205 return false;
206 } else {
207 fGrContext->ref();
208 return true;
209 }
210#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000211 }
212
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000213#if SK_SUPPORT_GPU
214 void setSampleCount(int sampleCount) {
215 fSampleCount = sampleCount;
216 }
217#endif
218
caryclark@google.come3e940c2012-11-07 16:42:17 +0000219 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
220 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000221 fDrawFiltersConfig = configName;
222 }
223
junov@chromium.org9313ca42012-11-02 18:11:49 +0000224 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
225 fBBoxHierarchyType = bbhType;
226 }
227
junov@chromium.orge286e842013-03-13 17:27:16 +0000228 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
229
junov@chromium.org7b537062012-11-06 18:58:43 +0000230 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000231 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000232 }
233
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000234 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000235 fJsonSummaryPtr = jsonSummaryPtr;
236 }
237
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000238 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000239 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000240 }
241
scroggo@google.com9a412522012-09-07 15:21:18 +0000242 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
243
244 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
245
scroggo@google.com0a049b82012-11-02 22:01:26 +0000246 /**
247 * Reports the configuration of this PictureRenderer.
248 */
249 SkString getConfigName() {
250 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000251 if (!fViewport.isEmpty()) {
252 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
253 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000254 if (fScaleFactor != SK_Scalar1) {
255 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
256 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000257 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
258 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000259 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
260 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000261 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
262 config.append("_grid");
kelvinly06fdc692014-06-03 15:43:34 -0700263 config.append("_");
264 config.appendS32(fGridInfo.fTileInterval.width());
265 config.append("x");
266 config.appendS32(fGridInfo.fTileInterval.height());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000267 }
268#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000269 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000270 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000271 if (fSampleCount) {
272 config.appendf("_msaa%d", fSampleCount);
273 } else {
274 config.append("_gpu");
275 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000276 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000277 case kNVPR_DeviceType:
278 config.appendf("_nvprmsaa%d", fSampleCount);
279 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000280#if SK_ANGLE
281 case kAngle_DeviceType:
282 config.append("_angle");
283 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000284#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000285#if SK_MESA
286 case kMesa_DeviceType:
287 config.append("_mesa");
288 break;
289#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000290 default:
291 // Assume that no extra info means bitmap.
292 break;
293 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000294#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000295 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000296 return config;
297 }
298
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000299#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000300 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000301 switch (fDeviceType) {
302 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000303 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000304 // fall through
305#if SK_ANGLE
306 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000307 // fall through
308#endif
309#if SK_MESA
310 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000311#endif
312 return true;
313 default:
314 return false;
315 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000316 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000317
robertphillips@google.com6177e692013-02-28 20:16:25 +0000318 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000319 GrContextFactory::GLContextType glContextType
320 = GrContextFactory::kNull_GLContextType;
321 switch(fDeviceType) {
322 case kGPU_DeviceType:
323 glContextType = GrContextFactory::kNative_GLContextType;
324 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000325 case kNVPR_DeviceType:
326 glContextType = GrContextFactory::kNVPR_GLContextType;
327 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000328#if SK_ANGLE
329 case kAngle_DeviceType:
330 glContextType = GrContextFactory::kANGLE_GLContextType;
331 break;
332#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000333#if SK_MESA
334 case kMesa_DeviceType:
335 glContextType = GrContextFactory::kMESA_GLContextType;
336 break;
337#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000338 default:
339 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000340 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000341 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000342 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000343
344 GrContext* getGrContext() {
345 return fGrContext;
346 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000347#endif
348
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000349 SkCanvas* getCanvas() {
350 return fCanvas;
351 }
352
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000353 SkPicture* getPicture() {
354 return fPicture;
355 }
356
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000357 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000358 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000359 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000360 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000361 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000362 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000363#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000364 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000365 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000366#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000367 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000368 fGridInfo.fMargin.setEmpty();
369 fGridInfo.fOffset.setZero();
370 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000371 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000372 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000373 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000374
scroggo@google.com0556ea02013-02-08 19:38:21 +0000375#if SK_SUPPORT_GPU
376 virtual ~PictureRenderer() {
377 SkSafeUnref(fGrContext);
378 }
379#endif
380
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000381protected:
382 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000383 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000384 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000385 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000386 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000387 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000388 BBoxHierarchyType fBBoxHierarchyType;
389 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
390 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000391 SkString fWritePath;
392 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000393 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000394 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000395
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000396 void buildBBoxHierarchy();
397
398 /**
399 * Return the total width that should be drawn. If the viewport width has been set greater than
400 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
401 */
402 int getViewWidth();
403
404 /**
405 * Return the total height that should be drawn. If the viewport height has been set greater
406 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
407 */
408 int getViewHeight();
409
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000410 /**
411 * Scales the provided canvas to the scale factor set by setScaleFactor.
412 */
413 void scaleToScaleFactor(SkCanvas*);
414
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000415 SkBBHFactory* getFactory();
robertphillips9f1c2412014-06-09 06:25:34 -0700416 uint32_t recordFlags() const { return 0; }
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000417 SkCanvas* setupCanvas();
418 virtual SkCanvas* setupCanvas(int width, int height);
419
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000420 /**
421 * Copy src to dest; if src==NULL, set dest to empty string.
422 */
423 static void CopyString(SkString* dest, const SkString* src);
424
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000425private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000426 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000427 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000428#if SK_SUPPORT_GPU
429 GrContextFactory fGrContextFactory;
430 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000431 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000432#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000433
scroggo@google.com0a049b82012-11-02 22:01:26 +0000434 virtual SkString getConfigNameInternal() = 0;
435
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000436 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000437};
438
scroggo@google.com9a412522012-09-07 15:21:18 +0000439/**
440 * This class does not do any rendering, but its render function executes recording, which we want
441 * to time.
442 */
443class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000444 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000445
446 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
447
448 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000449
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000450protected:
451 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
452
scroggo@google.com0a049b82012-11-02 22:01:26 +0000453private:
454 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000455};
456
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000457class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000458public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000459 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000460
461private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000462 virtual SkString getConfigNameInternal() SK_OVERRIDE;
463
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000464 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000465};
466
467class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000468public:
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000469 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000470 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000471
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000472 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000473
474private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000475 virtual SkString getConfigNameInternal() SK_OVERRIDE;
476
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000477 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000478};
479
480class TiledPictureRenderer : public PictureRenderer {
481public:
482 TiledPictureRenderer();
483
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000484 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000485 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000486
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000487 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000488 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000489 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000490 * created for each tile, named "path0.png", "path1.png", etc.
491 * Multithreaded mode currently does not support writing to a file.
492 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000493 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000494
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000495 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000496
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000497 void setTileWidth(int width) {
498 fTileWidth = width;
499 }
500
501 int getTileWidth() const {
502 return fTileWidth;
503 }
504
505 void setTileHeight(int height) {
506 fTileHeight = height;
507 }
508
509 int getTileHeight() const {
510 return fTileHeight;
511 }
512
513 void setTileWidthPercentage(double percentage) {
514 fTileWidthPercentage = percentage;
515 }
516
keyar@chromium.org163b5672012-08-01 17:53:29 +0000517 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000518 return fTileWidthPercentage;
519 }
520
521 void setTileHeightPercentage(double percentage) {
522 fTileHeightPercentage = percentage;
523 }
524
keyar@chromium.org163b5672012-08-01 17:53:29 +0000525 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000526 return fTileHeightPercentage;
527 }
528
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000529 void setTileMinPowerOf2Width(int width) {
530 SkASSERT(SkIsPow2(width) && width > 0);
531 if (!SkIsPow2(width) || width <= 0) {
532 return;
533 }
534
535 fTileMinPowerOf2Width = width;
536 }
537
538 int getTileMinPowerOf2Width() const {
539 return fTileMinPowerOf2Width;
540 }
541
scroggo@google.comcbcef702012-12-13 22:09:28 +0000542 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
543
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000544 virtual bool supportsTimingIndividualTiles() { return true; }
545
scroggo@google.comcbcef702012-12-13 22:09:28 +0000546 /**
547 * Report the number of tiles in the x and y directions. Must not be called before init.
548 * @param x Output parameter identifying the number of tiles in the x direction.
549 * @param y Output parameter identifying the number of tiles in the y direction.
550 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
551 * unmodified.
552 */
553 bool tileDimensions(int& x, int&y);
554
555 /**
556 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
557 * for the first time.
558 * @param i Output parameter identifying the column of the next tile to be drawn on the next
559 * call to drawNextTile.
560 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
561 * to drawNextTile.
562 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
563 * is within the range of tiles. If false, i and j are unmodified.
564 */
565 bool nextTile(int& i, int& j);
566
567 /**
568 * Render one tile. This will draw the same tile each time it is called until nextTile is
569 * called. The tile rendered will depend on how many calls have been made to nextTile.
570 * It is an error to call this without first calling nextTile, or if nextTile returns false.
571 */
572 void drawCurrentTile();
573
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000574protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000575 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000576
scroggo@google.com0a049b82012-11-02 22:01:26 +0000577 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
578 virtual SkString getConfigNameInternal() SK_OVERRIDE;
579
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000580private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000581 int fTileWidth;
582 int fTileHeight;
583 double fTileWidthPercentage;
584 double fTileHeightPercentage;
585 int fTileMinPowerOf2Width;
586
587 // These variables are only used for timing individual tiles.
588 // Next tile to draw in fTileRects.
589 int fCurrentTileOffset;
590 // Number of tiles in the x direction.
591 int fTilesX;
592 // Number of tiles in the y direction.
593 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000594
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000595 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000596 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000597
598 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000599};
600
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000601class CloneData;
602
603class MultiCorePictureRenderer : public TiledPictureRenderer {
604public:
605 explicit MultiCorePictureRenderer(int threadCount);
606
607 ~MultiCorePictureRenderer();
608
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000609 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000610 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000611
612 /**
613 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
614 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000615 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000616
617 virtual void end() SK_OVERRIDE;
618
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000619 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
620
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000621private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000622 virtual SkString getConfigNameInternal() SK_OVERRIDE;
623
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000624 const int fNumThreads;
625 SkTDArray<SkCanvas*> fCanvasPool;
626 SkThreadPool fThreadPool;
627 SkPicture* fPictureClones;
628 CloneData** fCloneData;
629 SkCountdown fCountdown;
630
631 typedef TiledPictureRenderer INHERITED;
632};
633
scroggo@google.com9a412522012-09-07 15:21:18 +0000634/**
635 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
636 * into an SkPicturePlayback, which we want to time.
637 */
638class PlaybackCreationRenderer : public PictureRenderer {
639public:
640 virtual void setup() SK_OVERRIDE;
641
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000642 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000643
644 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
645
646 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
647
648private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000649 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000650
651 virtual SkString getConfigNameInternal() SK_OVERRIDE;
652
scroggo@google.com9a412522012-09-07 15:21:18 +0000653 typedef PictureRenderer INHERITED;
654};
655
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000656extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000657extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000658
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000659}
660
661#endif // PictureRenderer_DEFINED