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