Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a14d809

Browse files
Nathaniel NifongSkia Commit-Bot
authored andcommitted
Support using an externally manage list of images in UrlDataManager for wasm debugger.
Bug: skia:9746 Change-Id: I07b5fe1f8f4250e29153b71ce2be106461f09928 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/261998 Commit-Queue: Nathaniel Nifong <nifong@google.com> Reviewed-by: Kevin Lubick <kjlubick@google.com>
1 parent 365c366 commit a14d809

File tree

4 files changed

+87
-38
lines changed

4 files changed

+87
-38
lines changed

experimental/wasm-skp-debugger/debugger_bindings.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,14 @@ struct SimpleImageInfo {
4444
int height;
4545
SkColorType colorType;
4646
SkAlphaType alphaType;
47-
// Shown in the textual description of the image, and used as a common identifier between
48-
// the resources tab and the command list.
49-
// TODO(nifong) make the command list use the resource tab's ids instead of UrlDataManager
50-
uintptr_t imageAddress;
5147
};
5248

5349
SkImageInfo toSkImageInfo(const SimpleImageInfo& sii) {
5450
return SkImageInfo::Make(sii.width, sii.height, sii.colorType, sii.alphaType);
5551
}
5652

57-
SimpleImageInfo toSimpleImageInfo(const SkImageInfo& ii, const SkImage* addr) {
58-
return (SimpleImageInfo){ii.width(), ii.height(), ii.colorType(), ii.alphaType(), (uintptr_t)addr};
53+
SimpleImageInfo toSimpleImageInfo(const SkImageInfo& ii) {
54+
return (SimpleImageInfo){ii.width(), ii.height(), ii.colorType(), ii.alphaType()};
5955
}
6056

6157
class SkpDebugPlayer {
@@ -242,7 +238,7 @@ class SkpDebugPlayer {
242238

243239
// Get the image info of one of the resource images.
244240
SimpleImageInfo getImageInfo(int index) {
245-
return toSimpleImageInfo(fImages[index]->imageInfo(), fImages[index].get());
241+
return toSimpleImageInfo(fImages[index]->imageInfo());
246242
}
247243

248244
// return a list of layer draw events that happened at the beginning of this frame.
@@ -323,6 +319,8 @@ class SkpDebugPlayer {
323319
i++;
324320
}
325321
fImages = deserialContext->fImages;
322+
323+
udm.indexImages(fImages);
326324
}
327325

328326
// constrains the draw command index to the frame's command list length.
@@ -349,11 +347,8 @@ class SkpDebugPlayer {
349347

350348
// The URLDataManager here is a cache that accepts encoded data (pngs) and puts
351349
// numbers on them. We have our own collection of images (fImages) that was populated by the
352-
// SkSharingDeserialContext when mskp files are loaded. It would be nice to have the mapping
353-
// indices between these two caches so the urls displayed in command info match the list
354-
// in the resource tab, and to make cross linking possible. One way to do this would be to
355-
// look up all of fImages in udm but the exact encoding of the PNG differs and we wouldn't
356-
// find anything. TODO(nifong): Unify these two numbering schemes in CollatingCanvas.
350+
// SkSharingDeserialContext when mskp files are loaded which it can use for IDing images
351+
// without having to serialize them.
357352
UrlDataManager udm;
358353

359354
// A structure holding the picture information needed to draw any layers used in an mskp file
@@ -481,8 +476,7 @@ EMSCRIPTEN_BINDINGS(my_module) {
481476
.field("width", &SimpleImageInfo::width)
482477
.field("height", &SimpleImageInfo::height)
483478
.field("colorType", &SimpleImageInfo::colorType)
484-
.field("alphaType", &SimpleImageInfo::alphaType)
485-
.field("imageAddress", &SimpleImageInfo::imageAddress);
479+
.field("alphaType", &SimpleImageInfo::alphaType);
486480
constant("TRANSPARENT", (JSColor) SK_ColorTRANSPARENT);
487481
function("_getRasterDirectSurface", optional_override([](const SimpleImageInfo ii,
488482
uintptr_t /* uint8_t* */ pPtr,

tools/UrlDataManager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "tools/UrlDataManager.h"
99

10+
#include <unordered_map>
11+
1012
bool operator==(const SkData& a, const SkData& b) {
1113
return a.equals(&b);
1214
}
@@ -42,3 +44,21 @@ void UrlDataManager::reset() {
4244

4345
fCache.rewind();
4446
}
47+
48+
void UrlDataManager::indexImages(const std::vector<sk_sp<SkImage>>& images) {
49+
SkASSERT(imageMap.size() == 0); // this method meant only for initialization once.
50+
for (size_t i = 0; i < images.size(); ++i) {
51+
imageMap.insert({images[i].get(), i});
52+
}
53+
}
54+
55+
int UrlDataManager::lookupImage(const SkImage* im) {
56+
auto search = imageMap.find(im);
57+
if (search != imageMap.end()) {
58+
return search->second;
59+
} else {
60+
// -1 signals the pointer to this image wasn't in the original list.
61+
// Maybe it was synthesized after file load? If so, you shouldn't be looking it up here.
62+
return -1;
63+
}
64+
}

tools/UrlDataManager.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
#define SkUrlDataManager_DEFINED
1010

1111
#include "include/core/SkData.h"
12+
#include "include/core/SkImage.h"
1213
#include "include/core/SkString.h"
1314
#include "src/core/SkOpts.h"
1415
#include "src/core/SkTDynamicHash.h"
1516

17+
#include <unordered_map>
18+
1619
/*
1720
* A simple class which allows clients to add opaque data types, and returns a url where this data
1821
* will be hosted. Its up to the owner of this class to actually serve the data.
@@ -44,6 +47,27 @@ class UrlDataManager {
4447
}
4548
void reset();
4649

50+
// Methods used to identify images differently in wasm debugger for mskp animations.
51+
// serving is uncessary, as a collection of images with identifiers is already present, we
52+
// just want to use it when serializing commands.
53+
54+
/*
55+
* Construct an index from a list of images
56+
* (expected to be the list that was loaded from the mskp file)
57+
* Use only once.
58+
*/
59+
void indexImages(const std::vector<sk_sp<SkImage>>&);
60+
61+
/*
62+
* Reports whether this UDM has an initialized image index (effevitely whether we're in wasm)
63+
*/
64+
bool hasImageIndex() { return imageMap.size() > 0; }
65+
66+
/*
67+
* Return the file id (index of the image in the originally provided list) of an SkImage
68+
*/
69+
int lookupImage(const SkImage*);
70+
4771
private:
4872
struct LookupTrait {
4973
// We use the data as a hash, this is not really optimal but is fine until proven otherwise
@@ -71,6 +95,7 @@ class UrlDataManager {
7195
SkTDynamicHash<UrlData, SkData, LookupTrait> fCache;
7296
SkTDynamicHash<UrlData, SkString, ReverseLookupTrait> fUrlLookup;
7397
uint32_t fDataId;
98+
std::unordered_map<const SkImage*, int> imageMap;
7499
};
75100

76101
#endif

tools/debugger/DrawCommand.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#define DEBUGCANVAS_ATTRIBUTE_COLORFILTER "colorfilter"
9090
#define DEBUGCANVAS_ATTRIBUTE_IMAGEFILTER "imagefilter"
9191
#define DEBUGCANVAS_ATTRIBUTE_IMAGE "image"
92-
#define DEBUGCANVAS_ATTRIBUTE_IMAGE_ADDRESS "imageAddress"
92+
#define DEBUGCANVAS_ATTRIBUTE_IMAGE_INDEX "imageIndex"
9393
#define DEBUGCANVAS_ATTRIBUTE_BITMAP "bitmap"
9494
#define DEBUGCANVAS_ATTRIBUTE_SRC "src"
9595
#define DEBUGCANVAS_ATTRIBUTE_DST "dst"
@@ -1318,12 +1318,16 @@ bool DrawImageCommand::render(SkCanvas* canvas) const {
13181318

13191319
void DrawImageCommand::toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager) const {
13201320
INHERITED::toJSON(writer, urlDataManager);
1321-
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1322-
flatten(*fImage, writer, urlDataManager);
1323-
writer.endObject(); // image
13241321

1325-
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_ADDRESS);
1326-
writer.appendU64((uint64_t)fImage.get());
1322+
1323+
if (urlDataManager.hasImageIndex()) {
1324+
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_INDEX);
1325+
writer.appendU64((uint64_t)urlDataManager.lookupImage(fImage.get()));
1326+
} else {
1327+
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1328+
flatten(*fImage, writer, urlDataManager);
1329+
writer.endObject(); // image
1330+
}
13271331

13281332
writer.appendName(DEBUGCANVAS_ATTRIBUTE_COORDS);
13291333
MakeJsonPoint(writer, fLeft, fTop);
@@ -1377,12 +1381,14 @@ bool DrawImageLatticeCommand::render(SkCanvas* canvas) const {
13771381

13781382
void DrawImageLatticeCommand::toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager) const {
13791383
INHERITED::toJSON(writer, urlDataManager);
1380-
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1381-
flatten(*fImage, writer, urlDataManager);
1382-
writer.endObject(); // image
1383-
1384-
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_ADDRESS);
1385-
writer.appendU64((uint64_t)fImage.get());
1384+
if (urlDataManager.hasImageIndex()) {
1385+
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_INDEX);
1386+
writer.appendU64((uint64_t)urlDataManager.lookupImage(fImage.get()));
1387+
} else {
1388+
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1389+
flatten(*fImage, writer, urlDataManager);
1390+
writer.endObject(); // image
1391+
}
13861392

13871393
writer.appendName(DEBUGCANVAS_ATTRIBUTE_LATTICE);
13881394
MakeJsonLattice(writer, fLattice);
@@ -1426,12 +1432,14 @@ bool DrawImageRectCommand::render(SkCanvas* canvas) const {
14261432

14271433
void DrawImageRectCommand::toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager) const {
14281434
INHERITED::toJSON(writer, urlDataManager);
1429-
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1430-
flatten(*fImage, writer, urlDataManager);
1431-
writer.endObject(); // image
1432-
1433-
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_ADDRESS);
1434-
writer.appendU64((uint64_t)fImage.get());
1435+
if (urlDataManager.hasImageIndex()) {
1436+
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_INDEX);
1437+
writer.appendU64((uint64_t)urlDataManager.lookupImage(fImage.get()));
1438+
} else {
1439+
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1440+
flatten(*fImage, writer, urlDataManager);
1441+
writer.endObject(); // image
1442+
}
14351443

14361444
if (fSrc.isValid()) {
14371445
writer.appendName(DEBUGCANVAS_ATTRIBUTE_SRC);
@@ -1536,12 +1544,14 @@ bool DrawImageNineCommand::render(SkCanvas* canvas) const {
15361544

15371545
void DrawImageNineCommand::toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager) const {
15381546
INHERITED::toJSON(writer, urlDataManager);
1539-
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1540-
flatten(*fImage, writer, urlDataManager);
1541-
writer.endObject(); // image
1542-
1543-
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_ADDRESS);
1544-
writer.appendU64((uint64_t)fImage.get());
1547+
if (urlDataManager.hasImageIndex()) {
1548+
writer.appendName(DEBUGCANVAS_ATTRIBUTE_IMAGE_INDEX);
1549+
writer.appendU64((uint64_t)urlDataManager.lookupImage(fImage.get()));
1550+
} else {
1551+
writer.beginObject(DEBUGCANVAS_ATTRIBUTE_IMAGE);
1552+
flatten(*fImage, writer, urlDataManager);
1553+
writer.endObject(); // image
1554+
}
15451555

15461556
writer.appendName(DEBUGCANVAS_ATTRIBUTE_CENTER);
15471557
MakeJsonIRect(writer, fCenter);

0 commit comments

Comments
 (0)