keyar@chromium.org | b3fb7c1 | 2012-08-20 21:02:49 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 8 | #include "PictureRenderer.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 9 | #include "picture_utils.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 10 | #include "SamplePipeControllers.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkDevice.h" |
| 13 | #include "SkGPipe.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
| 15 | #include "SkGpuDevice.h" |
| 16 | #endif |
| 17 | #include "SkGraphics.h" |
| 18 | #include "SkImageEncoder.h" |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 19 | #include "SkMaskFilter.h" |
keyar@chromium.org | ea82695 | 2012-08-23 15:24:13 +0000 | [diff] [blame] | 20 | #include "SkMatrix.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 21 | #include "SkPicture.h" |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 22 | #include "SkRTree.h" |
keyar@chromium.org | ea82695 | 2012-08-23 15:24:13 +0000 | [diff] [blame] | 23 | #include "SkScalar.h" |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 24 | #include "SkStream.h" |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 25 | #include "SkString.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 26 | #include "SkTemplates.h" |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 27 | #include "SkTileGridPicture.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 28 | #include "SkTDArray.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 29 | #include "SkThreadUtils.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 30 | #include "SkTypes.h" |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 31 | #include "SkData.h" |
| 32 | #include "SkPictureUtils.h" |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 33 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 34 | namespace sk_tools { |
| 35 | |
| 36 | enum { |
| 37 | kDefaultTileWidth = 256, |
| 38 | kDefaultTileHeight = 256 |
| 39 | }; |
| 40 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 41 | void PictureRenderer::init(SkPicture* pict) { |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 42 | SkASSERT(NULL == fPicture); |
| 43 | SkASSERT(NULL == fCanvas.get()); |
| 44 | if (fPicture != NULL || NULL != fCanvas.get()) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
| 48 | SkASSERT(pict != NULL); |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 49 | if (NULL == pict) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | fPicture = pict; |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 54 | fPicture->ref(); |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 55 | fCanvas.reset(this->setupCanvas()); |
| 56 | } |
| 57 | |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 58 | class FlagsDrawFilter : public SkDrawFilter { |
| 59 | public: |
| 60 | FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) : |
| 61 | fFlags(flags) {} |
| 62 | |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 63 | virtual bool filter(SkPaint* paint, Type t) { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 64 | paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags); |
caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 65 | if ((PictureRenderer::kBlur_DrawFilterFlag | PictureRenderer::kLowBlur_DrawFilterFlag) |
| 66 | & fFlags[t]) { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 67 | SkMaskFilter* maskFilter = paint->getMaskFilter(); |
| 68 | SkMaskFilter::BlurInfo blurInfo; |
| 69 | if (maskFilter && maskFilter->asABlur(&blurInfo)) { |
caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 70 | if (PictureRenderer::kBlur_DrawFilterFlag & fFlags[t]) { |
| 71 | paint->setMaskFilter(NULL); |
| 72 | } else { |
| 73 | blurInfo.fHighQuality = false; |
| 74 | maskFilter->setAsABlur(blurInfo); |
| 75 | } |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) { |
| 79 | paint->setHinting(SkPaint::kNo_Hinting); |
| 80 | } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) { |
| 81 | paint->setHinting(SkPaint::kSlight_Hinting); |
| 82 | } |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 83 | return true; |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | PictureRenderer::DrawFilterFlags* fFlags; |
| 88 | }; |
| 89 | |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 90 | static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 91 | if (drawFilters && !canvas->getDrawFilter()) { |
| 92 | canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref(); |
caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 93 | if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) { |
| 94 | canvas->setAllowSoftClip(false); |
| 95 | } |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 96 | } |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 99 | SkCanvas* PictureRenderer::setupCanvas() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 100 | const int width = this->getViewWidth(); |
| 101 | const int height = this->getViewHeight(); |
| 102 | return this->setupCanvas(width, height); |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | SkCanvas* PictureRenderer::setupCanvas(int width, int height) { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 106 | SkCanvas* canvas; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 107 | switch(fDeviceType) { |
| 108 | case kBitmap_DeviceType: { |
| 109 | SkBitmap bitmap; |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 110 | sk_tools::setup_bitmap(&bitmap, width, height); |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 111 | canvas = SkNEW_ARGS(SkCanvas, (bitmap)); |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 112 | } |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 113 | break; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 114 | #if SK_SUPPORT_GPU |
| 115 | case kGPU_DeviceType: { |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 116 | SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, |
keyar@chromium.org | 0612564 | 2012-08-20 15:03:33 +0000 | [diff] [blame] | 117 | (fGrContext, SkBitmap::kARGB_8888_Config, |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 118 | width, height))); |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 119 | canvas = SkNEW_ARGS(SkCanvas, (device.get())); |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 120 | } |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 121 | break; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 122 | #endif |
| 123 | default: |
| 124 | SkASSERT(0); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 125 | return NULL; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 126 | } |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 127 | setUpFilter(canvas, fDrawFilters); |
| 128 | this->scaleToScaleFactor(canvas); |
| 129 | return canvas; |
| 130 | } |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 131 | |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 132 | void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { |
| 133 | SkASSERT(canvas != NULL); |
| 134 | if (fScaleFactor != SK_Scalar1) { |
| 135 | canvas->scale(fScaleFactor, fScaleFactor); |
| 136 | } |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void PictureRenderer::end() { |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 140 | this->resetState(); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 141 | SkSafeUnref(fPicture); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 142 | fPicture = NULL; |
| 143 | fCanvas.reset(NULL); |
| 144 | } |
| 145 | |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 146 | int PictureRenderer::getViewWidth() { |
| 147 | SkASSERT(fPicture != NULL); |
| 148 | int width = fPicture->width(); |
| 149 | if (fViewport.width() > 0) { |
| 150 | width = SkMin32(width, fViewport.width()); |
| 151 | } |
| 152 | return width; |
| 153 | } |
| 154 | |
| 155 | int PictureRenderer::getViewHeight() { |
| 156 | SkASSERT(fPicture != NULL); |
| 157 | int height = fPicture->height(); |
| 158 | if (fViewport.height() > 0) { |
| 159 | height = SkMin32(height, fViewport.height()); |
| 160 | } |
| 161 | return height; |
| 162 | } |
| 163 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 164 | /** Converts fPicture to a picture that uses a BBoxHierarchy. |
| 165 | * PictureRenderer subclasses that are used to test picture playback |
| 166 | * should call this method during init. |
| 167 | */ |
| 168 | void PictureRenderer::buildBBoxHierarchy() { |
| 169 | SkASSERT(NULL != fPicture); |
| 170 | if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { |
| 171 | SkPicture* newPicture = this->createPicture(); |
| 172 | SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPicture->height(), |
| 173 | this->recordFlags()); |
| 174 | fPicture->draw(recorder); |
| 175 | newPicture->endRecording(); |
| 176 | fPicture->unref(); |
| 177 | fPicture = newPicture; |
| 178 | } |
| 179 | } |
| 180 | |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 181 | void PictureRenderer::resetState() { |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 182 | #if SK_SUPPORT_GPU |
| 183 | if (this->isUsingGpuDevice()) { |
| 184 | SkGLContext* glContext = fGrContextFactory.getGLContext( |
| 185 | GrContextFactory::kNative_GLContextType); |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 186 | |
| 187 | SkASSERT(glContext != NULL); |
| 188 | if (NULL == glContext) { |
| 189 | return; |
| 190 | } |
| 191 | |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 192 | fGrContext->flush(); |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 193 | SK_GL(*glContext, Finish()); |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 194 | } |
keyar@chromium.org | a40c20d | 2012-08-20 15:04:12 +0000 | [diff] [blame] | 195 | #endif |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 196 | } |
| 197 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 198 | uint32_t PictureRenderer::recordFlags() { |
| 199 | return kNone_BBoxHierarchyType == fBBoxHierarchyType ? 0 : |
| 200 | SkPicture::kOptimizeForClippedPlayback_RecordingFlag; |
| 201 | } |
| 202 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 203 | /** |
| 204 | * Write the canvas to the specified path. |
| 205 | * @param canvas Must be non-null. Canvas to be written to a file. |
| 206 | * @param path Path for the file to be written. Should have no extension; write() will append |
| 207 | * an appropriate one. Passed in by value so it can be modified. |
| 208 | * @return bool True if the Canvas is written to a file. |
| 209 | */ |
| 210 | static bool write(SkCanvas* canvas, SkString path) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 211 | SkASSERT(canvas != NULL); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 212 | if (NULL == canvas) { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | SkBitmap bitmap; |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 217 | SkISize size = canvas->getDeviceSize(); |
| 218 | sk_tools::setup_bitmap(&bitmap, size.width(), size.height()); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 219 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 220 | canvas->readPixels(&bitmap, 0, 0); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 221 | sk_tools::force_all_opaque(bitmap); |
| 222 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 223 | // Since path is passed in by value, it is okay to modify it. |
| 224 | path.append(".png"); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 225 | return SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 226 | } |
| 227 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 228 | /** |
| 229 | * If path is non NULL, append number to it, and call write(SkCanvas*, SkString) to write the |
| 230 | * provided canvas to a file. Returns true if path is NULL or if write() succeeds. |
| 231 | */ |
| 232 | static bool writeAppendNumber(SkCanvas* canvas, const SkString* path, int number) { |
| 233 | if (NULL == path) { |
| 234 | return true; |
| 235 | } |
| 236 | SkString pathWithNumber(*path); |
| 237 | pathWithNumber.appendf("%i", number); |
| 238 | return write(canvas, pathWithNumber); |
| 239 | } |
| 240 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 241 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 242 | |
djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 243 | SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) { |
| 244 | // defer the canvas setup until the render step |
| 245 | return NULL; |
| 246 | } |
| 247 | |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 248 | static bool PNGEncodeBitmapToStream(SkWStream* wStream, const SkBitmap& bm) { |
| 249 | return SkImageEncoder::EncodeStream(wStream, bm, SkImageEncoder::kPNG_Type, 100); |
| 250 | } |
| 251 | |
| 252 | bool RecordPictureRenderer::render(const SkString* path) { |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 253 | SkAutoTUnref<SkPicture> replayer(this->createPicture()); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 254 | SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->getViewHeight(), |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 255 | this->recordFlags()); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 256 | this->scaleToScaleFactor(recorder); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 257 | fPicture->draw(recorder); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 258 | replayer->endRecording(); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 259 | if (path != NULL) { |
| 260 | // Record the new picture as a new SKP with PNG encoded bitmaps. |
| 261 | SkString skpPath(*path); |
| 262 | // ".skp" was removed from 'path' before being passed in here. |
| 263 | skpPath.append(".skp"); |
| 264 | SkFILEWStream stream(skpPath.c_str()); |
| 265 | replayer->serialize(&stream, &PNGEncodeBitmapToStream); |
| 266 | return true; |
| 267 | } |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 268 | return false; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 269 | } |
| 270 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 271 | SkString RecordPictureRenderer::getConfigNameInternal() { |
| 272 | return SkString("record"); |
| 273 | } |
| 274 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 275 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 276 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 277 | bool PipePictureRenderer::render(const SkString* path) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 278 | SkASSERT(fCanvas.get() != NULL); |
| 279 | SkASSERT(fPicture != NULL); |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 280 | if (NULL == fCanvas.get() || NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 281 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | PipeController pipeController(fCanvas.get()); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 285 | SkGPipeWriter writer; |
| 286 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 287 | pipeCanvas->drawPicture(*fPicture); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 288 | writer.endRecording(); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 289 | fCanvas->flush(); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 290 | if (NULL != path) { |
| 291 | return write(fCanvas, *path); |
| 292 | } |
| 293 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 294 | } |
| 295 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 296 | SkString PipePictureRenderer::getConfigNameInternal() { |
| 297 | return SkString("pipe"); |
| 298 | } |
| 299 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 300 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 301 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 302 | void SimplePictureRenderer::init(SkPicture* picture) { |
| 303 | INHERITED::init(picture); |
| 304 | this->buildBBoxHierarchy(); |
| 305 | } |
| 306 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 307 | bool SimplePictureRenderer::render(const SkString* path) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 308 | SkASSERT(fCanvas.get() != NULL); |
| 309 | SkASSERT(fPicture != NULL); |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 310 | if (NULL == fCanvas.get() || NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 311 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | fCanvas->drawPicture(*fPicture); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 315 | fCanvas->flush(); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 316 | if (NULL != path) { |
| 317 | return write(fCanvas, *path); |
| 318 | } |
| 319 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 320 | } |
| 321 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 322 | SkString SimplePictureRenderer::getConfigNameInternal() { |
| 323 | return SkString("simple"); |
| 324 | } |
| 325 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 326 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 327 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 328 | TiledPictureRenderer::TiledPictureRenderer() |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 329 | : fTileWidth(kDefaultTileWidth) |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 330 | , fTileHeight(kDefaultTileHeight) |
rileya@google.com | a04dc02 | 2012-09-10 19:01:38 +0000 | [diff] [blame] | 331 | , fTileWidthPercentage(0.0) |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 332 | , fTileHeightPercentage(0.0) |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 333 | , fTileMinPowerOf2Width(0) |
| 334 | , fCurrentTileOffset(-1) |
| 335 | , fTilesX(0) |
| 336 | , fTilesY(0) { } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 337 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 338 | void TiledPictureRenderer::init(SkPicture* pict) { |
| 339 | SkASSERT(pict != NULL); |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 340 | SkASSERT(0 == fTileRects.count()); |
| 341 | if (NULL == pict || fTileRects.count() != 0) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 345 | // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not |
| 346 | // used by bench_pictures. |
| 347 | fPicture = pict; |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 348 | fPicture->ref(); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 349 | this->buildBBoxHierarchy(); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 350 | |
| 351 | if (fTileWidthPercentage > 0) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 352 | fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 353 | } |
| 354 | if (fTileHeightPercentage > 0) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 355 | fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 356 | } |
| 357 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 358 | if (fTileMinPowerOf2Width > 0) { |
| 359 | this->setupPowerOf2Tiles(); |
| 360 | } else { |
| 361 | this->setupTiles(); |
| 362 | } |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 363 | fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight)); |
| 364 | // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the |
| 365 | // first call to drawCurrentTile. |
| 366 | fCurrentTileOffset = -1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 367 | } |
| 368 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 369 | void TiledPictureRenderer::end() { |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 370 | fTileRects.reset(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 371 | this->INHERITED::end(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 372 | } |
| 373 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 374 | void TiledPictureRenderer::setupTiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 375 | // Only use enough tiles to cover the viewport |
| 376 | const int width = this->getViewWidth(); |
| 377 | const int height = this->getViewHeight(); |
| 378 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 379 | fTilesX = fTilesY = 0; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 380 | for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 381 | fTilesY++; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 382 | for (int tile_x_start = 0; tile_x_start < width; tile_x_start += fTileWidth) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 383 | if (0 == tile_y_start) { |
| 384 | // Only count tiles in the X direction on the first pass. |
| 385 | fTilesX++; |
| 386 | } |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 387 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 388 | SkIntToScalar(tile_y_start), |
| 389 | SkIntToScalar(fTileWidth), |
| 390 | SkIntToScalar(fTileHeight)); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 395 | bool TiledPictureRenderer::tileDimensions(int &x, int &y) { |
| 396 | if (fTileRects.count() == 0 || NULL == fPicture) { |
| 397 | return false; |
| 398 | } |
| 399 | x = fTilesX; |
| 400 | y = fTilesY; |
| 401 | return true; |
| 402 | } |
| 403 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 404 | // The goal of the powers of two tiles is to minimize the amount of wasted tile |
| 405 | // space in the width-wise direction and then minimize the number of tiles. The |
| 406 | // constraints are that every tile must have a pixel width that is a power of |
| 407 | // two and also be of some minimal width (that is also a power of two). |
| 408 | // |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 409 | // This is solved by first taking our picture size and rounding it up to the |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 410 | // multiple of the minimal width. The binary representation of this rounded |
| 411 | // value gives us the tiles we need: a bit of value one means we need a tile of |
| 412 | // that size. |
| 413 | void TiledPictureRenderer::setupPowerOf2Tiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 414 | // Only use enough tiles to cover the viewport |
| 415 | const int width = this->getViewWidth(); |
| 416 | const int height = this->getViewHeight(); |
| 417 | |
| 418 | int rounded_value = width; |
| 419 | if (width % fTileMinPowerOf2Width != 0) { |
| 420 | rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 421 | } |
| 422 | |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 423 | int num_bits = SkScalarCeilToInt(SkScalarLog2(SkIntToScalar(width))); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 424 | int largest_possible_tile_size = 1 << num_bits; |
| 425 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 426 | fTilesX = fTilesY = 0; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 427 | // The tile height is constant for a particular picture. |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 428 | for (int tile_y_start = 0; tile_y_start < height; tile_y_start += fTileHeight) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 429 | fTilesY++; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 430 | int tile_x_start = 0; |
| 431 | int current_width = largest_possible_tile_size; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 432 | // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough |
| 433 | // to draw each tile. |
| 434 | fTileWidth = current_width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 435 | |
| 436 | while (current_width >= fTileMinPowerOf2Width) { |
| 437 | // It is very important this is a bitwise AND. |
| 438 | if (current_width & rounded_value) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 439 | if (0 == tile_y_start) { |
| 440 | // Only count tiles in the X direction on the first pass. |
| 441 | fTilesX++; |
| 442 | } |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 443 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 444 | SkIntToScalar(tile_y_start), |
| 445 | SkIntToScalar(current_width), |
| 446 | SkIntToScalar(fTileHeight)); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 447 | tile_x_start += current_width; |
| 448 | } |
| 449 | |
| 450 | current_width >>= 1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 455 | /** |
| 456 | * Draw the specified playback to the canvas translated to rectangle provided, so that this mini |
| 457 | * canvas represents the rectangle's portion of the overall picture. |
| 458 | * Saves and restores so that the initial clip and matrix return to their state before this function |
| 459 | * is called. |
| 460 | */ |
| 461 | template<class T> |
| 462 | static void DrawTileToCanvas(SkCanvas* canvas, const SkRect& tileRect, T* playback) { |
| 463 | int saveCount = canvas->save(); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 464 | // Translate so that we draw the correct portion of the picture. |
| 465 | // Perform a postTranslate so that the scaleFactor does not interfere with the positioning. |
| 466 | SkMatrix mat(canvas->getTotalMatrix()); |
| 467 | mat.postTranslate(-tileRect.fLeft, -tileRect.fTop); |
| 468 | canvas->setMatrix(mat); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 469 | playback->draw(canvas); |
| 470 | canvas->restoreToCount(saveCount); |
| 471 | canvas->flush(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 472 | } |
| 473 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 474 | /////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 475 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 476 | bool TiledPictureRenderer::nextTile(int &i, int &j) { |
| 477 | if (++fCurrentTileOffset < fTileRects.count()) { |
| 478 | i = fCurrentTileOffset % fTilesX; |
| 479 | j = fCurrentTileOffset / fTilesX; |
| 480 | return true; |
| 481 | } |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | void TiledPictureRenderer::drawCurrentTile() { |
| 486 | SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count()); |
| 487 | DrawTileToCanvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture); |
| 488 | } |
| 489 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 490 | bool TiledPictureRenderer::render(const SkString* path) { |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 491 | SkASSERT(fPicture != NULL); |
| 492 | if (NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 493 | return false; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 494 | } |
| 495 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 496 | bool success = true; |
| 497 | for (int i = 0; i < fTileRects.count(); ++i) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 498 | DrawTileToCanvas(fCanvas, fTileRects[i], fPicture); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 499 | if (NULL != path) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 500 | success &= writeAppendNumber(fCanvas, path, i); |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 501 | } |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 502 | } |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 503 | return success; |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 504 | } |
| 505 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 506 | SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) { |
| 507 | SkCanvas* canvas = this->INHERITED::setupCanvas(width, height); |
| 508 | SkASSERT(fPicture != NULL); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 509 | // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This |
| 510 | // is mostly important for tiles on the right and bottom edges as they may go over this area and |
| 511 | // the picture may have some commands that draw outside of this area and so should not actually |
| 512 | // be written. |
| 513 | // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set |
| 514 | // by INHERITED::setupCanvas. |
| 515 | SkRegion clipRegion; |
| 516 | clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight()); |
| 517 | canvas->clipRegion(clipRegion); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 518 | return canvas; |
| 519 | } |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 520 | |
| 521 | SkString TiledPictureRenderer::getConfigNameInternal() { |
| 522 | SkString name; |
| 523 | if (fTileMinPowerOf2Width > 0) { |
| 524 | name.append("pow2tile_"); |
| 525 | name.appendf("%i", fTileMinPowerOf2Width); |
| 526 | } else { |
| 527 | name.append("tile_"); |
| 528 | if (fTileWidthPercentage > 0) { |
| 529 | name.appendf("%.f%%", fTileWidthPercentage); |
| 530 | } else { |
| 531 | name.appendf("%i", fTileWidth); |
| 532 | } |
| 533 | } |
| 534 | name.append("x"); |
| 535 | if (fTileHeightPercentage > 0) { |
| 536 | name.appendf("%.f%%", fTileHeightPercentage); |
| 537 | } else { |
| 538 | name.appendf("%i", fTileHeight); |
| 539 | } |
| 540 | return name; |
| 541 | } |
| 542 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 543 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 544 | |
| 545 | // Holds all of the information needed to draw a set of tiles. |
| 546 | class CloneData : public SkRunnable { |
| 547 | |
| 548 | public: |
| 549 | CloneData(SkPicture* clone, SkCanvas* canvas, SkTDArray<SkRect>& rects, int start, int end, |
| 550 | SkRunnable* done) |
| 551 | : fClone(clone) |
| 552 | , fCanvas(canvas) |
| 553 | , fPath(NULL) |
| 554 | , fRects(rects) |
| 555 | , fStart(start) |
| 556 | , fEnd(end) |
| 557 | , fSuccess(NULL) |
| 558 | , fDone(done) { |
| 559 | SkASSERT(fDone != NULL); |
| 560 | } |
| 561 | |
| 562 | virtual void run() SK_OVERRIDE { |
| 563 | SkGraphics::SetTLSFontCacheLimit(1024 * 1024); |
| 564 | for (int i = fStart; i < fEnd; i++) { |
| 565 | DrawTileToCanvas(fCanvas, fRects[i], fClone); |
| 566 | if (fPath != NULL && !writeAppendNumber(fCanvas, fPath, i) |
| 567 | && fSuccess != NULL) { |
| 568 | *fSuccess = false; |
| 569 | // If one tile fails to write to a file, do not continue drawing the rest. |
| 570 | break; |
| 571 | } |
| 572 | } |
| 573 | fDone->run(); |
| 574 | } |
| 575 | |
| 576 | void setPathAndSuccess(const SkString* path, bool* success) { |
| 577 | fPath = path; |
| 578 | fSuccess = success; |
| 579 | } |
| 580 | |
| 581 | private: |
| 582 | // All pointers unowned. |
| 583 | SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which |
| 584 | // is threadsafe. |
| 585 | SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile. |
| 586 | const SkString* fPath; // If non-null, path to write the result to as a PNG. |
| 587 | SkTDArray<SkRect>& fRects; // All tiles of the picture. |
| 588 | const int fStart; // Range of tiles drawn by this thread. |
| 589 | const int fEnd; |
| 590 | bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads, |
| 591 | // and only set to false upon failure to write to a PNG. |
| 592 | SkRunnable* fDone; |
| 593 | }; |
| 594 | |
| 595 | MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount) |
| 596 | : fNumThreads(threadCount) |
| 597 | , fThreadPool(threadCount) |
| 598 | , fCountdown(threadCount) { |
| 599 | // Only need to create fNumThreads - 1 clones, since one thread will use the base |
| 600 | // picture. |
| 601 | fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1); |
| 602 | fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads); |
| 603 | } |
| 604 | |
| 605 | void MultiCorePictureRenderer::init(SkPicture *pict) { |
| 606 | // Set fPicture and the tiles. |
| 607 | this->INHERITED::init(pict); |
| 608 | for (int i = 0; i < fNumThreads; ++i) { |
| 609 | *fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight()); |
| 610 | } |
| 611 | // Only need to create fNumThreads - 1 clones, since one thread will use the base picture. |
| 612 | fPicture->clone(fPictureClones, fNumThreads - 1); |
| 613 | // Populate each thread with the appropriate data. |
| 614 | // Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all. |
| 615 | const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads; |
| 616 | |
| 617 | for (int i = 0; i < fNumThreads; i++) { |
| 618 | SkPicture* pic; |
| 619 | if (i == fNumThreads-1) { |
| 620 | // The last set will use the original SkPicture. |
| 621 | pic = fPicture; |
| 622 | } else { |
| 623 | pic = &fPictureClones[i]; |
| 624 | } |
| 625 | const int start = i * chunkSize; |
| 626 | const int end = SkMin32(start + chunkSize, fTileRects.count()); |
| 627 | fCloneData[i] = SkNEW_ARGS(CloneData, |
| 628 | (pic, fCanvasPool[i], fTileRects, start, end, &fCountdown)); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | bool MultiCorePictureRenderer::render(const SkString *path) { |
| 633 | bool success = true; |
| 634 | if (path != NULL) { |
| 635 | for (int i = 0; i < fNumThreads-1; i++) { |
| 636 | fCloneData[i]->setPathAndSuccess(path, &success); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | fCountdown.reset(fNumThreads); |
| 641 | for (int i = 0; i < fNumThreads; i++) { |
| 642 | fThreadPool.add(fCloneData[i]); |
| 643 | } |
| 644 | fCountdown.wait(); |
| 645 | |
| 646 | return success; |
| 647 | } |
| 648 | |
| 649 | void MultiCorePictureRenderer::end() { |
| 650 | for (int i = 0; i < fNumThreads - 1; i++) { |
| 651 | SkDELETE(fCloneData[i]); |
| 652 | fCloneData[i] = NULL; |
| 653 | } |
| 654 | |
| 655 | fCanvasPool.unrefAll(); |
| 656 | |
| 657 | this->INHERITED::end(); |
| 658 | } |
| 659 | |
| 660 | MultiCorePictureRenderer::~MultiCorePictureRenderer() { |
| 661 | // Each individual CloneData was deleted in end. |
| 662 | SkDELETE_ARRAY(fCloneData); |
| 663 | SkDELETE_ARRAY(fPictureClones); |
| 664 | } |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 665 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 666 | SkString MultiCorePictureRenderer::getConfigNameInternal() { |
| 667 | SkString name = this->INHERITED::getConfigNameInternal(); |
| 668 | name.appendf("_multi_%i_threads", fNumThreads); |
| 669 | return name; |
| 670 | } |
| 671 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 672 | /////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 673 | |
| 674 | void PlaybackCreationRenderer::setup() { |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 675 | fReplayer.reset(this->createPicture()); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 676 | SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->getViewHeight(), |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 677 | this->recordFlags()); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame^] | 678 | this->scaleToScaleFactor(recorder); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 679 | fPicture->draw(recorder); |
| 680 | } |
| 681 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 682 | bool PlaybackCreationRenderer::render(const SkString*) { |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 683 | fReplayer->endRecording(); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 684 | // Since this class does not actually render, return false. |
| 685 | return false; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 686 | } |
| 687 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 688 | SkString PlaybackCreationRenderer::getConfigNameInternal() { |
| 689 | return SkString("playback_creation"); |
| 690 | } |
| 691 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 692 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 693 | // SkPicture variants for each BBoxHierarchy type |
| 694 | |
| 695 | class RTreePicture : public SkPicture { |
| 696 | public: |
| 697 | virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{ |
| 698 | static const int kRTreeMinChildren = 6; |
| 699 | static const int kRTreeMaxChildren = 11; |
| 700 | SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), |
| 701 | SkIntToScalar(fHeight)); |
| 702 | return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, |
| 703 | aspectRatio); |
| 704 | } |
| 705 | }; |
| 706 | |
| 707 | SkPicture* PictureRenderer::createPicture() { |
| 708 | switch (fBBoxHierarchyType) { |
| 709 | case kNone_BBoxHierarchyType: |
| 710 | return SkNEW(SkPicture); |
| 711 | case kRTree_BBoxHierarchyType: |
| 712 | return SkNEW(RTreePicture); |
junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 713 | case kTileGrid_BBoxHierarchyType: |
junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 714 | return SkNEW_ARGS(SkTileGridPicture, (fGridWidth, fGridHeight, fPicture->width(), |
| 715 | fPicture->height())); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 716 | } |
| 717 | SkASSERT(0); // invalid bbhType |
| 718 | return NULL; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 719 | } |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 720 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 721 | /////////////////////////////////////////////////////////////////////////////// |
| 722 | |
| 723 | class GatherRenderer : public PictureRenderer { |
| 724 | public: |
| 725 | virtual bool render(const SkString* path) SK_OVERRIDE { |
| 726 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()), |
| 727 | SkIntToScalar(fPicture->height())); |
| 728 | SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds); |
| 729 | SkSafeUnref(data); |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 730 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 731 | return NULL == path; // we don't have anything to write |
| 732 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 733 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 734 | private: |
| 735 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 736 | return SkString("gather_pixelrefs"); |
| 737 | } |
| 738 | }; |
| 739 | |
| 740 | PictureRenderer* CreateGatherPixelRefsRenderer() { |
| 741 | return SkNEW(GatherRenderer); |
| 742 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 743 | |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 744 | /////////////////////////////////////////////////////////////////////////////// |
| 745 | |
| 746 | class PictureCloneRenderer : public PictureRenderer { |
| 747 | public: |
| 748 | virtual bool render(const SkString* path) SK_OVERRIDE { |
| 749 | for (int i = 0; i < 100; ++i) { |
| 750 | SkPicture* clone = fPicture->clone(); |
| 751 | SkSafeUnref(clone); |
| 752 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 753 | |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 754 | return NULL == path; // we don't have anything to write |
| 755 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 756 | |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 757 | private: |
| 758 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 759 | return SkString("picture_clone"); |
| 760 | } |
| 761 | }; |
| 762 | |
| 763 | PictureRenderer* CreatePictureCloneRenderer() { |
| 764 | return SkNEW(PictureCloneRenderer); |
| 765 | } |
| 766 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 767 | } // namespace sk_tools |