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