Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/paint/paint_op_buffer_serializer.h" |
| 6 | |
| 7 | #include "base/bind.h" |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 8 | #include "base/trace_event/trace_event.h" |
Florin Malita | e32b0b7 | 2017-12-08 00:50:24 | [diff] [blame] | 9 | #include "cc/paint/scoped_raster_flags.h" |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 10 | #include "ui/gfx/skia_util.h" |
| 11 | |
Adrienne Walker | 9e508a68 | 2019-03-06 20:21:25 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 14 | namespace cc { |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | class ScopedFlagsOverride { |
| 18 | public: |
| 19 | ScopedFlagsOverride(PaintOp::SerializeOptions* options, |
| 20 | const PaintFlags* flags) |
| 21 | : options_(options) { |
| 22 | options_->flags_to_serialize = flags; |
| 23 | } |
| 24 | ~ScopedFlagsOverride() { options_->flags_to_serialize = nullptr; } |
| 25 | |
| 26 | private: |
| 27 | PaintOp::SerializeOptions* options_; |
| 28 | }; |
| 29 | |
danakj | 57baa77 | 2018-05-29 15:59:14 | [diff] [blame] | 30 | // Copied from viz::ClientResourceProvider. |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 31 | SkSurfaceProps ComputeSurfaceProps(bool can_use_lcd_text) { |
| 32 | uint32_t flags = 0; |
| 33 | // Use unknown pixel geometry to disable LCD text. |
| 34 | SkSurfaceProps surface_props(flags, kUnknown_SkPixelGeometry); |
| 35 | if (can_use_lcd_text) { |
| 36 | // LegacyFontHost will get LCD text and skia figures out what type to use. |
| 37 | surface_props = |
| 38 | SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 39 | } |
| 40 | return surface_props; |
| 41 | } |
| 42 | |
| 43 | PlaybackParams MakeParams(const SkCanvas* canvas) { |
| 44 | // We don't use an ImageProvider here since the ops are played onto a no-draw |
| 45 | // canvas for state tracking and don't need decoded images. |
| 46 | return PlaybackParams(nullptr, canvas->getTotalMatrix()); |
| 47 | } |
| 48 | |
| 49 | // Use half of the max int as the extent for the SkNoDrawCanvas. The correct |
| 50 | // clip is applied to the canvas during serialization. |
| 51 | const int kMaxExtent = std::numeric_limits<int>::max() >> 1; |
| 52 | |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 53 | } // namespace |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 54 | |
Vladimir Levin | 7273cfe05 | 2017-12-13 21:41:41 | [diff] [blame] | 55 | PaintOpBufferSerializer::PaintOpBufferSerializer( |
| 56 | SerializeCallback serialize_cb, |
| 57 | ImageProvider* image_provider, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 58 | TransferCacheSerializeHelper* transfer_cache, |
Khushal | 33205a7 | 2018-11-08 10:12:29 | [diff] [blame] | 59 | ClientPaintCache* paint_cache, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 60 | SkStrikeServer* strike_server, |
Adrienne Walker | 9e508a68 | 2019-03-06 20:21:25 | [diff] [blame] | 61 | sk_sp<SkColorSpace> color_space, |
Khushal | 315f2fd | 2018-05-29 03:20:39 | [diff] [blame] | 62 | bool can_use_lcd_text, |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 63 | bool context_supports_distance_field_text, |
| 64 | int max_texture_size, |
| 65 | size_t max_texture_bytes) |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 66 | : serialize_cb_(std::move(serialize_cb)), |
Vladimir Levin | 7273cfe05 | 2017-12-13 21:41:41 | [diff] [blame] | 67 | image_provider_(image_provider), |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 68 | transfer_cache_(transfer_cache), |
Khushal | 33205a7 | 2018-11-08 10:12:29 | [diff] [blame] | 69 | paint_cache_(paint_cache), |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 70 | strike_server_(strike_server), |
| 71 | color_space_(color_space), |
| 72 | can_use_lcd_text_(can_use_lcd_text), |
Khushal | 315f2fd | 2018-05-29 03:20:39 | [diff] [blame] | 73 | context_supports_distance_field_text_( |
| 74 | context_supports_distance_field_text), |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 75 | max_texture_size_(max_texture_size), |
| 76 | max_texture_bytes_(max_texture_bytes), |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 77 | text_blob_canvas_(kMaxExtent, |
| 78 | kMaxExtent, |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 79 | ComputeSurfaceProps(can_use_lcd_text), |
| 80 | strike_server, |
Adrienne Walker | 9e508a68 | 2019-03-06 20:21:25 | [diff] [blame] | 81 | std::move(color_space), |
Herb Derby | e2c214ab | 2019-08-19 21:54:33 | [diff] [blame] | 82 | context_supports_distance_field_text) { |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 83 | DCHECK(serialize_cb_); |
| 84 | } |
| 85 | |
| 86 | PaintOpBufferSerializer::~PaintOpBufferSerializer() = default; |
| 87 | |
| 88 | void PaintOpBufferSerializer::Serialize(const PaintOpBuffer* buffer, |
| 89 | const std::vector<size_t>* offsets, |
| 90 | const Preamble& preamble) { |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 91 | DCHECK(text_blob_canvas_.getTotalMatrix().isIdentity()); |
Eric Karl | 9c272562 | 2018-01-06 00:35:32 | [diff] [blame] | 92 | static const int kInitialSaveCount = 1; |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 93 | DCHECK_EQ(kInitialSaveCount, text_blob_canvas_.getSaveCount()); |
Eric Karl | 9c272562 | 2018-01-06 00:35:32 | [diff] [blame] | 94 | |
| 95 | // These SerializeOptions and PlaybackParams use the initial (identity) canvas |
| 96 | // matrix, as they are only used for serializing the preamble and the initial |
| 97 | // save / final restore. SerializeBuffer will create its own SerializeOptions |
| 98 | // and PlaybackParams based on the post-preamble canvas. |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 99 | PaintOp::SerializeOptions options = MakeSerializeOptions(); |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 100 | PlaybackParams params = MakeParams(&text_blob_canvas_); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 101 | |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 102 | Save(options, params); |
| 103 | SerializePreamble(preamble, options, params); |
| 104 | SerializeBuffer(buffer, offsets); |
Eric Karl | 9c272562 | 2018-01-06 00:35:32 | [diff] [blame] | 105 | RestoreToCount(kInitialSaveCount, options, params); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 106 | } |
| 107 | |
Vladimir Levin | bd083f78 | 2018-01-12 23:26:09 | [diff] [blame] | 108 | void PaintOpBufferSerializer::Serialize(const PaintOpBuffer* buffer) { |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 109 | DCHECK(text_blob_canvas_.getTotalMatrix().isIdentity()); |
Vladimir Levin | bd083f78 | 2018-01-12 23:26:09 | [diff] [blame] | 110 | |
| 111 | SerializeBuffer(buffer, nullptr); |
| 112 | } |
| 113 | |
Khushal | ec01f347 | 2018-05-19 00:36:26 | [diff] [blame] | 114 | void PaintOpBufferSerializer::Serialize( |
| 115 | const PaintOpBuffer* buffer, |
| 116 | const gfx::Rect& playback_rect, |
| 117 | const gfx::SizeF& post_scale, |
| 118 | const SkMatrix& post_matrix_for_analysis) { |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 119 | DCHECK(text_blob_canvas_.getTotalMatrix().isIdentity()); |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 120 | |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 121 | PaintOp::SerializeOptions options = MakeSerializeOptions(); |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 122 | PlaybackParams params = MakeParams(&text_blob_canvas_); |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 123 | |
| 124 | // TODO(khushalsagar): remove this clip rect if it's not needed. |
| 125 | if (!playback_rect.IsEmpty()) { |
| 126 | ClipRectOp clip_op(gfx::RectToSkRect(playback_rect), SkClipOp::kIntersect, |
| 127 | false); |
| 128 | SerializeOp(&clip_op, options, params); |
| 129 | } |
| 130 | |
| 131 | if (post_scale.width() != 1.f || post_scale.height() != 1.f) { |
| 132 | ScaleOp scale_op(post_scale.width(), post_scale.height()); |
| 133 | SerializeOp(&scale_op, options, params); |
| 134 | } |
| 135 | |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 136 | text_blob_canvas_.concat(post_matrix_for_analysis); |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 137 | SerializeBuffer(buffer, nullptr); |
| 138 | } |
| 139 | |
Adrienne Walker | 9300134 | 2018-06-12 22:08:40 | [diff] [blame] | 140 | // This function needs to have the exact same behavior as |
| 141 | // RasterSource::ClearForOpaqueRaster. |
| 142 | void PaintOpBufferSerializer::ClearForOpaqueRaster( |
| 143 | const Preamble& preamble, |
| 144 | const PaintOp::SerializeOptions& options, |
| 145 | const PlaybackParams& params) { |
| 146 | // Clear opaque raster sources. Opaque rasters sources guarantee that all |
| 147 | // pixels inside the opaque region are painted. However, due to scaling |
| 148 | // it's possible that the last row and column might include pixels that |
| 149 | // are not painted. Because this raster source is required to be opaque, |
| 150 | // we may need to do extra clearing outside of the clip. This needs to |
| 151 | // be done for both full and partial raster. |
| 152 | |
| 153 | // The last texel of this content is not guaranteed to be fully opaque, so |
| 154 | // inset by one to generate the fully opaque coverage rect. This rect is |
| 155 | // in device space. |
| 156 | SkIRect coverage_device_rect = SkIRect::MakeWH( |
| 157 | preamble.content_size.width() - preamble.full_raster_rect.x() - 1, |
| 158 | preamble.content_size.height() - preamble.full_raster_rect.y() - 1); |
| 159 | |
| 160 | // If not fully covered, we need to clear one texel inside the coverage |
| 161 | // rect (because of blending during raster) and one texel outside the canvas |
| 162 | // bitmap rect (because of bilinear filtering during draw). See comments |
| 163 | // in RasterSource. |
| 164 | SkIRect device_column = SkIRect::MakeXYWH(coverage_device_rect.right(), 0, 2, |
| 165 | coverage_device_rect.bottom()); |
| 166 | // row includes the corner, column excludes it. |
| 167 | SkIRect device_row = SkIRect::MakeXYWH(0, coverage_device_rect.bottom(), |
| 168 | coverage_device_rect.right() + 2, 2); |
| 169 | |
| 170 | bool right_edge = |
| 171 | preamble.content_size.width() == preamble.playback_rect.right(); |
| 172 | bool bottom_edge = |
| 173 | preamble.content_size.height() == preamble.playback_rect.bottom(); |
| 174 | |
| 175 | // If the playback rect is touching either edge of the content rect |
| 176 | // extend it by one pixel to include the extra texel outside the canvas |
| 177 | // bitmap rect that was added to device column and row above. |
| 178 | SkIRect playback_device_rect = SkIRect::MakeXYWH( |
| 179 | preamble.playback_rect.x() - preamble.full_raster_rect.x(), |
| 180 | preamble.playback_rect.y() - preamble.full_raster_rect.y(), |
| 181 | preamble.playback_rect.width() + (right_edge ? 1 : 0), |
| 182 | preamble.playback_rect.height() + (bottom_edge ? 1 : 0)); |
| 183 | |
| 184 | // Intersect the device column and row with the playback rect and only |
| 185 | // clear inside of that rect if needed. |
| 186 | if (device_column.intersect(playback_device_rect)) { |
| 187 | Save(options, params); |
Mike Klein | b3da522 | 2018-10-04 17:56:44 | [diff] [blame] | 188 | ClipRectOp clip_op(SkRect::Make(device_column), SkClipOp::kIntersect, |
| 189 | false); |
Adrienne Walker | 9300134 | 2018-06-12 22:08:40 | [diff] [blame] | 190 | SerializeOp(&clip_op, options, params); |
| 191 | DrawColorOp clear_op(preamble.background_color, SkBlendMode::kSrc); |
| 192 | SerializeOp(&clear_op, options, params); |
| 193 | RestoreToCount(1, options, params); |
| 194 | } |
| 195 | if (device_row.intersect(playback_device_rect)) { |
| 196 | Save(options, params); |
Mike Klein | b3da522 | 2018-10-04 17:56:44 | [diff] [blame] | 197 | ClipRectOp clip_op(SkRect::Make(device_row), SkClipOp::kIntersect, false); |
Adrienne Walker | 9300134 | 2018-06-12 22:08:40 | [diff] [blame] | 198 | SerializeOp(&clip_op, options, params); |
| 199 | DrawColorOp clear_op(preamble.background_color, SkBlendMode::kSrc); |
| 200 | SerializeOp(&clear_op, options, params); |
| 201 | RestoreToCount(1, options, params); |
| 202 | } |
| 203 | } |
| 204 | |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 205 | void PaintOpBufferSerializer::SerializePreamble( |
| 206 | const Preamble& preamble, |
| 207 | const PaintOp::SerializeOptions& options, |
| 208 | const PlaybackParams& params) { |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 209 | DCHECK(preamble.full_raster_rect.Contains(preamble.playback_rect)) |
| 210 | << "full: " << preamble.full_raster_rect.ToString() |
| 211 | << ", playback: " << preamble.playback_rect.ToString(); |
| 212 | |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 213 | bool is_partial_raster = preamble.full_raster_rect != preamble.playback_rect; |
Adrienne Walker | 9300134 | 2018-06-12 22:08:40 | [diff] [blame] | 214 | if (!preamble.requires_clear) { |
| 215 | ClearForOpaqueRaster(preamble, options, params); |
| 216 | } else if (!is_partial_raster) { |
| 217 | // If rastering the entire tile, clear to transparent pre-clip. This is so |
| 218 | // that any external texels outside of the playback rect also get cleared. |
| 219 | // There's not enough information at this point to know if this texture is |
| 220 | // being reused from another tile, so the external texels could have been |
| 221 | // cleared to some wrong value. |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 222 | DrawColorOp clear(SK_ColorTRANSPARENT, SkBlendMode::kSrc); |
| 223 | SerializeOp(&clear, options, params); |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (!preamble.full_raster_rect.OffsetFromOrigin().IsZero()) { |
| 227 | TranslateOp translate_op(-preamble.full_raster_rect.x(), |
| 228 | -preamble.full_raster_rect.y()); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 229 | SerializeOp(&translate_op, options, params); |
| 230 | } |
| 231 | |
| 232 | if (!preamble.playback_rect.IsEmpty()) { |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 233 | ClipRectOp clip_op(gfx::RectToSkRect(preamble.playback_rect), |
Khushal | 9fd411f | 2018-01-20 02:09:51 | [diff] [blame] | 234 | SkClipOp::kIntersect, false); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 235 | SerializeOp(&clip_op, options, params); |
| 236 | } |
| 237 | |
| 238 | if (!preamble.post_translation.IsZero()) { |
| 239 | TranslateOp translate_op(preamble.post_translation.x(), |
| 240 | preamble.post_translation.y()); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 241 | SerializeOp(&translate_op, options, params); |
| 242 | } |
| 243 | |
Khushal | 9fd411f | 2018-01-20 02:09:51 | [diff] [blame] | 244 | if (preamble.post_scale.width() != 1.f || |
| 245 | preamble.post_scale.height() != 1.f) { |
| 246 | ScaleOp scale_op(preamble.post_scale.width(), preamble.post_scale.height()); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 247 | SerializeOp(&scale_op, options, params); |
| 248 | } |
Adrienne Walker | 51c8e38 | 2018-02-06 20:30:33 | [diff] [blame] | 249 | |
| 250 | // If tile is transparent and this is partial raster, just clear the |
| 251 | // section that is being rastered. If this is opaque, trust the raster |
| 252 | // to write all the pixels inside of the full_raster_rect. |
| 253 | if (preamble.requires_clear && is_partial_raster) { |
| 254 | DrawColorOp clear_op(SK_ColorTRANSPARENT, SkBlendMode::kSrc); |
| 255 | SerializeOp(&clear_op, options, params); |
| 256 | } |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void PaintOpBufferSerializer::SerializeBuffer( |
| 260 | const PaintOpBuffer* buffer, |
| 261 | const std::vector<size_t>* offsets) { |
Vladimir Levin | bd083f78 | 2018-01-12 23:26:09 | [diff] [blame] | 262 | DCHECK(buffer); |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 263 | PaintOp::SerializeOptions options = MakeSerializeOptions(); |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 264 | PlaybackParams params = MakeParams(&text_blob_canvas_); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 265 | |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 266 | for (PaintOpBuffer::PlaybackFoldingIterator iter(buffer, offsets); iter; |
| 267 | ++iter) { |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 268 | const PaintOp* op = *iter; |
| 269 | |
| 270 | // Skip ops outside the current clip if they have images. This saves |
| 271 | // performing an unnecessary expensive decode. |
| 272 | const bool skip_op = PaintOp::OpHasDiscardableImages(op) && |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 273 | PaintOp::QuickRejectDraw(op, &text_blob_canvas_); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 274 | if (skip_op) |
| 275 | continue; |
| 276 | |
| 277 | if (op->GetType() != PaintOpType::DrawRecord) { |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 278 | bool success = false; |
| 279 | if (op->IsPaintOpWithFlags()) { |
| 280 | success = SerializeOpWithFlags(static_cast<const PaintOpWithFlags*>(op), |
| 281 | &options, params, iter.alpha()); |
| 282 | } else { |
| 283 | success = SerializeOp(op, options, params); |
| 284 | } |
| 285 | |
| 286 | if (!success) |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 287 | return; |
| 288 | continue; |
| 289 | } |
| 290 | |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 291 | int save_count = text_blob_canvas_.getSaveCount(); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 292 | Save(options, params); |
| 293 | SerializeBuffer(static_cast<const DrawRecordOp*>(op)->record.get(), |
| 294 | nullptr); |
| 295 | RestoreToCount(save_count, options, params); |
| 296 | } |
| 297 | } |
| 298 | |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 299 | bool PaintOpBufferSerializer::SerializeOpWithFlags( |
| 300 | const PaintOpWithFlags* flags_op, |
| 301 | PaintOp::SerializeOptions* options, |
| 302 | const PlaybackParams& params, |
| 303 | uint8_t alpha) { |
Khushal | 67c8c74 | 2018-05-16 18:05:05 | [diff] [blame] | 304 | // We use a null |image_provider| here because images are decoded during |
| 305 | // serialization. |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 306 | const ScopedRasterFlags scoped_flags(&flags_op->flags, nullptr, |
| 307 | options->canvas->getTotalMatrix(), |
| 308 | max_texture_size_, alpha); |
Florin Malita | e32b0b7 | 2017-12-08 00:50:24 | [diff] [blame] | 309 | const PaintFlags* flags_to_serialize = scoped_flags.flags(); |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 310 | if (!flags_to_serialize) |
| 311 | return true; |
| 312 | |
| 313 | ScopedFlagsOverride override_flags(options, flags_to_serialize); |
| 314 | return SerializeOp(flags_op, *options, params); |
| 315 | } |
| 316 | |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 317 | bool PaintOpBufferSerializer::SerializeOp( |
| 318 | const PaintOp* op, |
| 319 | const PaintOp::SerializeOptions& options, |
| 320 | const PlaybackParams& params) { |
Khushal | 515436e2 | 2019-04-05 05:03:51 | [diff] [blame] | 321 | TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("cc.debug"), |
| 322 | "PaintOpBufferSerializer::SerializeOp", "op", |
| 323 | PaintOpTypeToString(op->GetType())); |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 324 | if (!valid_) |
| 325 | return false; |
| 326 | |
Khushal | e2273458 | 2019-02-23 03:38:49 | [diff] [blame] | 327 | // Playback on analysis canvas first to make sure the canvas transform is set |
| 328 | // correctly for analysis of records in filters. |
| 329 | PlaybackOnAnalysisCanvas(op, options, params); |
| 330 | |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 331 | size_t bytes = serialize_cb_.Run(op, options); |
| 332 | if (!bytes) { |
| 333 | valid_ = false; |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | DCHECK_GE(bytes, 4u); |
| 338 | DCHECK_EQ(bytes % PaintOpBuffer::PaintOpAlign, 0u); |
Khushal | e2273458 | 2019-02-23 03:38:49 | [diff] [blame] | 339 | return true; |
| 340 | } |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 341 | |
Khushal | e2273458 | 2019-02-23 03:38:49 | [diff] [blame] | 342 | void PaintOpBufferSerializer::PlaybackOnAnalysisCanvas( |
| 343 | const PaintOp* op, |
| 344 | const PaintOp::SerializeOptions& options, |
| 345 | const PlaybackParams& params) { |
Khushal | ee6bf43f | 2018-07-10 01:46:11 | [diff] [blame] | 346 | // Only 2 types of ops need to played on the analysis canvas. |
| 347 | // 1) Non-draw ops which affect the transform/clip state on the canvas, since |
| 348 | // we need the correct ctm at which text and images will be rasterized, and |
| 349 | // the clip rect so we can skip sending data for ops which will not be |
| 350 | // rasterized. |
| 351 | // 2) DrawTextBlob ops since they need to be analyzed by the cache diff canvas |
| 352 | // to serialize/lock the requisite glyphs for this op. |
| 353 | if (op->IsDrawOp() && op->GetType() != PaintOpType::DrawTextBlob) |
Khushal | e2273458 | 2019-02-23 03:38:49 | [diff] [blame] | 354 | return; |
Khushal | ee6bf43f | 2018-07-10 01:46:11 | [diff] [blame] | 355 | |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 356 | if (op->IsPaintOpWithFlags() && options.flags_to_serialize) { |
| 357 | static_cast<const PaintOpWithFlags*>(op)->RasterWithFlags( |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 358 | &text_blob_canvas_, options.flags_to_serialize, params); |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 359 | } else { |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 360 | op->Raster(&text_blob_canvas_, params); |
Khushal | 92f6691 | 2017-11-20 10:08:50 | [diff] [blame] | 361 | } |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void PaintOpBufferSerializer::Save(const PaintOp::SerializeOptions& options, |
| 365 | const PlaybackParams& params) { |
| 366 | SaveOp save_op; |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 367 | SerializeOp(&save_op, options, params); |
| 368 | } |
| 369 | |
| 370 | void PaintOpBufferSerializer::RestoreToCount( |
| 371 | int count, |
| 372 | const PaintOp::SerializeOptions& options, |
| 373 | const PlaybackParams& params) { |
| 374 | RestoreOp restore_op; |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 375 | while (text_blob_canvas_.getSaveCount() > count) { |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 376 | if (!SerializeOp(&restore_op, options, params)) |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 381 | PaintOp::SerializeOptions PaintOpBufferSerializer::MakeSerializeOptions() { |
| 382 | return PaintOp::SerializeOptions( |
Mike Reed | 1612701 | 2019-02-25 18:44:36 | [diff] [blame] | 383 | image_provider_, transfer_cache_, paint_cache_, &text_blob_canvas_, |
| 384 | strike_server_, color_space_, can_use_lcd_text_, |
| 385 | context_supports_distance_field_text_, max_texture_size_, |
| 386 | max_texture_bytes_, text_blob_canvas_.getTotalMatrix()); |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 387 | } |
| 388 | |
Vladimir Levin | 7273cfe05 | 2017-12-13 21:41:41 | [diff] [blame] | 389 | SimpleBufferSerializer::SimpleBufferSerializer( |
| 390 | void* memory, |
| 391 | size_t size, |
| 392 | ImageProvider* image_provider, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 393 | TransferCacheSerializeHelper* transfer_cache, |
Khushal | 33205a7 | 2018-11-08 10:12:29 | [diff] [blame] | 394 | ClientPaintCache* paint_cache, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 395 | SkStrikeServer* strike_server, |
Adrienne Walker | 9e508a68 | 2019-03-06 20:21:25 | [diff] [blame] | 396 | sk_sp<SkColorSpace> color_space, |
Khushal | 315f2fd | 2018-05-29 03:20:39 | [diff] [blame] | 397 | bool can_use_lcd_text, |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 398 | bool context_supports_distance_field_text, |
| 399 | int max_texture_size, |
| 400 | size_t max_texture_bytes) |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 401 | : PaintOpBufferSerializer( |
kylechar | 4bb144d | 2019-01-11 20:42:07 | [diff] [blame] | 402 | base::BindRepeating(&SimpleBufferSerializer::SerializeToMemory, |
| 403 | base::Unretained(this)), |
Vladimir Levin | 7273cfe05 | 2017-12-13 21:41:41 | [diff] [blame] | 404 | image_provider, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 405 | transfer_cache, |
Khushal | 33205a7 | 2018-11-08 10:12:29 | [diff] [blame] | 406 | paint_cache, |
Khushal | a8d5064 | 2018-05-03 01:29:06 | [diff] [blame] | 407 | strike_server, |
Adrienne Walker | 9e508a68 | 2019-03-06 20:21:25 | [diff] [blame] | 408 | std::move(color_space), |
Khushal | 315f2fd | 2018-05-29 03:20:39 | [diff] [blame] | 409 | can_use_lcd_text, |
Khushal | f975070 | 2018-06-09 00:42:13 | [diff] [blame] | 410 | context_supports_distance_field_text, |
| 411 | max_texture_size, |
| 412 | max_texture_bytes), |
Khushal | 7b54657 | 2017-11-10 22:05:53 | [diff] [blame] | 413 | memory_(memory), |
| 414 | total_(size) {} |
| 415 | |
| 416 | SimpleBufferSerializer::~SimpleBufferSerializer() = default; |
| 417 | |
| 418 | size_t SimpleBufferSerializer::SerializeToMemory( |
| 419 | const PaintOp* op, |
| 420 | const PaintOp::SerializeOptions& options) { |
| 421 | if (written_ == total_) |
| 422 | return 0u; |
| 423 | |
| 424 | size_t bytes = op->Serialize(static_cast<char*>(memory_) + written_, |
| 425 | total_ - written_, options); |
| 426 | if (!bytes) |
| 427 | return 0u; |
| 428 | |
| 429 | written_ += bytes; |
| 430 | DCHECK_GE(total_, written_); |
| 431 | return bytes; |
| 432 | } |
| 433 | |
| 434 | } // namespace cc |