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