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