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" |
commit-bot@chromium.org | d393b17 | 2014-04-16 16:02:10 +0000 | [diff] [blame] | 31 | #include "SkRTreePicture.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) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 277 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
| 278 | SkPictureRecorder recorder; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 279 | SkCanvas* canvas = recorder.beginRecording(fPicture->width(), fPicture->height(), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 280 | factory.get(), |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 281 | this->recordFlags()); |
| 282 | fPicture->draw(canvas); |
| 283 | fPicture.reset(recorder.endRecording()); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
scroggo@google.com | 08085f8 | 2013-01-28 20:40:24 +0000 | [diff] [blame] | 287 | void PictureRenderer::resetState(bool callFinish) { |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 288 | #if SK_SUPPORT_GPU |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame] | 289 | SkGLContextHelper* glContext = this->getGLContext(); |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 290 | if (NULL == glContext) { |
| 291 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 292 | return; |
| 293 | } |
keyar@chromium.org | 28136b3 | 2012-08-20 15:04:15 +0000 | [diff] [blame] | 294 | |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 295 | fGrContext->flush(); |
commit-bot@chromium.org | 51c040e | 2014-03-11 22:58:00 +0000 | [diff] [blame] | 296 | glContext->swapBuffers(); |
scroggo@google.com | 0556ea0 | 2013-02-08 19:38:21 +0000 | [diff] [blame] | 297 | if (callFinish) { |
| 298 | SK_GL(*glContext, Finish()); |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 299 | } |
keyar@chromium.org | a40c20d | 2012-08-20 15:04:12 +0000 | [diff] [blame] | 300 | #endif |
keyar@chromium.org | 77a5522 | 2012-08-20 15:03:47 +0000 | [diff] [blame] | 301 | } |
| 302 | |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 303 | void PictureRenderer::purgeTextures() { |
| 304 | SkDiscardableMemoryPool* pool = SkGetGlobalDiscardableMemoryPool(); |
| 305 | |
| 306 | pool->dumpPool(); |
| 307 | |
| 308 | #if SK_SUPPORT_GPU |
| 309 | SkGLContextHelper* glContext = this->getGLContext(); |
| 310 | if (NULL == glContext) { |
| 311 | SkASSERT(kBitmap_DeviceType == fDeviceType); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | // resetState should've already done this |
| 316 | fGrContext->flush(); |
| 317 | |
| 318 | fGrContext->purgeAllUnlockedResources(); |
| 319 | #endif |
| 320 | } |
| 321 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 322 | uint32_t PictureRenderer::recordFlags() { |
skia.committer@gmail.com | 667b98d | 2014-04-17 03:05:10 +0000 | [diff] [blame] | 323 | return (kNone_BBoxHierarchyType == fBBoxHierarchyType) |
commit-bot@chromium.org | d393b17 | 2014-04-16 16:02:10 +0000 | [diff] [blame] | 324 | ? 0 |
| 325 | : SkPicture::kUsePathBoundsForClip_RecordingFlag; |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 326 | } |
| 327 | |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 328 | /** |
| 329 | * Write the canvas to the specified path. |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 330 | * |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 331 | * @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] | 332 | * @param outputDir If nonempty, write the binary image to a file within this directory. |
| 333 | * @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] | 334 | * @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] | 335 | * @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] | 336 | * @param tileNumberPtr If not null, which tile number this image contains. |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 337 | * @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] | 338 | * |
| 339 | * TODO(epoger): Right now, all canvases must pass through this function in order to be appended |
| 340 | * to the ImageResultsSummary. We need some way to add bitmaps to the ImageResultsSummary |
| 341 | * even if --writePath has not been specified (and thus this function is not called). |
| 342 | * |
| 343 | * One fix would be to pass in these path elements separately, and allow this function to be |
| 344 | * called even if --writePath was not specified... |
| 345 | * const char *outputDir // NULL if we don't want to write image files to disk |
| 346 | * 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] | 347 | * |
| 348 | * 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] | 349 | */ |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 350 | static bool write(SkCanvas* canvas, const SkString& outputDir, const SkString& inputFilename, |
| 351 | ImageResultsSummary *jsonSummaryPtr, bool useChecksumBasedFilenames, |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 352 | const int* tileNumberPtr=NULL) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 353 | SkASSERT(canvas != NULL); |
scroggo@google.com | b6e806b | 2012-10-03 17:32:33 +0000 | [diff] [blame] | 354 | if (NULL == canvas) { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | |
| 358 | SkBitmap bitmap; |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 359 | SkISize size = canvas->getDeviceSize(); |
| 360 | sk_tools::setup_bitmap(&bitmap, size.width(), size.height()); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 361 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 362 | // Make sure we only compute the bitmap hash once (at most). |
| 363 | uint64_t hash; |
| 364 | bool generatedHash = false; |
| 365 | |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 366 | canvas->readPixels(&bitmap, 0, 0); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 367 | sk_tools::force_all_opaque(bitmap); |
| 368 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 369 | SkString escapedInputFilename(inputFilename); |
| 370 | replace_char(&escapedInputFilename, '.', '_'); |
| 371 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 372 | // TODO(epoger): what about including the config type within outputFilename? That way, |
| 373 | // 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] | 374 | SkString outputFilename; |
| 375 | const char *outputSubdirPtr = NULL; |
| 376 | if (useChecksumBasedFilenames) { |
commit-bot@chromium.org | b470c21 | 2014-03-19 22:58:52 +0000 | [diff] [blame] | 377 | SkASSERT(!generatedHash); |
| 378 | SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash)); |
| 379 | generatedHash = true; |
| 380 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 381 | outputSubdirPtr = escapedInputFilename.c_str(); |
| 382 | outputFilename.set(kJsonValue_Image_ChecksumAlgorithm_Bitmap64bitMD5); |
| 383 | outputFilename.append("_"); |
| 384 | outputFilename.appendU64(hash); |
| 385 | } else { |
| 386 | outputFilename.set(escapedInputFilename); |
| 387 | if (NULL != tileNumberPtr) { |
| 388 | outputFilename.append("-tile"); |
| 389 | outputFilename.appendS32(*tileNumberPtr); |
| 390 | } |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 391 | } |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 392 | outputFilename.append(".png"); |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 393 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 394 | if (NULL != jsonSummaryPtr) { |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 395 | if (!generatedHash) { |
| 396 | SkAssertResult(SkBitmapHasher::ComputeDigest(bitmap, &hash)); |
| 397 | generatedHash = true; |
| 398 | } |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 399 | |
| 400 | SkString outputRelativePath; |
| 401 | if (outputSubdirPtr) { |
| 402 | outputRelativePath.set(outputSubdirPtr); |
| 403 | outputRelativePath.append("/"); // always use "/", even on Windows |
| 404 | outputRelativePath.append(outputFilename); |
| 405 | } else { |
| 406 | outputRelativePath.set(outputFilename); |
| 407 | } |
| 408 | |
| 409 | jsonSummaryPtr->add(inputFilename.c_str(), outputRelativePath.c_str(), |
| 410 | hash, tileNumberPtr); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | SkASSERT(!outputDir.isEmpty()); // TODO(epoger): we want to remove this constraint, |
| 414 | // as noted above |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 415 | SkString dirPath; |
| 416 | if (outputSubdirPtr) { |
| 417 | dirPath = SkOSPath::SkPathJoin(outputDir.c_str(), outputSubdirPtr); |
| 418 | sk_mkdir(dirPath.c_str()); |
| 419 | } else { |
| 420 | dirPath.set(outputDir); |
| 421 | } |
| 422 | SkString fullPath = SkOSPath::SkPathJoin(dirPath.c_str(), outputFilename.c_str()); |
| 423 | return SkImageEncoder::EncodeFile(fullPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 424 | } |
| 425 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 426 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 427 | |
djsollen@google.com | fd9720c | 2012-11-06 16:54:40 +0000 | [diff] [blame] | 428 | SkCanvas* RecordPictureRenderer::setupCanvas(int width, int height) { |
| 429 | // defer the canvas setup until the render step |
| 430 | return NULL; |
| 431 | } |
| 432 | |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 433 | // the size_t* parameter is deprecated, so we ignore it |
| 434 | static SkData* encode_bitmap_to_data(size_t*, const SkBitmap& bm) { |
scroggo@google.com | 1b1bcc3 | 2013-05-21 20:31:23 +0000 | [diff] [blame] | 435 | return SkImageEncoder::EncodeData(bm, SkImageEncoder::kPNG_Type, 100); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 436 | } |
| 437 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 438 | bool RecordPictureRenderer::render(SkBitmap** out) { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 439 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
| 440 | SkPictureRecorder recorder; |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 441 | SkCanvas* canvas = recorder.beginRecording(this->getViewWidth(), this->getViewHeight(), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 442 | factory.get(), |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 443 | this->recordFlags()); |
| 444 | this->scaleToScaleFactor(canvas); |
| 445 | fPicture->draw(canvas); |
| 446 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 447 | if (!fOutputDir.isEmpty()) { |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 448 | // 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] | 449 | SkString skpPath = SkOSPath::SkPathJoin(fOutputDir.c_str(), fInputFilename.c_str()); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 450 | SkFILEWStream stream(skpPath.c_str()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 451 | picture->serialize(&stream, &encode_bitmap_to_data); |
scroggo@google.com | a9e3a36 | 2012-11-07 17:52:48 +0000 | [diff] [blame] | 452 | return true; |
| 453 | } |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 454 | return false; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 455 | } |
| 456 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 457 | SkString RecordPictureRenderer::getConfigNameInternal() { |
| 458 | return SkString("record"); |
| 459 | } |
| 460 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 461 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 462 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 463 | bool PipePictureRenderer::render(SkBitmap** out) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 464 | SkASSERT(fCanvas.get() != NULL); |
| 465 | SkASSERT(fPicture != NULL); |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 466 | if (NULL == fCanvas.get() || NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 467 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | PipeController pipeController(fCanvas.get()); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 471 | SkGPipeWriter writer; |
| 472 | SkCanvas* pipeCanvas = writer.startRecording(&pipeController); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 473 | pipeCanvas->drawPicture(*fPicture); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 474 | writer.endRecording(); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 475 | fCanvas->flush(); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 476 | if (!fOutputDir.isEmpty()) { |
| 477 | return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 478 | fUseChecksumBasedFilenames); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 479 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 480 | if (NULL != out) { |
| 481 | *out = SkNEW(SkBitmap); |
| 482 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 483 | fCanvas->readPixels(*out, 0, 0); |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 484 | } |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 485 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 486 | } |
| 487 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 488 | SkString PipePictureRenderer::getConfigNameInternal() { |
| 489 | return SkString("pipe"); |
| 490 | } |
| 491 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 492 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 493 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 494 | void SimplePictureRenderer::init(SkPicture* picture, const SkString* outputDir, |
| 495 | const SkString* inputFilename, bool useChecksumBasedFilenames) { |
| 496 | INHERITED::init(picture, outputDir, inputFilename, useChecksumBasedFilenames); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 497 | this->buildBBoxHierarchy(); |
| 498 | } |
| 499 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 500 | bool SimplePictureRenderer::render(SkBitmap** out) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 501 | SkASSERT(fCanvas.get() != NULL); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 502 | SkASSERT(NULL != fPicture); |
keyar@chromium.org | 78a35c5 | 2012-08-20 15:03:44 +0000 | [diff] [blame] | 503 | if (NULL == fCanvas.get() || NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 504 | return false; |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | fCanvas->drawPicture(*fPicture); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 508 | fCanvas->flush(); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 509 | if (!fOutputDir.isEmpty()) { |
| 510 | return write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 511 | fUseChecksumBasedFilenames); |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 512 | } |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 513 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 514 | if (NULL != out) { |
| 515 | *out = SkNEW(SkBitmap); |
| 516 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 517 | fCanvas->readPixels(*out, 0, 0); |
| 518 | } |
| 519 | |
borenet@google.com | 070d354 | 2012-10-26 13:26:55 +0000 | [diff] [blame] | 520 | return true; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 521 | } |
| 522 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 523 | SkString SimplePictureRenderer::getConfigNameInternal() { |
| 524 | return SkString("simple"); |
| 525 | } |
| 526 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 527 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 528 | |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 529 | TiledPictureRenderer::TiledPictureRenderer() |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 530 | : fTileWidth(kDefaultTileWidth) |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 531 | , fTileHeight(kDefaultTileHeight) |
rileya@google.com | a04dc02 | 2012-09-10 19:01:38 +0000 | [diff] [blame] | 532 | , fTileWidthPercentage(0.0) |
rileya@google.com | b947b91 | 2012-08-29 17:35:07 +0000 | [diff] [blame] | 533 | , fTileHeightPercentage(0.0) |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 534 | , fTileMinPowerOf2Width(0) |
| 535 | , fCurrentTileOffset(-1) |
| 536 | , fTilesX(0) |
| 537 | , fTilesY(0) { } |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 538 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 539 | void TiledPictureRenderer::init(SkPicture* pict, const SkString* outputDir, |
| 540 | const SkString* inputFilename, bool useChecksumBasedFilenames) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 541 | SkASSERT(NULL != pict); |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 542 | SkASSERT(0 == fTileRects.count()); |
| 543 | if (NULL == pict || fTileRects.count() != 0) { |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 547 | // Do not call INHERITED::init(), which would create a (potentially large) canvas which is not |
| 548 | // used by bench_pictures. |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 549 | fPicture.reset(pict)->ref(); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 550 | this->CopyString(&fOutputDir, outputDir); |
| 551 | this->CopyString(&fInputFilename, inputFilename); |
| 552 | fUseChecksumBasedFilenames = useChecksumBasedFilenames; |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 553 | this->buildBBoxHierarchy(); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 554 | |
| 555 | if (fTileWidthPercentage > 0) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 556 | fTileWidth = sk_float_ceil2int(float(fTileWidthPercentage * fPicture->width() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 557 | } |
| 558 | if (fTileHeightPercentage > 0) { |
robertphillips@google.com | 5d8d186 | 2012-08-15 14:36:41 +0000 | [diff] [blame] | 559 | fTileHeight = sk_float_ceil2int(float(fTileHeightPercentage * fPicture->height() / 100)); |
keyar@chromium.org | cc6e5ef | 2012-07-27 20:09:26 +0000 | [diff] [blame] | 560 | } |
| 561 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 562 | if (fTileMinPowerOf2Width > 0) { |
| 563 | this->setupPowerOf2Tiles(); |
| 564 | } else { |
| 565 | this->setupTiles(); |
| 566 | } |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 567 | fCanvas.reset(this->setupCanvas(fTileWidth, fTileHeight)); |
| 568 | // Initialize to -1 so that the first call to nextTile will set this up to draw tile 0 on the |
| 569 | // first call to drawCurrentTile. |
| 570 | fCurrentTileOffset = -1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 571 | } |
| 572 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 573 | void TiledPictureRenderer::end() { |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 574 | fTileRects.reset(); |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 575 | this->INHERITED::end(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 576 | } |
| 577 | |
keyar@chromium.org | 9d696c0 | 2012-08-07 17:11:33 +0000 | [diff] [blame] | 578 | void TiledPictureRenderer::setupTiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 579 | // Only use enough tiles to cover the viewport |
| 580 | const int width = this->getViewWidth(); |
| 581 | const int height = this->getViewHeight(); |
| 582 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 583 | fTilesX = fTilesY = 0; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 584 | 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] | 585 | fTilesY++; |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 586 | 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] | 587 | if (0 == tile_y_start) { |
| 588 | // Only count tiles in the X direction on the first pass. |
| 589 | fTilesX++; |
| 590 | } |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 591 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 592 | SkIntToScalar(tile_y_start), |
| 593 | SkIntToScalar(fTileWidth), |
| 594 | SkIntToScalar(fTileHeight)); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 599 | bool TiledPictureRenderer::tileDimensions(int &x, int &y) { |
| 600 | if (fTileRects.count() == 0 || NULL == fPicture) { |
| 601 | return false; |
| 602 | } |
| 603 | x = fTilesX; |
| 604 | y = fTilesY; |
| 605 | return true; |
| 606 | } |
| 607 | |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 608 | // The goal of the powers of two tiles is to minimize the amount of wasted tile |
| 609 | // space in the width-wise direction and then minimize the number of tiles. The |
| 610 | // constraints are that every tile must have a pixel width that is a power of |
| 611 | // two and also be of some minimal width (that is also a power of two). |
| 612 | // |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 613 | // 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] | 614 | // multiple of the minimal width. The binary representation of this rounded |
| 615 | // value gives us the tiles we need: a bit of value one means we need a tile of |
| 616 | // that size. |
| 617 | void TiledPictureRenderer::setupPowerOf2Tiles() { |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 618 | // Only use enough tiles to cover the viewport |
| 619 | const int width = this->getViewWidth(); |
| 620 | const int height = this->getViewHeight(); |
| 621 | |
| 622 | int rounded_value = width; |
| 623 | if (width % fTileMinPowerOf2Width != 0) { |
| 624 | rounded_value = width - (width % fTileMinPowerOf2Width) + fTileMinPowerOf2Width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
reed@google.com | e15b2f5 | 2013-12-18 04:59:26 +0000 | [diff] [blame] | 627 | int num_bits = SkScalarCeilToInt(scalar_log2(SkIntToScalar(width))); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 628 | int largest_possible_tile_size = 1 << num_bits; |
| 629 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 630 | fTilesX = fTilesY = 0; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 631 | // The tile height is constant for a particular picture. |
scroggo@google.com | c0d5e54 | 2012-12-13 21:40:48 +0000 | [diff] [blame] | 632 | 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] | 633 | fTilesY++; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 634 | int tile_x_start = 0; |
| 635 | int current_width = largest_possible_tile_size; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 636 | // Set fTileWidth to be the width of the widest tile, so that each canvas is large enough |
| 637 | // to draw each tile. |
| 638 | fTileWidth = current_width; |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 639 | |
| 640 | while (current_width >= fTileMinPowerOf2Width) { |
| 641 | // It is very important this is a bitwise AND. |
| 642 | if (current_width & rounded_value) { |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 643 | if (0 == tile_y_start) { |
| 644 | // Only count tiles in the X direction on the first pass. |
| 645 | fTilesX++; |
| 646 | } |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 647 | *fTileRects.append() = SkRect::MakeXYWH(SkIntToScalar(tile_x_start), |
| 648 | SkIntToScalar(tile_y_start), |
| 649 | SkIntToScalar(current_width), |
| 650 | SkIntToScalar(fTileHeight)); |
keyar@chromium.org | f4959ab | 2012-08-23 20:53:25 +0000 | [diff] [blame] | 651 | tile_x_start += current_width; |
| 652 | } |
| 653 | |
| 654 | current_width >>= 1; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 659 | /** |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 660 | * 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] | 661 | * canvas represents the rectangle's portion of the overall picture. |
| 662 | * Saves and restores so that the initial clip and matrix return to their state before this function |
| 663 | * is called. |
| 664 | */ |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 665 | 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] | 666 | int saveCount = canvas->save(); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 667 | // Translate so that we draw the correct portion of the picture. |
| 668 | // Perform a postTranslate so that the scaleFactor does not interfere with the positioning. |
| 669 | SkMatrix mat(canvas->getTotalMatrix()); |
| 670 | mat.postTranslate(-tileRect.fLeft, -tileRect.fTop); |
| 671 | canvas->setMatrix(mat); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 672 | canvas->drawPicture(*picture); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 673 | canvas->restoreToCount(saveCount); |
| 674 | canvas->flush(); |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 675 | } |
| 676 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 677 | /////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 678 | |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 679 | /** |
| 680 | * Copies the entirety of the src bitmap (typically a tile) into a portion of the dst bitmap. |
| 681 | * If the src bitmap is too large to fit within the dst bitmap after the x and y |
| 682 | * offsets have been applied, any excess will be ignored (so only the top-left portion of the |
| 683 | * src bitmap will be copied). |
| 684 | * |
| 685 | * @param src source bitmap |
| 686 | * @param dst destination bitmap |
| 687 | * @param xOffset x-offset within destination bitmap |
| 688 | * @param yOffset y-offset within destination bitmap |
| 689 | */ |
| 690 | static void bitmapCopyAtOffset(const SkBitmap& src, SkBitmap* dst, |
| 691 | int xOffset, int yOffset) { |
| 692 | for (int y = 0; y <src.height() && y + yOffset < dst->height() ; y++) { |
| 693 | for (int x = 0; x < src.width() && x + xOffset < dst->width() ; x++) { |
| 694 | *dst->getAddr32(xOffset + x, yOffset + y) = *src.getAddr32(x, y); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 699 | bool TiledPictureRenderer::nextTile(int &i, int &j) { |
| 700 | if (++fCurrentTileOffset < fTileRects.count()) { |
| 701 | i = fCurrentTileOffset % fTilesX; |
| 702 | j = fCurrentTileOffset / fTilesX; |
| 703 | return true; |
| 704 | } |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | void TiledPictureRenderer::drawCurrentTile() { |
| 709 | SkASSERT(fCurrentTileOffset >= 0 && fCurrentTileOffset < fTileRects.count()); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 710 | draw_tile_to_canvas(fCanvas, fTileRects[fCurrentTileOffset], fPicture); |
scroggo@google.com | cbcef70 | 2012-12-13 22:09:28 +0000 | [diff] [blame] | 711 | } |
| 712 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 713 | bool TiledPictureRenderer::render(SkBitmap** out) { |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 714 | SkASSERT(fPicture != NULL); |
| 715 | if (NULL == fPicture) { |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 716 | return false; |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 717 | } |
| 718 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 719 | SkBitmap bitmap; |
| 720 | if (out){ |
| 721 | *out = SkNEW(SkBitmap); |
| 722 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 723 | setup_bitmap(&bitmap, fTileWidth, fTileHeight); |
| 724 | } |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 725 | bool success = true; |
| 726 | for (int i = 0; i < fTileRects.count(); ++i) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 727 | draw_tile_to_canvas(fCanvas, fTileRects[i], fPicture); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 728 | if (!fOutputDir.isEmpty()) { |
| 729 | success &= write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 730 | fUseChecksumBasedFilenames, &i); |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 731 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 732 | if (NULL != out) { |
| 733 | if (fCanvas->readPixels(&bitmap, 0, 0)) { |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 734 | // Add this tile to the entire bitmap. |
| 735 | bitmapCopyAtOffset(bitmap, *out, SkScalarFloorToInt(fTileRects[i].left()), |
| 736 | SkScalarFloorToInt(fTileRects[i].top())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 737 | } else { |
| 738 | success = false; |
| 739 | } |
| 740 | } |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 741 | } |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 742 | return success; |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 743 | } |
| 744 | |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 745 | SkCanvas* TiledPictureRenderer::setupCanvas(int width, int height) { |
| 746 | SkCanvas* canvas = this->INHERITED::setupCanvas(width, height); |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 747 | SkASSERT(NULL != fPicture); |
scroggo@google.com | 82ec0b0 | 2012-12-17 19:25:54 +0000 | [diff] [blame] | 748 | // Clip the tile to an area that is completely inside both the SkPicture and the viewport. This |
| 749 | // is mostly important for tiles on the right and bottom edges as they may go over this area and |
| 750 | // the picture may have some commands that draw outside of this area and so should not actually |
| 751 | // be written. |
| 752 | // Uses a clipRegion so that it will be unaffected by the scale factor, which may have been set |
| 753 | // by INHERITED::setupCanvas. |
| 754 | SkRegion clipRegion; |
| 755 | clipRegion.setRect(0, 0, this->getViewWidth(), this->getViewHeight()); |
| 756 | canvas->clipRegion(clipRegion); |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 757 | return canvas; |
| 758 | } |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 759 | |
| 760 | SkString TiledPictureRenderer::getConfigNameInternal() { |
| 761 | SkString name; |
| 762 | if (fTileMinPowerOf2Width > 0) { |
| 763 | name.append("pow2tile_"); |
| 764 | name.appendf("%i", fTileMinPowerOf2Width); |
| 765 | } else { |
| 766 | name.append("tile_"); |
| 767 | if (fTileWidthPercentage > 0) { |
| 768 | name.appendf("%.f%%", fTileWidthPercentage); |
| 769 | } else { |
| 770 | name.appendf("%i", fTileWidth); |
| 771 | } |
| 772 | } |
| 773 | name.append("x"); |
| 774 | if (fTileHeightPercentage > 0) { |
| 775 | name.appendf("%.f%%", fTileHeightPercentage); |
| 776 | } else { |
| 777 | name.appendf("%i", fTileHeight); |
| 778 | } |
| 779 | return name; |
| 780 | } |
| 781 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 782 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 783 | |
| 784 | // Holds all of the information needed to draw a set of tiles. |
| 785 | class CloneData : public SkRunnable { |
| 786 | |
| 787 | public: |
| 788 | 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] | 789 | SkRunnable* done, ImageResultsSummary* jsonSummaryPtr, bool useChecksumBasedFilenames) |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 790 | : fClone(clone) |
| 791 | , fCanvas(canvas) |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 792 | , fRects(rects) |
| 793 | , fStart(start) |
| 794 | , fEnd(end) |
| 795 | , fSuccess(NULL) |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 796 | , fDone(done) |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 797 | , fJsonSummaryPtr(jsonSummaryPtr) |
| 798 | , fUseChecksumBasedFilenames(useChecksumBasedFilenames) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 799 | SkASSERT(fDone != NULL); |
| 800 | } |
| 801 | |
| 802 | virtual void run() SK_OVERRIDE { |
| 803 | SkGraphics::SetTLSFontCacheLimit(1024 * 1024); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 804 | |
| 805 | SkBitmap bitmap; |
| 806 | if (fBitmap != NULL) { |
| 807 | // All tiles are the same size. |
jvanverth@google.com | 9c4e5ac | 2013-01-07 18:41:28 +0000 | [diff] [blame] | 808 | setup_bitmap(&bitmap, SkScalarFloorToInt(fRects[0].width()), SkScalarFloorToInt(fRects[0].height())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 809 | } |
skia.committer@gmail.com | a7d8e3e | 2012-12-19 02:01:38 +0000 | [diff] [blame] | 810 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 811 | for (int i = fStart; i < fEnd; i++) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 812 | draw_tile_to_canvas(fCanvas, fRects[i], fClone); |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 813 | if ((!fOutputDir.isEmpty()) |
| 814 | && !write(fCanvas, fOutputDir, fInputFilename, fJsonSummaryPtr, |
| 815 | fUseChecksumBasedFilenames, &i) |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 816 | && fSuccess != NULL) { |
| 817 | *fSuccess = false; |
| 818 | // If one tile fails to write to a file, do not continue drawing the rest. |
| 819 | break; |
| 820 | } |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 821 | if (fBitmap != NULL) { |
| 822 | if (fCanvas->readPixels(&bitmap, 0, 0)) { |
| 823 | SkAutoLockPixels alp(*fBitmap); |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 824 | bitmapCopyAtOffset(bitmap, fBitmap, SkScalarFloorToInt(fRects[i].left()), |
| 825 | SkScalarFloorToInt(fRects[i].top())); |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 826 | } else { |
| 827 | *fSuccess = false; |
| 828 | // If one tile fails to read pixels, do not continue drawing the rest. |
| 829 | break; |
| 830 | } |
| 831 | } |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 832 | } |
| 833 | fDone->run(); |
| 834 | } |
| 835 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 836 | void setPathsAndSuccess(const SkString& outputDir, const SkString& inputFilename, |
| 837 | bool* success) { |
| 838 | fOutputDir.set(outputDir); |
| 839 | fInputFilename.set(inputFilename); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 840 | fSuccess = success; |
| 841 | } |
| 842 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 843 | void setBitmap(SkBitmap* bitmap) { |
| 844 | fBitmap = bitmap; |
| 845 | } |
| 846 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 847 | private: |
| 848 | // All pointers unowned. |
| 849 | SkPicture* fClone; // Picture to draw from. Each CloneData has a unique one which |
| 850 | // is threadsafe. |
| 851 | SkCanvas* fCanvas; // Canvas to draw to. Reused for each tile. |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 852 | SkString fOutputDir; // If not empty, write results into this directory. |
| 853 | SkString fInputFilename; // Filename of input SkPicture file. |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 854 | SkTDArray<SkRect>& fRects; // All tiles of the picture. |
| 855 | const int fStart; // Range of tiles drawn by this thread. |
| 856 | const int fEnd; |
| 857 | bool* fSuccess; // Only meaningful if path is non-null. Shared by all threads, |
| 858 | // and only set to false upon failure to write to a PNG. |
| 859 | SkRunnable* fDone; |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 860 | SkBitmap* fBitmap; |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 861 | ImageResultsSummary* fJsonSummaryPtr; |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 862 | bool fUseChecksumBasedFilenames; |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 863 | }; |
| 864 | |
| 865 | MultiCorePictureRenderer::MultiCorePictureRenderer(int threadCount) |
| 866 | : fNumThreads(threadCount) |
| 867 | , fThreadPool(threadCount) |
| 868 | , fCountdown(threadCount) { |
| 869 | // Only need to create fNumThreads - 1 clones, since one thread will use the base |
| 870 | // picture. |
| 871 | fPictureClones = SkNEW_ARRAY(SkPicture, fNumThreads - 1); |
| 872 | fCloneData = SkNEW_ARRAY(CloneData*, fNumThreads); |
| 873 | } |
| 874 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 875 | void MultiCorePictureRenderer::init(SkPicture *pict, const SkString* outputDir, |
| 876 | const SkString* inputFilename, bool useChecksumBasedFilenames) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 877 | // Set fPicture and the tiles. |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 878 | this->INHERITED::init(pict, outputDir, inputFilename, useChecksumBasedFilenames); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 879 | for (int i = 0; i < fNumThreads; ++i) { |
| 880 | *fCanvasPool.append() = this->setupCanvas(this->getTileWidth(), this->getTileHeight()); |
| 881 | } |
| 882 | // Only need to create fNumThreads - 1 clones, since one thread will use the base picture. |
| 883 | fPicture->clone(fPictureClones, fNumThreads - 1); |
| 884 | // Populate each thread with the appropriate data. |
| 885 | // Group the tiles into nearly equal size chunks, rounding up so we're sure to cover them all. |
| 886 | const int chunkSize = (fTileRects.count() + fNumThreads - 1) / fNumThreads; |
| 887 | |
| 888 | for (int i = 0; i < fNumThreads; i++) { |
| 889 | SkPicture* pic; |
| 890 | if (i == fNumThreads-1) { |
| 891 | // The last set will use the original SkPicture. |
| 892 | pic = fPicture; |
| 893 | } else { |
| 894 | pic = &fPictureClones[i]; |
| 895 | } |
| 896 | const int start = i * chunkSize; |
| 897 | const int end = SkMin32(start + chunkSize, fTileRects.count()); |
| 898 | fCloneData[i] = SkNEW_ARGS(CloneData, |
commit-bot@chromium.org | a3f882c | 2013-12-13 20:52:36 +0000 | [diff] [blame] | 899 | (pic, fCanvasPool[i], fTileRects, start, end, &fCountdown, |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 900 | fJsonSummaryPtr, useChecksumBasedFilenames)); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 904 | bool MultiCorePictureRenderer::render(SkBitmap** out) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 905 | bool success = true; |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 906 | if (!fOutputDir.isEmpty()) { |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 907 | for (int i = 0; i < fNumThreads-1; i++) { |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 908 | fCloneData[i]->setPathsAndSuccess(fOutputDir, fInputFilename, &success); |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
edisonn@google.com | 84f548c | 2012-12-18 22:24:03 +0000 | [diff] [blame] | 912 | if (NULL != out) { |
| 913 | *out = SkNEW(SkBitmap); |
| 914 | setup_bitmap(*out, fPicture->width(), fPicture->height()); |
| 915 | for (int i = 0; i < fNumThreads; i++) { |
| 916 | fCloneData[i]->setBitmap(*out); |
| 917 | } |
| 918 | } else { |
| 919 | for (int i = 0; i < fNumThreads; i++) { |
| 920 | fCloneData[i]->setBitmap(NULL); |
| 921 | } |
| 922 | } |
| 923 | |
scroggo@google.com | a62da2f | 2012-11-02 21:28:12 +0000 | [diff] [blame] | 924 | fCountdown.reset(fNumThreads); |
| 925 | for (int i = 0; i < fNumThreads; i++) { |
| 926 | fThreadPool.add(fCloneData[i]); |
| 927 | } |
| 928 | fCountdown.wait(); |
| 929 | |
| 930 | return success; |
| 931 | } |
| 932 | |
| 933 | void MultiCorePictureRenderer::end() { |
| 934 | for (int i = 0; i < fNumThreads - 1; i++) { |
| 935 | SkDELETE(fCloneData[i]); |
| 936 | fCloneData[i] = NULL; |
| 937 | } |
| 938 | |
| 939 | fCanvasPool.unrefAll(); |
| 940 | |
| 941 | this->INHERITED::end(); |
| 942 | } |
| 943 | |
| 944 | MultiCorePictureRenderer::~MultiCorePictureRenderer() { |
| 945 | // Each individual CloneData was deleted in end. |
| 946 | SkDELETE_ARRAY(fCloneData); |
| 947 | SkDELETE_ARRAY(fPictureClones); |
| 948 | } |
scroggo@google.com | bcdf2ec | 2012-09-20 14:42:33 +0000 | [diff] [blame] | 949 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 950 | SkString MultiCorePictureRenderer::getConfigNameInternal() { |
| 951 | SkString name = this->INHERITED::getConfigNameInternal(); |
| 952 | name.appendf("_multi_%i_threads", fNumThreads); |
| 953 | return name; |
| 954 | } |
| 955 | |
scroggo@google.com | acfb30e | 2012-09-18 14:32:35 +0000 | [diff] [blame] | 956 | /////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 957 | |
| 958 | void PlaybackCreationRenderer::setup() { |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 959 | SkAutoTDelete<SkBBHFactory> factory(this->getFactory()); |
| 960 | fRecorder.reset(SkNEW(SkPictureRecorder)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 961 | SkCanvas* canvas = fRecorder->beginRecording(this->getViewWidth(), this->getViewHeight(), |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 962 | factory.get(), |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 963 | this->recordFlags()); |
| 964 | this->scaleToScaleFactor(canvas); |
| 965 | canvas->drawPicture(*fPicture); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 966 | } |
| 967 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 968 | bool PlaybackCreationRenderer::render(SkBitmap** out) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 969 | fPicture.reset(fRecorder->endRecording()); |
scroggo@google.com | 81f9d2e | 2012-09-20 14:54:21 +0000 | [diff] [blame] | 970 | // Since this class does not actually render, return false. |
| 971 | return false; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 972 | } |
| 973 | |
scroggo@google.com | 0a049b8 | 2012-11-02 22:01:26 +0000 | [diff] [blame] | 974 | SkString PlaybackCreationRenderer::getConfigNameInternal() { |
| 975 | return SkString("playback_creation"); |
| 976 | } |
| 977 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 978 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 979 | // SkPicture variants for each BBoxHierarchy type |
| 980 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 981 | SkBBHFactory* PictureRenderer::getFactory() { |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 982 | switch (fBBoxHierarchyType) { |
| 983 | case kNone_BBoxHierarchyType: |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 984 | return NULL; |
commit-bot@chromium.org | c22d139 | 2014-02-03 18:08:33 +0000 | [diff] [blame] | 985 | case kQuadTree_BBoxHierarchyType: |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 986 | return SkNEW(SkQuadTreeFactory); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 987 | case kRTree_BBoxHierarchyType: |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 988 | return SkNEW(SkRTreeFactory); |
junov@chromium.org | 7b53706 | 2012-11-06 18:58:43 +0000 | [diff] [blame] | 989 | case kTileGrid_BBoxHierarchyType: |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame^] | 990 | return SkNEW_ARGS(SkTileGridFactory, (fGridInfo)); |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 991 | } |
| 992 | SkASSERT(0); // invalid bbhType |
| 993 | return NULL; |
keyar@chromium.org | 451bb9f | 2012-07-26 17:27:57 +0000 | [diff] [blame] | 994 | } |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 995 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 996 | /////////////////////////////////////////////////////////////////////////////// |
| 997 | |
| 998 | class GatherRenderer : public PictureRenderer { |
| 999 | public: |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1000 | virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE { |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1001 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture->width()), |
| 1002 | SkIntToScalar(fPicture->height())); |
| 1003 | SkData* data = SkPictureUtils::GatherPixelRefs(fPicture, bounds); |
| 1004 | SkSafeUnref(data); |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 1005 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 1006 | return (fOutputDir.isEmpty()); // we don't have anything to write |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1007 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 1008 | |
reed@google.com | fe7b1ed | 2012-11-29 21:00:39 +0000 | [diff] [blame] | 1009 | private: |
| 1010 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 1011 | return SkString("gather_pixelrefs"); |
| 1012 | } |
| 1013 | }; |
| 1014 | |
| 1015 | PictureRenderer* CreateGatherPixelRefsRenderer() { |
| 1016 | return SkNEW(GatherRenderer); |
| 1017 | } |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 1018 | |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 1019 | /////////////////////////////////////////////////////////////////////////////// |
| 1020 | |
| 1021 | class PictureCloneRenderer : public PictureRenderer { |
| 1022 | public: |
commit-bot@chromium.org | 8ddc26b | 2014-03-31 17:55:12 +0000 | [diff] [blame] | 1023 | virtual bool render(SkBitmap** out = NULL) SK_OVERRIDE { |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 1024 | for (int i = 0; i < 100; ++i) { |
| 1025 | SkPicture* clone = fPicture->clone(); |
| 1026 | SkSafeUnref(clone); |
| 1027 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 1028 | |
commit-bot@chromium.org | f5e315c | 2014-03-19 17:26:07 +0000 | [diff] [blame] | 1029 | return (fOutputDir.isEmpty()); // we don't have anything to write |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 1030 | } |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 1031 | |
reed@google.com | 5a34fd3 | 2012-12-10 16:05:09 +0000 | [diff] [blame] | 1032 | private: |
| 1033 | virtual SkString getConfigNameInternal() SK_OVERRIDE { |
| 1034 | return SkString("picture_clone"); |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | PictureRenderer* CreatePictureCloneRenderer() { |
| 1039 | return SkNEW(PictureCloneRenderer); |
| 1040 | } |
| 1041 | |
junov@chromium.org | 9313ca4 | 2012-11-02 18:11:49 +0000 | [diff] [blame] | 1042 | } // namespace sk_tools |