blob: 3ff8967d446147e6aed24950cf439097a62bfcda [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
scroggo@google.coma62da2f2012-11-02 21:28:12 +000010
11#include "SkCountdown.h"
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +000012#include "SkMath.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000013#include "SkPicture.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.coma62da2f2012-11-02 21:28:12 +000016#include "SkRunnable.h"
scroggo@google.com9a412522012-09-07 15:21:18 +000017#include "SkString.h"
scroggo@google.coma62da2f2012-11-02 21:28:12 +000018#include "SkTDArray.h"
19#include "SkThreadPool.h"
20#include "SkTypes.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000021
keyar@chromium.org06125642012-08-20 15:03:33 +000022#if SK_SUPPORT_GPU
23#include "GrContextFactory.h"
24#include "GrContext.h"
25#endif
26
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000027class SkBitmap;
28class SkCanvas;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000029class SkGLContext;
scroggo@google.coma62da2f2012-11-02 21:28:12 +000030class SkThread;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000031
32namespace sk_tools {
33
34class PictureRenderer : public SkRefCnt {
35public:
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000036 enum SkDeviceTypes {
37 kBitmap_DeviceType,
38#if SK_SUPPORT_GPU
39 kGPU_DeviceType
40#endif
41 };
42
junov@chromium.org9313ca42012-11-02 18:11:49 +000043 enum BBoxHierarchyType {
44 kNone_BBoxHierarchyType = 0,
45 kRTree_BBoxHierarchyType,
46 };
47
scroggo@google.coma62da2f2012-11-02 21:28:12 +000048 /**
49 * Called with each new SkPicture to render.
50 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +000051 virtual void init(SkPicture* pict);
scroggo@google.com9a412522012-09-07 15:21:18 +000052
53 /**
54 * Perform any setup that should done prior to each iteration of render() which should not be
55 * timed.
56 */
57 virtual void setup() {}
58
59 /**
60 * Perform work that is to be timed. Typically this is rendering, but is also used for recording
61 * and preparing picture for playback by the subclasses which do those.
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000062 * If path is non-null, subclass implementations should call write().
63 * @param path If non-null, also write the output to the file specified by path. path should
64 * have no extension; it will be added by write().
borenet@google.com070d3542012-10-26 13:26:55 +000065 * @return bool True if rendering succeeded and, if path is non-null, the output was
66 * successfully written to a file.
scroggo@google.com9a412522012-09-07 15:21:18 +000067 */
scroggo@google.com81f9d2e2012-09-20 14:54:21 +000068 virtual bool render(const SkString* path) = 0;
scroggo@google.com9a412522012-09-07 15:21:18 +000069
scroggo@google.coma62da2f2012-11-02 21:28:12 +000070 /**
71 * Called once finished with a particular SkPicture, before calling init again, and before
72 * being done with this Renderer.
73 */
keyar@chromium.org9d696c02012-08-07 17:11:33 +000074 virtual void end();
scroggo@google.coma62da2f2012-11-02 21:28:12 +000075
keyar@chromium.org28136b32012-08-20 15:04:15 +000076 void resetState();
keyar@chromium.org9d696c02012-08-07 17:11:33 +000077
keyar@chromium.orgc81686c2012-08-20 15:04:04 +000078 void setDeviceType(SkDeviceTypes deviceType) {
79 fDeviceType = deviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000080 }
81
junov@chromium.org9313ca42012-11-02 18:11:49 +000082 void setBBoxHierarchyType(BBoxHierarchyType bbhType) {
83 fBBoxHierarchyType = bbhType;
84 }
85
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000086 bool isUsingBitmapDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +000087 return kBitmap_DeviceType == fDeviceType;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000088 }
89
scroggo@google.com9a412522012-09-07 15:21:18 +000090 virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); }
91
92 virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); }
93
keyar@chromium.org4ea96c52012-08-20 15:03:29 +000094#if SK_SUPPORT_GPU
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000095 bool isUsingGpuDevice() {
keyar@chromium.org78a35c52012-08-20 15:03:44 +000096 return kGPU_DeviceType == fDeviceType;
keyar@chromium.orgfe6391a2012-08-20 15:03:41 +000097 }
keyar@chromium.org77a55222012-08-20 15:03:47 +000098
99 SkGLContext* getGLContext() {
100 if (this->isUsingGpuDevice()) {
101 return fGrContextFactory.getGLContext(GrContextFactory::kNative_GLContextType);
102 } else {
103 return NULL;
104 }
105 }
robertphillips@google.com163c84b2012-09-13 15:40:37 +0000106
107 GrContext* getGrContext() {
108 return fGrContext;
109 }
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000110#endif
111
keyar@chromium.org02dfb122012-08-20 15:03:36 +0000112 PictureRenderer()
keyar@chromium.org06125642012-08-20 15:03:33 +0000113 : fPicture(NULL)
114 , fDeviceType(kBitmap_DeviceType)
junov@chromium.org50ff9bd2012-11-02 19:16:22 +0000115 , fBBoxHierarchyType(kNone_BBoxHierarchyType)
keyar@chromium.org06125642012-08-20 15:03:33 +0000116#if SK_SUPPORT_GPU
117 , fGrContext(fGrContextFactory.get(GrContextFactory::kNative_GLContextType))
118#endif
119 {}
120
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000121protected:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000122 void buildBBoxHierarchy();
123 SkPicture* createPicture();
124 uint32_t recordFlags();
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000125 SkCanvas* setupCanvas();
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000126 virtual SkCanvas* setupCanvas(int width, int height);
keyar@chromium.orga474ce32012-08-20 15:03:57 +0000127
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000128 SkAutoTUnref<SkCanvas> fCanvas;
129 SkPicture* fPicture;
keyar@chromium.org4ea96c52012-08-20 15:03:29 +0000130 SkDeviceTypes fDeviceType;
junov@chromium.org9313ca42012-11-02 18:11:49 +0000131 BBoxHierarchyType fBBoxHierarchyType;
132
keyar@chromium.org06125642012-08-20 15:03:33 +0000133
134#if SK_SUPPORT_GPU
135 GrContextFactory fGrContextFactory;
136 GrContext* fGrContext;
137#endif
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000138
139private:
140 typedef SkRefCnt INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000141};
142
scroggo@google.com9a412522012-09-07 15:21:18 +0000143/**
144 * This class does not do any rendering, but its render function executes recording, which we want
145 * to time.
146 */
147class RecordPictureRenderer : public PictureRenderer {
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000148 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000149
150 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
151
152 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
153};
154
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000155class PipePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000156public:
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000157 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000158
159private:
160 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000161};
162
163class SimplePictureRenderer : public PictureRenderer {
keyar@chromium.org163b5672012-08-01 17:53:29 +0000164public:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000165 virtual void init(SkPicture* pict) SK_OVERRIDE;
166
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000167 virtual bool render(const SkString*) SK_OVERRIDE;
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000168
169private:
170 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000171};
172
173class TiledPictureRenderer : public PictureRenderer {
174public:
175 TiledPictureRenderer();
176
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000177 virtual void init(SkPicture* pict) SK_OVERRIDE;
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000178
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000179 /**
180 * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is
181 * created for each tile, named "path0.png", "path1.png", etc.
182 * Multithreaded mode currently does not support writing to a file.
183 */
184 virtual bool render(const SkString* path) SK_OVERRIDE;
185
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000186 virtual void end() SK_OVERRIDE;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000187
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000188 void setTileWidth(int width) {
189 fTileWidth = width;
190 }
191
192 int getTileWidth() const {
193 return fTileWidth;
194 }
195
196 void setTileHeight(int height) {
197 fTileHeight = height;
198 }
199
200 int getTileHeight() const {
201 return fTileHeight;
202 }
203
204 void setTileWidthPercentage(double percentage) {
205 fTileWidthPercentage = percentage;
206 }
207
keyar@chromium.org163b5672012-08-01 17:53:29 +0000208 double getTileWidthPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000209 return fTileWidthPercentage;
210 }
211
212 void setTileHeightPercentage(double percentage) {
213 fTileHeightPercentage = percentage;
214 }
215
keyar@chromium.org163b5672012-08-01 17:53:29 +0000216 double getTileHeightPercentage() const {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000217 return fTileHeightPercentage;
218 }
219
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000220 void setTileMinPowerOf2Width(int width) {
221 SkASSERT(SkIsPow2(width) && width > 0);
222 if (!SkIsPow2(width) || width <= 0) {
223 return;
224 }
225
226 fTileMinPowerOf2Width = width;
227 }
228
229 int getTileMinPowerOf2Width() const {
230 return fTileMinPowerOf2Width;
231 }
232
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000233protected:
234 virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE;
235 SkTDArray<SkRect> fTileRects;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000236
237private:
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000238 int fTileWidth;
239 int fTileHeight;
240 double fTileWidthPercentage;
241 double fTileHeightPercentage;
242 int fTileMinPowerOf2Width;
scroggo@google.combcdf2ec2012-09-20 14:42:33 +0000243
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000244 void setupTiles();
keyar@chromium.orgf4959ab2012-08-23 20:53:25 +0000245 void setupPowerOf2Tiles();
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000246
247 typedef PictureRenderer INHERITED;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000248};
249
scroggo@google.coma62da2f2012-11-02 21:28:12 +0000250class CloneData;
251
252class MultiCorePictureRenderer : public TiledPictureRenderer {
253public:
254 explicit MultiCorePictureRenderer(int threadCount);
255
256 ~MultiCorePictureRenderer();
257
258 virtual void init(SkPicture* pict) SK_OVERRIDE;
259
260 /**
261 * Behaves like TiledPictureRenderer::render(), only using multiple threads.
262 */
263 virtual bool render(const SkString* path) SK_OVERRIDE;
264
265 virtual void end() SK_OVERRIDE;
266
267private:
268 const int fNumThreads;
269 SkTDArray<SkCanvas*> fCanvasPool;
270 SkThreadPool fThreadPool;
271 SkPicture* fPictureClones;
272 CloneData** fCloneData;
273 SkCountdown fCountdown;
274
275 typedef TiledPictureRenderer INHERITED;
276};
277
scroggo@google.com9a412522012-09-07 15:21:18 +0000278/**
279 * This class does not do any rendering, but its render function executes turning an SkPictureRecord
280 * into an SkPicturePlayback, which we want to time.
281 */
282class PlaybackCreationRenderer : public PictureRenderer {
283public:
284 virtual void setup() SK_OVERRIDE;
285
scroggo@google.com81f9d2e2012-09-20 14:54:21 +0000286 virtual bool render(const SkString*) SK_OVERRIDE;
scroggo@google.com9a412522012-09-07 15:21:18 +0000287
288 virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); }
289
290 virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); }
291
292private:
junov@chromium.org9313ca42012-11-02 18:11:49 +0000293 SkAutoTUnref<SkPicture> fReplayer;
scroggo@google.com9a412522012-09-07 15:21:18 +0000294 typedef PictureRenderer INHERITED;
295};
296
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000297}
298
299#endif // PictureRenderer_DEFINED