2019-08-07 15:22:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc. and Adobe Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-09 17:25:17 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-08-07 15:22:13 +00:00
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "tools/timer/TimeUtils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This sample exercises heavy texture updates and uploads.
|
|
|
|
*/
|
|
|
|
class TextureUploadSample : public Sample {
|
|
|
|
static constexpr int kMinTileSize = 128;
|
|
|
|
static constexpr int kMaxTileSize = 2048;
|
|
|
|
static constexpr float kGridScale = 0.25f;
|
|
|
|
|
|
|
|
bool fDrawTexturesToScreen = true;
|
|
|
|
int fTileSize = 256;
|
|
|
|
int fTileRows = 8;
|
|
|
|
int fTileCols = 8;
|
|
|
|
|
|
|
|
sk_sp<SkSurface> fBlueSurface;
|
|
|
|
sk_sp<SkSurface> fGraySurface;
|
|
|
|
|
|
|
|
class RenderTargetTexture : public SkRefCnt {
|
|
|
|
public:
|
2020-07-09 17:25:17 +00:00
|
|
|
RenderTargetTexture(GrDirectContext* direct, int size) {
|
2020-09-24 18:49:00 +00:00
|
|
|
SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);
|
2019-08-07 15:22:13 +00:00
|
|
|
SkImageInfo imageInfo = SkImageInfo::Make(size, size, kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
2020-07-09 17:25:17 +00:00
|
|
|
fSurface = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, imageInfo, 0,
|
2019-08-07 15:22:13 +00:00
|
|
|
&surfaceProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkImage> getImage() {
|
|
|
|
return fSurface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uploadRasterSurface(sk_sp<SkSurface> rasterSurface) {
|
|
|
|
SkPixmap pixmap;
|
|
|
|
rasterSurface->peekPixels(&pixmap);
|
|
|
|
fSurface->writePixels(pixmap, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<SkSurface> fSurface;
|
|
|
|
sk_sp<SkImage> fCachedImage;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkTArray<sk_sp<RenderTargetTexture>> fTextures;
|
2020-07-09 17:25:17 +00:00
|
|
|
GrDirectContext* fCachedContext = nullptr;
|
2019-08-07 15:22:13 +00:00
|
|
|
SkScalar fActiveTileIndex = 0;
|
|
|
|
|
|
|
|
SkString name() override {
|
|
|
|
return SkString("TextureUpload");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onChar(SkUnichar uni) override {
|
|
|
|
if ('m' == uni) {
|
|
|
|
fDrawTexturesToScreen = !fDrawTexturesToScreen;
|
|
|
|
return true;
|
|
|
|
} else if ('>' == uni) {
|
2020-02-07 15:36:46 +00:00
|
|
|
fTileSize = std::min(kMaxTileSize, 2*fTileSize);
|
2019-08-07 15:22:13 +00:00
|
|
|
fTileRows = kMaxTileSize/fTileSize;
|
|
|
|
fTileCols = kMaxTileSize/fTileSize;
|
|
|
|
fCachedContext = nullptr;
|
|
|
|
} else if ('<' == uni) {
|
2020-02-07 15:36:46 +00:00
|
|
|
fTileSize = std::max(kMinTileSize, fTileSize/2);
|
2019-08-07 15:22:13 +00:00
|
|
|
fTileRows = kMaxTileSize/fTileSize;
|
|
|
|
fTileCols = kMaxTileSize/fTileSize;
|
|
|
|
fCachedContext = nullptr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSurface> getFilledRasterSurface(SkColor color, int size) {
|
|
|
|
sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(size, size));
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
canvas->clear(color);
|
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
this->setSize(1024, 1024);
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:25:17 +00:00
|
|
|
void initializeTextures(GrDirectContext* direct) {
|
2019-08-07 15:22:13 +00:00
|
|
|
fTextures.reset();
|
|
|
|
int textureCount = fTileRows * fTileCols;
|
|
|
|
for (int i = 0; i < textureCount; i++) {
|
2020-07-09 17:25:17 +00:00
|
|
|
fTextures.emplace_back(new RenderTargetTexture(direct, fTileSize));
|
2019-08-07 15:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct two simple rasters of differing colors to serve
|
|
|
|
// as cpu rasterized data to refresh textures with.
|
|
|
|
fBlueSurface = this->getFilledRasterSurface(SK_ColorBLUE, fTileSize);
|
|
|
|
fGraySurface = this->getFilledRasterSurface(SK_ColorGRAY, fTileSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
SkPaint paint;
|
|
|
|
|
2020-07-09 17:25:17 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (direct) {
|
2019-08-07 15:22:13 +00:00
|
|
|
// One-time context-specific setup.
|
2020-07-09 17:25:17 +00:00
|
|
|
if (direct != fCachedContext) {
|
|
|
|
fCachedContext = direct;
|
|
|
|
this->initializeTextures(direct);
|
2019-08-07 15:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upload new texture data for all textures, simulating a full page of tiles
|
|
|
|
// needing refresh.
|
|
|
|
int textureCount = fTileRows * fTileCols;
|
|
|
|
for (int i = 0; i < textureCount; i++) {
|
|
|
|
fTextures[i]->uploadRasterSurface(i == fActiveTileIndex ? fBlueSurface
|
|
|
|
: fGraySurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale grid.
|
|
|
|
canvas->scale(kGridScale, kGridScale);
|
|
|
|
|
|
|
|
if (fDrawTexturesToScreen) {
|
|
|
|
for (int y = 0; y < fTileRows; y++) {
|
|
|
|
for (int x = 0; x < fTileCols; x++) {
|
|
|
|
int currentIndex = y * fTileCols + x;
|
|
|
|
canvas->drawImage(fTextures[currentIndex]->getImage(),
|
|
|
|
x * fTileSize, y * fTileSize, &paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
constexpr SkScalar kDesiredDurationSecs = 16.0f;
|
|
|
|
float numTiles = fTileRows*fTileCols;
|
|
|
|
fActiveTileIndex = floorf(TimeUtils::Scaled(1e-9 * nanos,
|
|
|
|
numTiles/kDesiredDurationSecs, numTiles));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DEF_SAMPLE( return new TextureUploadSample(); )
|
|
|
|
|