blob: 342df7872abb417ded8dcc2a133d17b343fee2d8 [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.
87 * @param outputDir The output directory within which this renderer should write files,
88 * or NULL if this renderer should not write files at all.
89 * @param inputFilename The name of the input file we are rendering.
90 * @param useChecksumBasedFilenames Whether to use checksum-based filenames when writing
91 * bitmap images to disk.
scroggo@google.coma62da2f2012-11-02 21:28:12 +000092 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000093 virtual void init(SkPicture* pict, const SkString* outputDir,
94 const SkString* inputFilename, bool useChecksumBasedFilenames);
scroggo@google.com9a412522012-09-07 15:21:18 +000095
96 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +000097 * Set the viewport so that only the portion listed gets drawn.
98 */
99 void setViewport(SkISize size) { fViewport = size; }
100
101 /**
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000102 * Set the scale factor at which draw the picture.
103 */
104 void setScaleFactor(SkScalar scale) { fScaleFactor = scale; }
105
106 /**
scroggo@google.com9a412522012-09-07 15:21:18 +0000107 * Perform any setup that should done prior to each iteration of render() which should not be
108 * timed.
109 */
110 virtual void setup() {}
111
112 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000113 * Perform the work. If this is being called within the context of bench_pictures,
114 * this is the step that will be timed.
115 *
116 * Typically "the work" is rendering an SkPicture into a bitmap, but in some subclasses
117 * it is recording the source SkPicture into another SkPicture.
118 *
119 * If fOutputDir has been specified, the result of the work will be written to that dir.
120 *
121 * @param out If non-null, the implementing subclass MAY allocate an SkBitmap, copy the
122 * output image into it, and return it here. (Some subclasses ignore this parameter)
123 * @return bool True if rendering succeeded and, if fOutputDir had been specified, the output
124 * was successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +0000125 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000126 virtual bool render(SkBitmap** out = NULL) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +0000127
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000128 /**
129 * Called once finished with a particular SkPicture, before calling init again, and before
130 * being done with this Renderer.
131 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000132 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000133
scroggo@google.comcbcef702012-12-13 22:09:28 +0000134 /**
135 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
136 * TiledPictureRender so its methods can be called.
137 */
138 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
139
scroggo@google.com08085f82013-01-28 20:40:24 +0000140 /**
141 * 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 +0000142 * flush, swapBuffers and, if callFinish is true, finish.
scroggo@google.com08085f82013-01-28 20:40:24 +0000143 * @param callFinish Whether to call finish.
144 */
145 void resetState(bool callFinish);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000146
scroggo@google.com0556ea02013-02-08 19:38:21 +0000147 /**
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +0000148 * Remove all decoded textures from the CPU caches and all uploaded textures
149 * from the GPU.
150 */
151 void purgeTextures();
152
153 /**
scroggo@google.com0556ea02013-02-08 19:38:21 +0000154 * Set the backend type. Returns true on success and false on failure.
155 */
156 bool setDeviceType(SkDeviceTypes deviceType) {
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000157 fDeviceType = deviceType;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000158#if SK_SUPPORT_GPU
159 // In case this function is called more than once
160 SkSafeUnref(fGrContext);
161 fGrContext = NULL;
162 // Set to Native so it will have an initial value.
163 GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType;
164#endif
165 switch(deviceType) {
166 case kBitmap_DeviceType:
167 return true;
168#if SK_SUPPORT_GPU
169 case kGPU_DeviceType:
170 // Already set to GrContextFactory::kNative_GLContextType, above.
171 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000172 case kNVPR_DeviceType:
173 glContextType = GrContextFactory::kNVPR_GLContextType;
174 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000175#if SK_ANGLE
176 case kAngle_DeviceType:
177 glContextType = GrContextFactory::kANGLE_GLContextType;
178 break;
179#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000180#if SK_MESA
181 case kMesa_DeviceType:
182 glContextType = GrContextFactory::kMESA_GLContextType;
183 break;
184#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000185#endif
186 default:
187 // Invalid device type.
188 return false;
189 }
190#if SK_SUPPORT_GPU
191 fGrContext = fGrContextFactory.get(glContextType);
192 if (NULL == fGrContext) {
193 return false;
194 } else {
195 fGrContext->ref();
196 return true;
197 }
198#endif
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000199 }
200
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000201#if SK_SUPPORT_GPU
202 void setSampleCount(int sampleCount) {
203 fSampleCount = sampleCount;
204 }
205#endif
206
caryclark@google.come3e940c2012-11-07 16:42:17 +0000207 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
208 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000209 fDrawFiltersConfig = configName;
210 }
211
junov@chromium.org9313ca42012-11-02 18:11:49 +0000212 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
213 fBBoxHierarchyType = bbhType;
214 }
215
junov@chromium.orge286e842013-03-13 17:27:16 +0000216 BBoxHierarchyType getBBoxHierarchyType() { return fBBoxHierarchyType; }
217
junov@chromium.org7b537062012-11-06 18:58:43 +0000218 void setGridSize(int width, int height) {
junov@chromium.org29b19e52013-02-27 18:35:16 +0000219 fGridInfo.fTileInterval.set(width, height);
junov@chromium.org7b537062012-11-06 18:58:43 +0000220 }
221
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000222 void setJsonSummaryPtr(ImageResultsAndExpectations* jsonSummaryPtr) {
commit-bot@chromium.orga3f882c2013-12-13 20:52:36 +0000223 fJsonSummaryPtr = jsonSummaryPtr;
224 }
225
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000226 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000227 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000228 }
229
scroggo@google.com9a412522012-09-07 15:21:18 +0000230 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
231
232 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
233
scroggo@google.com0a049b82012-11-02 22:01:26 +0000234 /**
235 * Reports the configuration of this PictureRenderer.
236 */
237 SkString getConfigName() {
238 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000239 if (!fViewport.isEmpty()) {
240 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
241 }
commit-bot@chromium.org9de35eb2013-12-20 21:49:33 +0000242 if (fScaleFactor != SK_Scalar1) {
243 config.appendf("_scalar_%f", SkScalarToFloat(fScaleFactor));
244 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000245 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
246 config.append("_rtree");
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000247 } else if (kQuadTree_BBoxHierarchyType == fBBoxHierarchyType) {
248 config.append("_quadtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000249 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
250 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000251 }
252#if SK_SUPPORT_GPU
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000253 switch (fDeviceType) {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000254 case kGPU_DeviceType:
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000255 if (fSampleCount) {
256 config.appendf("_msaa%d", fSampleCount);
257 } else {
258 config.append("_gpu");
259 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000260 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000261 case kNVPR_DeviceType:
262 config.appendf("_nvprmsaa%d", fSampleCount);
263 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000264#if SK_ANGLE
265 case kAngle_DeviceType:
266 config.append("_angle");
267 break;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000268#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000269#if SK_MESA
270 case kMesa_DeviceType:
271 config.append("_mesa");
272 break;
273#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000274 default:
275 // Assume that no extra info means bitmap.
276 break;
277 }
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +0000278#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000279 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000280 return config;
281 }
282
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000283#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000284 bool isUsingGpuDevice() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000285 switch (fDeviceType) {
286 case kGPU_DeviceType:
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000287 case kNVPR_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000288 // fall through
289#if SK_ANGLE
290 case kAngle_DeviceType:
rmistry@google.com6ab96732014-01-06 18:37:24 +0000291 // fall through
292#endif
293#if SK_MESA
294 case kMesa_DeviceType:
scroggo@google.com0556ea02013-02-08 19:38:21 +0000295#endif
296 return true;
297 default:
298 return false;
299 }
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000300 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000301
robertphillips@google.com6177e692013-02-28 20:16:25 +0000302 SkGLContextHelper* getGLContext() {
scroggo@google.com0556ea02013-02-08 19:38:21 +0000303 GrContextFactory::GLContextType glContextType
304 = GrContextFactory::kNull_GLContextType;
305 switch(fDeviceType) {
306 case kGPU_DeviceType:
307 glContextType = GrContextFactory::kNative_GLContextType;
308 break;
commit-bot@chromium.org0fd52702014-03-07 18:41:14 +0000309 case kNVPR_DeviceType:
310 glContextType = GrContextFactory::kNVPR_GLContextType;
311 break;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000312#if SK_ANGLE
313 case kAngle_DeviceType:
314 glContextType = GrContextFactory::kANGLE_GLContextType;
315 break;
316#endif
rmistry@google.com6ab96732014-01-06 18:37:24 +0000317#if SK_MESA
318 case kMesa_DeviceType:
319 glContextType = GrContextFactory::kMESA_GLContextType;
320 break;
321#endif
scroggo@google.com0556ea02013-02-08 19:38:21 +0000322 default:
323 return NULL;
keyar@chromium.org77a55222012-08-20 15:03:47 +0000324 }
scroggo@google.com0556ea02013-02-08 19:38:21 +0000325 return fGrContextFactory.getGLContext(glContextType);
keyar@chromium.org77a55222012-08-20 15:03:47 +0000326 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000327
328 GrContext* getGrContext() {
329 return fGrContext;
330 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000331#endif
332
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000333 SkCanvas* getCanvas() {
334 return fCanvas;
335 }
336
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +0000337 SkPicture* getPicture() {
338 return fPicture;
339 }
340
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000341 PictureRenderer()
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000342 : fJsonSummaryPtr(NULL)
keyar@chromium.org06125642012-08-20 15:03:33 +0000343 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000344 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
scroggo@google.com06d6ac62013-02-08 21:16:19 +0000345 , fScaleFactor(SK_Scalar1)
keyar@chromium.org06125642012-08-20 15:03:33 +0000346#if SK_SUPPORT_GPU
scroggo@google.com0556ea02013-02-08 19:38:21 +0000347 , fGrContext(NULL)
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000348 , fSampleCount(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000349#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000350 {
robertphillips@google.com7ae918e2013-03-02 17:45:27 +0000351 fGridInfo.fMargin.setEmpty();
352 fGridInfo.fOffset.setZero();
353 fGridInfo.fTileInterval.set(1, 1);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000354 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000355 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000356 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000357
scroggo@google.com0556ea02013-02-08 19:38:21 +0000358#if SK_SUPPORT_GPU
359 virtual ~PictureRenderer() {
360 SkSafeUnref(fGrContext);
361 }
362#endif
363
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000364protected:
365 SkAutoTUnref<SkCanvas> fCanvas;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000366 SkAutoTUnref<SkPicture> fPicture;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000367 bool fUseChecksumBasedFilenames;
commit-bot@chromium.org205ce482014-05-12 15:37:20 +0000368 ImageResultsAndExpectations* fJsonSummaryPtr;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000369 SkDeviceTypes fDeviceType;
370 BBoxHierarchyType fBBoxHierarchyType;
371 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
372 SkString fDrawFiltersConfig;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000373 SkString fOutputDir;
374 SkString fInputFilename;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000375 SkTileGridFactory::TileGridInfo fGridInfo; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000376
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000377 void buildBBoxHierarchy();
378
379 /**
380 * Return the total width that should be drawn. If the viewport width has been set greater than
381 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
382 */
383 int getViewWidth();
384
385 /**
386 * Return the total height that should be drawn. If the viewport height has been set greater
387 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
388 */
389 int getViewHeight();
390
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000391 /**
392 * Scales the provided canvas to the scale factor set by setScaleFactor.
393 */
394 void scaleToScaleFactor(SkCanvas*);
395
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000396 SkBBHFactory* getFactory();
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000397 uint32_t recordFlags();
398 SkCanvas* setupCanvas();
399 virtual SkCanvas* setupCanvas(int width, int height);
400
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000401 /**
402 * Copy src to dest; if src==NULL, set dest to empty string.
403 */
404 static void CopyString(SkString* dest, const SkString* src);
405
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000406private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000407 SkISize fViewport;
scroggo@google.com82ec0b02012-12-17 19:25:54 +0000408 SkScalar fScaleFactor;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000409#if SK_SUPPORT_GPU
410 GrContextFactory fGrContextFactory;
411 GrContext* fGrContext;
jvanverth@google.comf6a90332013-05-02 12:39:37 +0000412 int fSampleCount;
scroggo@google.com0556ea02013-02-08 19:38:21 +0000413#endif
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000414
scroggo@google.com0a049b82012-11-02 22:01:26 +0000415 virtual SkString getConfigNameInternal() = 0;
416
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000417 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000418};
419
scroggo@google.com9a412522012-09-07 15:21:18 +0000420/**
421 * This class does not do any rendering, but its render function executes recording, which we want
422 * to time.
423 */
424class RecordPictureRenderer : public PictureRenderer {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000425 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000426
427 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
428
429 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000430
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000431protected:
432 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
433
scroggo@google.com0a049b82012-11-02 22:01:26 +0000434private:
435 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000436};
437
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000438class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000439public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000440 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000441
442private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000443 virtual SkString getConfigNameInternal() SK_OVERRIDE;
444
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000445 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000446};
447
448class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000449public:
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000450 virtual void init(SkPicture* pict, const SkString* outputDir,
451 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000452
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000453 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000454
455private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000456 virtual SkString getConfigNameInternal() SK_OVERRIDE;
457
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000458 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000459};
460
461class TiledPictureRenderer : public PictureRenderer {
462public:
463 TiledPictureRenderer();
464
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000465 virtual void init(SkPicture* pict, const SkString* outputDir,
466 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000467
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000468 /**
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000469 * Renders to tiles, rather than a single canvas.
470 * If fOutputDir was provided, a separate file is
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000471 * created for each tile, named "path0.png", "path1.png", etc.
472 * Multithreaded mode currently does not support writing to a file.
473 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000474 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000475
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000476 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000477
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000478 void setTileWidth(int width) {
479 fTileWidth = width;
480 }
481
482 int getTileWidth() const {
483 return fTileWidth;
484 }
485
486 void setTileHeight(int height) {
487 fTileHeight = height;
488 }
489
490 int getTileHeight() const {
491 return fTileHeight;
492 }
493
494 void setTileWidthPercentage(double percentage) {
495 fTileWidthPercentage = percentage;
496 }
497
keyar@chromium.org163b5672012-08-01 17:53:29 +0000498 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000499 return fTileWidthPercentage;
500 }
501
502 void setTileHeightPercentage(double percentage) {
503 fTileHeightPercentage = percentage;
504 }
505
keyar@chromium.org163b5672012-08-01 17:53:29 +0000506 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000507 return fTileHeightPercentage;
508 }
509
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000510 void setTileMinPowerOf2Width(int width) {
511 SkASSERT(SkIsPow2(width) && width > 0);
512 if (!SkIsPow2(width) || width <= 0) {
513 return;
514 }
515
516 fTileMinPowerOf2Width = width;
517 }
518
519 int getTileMinPowerOf2Width() const {
520 return fTileMinPowerOf2Width;
521 }
522
scroggo@google.comcbcef702012-12-13 22:09:28 +0000523 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
524
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000525 virtual bool supportsTimingIndividualTiles() { return true; }
526
scroggo@google.comcbcef702012-12-13 22:09:28 +0000527 /**
528 * Report the number of tiles in the x and y directions. Must not be called before init.
529 * @param x Output parameter identifying the number of tiles in the x direction.
530 * @param y Output parameter identifying the number of tiles in the y direction.
531 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
532 * unmodified.
533 */
534 bool tileDimensions(int& x, int&y);
535
536 /**
537 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
538 * for the first time.
539 * @param i Output parameter identifying the column of the next tile to be drawn on the next
540 * call to drawNextTile.
541 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
542 * to drawNextTile.
543 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
544 * is within the range of tiles. If false, i and j are unmodified.
545 */
546 bool nextTile(int& i, int& j);
547
548 /**
549 * Render one tile. This will draw the same tile each time it is called until nextTile is
550 * called. The tile rendered will depend on how many calls have been made to nextTile.
551 * It is an error to call this without first calling nextTile, or if nextTile returns false.
552 */
553 void drawCurrentTile();
554
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000555protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000556 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000557
scroggo@google.com0a049b82012-11-02 22:01:26 +0000558 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
559 virtual SkString getConfigNameInternal() SK_OVERRIDE;
560
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000561private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000562 int fTileWidth;
563 int fTileHeight;
564 double fTileWidthPercentage;
565 double fTileHeightPercentage;
566 int fTileMinPowerOf2Width;
567
568 // These variables are only used for timing individual tiles.
569 // Next tile to draw in fTileRects.
570 int fCurrentTileOffset;
571 // Number of tiles in the x direction.
572 int fTilesX;
573 // Number of tiles in the y direction.
574 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000575
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000576 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000577 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000578
579 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000580};
581
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000582class CloneData;
583
584class MultiCorePictureRenderer : public TiledPictureRenderer {
585public:
586 explicit MultiCorePictureRenderer(int threadCount);
587
588 ~MultiCorePictureRenderer();
589
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000590 virtual void init(SkPicture* pict, const SkString* outputDir,
591 const SkString* inputFilename, bool useChecksumBasedFilenames) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000592
593 /**
594 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
595 */
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000596 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000597
598 virtual void end() SK_OVERRIDE;
599
scroggo@google.com161e1ba2013-03-04 16:41:06 +0000600 virtual bool supportsTimingIndividualTiles() SK_OVERRIDE { return false; }
601
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000602private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000603 virtual SkString getConfigNameInternal() SK_OVERRIDE;
604
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000605 const int fNumThreads;
606 SkTDArray<SkCanvas*> fCanvasPool;
607 SkThreadPool fThreadPool;
608 SkPicture* fPictureClones;
609 CloneData** fCloneData;
610 SkCountdown fCountdown;
611
612 typedef TiledPictureRenderer INHERITED;
613};
614
scroggo@google.com9a412522012-09-07 15:21:18 +0000615/**
616 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
617 * into an SkPicturePlayback, which we want to time.
618 */
619class PlaybackCreationRenderer : public PictureRenderer {
620public:
621 virtual void setup() SK_OVERRIDE;
622
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +0000623 virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000624
625 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
626
627 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
628
629private:
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000630 SkAutoTDelete<SkPictureRecorder> fRecorder;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000631
632 virtual SkString getConfigNameInternal() SK_OVERRIDE;
633
scroggo@google.com9a412522012-09-07 15:21:18 +0000634 typedef PictureRenderer INHERITED;
635};
636
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000637extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000638extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000639
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000640}
641
642#endif // PictureRenderer_DEFINED