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