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" |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 11 | #include "SkBitmapHasher.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 13 | #include "SkData.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 14 | #include "SkDevice.h" |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 15 | #include "SkDiscardableMemoryPool.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 16 | #include "SkGPipe.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
robertphillips@google.com | fe1b536 | 2013-02-07 19:45:46 +0000 | [diff] [blame] | 18 | #include "gl/GrGLDefines.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 19 | #include "SkGpuDevice.h" |
| 20 | #endif |
| 21 | #include "SkGraphics.h" |
| 22 | #include "SkImageEncoder.h" |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 23 | #include "SkMaskFilter.h" |
keyar@chromium.org | ea82695 | 2012-08-23 15:24:13 +0000 | [diff] [blame] | 24 | #include "SkMatrix.h" |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 25 | #include "SkMultiPictureDraw.h" |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 26 | #include "SkOSFile.h" |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 27 | #include "SkPaintFilterCanvas.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 28 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 29 | #include "SkPictureRecorder.h" |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 30 | #include "SkPictureUtils.h" |
| 31 | #include "SkPixelRef.h" |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 32 | #include "SkPixelSerializer.h" |
keyar@chromium.org | ea82695 | 2012-08-23 15:24:13 +0000 | [diff] [blame] | 33 | #include "SkScalar.h" |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 34 | #include "SkStream.h" |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 35 | #include "SkString.h" |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 36 | #include "SkSurface.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 37 | #include "SkTemplates.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 38 | #include "SkTDArray.h" |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 39 | #include "SkThreadUtils.h" |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 40 | #include "SkTypes.h" |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 41 | #include "sk_tool_utils.h" |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 42 | |
reed@google.com | e15b2f5 | 2013-12-18 04:59:26 +0000 | [diff] [blame] | 43 | static inline SkScalar scalar_log2(SkScalar x) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 44 | static const SkScalar log2_conversion_factor = SkScalarInvert(SkScalarLog(2)); |
skia.committer@gmail.com | 3b85deb | 2013-12-18 07:01:56 +0000 | [diff] [blame] | 45 | |
reed@google.com | e15b2f5 | 2013-12-18 04:59:26 +0000 | [diff] [blame] | 46 | return SkScalarLog(x) * log2_conversion_factor; |
| 47 | } |
| 48 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 49 | namespace sk_tools { |
| 50 | |
| 51 | enum { |
| 52 | kDefaultTileWidth = 256, |
| 53 | kDefaultTileHeight = 256 |
| 54 | }; |
| 55 | |
mtklein | 2a65a23 | 2014-08-26 14:07:04 -0700 | [diff] [blame] | 56 | void PictureRenderer::init(const SkPicture* pict, |
| 57 | const SkString* writePath, |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 58 | const SkString* mismatchPath, |
mtklein | 2a65a23 | 2014-08-26 14:07:04 -0700 | [diff] [blame] | 59 | const SkString* inputFilename, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 60 | bool useChecksumBasedFilenames, |
| 61 | bool useMultiPictureDraw) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 62 | this->CopyString(&fWritePath, writePath); |
| 63 | this->CopyString(&fMismatchPath, mismatchPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 64 | this->CopyString(&fInputFilename, inputFilename); |
| 65 | fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 66 | fUseMultiPictureDraw = useMultiPictureDraw; |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 67 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | SkASSERT(nullptr == fPicture); |
| 69 | SkASSERT(nullptr == fCanvas.get()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 70 | if (fPicture || fCanvas.get()) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | SkASSERT(pict != nullptr); |
| 75 | if (nullptr == pict) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 79 | fPicture.reset(SkRef(pict)); |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 80 | fCanvas.reset(this->setupCanvas()); |
| 81 | } |
| 82 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 83 | void PictureRenderer::CopyString(SkString* dest, const SkString* src) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 84 | if (src) { |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 85 | dest->set(*src); |
| 86 | } else { |
| 87 | dest->reset(); |
| 88 | } |
| 89 | } |
| 90 | |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 91 | class FlagsFilterCanvas : public SkPaintFilterCanvas { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 92 | public: |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 93 | FlagsFilterCanvas(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* flags) |
| 94 | : INHERITED(canvas->imageInfo().width(), canvas->imageInfo().height()) |
| 95 | , fFlags(flags) { |
| 96 | this->addCanvas(canvas); |
| 97 | } |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 98 | |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 99 | protected: |
| 100 | void onFilterPaint(SkPaint* paint, Type t) const override { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 101 | paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags); |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 102 | if (PictureRenderer::kMaskFilter_DrawFilterFlag & fFlags[t]) { |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 103 | SkMaskFilter* maskFilter = paint->getMaskFilter(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 104 | if (maskFilter) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | paint->setMaskFilter(nullptr); |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) { |
| 109 | paint->setHinting(SkPaint::kNo_Hinting); |
| 110 | } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) { |
| 111 | paint->setHinting(SkPaint::kSlight_Hinting); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 116 | const PictureRenderer::DrawFilterFlags* fFlags; |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 117 | |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 118 | typedef SkPaintFilterCanvas INHERITED; |
| 119 | }; |
caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 120 | |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 121 | SkCanvas* PictureRenderer::setupCanvas() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 122 | const int width = this->getViewWidth(); |
| 123 | const int height = this->getViewHeight(); |
| 124 | return this->setupCanvas(width, height); |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | SkCanvas* PictureRenderer::setupCanvas(int width, int height) { |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 128 | SkAutoTUnref<SkCanvas> canvas; |
| 129 | |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 130 | switch(fDeviceType) { |
| 131 | case kBitmap_DeviceType: { |
| 132 | SkBitmap bitmap; |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 133 | sk_tools::setup_bitmap(&bitmap, width, height); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 134 | canvas.reset(new SkCanvas(bitmap)); |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 135 | } |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 136 | break; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 137 | #if SK_SUPPORT_GPU |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 138 | #if SK_ANGLE |
| 139 | case kAngle_DeviceType: |
| 140 | // fall through |
| 141 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 142 | #if SK_COMMAND_BUFFER |
| 143 | case kCommandBuffer_DeviceType: |
| 144 | // fall through |
| 145 | #endif |
rmistry@google.com | 6ab9673 | 2014-01-06 18:37:24 +0000 | [diff] [blame] | 146 | #if SK_MESA |
| 147 | case kMesa_DeviceType: |
| 148 | // fall through |
| 149 | #endif |
commit-bot@chromium.org | 0fd5270 | 2014-03-07 18:41:14 +0000 | [diff] [blame] | 150 | case kGPU_DeviceType: |
| 151 | case kNVPR_DeviceType: { |
commit-bot@chromium.org | ae403b9 | 2013-04-10 17:27:30 +0000 | [diff] [blame] | 152 | SkAutoTUnref<GrSurface> target; |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 153 | if (fGrContext) { |
| 154 | // create a render target to back the device |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 155 | GrSurfaceDesc desc; |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 156 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 157 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 158 | desc.fWidth = width; |
| 159 | desc.fHeight = height; |
jvanverth@google.com | f6a9033 | 2013-05-02 12:39:37 +0000 | [diff] [blame] | 160 | desc.fSampleCnt = fSampleCount; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | target.reset(fGrContext->textureProvider()->createTexture(desc, false, nullptr, 0)); |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 162 | } |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 163 | |
bsalomon | afcd7cd | 2015-08-31 12:39:41 -0700 | [diff] [blame] | 164 | uint32_t flags = fUseDFText ? SkSurfaceProps::kUseDeviceIndependentFonts_Flag : 0; |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 165 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
bsalomon | 74f681d | 2015-06-23 14:38:48 -0700 | [diff] [blame] | 166 | SkAutoTUnref<SkGpuDevice> device( |
| 167 | SkGpuDevice::Create(target->asRenderTarget(), &props, |
| 168 | SkGpuDevice::kUninit_InitContents)); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 169 | if (!device) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | return nullptr; |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 171 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 172 | canvas.reset(new SkCanvas(device)); |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 173 | break; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 174 | } |
| 175 | #endif |
| 176 | default: |
| 177 | SkASSERT(0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | return nullptr; |
keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 179 | } |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 180 | |
| 181 | if (fHasDrawFilters) { |
| 182 | if (fDrawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) { |
| 183 | canvas->setAllowSoftClip(false); |
| 184 | } |
| 185 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 186 | canvas.reset(new FlagsFilterCanvas(canvas.get(), fDrawFilters)); |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 187 | } |
| 188 | |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 189 | this->scaleToScaleFactor(canvas); |
commit-bot@chromium.org | 17cc3ea | 2014-01-15 14:51:25 +0000 | [diff] [blame] | 190 | |
| 191 | // Pictures often lie about their extent (i.e., claim to be 100x100 but |
| 192 | // only ever draw to 90x100). Clear here so the undrawn portion will have |
| 193 | // a consistent color |
| 194 | canvas->clear(SK_ColorTRANSPARENT); |
fmalita | 5042020 | 2015-06-15 11:48:16 -0700 | [diff] [blame] | 195 | return canvas.detach(); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 196 | } |
keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 197 | |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 198 | void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | SkASSERT(canvas != nullptr); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 200 | if (fScaleFactor != SK_Scalar1) { |
| 201 | canvas->scale(fScaleFactor, fScaleFactor); |
| 202 | } |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void PictureRenderer::end() { |
scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 206 | this->resetState(true); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 207 | fPicture.reset(nullptr); |
| 208 | fCanvas.reset(nullptr); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 209 | } |
| 210 | |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 211 | int PictureRenderer::getViewWidth() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | SkASSERT(fPicture != nullptr); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 213 | int width = SkScalarCeilToInt(fPicture->cullRect().width() * fScaleFactor); |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 214 | if (fViewport.width() > 0) { |
| 215 | width = SkMin32(width, fViewport.width()); |
| 216 | } |
| 217 | return width; |
| 218 | } |
| 219 | |
| 220 | int PictureRenderer::getViewHeight() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 221 | SkASSERT(fPicture != nullptr); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 222 | int height = SkScalarCeilToInt(fPicture->cullRect().height() * fScaleFactor); |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 223 | if (fViewport.height() > 0) { |
| 224 | height = SkMin32(height, fViewport.height()); |
| 225 | } |
| 226 | return height; |
| 227 | } |
| 228 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 229 | /** Converts fPicture to a picture that uses a BBoxHierarchy. |
| 230 | * PictureRenderer subclasses that are used to test picture playback |
| 231 | * should call this method during init. |
| 232 | */ |
| 233 | void PictureRenderer::buildBBoxHierarchy() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 234 | SkASSERT(fPicture); |
| 235 | if (kNone_BBoxHierarchyType != fBBoxHierarchyType && fPicture) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 236 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
| 237 | SkPictureRecorder recorder; |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 238 | uint32_t flags = this->recordFlags(); |
| 239 | if (fUseMultiPictureDraw) { |
| 240 | flags |= SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag; |
| 241 | } |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 242 | SkCanvas* canvas = recorder.beginRecording(fPicture->cullRect().width(), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 243 | fPicture->cullRect().height(), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 244 | factory.get(), |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 245 | flags); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 246 | fPicture->playback(canvas); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 247 | fPicture.reset(recorder.endRecording()); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 251 | void PictureRenderer::resetState(bool callFinish) { |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 252 | #if SK_SUPPORT_GPU |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 253 | SkGLContext* glContext = this->getGLContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 254 | if (nullptr == glContext) { |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 255 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 256 | return; |
| 257 | } |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 258 | |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 259 | fGrContext->flush(); |
commit-bot@chromium.org | 51c040e | 2014-03-11 22:58:00 +0000 | [diff] [blame] | 260 | glContext->swapBuffers(); |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 261 | if (callFinish) { |
| 262 | SK_GL(*glContext, Finish()); |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 263 | } |
keyar@chromium.org | a40c20d | 2012-08-20 15:04:12 +0000 | [diff] [blame] | 264 | #endif |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 265 | } |
| 266 | |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 267 | void PictureRenderer::purgeTextures() { |
| 268 | SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool(); |
| 269 | |
| 270 | pool->dumpPool(); |
| 271 | |
| 272 | #if SK_SUPPORT_GPU |
kkinnunen | 9e61bb7 | 2014-10-09 05:24:15 -0700 | [diff] [blame] | 273 | SkGLContext* glContext = this->getGLContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 274 | if (nullptr == glContext) { |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 275 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | // resetState should've already done this |
| 280 | fGrContext->flush(); |
| 281 | |
| 282 | fGrContext->purgeAllUnlockedResources(); |
| 283 | #endif |
| 284 | } |
| 285 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 286 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 287 | |
djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 288 | SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) { |
| 289 | // defer the canvas setup until the render step |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 290 | return nullptr; |
djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 291 | } |
| 292 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 293 | bool RecordPictureRenderer::render(SkBitmap** out) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 294 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
| 295 | SkPictureRecorder recorder; |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 296 | SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(this->getViewWidth()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 297 | SkIntToScalar(this->getViewHeight()), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 298 | factory.get(), |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 299 | this->recordFlags()); |
| 300 | this->scaleToScaleFactor(canvas); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 301 | fPicture->playback(canvas); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 302 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 303 | if (!fWritePath.isEmpty()) { |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 304 | // Record the new picture as a new SKP with PNG encoded bitmaps. |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 305 | SkString skpPath = SkOSPath::Join(fWritePath.c_str(), fInputFilename.c_str()); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 306 | SkFILEWStream stream(skpPath.c_str()); |
robertphillips | 3e5c2b1 | 2015-03-23 05:46:51 -0700 | [diff] [blame] | 307 | sk_tool_utils::PngPixelSerializer serializer; |
scroggo | 895c43b | 2014-12-11 10:53:58 -0800 | [diff] [blame] | 308 | picture->serialize(&stream, &serializer); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 311 | return false; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 312 | } |
| 313 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 314 | SkString RecordPictureRenderer::getConfigNameInternal() { |
| 315 | return SkString("record"); |
| 316 | } |
| 317 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 318 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 319 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 320 | bool PipePictureRenderer::render(SkBitmap** out) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 321 | SkASSERT(fCanvas.get() != nullptr); |
| 322 | SkASSERT(fPicture != nullptr); |
| 323 | if (nullptr == fCanvas.get() || nullptr == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 324 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | PipeController pipeController(fCanvas.get()); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 328 | SkGPipeWriter writer; |
| 329 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 330 | pipeCanvas->drawPicture(fPicture); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 331 | writer.endRecording(); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 332 | fCanvas->flush(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 333 | if (out) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 334 | *out = new SkBitmap; |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 335 | setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 336 | SkScalarCeilToInt(fPicture->cullRect().height())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 337 | fCanvas->readPixels(*out, 0, 0); |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 338 | } |
mtklein | 0c768a2 | 2015-10-20 14:29:10 -0700 | [diff] [blame] | 339 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 340 | } |
| 341 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 342 | SkString PipePictureRenderer::getConfigNameInternal() { |
| 343 | return SkString("pipe"); |
| 344 | } |
| 345 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 346 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 347 | |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 348 | void SimplePictureRenderer::init(const SkPicture* picture, const SkString* writePath, |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 349 | const SkString* mismatchPath, const SkString* inputFilename, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 350 | bool useChecksumBasedFilenames, bool useMultiPictureDraw) { |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 351 | INHERITED::init(picture, writePath, mismatchPath, inputFilename, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 352 | useChecksumBasedFilenames, useMultiPictureDraw); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 353 | this->buildBBoxHierarchy(); |
| 354 | } |
| 355 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 356 | bool SimplePictureRenderer::render(SkBitmap** out) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 357 | SkASSERT(fCanvas.get() != nullptr); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 358 | SkASSERT(fPicture); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 359 | if (nullptr == fCanvas.get() || nullptr == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 360 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 361 | } |
| 362 | |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 363 | if (fUseMultiPictureDraw) { |
| 364 | SkMultiPictureDraw mpd; |
| 365 | |
| 366 | mpd.add(fCanvas, fPicture); |
| 367 | |
| 368 | mpd.draw(); |
| 369 | } else { |
| 370 | fCanvas->drawPicture(fPicture); |
| 371 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 372 | fCanvas->flush(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 373 | if (out) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 374 | *out = new SkBitmap; |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 375 | setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 376 | SkScalarCeilToInt(fPicture->cullRect().height())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 377 | fCanvas->readPixels(*out, 0, 0); |
| 378 | } |
mtklein | 0c768a2 | 2015-10-20 14:29:10 -0700 | [diff] [blame] | 379 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 380 | } |
| 381 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 382 | SkString SimplePictureRenderer::getConfigNameInternal() { |
| 383 | return SkString("simple"); |
| 384 | } |
| 385 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 386 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 387 | |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 388 | #if SK_SUPPORT_GPU |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 389 | TiledPictureRenderer::TiledPictureRenderer(const GrContextOptions& opts) |
mtklein | 2a65a23 | 2014-08-26 14:07:04 -0700 | [diff] [blame] | 390 | : INHERITED(opts) |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 391 | , fTileWidth(kDefaultTileWidth) |
| 392 | #else |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 393 | TiledPictureRenderer::TiledPictureRenderer() |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 394 | : fTileWidth(kDefaultTileWidth) |
krajcevski | b1aded8 | 2014-08-18 07:52:17 -0700 | [diff] [blame] | 395 | #endif |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 396 | , fTileHeight(kDefaultTileHeight) |
rileya@google.com | a04dc02 | 2012-09-10 19:01:38 +0000 | [diff] [blame] | 397 | , fTileWidthPercentage(0.0) |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 398 | , fTileHeightPercentage(0.0) |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 399 | , fTileMinPowerOf2Width(0) |
| 400 | , fCurrentTileOffset(-1) |
| 401 | , fTilesX(0) |
| 402 | , fTilesY(0) { } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 403 | |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 404 | void TiledPictureRenderer::init(const SkPicture* pict, const SkString* writePath, |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 405 | const SkString* mismatchPath, const SkString* inputFilename, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 406 | bool useChecksumBasedFilenames, bool useMultiPictureDraw) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 407 | SkASSERT(pict); |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 408 | SkASSERT(0 == fTileRects.count()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 409 | if (nullptr == pict || fTileRects.count() != 0) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 413 | // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not |
| 414 | // used by bench_pictures. |
bungeman | 77a53de | 2015-10-01 12:28:49 -0700 | [diff] [blame] | 415 | fPicture.reset(SkRef(pict)); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 416 | this->CopyString(&fWritePath, writePath); |
| 417 | this->CopyString(&fMismatchPath, mismatchPath); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 418 | this->CopyString(&fInputFilename, inputFilename); |
| 419 | fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 420 | fUseMultiPictureDraw = useMultiPictureDraw; |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 421 | this->buildBBoxHierarchy(); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 422 | |
| 423 | if (fTileWidthPercentage > 0) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 424 | fTileWidth = SkScalarCeilToInt(float(fTileWidthPercentage * fPicture->cullRect().width() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 425 | } |
| 426 | if (fTileHeightPercentage > 0) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 427 | fTileHeight = SkScalarCeilToInt(float(fTileHeightPercentage * fPicture->cullRect().height() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 428 | } |
| 429 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 430 | if (fTileMinPowerOf2Width > 0) { |
| 431 | this->setupPowerOf2Tiles(); |
| 432 | } else { |
| 433 | this->setupTiles(); |
| 434 | } |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 435 | fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight)); |
| 436 | // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the |
| 437 | // first call to drawCurrentTile. |
| 438 | fCurrentTileOffset = -1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 439 | } |
| 440 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 441 | void TiledPictureRenderer::end() { |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 442 | fTileRects.reset(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 443 | this->INHERITED::end(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 444 | } |
| 445 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 446 | void TiledPictureRenderer::setupTiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 447 | // Only use enough tiles to cover the viewport |
| 448 | const int width = this->getViewWidth(); |
| 449 | const int height = this->getViewHeight(); |
| 450 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 451 | fTilesX = fTilesY = 0; |
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++; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 454 | 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] | 455 | if (0 == tile_y_start) { |
| 456 | // Only count tiles in the X direction on the first pass. |
| 457 | fTilesX++; |
| 458 | } |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 459 | *fTileRects.append() = SkIRect::MakeXYWH(tile_x_start, tile_y_start, |
| 460 | fTileWidth, fTileHeight); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 465 | bool TiledPictureRenderer::tileDimensions(int &x, int &y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 466 | if (fTileRects.count() == 0 || nullptr == fPicture) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | x = fTilesX; |
| 470 | y = fTilesY; |
| 471 | return true; |
| 472 | } |
| 473 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 474 | // The goal of the powers of two tiles is to minimize the amount of wasted tile |
| 475 | // space in the width-wise direction and then minimize the number of tiles. The |
| 476 | // constraints are that every tile must have a pixel width that is a power of |
| 477 | // two and also be of some minimal width (that is also a power of two). |
| 478 | // |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 479 | // 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] | 480 | // multiple of the minimal width. The binary representation of this rounded |
| 481 | // value gives us the tiles we need: a bit of value one means we need a tile of |
| 482 | // that size. |
| 483 | void TiledPictureRenderer::setupPowerOf2Tiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 484 | // Only use enough tiles to cover the viewport |
| 485 | const int width = this->getViewWidth(); |
| 486 | const int height = this->getViewHeight(); |
| 487 | |
| 488 | int rounded_value = width; |
| 489 | if (width % fTileMinPowerOf2Width != 0) { |
| 490 | rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 491 | } |
| 492 | |
reed@google.com | e15b2f5 | 2013-12-18 04:59:26 +0000 | [diff] [blame] | 493 | int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width))); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 494 | int largest_possible_tile_size = 1 << num_bits; |
| 495 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 496 | fTilesX = fTilesY = 0; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 497 | // The tile height is constant for a particular picture. |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 498 | 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] | 499 | fTilesY++; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 500 | int tile_x_start = 0; |
| 501 | int current_width = largest_possible_tile_size; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 502 | // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough |
| 503 | // to draw each tile. |
| 504 | fTileWidth = current_width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 505 | |
| 506 | while (current_width >= fTileMinPowerOf2Width) { |
| 507 | // It is very important this is a bitwise AND. |
| 508 | if (current_width & rounded_value) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 509 | if (0 == tile_y_start) { |
| 510 | // Only count tiles in the X direction on the first pass. |
| 511 | fTilesX++; |
| 512 | } |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 513 | *fTileRects.append() = SkIRect::MakeXYWH(tile_x_start, tile_y_start, |
| 514 | current_width, fTileHeight); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 515 | tile_x_start += current_width; |
| 516 | } |
| 517 | |
| 518 | current_width >>= 1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 523 | /** |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 524 | * Draw the specified picture to the canvas translated to rectangle provided, so that this mini |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 525 | * canvas represents the rectangle's portion of the overall picture. |
| 526 | * Saves and restores so that the initial clip and matrix return to their state before this function |
| 527 | * is called. |
| 528 | */ |
mtklein | 2a65a23 | 2014-08-26 14:07:04 -0700 | [diff] [blame] | 529 | static void draw_tile_to_canvas(SkCanvas* canvas, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 530 | const SkIRect& tileRect, |
robertphillips | ce4dd3d | 2014-07-07 13:46:35 -0700 | [diff] [blame] | 531 | const SkPicture* picture) { |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 532 | int saveCount = canvas->save(); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 533 | // Translate so that we draw the correct portion of the picture. |
| 534 | // Perform a postTranslate so that the scaleFactor does not interfere with the positioning. |
| 535 | SkMatrix mat(canvas->getTotalMatrix()); |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 536 | mat.postTranslate(-SkIntToScalar(tileRect.fLeft), -SkIntToScalar(tileRect.fTop)); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 537 | canvas->setMatrix(mat); |
robertphillips | 43044dd | 2014-11-28 07:42:16 -0800 | [diff] [blame] | 538 | canvas->clipRect(SkRect::Make(tileRect)); |
robertphillips | 4a36d9a | 2014-10-20 08:45:57 -0700 | [diff] [blame] | 539 | canvas->clear(SK_ColorTRANSPARENT); // Not every picture covers the entirety of every tile |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 540 | canvas->drawPicture(picture); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 541 | canvas->restoreToCount(saveCount); |
| 542 | canvas->flush(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 543 | } |
| 544 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 545 | /////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 546 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 547 | /** |
| 548 | * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap. |
| 549 | * If the src bitmap is too large to fit within the dst bitmap after the x and y |
| 550 | * offsets have been applied, any excess will be ignored (so only the top-left portion of the |
| 551 | * src bitmap will be copied). |
| 552 | * |
| 553 | * @param src source bitmap |
| 554 | * @param dst destination bitmap |
| 555 | * @param xOffset x-offset within destination bitmap |
| 556 | * @param yOffset y-offset within destination bitmap |
| 557 | */ |
| 558 | static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst, |
| 559 | int xOffset, int yOffset) { |
| 560 | for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) { |
| 561 | for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) { |
| 562 | *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 567 | bool TiledPictureRenderer::nextTile(int &i, int &j) { |
| 568 | if (++fCurrentTileOffset < fTileRects.count()) { |
| 569 | i = fCurrentTileOffset % fTilesX; |
| 570 | j = fCurrentTileOffset / fTilesX; |
| 571 | return true; |
| 572 | } |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | void TiledPictureRenderer::drawCurrentTile() { |
| 577 | SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count()); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 578 | draw_tile_to_canvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture); |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 579 | } |
| 580 | |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 581 | bool TiledPictureRenderer::postRender(SkCanvas* canvas, const SkIRect& tileRect, |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 582 | SkBitmap* tempBM, SkBitmap** out, |
| 583 | int tileNumber) { |
| 584 | bool success = true; |
| 585 | |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 586 | if (out) { |
| 587 | if (canvas->readPixels(tempBM, 0, 0)) { |
| 588 | // Add this tile to the entire bitmap. |
| 589 | bitmapCopyAtOffset(*tempBM, *out, tileRect.left(), tileRect.top()); |
| 590 | } else { |
| 591 | success = false; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return success; |
| 596 | } |
| 597 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 598 | bool TiledPictureRenderer::render(SkBitmap** out) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 599 | SkASSERT(fPicture != nullptr); |
| 600 | if (nullptr == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 601 | return false; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 602 | } |
| 603 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 604 | SkBitmap bitmap; |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 605 | if (out) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 606 | *out = new SkBitmap; |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 607 | setup_bitmap(*out, SkScalarCeilToInt(fPicture->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 608 | SkScalarCeilToInt(fPicture->cullRect().height())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 609 | setup_bitmap(&bitmap, fTileWidth, fTileHeight); |
| 610 | } |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 611 | bool success = true; |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 612 | |
| 613 | if (fUseMultiPictureDraw) { |
| 614 | SkMultiPictureDraw mpd; |
| 615 | SkTDArray<SkSurface*> surfaces; |
| 616 | surfaces.setReserve(fTileRects.count()); |
| 617 | |
| 618 | // Create a separate SkSurface/SkCanvas for each tile along with a |
| 619 | // translated version of the skp (to mimic Chrome's behavior) and |
| 620 | // feed all such pairs to the MultiPictureDraw. |
| 621 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 622 | SkImageInfo ii = fCanvas->imageInfo().makeWH(fTileRects[i].width(), |
| 623 | fTileRects[i].height()); |
| 624 | *surfaces.append() = fCanvas->newSurface(ii); |
| 625 | surfaces[i]->getCanvas()->setMatrix(fCanvas->getTotalMatrix()); |
| 626 | |
| 627 | SkPictureRecorder recorder; |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 628 | SkRTreeFactory bbhFactory; |
| 629 | |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 630 | SkCanvas* c = recorder.beginRecording(SkIntToScalar(fTileRects[i].width()), |
robertphillips | 81f71b6 | 2014-11-11 04:54:49 -0800 | [diff] [blame] | 631 | SkIntToScalar(fTileRects[i].height()), |
| 632 | &bbhFactory, |
| 633 | SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag); |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 634 | c->save(); |
| 635 | SkMatrix mat; |
| 636 | mat.setTranslate(-SkIntToScalar(fTileRects[i].fLeft), |
| 637 | -SkIntToScalar(fTileRects[i].fTop)); |
| 638 | c->setMatrix(mat); |
| 639 | c->drawPicture(fPicture); |
| 640 | c->restore(); |
| 641 | |
| 642 | SkAutoTUnref<SkPicture> xlatedPicture(recorder.endRecording()); |
| 643 | |
| 644 | mpd.add(surfaces[i]->getCanvas(), xlatedPicture); |
commit-bot@chromium.org | 8991c67 | 2014-05-22 00:36:05 +0000 | [diff] [blame] | 645 | } |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 646 | |
| 647 | // Render all the buffered SkCanvases/SkPictures |
| 648 | mpd.draw(); |
| 649 | |
| 650 | // Sort out the results and cleanup the allocated surfaces |
| 651 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 652 | success &= this->postRender(surfaces[i]->getCanvas(), fTileRects[i], &bitmap, out, i); |
| 653 | surfaces[i]->unref(); |
| 654 | } |
| 655 | } else { |
| 656 | for (int i = 0; i < fTileRects.count(); ++i) { |
| 657 | draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture); |
| 658 | success &= this->postRender(fCanvas, fTileRects[i], &bitmap, out, i); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 659 | } |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 660 | } |
robertphillips | 78c7127 | 2014-10-09 04:59:19 -0700 | [diff] [blame] | 661 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 662 | return success; |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 663 | } |
| 664 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 665 | SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) { |
| 666 | SkCanvas* canvas = this->INHERITED::setupCanvas(width, height); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 667 | SkASSERT(fPicture); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 668 | // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This |
| 669 | // is mostly important for tiles on the right and bottom edges as they may go over this area and |
| 670 | // the picture may have some commands that draw outside of this area and so should not actually |
| 671 | // be written. |
| 672 | // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set |
| 673 | // by INHERITED::setupCanvas. |
| 674 | SkRegion clipRegion; |
| 675 | clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight()); |
| 676 | canvas->clipRegion(clipRegion); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 677 | return canvas; |
| 678 | } |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 679 | |
| 680 | SkString TiledPictureRenderer::getConfigNameInternal() { |
| 681 | SkString name; |
| 682 | if (fTileMinPowerOf2Width > 0) { |
| 683 | name.append("pow2tile_"); |
| 684 | name.appendf("%i", fTileMinPowerOf2Width); |
| 685 | } else { |
| 686 | name.append("tile_"); |
| 687 | if (fTileWidthPercentage > 0) { |
| 688 | name.appendf("%.f%%", fTileWidthPercentage); |
| 689 | } else { |
| 690 | name.appendf("%i", fTileWidth); |
| 691 | } |
| 692 | } |
| 693 | name.append("x"); |
| 694 | if (fTileHeightPercentage > 0) { |
| 695 | name.appendf("%.f%%", fTileHeightPercentage); |
| 696 | } else { |
| 697 | name.appendf("%i", fTileHeight); |
| 698 | } |
| 699 | return name; |
| 700 | } |
| 701 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 702 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 703 | |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 704 | void PlaybackCreationRenderer::setup() { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 705 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 706 | fRecorder.reset(new SkPictureRecorder); |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 707 | SkCanvas* canvas = fRecorder->beginRecording(SkIntToScalar(this->getViewWidth()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 708 | SkIntToScalar(this->getViewHeight()), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 709 | factory.get(), |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 710 | this->recordFlags()); |
| 711 | this->scaleToScaleFactor(canvas); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 712 | canvas->drawPicture(fPicture); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 713 | } |
| 714 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 715 | bool PlaybackCreationRenderer::render(SkBitmap** out) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 716 | fPicture.reset(fRecorder->endRecording()); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 717 | // Since this class does not actually render, return false. |
| 718 | return false; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 719 | } |
| 720 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 721 | SkString PlaybackCreationRenderer::getConfigNameInternal() { |
| 722 | return SkString("playback_creation"); |
| 723 | } |
| 724 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 725 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 726 | // SkPicture variants for each BBoxHierarchy type |
| 727 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 728 | SkBBHFactory* PictureRenderer::getFactory() { |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 729 | switch (fBBoxHierarchyType) { |
| 730 | case kNone_BBoxHierarchyType: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 731 | return nullptr; |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 732 | case kRTree_BBoxHierarchyType: |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 733 | return new SkRTreeFactory; |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 734 | } |
| 735 | SkASSERT(0); // invalid bbhType |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 736 | return nullptr; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 737 | } |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 738 | |
| 739 | } // namespace sk_tools |