forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SkDrawLooper is unused and untested in Skia, so moving the functionality into Chrome. Bug: skia:8672 Change-Id: I412ff1f2fe51c87cbe3b173efb0fc6e56df4dabb Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2679292 Commit-Queue: Mike Reed <reed@google.com> Reviewed-by: Florin Malita <fmalita@chromium.org> Reviewed-by: vmpstr <vmpstr@chromium.org> Reviewed-by: Scott Violet <sky@chromium.org> Reviewed-by: Aaron Krajeski <aaronhk@chromium.org> Cr-Commit-Position: refs/heads/master@{#853590}
- Loading branch information
1 parent
122ea27
commit efacd53
Showing
39 changed files
with
398 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <utility> | ||
|
||
#include "cc/paint/draw_looper.h" | ||
#include "third_party/skia/include/core/SkCanvas.h" | ||
#include "third_party/skia/include/core/SkColorFilter.h" | ||
#include "third_party/skia/include/core/SkMaskFilter.h" | ||
#include "third_party/skia/include/core/SkPaint.h" | ||
|
||
namespace cc { | ||
|
||
bool DrawLooper::DrawLooper::Layer::operator==(const Layer& other) const { | ||
return offset == other.offset && blurSigma == other.blurSigma && | ||
color == other.color && flags == other.flags; | ||
} | ||
|
||
DrawLooper::DrawLooper(std::vector<Layer> l) : layers_(std::move(l)) {} | ||
DrawLooper::~DrawLooper() = default; | ||
|
||
void DrawLooper::Layer::Apply(SkCanvas* canvas, SkPaint* paint) const { | ||
if (!(flags & kDontModifyPaintFlag)) { | ||
if (flags & kOverrideAlphaFlag) | ||
paint->setAlpha(0xFF); | ||
|
||
if (blurSigma > 0) | ||
paint->setMaskFilter(SkMaskFilter::MakeBlur( | ||
kNormal_SkBlurStyle, blurSigma, !(flags & kPostTransformFlag))); | ||
|
||
paint->setColorFilter(SkColorFilters::Blend(color, SkBlendMode::kSrcIn)); | ||
} | ||
|
||
if (flags & kPostTransformFlag) | ||
canvas->setMatrix( | ||
canvas->getLocalToDevice().postTranslate(offset.fX, offset.fY)); | ||
else | ||
canvas->translate(offset.fX, offset.fY); | ||
} | ||
|
||
bool DrawLooper::operator==(const DrawLooper& other) const { | ||
return layers_ == other.layers_; | ||
} | ||
|
||
// static | ||
size_t DrawLooper::GetSerializedSize(const DrawLooper* looper) { | ||
size_t size = sizeof(bool); | ||
if (!looper) | ||
return size; | ||
|
||
size_t count = looper->layers_.size(); | ||
size += sizeof(count); | ||
if (count == 0) | ||
return size; | ||
|
||
auto layer = looper->layers_.begin(); | ||
|
||
size_t layer_size = sizeof(layer->offset.fX) + sizeof(layer->offset.fY) + | ||
sizeof(layer->blurSigma) + sizeof(layer->color) + | ||
sizeof(layer->flags); | ||
|
||
return size + count * layer_size; | ||
} | ||
|
||
DrawLooperBuilder::DrawLooperBuilder() = default; | ||
DrawLooperBuilder::~DrawLooperBuilder() = default; | ||
|
||
void DrawLooperBuilder::AddUnmodifiedContent(bool addOnTop) { | ||
AddShadow({0, 0}, 0, 0, DrawLooper::kDontModifyPaintFlag, addOnTop); | ||
} | ||
|
||
void DrawLooperBuilder::AddShadow(SkPoint offset, | ||
float blurSigma, | ||
SkColor color, | ||
uint32_t flags, | ||
bool addOnTop) { | ||
const DrawLooper::Layer layer = {offset, blurSigma, color, | ||
flags & DrawLooper::kAllFlagsMask}; | ||
if (addOnTop) | ||
layers_.insert(layers_.begin(), layer); | ||
else | ||
layers_.push_back(layer); | ||
} | ||
|
||
sk_sp<DrawLooper> DrawLooperBuilder::Detach() { | ||
return sk_sp<DrawLooper>(new DrawLooper(std::move(layers_))); | ||
} | ||
|
||
} // namespace cc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CC_PAINT_DRAW_LOOPER_H_ | ||
#define CC_PAINT_DRAW_LOOPER_H_ | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include "base/optional.h" | ||
#include "base/stl_util.h" | ||
#include "cc/paint/paint_export.h" | ||
#include "third_party/skia/include/core/SkBlendMode.h" | ||
#include "third_party/skia/include/core/SkCanvas.h" | ||
#include "third_party/skia/include/core/SkColor.h" | ||
#include "third_party/skia/include/core/SkPoint.h" | ||
#include "third_party/skia/include/core/SkRefCnt.h" | ||
|
||
class SkCanvas; | ||
class SkPaint; | ||
|
||
namespace cc { | ||
|
||
/* | ||
* Optional collection of modifiers to paint/canvas to facilitate drawing | ||
* a given primitive multiple times. Often this is used for shadows. | ||
*/ | ||
class CC_PAINT_EXPORT DrawLooper : public SkRefCnt { | ||
public: | ||
enum Flags { | ||
// If set, apply layer's offset after the canvas' transformation matrix. | ||
// If clear, pre-translate with layer's offset | ||
kPostTransformFlag = 1 << 0, | ||
|
||
// If set, set the paint's alpha to the layer's alpha | ||
// If clear, set the paint's alpha to opaque | ||
kOverrideAlphaFlag = 1 << 1, | ||
|
||
// If set, ignore all modifiers but still translate using layer's offset | ||
kDontModifyPaintFlag = 1 << 2, | ||
}; | ||
|
||
~DrawLooper() override; | ||
|
||
// The callback will be invoked for each "layer" in the looper, each time | ||
// with a modified canvas and paint (depending on what the looper wanted | ||
// to change). These can each be drawn directly, as the looper will callback | ||
// logically bottom to top visually. | ||
template <typename DrawProc> | ||
void Apply(SkCanvas* canvas, const SkPaint& paint, DrawProc proc) const { | ||
// Our array is stored top to bottom | ||
// e.g. layers_[0] is the top (will be drawn last) | ||
// layers_[N-1] is on bottom, and will be drawn first | ||
// | ||
// Hence, since we must draw in painter's order (bottom to top), we | ||
// walk the array in reverse. | ||
// | ||
// Each time through the loop, we make a copy of the draw's paint, modify | ||
// it as indicated by the layer info, modify the canvas' translate, and | ||
// then call back to issue the actual draw. | ||
for (auto it = layers_.rbegin(); it != layers_.rend(); ++it) { | ||
SkAutoCanvasRestore acr(canvas, true); | ||
SkPaint p(paint); | ||
it->Apply(canvas, &p); | ||
proc(canvas, p); | ||
} | ||
} | ||
|
||
bool operator==(const DrawLooper& other) const; | ||
bool operator!=(const DrawLooper& other) const { return !(*this == other); } | ||
|
||
static size_t GetSerializedSize(const DrawLooper* looper); | ||
|
||
private: | ||
// Keep this in sync with the fields in Flags | ||
// Used to mask out illegal bits when constructing Layer | ||
enum { | ||
kAllFlagsMask = | ||
kPostTransformFlag | kOverrideAlphaFlag | kDontModifyPaintFlag, | ||
}; | ||
|
||
struct Layer { | ||
SkPoint offset; | ||
float blurSigma; | ||
SkColor color; | ||
uint32_t flags; | ||
|
||
bool operator==(const Layer& other) const; | ||
|
||
void Apply(SkCanvas* canvas, SkPaint* paint) const; | ||
}; | ||
std::vector<Layer> layers_; | ||
|
||
explicit DrawLooper(std::vector<Layer> l); | ||
|
||
void UpdateForLayer(const Layer& layer, | ||
SkCanvas* canvas, | ||
SkPaint* paint) const; | ||
|
||
friend class DrawLooperBuilder; | ||
friend class PaintOpReader; | ||
friend class PaintOpWriter; | ||
}; | ||
|
||
class CC_PAINT_EXPORT DrawLooperBuilder { | ||
public: | ||
DrawLooperBuilder(); | ||
~DrawLooperBuilder(); | ||
|
||
void AddUnmodifiedContent(bool addOnTop = false); | ||
void AddShadow(SkPoint offset, | ||
float blurSigma, | ||
SkColor color, | ||
uint32_t flags, | ||
bool addOnTop = false); | ||
sk_sp<DrawLooper> Detach(); | ||
|
||
private: | ||
std::vector<DrawLooper::Layer> layers_; | ||
}; | ||
|
||
} // namespace cc | ||
|
||
#endif // CC_PAINT_DRAW_LOOPER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.