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