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