| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
#include "include/core/SkBitmap.h" |
| 8 |
#include "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkImage.h" |
| 10 |
#include "include/core/SkImageInfo.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/effects/SkGradient.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "src/base/SkArenaAlloc.h" |
| 16 |
#include "src/core/SkBlitter.h" |
| 17 |
#include "src/core/SkCoreBlitters.h" |
| 18 |
#include "src/core/SkRasterPipelineOpList.h" |
| 19 |
#include "src/core/SkRasterPipelineVizualizer.h" |
| 20 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 21 |
#include "tools/viewer/Slide.h" |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
|
| 25 |
static constexpr float kPanelSize = 100.f; |
| 26 |
static constexpr float kPadding = 10.f; |
| 27 |
static constexpr float kBorder = 2.f; |
| 28 |
static constexpr float kHandleRadius = 3.f; |
| 29 |
|
| 30 |
static SkBitmap make_panel() { |
| 31 |
SkBitmap panel; |
| 32 |
panel.setInfo(SkImageInfo::Make(kPanelSize, kPanelSize, |
| 33 |
kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); |
| 34 |
panel.allocPixels(); |
| 35 |
panel.eraseColor(SK_ColorBLACK); |
| 36 |
return panel; |
| 37 |
} |
| 38 |
|
| 39 |
static SkColor4f rgb(uint8_t r, uint8_t g, uint8_t b) { |
| 40 |
constexpr float scale = 1.0f/255; |
| 41 |
return {r*scale, g*scale, b*scale, 1}; |
| 42 |
} |
| 43 |
|
| 44 |
class RPVizSlide : public ClickHandlerSlide { |
| 45 |
public: |
| 46 |
RPVizSlide() { fName = "RasterPipelineViz"; } |
| 47 |
|
| 48 |
void draw(SkCanvas* canvas) override { |
| 49 |
canvas->clear(SK_ColorWHITE); |
| 50 |
|
| 51 |
SkPaint paint; |
| 52 |
paint.setColor(SK_ColorRED); |
| 53 |
// paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 1.0)); |
| 54 |
const SkColor4f colors[] = { |
| 55 |
rgb(0xE4, 0x03, 0x03), // Red |
| 56 |
rgb(0xFF, 0x8C, 0x00), // Orange |
| 57 |
rgb(0xFF, 0xED, 0x00), // Yellow |
| 58 |
rgb(0x00, 0x80, 0x26), // Green |
| 59 |
rgb(0x00, 0x4D, 0xFF), // Blue |
| 60 |
rgb(0x75, 0x07, 0x87) // Violet |
| 61 |
}; |
| 62 |
SkPoint points[] = { |
| 63 |
{fStartDX, fStartDY}, |
| 64 |
{fEndX, fEndY}, |
| 65 |
}; |
| 66 |
auto shader = SkShaders::LinearGradient( |
| 67 |
points, {{colors, {}, SkTileMode::kClamp}, {}}); |
| 68 |
paint.setShader(shader); |
| 69 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 70 |
|
| 71 |
SkBitmap dst; |
| 72 |
dst.setInfo(SkImageInfo::Make( |
| 73 |
kPanelSize, kPanelSize, kRGBA_8888_SkColorType, kOpaque_SkAlphaType)); |
| 74 |
dst.allocPixels(); |
| 75 |
dst.eraseColor(SK_ColorBLACK); |
| 76 |
|
| 77 |
// Suggested flow for customizing this (or making your own) |
| 78 |
// 1) Create a shader of interest and put it into a paint. |
| 79 |
// 2) Use SkRasterPipelineVisualizer::DebugStageBuilder to create an *empty* list of stages. |
| 80 |
// 3) Call SkRasterPipelineVisualizer::CreateBlitter and check stdout for a dump |
| 81 |
// of the stages (and it will assert). |
| 82 |
// 4) With SkRasterPipeline_opts.h as a reference, add one or more entries for each stage |
| 83 |
// to pick out the "interesting" lanes. |
| 84 |
// 5) Re-compile and run. Modify stages to taste. |
| 85 |
SkRasterPipelineVisualizer::DebugStageBuilder stageBuilder; |
| 86 |
stageBuilder |
| 87 |
.add(make_panel(), SkRasterPipelineOp::debug_x, |
| 88 |
make_panel(), SkRasterPipelineOp::debug_y) |
| 89 |
.add(make_panel(), SkRasterPipelineOp::debug_x) |
| 90 |
.add(make_panel(), SkRasterPipelineOp::debug_x) |
| 91 |
.add(make_panel(), SkRasterPipelineOp::debug_r_255, |
| 92 |
make_panel(), SkRasterPipelineOp::debug_g_255, |
| 93 |
make_panel(), SkRasterPipelineOp::debug_b_255, |
| 94 |
make_panel(), SkRasterPipelineOp::debug_a_255); |
| 95 |
|
| 96 |
std::vector<SkRasterPipelineVisualizer::DebugStage> stages = stageBuilder.build(); |
| 97 |
|
| 98 |
static constexpr size_t kStackMemory = 1024; // arbitrary |
| 99 |
SkSTArenaAlloc<kStackMemory> alloc; |
| 100 |
auto blitter = SkRasterPipelineVisualizer::CreateBlitter( |
| 101 |
dst.pixmap(), stages, paint, SkMatrix::I(), &alloc, nullptr, {}); |
| 102 |
|
| 103 |
blitter->blitRect(0, 0, kPanelSize, kPanelSize); |
| 104 |
|
| 105 |
SkPaint panelBackdrop; |
| 106 |
panelBackdrop.setColor(SK_ColorGRAY); |
| 107 |
panelBackdrop.setStyle(SkPaint::Style::kFill_Style); |
| 108 |
|
| 109 |
for (size_t row = 0; row < stages.size(); row++) { |
| 110 |
for (size_t col = 0; col < stages[row].panels.size(); col++) { |
| 111 |
float x = (kPanelSize + kPadding) * col + kPadding; |
| 112 |
float y = (kPanelSize + kPadding) * row + kPadding; |
| 113 |
canvas->drawRect(SkRect::MakeXYWH(x - kBorder, |
| 114 |
y - kBorder, |
| 115 |
kPanelSize + 2 * kBorder, |
| 116 |
kPanelSize + 2 * kBorder), |
| 117 |
panelBackdrop); |
| 118 |
auto panelImg = SkImages::RasterFromBitmap(stages[row].panels[col]); |
| 119 |
canvas->drawImage(panelImg, x, y); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Draw the output |
| 124 |
auto dstImg = SkImages::RasterFromBitmap(dst); |
| 125 |
fOutputCornerX = kPadding; |
| 126 |
fOutputCornerY = (kPanelSize + kPadding) * stages.size() + kPadding; |
| 127 |
canvas->drawImage(dstImg, fOutputCornerX, fOutputCornerY); |
| 128 |
|
| 129 |
// Draw the handles |
| 130 |
SkPaint handlePaint; |
| 131 |
handlePaint.setAntiAlias(true); |
| 132 |
handlePaint.setStrokeWidth(2); |
| 133 |
handlePaint.setStyle(SkPaint::Style::kStroke_Style); |
| 134 |
|
| 135 |
canvas->drawCircle( |
| 136 |
fOutputCornerX + fStartDX, fOutputCornerY + fStartDY, kHandleRadius, handlePaint); |
| 137 |
canvas->drawCircle( |
| 138 |
fOutputCornerX + fEndX, fOutputCornerY + fEndY, kHandleRadius, handlePaint); |
| 139 |
} |
| 140 |
|
| 141 |
private: |
| 142 |
class Click : public ClickHandlerSlide::Click { |
| 143 |
public: |
| 144 |
Click(float* x, float* y) : fX(x), fY(y) {} |
| 145 |
|
| 146 |
void doClick(RPVizSlide* that) { |
| 147 |
float newX = SkTPin(fCurr.fX - that->fOutputCornerX, 0.f, kPanelSize); |
| 148 |
float newY = SkTPin(fCurr.fY - that->fOutputCornerY, 0.f, kPanelSize); |
| 149 |
*fX = newX; |
| 150 |
*fY = newY; |
| 151 |
} |
| 152 |
|
| 153 |
private: |
| 154 |
float* fX; |
| 155 |
float* fY; |
| 156 |
}; |
| 157 |
|
| 158 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override { |
| 159 |
SkPoint start{fOutputCornerX + fStartDX, fOutputCornerY + fStartDY}; |
| 160 |
SkPoint end{fOutputCornerX + fEndX, fOutputCornerY + fEndY}; |
| 161 |
|
| 162 |
SkPoint click{x, y}; |
| 163 |
|
| 164 |
if (SkPoint::Distance(start, click) < SkPoint::Distance(end, click)) { |
| 165 |
return new Click(&fStartDX, &fStartDY); |
| 166 |
} |
| 167 |
return new Click(&fEndX, &fEndY); |
| 168 |
} |
| 169 |
|
| 170 |
bool onClick(ClickHandlerSlide::Click* click) override { |
| 171 |
Click* myClick = (Click*)click; |
| 172 |
myClick->doClick(this); |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
float fOutputCornerX = 0.f; |
| 177 |
float fOutputCornerY = 0.f; |
| 178 |
|
| 179 |
float fStartDX = 5.f; |
| 180 |
float fStartDY = 5.f; |
| 181 |
float fEndX = 95.f; |
| 182 |
float fEndY = 95.f; |
| 183 |
}; |
| 184 |
|
| 185 |
DEF_SLIDE(return new RPVizSlide();) |