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