Skip to content

Commit

Permalink
[StyleBuilder] Avoid DataRef for copy-on-write in NinePieceImage
Browse files Browse the repository at this point in the history
A subsequent CL will change how DataRef works such that it can't be
used as a stand-alone copy-on-write container without additional
context.

This is the only instance of DataRef being used "manually" (all other
usage being in generated code).

Bug: 1377295
Change-Id: I5a08ce48bee04744cd91557d63442005748d78ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4145960
Commit-Queue: Anders Hartvoll Ruud <andruud@chromium.org>
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1115557}
  • Loading branch information
andruud authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent 77ea30d commit 11b6352
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
24 changes: 12 additions & 12 deletions third_party/blink/renderer/core/style/nine_piece_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

namespace blink {

static DataRef<NinePieceImageData>& DefaultData() {
static DataRef<NinePieceImageData>* data = new DataRef<NinePieceImageData>;
if (!data->Get()) {
data->Init();
static scoped_refptr<NinePieceImageData>& DefaultData() {
static scoped_refptr<NinePieceImageData>* data = nullptr;
if (!data) {
data = new scoped_refptr<NinePieceImageData>(NinePieceImageData::Create());
}
return *data;
}
Expand All @@ -45,14 +45,14 @@ NinePieceImage::NinePieceImage(StyleImage* image,
const BorderImageLengthBox& outset,
ENinePieceImageRule horizontal_rule,
ENinePieceImageRule vertical_rule) {
data_.Init();
data_.Access()->image = image;
data_.Access()->image_slices = image_slices;
data_.Access()->border_slices = border_slices;
data_.Access()->outset = outset;
data_.Access()->fill = fill;
data_.Access()->horizontal_rule = horizontal_rule;
data_.Access()->vertical_rule = vertical_rule;
data_ = NinePieceImageData::Create();
Access()->image = image;
Access()->image_slices = image_slices;
Access()->border_slices = border_slices;
Access()->outset = outset;
Access()->fill = fill;
Access()->horizontal_rule = horizontal_rule;
Access()->vertical_rule = vertical_rule;
}

NinePieceImageData::NinePieceImageData()
Expand Down
47 changes: 27 additions & 20 deletions third_party/blink/renderer/core/style/nine_piece_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_NINE_PIECE_IMAGE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_NINE_PIECE_IMAGE_H_

#include "base/memory/values_equivalent.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/style/border_image_length_box.h"
#include "third_party/blink/renderer/core/style/data_ref.h"
#include "third_party/blink/renderer/core/style/style_image.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/geometry/length_box.h"
Expand Down Expand Up @@ -85,73 +85,73 @@ class CORE_EXPORT NinePieceImage {

static NinePieceImage MaskDefaults() {
NinePieceImage image;
image.data_.Access()->image_slices = LengthBox(0);
image.data_.Access()->fill = true;
image.data_.Access()->border_slices = BorderImageLengthBox(Length::Auto());
image.Access()->image_slices = LengthBox(0);
image.Access()->fill = true;
image.Access()->border_slices = BorderImageLengthBox(Length::Auto());
return image;
}

bool operator==(const NinePieceImage& other) const {
return data_ == other.data_;
return base::ValuesEquivalent(data_, other.data_);
}
bool operator!=(const NinePieceImage& other) const {
return data_ != other.data_;
return !(*this == other);
}

bool HasImage() const { return data_->image; }
StyleImage* GetImage() const { return data_->image.Get(); }
void SetImage(StyleImage* image) { data_.Access()->image = image; }
void SetImage(StyleImage* image) { Access()->image = image; }

const LengthBox& ImageSlices() const { return data_->image_slices; }
void SetImageSlices(const LengthBox& slices) {
data_.Access()->image_slices = slices;
Access()->image_slices = slices;
}

bool Fill() const { return data_->fill; }
void SetFill(bool fill) { data_.Access()->fill = fill; }
void SetFill(bool fill) { Access()->fill = fill; }

const BorderImageLengthBox& BorderSlices() const {
return data_->border_slices;
}
void SetBorderSlices(const BorderImageLengthBox& slices) {
data_.Access()->border_slices = slices;
Access()->border_slices = slices;
}

const BorderImageLengthBox& Outset() const { return data_->outset; }
void SetOutset(const BorderImageLengthBox& outset) {
data_.Access()->outset = outset;
Access()->outset = outset;
}

ENinePieceImageRule HorizontalRule() const {
return static_cast<ENinePieceImageRule>(data_->horizontal_rule);
}
void SetHorizontalRule(ENinePieceImageRule rule) {
data_.Access()->horizontal_rule = rule;
Access()->horizontal_rule = rule;
}

ENinePieceImageRule VerticalRule() const {
return static_cast<ENinePieceImageRule>(data_->vertical_rule);
}
void SetVerticalRule(ENinePieceImageRule rule) {
data_.Access()->vertical_rule = rule;
Access()->vertical_rule = rule;
}

void CopyImageSlicesFrom(const NinePieceImage& other) {
data_.Access()->image_slices = other.data_->image_slices;
data_.Access()->fill = other.data_->fill;
Access()->image_slices = other.data_->image_slices;
Access()->fill = other.data_->fill;
}

void CopyBorderSlicesFrom(const NinePieceImage& other) {
data_.Access()->border_slices = other.data_->border_slices;
Access()->border_slices = other.data_->border_slices;
}

void CopyOutsetFrom(const NinePieceImage& other) {
data_.Access()->outset = other.data_->outset;
Access()->outset = other.data_->outset;
}

void CopyRepeatFrom(const NinePieceImage& other) {
data_.Access()->horizontal_rule = other.data_->horizontal_rule;
data_.Access()->vertical_rule = other.data_->vertical_rule;
Access()->horizontal_rule = other.data_->horizontal_rule;
Access()->vertical_rule = other.data_->vertical_rule;
}

static LayoutUnit ComputeOutset(const BorderImageLength& outset_side,
Expand All @@ -163,7 +163,14 @@ class CORE_EXPORT NinePieceImage {
}

private:
DataRef<NinePieceImageData> data_;
NinePieceImageData* Access() {
if (!data_->HasOneRef()) {
data_ = data_->Copy();
}
return data_.get();
}

scoped_refptr<NinePieceImageData> data_;
};

} // namespace blink
Expand Down

0 comments on commit 11b6352

Please sign in to comment.