blob: f8133ea4a4d2e593249bbb3a0c5145f2f3d7dc55 [file] [log] [blame]
keyar@chromium.org451bb9f2012-07-26 17:27:57 +00001/*
2 * 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
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000010#include "SkMath.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000011#include "SkPicture.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000012#include "SkTypes.h"
13#include "SkTDArray.h"
scroggo@google.comacfb30e2012-09-18 14:32:35 +000014#include "SkRect.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000015#include "SkRefCnt.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000016#include "SkString.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000017
keyar@chromium.org06125642012-08-20 15:03:33 +000018#if SK_SUPPORT_GPU
19#include "GrContextFactory.h"
20#include "GrContext.h"
21#endif
22
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000023class SkBitmap;
24class SkCanvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000025class SkGLContext;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +000026class ThreadSafePipeController;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000027
28namespace sk_tools {
29
30class PictureRenderer : public SkRefCnt {
31public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000032 enum SkDeviceTypes {
33 kBitmap_DeviceType,
34#if SK_SUPPORT_GPU
35 kGPU_DeviceType
36#endif
37 };
38
keyar@chromium.org9d696c02012-08-07 17:11:33 +000039 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +000040
41 /**
42 * Perform any setup that should done prior to each iteration of render() which should not be
43 * timed.
44 */
45 virtual void setup() {}
46
47 /**
48 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
49 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000050 * If path is non-null, subclass implementations should call write().
51 * @param path If non-null, also write the output to the file specified by path. path should
52 * have no extension; it will be added by write().
53 * @return bool True if path is non-null and the output is successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +000054 */
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000055 virtual bool render(const SkString* path) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +000056
keyar@chromium.org9d696c02012-08-07 17:11:33 +000057 virtual void end();
keyar@chromium.org28136b32012-08-20 15:04:15 +000058 void resetState();
keyar@chromium.org9d696c02012-08-07 17:11:33 +000059
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000060 void setDeviceType(SkDeviceTypes deviceType) {
61 fDeviceType = deviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000062 }
63
64 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +000065 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000066 }
67
scroggo@google.com9a412522012-09-07 15:21:18 +000068 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
69
70 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
71
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000072#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000073 bool isUsingGpuDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +000074 return kGPU_DeviceType == fDeviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000075 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000076
77 SkGLContext* getGLContext() {
78 if (this->isUsingGpuDevice()) {
79 return fGrContextFactory.getGLContext(GrContextFactory::kNative_GLContextType);
80 } else {
81 return NULL;
82 }
83 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +000084
85 GrContext* getGrContext() {
86 return fGrContext;
87 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000088#endif
89
keyar@chromium.org02dfb122012-08-20 15:03:36 +000090 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +000091 : fPicture(NULL)
92 , fDeviceType(kBitmap_DeviceType)
93#if SK_SUPPORT_GPU
94 , fGrContext(fGrContextFactory.get(GrContextFactory::kNative_GLContextType))
95#endif
96 {}
97
keyar@chromium.org9d696c02012-08-07 17:11:33 +000098protected:
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000099 /**
100 * Write the canvas to the specified path.
101 * @param canvas Must be non-null. Canvas to be written to a file.
102 * @param path Path for the file to be written. Should have no extension; write() will append
103 * an appropriate one.
104 */
105 bool write(SkCanvas* canvas, SkString path) const;
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000106 SkCanvas* setupCanvas();
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000107 virtual SkCanvas* setupCanvas(int width, int height);
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000108
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000109 SkAutoTUnref<SkCanvas> fCanvas;
110 SkPicture* fPicture;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000111 SkDeviceTypes fDeviceType;
keyar@chromium.org06125642012-08-20 15:03:33 +0000112
113#if SK_SUPPORT_GPU
114 GrContextFactory fGrContextFactory;
115 GrContext* fGrContext;
116#endif
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000117
118private:
119 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000120};
121
scroggo@google.com9a412522012-09-07 15:21:18 +0000122/**
123 * This class does not do any rendering, but its render function executes recording, which we want
124 * to time.
125 */
126class RecordPictureRenderer : public PictureRenderer {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000127 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000128
129 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
130
131 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
132};
133
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000134class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000135public:
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000136 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000137
138private:
139 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000140};
141
142class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000143public:
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000144 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000145
146private:
147 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000148};
149
150class TiledPictureRenderer : public PictureRenderer {
151public:
152 TiledPictureRenderer();
153
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000154 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000155
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000156 virtual void setup() SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000157
158 /**
159 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
160 * created for each tile, named "path0.png", "path1.png", etc.
161 * Multithreaded mode currently does not support writing to a file.
162 */
163 virtual bool render(const SkString* path) SK_OVERRIDE;
164
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000165 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000166
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000167 void setTileWidth(int width) {
168 fTileWidth = width;
169 }
170
171 int getTileWidth() const {
172 return fTileWidth;
173 }
174
175 void setTileHeight(int height) {
176 fTileHeight = height;
177 }
178
179 int getTileHeight() const {
180 return fTileHeight;
181 }
182
183 void setTileWidthPercentage(double percentage) {
184 fTileWidthPercentage = percentage;
185 }
186
keyar@chromium.org163b5672012-08-01 17:53:29 +0000187 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000188 return fTileWidthPercentage;
189 }
190
191 void setTileHeightPercentage(double percentage) {
192 fTileHeightPercentage = percentage;
193 }
194
keyar@chromium.org163b5672012-08-01 17:53:29 +0000195 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000196 return fTileHeightPercentage;
197 }
198
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000199 void setTileMinPowerOf2Width(int width) {
200 SkASSERT(SkIsPow2(width) && width > 0);
201 if (!SkIsPow2(width) || width <= 0) {
202 return;
203 }
204
205 fTileMinPowerOf2Width = width;
206 }
207
208 int getTileMinPowerOf2Width() const {
209 return fTileMinPowerOf2Width;
210 }
211
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000212 /**
213 * Set the number of threads to use for drawing. Non-positive numbers will set it to 1.
214 */
215 void setNumberOfThreads(int num) {
216 fNumThreads = SkMax32(num, 1);
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000217 }
218
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000219 void setUsePipe(bool usePipe) {
220 fUsePipe = usePipe;
221 }
222
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000223 ~TiledPictureRenderer();
224
225private:
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000226 bool fUsePipe;
227 int fTileWidth;
228 int fTileHeight;
229 double fTileWidthPercentage;
230 double fTileHeightPercentage;
231 int fTileMinPowerOf2Width;
scroggo@google.comacfb30e2012-09-18 14:32:35 +0000232 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000233
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000234 // These are only used for multithreaded rendering
235 int32_t fTileCounter;
236 int fNumThreads;
237 SkTDArray<SkCanvas*> fCanvasPool;
238 SkPicture* fPictureClones;
239 ThreadSafePipeController* fPipeController;
240
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000241 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000242 void setupPowerOf2Tiles();
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000243 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
244 bool multiThreaded() { return fNumThreads > 1; }
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000245
246 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000247};
248
scroggo@google.com9a412522012-09-07 15:21:18 +0000249/**
250 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
251 * into an SkPicturePlayback, which we want to time.
252 */
253class PlaybackCreationRenderer : public PictureRenderer {
254public:
255 virtual void setup() SK_OVERRIDE;
256
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000257 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000258
259 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
260
261 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
262
263private:
264 SkPicture fReplayer;
265 typedef PictureRenderer INHERITED;
266};
267
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000268}
269
270#endif // PictureRenderer_DEFINED