paint_op_reader.cc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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. #include "cc/paint/paint_op_reader.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <memory>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/bits.h"
  12. #include "base/compiler_specific.h"
  13. #include "base/debug/dump_without_crashing.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/metrics/histogram_functions.h"
  16. #include "base/notreached.h"
  17. #include "base/rand_util.h"
  18. #include "base/stl_util.h"
  19. #include "base/strings/string_number_conversions.h"
  20. #include "cc/paint/image_transfer_cache_entry.h"
  21. #include "cc/paint/paint_cache.h"
  22. #include "cc/paint/paint_flags.h"
  23. #include "cc/paint/paint_image_builder.h"
  24. #include "cc/paint/paint_op_buffer.h"
  25. #include "cc/paint/paint_shader.h"
  26. #include "cc/paint/shader_transfer_cache_entry.h"
  27. #include "cc/paint/skottie_transfer_cache_entry.h"
  28. #include "cc/paint/skottie_wrapper.h"
  29. #include "cc/paint/transfer_cache_deserialize_helper.h"
  30. #include "components/crash/core/common/crash_key.h"
  31. #include "third_party/skia/include/core/SkColorSpace.h"
  32. #include "third_party/skia/include/core/SkPath.h"
  33. #include "third_party/skia/include/core/SkRRect.h"
  34. #include "third_party/skia/include/core/SkSerialProcs.h"
  35. #include "third_party/skia/include/private/chromium/GrSlug.h"
  36. #include "third_party/skia/include/private/chromium/SkChromeRemoteGlyphCache.h"
  37. namespace cc {
  38. namespace {
  39. bool IsValidPaintShaderType(PaintShader::Type type) {
  40. return static_cast<uint8_t>(type) <
  41. static_cast<uint8_t>(PaintShader::Type::kShaderCount);
  42. }
  43. bool IsValidPaintShaderScalingBehavior(PaintShader::ScalingBehavior behavior) {
  44. return behavior == PaintShader::ScalingBehavior::kRasterAtScale ||
  45. behavior == PaintShader::ScalingBehavior::kFixedScale;
  46. }
  47. } // namespace
  48. // static
  49. void PaintOpReader::FixupMatrixPostSerialization(SkMatrix* matrix) {
  50. // Can't trust malicious clients to provide the correct derived matrix type.
  51. // However, if a matrix thinks that it's identity, then make it so.
  52. if (matrix->isIdentity())
  53. matrix->setIdentity();
  54. else
  55. matrix->dirtyMatrixTypeCache();
  56. }
  57. // static
  58. bool PaintOpReader::ReadAndValidateOpHeader(const volatile void* input,
  59. size_t input_size,
  60. uint8_t* type,
  61. uint32_t* skip) {
  62. if (input_size < 4)
  63. return false;
  64. uint32_t first_word = reinterpret_cast<const volatile uint32_t*>(input)[0];
  65. *type = static_cast<uint8_t>(first_word & 0xFF);
  66. *skip = first_word >> 8;
  67. if (input_size < *skip)
  68. return false;
  69. if (*skip % PaintOpBuffer::PaintOpAlign != 0)
  70. return false;
  71. if (*type > static_cast<uint8_t>(PaintOpType::LastPaintOpType))
  72. return false;
  73. return true;
  74. }
  75. template <typename T>
  76. void PaintOpReader::ReadSimple(T* val) {
  77. static_assert(std::is_trivially_copyable_v<T>);
  78. DCHECK_EQ(memory_, base::bits::AlignUp(memory_, PaintOpWriter::Alignment()));
  79. // Align everything to 4 bytes, as the writer does.
  80. static constexpr size_t size =
  81. base::bits::AlignUp(sizeof(T), PaintOpWriter::Alignment());
  82. if (remaining_bytes_ < size)
  83. SetInvalid(DeserializationError::kInsufficientRemainingBytes_ReadSimple);
  84. if (!valid_)
  85. return;
  86. // Most of the time this is used for primitives, but this function is also
  87. // used for SkRect/SkIRect/SkMatrix whose implicit operator= can't use a
  88. // volatile. TOCTOU violations don't matter for these simple types so
  89. // use assignment.
  90. *val = *reinterpret_cast<const T*>(const_cast<const char*>(memory_));
  91. memory_ += size;
  92. remaining_bytes_ -= size;
  93. }
  94. uint8_t* PaintOpReader::CopyScratchSpace(size_t bytes) {
  95. DCHECK(SkIsAlign4(reinterpret_cast<uintptr_t>(memory_)));
  96. if (options_.scratch_buffer->size() < bytes)
  97. options_.scratch_buffer->resize(bytes);
  98. memcpy(options_.scratch_buffer->data(), const_cast<const char*>(memory_),
  99. bytes);
  100. return options_.scratch_buffer->data();
  101. }
  102. template <typename T>
  103. void PaintOpReader::ReadFlattenable(
  104. sk_sp<T>* val,
  105. Factory<T> factory,
  106. DeserializationError error_on_factory_failure) {
  107. size_t bytes = 0;
  108. ReadSize(&bytes);
  109. if (remaining_bytes_ < bytes) {
  110. SetInvalid(
  111. DeserializationError::kInsufficientRemainingBytes_ReadFlattenable);
  112. return;
  113. }
  114. if (bytes == 0)
  115. return;
  116. auto* scratch = CopyScratchSpace(bytes);
  117. val->reset(factory(scratch, bytes, nullptr).release());
  118. if (!val) {
  119. SetInvalid(error_on_factory_failure);
  120. return;
  121. }
  122. DidRead(bytes);
  123. }
  124. void PaintOpReader::ReadData(size_t bytes, void* data) {
  125. DCHECK_EQ(memory_, base::bits::AlignUp(memory_, PaintOpWriter::Alignment()));
  126. if (bytes == 0)
  127. return;
  128. if (remaining_bytes_ < bytes) {
  129. SetInvalid(DeserializationError::kInsufficientRemainingBytes_ReadData);
  130. return;
  131. }
  132. memcpy(data, const_cast<const char*>(memory_), bytes);
  133. DidRead(bytes);
  134. }
  135. void PaintOpReader::ReadSize(size_t* size) {
  136. AlignMemory(8);
  137. if (!valid_)
  138. return;
  139. uint64_t size64 = 0;
  140. ReadSimple(&size64);
  141. *size = size64;
  142. }
  143. void PaintOpReader::Read(SkScalar* data) {
  144. ReadSimple(data);
  145. }
  146. void
  147. PaintOpReader::Read(uint8_t* data) {
  148. ReadSimple(data);
  149. }
  150. void PaintOpReader::Read(uint32_t* data) {
  151. ReadSimple(data);
  152. }
  153. void PaintOpReader::Read(uint64_t* data) {
  154. ReadSimple(data);
  155. }
  156. void PaintOpReader::Read(int32_t* data) {
  157. ReadSimple(data);
  158. }
  159. void PaintOpReader::Read(SkRect* rect) {
  160. ReadSimple(rect);
  161. }
  162. void PaintOpReader::Read(SkIRect* rect) {
  163. ReadSimple(rect);
  164. }
  165. void PaintOpReader::Read(SkRRect* rect) {
  166. ReadSimple(rect);
  167. }
  168. void PaintOpReader::Read(SkColor4f* color) {
  169. ReadSimple(color);
  170. }
  171. void PaintOpReader::Read(SkPath* path) {
  172. uint32_t path_id;
  173. ReadSimple(&path_id);
  174. if (!valid_)
  175. return;
  176. uint32_t entry_state_int = 0u;
  177. ReadSimple(&entry_state_int);
  178. if (entry_state_int > static_cast<uint32_t>(PaintCacheEntryState::kLast)) {
  179. valid_ = false;
  180. return;
  181. }
  182. auto entry_state = static_cast<PaintCacheEntryState>(entry_state_int);
  183. switch (entry_state) {
  184. case PaintCacheEntryState::kEmpty:
  185. return;
  186. case PaintCacheEntryState::kCached:
  187. if (!options_.paint_cache->GetPath(path_id, path))
  188. SetInvalid(DeserializationError::kMissingPaintCachePathEntry);
  189. return;
  190. case PaintCacheEntryState::kInlined:
  191. case PaintCacheEntryState::kInlinedDoNotCache: {
  192. size_t path_bytes = 0u;
  193. ReadSize(&path_bytes);
  194. if (path_bytes > remaining_bytes_)
  195. SetInvalid(
  196. DeserializationError::kInsufficientRemainingBytes_Read_SkPath);
  197. if (path_bytes == 0u)
  198. SetInvalid(DeserializationError::kZeroSkPathBytes);
  199. if (!valid_)
  200. return;
  201. auto* scratch = CopyScratchSpace(path_bytes);
  202. size_t bytes_read = path->readFromMemory(scratch, path_bytes);
  203. if (bytes_read == 0u) {
  204. SetInvalid(DeserializationError::kSkPathReadFromMemoryFailure);
  205. return;
  206. }
  207. if (entry_state == PaintCacheEntryState::kInlined) {
  208. options_.paint_cache->PutPath(path_id, *path);
  209. } else {
  210. // If we know that this path will only be drawn once, which is
  211. // implied by kInlinedDoNotCache, we signal to skia that it should not
  212. // do any caching either.
  213. path->setIsVolatile(true);
  214. }
  215. DidRead(path_bytes);
  216. return;
  217. }
  218. }
  219. }
  220. void PaintOpReader::Read(PaintFlags* flags) {
  221. ReadSimple(&flags->color_);
  222. Read(&flags->width_);
  223. Read(&flags->miter_limit_);
  224. Read(&flags->blend_mode_);
  225. ReadSimple(&flags->bitfields_uint_);
  226. ReadFlattenable(&flags->path_effect_, SkPathEffect::Deserialize,
  227. DeserializationError::kSkPathEffectUnflattenFailure);
  228. ReadFlattenable(&flags->mask_filter_, SkMaskFilter::Deserialize,
  229. DeserializationError::kSkMaskFilterUnflattenFailure);
  230. ReadFlattenable(&flags->color_filter_, SkColorFilter::Deserialize,
  231. DeserializationError::kSkColorFilterUnflattenFailure);
  232. if (enable_security_constraints_) {
  233. size_t bytes = 0;
  234. ReadSize(&bytes);
  235. if (bytes != 0u) {
  236. SetInvalid(DeserializationError::kDrawLooperForbidden);
  237. return;
  238. }
  239. } else {
  240. ReadFlattenable(&flags->draw_looper_, SkDrawLooper::Deserialize,
  241. DeserializationError::kSkDrawLooperUnflattenFailure);
  242. }
  243. Read(&flags->image_filter_);
  244. Read(&flags->shader_);
  245. }
  246. void PaintOpReader::Read(PaintImage* image) {
  247. uint8_t serialized_type_int = 0u;
  248. Read(&serialized_type_int);
  249. if (serialized_type_int >
  250. static_cast<uint8_t>(PaintOp::SerializedImageType::kLastType)) {
  251. SetInvalid(DeserializationError::kInvalidSerializedImageType);
  252. return;
  253. }
  254. auto serialized_type =
  255. static_cast<PaintOp::SerializedImageType>(serialized_type_int);
  256. if (serialized_type == PaintOp::SerializedImageType::kNoImage)
  257. return;
  258. if (enable_security_constraints_) {
  259. switch (serialized_type) {
  260. case PaintOp::SerializedImageType::kNoImage:
  261. NOTREACHED();
  262. return;
  263. case PaintOp::SerializedImageType::kImageData: {
  264. SkColorType color_type;
  265. Read(&color_type);
  266. uint32_t width;
  267. Read(&width);
  268. uint32_t height;
  269. Read(&height);
  270. size_t pixel_size;
  271. ReadSize(&pixel_size);
  272. if (!valid_)
  273. return;
  274. SkImageInfo image_info =
  275. SkImageInfo::Make(width, height, color_type, kPremul_SkAlphaType);
  276. if (pixel_size < image_info.computeMinByteSize()) {
  277. SetInvalid(DeserializationError::kInsufficientPixelData);
  278. return;
  279. }
  280. const volatile void* pixel_data = ExtractReadableMemory(pixel_size);
  281. if (!valid_)
  282. return;
  283. SkPixmap pixmap(image_info, const_cast<const void*>(pixel_data),
  284. image_info.minRowBytes());
  285. *image = PaintImageBuilder::WithDefault()
  286. .set_id(PaintImage::GetNextId())
  287. .set_texture_image(SkImage::MakeRasterCopy(pixmap),
  288. PaintImage::kNonLazyStableId)
  289. .TakePaintImage();
  290. }
  291. return;
  292. case PaintOp::SerializedImageType::kTransferCacheEntry:
  293. case PaintOp::SerializedImageType::kMailbox:
  294. SetInvalid(DeserializationError::kForbiddenSerializedImageType);
  295. return;
  296. }
  297. NOTREACHED();
  298. return;
  299. }
  300. if (serialized_type == PaintOp::SerializedImageType::kMailbox) {
  301. if (!options_.shared_image_provider) {
  302. SetInvalid(DeserializationError::kMissingSharedImageProvider);
  303. return;
  304. }
  305. gpu::Mailbox mailbox;
  306. Read(&mailbox);
  307. if (mailbox.IsZero()) {
  308. SetInvalid(DeserializationError::kZeroMailbox);
  309. return;
  310. }
  311. SharedImageProvider::Error error;
  312. sk_sp<SkImage> sk_image =
  313. options_.shared_image_provider->OpenSharedImageForRead(mailbox, error);
  314. if (error != SharedImageProvider::Error::kNoError) {
  315. switch (error) {
  316. case SharedImageProvider::Error::kNoAccess:
  317. SetInvalid(DeserializationError::kSharedImageProviderNoAccess);
  318. break;
  319. case SharedImageProvider::Error::kSkImageCreationFailed:
  320. SetInvalid(
  321. DeserializationError::kSharedImageProviderSkImageCreationFailed);
  322. break;
  323. case SharedImageProvider::Error::kUnknownMailbox:
  324. SetInvalid(DeserializationError::kSharedImageProviderUnknownMailbox);
  325. break;
  326. default:
  327. NOTREACHED();
  328. break;
  329. }
  330. SetInvalid(DeserializationError::kSharedImageOpenFailure);
  331. return;
  332. }
  333. DCHECK(sk_image);
  334. *image = PaintImageBuilder::WithDefault()
  335. .set_id(PaintImage::GetNextId())
  336. .set_texture_image(std::move(sk_image),
  337. PaintImage::kNonLazyStableId)
  338. .TakePaintImage();
  339. return;
  340. }
  341. if (serialized_type != PaintOp::SerializedImageType::kTransferCacheEntry) {
  342. SetInvalid(DeserializationError::kUnexpectedSerializedImageType);
  343. return;
  344. }
  345. uint32_t transfer_cache_entry_id;
  346. ReadSimple(&transfer_cache_entry_id);
  347. if (!valid_)
  348. return;
  349. bool needs_mips;
  350. ReadSimple(&needs_mips);
  351. if (!valid_)
  352. return;
  353. // If we encountered a decode failure, we may write an invalid id for the
  354. // image. In these cases, just return, leaving the image as nullptr.
  355. if (transfer_cache_entry_id == kInvalidImageTransferCacheEntryId)
  356. return;
  357. // The transfer cache entry for an image may not exist if the upload fails.
  358. if (auto* entry =
  359. options_.transfer_cache->GetEntryAs<ServiceImageTransferCacheEntry>(
  360. transfer_cache_entry_id)) {
  361. if (needs_mips)
  362. entry->EnsureMips();
  363. *image =
  364. PaintImageBuilder::WithDefault()
  365. .set_id(PaintImage::GetNextId())
  366. .set_texture_image(entry->image(), PaintImage::kNonLazyStableId)
  367. .TakePaintImage();
  368. }
  369. }
  370. void PaintOpReader::Read(sk_sp<SkData>* data) {
  371. size_t bytes = 0;
  372. ReadSize(&bytes);
  373. if (remaining_bytes_ < bytes)
  374. SetInvalid(DeserializationError::kInsufficientRemainingBytes_Read_SkData);
  375. if (!valid_)
  376. return;
  377. // Separate out empty vs not valid cases.
  378. if (bytes == 0) {
  379. bool has_data = false;
  380. Read(&has_data);
  381. if (has_data)
  382. *data = SkData::MakeEmpty();
  383. return;
  384. }
  385. // This is safe to cast away the volatile as it is just a memcpy internally.
  386. *data = SkData::MakeWithCopy(const_cast<const char*>(memory_), bytes);
  387. DidRead(bytes);
  388. }
  389. void PaintOpReader::Read(sk_sp<SkColorSpace>* color_space) {
  390. size_t size = 0;
  391. ReadSize(&size);
  392. if (remaining_bytes_ < size)
  393. valid_ = false;
  394. if (!valid_ || size == 0)
  395. return;
  396. auto* scratch = CopyScratchSpace(size);
  397. *color_space = SkColorSpace::Deserialize(scratch, size);
  398. // If this had non-zero bytes, it should be a valid color space.
  399. if (!color_space)
  400. SetInvalid(DeserializationError::kSkColorSpaceDeserializeFailure);
  401. DidRead(size);
  402. }
  403. void PaintOpReader::Read(sk_sp<GrSlug>* slug) {
  404. AssertAlignment(PaintOpWriter::Alignment());
  405. size_t data_bytes = 0u;
  406. ReadSize(&data_bytes);
  407. if (data_bytes == 0) {
  408. *slug = nullptr;
  409. return;
  410. }
  411. if (remaining_bytes_ < data_bytes) {
  412. SetInvalid(DeserializationError::kInsufficientRemainingBytes_Read_GrSlug);
  413. return;
  414. }
  415. *slug = GrSlug::Deserialize(const_cast<const char*>(memory_), data_bytes,
  416. options_.strike_client);
  417. DidRead(data_bytes);
  418. if (!*slug) {
  419. SetInvalid(DeserializationError::kGrSlugDeserializeFailure);
  420. return;
  421. }
  422. }
  423. void PaintOpReader::Read(sk_sp<PaintShader>* shader) {
  424. bool has_shader = false;
  425. ReadSimple(&has_shader);
  426. if (!has_shader) {
  427. *shader = nullptr;
  428. return;
  429. }
  430. PaintShader::Type shader_type;
  431. ReadSimple(&shader_type);
  432. // Avoid creating a shader if something is invalid.
  433. if (!valid_ || !IsValidPaintShaderType(shader_type)) {
  434. SetInvalid(DeserializationError::kInvalidPaintShaderType);
  435. return;
  436. }
  437. *shader = sk_sp<PaintShader>(new PaintShader(shader_type));
  438. PaintShader& ref = **shader;
  439. ReadSimple(&ref.flags_);
  440. ReadSimple(&ref.end_radius_);
  441. ReadSimple(&ref.start_radius_);
  442. Read(&ref.tx_);
  443. Read(&ref.ty_);
  444. ReadSimple(&ref.fallback_color_);
  445. ReadSimple(&ref.scaling_behavior_);
  446. if (!IsValidPaintShaderScalingBehavior(ref.scaling_behavior_))
  447. SetInvalid(DeserializationError::kInvalidPaintShaderScalingBehavior);
  448. bool has_local_matrix = false;
  449. ReadSimple(&has_local_matrix);
  450. if (has_local_matrix) {
  451. ref.local_matrix_.emplace();
  452. Read(&*ref.local_matrix_);
  453. }
  454. ReadSimple(&ref.center_);
  455. ReadSimple(&ref.tile_);
  456. ReadSimple(&ref.start_point_);
  457. ReadSimple(&ref.end_point_);
  458. ReadSimple(&ref.start_degrees_);
  459. ReadSimple(&ref.end_degrees_);
  460. Read(&ref.image_);
  461. bool has_record = false;
  462. ReadSimple(&has_record);
  463. uint32_t shader_id = PaintShader::kInvalidRecordShaderId;
  464. size_t shader_size = 0;
  465. if (has_record) {
  466. if (shader_type != PaintShader::Type::kPaintRecord) {
  467. SetInvalid(DeserializationError::kUnexpectedPaintShaderType);
  468. return;
  469. }
  470. Read(&shader_id);
  471. if (shader_id == PaintShader::kInvalidRecordShaderId) {
  472. SetInvalid(DeserializationError::kInvalidRecordShaderId);
  473. return;
  474. }
  475. // Track dependent transfer cache entries to make cached shader size
  476. // more realistic.
  477. size_t pre_size = options_.transfer_cache->GetTotalEntrySizes();
  478. size_t record_size = Read(&ref.record_);
  479. size_t post_size = options_.transfer_cache->GetTotalEntrySizes();
  480. shader_size = post_size - pre_size + record_size;
  481. ref.id_ = shader_id;
  482. }
  483. decltype(ref.colors_)::size_type colors_size = 0;
  484. ReadSize(&colors_size);
  485. // If there are too many colors, abort.
  486. if (colors_size > remaining_bytes_) {
  487. SetInvalid(DeserializationError::
  488. kInsufficientRemainingBytes_Read_PaintShader_ColorSize);
  489. return;
  490. }
  491. size_t colors_bytes =
  492. colors_size * (colors_size > 0 ? sizeof(ref.colors_[0]) : 0u);
  493. if (colors_bytes > remaining_bytes_) {
  494. SetInvalid(DeserializationError::
  495. kInsufficientRemainingBytes_Read_PaintShader_ColorBytes);
  496. return;
  497. }
  498. ref.colors_.resize(colors_size);
  499. ReadData(colors_bytes, ref.colors_.data());
  500. decltype(ref.positions_)::size_type positions_size = 0;
  501. ReadSize(&positions_size);
  502. // Positions are optional. If they exist, they have the same count as colors.
  503. if (positions_size > 0 && positions_size != colors_size) {
  504. SetInvalid(DeserializationError::kInvalidPaintShaderPositionsSize);
  505. return;
  506. }
  507. size_t positions_bytes = positions_size * sizeof(SkScalar);
  508. if (positions_bytes > remaining_bytes_) {
  509. SetInvalid(DeserializationError::
  510. kInsufficientRemainingBytes_Read_PaintShader_Positions);
  511. return;
  512. }
  513. ref.positions_.resize(positions_size);
  514. ReadData(positions_size * sizeof(SkScalar), ref.positions_.data());
  515. // We don't write the cached shader, so don't attempt to read it either.
  516. if (!(*shader)->IsValid()) {
  517. SetInvalid(DeserializationError::kInvalidPaintShader);
  518. return;
  519. }
  520. // All shader types but records are done.
  521. if (shader_type != PaintShader::Type::kPaintRecord) {
  522. (*shader)->ResolveSkObjects();
  523. return;
  524. }
  525. // Record shaders have shader ids. Attempt to use cached versions of
  526. // these so that Skia can cache based on SkPictureShader::fUniqueId.
  527. // These shaders are always serialized (and assumed to not be large
  528. // records). Handling this edge case in this roundabout way prevents
  529. // transfer cache entries from needing to depend on other transfer cache
  530. // entries.
  531. auto* entry =
  532. options_.transfer_cache->GetEntryAs<ServiceShaderTransferCacheEntry>(
  533. shader_id);
  534. // Only consider entries that use the same scale. This limits the service
  535. // side transfer cache to only having one entry per shader but this will hit
  536. // the common case of enabling Skia reuse.
  537. if (entry && entry->shader()->tile_ == ref.tile_) {
  538. DCHECK(!ref.sk_cached_picture_);
  539. ref.sk_cached_picture_ = entry->shader()->sk_cached_picture_;
  540. } else {
  541. ref.ResolveSkObjects();
  542. DCHECK(ref.sk_cached_picture_);
  543. options_.transfer_cache->CreateLocalEntry(
  544. shader_id, std::make_unique<ServiceShaderTransferCacheEntry>(
  545. *shader, shader_size));
  546. }
  547. }
  548. void PaintOpReader::Read(SkMatrix* matrix) {
  549. ReadSimple(matrix);
  550. FixupMatrixPostSerialization(matrix);
  551. }
  552. void PaintOpReader::Read(SkM44* matrix) {
  553. ReadSimple(matrix);
  554. }
  555. void PaintOpReader::Read(SkSamplingOptions* sampling) {
  556. bool useCubic;
  557. Read(&useCubic);
  558. if (useCubic) {
  559. SkCubicResampler cubic;
  560. Read(&cubic.B);
  561. Read(&cubic.C);
  562. *sampling = SkSamplingOptions(cubic);
  563. } else {
  564. SkFilterMode filter;
  565. SkMipmapMode mipmap;
  566. Read(&filter);
  567. Read(&mipmap);
  568. *sampling = SkSamplingOptions(filter, mipmap);
  569. }
  570. }
  571. void PaintOpReader::Read(SkYUVColorSpace* yuv_color_space) {
  572. uint32_t raw_yuv_color_space = kIdentity_SkYUVColorSpace;
  573. ReadSimple(&raw_yuv_color_space);
  574. if (raw_yuv_color_space > kLastEnum_SkYUVColorSpace) {
  575. SetInvalid(DeserializationError::kInvalidSkYUVColorSpace);
  576. return;
  577. }
  578. *yuv_color_space = static_cast<SkYUVColorSpace>(raw_yuv_color_space);
  579. }
  580. void PaintOpReader::Read(SkYUVAInfo::PlaneConfig* plane_config) {
  581. uint32_t raw_plane_config =
  582. static_cast<uint32_t>(SkYUVAInfo::PlaneConfig::kUnknown);
  583. ReadSimple(&raw_plane_config);
  584. if (raw_plane_config >
  585. static_cast<uint32_t>(SkYUVAInfo::PlaneConfig::kLast)) {
  586. SetInvalid(DeserializationError::kInvalidPlaneConfig);
  587. return;
  588. }
  589. *plane_config = static_cast<SkYUVAInfo::PlaneConfig>(raw_plane_config);
  590. }
  591. void PaintOpReader::Read(SkYUVAInfo::Subsampling* subsampling) {
  592. uint32_t raw_subsampling =
  593. static_cast<uint32_t>(SkYUVAInfo::Subsampling::kUnknown);
  594. ReadSimple(&raw_subsampling);
  595. if (raw_subsampling > static_cast<uint32_t>(SkYUVAInfo::Subsampling::kLast)) {
  596. SetInvalid(DeserializationError::kInvalidSubsampling);
  597. return;
  598. }
  599. *subsampling = static_cast<SkYUVAInfo::Subsampling>(raw_subsampling);
  600. }
  601. void PaintOpReader::Read(gpu::Mailbox* mailbox) {
  602. ReadData(sizeof(gpu::Mailbox::Name), (*mailbox).name);
  603. }
  604. void PaintOpReader::Read(scoped_refptr<SkottieWrapper>* skottie) {
  605. if (!options_.is_privileged) {
  606. valid_ = false;
  607. return;
  608. }
  609. uint32_t transfer_cache_entry_id;
  610. ReadSimple(&transfer_cache_entry_id);
  611. if (!valid_)
  612. return;
  613. auto* entry =
  614. options_.transfer_cache->GetEntryAs<ServiceSkottieTransferCacheEntry>(
  615. transfer_cache_entry_id);
  616. if (entry) {
  617. *skottie = entry->skottie();
  618. } else {
  619. valid_ = false;
  620. }
  621. size_t bytes_to_skip = 0u;
  622. ReadSize(&bytes_to_skip);
  623. if (!valid_)
  624. return;
  625. if (bytes_to_skip > remaining_bytes_) {
  626. valid_ = false;
  627. return;
  628. }
  629. DidRead(bytes_to_skip);
  630. }
  631. void PaintOpReader::AlignMemory(size_t alignment) {
  632. size_t padding = base::bits::AlignUp(memory_, alignment) - memory_;
  633. if (padding > remaining_bytes_)
  634. SetInvalid(DeserializationError::kInsufficientRemainingBytes_AlignMemory);
  635. memory_ += padding;
  636. remaining_bytes_ -= padding;
  637. }
  638. // Don't inline this function so that crash reports can show the caller.
  639. NOINLINE void PaintOpReader::SetInvalid(DeserializationError error) {
  640. static crash_reporter::CrashKeyString<4> deserialization_error_crash_key(
  641. "PaintOpReader deserialization error");
  642. base::UmaHistogramEnumeration("GPU.PaintOpReader.DeserializationError",
  643. error);
  644. if (valid_ && options_.crash_dump_on_failure && base::RandInt(1, 10) == 1) {
  645. crash_reporter::ScopedCrashKeyString crash_key_scope(
  646. &deserialization_error_crash_key,
  647. base::NumberToString(static_cast<int>(error)));
  648. base::debug::DumpWithoutCrashing();
  649. }
  650. valid_ = false;
  651. }
  652. const volatile void* PaintOpReader::ExtractReadableMemory(size_t bytes) {
  653. if (remaining_bytes_ < bytes)
  654. SetInvalid(DeserializationError::
  655. kInsufficientRemainingBytes_ExtractReadableMemory);
  656. if (!valid_)
  657. return nullptr;
  658. if (bytes == 0)
  659. return nullptr;
  660. const volatile void* extracted_memory = memory_;
  661. DidRead(bytes);
  662. return extracted_memory;
  663. }
  664. void PaintOpReader::Read(sk_sp<PaintFilter>* filter) {
  665. PaintFilter::Type type;
  666. ReadEnum(&type);
  667. if (!valid_)
  668. return;
  669. if (type == PaintFilter::Type::kNullFilter) {
  670. *filter = nullptr;
  671. return;
  672. }
  673. uint32_t has_crop_rect = 0;
  674. absl::optional<PaintFilter::CropRect> crop_rect;
  675. ReadSimple(&has_crop_rect);
  676. if (has_crop_rect) {
  677. SkRect rect = SkRect::MakeEmpty();
  678. ReadSimple(&rect);
  679. crop_rect.emplace(rect);
  680. }
  681. AssertAlignment(PaintOpWriter::Alignment());
  682. switch (type) {
  683. case PaintFilter::Type::kNullFilter:
  684. NOTREACHED();
  685. break;
  686. case PaintFilter::Type::kColorFilter:
  687. ReadColorFilterPaintFilter(filter, crop_rect);
  688. break;
  689. case PaintFilter::Type::kBlur:
  690. ReadBlurPaintFilter(filter, crop_rect);
  691. break;
  692. case PaintFilter::Type::kDropShadow:
  693. ReadDropShadowPaintFilter(filter, crop_rect);
  694. break;
  695. case PaintFilter::Type::kMagnifier:
  696. ReadMagnifierPaintFilter(filter, crop_rect);
  697. break;
  698. case PaintFilter::Type::kCompose:
  699. ReadComposePaintFilter(filter, crop_rect);
  700. break;
  701. case PaintFilter::Type::kAlphaThreshold:
  702. ReadAlphaThresholdPaintFilter(filter, crop_rect);
  703. break;
  704. case PaintFilter::Type::kXfermode:
  705. ReadXfermodePaintFilter(filter, crop_rect);
  706. break;
  707. case PaintFilter::Type::kArithmetic:
  708. ReadArithmeticPaintFilter(filter, crop_rect);
  709. break;
  710. case PaintFilter::Type::kMatrixConvolution:
  711. ReadMatrixConvolutionPaintFilter(filter, crop_rect);
  712. break;
  713. case PaintFilter::Type::kDisplacementMapEffect:
  714. ReadDisplacementMapEffectPaintFilter(filter, crop_rect);
  715. break;
  716. case PaintFilter::Type::kImage:
  717. ReadImagePaintFilter(filter, crop_rect);
  718. break;
  719. case PaintFilter::Type::kPaintRecord:
  720. ReadRecordPaintFilter(filter, crop_rect);
  721. break;
  722. case PaintFilter::Type::kMerge:
  723. ReadMergePaintFilter(filter, crop_rect);
  724. break;
  725. case PaintFilter::Type::kMorphology:
  726. ReadMorphologyPaintFilter(filter, crop_rect);
  727. break;
  728. case PaintFilter::Type::kOffset:
  729. ReadOffsetPaintFilter(filter, crop_rect);
  730. break;
  731. case PaintFilter::Type::kTile:
  732. ReadTilePaintFilter(filter, crop_rect);
  733. break;
  734. case PaintFilter::Type::kTurbulence:
  735. ReadTurbulencePaintFilter(filter, crop_rect);
  736. break;
  737. case PaintFilter::Type::kShader:
  738. ReadShaderPaintFilter(filter, crop_rect);
  739. break;
  740. case PaintFilter::Type::kMatrix:
  741. ReadMatrixPaintFilter(filter, crop_rect);
  742. break;
  743. case PaintFilter::Type::kLightingDistant:
  744. ReadLightingDistantPaintFilter(filter, crop_rect);
  745. break;
  746. case PaintFilter::Type::kLightingPoint:
  747. ReadLightingPointPaintFilter(filter, crop_rect);
  748. break;
  749. case PaintFilter::Type::kLightingSpot:
  750. ReadLightingSpotPaintFilter(filter, crop_rect);
  751. break;
  752. case PaintFilter::Type::kStretch:
  753. ReadStretchPaintFilter(filter, crop_rect);
  754. break;
  755. }
  756. }
  757. void PaintOpReader::ReadColorFilterPaintFilter(
  758. sk_sp<PaintFilter>* filter,
  759. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  760. sk_sp<SkColorFilter> color_filter;
  761. sk_sp<PaintFilter> input;
  762. ReadFlattenable(&color_filter, SkColorFilter::Deserialize,
  763. DeserializationError::kSkColorFilterUnflattenFailure);
  764. Read(&input);
  765. if (!color_filter)
  766. SetInvalid(DeserializationError::kZeroSkColorFilterBytes);
  767. if (!valid_)
  768. return;
  769. filter->reset(new ColorFilterPaintFilter(std::move(color_filter),
  770. std::move(input),
  771. base::OptionalOrNullptr(crop_rect)));
  772. }
  773. void PaintOpReader::ReadBlurPaintFilter(
  774. sk_sp<PaintFilter>* filter,
  775. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  776. SkScalar sigma_x = 0.f;
  777. SkScalar sigma_y = 0.f;
  778. SkTileMode tile_mode;
  779. sk_sp<PaintFilter> input;
  780. Read(&sigma_x);
  781. Read(&sigma_y);
  782. Read(&tile_mode);
  783. Read(&input);
  784. if (!valid_)
  785. return;
  786. filter->reset(new BlurPaintFilter(sigma_x, sigma_y, tile_mode,
  787. std::move(input),
  788. base::OptionalOrNullptr(crop_rect)));
  789. }
  790. void PaintOpReader::ReadDropShadowPaintFilter(
  791. sk_sp<PaintFilter>* filter,
  792. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  793. SkScalar dx = 0.f;
  794. SkScalar dy = 0.f;
  795. SkScalar sigma_x = 0.f;
  796. SkScalar sigma_y = 0.f;
  797. SkColor color = SK_ColorBLACK;
  798. DropShadowPaintFilter::ShadowMode shadow_mode;
  799. sk_sp<PaintFilter> input;
  800. Read(&dx);
  801. Read(&dy);
  802. Read(&sigma_x);
  803. Read(&sigma_y);
  804. Read(&color);
  805. ReadEnum(&shadow_mode);
  806. Read(&input);
  807. if (!valid_)
  808. return;
  809. // TODO(crbug/1308932): Remove FromColor and make all SkColor4f.
  810. filter->reset(new DropShadowPaintFilter(
  811. dx, dy, sigma_x, sigma_y, SkColor4f::FromColor(color), shadow_mode,
  812. std::move(input), base::OptionalOrNullptr(crop_rect)));
  813. }
  814. void PaintOpReader::ReadMagnifierPaintFilter(
  815. sk_sp<PaintFilter>* filter,
  816. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  817. SkRect src_rect = SkRect::MakeEmpty();
  818. SkScalar inset = 0.f;
  819. sk_sp<PaintFilter> input;
  820. Read(&src_rect);
  821. Read(&inset);
  822. Read(&input);
  823. if (!valid_)
  824. return;
  825. filter->reset(new MagnifierPaintFilter(src_rect, inset, std::move(input),
  826. base::OptionalOrNullptr(crop_rect)));
  827. }
  828. void PaintOpReader::ReadComposePaintFilter(
  829. sk_sp<PaintFilter>* filter,
  830. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  831. sk_sp<PaintFilter> outer;
  832. sk_sp<PaintFilter> inner;
  833. Read(&outer);
  834. Read(&inner);
  835. if (!valid_)
  836. return;
  837. filter->reset(new ComposePaintFilter(std::move(outer), std::move(inner)));
  838. }
  839. void PaintOpReader::ReadAlphaThresholdPaintFilter(
  840. sk_sp<PaintFilter>* filter,
  841. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  842. SkRegion region;
  843. SkScalar inner_min = 0.f;
  844. SkScalar outer_max = 0.f;
  845. sk_sp<PaintFilter> input;
  846. Read(&region);
  847. ReadSimple(&inner_min);
  848. ReadSimple(&outer_max);
  849. Read(&input);
  850. if (!valid_)
  851. return;
  852. filter->reset(new AlphaThresholdPaintFilter(
  853. region, inner_min, outer_max, std::move(input),
  854. base::OptionalOrNullptr(crop_rect)));
  855. }
  856. void PaintOpReader::ReadXfermodePaintFilter(
  857. sk_sp<PaintFilter>* filter,
  858. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  859. SkBlendMode blend_mode;
  860. sk_sp<PaintFilter> background;
  861. sk_sp<PaintFilter> foreground;
  862. Read(&blend_mode);
  863. Read(&background);
  864. Read(&foreground);
  865. if (!valid_)
  866. return;
  867. filter->reset(new XfermodePaintFilter(blend_mode, std::move(background),
  868. std::move(foreground),
  869. base::OptionalOrNullptr(crop_rect)));
  870. }
  871. void PaintOpReader::ReadArithmeticPaintFilter(
  872. sk_sp<PaintFilter>* filter,
  873. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  874. float k1 = 0.f;
  875. float k2 = 0.f;
  876. float k3 = 0.f;
  877. float k4 = 0.f;
  878. bool enforce_pm_color = false;
  879. sk_sp<PaintFilter> background;
  880. sk_sp<PaintFilter> foreground;
  881. Read(&k1);
  882. Read(&k2);
  883. Read(&k3);
  884. Read(&k4);
  885. Read(&enforce_pm_color);
  886. Read(&background);
  887. Read(&foreground);
  888. if (!valid_)
  889. return;
  890. filter->reset(new ArithmeticPaintFilter(
  891. k1, k2, k3, k4, enforce_pm_color, std::move(background),
  892. std::move(foreground), base::OptionalOrNullptr(crop_rect)));
  893. }
  894. void PaintOpReader::ReadMatrixConvolutionPaintFilter(
  895. sk_sp<PaintFilter>* filter,
  896. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  897. SkISize kernel_size = SkISize::MakeEmpty();
  898. SkScalar gain = 0.f;
  899. SkScalar bias = 0.f;
  900. SkIPoint kernel_offset = SkIPoint::Make(0, 0);
  901. SkTileMode tile_mode;
  902. bool convolve_alpha = false;
  903. sk_sp<PaintFilter> input;
  904. ReadSimple(&kernel_size);
  905. if (!valid_)
  906. return;
  907. auto size =
  908. static_cast<size_t>(sk_64_mul(kernel_size.width(), kernel_size.height()));
  909. if (size > remaining_bytes_) {
  910. SetInvalid(
  911. DeserializationError::
  912. kInsufficientRemainingBytes_ReadMatrixConvolutionPaintFilter);
  913. return;
  914. }
  915. std::vector<SkScalar> kernel(size);
  916. for (size_t i = 0; i < size; ++i)
  917. Read(&kernel[i]);
  918. Read(&gain);
  919. Read(&bias);
  920. ReadSimple(&kernel_offset);
  921. Read(&tile_mode);
  922. Read(&convolve_alpha);
  923. Read(&input);
  924. if (!valid_)
  925. return;
  926. filter->reset(new MatrixConvolutionPaintFilter(
  927. kernel_size, kernel.data(), gain, bias, kernel_offset, tile_mode,
  928. convolve_alpha, std::move(input), base::OptionalOrNullptr(crop_rect)));
  929. }
  930. void PaintOpReader::ReadDisplacementMapEffectPaintFilter(
  931. sk_sp<PaintFilter>* filter,
  932. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  933. SkColorChannel channel_x;
  934. SkColorChannel channel_y;
  935. SkScalar scale = 0.f;
  936. sk_sp<PaintFilter> displacement;
  937. sk_sp<PaintFilter> color;
  938. ReadEnum<SkColorChannel, SkColorChannel::kA>(&channel_x);
  939. ReadEnum<SkColorChannel, SkColorChannel::kA>(&channel_y);
  940. Read(&scale);
  941. Read(&displacement);
  942. Read(&color);
  943. if (!valid_)
  944. return;
  945. filter->reset(new DisplacementMapEffectPaintFilter(
  946. channel_x, channel_y, scale, std::move(displacement), std::move(color),
  947. base::OptionalOrNullptr(crop_rect)));
  948. }
  949. void PaintOpReader::ReadImagePaintFilter(
  950. sk_sp<PaintFilter>* filter,
  951. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  952. PaintImage image;
  953. Read(&image);
  954. if (!image) {
  955. SetInvalid(DeserializationError::kReadImageFailure);
  956. return;
  957. }
  958. SkRect src_rect;
  959. Read(&src_rect);
  960. SkRect dst_rect;
  961. Read(&dst_rect);
  962. PaintFlags::FilterQuality quality;
  963. Read(&quality);
  964. if (!valid_)
  965. return;
  966. filter->reset(
  967. new ImagePaintFilter(std::move(image), src_rect, dst_rect, quality));
  968. }
  969. void PaintOpReader::ReadRecordPaintFilter(
  970. sk_sp<PaintFilter>* filter,
  971. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  972. bool has_filter = false;
  973. ReadSimple(&has_filter);
  974. if (!has_filter) {
  975. *filter = nullptr;
  976. return;
  977. }
  978. SkRect record_bounds = SkRect::MakeEmpty();
  979. gfx::SizeF raster_scale = {0.f, 0.f};
  980. PaintShader::ScalingBehavior scaling_behavior =
  981. PaintShader::ScalingBehavior::kRasterAtScale;
  982. sk_sp<PaintRecord> record;
  983. ReadSimple(&record_bounds);
  984. ReadSimple(&raster_scale);
  985. if (raster_scale.width() <= 0.f || raster_scale.height() <= 0.f) {
  986. SetInvalid(DeserializationError::kInvalidRasterScale);
  987. return;
  988. }
  989. ReadSimple(&scaling_behavior);
  990. if (!IsValidPaintShaderScalingBehavior(scaling_behavior)) {
  991. SetInvalid(DeserializationError::kInvalidPaintShaderScalingBehavior);
  992. return;
  993. }
  994. // RecordPaintFilter also requires kRasterAtScale to have {1.f, 1.f} as the
  995. // raster_scale, since that is intended for kFixedScale
  996. if (scaling_behavior == PaintShader::ScalingBehavior::kRasterAtScale &&
  997. (raster_scale.width() != 1.f || raster_scale.height() != 1.f)) {
  998. SetInvalid(DeserializationError::kInvalidRasterScale);
  999. return;
  1000. }
  1001. Read(&record);
  1002. if (!valid_)
  1003. return;
  1004. filter->reset(new RecordPaintFilter(std::move(record), record_bounds,
  1005. raster_scale, scaling_behavior));
  1006. }
  1007. void PaintOpReader::ReadMergePaintFilter(
  1008. sk_sp<PaintFilter>* filter,
  1009. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1010. size_t input_count = 0;
  1011. ReadSize(&input_count);
  1012. // The minimum size for a serialized filter is 4 bytes (a zero uint32_t to
  1013. // indicate a null filter). Make sure the |input_count| doesn't exceed the
  1014. // maximum number of filters possible for the remaining data.
  1015. const size_t max_filters = remaining_bytes_ / 4u;
  1016. if (input_count > max_filters)
  1017. SetInvalid(DeserializationError::kPaintFilterHasTooManyInputs);
  1018. if (!valid_)
  1019. return;
  1020. std::vector<sk_sp<PaintFilter>> inputs(input_count);
  1021. for (auto& input : inputs)
  1022. Read(&input);
  1023. if (!valid_)
  1024. return;
  1025. filter->reset(new MergePaintFilter(inputs.data(),
  1026. static_cast<int>(input_count),
  1027. base::OptionalOrNullptr(crop_rect)));
  1028. }
  1029. void PaintOpReader::ReadMorphologyPaintFilter(
  1030. sk_sp<PaintFilter>* filter,
  1031. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1032. MorphologyPaintFilter::MorphType morph_type;
  1033. float radius_x = 0;
  1034. float radius_y = 0;
  1035. sk_sp<PaintFilter> input;
  1036. ReadEnum(&morph_type);
  1037. Read(&radius_x);
  1038. Read(&radius_y);
  1039. Read(&input);
  1040. if (!valid_)
  1041. return;
  1042. filter->reset(new MorphologyPaintFilter(morph_type, radius_x, radius_y,
  1043. std::move(input),
  1044. base::OptionalOrNullptr(crop_rect)));
  1045. }
  1046. void PaintOpReader::ReadOffsetPaintFilter(
  1047. sk_sp<PaintFilter>* filter,
  1048. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1049. SkScalar dx = 0.f;
  1050. SkScalar dy = 0.f;
  1051. sk_sp<PaintFilter> input;
  1052. Read(&dx);
  1053. Read(&dy);
  1054. Read(&input);
  1055. if (!valid_)
  1056. return;
  1057. filter->reset(new OffsetPaintFilter(dx, dy, std::move(input),
  1058. base::OptionalOrNullptr(crop_rect)));
  1059. }
  1060. void PaintOpReader::ReadTilePaintFilter(
  1061. sk_sp<PaintFilter>* filter,
  1062. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1063. SkRect src = SkRect::MakeEmpty();
  1064. SkRect dst = SkRect::MakeEmpty();
  1065. sk_sp<PaintFilter> input;
  1066. Read(&src);
  1067. Read(&dst);
  1068. Read(&input);
  1069. if (!valid_)
  1070. return;
  1071. filter->reset(new TilePaintFilter(src, dst, std::move(input)));
  1072. }
  1073. void PaintOpReader::ReadTurbulencePaintFilter(
  1074. sk_sp<PaintFilter>* filter,
  1075. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1076. TurbulencePaintFilter::TurbulenceType turbulence_type;
  1077. SkScalar base_frequency_x = 0.f;
  1078. SkScalar base_frequency_y = 0.f;
  1079. int num_octaves = 0;
  1080. SkScalar seed = 0.f;
  1081. SkISize tile_size = SkISize::MakeEmpty();
  1082. ReadEnum(&turbulence_type);
  1083. Read(&base_frequency_x);
  1084. Read(&base_frequency_y);
  1085. Read(&num_octaves);
  1086. Read(&seed);
  1087. ReadSimple(&tile_size);
  1088. if (!valid_)
  1089. return;
  1090. filter->reset(new TurbulencePaintFilter(
  1091. turbulence_type, base_frequency_x, base_frequency_y, num_octaves, seed,
  1092. &tile_size, base::OptionalOrNullptr(crop_rect)));
  1093. }
  1094. void PaintOpReader::ReadShaderPaintFilter(
  1095. sk_sp<PaintFilter>* filter,
  1096. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1097. using Dither = SkImageFilters::Dither;
  1098. sk_sp<PaintShader> shader;
  1099. uint8_t alpha = 255;
  1100. PaintFlags::FilterQuality quality = PaintFlags::FilterQuality::kNone;
  1101. Dither dither = Dither::kNo;
  1102. Read(&shader);
  1103. Read(&alpha);
  1104. Read(&quality);
  1105. ReadEnum<Dither, Dither::kYes>(&dither);
  1106. if (!shader || !valid_)
  1107. return;
  1108. filter->reset(new ShaderPaintFilter(std::move(shader), alpha, quality, dither,
  1109. base::OptionalOrNullptr(crop_rect)));
  1110. }
  1111. void PaintOpReader::ReadMatrixPaintFilter(
  1112. sk_sp<PaintFilter>* filter,
  1113. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1114. SkMatrix matrix = SkMatrix::I();
  1115. PaintFlags::FilterQuality filter_quality = PaintFlags::FilterQuality::kNone;
  1116. sk_sp<PaintFilter> input;
  1117. Read(&matrix);
  1118. Read(&filter_quality);
  1119. Read(&input);
  1120. if (!valid_)
  1121. return;
  1122. filter->reset(
  1123. new MatrixPaintFilter(matrix, filter_quality, std::move(input)));
  1124. }
  1125. void PaintOpReader::ReadLightingDistantPaintFilter(
  1126. sk_sp<PaintFilter>* filter,
  1127. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1128. PaintFilter::LightingType lighting_type;
  1129. SkPoint3 direction = SkPoint3::Make(0.f, 0.f, 0.f);
  1130. SkColor light_color = SK_ColorBLACK;
  1131. SkScalar surface_scale = 0.f;
  1132. SkScalar kconstant = 0.f;
  1133. SkScalar shininess = 0.f;
  1134. sk_sp<PaintFilter> input;
  1135. ReadEnum(&lighting_type);
  1136. ReadSimple(&direction);
  1137. Read(&light_color);
  1138. Read(&surface_scale);
  1139. Read(&kconstant);
  1140. Read(&shininess);
  1141. Read(&input);
  1142. if (!valid_)
  1143. return;
  1144. // TODO(crbug/1308932): Remove FromColor and make all SkColor4f.
  1145. filter->reset(new LightingDistantPaintFilter(
  1146. lighting_type, direction, SkColor4f::FromColor(light_color),
  1147. surface_scale, kconstant, shininess, std::move(input),
  1148. base::OptionalOrNullptr(crop_rect)));
  1149. }
  1150. void PaintOpReader::ReadLightingPointPaintFilter(
  1151. sk_sp<PaintFilter>* filter,
  1152. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1153. PaintFilter::LightingType lighting_type;
  1154. SkPoint3 location = SkPoint3::Make(0.f, 0.f, 0.f);
  1155. SkColor light_color = SK_ColorBLACK;
  1156. SkScalar surface_scale = 0.f;
  1157. SkScalar kconstant = 0.f;
  1158. SkScalar shininess = 0.f;
  1159. sk_sp<PaintFilter> input;
  1160. ReadEnum(&lighting_type);
  1161. ReadSimple(&location);
  1162. Read(&light_color);
  1163. Read(&surface_scale);
  1164. Read(&kconstant);
  1165. Read(&shininess);
  1166. Read(&input);
  1167. if (!valid_)
  1168. return;
  1169. // TODO(crbug/1308932): Remove FromColor and make all SkColor4f.
  1170. filter->reset(new LightingPointPaintFilter(
  1171. lighting_type, location, SkColor4f::FromColor(light_color), surface_scale,
  1172. kconstant, shininess, std::move(input),
  1173. base::OptionalOrNullptr(crop_rect)));
  1174. }
  1175. void PaintOpReader::ReadLightingSpotPaintFilter(
  1176. sk_sp<PaintFilter>* filter,
  1177. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1178. PaintFilter::LightingType lighting_type;
  1179. SkPoint3 location = SkPoint3::Make(0.f, 0.f, 0.f);
  1180. SkPoint3 target = SkPoint3::Make(0.f, 0.f, 0.f);
  1181. SkScalar specular_exponent = 0.f;
  1182. SkScalar cutoff_angle = 0.f;
  1183. SkColor light_color = SK_ColorBLACK;
  1184. SkScalar surface_scale = 0.f;
  1185. SkScalar kconstant = 0.f;
  1186. SkScalar shininess = 0.f;
  1187. sk_sp<PaintFilter> input;
  1188. ReadEnum(&lighting_type);
  1189. ReadSimple(&location);
  1190. ReadSimple(&target);
  1191. Read(&specular_exponent);
  1192. Read(&cutoff_angle);
  1193. Read(&light_color);
  1194. Read(&surface_scale);
  1195. Read(&kconstant);
  1196. Read(&shininess);
  1197. Read(&input);
  1198. if (!valid_)
  1199. return;
  1200. // TODO(crbug/1308932): Remove FromColor and make all SkColor4f.
  1201. filter->reset(new LightingSpotPaintFilter(
  1202. lighting_type, location, target, specular_exponent, cutoff_angle,
  1203. SkColor4f::FromColor(light_color), surface_scale, kconstant, shininess,
  1204. std::move(input), base::OptionalOrNullptr(crop_rect)));
  1205. }
  1206. void PaintOpReader::ReadStretchPaintFilter(
  1207. sk_sp<PaintFilter>* filter,
  1208. const absl::optional<PaintFilter::CropRect>& crop_rect) {
  1209. SkScalar stretch_x = 0.f;
  1210. SkScalar stretch_y = 0.f;
  1211. SkScalar width = 0.f;
  1212. SkScalar height = 0.f;
  1213. sk_sp<PaintFilter> input;
  1214. Read(&stretch_x);
  1215. Read(&stretch_y);
  1216. Read(&width);
  1217. Read(&height);
  1218. Read(&input);
  1219. if (!valid_)
  1220. return;
  1221. filter->reset(new StretchPaintFilter(stretch_x, stretch_y, width, height,
  1222. std::move(input),
  1223. base::OptionalOrNullptr(crop_rect)));
  1224. }
  1225. size_t PaintOpReader::Read(sk_sp<PaintRecord>* record) {
  1226. size_t size_bytes = 0;
  1227. ReadSize(&size_bytes);
  1228. AlignMemory(PaintOpBuffer::PaintOpAlign);
  1229. if (enable_security_constraints_) {
  1230. // Validate that the record was not serialized if security constraints are
  1231. // enabled.
  1232. if (size_bytes != 0) {
  1233. SetInvalid(DeserializationError::kPaintRecordForbidden);
  1234. return 0;
  1235. }
  1236. *record = sk_make_sp<PaintOpBuffer>();
  1237. return 0;
  1238. }
  1239. if (size_bytes > remaining_bytes_)
  1240. SetInvalid(
  1241. DeserializationError::kInsufficientRemainingBytes_Read_PaintRecord);
  1242. if (!valid_)
  1243. return 0;
  1244. *record = PaintOpBuffer::MakeFromMemory(memory_, size_bytes, options_);
  1245. if (!*record) {
  1246. SetInvalid(DeserializationError::kPaintOpBufferMakeFromMemoryFailure);
  1247. return 0;
  1248. }
  1249. DidRead(size_bytes);
  1250. return size_bytes;
  1251. }
  1252. void PaintOpReader::Read(SkRegion* region) {
  1253. size_t region_bytes = 0;
  1254. ReadSize(&region_bytes);
  1255. if (region_bytes == 0)
  1256. SetInvalid(DeserializationError::kZeroRegionBytes);
  1257. if (region_bytes > remaining_bytes_)
  1258. SetInvalid(DeserializationError::kInsufficientRemainingBytes_Read_SkRegion);
  1259. if (!valid_)
  1260. return;
  1261. std::unique_ptr<char[]> data(new char[region_bytes]);
  1262. ReadData(region_bytes, data.get());
  1263. if (!valid_)
  1264. return;
  1265. size_t result = region->readFromMemory(data.get(), region_bytes);
  1266. if (!result)
  1267. SetInvalid(DeserializationError::kSkRegionReadFromMemoryFailure);
  1268. }
  1269. inline void PaintOpReader::DidRead(size_t bytes_read) {
  1270. // All data are aligned with PaintOpWriter::Alignment() at least.
  1271. size_t aligned_bytes =
  1272. base::bits::AlignUp(bytes_read, PaintOpWriter::Alignment());
  1273. memory_ += aligned_bytes;
  1274. DCHECK_LE(aligned_bytes, remaining_bytes_);
  1275. remaining_bytes_ -= aligned_bytes;
  1276. }
  1277. } // namespace cc