blob: 109073bca764dee97dc1895292e36ccbfb8fa41f [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
203 fGrContext = fGrContextFactory.get(glContextType);
204 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");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000263 }
264#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000265 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000266 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000267 if (fSampleCount) {
268 config.appendf("_msaa%d", fSampleCount);
269 } else {
270 config.append("_gpu");
271 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000272 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000273 case kNVPR_DeviceType:
274 config.appendf("_nvprmsaa%d", fSampleCount);
275 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000276#if SK_ANGLE
277 case kAngle_DeviceType:
278 config.append("_angle");
279 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000280#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000281#if SK_MESA
282 case kMesa_DeviceType:
283 config.append("_mesa");
284 break;
285#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000286 default:
287 // Assume that no extra info means bitmap.
288 break;
289 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000290#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000291 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000292 return config;
293 }
294
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000295#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000296 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000297 switch (fDeviceType) {
298 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000299 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000300 // fall through
301#if SK_ANGLE
302 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000303 // fall through
304#endif
305#if SK_MESA
306 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000307#endif
308 return true;
309 default:
310 return false;
311 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000312 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000313
robertphillips@google.com6177e692013-02-28 20:16:25 +0000314 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000315 GrContextFactory::GLContextType glContextType
316 = GrContextFactory::kNull_GLContextType;
317 switch(fDeviceType) {
318 case kGPU_DeviceType:
319 glContextType = GrContextFactory::kNative_GLContextType;
320 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000321 case kNVPR_DeviceType:
322 glContextType = GrContextFactory::kNVPR_GLContextType;
323 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000324#if SK_ANGLE
325 case kAngle_DeviceType:
326 glContextType = GrContextFactory::kANGLE_GLContextType;
327 break;
328#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000329#if SK_MESA
330 case kMesa_DeviceType:
331 glContextType = GrContextFactory::kMESA_GLContextType;
332 break;
333#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000334 default:
335 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000336 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000337 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000338 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000339
340 GrContext* getGrContext() {
341 return fGrContext;
342 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000343#endif
344
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000345 SkCanvas* getCanvas() {
346 return fCanvas;
347 }
348
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000349 SkPicture* getPicture() {
350 return fPicture;
351 }
352
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000353 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000354 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000355 , fDeviceType(kBitmap_DeviceType)
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000356 , fEnableWrites(false)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000357 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000358 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000359#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000360 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000361 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000362#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000363 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000364 fGridInfo.fMargin.setEmpty();
365 fGridInfo.fOffset.setZero();
366 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000367 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000368 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000369 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000370
scroggo@google.com0556ea02013-02-08 19:38:21 +0000371#if SK_SUPPORT_GPU
372 virtual ~PictureRenderer() {
373 SkSafeUnref(fGrContext);
374 }
375#endif
376
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000377protected:
378 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000379 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000380 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000381 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000382 SkDeviceTypes fDeviceType;
commit-bot@chromium.org8991c672014-05-22 00:36:05 +0000383 bool fEnableWrites;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000384 BBoxHierarchyType fBBoxHierarchyType;
385 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
386 SkString fDrawFiltersConfig;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000387 SkString fWritePath;
388 SkString fMismatchPath;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000389 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000390 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000391
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000392 void buildBBoxHierarchy();
393
394 /**
395 * Return the total width that should be drawn. If the viewport width has been set greater than
396 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
397 */
398 int getViewWidth();
399
400 /**
401 * Return the total height that should be drawn. If the viewport height has been set greater
402 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
403 */
404 int getViewHeight();
405
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000406 /**
407 * Scales the provided canvas to the scale factor set by setScaleFactor.
408 */
409 void scaleToScaleFactor(SkCanvas*);
410
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000411 SkBBHFactory* getFactory();
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000412 uint32_t recordFlags();
413 SkCanvas* setupCanvas();
414 virtual SkCanvas* setupCanvas(int width, int height);
415
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000416 /**
417 * Copy src to dest; if src==NULL, set dest to empty string.
418 */
419 static void CopyString(SkString* dest, const SkString* src);
420
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000421private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000422 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000423 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000424#if SK_SUPPORT_GPU
425 GrContextFactory fGrContextFactory;
426 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000427 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000428#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000429
scroggo@google.com0a049b82012-11-02 22:01:26 +0000430 virtual SkString getConfigNameInternal() = 0;
431
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000432 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000433};
434
scroggo@google.com9a412522012-09-07 15:21:18 +0000435/**
436 * This class does not do any rendering, but its render function executes recording, which we want
437 * to time.
438 */
439class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000440 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000441
442 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
443
444 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000445
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000446protected:
447 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
448
scroggo@google.com0a049b82012-11-02 22:01:26 +0000449private:
450 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000451};
452
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000453class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000454public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000455 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000456
457private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000458 virtual SkString getConfigNameInternal() SK_OVERRIDE;
459
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000460 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000461};
462
463class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000464public:
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000465 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000466 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000467
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 TiledPictureRenderer : public PictureRenderer {
477public:
478 TiledPictureRenderer();
479
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000480 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000481 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000482
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000483 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000484 * Renders to tiles, rather than a single canvas.
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000485 * If fWritePath was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000486 * created for each tile, named "path0.png", "path1.png", etc.
487 * Multithreaded mode currently does not support writing to a file.
488 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000489 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000490
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000491 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000492
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000493 void setTileWidth(int width) {
494 fTileWidth = width;
495 }
496
497 int getTileWidth() const {
498 return fTileWidth;
499 }
500
501 void setTileHeight(int height) {
502 fTileHeight = height;
503 }
504
505 int getTileHeight() const {
506 return fTileHeight;
507 }
508
509 void setTileWidthPercentage(double percentage) {
510 fTileWidthPercentage = percentage;
511 }
512
keyar@chromium.org163b5672012-08-01 17:53:29 +0000513 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000514 return fTileWidthPercentage;
515 }
516
517 void setTileHeightPercentage(double percentage) {
518 fTileHeightPercentage = percentage;
519 }
520
keyar@chromium.org163b5672012-08-01 17:53:29 +0000521 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000522 return fTileHeightPercentage;
523 }
524
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000525 void setTileMinPowerOf2Width(int width) {
526 SkASSERT(SkIsPow2(width) && width > 0);
527 if (!SkIsPow2(width) || width <= 0) {
528 return;
529 }
530
531 fTileMinPowerOf2Width = width;
532 }
533
534 int getTileMinPowerOf2Width() const {
535 return fTileMinPowerOf2Width;
536 }
537
scroggo@google.comcbcef702012-12-13 22:09:28 +0000538 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
539
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000540 virtual bool supportsTimingIndividualTiles() { return true; }
541
scroggo@google.comcbcef702012-12-13 22:09:28 +0000542 /**
543 * Report the number of tiles in the x and y directions. Must not be called before init.
544 * @param x Output parameter identifying the number of tiles in the x direction.
545 * @param y Output parameter identifying the number of tiles in the y direction.
546 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
547 * unmodified.
548 */
549 bool tileDimensions(int& x, int&y);
550
551 /**
552 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
553 * for the first time.
554 * @param i Output parameter identifying the column of the next tile to be drawn on the next
555 * call to drawNextTile.
556 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
557 * to drawNextTile.
558 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
559 * is within the range of tiles. If false, i and j are unmodified.
560 */
561 bool nextTile(int& i, int& j);
562
563 /**
564 * Render one tile. This will draw the same tile each time it is called until nextTile is
565 * called. The tile rendered will depend on how many calls have been made to nextTile.
566 * It is an error to call this without first calling nextTile, or if nextTile returns false.
567 */
568 void drawCurrentTile();
569
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000570protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000571 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000572
scroggo@google.com0a049b82012-11-02 22:01:26 +0000573 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
574 virtual SkString getConfigNameInternal() SK_OVERRIDE;
575
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000576private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000577 int fTileWidth;
578 int fTileHeight;
579 double fTileWidthPercentage;
580 double fTileHeightPercentage;
581 int fTileMinPowerOf2Width;
582
583 // These variables are only used for timing individual tiles.
584 // Next tile to draw in fTileRects.
585 int fCurrentTileOffset;
586 // Number of tiles in the x direction.
587 int fTilesX;
588 // Number of tiles in the y direction.
589 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000590
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000591 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000592 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000593
594 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000595};
596
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000597class CloneData;
598
599class MultiCorePictureRenderer : public TiledPictureRenderer {
600public:
601 explicit MultiCorePictureRenderer(int threadCount);
602
603 ~MultiCorePictureRenderer();
604
commit-bot@chromium.org3f045172014-05-15 15:10:48 +0000605 virtual void init(SkPicture* pict, const SkString* writePath, const SkString* mismatchPath,
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000606 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000607
608 /**
609 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
610 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000611 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000612
613 virtual void end() SK_OVERRIDE;
614
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000615 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
616
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000617private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000618 virtual SkString getConfigNameInternal() SK_OVERRIDE;
619
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000620 const int fNumThreads;
621 SkTDArray<SkCanvas*> fCanvasPool;
622 SkThreadPool fThreadPool;
623 SkPicture* fPictureClones;
624 CloneData** fCloneData;
625 SkCountdown fCountdown;
626
627 typedef TiledPictureRenderer INHERITED;
628};
629
scroggo@google.com9a412522012-09-07 15:21:18 +0000630/**
631 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
632 * into an SkPicturePlayback, which we want to time.
633 */
634class PlaybackCreationRenderer : public PictureRenderer {
635public:
636 virtual void setup() SK_OVERRIDE;
637
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000638 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000639
640 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
641
642 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
643
644private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000645 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000646
647 virtual SkString getConfigNameInternal() SK_OVERRIDE;
648
scroggo@google.com9a412522012-09-07 15:21:18 +0000649 typedef PictureRenderer INHERITED;
650};
651
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000652extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000653extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000654
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000655}
656
657#endif // PictureRenderer_DEFINED