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