| caryclark@google.com | 411bb72 | 2012-11-06 21:29:16 +0000 | [diff] [blame] | 1 | /* |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 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.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkCountdown.h" |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 12 | #include "SkDrawFilter.h" |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 13 | #include "SkMath.h" |
| reed@google.com | ea6a306 | 2012-11-06 22:14:54 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 15 | #include "SkPicture.h" |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 16 | #include "SkRect.h" |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 17 | #include "SkRefCnt.h" |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 18 | #include "SkRunnable.h" |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 19 | #include "SkString.h" |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 20 | #include "SkTDArray.h" |
| 21 | #include "SkThreadPool.h" |
| 22 | #include "SkTypes.h" |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 23 | |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 24 | #if SK_SUPPORT_GPU |
| 25 | #include "GrContextFactory.h" |
| 26 | #include "GrContext.h" |
| 27 | #endif |
| 28 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 29 | class SkBitmap; |
| 30 | class SkCanvas; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 31 | class SkGLContext; |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 32 | class SkThread; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 33 | |
| 34 | namespace sk_tools { |
| 35 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 36 | class TiledPictureRenderer; |
| 37 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 38 | class PictureRenderer : public SkRefCnt { |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 39 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 40 | public: |
| keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 41 | enum SkDeviceTypes { |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 42 | #if SK_ANGLE |
| 43 | kAngle_DeviceType, |
| 44 | #endif |
| keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 45 | kBitmap_DeviceType, |
| 46 | #if SK_SUPPORT_GPU |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 47 | kGPU_DeviceType, |
| keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 48 | #endif |
| 49 | }; |
| 50 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 51 | enum BBoxHierarchyType { |
| 52 | kNone_BBoxHierarchyType = 0, |
| 53 | kRTree_BBoxHierarchyType, |
| junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 54 | kTileGrid_BBoxHierarchyType, |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 57 | // this uses SkPaint::Flags as a base and adds additional flags |
| 58 | enum DrawFilterFlags { |
| 59 | kNone_DrawFilterFlag = 0, |
| caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 60 | kBlur_DrawFilterFlag = 0x4000, // toggles between blur and no blur |
| reed@google.com | 457d8a7 | 2012-12-18 18:20:44 +0000 | [diff] [blame] | 61 | kHinting_DrawFilterFlag = 0x8000, // toggles between no hinting and normal hinting |
| 62 | kSlightHinting_DrawFilterFlag = 0x10000, // toggles between slight and normal hinting |
| 63 | kAAClip_DrawFilterFlag = 0x20000, // toggles between soft and hard clip |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | SK_COMPILE_ASSERT(!(kBlur_DrawFilterFlag & SkPaint::kAllFlags), blur_flag_must_be_greater); |
| 67 | SK_COMPILE_ASSERT(!(kHinting_DrawFilterFlag & SkPaint::kAllFlags), |
| 68 | hinting_flag_must_be_greater); |
| 69 | SK_COMPILE_ASSERT(!(kSlightHinting_DrawFilterFlag & SkPaint::kAllFlags), |
| 70 | slight_hinting_flag_must_be_greater); |
| 71 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Called with each new SkPicture to render. |
| 74 | */ |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 75 | virtual void init(SkPicture* pict); |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 76 | |
| 77 | /** |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 78 | * Set the viewport so that only the portion listed gets drawn. |
| 79 | */ |
| 80 | void setViewport(SkISize size) { fViewport = size; } |
| 81 | |
| 82 | /** |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 83 | * Set the scale factor at which draw the picture. |
| 84 | */ |
| 85 | void setScaleFactor(SkScalar scale) { fScaleFactor = scale; } |
| 86 | |
| 87 | /** |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 88 | * Perform any setup that should done prior to each iteration of render() which should not be |
| 89 | * timed. |
| 90 | */ |
| 91 | virtual void setup() {} |
| 92 | |
| 93 | /** |
| 94 | * Perform work that is to be timed. Typically this is rendering, but is also used for recording |
| 95 | * and preparing picture for playback by the subclasses which do those. |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 96 | * If path is non-null, subclass implementations should call write(). |
| 97 | * @param path If non-null, also write the output to the file specified by path. path should |
| 98 | * have no extension; it will be added by write(). |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 99 | * @return bool True if rendering succeeded and, if path is non-null, the output was |
| 100 | * successfully written to a file. |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 101 | */ |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 102 | virtual bool render(const SkString* path, SkBitmap** out = NULL) = 0; |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 103 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Called once finished with a particular SkPicture, before calling init again, and before |
| 106 | * being done with this Renderer. |
| 107 | */ |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 108 | virtual void end(); |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 109 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 110 | /** |
| 111 | * If this PictureRenderer is actually a TiledPictureRender, return a pointer to this as a |
| 112 | * TiledPictureRender so its methods can be called. |
| 113 | */ |
| 114 | virtual TiledPictureRenderer* getTiledRenderer() { return NULL; } |
| 115 | |
| scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 116 | /** |
| 117 | * Resets the GPU's state. Does nothing if the backing is raster. For a GPU renderer, calls |
| 118 | * flush, and calls finish if callFinish is true. |
| 119 | * @param callFinish Whether to call finish. |
| 120 | */ |
| 121 | void resetState(bool callFinish); |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 122 | |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 123 | /** |
| 124 | * Set the backend type. Returns true on success and false on failure. |
| 125 | */ |
| 126 | bool setDeviceType(SkDeviceTypes deviceType) { |
| keyar@chromium.org | c81686c | 2012-08-20 15:04:04 +0000 | [diff] [blame] | 127 | fDeviceType = deviceType; |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 128 | #if SK_SUPPORT_GPU |
| 129 | // In case this function is called more than once |
| 130 | SkSafeUnref(fGrContext); |
| 131 | fGrContext = NULL; |
| 132 | // Set to Native so it will have an initial value. |
| 133 | GrContextFactory::GLContextType glContextType = GrContextFactory::kNative_GLContextType; |
| 134 | #endif |
| 135 | switch(deviceType) { |
| 136 | case kBitmap_DeviceType: |
| 137 | return true; |
| 138 | #if SK_SUPPORT_GPU |
| 139 | case kGPU_DeviceType: |
| 140 | // Already set to GrContextFactory::kNative_GLContextType, above. |
| 141 | break; |
| 142 | #if SK_ANGLE |
| 143 | case kAngle_DeviceType: |
| 144 | glContextType = GrContextFactory::kANGLE_GLContextType; |
| 145 | break; |
| 146 | #endif |
| 147 | #endif |
| 148 | default: |
| 149 | // Invalid device type. |
| 150 | return false; |
| 151 | } |
| 152 | #if SK_SUPPORT_GPU |
| 153 | fGrContext = fGrContextFactory.get(glContextType); |
| 154 | if (NULL == fGrContext) { |
| 155 | return false; |
| 156 | } else { |
| 157 | fGrContext->ref(); |
| 158 | return true; |
| 159 | } |
| 160 | #endif |
| keyar@chromium.org | fe6391a | 2012-08-20 15:03:41 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 163 | void setDrawFilters(DrawFilterFlags const * const filters, const SkString& configName) { |
| 164 | memcpy(fDrawFilters, filters, sizeof(fDrawFilters)); |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 165 | fDrawFiltersConfig = configName; |
| 166 | } |
| 167 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 168 | void setBBoxHierarchyType(BBoxHierarchyType bbhType) { |
| 169 | fBBoxHierarchyType = bbhType; |
| 170 | } |
| 171 | |
| junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 172 | void setGridSize(int width, int height) { |
| 173 | fGridWidth = width; |
| 174 | fGridHeight = height; |
| 175 | } |
| 176 | |
| keyar@chromium.org | fe6391a | 2012-08-20 15:03:41 +0000 | [diff] [blame] | 177 | bool isUsingBitmapDevice() { |
| keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 178 | return kBitmap_DeviceType == fDeviceType; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 181 | virtual SkString getPerIterTimeFormat() { return SkString("%.2f"); } |
| 182 | |
| 183 | virtual SkString getNormalTimeFormat() { return SkString("%6.2f"); } |
| 184 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Reports the configuration of this PictureRenderer. |
| 187 | */ |
| 188 | SkString getConfigName() { |
| 189 | SkString config = this->getConfigNameInternal(); |
| scroggo@google.com | c4013c1 | 2012-12-13 22:07:08 +0000 | [diff] [blame] | 190 | if (!fViewport.isEmpty()) { |
| 191 | config.appendf("_viewport_%ix%i", fViewport.width(), fViewport.height()); |
| 192 | } |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 193 | if (kRTree_BBoxHierarchyType == fBBoxHierarchyType) { |
| 194 | config.append("_rtree"); |
| junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 195 | } else if (kTileGrid_BBoxHierarchyType == fBBoxHierarchyType) { |
| 196 | config.append("_grid"); |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 197 | } |
| 198 | #if SK_SUPPORT_GPU |
| robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame^] | 199 | switch (fDeviceType) { |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 200 | case kGPU_DeviceType: |
| 201 | config.append("_gpu"); |
| 202 | break; |
| 203 | #if SK_ANGLE |
| 204 | case kAngle_DeviceType: |
| 205 | config.append("_angle"); |
| 206 | break; |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 207 | #endif |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 208 | default: |
| 209 | // Assume that no extra info means bitmap. |
| 210 | break; |
| 211 | } |
| robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame^] | 212 | #endif |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 213 | config.append(fDrawFiltersConfig.c_str()); |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 214 | return config; |
| 215 | } |
| 216 | |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 217 | #if SK_SUPPORT_GPU |
| keyar@chromium.org | fe6391a | 2012-08-20 15:03:41 +0000 | [diff] [blame] | 218 | bool isUsingGpuDevice() { |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 219 | switch (fDeviceType) { |
| 220 | case kGPU_DeviceType: |
| 221 | // fall through |
| 222 | #if SK_ANGLE |
| 223 | case kAngle_DeviceType: |
| 224 | #endif |
| 225 | return true; |
| 226 | default: |
| 227 | return false; |
| 228 | } |
| keyar@chromium.org | fe6391a | 2012-08-20 15:03:41 +0000 | [diff] [blame] | 229 | } |
| keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 230 | |
| 231 | SkGLContext* getGLContext() { |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 232 | GrContextFactory::GLContextType glContextType |
| 233 | = GrContextFactory::kNull_GLContextType; |
| 234 | switch(fDeviceType) { |
| 235 | case kGPU_DeviceType: |
| 236 | glContextType = GrContextFactory::kNative_GLContextType; |
| 237 | break; |
| 238 | #if SK_ANGLE |
| 239 | case kAngle_DeviceType: |
| 240 | glContextType = GrContextFactory::kANGLE_GLContextType; |
| 241 | break; |
| 242 | #endif |
| 243 | default: |
| 244 | return NULL; |
| keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 245 | } |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 246 | return fGrContextFactory.getGLContext(glContextType); |
| keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 247 | } |
| robertphillips@google.com | 163c84b | 2012-09-13 15:40:37 +0000 | [diff] [blame] | 248 | |
| 249 | GrContext* getGrContext() { |
| 250 | return fGrContext; |
| 251 | } |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 252 | #endif |
| 253 | |
| keyar@chromium.org | 02dfb12 | 2012-08-20 15:03:36 +0000 | [diff] [blame] | 254 | PictureRenderer() |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 255 | : fPicture(NULL) |
| 256 | , fDeviceType(kBitmap_DeviceType) |
| junov@chromium.org | 50ff9bd | 2012-11-02 19:16:22 +0000 | [diff] [blame] | 257 | , fBBoxHierarchyType(kNone_BBoxHierarchyType) |
| junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 258 | , fGridWidth(0) |
| 259 | , fGridHeight(0) |
| scroggo@google.com | 06d6ac6 | 2013-02-08 21:16:19 +0000 | [diff] [blame] | 260 | , fScaleFactor(SK_Scalar1) |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 261 | #if SK_SUPPORT_GPU |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 262 | , fGrContext(NULL) |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 263 | #endif |
| caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 264 | { |
| 265 | sk_bzero(fDrawFilters, sizeof(fDrawFilters)); |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 266 | fViewport.set(0, 0); |
| caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 267 | } |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 268 | |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 269 | #if SK_SUPPORT_GPU |
| 270 | virtual ~PictureRenderer() { |
| 271 | SkSafeUnref(fGrContext); |
| 272 | } |
| 273 | #endif |
| 274 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 275 | protected: |
| 276 | SkAutoTUnref<SkCanvas> fCanvas; |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 277 | SkPicture* fPicture; |
| 278 | SkDeviceTypes fDeviceType; |
| 279 | BBoxHierarchyType fBBoxHierarchyType; |
| 280 | DrawFilterFlags fDrawFilters[SkDrawFilter::kTypeCount]; |
| 281 | SkString fDrawFiltersConfig; |
| 282 | int fGridWidth, fGridHeight; // used when fBBoxHierarchyType is TileGrid |
| keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 283 | |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 284 | void buildBBoxHierarchy(); |
| 285 | |
| 286 | /** |
| 287 | * Return the total width that should be drawn. If the viewport width has been set greater than |
| 288 | * 0, this will be the minimum of the current SkPicture's width and the viewport's width. |
| 289 | */ |
| 290 | int getViewWidth(); |
| 291 | |
| 292 | /** |
| 293 | * Return the total height that should be drawn. If the viewport height has been set greater |
| 294 | * than 0, this will be the minimum of the current SkPicture's height and the viewport's height. |
| 295 | */ |
| 296 | int getViewHeight(); |
| 297 | |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 298 | /** |
| 299 | * Scales the provided canvas to the scale factor set by setScaleFactor. |
| 300 | */ |
| 301 | void scaleToScaleFactor(SkCanvas*); |
| 302 | |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 303 | SkPicture* createPicture(); |
| 304 | uint32_t recordFlags(); |
| 305 | SkCanvas* setupCanvas(); |
| 306 | virtual SkCanvas* setupCanvas(int width, int height); |
| 307 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 308 | private: |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 309 | SkISize fViewport; |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 310 | SkScalar fScaleFactor; |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 311 | #if SK_SUPPORT_GPU |
| 312 | GrContextFactory fGrContextFactory; |
| 313 | GrContext* fGrContext; |
| 314 | #endif |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 315 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 316 | virtual SkString getConfigNameInternal() = 0; |
| 317 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 318 | typedef SkRefCnt INHERITED; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 321 | /** |
| 322 | * This class does not do any rendering, but its render function executes recording, which we want |
| 323 | * to time. |
| 324 | */ |
| 325 | class RecordPictureRenderer : public PictureRenderer { |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 326 | virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE; |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 327 | |
| 328 | virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); } |
| 329 | |
| 330 | virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); } |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 331 | |
| djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 332 | protected: |
| 333 | virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE; |
| 334 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 335 | private: |
| 336 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 339 | class PipePictureRenderer : public PictureRenderer { |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 340 | public: |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 341 | virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE; |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 342 | |
| 343 | private: |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 344 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| 345 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 346 | typedef PictureRenderer INHERITED; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | class SimplePictureRenderer : public PictureRenderer { |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 350 | public: |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 351 | virtual void init(SkPicture* pict) SK_OVERRIDE; |
| 352 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 353 | virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE; |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 354 | |
| 355 | private: |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 356 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| 357 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 358 | typedef PictureRenderer INHERITED; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | class TiledPictureRenderer : public PictureRenderer { |
| 362 | public: |
| 363 | TiledPictureRenderer(); |
| 364 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 365 | virtual void init(SkPicture* pict) SK_OVERRIDE; |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 366 | |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 367 | /** |
| 368 | * Renders to tiles, rather than a single canvas. If a path is provided, a separate file is |
| 369 | * created for each tile, named "path0.png", "path1.png", etc. |
| 370 | * Multithreaded mode currently does not support writing to a file. |
| 371 | */ |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 372 | virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE; |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 373 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 374 | virtual void end() SK_OVERRIDE; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 375 | |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 376 | void setTileWidth(int width) { |
| 377 | fTileWidth = width; |
| 378 | } |
| 379 | |
| 380 | int getTileWidth() const { |
| 381 | return fTileWidth; |
| 382 | } |
| 383 | |
| 384 | void setTileHeight(int height) { |
| 385 | fTileHeight = height; |
| 386 | } |
| 387 | |
| 388 | int getTileHeight() const { |
| 389 | return fTileHeight; |
| 390 | } |
| 391 | |
| 392 | void setTileWidthPercentage(double percentage) { |
| 393 | fTileWidthPercentage = percentage; |
| 394 | } |
| 395 | |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 396 | double getTileWidthPercentage() const { |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 397 | return fTileWidthPercentage; |
| 398 | } |
| 399 | |
| 400 | void setTileHeightPercentage(double percentage) { |
| 401 | fTileHeightPercentage = percentage; |
| 402 | } |
| 403 | |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 404 | double getTileHeightPercentage() const { |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 405 | return fTileHeightPercentage; |
| 406 | } |
| 407 | |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 408 | void setTileMinPowerOf2Width(int width) { |
| 409 | SkASSERT(SkIsPow2(width) && width > 0); |
| 410 | if (!SkIsPow2(width) || width <= 0) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | fTileMinPowerOf2Width = width; |
| 415 | } |
| 416 | |
| 417 | int getTileMinPowerOf2Width() const { |
| 418 | return fTileMinPowerOf2Width; |
| 419 | } |
| 420 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 421 | virtual TiledPictureRenderer* getTiledRenderer() SK_OVERRIDE { return this; } |
| 422 | |
| 423 | /** |
| 424 | * Report the number of tiles in the x and y directions. Must not be called before init. |
| 425 | * @param x Output parameter identifying the number of tiles in the x direction. |
| 426 | * @param y Output parameter identifying the number of tiles in the y direction. |
| 427 | * @return True if the tiles have been set up, and x and y are meaningful. If false, x and y are |
| 428 | * unmodified. |
| 429 | */ |
| 430 | bool tileDimensions(int& x, int&y); |
| 431 | |
| 432 | /** |
| 433 | * Move to the next tile and return its indices. Must be called before calling drawCurrentTile |
| 434 | * for the first time. |
| 435 | * @param i Output parameter identifying the column of the next tile to be drawn on the next |
| 436 | * call to drawNextTile. |
| 437 | * @param j Output parameter identifying the row of the next tile to be drawn on the next call |
| 438 | * to drawNextTile. |
| 439 | * @param True if the tiles have been created and the next tile to be drawn by drawCurrentTile |
| 440 | * is within the range of tiles. If false, i and j are unmodified. |
| 441 | */ |
| 442 | bool nextTile(int& i, int& j); |
| 443 | |
| 444 | /** |
| 445 | * Render one tile. This will draw the same tile each time it is called until nextTile is |
| 446 | * called. The tile rendered will depend on how many calls have been made to nextTile. |
| 447 | * It is an error to call this without first calling nextTile, or if nextTile returns false. |
| 448 | */ |
| 449 | void drawCurrentTile(); |
| 450 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 451 | protected: |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 452 | SkTDArray<SkRect> fTileRects; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 453 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 454 | virtual SkCanvas* setupCanvas(int width, int height) SK_OVERRIDE; |
| 455 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| 456 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 457 | private: |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 458 | int fTileWidth; |
| 459 | int fTileHeight; |
| 460 | double fTileWidthPercentage; |
| 461 | double fTileHeightPercentage; |
| 462 | int fTileMinPowerOf2Width; |
| 463 | |
| 464 | // These variables are only used for timing individual tiles. |
| 465 | // Next tile to draw in fTileRects. |
| 466 | int fCurrentTileOffset; |
| 467 | // Number of tiles in the x direction. |
| 468 | int fTilesX; |
| 469 | // Number of tiles in the y direction. |
| 470 | int fTilesY; |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 471 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 472 | void setupTiles(); |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 473 | void setupPowerOf2Tiles(); |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 474 | |
| 475 | typedef PictureRenderer INHERITED; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 478 | class CloneData; |
| 479 | |
| 480 | class MultiCorePictureRenderer : public TiledPictureRenderer { |
| 481 | public: |
| 482 | explicit MultiCorePictureRenderer(int threadCount); |
| 483 | |
| 484 | ~MultiCorePictureRenderer(); |
| 485 | |
| 486 | virtual void init(SkPicture* pict) SK_OVERRIDE; |
| 487 | |
| 488 | /** |
| 489 | * Behaves like TiledPictureRenderer::render(), only using multiple threads. |
| 490 | */ |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 491 | virtual bool render(const SkString* path, SkBitmap** out = NULL) SK_OVERRIDE; |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 492 | |
| 493 | virtual void end() SK_OVERRIDE; |
| 494 | |
| 495 | private: |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 496 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| 497 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 498 | const int fNumThreads; |
| 499 | SkTDArray<SkCanvas*> fCanvasPool; |
| 500 | SkThreadPool fThreadPool; |
| 501 | SkPicture* fPictureClones; |
| 502 | CloneData** fCloneData; |
| 503 | SkCountdown fCountdown; |
| 504 | |
| 505 | typedef TiledPictureRenderer INHERITED; |
| 506 | }; |
| 507 | |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 508 | /** |
| 509 | * This class does not do any rendering, but its render function executes turning an SkPictureRecord |
| 510 | * into an SkPicturePlayback, which we want to time. |
| 511 | */ |
| 512 | class PlaybackCreationRenderer : public PictureRenderer { |
| 513 | public: |
| 514 | virtual void setup() SK_OVERRIDE; |
| 515 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 516 | virtual bool render(const SkString*, SkBitmap** out = NULL) SK_OVERRIDE; |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 517 | |
| 518 | virtual SkString getPerIterTimeFormat() SK_OVERRIDE { return SkString("%.4f"); } |
| 519 | |
| 520 | virtual SkString getNormalTimeFormat() SK_OVERRIDE { return SkString("%6.4f"); } |
| 521 | |
| 522 | private: |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 523 | SkAutoTUnref<SkPicture> fReplayer; |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 524 | |
| 525 | virtual SkString getConfigNameInternal() SK_OVERRIDE; |
| 526 | |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 527 | typedef PictureRenderer INHERITED; |
| 528 | }; |
| 529 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 530 | extern PictureRenderer* CreateGatherPixelRefsRenderer(); |
| reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 531 | extern PictureRenderer* CreatePictureCloneRenderer(); |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 532 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | #endif // PictureRenderer_DEFINED |