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