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