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