blob: 284f0684fd866e2052de38d999f1c4b4496b3ad5 [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
11#include "SkCountdown.h"
caryclark@google.coma3622372012-11-06 21:26:13 +000012#include "SkDrawFilter.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000013#include "SkMath.h"
reed@google.comea6a3062012-11-06 22:14:54 +000014#include "SkPaint.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000015#include "SkPicture.h"
scroggo@google.comacfb30e2012-09-18 14:32:35 +000016#include "SkRect.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000017#include "SkRefCnt.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000018#include "SkRunnable.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000019#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000020#include "SkTDArray.h"
21#include "SkThreadPool.h"
22#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000023
keyar@chromium.org06125642012-08-20 15:03:33 +000024#if SK_SUPPORT_GPU
25#include "GrContextFactory.h"
26#include "GrContext.h"
27#endif
28
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000029class SkBitmap;
30class SkCanvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000031class SkGLContext;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000032class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000033
34namespace sk_tools {
35
scroggo@google.comcbcef702012-12-13 22:09:28 +000036class TiledPictureRenderer;
37
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000038class PictureRenderer : public SkRefCnt {
scroggo@google.comcbcef702012-12-13 22:09:28 +000039
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000040public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000041 enum SkDeviceTypes {
42 kBitmap_DeviceType,
43#if SK_SUPPORT_GPU
44 kGPU_DeviceType
45#endif
46 };
47
junov@chromium.org9313ca42012-11-02 18:11:49 +000048 enum BBoxHierarchyType {
49 kNone_BBoxHierarchyType = 0,
50 kRTree_BBoxHierarchyType,
junov@chromium.org7b537062012-11-06 18:58:43 +000051 kTileGrid_BBoxHierarchyType,
junov@chromium.org9313ca42012-11-02 18:11:49 +000052 };
53
caryclark@google.coma3622372012-11-06 21:26:13 +000054 // this uses SkPaint::Flags as a base and adds additional flags
55 enum DrawFilterFlags {
56 kNone_DrawFilterFlag = 0,
caryclark@google.come3e940c2012-11-07 16:42:17 +000057 kBlur_DrawFilterFlag = 0x4000, // toggles between blur and no blur
58 kLowBlur_DrawFilterFlag = 0x8000, // toggles between low and high quality blur
59 kHinting_DrawFilterFlag = 0x10000, // toggles between no hinting and normal hinting
60 kSlightHinting_DrawFilterFlag = 0x20000, // toggles between slight and normal hinting
61 kAAClip_DrawFilterFlag = 0x40000, // toggles between soft and hard clip
caryclark@google.coma3622372012-11-06 21:26:13 +000062 };
63
64 SK_COMPILE_ASSERT(!(kBlur_DrawFilterFlag & SkPaint::kAllFlags), blur_flag_must_be_greater);
65 SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags),
66 hinting_flag_must_be_greater);
67 SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags),
68 slight_hinting_flag_must_be_greater);
69
scroggo@google.coma62da2f2012-11-02 21:28:12 +000070 /**
71 * Called with each new SkPicture to render.
72 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +000073 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +000074
75 /**
scroggo@google.comc0d5e542012-12-13 21:40:48 +000076 * Set the viewport so that only the portion listed gets drawn.
77 */
78 void setViewport(SkISize size) { fViewport = size; }
79
80 /**
scroggo@google.com9a412522012-09-07 15:21:18 +000081 * Perform any setup that should done prior to each iteration of render() which should not be
82 * timed.
83 */
84 virtual void setup() {}
85
86 /**
87 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
88 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000089 * If path is non-null, subclass implementations should call write().
90 * @param path If non-null, also write the output to the file specified by path. path should
91 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +000092 * @return bool True if rendering succeeded and, if path is non-null, the output was
93 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +000094 */
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000095 virtual bool render(const SkString* path) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +000096
scroggo@google.coma62da2f2012-11-02 21:28:12 +000097 /**
98 * Called once finished with a particular SkPicture, before calling init again, and before
99 * being done with this Renderer.
100 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000101 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000102
scroggo@google.comcbcef702012-12-13 22:09:28 +0000103 /**
104 * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a
105 * TiledPictureRender so its methods can be called.
106 */
107 virtual TiledPictureRenderer* getTiledRenderer() { return NULL; }
108
keyar@chromium.org28136b32012-08-20 15:04:15 +0000109 void resetState();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000110
keyar@chromium.orgc81686c2012-08-20 15:04:04 +0000111 void setDeviceType(SkDeviceTypes deviceType) {
112 fDeviceType = deviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000113 }
114
caryclark@google.come3e940c2012-11-07 16:42:17 +0000115 void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) {
116 memcpy(fDrawFilters, filters, sizeof(fDrawFilters));
caryclark@google.coma3622372012-11-06 21:26:13 +0000117 fDrawFiltersConfig = configName;
118 }
119
junov@chromium.org9313ca42012-11-02 18:11:49 +0000120 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
121 fBBoxHierarchyType = bbhType;
122 }
123
junov@chromium.org7b537062012-11-06 18:58:43 +0000124 void setGridSize(int width, int height) {
125 fGridWidth = width;
126 fGridHeight = height;
127 }
128
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000129 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000130 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000131 }
132
scroggo@google.com9a412522012-09-07 15:21:18 +0000133 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
134
135 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
136
scroggo@google.com0a049b82012-11-02 22:01:26 +0000137 /**
138 * Reports the configuration of this PictureRenderer.
139 */
140 SkString getConfigName() {
141 SkString config = this->getConfigNameInternal();
scroggo@google.comc4013c12012-12-13 22:07:08 +0000142 if (!fViewport.isEmpty()) {
143 config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height());
144 }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000145 if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) {
146 config.append("_rtree");
junov@chromium.org7b537062012-11-06 18:58:43 +0000147 } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) {
148 config.append("_grid");
scroggo@google.com0a049b82012-11-02 22:01:26 +0000149 }
150#if SK_SUPPORT_GPU
151 if (this->isUsingGpuDevice()) {
152 config.append("_gpu");
153 }
154#endif
caryclark@google.coma3622372012-11-06 21:26:13 +0000155 config.append(fDrawFiltersConfig.c_str());
scroggo@google.com0a049b82012-11-02 22:01:26 +0000156 return config;
157 }
158
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000159#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000160 bool isUsingGpuDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +0000161 return kGPU_DeviceType == fDeviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +0000162 }
keyar@chromium.org77a55222012-08-20 15:03:47 +0000163
164 SkGLContext* getGLContext() {
165 if (this->isUsingGpuDevice()) {
166 return fGrContextFactory.getGLContext(GrContextFactory::kNative_GLContextType);
167 } else {
168 return NULL;
169 }
170 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000171
172 GrContext* getGrContext() {
173 return fGrContext;
174 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000175#endif
176
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000177 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000178 : fPicture(NULL)
179 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000180 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
junov@chromium.org7b537062012-11-06 18:58:43 +0000181 , fGridWidth(0)
182 , fGridHeight(0)
keyar@chromium.org06125642012-08-20 15:03:33 +0000183#if SK_SUPPORT_GPU
184 , fGrContext(fGrContextFactory.get(GrContextFactory::kNative_GLContextType))
185#endif
caryclark@google.come3e940c2012-11-07 16:42:17 +0000186 {
187 sk_bzero(fDrawFilters, sizeof(fDrawFilters));
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000188 fViewport.set(0, 0);
caryclark@google.come3e940c2012-11-07 16:42:17 +0000189 }
keyar@chromium.org06125642012-08-20 15:03:33 +0000190
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000191protected:
192 SkAutoTUnref<SkCanvas> fCanvas;
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000193 SkPicture* fPicture;
194 SkDeviceTypes fDeviceType;
195 BBoxHierarchyType fBBoxHierarchyType;
196 DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount];
197 SkString fDrawFiltersConfig;
198 int fGridWidth, fGridHeight; // used when fBBoxHierarchyType is TileGrid
keyar@chromium.org06125642012-08-20 15:03:33 +0000199
200#if SK_SUPPORT_GPU
201 GrContextFactory fGrContextFactory;
202 GrContext* fGrContext;
203#endif
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000204
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000205 void buildBBoxHierarchy();
206
207 /**
208 * Return the total width that should be drawn. If the viewport width has been set greater than
209 * 0, this will be the minimum of the current SkPicture's width and the viewport's width.
210 */
211 int getViewWidth();
212
213 /**
214 * Return the total height that should be drawn. If the viewport height has been set greater
215 * than 0, this will be the minimum of the current SkPicture's height and the viewport's height.
216 */
217 int getViewHeight();
218
219 SkPicture* createPicture();
220 uint32_t recordFlags();
221 SkCanvas* setupCanvas();
222 virtual SkCanvas* setupCanvas(int width, int height);
223
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000224private:
scroggo@google.comc0d5e542012-12-13 21:40:48 +0000225 SkISize fViewport;
226
scroggo@google.com0a049b82012-11-02 22:01:26 +0000227 virtual SkString getConfigNameInternal() = 0;
228
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000229 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000230};
231
scroggo@google.com9a412522012-09-07 15:21:18 +0000232/**
233 * This class does not do any rendering, but its render function executes recording, which we want
234 * to time.
235 */
236class RecordPictureRenderer : public PictureRenderer {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000237 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000238
239 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
240
241 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
scroggo@google.com0a049b82012-11-02 22:01:26 +0000242
djsollen@google.comfd9720c2012-11-06 16:54:40 +0000243protected:
244 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
245
scroggo@google.com0a049b82012-11-02 22:01:26 +0000246private:
247 virtual SkString getConfigNameInternal() SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000248};
249
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000250class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000251public:
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000252 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000253
254private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000255 virtual SkString getConfigNameInternal() SK_OVERRIDE;
256
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000257 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000258};
259
260class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000261public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000262 virtual void init(SkPicture* pict) SK_OVERRIDE;
263
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000264 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000265
266private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000267 virtual SkString getConfigNameInternal() SK_OVERRIDE;
268
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000269 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000270};
271
272class TiledPictureRenderer : public PictureRenderer {
273public:
274 TiledPictureRenderer();
275
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000276 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000277
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000278 /**
279 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
280 * created for each tile, named "path0.png", "path1.png", etc.
281 * Multithreaded mode currently does not support writing to a file.
282 */
283 virtual bool render(const SkString* path) SK_OVERRIDE;
284
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000285 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000286
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000287 void setTileWidth(int width) {
288 fTileWidth = width;
289 }
290
291 int getTileWidth() const {
292 return fTileWidth;
293 }
294
295 void setTileHeight(int height) {
296 fTileHeight = height;
297 }
298
299 int getTileHeight() const {
300 return fTileHeight;
301 }
302
303 void setTileWidthPercentage(double percentage) {
304 fTileWidthPercentage = percentage;
305 }
306
keyar@chromium.org163b5672012-08-01 17:53:29 +0000307 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000308 return fTileWidthPercentage;
309 }
310
311 void setTileHeightPercentage(double percentage) {
312 fTileHeightPercentage = percentage;
313 }
314
keyar@chromium.org163b5672012-08-01 17:53:29 +0000315 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000316 return fTileHeightPercentage;
317 }
318
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000319 void setTileMinPowerOf2Width(int width) {
320 SkASSERT(SkIsPow2(width) && width > 0);
321 if (!SkIsPow2(width) || width <= 0) {
322 return;
323 }
324
325 fTileMinPowerOf2Width = width;
326 }
327
328 int getTileMinPowerOf2Width() const {
329 return fTileMinPowerOf2Width;
330 }
331
scroggo@google.comcbcef702012-12-13 22:09:28 +0000332 virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; }
333
334 /**
335 * Report the number of tiles in the x and y directions. Must not be called before init.
336 * @param x Output parameter identifying the number of tiles in the x direction.
337 * @param y Output parameter identifying the number of tiles in the y direction.
338 * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are
339 * unmodified.
340 */
341 bool tileDimensions(int& x, int&y);
342
343 /**
344 * Move to the next tile and return its indices. Must be called before calling drawCurrentTile
345 * for the first time.
346 * @param i Output parameter identifying the column of the next tile to be drawn on the next
347 * call to drawNextTile.
348 * @param j Output parameter identifying the row of the next tile to be drawn on the next call
349 * to drawNextTile.
350 * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile
351 * is within the range of tiles. If false, i and j are unmodified.
352 */
353 bool nextTile(int& i, int& j);
354
355 /**
356 * Render one tile. This will draw the same tile each time it is called until nextTile is
357 * called. The tile rendered will depend on how many calls have been made to nextTile.
358 * It is an error to call this without first calling nextTile, or if nextTile returns false.
359 */
360 void drawCurrentTile();
361
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000362protected:
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000363 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000364
scroggo@google.com0a049b82012-11-02 22:01:26 +0000365 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
366 virtual SkString getConfigNameInternal() SK_OVERRIDE;
367
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000368private:
scroggo@google.comcbcef702012-12-13 22:09:28 +0000369 int fTileWidth;
370 int fTileHeight;
371 double fTileWidthPercentage;
372 double fTileHeightPercentage;
373 int fTileMinPowerOf2Width;
374
375 // These variables are only used for timing individual tiles.
376 // Next tile to draw in fTileRects.
377 int fCurrentTileOffset;
378 // Number of tiles in the x direction.
379 int fTilesX;
380 // Number of tiles in the y direction.
381 int fTilesY;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000382
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000383 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000384 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000385
386 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000387};
388
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000389class CloneData;
390
391class MultiCorePictureRenderer : public TiledPictureRenderer {
392public:
393 explicit MultiCorePictureRenderer(int threadCount);
394
395 ~MultiCorePictureRenderer();
396
397 virtual void init(SkPicture* pict) SK_OVERRIDE;
398
399 /**
400 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
401 */
402 virtual bool render(const SkString* path) SK_OVERRIDE;
403
404 virtual void end() SK_OVERRIDE;
405
406private:
scroggo@google.com0a049b82012-11-02 22:01:26 +0000407 virtual SkString getConfigNameInternal() SK_OVERRIDE;
408
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000409 const int fNumThreads;
410 SkTDArray<SkCanvas*> fCanvasPool;
411 SkThreadPool fThreadPool;
412 SkPicture* fPictureClones;
413 CloneData** fCloneData;
414 SkCountdown fCountdown;
415
416 typedef TiledPictureRenderer INHERITED;
417};
418
scroggo@google.com9a412522012-09-07 15:21:18 +0000419/**
420 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
421 * into an SkPicturePlayback, which we want to time.
422 */
423class PlaybackCreationRenderer : public PictureRenderer {
424public:
425 virtual void setup() SK_OVERRIDE;
426
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000427 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000428
429 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
430
431 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
432
433private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000434 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com0a049b82012-11-02 22:01:26 +0000435
436 virtual SkString getConfigNameInternal() SK_OVERRIDE;
437
scroggo@google.com9a412522012-09-07 15:21:18 +0000438 typedef PictureRenderer INHERITED;
439};
440
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000441extern PictureRenderer* CreateGatherPixelRefsRenderer();
reed@google.com5a34fd32012-12-10 16:05:09 +0000442extern PictureRenderer* CreatePictureCloneRenderer();
reed@google.comfe7b1ed2012-11-29 21:00:39 +0000443
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000444}
445
446#endif // PictureRenderer_DEFINED