| 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" |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 25 | #include "SkPicture.h" |
| scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 26 | #include "SkPictureUtils.h" |
| 27 | #include "SkPixelRef.h" |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 28 | #include "SkRTree.h" |
| keyar@chromium.org | ea82695 | 2012-08-23 15:24:13 +0000 | [diff] [blame] | 29 | #include "SkScalar.h" |
| scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 30 | #include "SkStream.h" |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 31 | #include "SkString.h" |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 32 | #include "SkTemplates.h" |
| junov@chromium.org | 3cb834b | 2012-12-13 16:39:53 +0000 | [diff] [blame] | 33 | #include "SkTileGridPicture.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 | |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 51 | /* TODO(epoger): These constants are already maintained in 2 other places: |
| 52 | * gm/gm_json.py and gm/gm_expectations.cpp. We shouldn't add yet a third place. |
| 53 | * Figure out a way to share the definitions instead. |
| 54 | */ |
| 55 | const static char kJsonKey_ActualResults[] = "actual-results"; |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 56 | const static char kJsonKey_ActualResults_NoComparison[] = "no-comparison"; |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 57 | const static char kJsonKey_Hashtype_Bitmap_64bitMD5[] = "bitmap-64bitMD5"; |
| 58 | |
| 59 | void ImageResultsSummary::add(const char *testName, const SkBitmap& bitmap) { |
| 60 | uint64_t hash; |
| 61 | SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash)); |
| 62 | Json::Value jsonTypeValuePair; |
| 63 | jsonTypeValuePair.append(Json::Value(kJsonKey_Hashtype_Bitmap_64bitMD5)); |
| 64 | jsonTypeValuePair.append(Json::UInt64(hash)); |
| 65 | fActualResultsNoComparison[testName] = jsonTypeValuePair; |
| 66 | } |
| 67 | |
| 68 | void ImageResultsSummary::writeToFile(const char *filename) { |
| 69 | Json::Value actualResults; |
| 70 | actualResults[kJsonKey_ActualResults_NoComparison] = fActualResultsNoComparison; |
| 71 | Json::Value root; |
| 72 | root[kJsonKey_ActualResults] = actualResults; |
| 73 | std::string jsonStdString = root.toStyledString(); |
| 74 | SkFILEWStream stream(filename); |
| 75 | stream.write(jsonStdString.c_str(), jsonStdString.length()); |
| 76 | } |
| 77 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 78 | void PictureRenderer::init(SkPicture* pict) { |
| keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 79 | SkASSERT(NULL == fPicture); |
| 80 | SkASSERT(NULL == fCanvas.get()); |
| 81 | if (fPicture != NULL || NULL != fCanvas.get()) { |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | SkASSERT(pict != NULL); |
| keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 86 | if (NULL == pict) { |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | fPicture = pict; |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 91 | fPicture->ref(); |
| keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 92 | fCanvas.reset(this->setupCanvas()); |
| 93 | } |
| 94 | |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 95 | class FlagsDrawFilter : public SkDrawFilter { |
| 96 | public: |
| 97 | FlagsDrawFilter(PictureRenderer::DrawFilterFlags* flags) : |
| 98 | fFlags(flags) {} |
| 99 | |
| reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 100 | virtual bool filter(SkPaint* paint, Type t) { |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 101 | paint->setFlags(paint->getFlags() & ~fFlags[t] & SkPaint::kAllFlags); |
| robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 102 | if (PictureRenderer::kMaskFilter_DrawFilterFlag & fFlags[t]) { |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 103 | SkMaskFilter* maskFilter = paint->getMaskFilter(); |
| robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 104 | if (NULL != maskFilter) { |
| reed@google.com | 457d8a7 | 2012-12-18 18:20:44 +0000 | [diff] [blame] | 105 | paint->setMaskFilter(NULL); |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | if (PictureRenderer::kHinting_DrawFilterFlag & fFlags[t]) { |
| 109 | paint->setHinting(SkPaint::kNo_Hinting); |
| 110 | } else if (PictureRenderer::kSlightHinting_DrawFilterFlag & fFlags[t]) { |
| 111 | paint->setHinting(SkPaint::kSlight_Hinting); |
| 112 | } |
| reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 113 | return true; |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | private: |
| 117 | PictureRenderer::DrawFilterFlags* fFlags; |
| 118 | }; |
| 119 | |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 120 | static void setUpFilter(SkCanvas* canvas, PictureRenderer::DrawFilterFlags* drawFilters) { |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 121 | if (drawFilters && !canvas->getDrawFilter()) { |
| 122 | canvas->setDrawFilter(SkNEW_ARGS(FlagsDrawFilter, (drawFilters)))->unref(); |
| caryclark@google.com | e3e940c | 2012-11-07 16:42:17 +0000 | [diff] [blame] | 123 | if (drawFilters[0] & PictureRenderer::kAAClip_DrawFilterFlag) { |
| 124 | canvas->setAllowSoftClip(false); |
| 125 | } |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 126 | } |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 129 | SkCanvas* PictureRenderer::setupCanvas() { |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 130 | const int width = this->getViewWidth(); |
| 131 | const int height = this->getViewHeight(); |
| 132 | return this->setupCanvas(width, height); |
| keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | SkCanvas* PictureRenderer::setupCanvas(int width, int height) { |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 136 | SkCanvas* canvas; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 137 | switch(fDeviceType) { |
| 138 | case kBitmap_DeviceType: { |
| 139 | SkBitmap bitmap; |
| keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 140 | sk_tools::setup_bitmap(&bitmap, width, height); |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 141 | canvas = SkNEW_ARGS(SkCanvas, (bitmap)); |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 142 | } |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 143 | break; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 144 | #if SK_SUPPORT_GPU |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 145 | #if SK_ANGLE |
| 146 | case kAngle_DeviceType: |
| 147 | // fall through |
| 148 | #endif |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 149 | case kGPU_DeviceType: { |
| commit-bot@chromium.org | ae403b9 | 2013-04-10 17:27:30 +0000 | [diff] [blame] | 150 | SkAutoTUnref<GrSurface> target; |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 151 | if (fGrContext) { |
| 152 | // create a render target to back the device |
| 153 | GrTextureDesc desc; |
| 154 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 155 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 156 | desc.fWidth = width; |
| 157 | desc.fHeight = height; |
| jvanverth@google.com | f6a9033 | 2013-05-02 12:39:37 +0000 | [diff] [blame] | 158 | desc.fSampleCnt = fSampleCount; |
| commit-bot@chromium.org | ae403b9 | 2013-04-10 17:27:30 +0000 | [diff] [blame] | 159 | target.reset(fGrContext->createUncachedTexture(desc, NULL, 0)); |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 160 | } |
| commit-bot@chromium.org | ae403b9 | 2013-04-10 17:27:30 +0000 | [diff] [blame] | 161 | if (NULL == target.get()) { |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 162 | SkASSERT(0); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| commit-bot@chromium.org | ae403b9 | 2013-04-10 17:27:30 +0000 | [diff] [blame] | 166 | SkAutoTUnref<SkGpuDevice> device(SkGpuDevice::Create(target)); |
| caryclark@google.com | a362237 | 2012-11-06 21:26:13 +0000 | [diff] [blame] | 167 | canvas = SkNEW_ARGS(SkCanvas, (device.get())); |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 168 | break; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 169 | } |
| 170 | #endif |
| 171 | default: |
| 172 | SkASSERT(0); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 173 | return NULL; |
| keyar@chromium.org | 4ea96c5 | 2012-08-20 15:03:29 +0000 | [diff] [blame] | 174 | } |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 175 | setUpFilter(canvas, fDrawFilters); |
| 176 | this->scaleToScaleFactor(canvas); |
| 177 | return canvas; |
| 178 | } |
| keyar@chromium.org | a474ce3 | 2012-08-20 15:03:57 +0000 | [diff] [blame] | 179 | |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 180 | void PictureRenderer::scaleToScaleFactor(SkCanvas* canvas) { |
| 181 | SkASSERT(canvas != NULL); |
| 182 | if (fScaleFactor != SK_Scalar1) { |
| 183 | canvas->scale(fScaleFactor, fScaleFactor); |
| 184 | } |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void PictureRenderer::end() { |
| scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 188 | this->resetState(true); |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 189 | SkSafeUnref(fPicture); |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 190 | fPicture = NULL; |
| 191 | fCanvas.reset(NULL); |
| 192 | } |
| 193 | |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 194 | int PictureRenderer::getViewWidth() { |
| 195 | SkASSERT(fPicture != NULL); |
| robertphillips@google.com | 8ac811e | 2013-02-07 00:13:34 +0000 | [diff] [blame] | 196 | int width = SkScalarCeilToInt(fPicture->width() * fScaleFactor); |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 197 | if (fViewport.width() > 0) { |
| 198 | width = SkMin32(width, fViewport.width()); |
| 199 | } |
| 200 | return width; |
| 201 | } |
| 202 | |
| 203 | int PictureRenderer::getViewHeight() { |
| 204 | SkASSERT(fPicture != NULL); |
| robertphillips@google.com | 8ac811e | 2013-02-07 00:13:34 +0000 | [diff] [blame] | 205 | int height = SkScalarCeilToInt(fPicture->height() * fScaleFactor); |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 206 | if (fViewport.height() > 0) { |
| 207 | height = SkMin32(height, fViewport.height()); |
| 208 | } |
| 209 | return height; |
| 210 | } |
| 211 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 212 | /** Converts fPicture to a picture that uses a BBoxHierarchy. |
| 213 | * PictureRenderer subclasses that are used to test picture playback |
| 214 | * should call this method during init. |
| 215 | */ |
| 216 | void PictureRenderer::buildBBoxHierarchy() { |
| 217 | SkASSERT(NULL != fPicture); |
| 218 | if (kNone_BBoxHierarchyType != fBBoxHierarchyType && NULL != fPicture) { |
| 219 | SkPicture* newPicture = this->createPicture(); |
| 220 | SkCanvas* recorder = newPicture->beginRecording(fPicture->width(), fPicture->height(), |
| 221 | this->recordFlags()); |
| 222 | fPicture->draw(recorder); |
| 223 | newPicture->endRecording(); |
| 224 | fPicture->unref(); |
| 225 | fPicture = newPicture; |
| 226 | } |
| 227 | } |
| 228 | |
| scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 229 | void PictureRenderer::resetState(bool callFinish) { |
| keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 230 | #if SK_SUPPORT_GPU |
| robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 231 | SkGLContextHelper* glContext = this->getGLContext(); |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 232 | if (NULL == glContext) { |
| 233 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 234 | return; |
| 235 | } |
| keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 236 | |
| scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 237 | fGrContext->flush(); |
| 238 | if (callFinish) { |
| 239 | SK_GL(*glContext, Finish()); |
| keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 240 | } |
| keyar@chromium.org | a40c20d | 2012-08-20 15:04:12 +0000 | [diff] [blame] | 241 | #endif |
| keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 244 | void PictureRenderer::purgeTextures() { |
| 245 | SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool(); |
| 246 | |
| 247 | pool->dumpPool(); |
| 248 | |
| 249 | #if SK_SUPPORT_GPU |
| 250 | SkGLContextHelper* glContext = this->getGLContext(); |
| 251 | if (NULL == glContext) { |
| 252 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // resetState should've already done this |
| 257 | fGrContext->flush(); |
| 258 | |
| 259 | fGrContext->purgeAllUnlockedResources(); |
| 260 | #endif |
| 261 | } |
| 262 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 263 | uint32_t PictureRenderer::recordFlags() { |
| junov@chromium.org | 100b1c5 | 2013-01-16 20:12:22 +0000 | [diff] [blame] | 264 | return ((kNone_BBoxHierarchyType == fBBoxHierarchyType) ? 0 : |
| 265 | SkPicture::kOptimizeForClippedPlayback_RecordingFlag) | |
| 266 | SkPicture::kUsePathBoundsForClip_RecordingFlag; |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 269 | /** |
| 270 | * Write the canvas to the specified path. |
| 271 | * @param canvas Must be non-null. Canvas to be written to a file. |
| 272 | * @param path Path for the file to be written. Should have no extension; write() will append |
| 273 | * an appropriate one. Passed in by value so it can be modified. |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 274 | * @param jsonSummaryPtr If not null, add image results to this summary. |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 275 | * @return bool True if the Canvas is written to a file. |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 276 | * |
| 277 | * TODO(epoger): Right now, all canvases must pass through this function in order to be appended |
| 278 | * to the ImageResultsSummary. We need some way to add bitmaps to the ImageResultsSummary |
| 279 | * even if --writePath has not been specified (and thus this function is not called). |
| 280 | * |
| 281 | * One fix would be to pass in these path elements separately, and allow this function to be |
| 282 | * called even if --writePath was not specified... |
| 283 | * const char *outputDir // NULL if we don't want to write image files to disk |
| 284 | * const char *filename // name we use within JSON summary, and as the filename within outputDir |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 285 | */ |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 286 | static bool write(SkCanvas* canvas, const SkString* path, ImageResultsSummary *jsonSummaryPtr) { |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 287 | SkASSERT(canvas != NULL); |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 288 | if (NULL == canvas) { |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 292 | SkASSERT(path != NULL); // TODO(epoger): we want to remove this constraint, as noted above |
| 293 | SkString fullPathname(*path); |
| 294 | fullPathname.append(".png"); |
| 295 | |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 296 | SkBitmap bitmap; |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 297 | SkISize size = canvas->getDeviceSize(); |
| 298 | sk_tools::setup_bitmap(&bitmap, size.width(), size.height()); |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 299 | |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 300 | canvas->readPixels(&bitmap, 0, 0); |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 301 | sk_tools::force_all_opaque(bitmap); |
| 302 | |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 303 | if (NULL != jsonSummaryPtr) { |
| 304 | // EPOGER: This is a hacky way of constructing the filename associated with the |
| 305 | // image checksum; we assume that outputDir is not NULL, and we remove outputDir |
| 306 | // from fullPathname. |
| 307 | // |
| 308 | // EPOGER: what about including the config type within hashFilename? That way, |
| 309 | // we could combine results of different config types without conflicting filenames. |
| 310 | SkString hashFilename; |
| 311 | sk_tools::get_basename(&hashFilename, fullPathname); |
| 312 | jsonSummaryPtr->add(hashFilename.c_str(), bitmap); |
| 313 | } |
| 314 | |
| 315 | return SkImageEncoder::EncodeFile(fullPathname.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 318 | /** |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 319 | * If path is non NULL, append number to it, and call write() to write the |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 320 | * provided canvas to a file. Returns true if path is NULL or if write() succeeds. |
| 321 | */ |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 322 | static bool writeAppendNumber(SkCanvas* canvas, const SkString* path, int number, |
| 323 | ImageResultsSummary *jsonSummaryPtr) { |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 324 | if (NULL == path) { |
| 325 | return true; |
| 326 | } |
| 327 | SkString pathWithNumber(*path); |
| 328 | pathWithNumber.appendf("%i", number); |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 329 | return write(canvas, &pathWithNumber, jsonSummaryPtr); |
| scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 332 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 333 | |
| djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 334 | SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) { |
| 335 | // defer the canvas setup until the render step |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 339 | static SkData* encode_bitmap_to_data(size_t* offset, const SkBitmap& bm) { |
| 340 | SkPixelRef* pr = bm.pixelRef(); |
| 341 | if (pr != NULL) { |
| 342 | SkData* data = pr->refEncodedData(); |
| 343 | if (data != NULL) { |
| 344 | *offset = bm.pixelRefOffset(); |
| 345 | return data; |
| 346 | } |
| 347 | } |
| 348 | *offset = 0; |
| 349 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
| scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 352 | bool RecordPictureRenderer::render(const SkString* path, SkBitmap** out) { |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 353 | SkAutoTUnref<SkPicture> replayer(this->createPicture()); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 354 | SkCanvas* recorder = replayer->beginRecording(this->getViewWidth(), this->getViewHeight(), |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 355 | this->recordFlags()); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 356 | this->scaleToScaleFactor(recorder); |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 357 | fPicture->draw(recorder); |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 358 | replayer->endRecording(); |
| scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 359 | if (path != NULL) { |
| 360 | // Record the new picture as a new SKP with PNG encoded bitmaps. |
| 361 | SkString skpPath(*path); |
| 362 | // ".skp" was removed from 'path' before being passed in here. |
| 363 | skpPath.append(".skp"); |
| 364 | SkFILEWStream stream(skpPath.c_str()); |
| scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 365 | replayer->serialize(&stream, &encode_bitmap_to_data); |
| scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 368 | return false; |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 371 | SkString RecordPictureRenderer::getConfigNameInternal() { |
| 372 | return SkString("record"); |
| 373 | } |
| 374 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 375 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 376 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 377 | bool PipePictureRenderer::render(const SkString* path, SkBitmap** out) { |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 378 | SkASSERT(fCanvas.get() != NULL); |
| 379 | SkASSERT(fPicture != NULL); |
| keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 380 | if (NULL == fCanvas.get() || NULL == fPicture) { |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 381 | return false; |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | PipeController pipeController(fCanvas.get()); |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 385 | SkGPipeWriter writer; |
| 386 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 387 | pipeCanvas->drawPicture(*fPicture); |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 388 | writer.endRecording(); |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 389 | fCanvas->flush(); |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 390 | if (NULL != path) { |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 391 | return write(fCanvas, path, fJsonSummaryPtr); |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 392 | } |
| 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 | } |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 398 | return true; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 401 | SkString PipePictureRenderer::getConfigNameInternal() { |
| 402 | return SkString("pipe"); |
| 403 | } |
| 404 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 405 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 406 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 407 | void SimplePictureRenderer::init(SkPicture* picture) { |
| 408 | INHERITED::init(picture); |
| 409 | this->buildBBoxHierarchy(); |
| 410 | } |
| 411 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 412 | bool SimplePictureRenderer::render(const SkString* path, SkBitmap** out) { |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 413 | SkASSERT(fCanvas.get() != NULL); |
| 414 | SkASSERT(fPicture != NULL); |
| keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 415 | if (NULL == fCanvas.get() || NULL == fPicture) { |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 416 | return false; |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | fCanvas->drawPicture(*fPicture); |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 420 | fCanvas->flush(); |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 421 | if (NULL != path) { |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 422 | return write(fCanvas, path, fJsonSummaryPtr); |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 423 | } |
| skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 424 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 425 | if (NULL != out) { |
| 426 | *out = SkNEW(SkBitmap); |
| 427 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 428 | fCanvas->readPixels(*out, 0, 0); |
| 429 | } |
| 430 | |
| borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 431 | return true; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 434 | SkString SimplePictureRenderer::getConfigNameInternal() { |
| 435 | return SkString("simple"); |
| 436 | } |
| 437 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 438 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 439 | |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 440 | TiledPictureRenderer::TiledPictureRenderer() |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 441 | : fTileWidth(kDefaultTileWidth) |
| rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 442 | , fTileHeight(kDefaultTileHeight) |
| rileya@google.com | a04dc02 | 2012-09-10 19:01:38 +0000 | [diff] [blame] | 443 | , fTileWidthPercentage(0.0) |
| rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 444 | , fTileHeightPercentage(0.0) |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 445 | , fTileMinPowerOf2Width(0) |
| 446 | , fCurrentTileOffset(-1) |
| 447 | , fTilesX(0) |
| 448 | , fTilesY(0) { } |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 449 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 450 | void TiledPictureRenderer::init(SkPicture* pict) { |
| 451 | SkASSERT(pict != NULL); |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 452 | SkASSERT(0 == fTileRects.count()); |
| 453 | if (NULL == pict || fTileRects.count() != 0) { |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 457 | // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not |
| 458 | // used by bench_pictures. |
| 459 | fPicture = pict; |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 460 | fPicture->ref(); |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 461 | this->buildBBoxHierarchy(); |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 462 | |
| 463 | if (fTileWidthPercentage > 0) { |
| robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 464 | fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100)); |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 465 | } |
| 466 | if (fTileHeightPercentage > 0) { |
| robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 467 | fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100)); |
| keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 470 | if (fTileMinPowerOf2Width > 0) { |
| 471 | this->setupPowerOf2Tiles(); |
| 472 | } else { |
| 473 | this->setupTiles(); |
| 474 | } |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 475 | fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight)); |
| 476 | // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the |
| 477 | // first call to drawCurrentTile. |
| 478 | fCurrentTileOffset = -1; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 481 | void TiledPictureRenderer::end() { |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 482 | fTileRects.reset(); |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 483 | this->INHERITED::end(); |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 486 | void TiledPictureRenderer::setupTiles() { |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 487 | // Only use enough tiles to cover the viewport |
| 488 | const int width = this->getViewWidth(); |
| 489 | const int height = this->getViewHeight(); |
| 490 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 491 | fTilesX = fTilesY = 0; |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 492 | 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] | 493 | fTilesY++; |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 494 | 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] | 495 | if (0 == tile_y_start) { |
| 496 | // Only count tiles in the X direction on the first pass. |
| 497 | fTilesX++; |
| 498 | } |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 499 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 500 | SkIntToScalar(tile_y_start), |
| 501 | SkIntToScalar(fTileWidth), |
| 502 | SkIntToScalar(fTileHeight)); |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 507 | bool TiledPictureRenderer::tileDimensions(int &x, int &y) { |
| 508 | if (fTileRects.count() == 0 || NULL == fPicture) { |
| 509 | return false; |
| 510 | } |
| 511 | x = fTilesX; |
| 512 | y = fTilesY; |
| 513 | return true; |
| 514 | } |
| 515 | |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 516 | // The goal of the powers of two tiles is to minimize the amount of wasted tile |
| 517 | // space in the width-wise direction and then minimize the number of tiles. The |
| 518 | // constraints are that every tile must have a pixel width that is a power of |
| 519 | // two and also be of some minimal width (that is also a power of two). |
| 520 | // |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 521 | // 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] | 522 | // multiple of the minimal width. The binary representation of this rounded |
| 523 | // value gives us the tiles we need: a bit of value one means we need a tile of |
| 524 | // that size. |
| 525 | void TiledPictureRenderer::setupPowerOf2Tiles() { |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 526 | // Only use enough tiles to cover the viewport |
| 527 | const int width = this->getViewWidth(); |
| 528 | const int height = this->getViewHeight(); |
| 529 | |
| 530 | int rounded_value = width; |
| 531 | if (width % fTileMinPowerOf2Width != 0) { |
| 532 | rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width; |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| reed@google.com | e15b2f5 | 2013-12-18 04:59:26 +0000 | [diff] [blame] | 535 | int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width))); |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 536 | int largest_possible_tile_size = 1 << num_bits; |
| 537 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 538 | fTilesX = fTilesY = 0; |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 539 | // The tile height is constant for a particular picture. |
| scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 540 | 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] | 541 | fTilesY++; |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 542 | int tile_x_start = 0; |
| 543 | int current_width = largest_possible_tile_size; |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 544 | // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough |
| 545 | // to draw each tile. |
| 546 | fTileWidth = current_width; |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 547 | |
| 548 | while (current_width >= fTileMinPowerOf2Width) { |
| 549 | // It is very important this is a bitwise AND. |
| 550 | if (current_width & rounded_value) { |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 551 | if (0 == tile_y_start) { |
| 552 | // Only count tiles in the X direction on the first pass. |
| 553 | fTilesX++; |
| 554 | } |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 555 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 556 | SkIntToScalar(tile_y_start), |
| 557 | SkIntToScalar(current_width), |
| 558 | SkIntToScalar(fTileHeight)); |
| keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 559 | tile_x_start += current_width; |
| 560 | } |
| 561 | |
| 562 | current_width >>= 1; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 567 | /** |
| 568 | * Draw the specified playback to the canvas translated to rectangle provided, so that this mini |
| 569 | * canvas represents the rectangle's portion of the overall picture. |
| 570 | * Saves and restores so that the initial clip and matrix return to their state before this function |
| 571 | * is called. |
| 572 | */ |
| 573 | template<class T> |
| 574 | static void DrawTileToCanvas(SkCanvas* canvas, const SkRect& tileRect, T* playback) { |
| 575 | int saveCount = canvas->save(); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 576 | // Translate so that we draw the correct portion of the picture. |
| 577 | // Perform a postTranslate so that the scaleFactor does not interfere with the positioning. |
| 578 | SkMatrix mat(canvas->getTotalMatrix()); |
| 579 | mat.postTranslate(-tileRect.fLeft, -tileRect.fTop); |
| 580 | canvas->setMatrix(mat); |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 581 | playback->draw(canvas); |
| 582 | canvas->restoreToCount(saveCount); |
| 583 | canvas->flush(); |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 586 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 587 | |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 588 | /** |
| 589 | * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap. |
| 590 | * If the src bitmap is too large to fit within the dst bitmap after the x and y |
| 591 | * offsets have been applied, any excess will be ignored (so only the top-left portion of the |
| 592 | * src bitmap will be copied). |
| 593 | * |
| 594 | * @param src source bitmap |
| 595 | * @param dst destination bitmap |
| 596 | * @param xOffset x-offset within destination bitmap |
| 597 | * @param yOffset y-offset within destination bitmap |
| 598 | */ |
| 599 | static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst, |
| 600 | int xOffset, int yOffset) { |
| 601 | for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) { |
| 602 | for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) { |
| 603 | *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y); |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 608 | bool TiledPictureRenderer::nextTile(int &i, int &j) { |
| 609 | if (++fCurrentTileOffset < fTileRects.count()) { |
| 610 | i = fCurrentTileOffset % fTilesX; |
| 611 | j = fCurrentTileOffset / fTilesX; |
| 612 | return true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | void TiledPictureRenderer::drawCurrentTile() { |
| 618 | SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count()); |
| 619 | DrawTileToCanvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture); |
| 620 | } |
| 621 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 622 | bool TiledPictureRenderer::render(const SkString* path, SkBitmap** out) { |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 623 | SkASSERT(fPicture != NULL); |
| 624 | if (NULL == fPicture) { |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 625 | return false; |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 628 | SkBitmap bitmap; |
| 629 | if (out){ |
| 630 | *out = SkNEW(SkBitmap); |
| 631 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 632 | setup_bitmap(&bitmap, fTileWidth, fTileHeight); |
| 633 | } |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 634 | bool success = true; |
| 635 | for (int i = 0; i < fTileRects.count(); ++i) { |
| scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 636 | DrawTileToCanvas(fCanvas, fTileRects[i], fPicture); |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 637 | if (NULL != path) { |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 638 | success &= writeAppendNumber(fCanvas, path, i, fJsonSummaryPtr); |
| scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 639 | } |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 640 | if (NULL != out) { |
| 641 | if (fCanvas->readPixels(&bitmap, 0, 0)) { |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 642 | // Add this tile to the entire bitmap. |
| 643 | bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()), |
| 644 | SkScalarFloorToInt(fTileRects[i].top())); |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 645 | } else { |
| 646 | success = false; |
| 647 | } |
| 648 | } |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 649 | } |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 650 | return success; |
| keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 653 | SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) { |
| 654 | SkCanvas* canvas = this->INHERITED::setupCanvas(width, height); |
| 655 | SkASSERT(fPicture != NULL); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 656 | // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This |
| 657 | // is mostly important for tiles on the right and bottom edges as they may go over this area and |
| 658 | // the picture may have some commands that draw outside of this area and so should not actually |
| 659 | // be written. |
| 660 | // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set |
| 661 | // by INHERITED::setupCanvas. |
| 662 | SkRegion clipRegion; |
| 663 | clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight()); |
| 664 | canvas->clipRegion(clipRegion); |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 665 | return canvas; |
| 666 | } |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 667 | |
| 668 | SkString TiledPictureRenderer::getConfigNameInternal() { |
| 669 | SkString name; |
| 670 | if (fTileMinPowerOf2Width > 0) { |
| 671 | name.append("pow2tile_"); |
| 672 | name.appendf("%i", fTileMinPowerOf2Width); |
| 673 | } else { |
| 674 | name.append("tile_"); |
| 675 | if (fTileWidthPercentage > 0) { |
| 676 | name.appendf("%.f%%", fTileWidthPercentage); |
| 677 | } else { |
| 678 | name.appendf("%i", fTileWidth); |
| 679 | } |
| 680 | } |
| 681 | name.append("x"); |
| 682 | if (fTileHeightPercentage > 0) { |
| 683 | name.appendf("%.f%%", fTileHeightPercentage); |
| 684 | } else { |
| 685 | name.appendf("%i", fTileHeight); |
| 686 | } |
| 687 | return name; |
| 688 | } |
| 689 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 690 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 691 | |
| 692 | // Holds all of the information needed to draw a set of tiles. |
| 693 | class CloneData : public SkRunnable { |
| 694 | |
| 695 | public: |
| 696 | CloneData(SkPicture* clone, SkCanvas* canvas, SkTDArray<SkRect>& rects, int start, int end, |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 697 | SkRunnable* done, ImageResultsSummary* jsonSummaryPtr) |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 698 | : fClone(clone) |
| 699 | , fCanvas(canvas) |
| 700 | , fPath(NULL) |
| 701 | , fRects(rects) |
| 702 | , fStart(start) |
| 703 | , fEnd(end) |
| 704 | , fSuccess(NULL) |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 705 | , fDone(done) |
| 706 | , fJsonSummaryPtr(jsonSummaryPtr) { |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 707 | SkASSERT(fDone != NULL); |
| 708 | } |
| 709 | |
| 710 | virtual void run() SK_OVERRIDE { |
| 711 | SkGraphics::SetTLSFontCacheLimit(1024 * 1024); |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 712 | |
| 713 | SkBitmap bitmap; |
| 714 | if (fBitmap != NULL) { |
| 715 | // All tiles are the same size. |
| jvanverth@google.com | 9c4e5ac | 2013-01-07 18:41:28 +0000 | [diff] [blame] | 716 | setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScalarFloorToInt(fRects[0].height())); |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 717 | } |
| skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 718 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 719 | for (int i = fStart; i < fEnd; i++) { |
| 720 | DrawTileToCanvas(fCanvas, fRects[i], fClone); |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 721 | if ((fPath != NULL) && !writeAppendNumber(fCanvas, fPath, i, fJsonSummaryPtr) |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 722 | && fSuccess != NULL) { |
| 723 | *fSuccess = false; |
| 724 | // If one tile fails to write to a file, do not continue drawing the rest. |
| 725 | break; |
| 726 | } |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 727 | if (fBitmap != NULL) { |
| 728 | if (fCanvas->readPixels(&bitmap, 0, 0)) { |
| 729 | SkAutoLockPixels alp(*fBitmap); |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 730 | bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRects[i].left()), |
| 731 | SkScalarFloorToInt(fRects[i].top())); |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 732 | } else { |
| 733 | *fSuccess = false; |
| 734 | // If one tile fails to read pixels, do not continue drawing the rest. |
| 735 | break; |
| 736 | } |
| 737 | } |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 738 | } |
| 739 | fDone->run(); |
| 740 | } |
| 741 | |
| 742 | void setPathAndSuccess(const SkString* path, bool* success) { |
| 743 | fPath = path; |
| 744 | fSuccess = success; |
| 745 | } |
| 746 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 747 | void setBitmap(SkBitmap* bitmap) { |
| 748 | fBitmap = bitmap; |
| 749 | } |
| 750 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 751 | private: |
| 752 | // All pointers unowned. |
| 753 | SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which |
| 754 | // is threadsafe. |
| 755 | SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile. |
| 756 | const SkString* fPath; // If non-null, path to write the result to as a PNG. |
| 757 | SkTDArray<SkRect>& fRects; // All tiles of the picture. |
| 758 | const int fStart; // Range of tiles drawn by this thread. |
| 759 | const int fEnd; |
| 760 | bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads, |
| 761 | // and only set to false upon failure to write to a PNG. |
| 762 | SkRunnable* fDone; |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 763 | SkBitmap* fBitmap; |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 764 | ImageResultsSummary* fJsonSummaryPtr; |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount) |
| 768 | : fNumThreads(threadCount) |
| 769 | , fThreadPool(threadCount) |
| 770 | , fCountdown(threadCount) { |
| 771 | // Only need to create fNumThreads - 1 clones, since one thread will use the base |
| 772 | // picture. |
| 773 | fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1); |
| 774 | fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads); |
| 775 | } |
| 776 | |
| 777 | void MultiCorePictureRenderer::init(SkPicture *pict) { |
| 778 | // Set fPicture and the tiles. |
| 779 | this->INHERITED::init(pict); |
| 780 | for (int i = 0; i < fNumThreads; ++i) { |
| 781 | *fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight()); |
| 782 | } |
| 783 | // Only need to create fNumThreads - 1 clones, since one thread will use the base picture. |
| 784 | fPicture->clone(fPictureClones, fNumThreads - 1); |
| 785 | // Populate each thread with the appropriate data. |
| 786 | // Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all. |
| 787 | const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads; |
| 788 | |
| 789 | for (int i = 0; i < fNumThreads; i++) { |
| 790 | SkPicture* pic; |
| 791 | if (i == fNumThreads-1) { |
| 792 | // The last set will use the original SkPicture. |
| 793 | pic = fPicture; |
| 794 | } else { |
| 795 | pic = &fPictureClones[i]; |
| 796 | } |
| 797 | const int start = i * chunkSize; |
| 798 | const int end = SkMin32(start + chunkSize, fTileRects.count()); |
| 799 | fCloneData[i] = SkNEW_ARGS(CloneData, |
| commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 800 | (pic, fCanvasPool[i], fTileRects, start, end, &fCountdown, |
| 801 | fJsonSummaryPtr)); |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 805 | bool MultiCorePictureRenderer::render(const SkString *path, SkBitmap** out) { |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 806 | bool success = true; |
| 807 | if (path != NULL) { |
| 808 | for (int i = 0; i < fNumThreads-1; i++) { |
| 809 | fCloneData[i]->setPathAndSuccess(path, &success); |
| 810 | } |
| 811 | } |
| 812 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 813 | if (NULL != out) { |
| 814 | *out = SkNEW(SkBitmap); |
| 815 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 816 | for (int i = 0; i < fNumThreads; i++) { |
| 817 | fCloneData[i]->setBitmap(*out); |
| 818 | } |
| 819 | } else { |
| 820 | for (int i = 0; i < fNumThreads; i++) { |
| 821 | fCloneData[i]->setBitmap(NULL); |
| 822 | } |
| 823 | } |
| 824 | |
| scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 825 | fCountdown.reset(fNumThreads); |
| 826 | for (int i = 0; i < fNumThreads; i++) { |
| 827 | fThreadPool.add(fCloneData[i]); |
| 828 | } |
| 829 | fCountdown.wait(); |
| 830 | |
| 831 | return success; |
| 832 | } |
| 833 | |
| 834 | void MultiCorePictureRenderer::end() { |
| 835 | for (int i = 0; i < fNumThreads - 1; i++) { |
| 836 | SkDELETE(fCloneData[i]); |
| 837 | fCloneData[i] = NULL; |
| 838 | } |
| 839 | |
| 840 | fCanvasPool.unrefAll(); |
| 841 | |
| 842 | this->INHERITED::end(); |
| 843 | } |
| 844 | |
| 845 | MultiCorePictureRenderer::~MultiCorePictureRenderer() { |
| 846 | // Each individual CloneData was deleted in end. |
| 847 | SkDELETE_ARRAY(fCloneData); |
| 848 | SkDELETE_ARRAY(fPictureClones); |
| 849 | } |
| scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 850 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 851 | SkString MultiCorePictureRenderer::getConfigNameInternal() { |
| 852 | SkString name = this->INHERITED::getConfigNameInternal(); |
| 853 | name.appendf("_multi_%i_threads", fNumThreads); |
| 854 | return name; |
| 855 | } |
| 856 | |
| scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 857 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 858 | |
| 859 | void PlaybackCreationRenderer::setup() { |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 860 | fReplayer.reset(this->createPicture()); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 861 | SkCanvas* recorder = fReplayer->beginRecording(this->getViewWidth(), this->getViewHeight(), |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 862 | this->recordFlags()); |
| scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 863 | this->scaleToScaleFactor(recorder); |
| scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 864 | fPicture->draw(recorder); |
| 865 | } |
| 866 | |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 867 | bool PlaybackCreationRenderer::render(const SkString*, SkBitmap** out) { |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 868 | fReplayer->endRecording(); |
| scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 869 | // Since this class does not actually render, return false. |
| 870 | return false; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 873 | SkString PlaybackCreationRenderer::getConfigNameInternal() { |
| 874 | return SkString("playback_creation"); |
| 875 | } |
| 876 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 877 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 878 | // SkPicture variants for each BBoxHierarchy type |
| 879 | |
| 880 | class RTreePicture : public SkPicture { |
| 881 | public: |
| 882 | virtual SkBBoxHierarchy* createBBoxHierarchy() const SK_OVERRIDE{ |
| 883 | static const int kRTreeMinChildren = 6; |
| 884 | static const int kRTreeMaxChildren = 11; |
| 885 | SkScalar aspectRatio = SkScalarDiv(SkIntToScalar(fWidth), |
| 886 | SkIntToScalar(fHeight)); |
| sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 887 | bool sortDraws = false; |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 888 | return SkRTree::Create(kRTreeMinChildren, kRTreeMaxChildren, |
| sglez@google.com | 8c90212 | 2013-08-30 17:27:47 +0000 | [diff] [blame] | 889 | aspectRatio, sortDraws); |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 890 | } |
| 891 | }; |
| 892 | |
| 893 | SkPicture* PictureRenderer::createPicture() { |
| 894 | switch (fBBoxHierarchyType) { |
| 895 | case kNone_BBoxHierarchyType: |
| 896 | return SkNEW(SkPicture); |
| 897 | case kRTree_BBoxHierarchyType: |
| 898 | return SkNEW(RTreePicture); |
| junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 899 | case kTileGrid_BBoxHierarchyType: |
| junov@chromium.org | 29b19e5 | 2013-02-27 18:35:16 +0000 | [diff] [blame] | 900 | return SkNEW_ARGS(SkTileGridPicture, (fPicture->width(), |
| 901 | fPicture->height(), fGridInfo)); |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 902 | } |
| 903 | SkASSERT(0); // invalid bbhType |
| 904 | return NULL; |
| keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 905 | } |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 906 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 907 | /////////////////////////////////////////////////////////////////////////////// |
| 908 | |
| 909 | class GatherRenderer : public PictureRenderer { |
| 910 | public: |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 911 | virtual bool render(const SkString* path, SkBitmap** out = NULL) |
| 912 | SK_OVERRIDE { |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 913 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()), |
| 914 | SkIntToScalar(fPicture->height())); |
| 915 | SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds); |
| 916 | SkSafeUnref(data); |
| skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 917 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 918 | return NULL == path; // we don't have anything to write |
| 919 | } |
| skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 920 | |
| reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 921 | private: |
| 922 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 923 | return SkString("gather_pixelrefs"); |
| 924 | } |
| 925 | }; |
| 926 | |
| 927 | PictureRenderer* CreateGatherPixelRefsRenderer() { |
| 928 | return SkNEW(GatherRenderer); |
| 929 | } |
| skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 930 | |
| reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 931 | /////////////////////////////////////////////////////////////////////////////// |
| 932 | |
| 933 | class PictureCloneRenderer : public PictureRenderer { |
| 934 | public: |
| edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 935 | virtual bool render(const SkString* path, SkBitmap** out = NULL) |
| 936 | SK_OVERRIDE { |
| reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 937 | for (int i = 0; i < 100; ++i) { |
| 938 | SkPicture* clone = fPicture->clone(); |
| 939 | SkSafeUnref(clone); |
| 940 | } |
| skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 941 | |
| reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 942 | return NULL == path; // we don't have anything to write |
| 943 | } |
| skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 944 | |
| reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 945 | private: |
| 946 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 947 | return SkString("picture_clone"); |
| 948 | } |
| 949 | }; |
| 950 | |
| 951 | PictureRenderer* CreatePictureCloneRenderer() { |
| 952 | return SkNEW(PictureCloneRenderer); |
| 953 | } |
| 954 | |
| junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 955 | } // namespace sk_tools |