15bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt/*
25bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt * Copyright 2015 Google Inc.
35bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt *
45bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt * Use of this source code is governed by a BSD-style license that can be
55bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt * found in the LICENSE file.
65bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt */
75bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
8903da79a19c35723ff198d56923250d5f3fe0f15Brian Salomon#include "GrDrawOpAtlas.h"
932f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips
1032f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips#include "GrContext.h"
11742e31de1599f3902810aecdf2e2e3eed3b40a09Brian Salomon#include "GrOpFlushState.h"
125bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt#include "GrRectanizer.h"
13256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips#include "GrResourceProvider.h"
14646e4293f06d9de6d44dbfa3c32cdc15a6f5906eRobert Phillips#include "GrTexture.h"
155bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt#include "GrTracing.h"
165bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
17256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillipsstd::unique_ptr<GrDrawOpAtlas> GrDrawOpAtlas::Make(GrContext* ctx, GrPixelConfig config,
18256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips                                                   int width, int height,
19256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips                                                   int numPlotsX, int numPlotsY,
20256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips                                                   GrDrawOpAtlas::EvictionFunc func,
21256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips                                                   void* data) {
22256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    GrSurfaceDesc desc;
23256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    desc.fFlags = kNone_GrSurfaceFlags;
24256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    desc.fWidth = width;
25256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    desc.fHeight = height;
26256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    desc.fConfig = config;
27256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
28256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // We don't want to flush the context so we claim we're in the middle of flushing so as to
29256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // guarantee we do not recieve a texture with pending IO
30256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // TODO: Determine how to avoid having to do this. (https://bug.skia.org/4156)
31256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
3232342f032e1dfd133040324f851f0365f9d4cb51Brian Osman    sk_sp<GrTexture> texture(ctx->resourceProvider()->createApproxTexture(desc, kFlags));
33256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    if (!texture) {
34256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        return nullptr;
35256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    }
36256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
37256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // MDB TODO: for now, wrap an instantiated texture. Having the deferred instantiation
38256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // possess the correct properties (e.g., no pendingIO) should fall out of the system but
39256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // should receive special attention.
40256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // Note: When switching over to the deferred proxy, use the kExact flag to create
41256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    // the atlas and assert that the width & height are powers of 2.
42256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeWrapped(std::move(texture));
43256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    if (!proxy) {
44256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        return nullptr;
45256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    }
46256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
47256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    std::unique_ptr<GrDrawOpAtlas> atlas(
48256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            new GrDrawOpAtlas(ctx, std::move(proxy), numPlotsX, numPlotsY));
49256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    atlas->registerEvictionCallback(func, data);
50256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    return atlas;
51256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips}
52256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
53256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
545df175ee71164dce97758564f308ab301bfe393ajoshualitt////////////////////////////////////////////////////////////////////////////////
555bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
562ee084e73056b0ad76b721017f576168b7306da3Brian SalomonGrDrawOpAtlas::Plot::Plot(int index, uint64_t genID, int offX, int offY, int width, int height,
572ee084e73056b0ad76b721017f576168b7306da3Brian Salomon                          GrPixelConfig config)
582ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        : fLastUpload(GrDrawOpUploadToken::AlreadyFlushedToken())
592ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fLastUse(GrDrawOpUploadToken::AlreadyFlushedToken())
602ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fIndex(index)
612ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fGenID(genID)
622ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fID(CreateId(fIndex, fGenID))
632ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fData(nullptr)
642ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fWidth(width)
652ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fHeight(height)
662ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fX(offX)
672ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fY(offY)
682ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fRects(nullptr)
692ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
702ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fConfig(config)
712ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fBytesPerPixel(GrBytesPerPixel(config))
725df175ee71164dce97758564f308ab301bfe393ajoshualitt#ifdef SK_DEBUG
732ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        , fDirty(false)
745df175ee71164dce97758564f308ab301bfe393ajoshualitt#endif
755df175ee71164dce97758564f308ab301bfe393ajoshualitt{
765df175ee71164dce97758564f308ab301bfe393ajoshualitt    fDirtyRect.setEmpty();
775df175ee71164dce97758564f308ab301bfe393ajoshualitt}
785bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
792ee084e73056b0ad76b721017f576168b7306da3Brian SalomonGrDrawOpAtlas::Plot::~Plot() {
80c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    sk_free(fData);
815df175ee71164dce97758564f308ab301bfe393ajoshualitt    delete fRects;
825df175ee71164dce97758564f308ab301bfe393ajoshualitt}
832b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips
842ee084e73056b0ad76b721017f576168b7306da3Brian Salomonbool GrDrawOpAtlas::Plot::addSubImage(int width, int height, const void* image, SkIPoint16* loc) {
855df175ee71164dce97758564f308ab301bfe393ajoshualitt    SkASSERT(width <= fWidth && height <= fHeight);
865bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
875df175ee71164dce97758564f308ab301bfe393ajoshualitt    if (!fRects) {
885df175ee71164dce97758564f308ab301bfe393ajoshualitt        fRects = GrRectanizer::Factory(fWidth, fHeight);
895bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
905bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
915df175ee71164dce97758564f308ab301bfe393ajoshualitt    if (!fRects->addRect(width, height, loc)) {
925df175ee71164dce97758564f308ab301bfe393ajoshualitt        return false;
93b4c507e03386f2105e33f0c4c09b4a9d0a23196djoshualitt    }
945bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
95c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    if (!fData) {
96c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth        fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel * fWidth *
97c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth                                                                 fHeight));
985df175ee71164dce97758564f308ab301bfe393ajoshualitt    }
995df175ee71164dce97758564f308ab301bfe393ajoshualitt    size_t rowBytes = width * fBytesPerPixel;
1005df175ee71164dce97758564f308ab301bfe393ajoshualitt    const unsigned char* imagePtr = (const unsigned char*)image;
1015df175ee71164dce97758564f308ab301bfe393ajoshualitt    // point ourselves at the right starting spot
102c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    unsigned char* dataPtr = fData;
1035df175ee71164dce97758564f308ab301bfe393ajoshualitt    dataPtr += fBytesPerPixel * fWidth * loc->fY;
1045df175ee71164dce97758564f308ab301bfe393ajoshualitt    dataPtr += fBytesPerPixel * loc->fX;
105cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman    // copy into the data buffer, swizzling as we go if this is ARGB data
106cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman    if (4 == fBytesPerPixel && kSkia8888_GrPixelConfig == kBGRA_8888_GrPixelConfig) {
107cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman        for (int i = 0; i < height; ++i) {
108cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            SkOpts::RGBA_to_BGRA(reinterpret_cast<uint32_t*>(dataPtr), imagePtr, width);
109cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            dataPtr += fBytesPerPixel * fWidth;
110cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            imagePtr += rowBytes;
111cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman        }
112cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman    } else {
113cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman        for (int i = 0; i < height; ++i) {
114cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            memcpy(dataPtr, imagePtr, rowBytes);
115cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            dataPtr += fBytesPerPixel * fWidth;
116cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman            imagePtr += rowBytes;
117cce3e58f6660a77e1a6f93d8579a279a2450b0daBrian Osman        }
1185bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
1195bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1205df175ee71164dce97758564f308ab301bfe393ajoshualitt    fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height);
1215bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1225df175ee71164dce97758564f308ab301bfe393ajoshualitt    loc->fX += fOffset.fX;
1235df175ee71164dce97758564f308ab301bfe393ajoshualitt    loc->fY += fOffset.fY;
1245df175ee71164dce97758564f308ab301bfe393ajoshualitt    SkDEBUGCODE(fDirty = true;)
1255bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1265df175ee71164dce97758564f308ab301bfe393ajoshualitt    return true;
1275df175ee71164dce97758564f308ab301bfe393ajoshualitt}
1285bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1292ee084e73056b0ad76b721017f576168b7306da3Brian Salomonvoid GrDrawOpAtlas::Plot::uploadToTexture(GrDrawOp::WritePixelsFn& writePixels,
1302ee084e73056b0ad76b721017f576168b7306da3Brian Salomon                                          GrTexture* texture) {
1315df175ee71164dce97758564f308ab301bfe393ajoshualitt    // We should only be issuing uploads if we are in fact dirty
132c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    SkASSERT(fDirty && fData && texture);
1332ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrDrawOpAtlas::Plot::uploadToTexture");
1345df175ee71164dce97758564f308ab301bfe393ajoshualitt    size_t rowBytes = fBytesPerPixel * fWidth;
135c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    const unsigned char* dataPtr = fData;
136c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    dataPtr += rowBytes * fDirtyRect.fTop;
137c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
138c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.fTop,
139c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth                fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBytes);
1405df175ee71164dce97758564f308ab301bfe393ajoshualitt    fDirtyRect.setEmpty();
1415df175ee71164dce97758564f308ab301bfe393ajoshualitt    SkDEBUGCODE(fDirty = false;)
1425df175ee71164dce97758564f308ab301bfe393ajoshualitt}
1435bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1442ee084e73056b0ad76b721017f576168b7306da3Brian Salomonvoid GrDrawOpAtlas::Plot::resetRects() {
1455df175ee71164dce97758564f308ab301bfe393ajoshualitt    if (fRects) {
1465df175ee71164dce97758564f308ab301bfe393ajoshualitt        fRects->reset();
1475bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
1485bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1495df175ee71164dce97758564f308ab301bfe393ajoshualitt    fGenID++;
1505df175ee71164dce97758564f308ab301bfe393ajoshualitt    fID = CreateId(fIndex, fGenID);
1512b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips
1525df175ee71164dce97758564f308ab301bfe393ajoshualitt    // zero out the plot
153c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth    if (fData) {
154c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth        sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
1555bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
1565bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1575df175ee71164dce97758564f308ab301bfe393ajoshualitt    fDirtyRect.setEmpty();
1585df175ee71164dce97758564f308ab301bfe393ajoshualitt    SkDEBUGCODE(fDirty = false;)
1595df175ee71164dce97758564f308ab301bfe393ajoshualitt}
1605bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1615bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt///////////////////////////////////////////////////////////////////////////////
1625bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
16332f2818c9d10090efeea62ccc211d48a33322dfbRobert PhillipsGrDrawOpAtlas::GrDrawOpAtlas(GrContext* context, sk_sp<GrTextureProxy> proxy,
16432f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips                             int numPlotsX, int numPlotsY)
16532f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        : fContext(context)
16632f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        , fProxy(std::move(proxy))
16732f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        , fAtlasGeneration(kInvalidAtlasGeneration + 1) {
16832f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    fPlotWidth = fProxy->width() / numPlotsX;
16932f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    fPlotHeight = fProxy->height() / numPlotsY;
1702b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots);
17132f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    SkASSERT(fPlotWidth * numPlotsX == fProxy->width());
17232f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    SkASSERT(fPlotHeight * numPlotsY == fProxy->height());
1732b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips
1742b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;)
1755bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1765bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // set up allocated plots
1772ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    fPlotArray.reset(new sk_sp<Plot>[ numPlotsX * numPlotsY ]);
1785bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1792ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    sk_sp<Plot>* currPlot = fPlotArray.get();
1802b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) {
1812b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips        for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) {
1822b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips            uint32_t index = r * numPlotsX + c;
1832ee084e73056b0ad76b721017f576168b7306da3Brian Salomon            currPlot->reset(
18463e7973d1f01bd03216659b9d2267f83a752c8fbBrian Salomon                    new Plot(index, 1, x, y, fPlotWidth, fPlotHeight, fProxy->config()));
1855bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1865bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt            // build LRU list
1875bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt            fPlotList.addToHead(currPlot->get());
1885bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt            ++currPlot;
1895bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        }
1905bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
1915bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt}
1925bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
1932ee084e73056b0ad76b721017f576168b7306da3Brian Salomonvoid GrDrawOpAtlas::processEviction(AtlasID id) {
1945bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    for (int i = 0; i < fEvictionCallbacks.count(); i++) {
1955bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData);
1965bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
1975bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt}
1985bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
199256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillipsinline bool GrDrawOpAtlas::updatePlot(GrDrawOp::Target* target, AtlasID* id, Plot* plot) {
2005bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    this->makeMRU(plot);
2015bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2025bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // If our most recent upload has already occurred then we have to insert a new
2035bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // upload. Otherwise, we already have a scheduled upload that hasn't yet ocurred.
2045bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // This new update will piggy back on that previously scheduled update.
205342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon    if (target->hasDrawBeenFlushed(plot->lastUploadToken())) {
206c3d706f7ce87cdd94158d2266ab2fe2f18f5020ajvanverth        // With c+14 we could move sk_sp into lamba to only ref once.
2072ee084e73056b0ad76b721017f576168b7306da3Brian Salomon        sk_sp<Plot> plotsp(SkRef(plot));
208256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
20932f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
21032f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        // Once it is deferred more care must be taken upon instantiation failure.
211eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips        if (!fProxy->instantiate(fContext->resourceProvider())) {
212256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            return false;
21332f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips        }
214eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips        GrTexture* texture = fProxy->priv().peekTexture();
215256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
216256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        GrDrawOpUploadToken lastUploadToken = target->addAsapUpload(
217256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
218256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips                plotsp->uploadToTexture(writePixels, texture);
219256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            }
220256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        );
221256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        plot->setLastUploadToken(lastUploadToken);
2225bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
2235bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    *id = plot->id();
224256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    return true;
2255bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt}
2265bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2272ee084e73056b0ad76b721017f576168b7306da3Brian Salomonbool GrDrawOpAtlas::addToAtlas(AtlasID* id, GrDrawOp::Target* target, int width, int height,
2282ee084e73056b0ad76b721017f576168b7306da3Brian Salomon                               const void* image, SkIPoint16* loc) {
2295bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // We should already have a texture, TODO clean this up
23032f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    SkASSERT(fProxy);
2316d6b6ad0f30504b6d0540f1f6bc7c072f33e85b2bsalomon    if (width > fPlotWidth || height > fPlotHeight) {
2326d6b6ad0f30504b6d0540f1f6bc7c072f33e85b2bsalomon        return false;
2336d6b6ad0f30504b6d0540f1f6bc7c072f33e85b2bsalomon    }
2345bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2355bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // now look through all allocated plots for one we can share, in Most Recently Refed order
2362ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    PlotList::Iter plotIter;
2372ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    plotIter.init(fPlotList, PlotList::Iter::kHead_IterStart);
2382ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    Plot* plot;
2395bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    while ((plot = plotIter.get())) {
24063e7973d1f01bd03216659b9d2267f83a752c8fbBrian Salomon        SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
2412b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips        if (plot->addSubImage(width, height, image, loc)) {
242256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            return this->updatePlot(target, id, plot);
2435bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        }
2445bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        plotIter.next();
2455bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
2465bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2475bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // If the above fails, then see if the least recently refed plot has already been flushed to the
2485bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    // gpu
2492b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    plot = fPlotList.tail();
2505bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    SkASSERT(plot);
251342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon    if (target->hasDrawBeenFlushed(plot->lastUseToken())) {
2525bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        this->processEviction(plot->id());
2535bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        plot->resetRects();
25463e7973d1f01bd03216659b9d2267f83a752c8fbBrian Salomon        SkASSERT(GrBytesPerPixel(fProxy->config()) == plot->bpp());
2552b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips        SkDEBUGCODE(bool verify = )plot->addSubImage(width, height, image, loc);
2565bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        SkASSERT(verify);
257256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        if (!this->updatePlot(target, id, plot)) {
258256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            return false;
259256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        }
260256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
2617c3a2f834e0ba3f11a3129d5348b393efcc9b0e1joshualitt        fAtlasGeneration++;
2625bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        return true;
2635bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
2645bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2652ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // If this plot has been used in a draw that is currently being prepared by an op, then we have
2662ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // to fail. This gives the op a chance to enqueue the draw, and call back into this function.
2672ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // When that draw is enqueued, the draw token advances, and the subsequent call will continue
2682ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // past this branch and prepare an inline upload that will occur after the enqueued draw which
2692ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // references the plot's pre-upload content.
270342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon    if (plot->lastUseToken() == target->nextDrawToken()) {
2715bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt        return false;
2725bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    }
2735bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2745bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    this->processEviction(plot->id());
2755bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    fPlotList.remove(plot);
2762ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    sk_sp<Plot>& newPlot = fPlotArray[plot->index()];
2772b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    newPlot.reset(plot->clone());
2785bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt
2795bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    fPlotList.addToHead(newPlot.get());
28063e7973d1f01bd03216659b9d2267f83a752c8fbBrian Salomon    SkASSERT(GrBytesPerPixel(fProxy->config()) == newPlot->bpp());
2812b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips    SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc);
2825bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    SkASSERT(verify);
2832b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips
2841f0e350af690044b2ba807893ac470800f8914a2robertphillips    // Note that this plot will be uploaded inline with the draws whereas the
2851f0e350af690044b2ba807893ac470800f8914a2robertphillips    // one it displaced most likely was uploaded asap.
2862ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    // With c+14 we could move sk_sp into lambda to only ref once.
2872ee084e73056b0ad76b721017f576168b7306da3Brian Salomon    sk_sp<Plot> plotsp(SkRef(newPlot.get()));
28832f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    // MDB TODO: this is currently fine since the atlas' proxy is always pre-instantiated.
28932f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    // Once it is deferred more care must be taken upon instantiation failure.
290eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips    if (!fProxy->instantiate(fContext->resourceProvider())) {
291256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        return false;
29232f2818c9d10090efeea62ccc211d48a33322dfbRobert Phillips    }
293eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips    GrTexture* texture = fProxy->priv().peekTexture();
294342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon
295256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    GrDrawOpUploadToken lastUploadToken = target->addInlineUpload(
296256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        [plotsp, texture] (GrDrawOp::WritePixelsFn& writePixels) {
297256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips            plotsp->uploadToTexture(writePixels, texture);
298256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips        }
299256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    );
300256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips    newPlot->setLastUploadToken(lastUploadToken);
301256c37bc9ea2a0420b8ac1084f6d645aaeb919f0Robert Phillips
3025bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    *id = newPlot->id();
3032b0536f37aa8915b6f58dae0b88b18023cb04d17robertphillips
3047c3a2f834e0ba3f11a3129d5348b393efcc9b0e1joshualitt    fAtlasGeneration++;
3055bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt    return true;
3065bf99f1ca8f30287803b594d06c60a7b6796ad45joshualitt}
307