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