GrDrawingManager.cpp revision 18e9484ad5b5755757dc6badb986017a088a9c6b
13dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips/*
23dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips * Copyright 2015 Google Inc.
33dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips *
43dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips * Use of this source code is governed by a BSD-style license that can be
53dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips * found in the LICENSE file.
63dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips */
73dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
85eb41fdf94187d6cc22702444622ed7897c8039absalomon#include "GrDrawingManager.h"
9b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon
10b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon#include "GrContext.h"
111105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman#include "GrRenderTargetContext.h"
121105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman#include "GrPathRenderingRenderTargetContext.h"
13c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips#include "GrRenderTargetProxy.h"
143dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips#include "GrResourceProvider.h"
15687378229aecefc0ab7e639181593774ec8a4290robertphillips#include "GrSoftwarePathRenderer.h"
166a2b1941c1da272f704439e417f212932eed2004bsalomon#include "GrSurfacePriv.h"
177ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips#include "GrSurfaceProxyPriv.h"
1845580d3e3024c1536e8e1b2017b704805442b634Brian Osman#include "GrTextureContext.h"
1945580d3e3024c1536e8e1b2017b704805442b634Brian Osman#include "GrTextureOpList.h"
200e22eb8e6efc7d7ab7a601ba555947916d139906brianosman#include "SkSurface_Gpu.h"
213dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips#include "SkTTopoSort.h"
223dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
23e804292e805917002cc3d7baa7f967fb20d2c7cbjoshualitt#include "text/GrAtlasTextContext.h"
24e804292e805917002cc3d7baa7f967fb20d2c7cbjoshualitt#include "text/GrStencilAndCoverTextContext.h"
25498d7ac86bfb45bdfc8f9aa7424f7236235987b5robertphillips
263dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillipsvoid GrDrawingManager::cleanup() {
27f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
28ee683655a9a9d57487ab8d4b0b01bb9bc2def144Robert Phillips        // no opList should receive a new command after this
29ee683655a9a9d57487ab8d4b0b01bb9bc2def144Robert Phillips        fOpLists[i]->makeClosed(*fContext->caps());
300dfa62c97707ac9b0fc0bdba7acfc8405db5c836robertphillips
31f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        // We shouldn't need to do this, but it turns out some clients still hold onto opLists
326cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        // after a cleanup.
336cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        // MDB TODO: is this still true?
34f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        fOpLists[i]->reset();
353dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
363dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
37f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    fOpLists.reset();
383dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
3913391dd97209f36c71bc18cc75084f74c2f6af15robertphillips    delete fPathRendererChain;
4013391dd97209f36c71bc18cc75084f74c2f6af15robertphillips    fPathRendererChain = nullptr;
41687378229aecefc0ab7e639181593774ec8a4290robertphillips    SkSafeSetNull(fSoftwarePathRenderer);
423dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
433dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
443dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillipsGrDrawingManager::~GrDrawingManager() {
453dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    this->cleanup();
463dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
473dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
483dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillipsvoid GrDrawingManager::abandon() {
493dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    fAbandoned = true;
50f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
51f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        fOpLists[i]->abandonGpuResources();
52a7f29640f6ab4eb50962a9d9f12d01ac2ce8b471csmartdalton    }
533dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    this->cleanup();
543dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
553dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
56687378229aecefc0ab7e639181593774ec8a4290robertphillipsvoid GrDrawingManager::freeGpuResources() {
57687378229aecefc0ab7e639181593774ec8a4290robertphillips    // a path renderer may be holding onto resources
5813391dd97209f36c71bc18cc75084f74c2f6af15robertphillips    delete fPathRendererChain;
5913391dd97209f36c71bc18cc75084f74c2f6af15robertphillips    fPathRendererChain = nullptr;
60687378229aecefc0ab7e639181593774ec8a4290robertphillips    SkSafeSetNull(fSoftwarePathRenderer);
61f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
62f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        fOpLists[i]->freeGpuResources();
63a7f29640f6ab4eb50962a9d9f12d01ac2ce8b471csmartdalton    }
64687378229aecefc0ab7e639181593774ec8a4290robertphillips}
65687378229aecefc0ab7e639181593774ec8a4290robertphillips
663dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillipsvoid GrDrawingManager::reset() {
67f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
68f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        fOpLists[i]->reset();
693dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
70a13e202563979fd5076936606dcc1d660da8c632robertphillips    fFlushState.reset();
713dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
723dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
73e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillipsgr_instanced::OpAllocator* GrDrawingManager::instancingAllocator() {
74e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips    if (fInstancingAllocator) {
75e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips        return fInstancingAllocator.get();
76e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips    }
77e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips
78e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips    fInstancingAllocator = fContext->getGpu()->createInstancedRenderingAllocator();
79e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips    return fInstancingAllocator.get();
80e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips}
81e3302dfb81865051ad19cd1c91e2ec3a27df3417Robert Phillips
827ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips// MDB TODO: make use of the 'proxy' parameter.
837ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillipsvoid GrDrawingManager::internalFlush(GrSurfaceProxy*, GrResourceCache::FlushType type) {
847761d61876ea479dd9672b684fd5c7601932a767robertphillips    if (fFlushing || this->wasAbandoned()) {
85b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon        return;
86b8918c42b7a4a603e880f9db994f993ca0052ed2joshualitt    }
87b8918c42b7a4a603e880f9db994f993ca0052ed2joshualitt    fFlushing = true;
88dc43898bbbd06202562514ae785116e0cc40f825bsalomon    bool flushed = false;
89eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
90eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
91eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
92eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
93eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // but need to be flushed anyway. Closing such GrOpLists here will mean new
94eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
95ee683655a9a9d57487ab8d4b0b01bb9bc2def144Robert Phillips        fOpLists[i]->makeClosed(*fContext->caps());
966cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        SkDEBUGCODE(fOpLists[i]->validateTargetsSingleRenderTarget());
97eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips    }
98eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
99f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips#ifdef ENABLE_MDB
1009d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary    SkDEBUGCODE(bool result =)
101f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips                        SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
1023dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    SkASSERT(result);
103f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips#endif
1043dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
105fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    GrOnFlushResourceProvider onFlushProvider(this);
106eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
107fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    if (!fOnFlushCBObjects.empty()) {
108eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // MDB TODO: pre-MDB '1' is the correct pre-allocated size. Post-MDB it will need
109eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // to be larger.
110eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        SkAutoSTArray<1, uint32_t> opListIds(fOpLists.count());
111eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        for (int i = 0; i < fOpLists.count(); ++i) {
112eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            opListIds[i] = fOpLists[i]->uniqueID();
113eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        }
114eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
115eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        SkSTArray<1, sk_sp<GrRenderTargetContext>> renderTargetContexts;
116fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton        for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
117fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton            onFlushCBObject->preFlush(&onFlushProvider,
118fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton                                      opListIds.get(), opListIds.count(),
119fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton                                      &renderTargetContexts);
120eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            if (!renderTargetContexts.count()) {
121eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                continue;       // This is fine. No atlases of this type are required for this flush
122eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            }
123eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
124eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            for (int j = 0; j < renderTargetContexts.count(); ++j) {
125eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                GrRenderTargetOpList* opList = renderTargetContexts[j]->getOpList();
126eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                if (!opList) {
127eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                    continue;   // Odd - but not a big deal
128eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                }
129b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillips                opList->makeClosed(*fContext->caps());
130eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                SkDEBUGCODE(opList->validateTargetsSingleRenderTarget());
131eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                opList->prepareOps(&fFlushState);
132eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                if (!opList->executeOps(&fFlushState)) {
133eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                    continue;         // This is bad
134eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                }
135eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            }
136eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            renderTargetContexts.reset();
137eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        }
138eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips    }
139eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
140f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
1411e41f4a111e5b19e55d688033e7b857caef658e6Brian Salomon        fOpLists[i]->prepareOps(&fFlushState);
14227fe1f849d2b6f96b49cc919df7d5f6d74598aa1robertphillips    }
14327fe1f849d2b6f96b49cc919df7d5f6d74598aa1robertphillips
144a13e202563979fd5076936606dcc1d660da8c632robertphillips#if 0
14509d994ecb30de2e62a31af2c16307af31fe0e0b3Brian Salomon    // Enable this to print out verbose GrOp information
146f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
147f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        SkDEBUGCODE(fOpLists[i]->dump();)
148a13e202563979fd5076936606dcc1d660da8c632robertphillips    }
149a13e202563979fd5076936606dcc1d660da8c632robertphillips#endif
150a13e202563979fd5076936606dcc1d660da8c632robertphillips
151a13e202563979fd5076936606dcc1d660da8c632robertphillips    // Upload all data to the GPU
152a13e202563979fd5076936606dcc1d660da8c632robertphillips    fFlushState.preIssueDraws();
153a13e202563979fd5076936606dcc1d660da8c632robertphillips
154f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
1551e41f4a111e5b19e55d688033e7b857caef658e6Brian Salomon        if (fOpLists[i]->executeOps(&fFlushState)) {
156dc43898bbbd06202562514ae785116e0cc40f825bsalomon            flushed = true;
157dc43898bbbd06202562514ae785116e0cc40f825bsalomon        }
15818e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips        fOpLists[i]->reset();
159a13e202563979fd5076936606dcc1d660da8c632robertphillips    }
16018e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips    fOpLists.reset();
161a13e202563979fd5076936606dcc1d660da8c632robertphillips
162342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon    SkASSERT(fFlushState.nextDrawToken() == fFlushState.nextTokenToFlush());
163a13e202563979fd5076936606dcc1d660da8c632robertphillips
16418e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips    fContext->getGpu()->finishFlush();
165a13e202563979fd5076936606dcc1d660da8c632robertphillips
166a13e202563979fd5076936606dcc1d660da8c632robertphillips    fFlushState.reset();
167ee843b2ae4d75748d8bc323287f0c310fad548a7robertphillips    // We always have to notify the cache when it requested a flush so it can reset its state.
168ee843b2ae4d75748d8bc323287f0c310fad548a7robertphillips    if (flushed || type == GrResourceCache::FlushType::kCacheRequested) {
169b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon        fContext->getResourceCache()->notifyFlushOccurred(type);
170b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon    }
171fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
172fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton        onFlushCBObject->postFlush();
173fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    }
174b8918c42b7a4a603e880f9db994f993ca0052ed2joshualitt    fFlushing = false;
1753dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
1763dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
1777ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillipsvoid GrDrawingManager::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
1786a2b1941c1da272f704439e417f212932eed2004bsalomon    if (this->wasAbandoned()) {
1796a2b1941c1da272f704439e417f212932eed2004bsalomon        return;
1806a2b1941c1da272f704439e417f212932eed2004bsalomon    }
1817ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    SkASSERT(proxy);
1826a2b1941c1da272f704439e417f212932eed2004bsalomon
1837ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    if (proxy->priv().hasPendingIO()) {
1847ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        this->flush(proxy);
1856a2b1941c1da272f704439e417f212932eed2004bsalomon    }
1866a2b1941c1da272f704439e417f212932eed2004bsalomon
1877ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    GrSurface* surface = proxy->instantiate(fContext->resourceProvider());
1887ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    if (!surface) {
1897ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        return;
1907ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    }
1917ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips
1927ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    if (fContext->getGpu() && surface->asRenderTarget()) {
1937ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        fContext->getGpu()->resolveRenderTarget(surface->asRenderTarget());
1946a2b1941c1da272f704439e417f212932eed2004bsalomon    }
1956a2b1941c1da272f704439e417f212932eed2004bsalomon}
1966a2b1941c1da272f704439e417f212932eed2004bsalomon
197fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Daltonvoid GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
198fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    fOnFlushCBObjects.push_back(onFlushCBObject);
199eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips}
200eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
201b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillipssk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp) {
2023dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    SkASSERT(fContext);
2033dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
2046cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // This is  a temporary fix for the partial-MDB world. In that world we're not reordering
2056cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // so ops that (in the single opList world) would've just glommed onto the end of the single
2066cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // opList but referred to a far earlier RT need to appear in their own opList.
2076cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    if (!fOpLists.empty()) {
2086cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        fOpLists.back()->makeClosed(*fContext->caps());
2093dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
2103dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
211f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(rtp,
212f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips                                                                fContext->getGpu(),
2138185f59335e8055961ea63502e74c6a186e3a546Robert Phillips                                                                fContext->getAuditTrail()));
214f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    SkASSERT(rtp->getLastOpList() == opList.get());
2153dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
216f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    fOpLists.push_back() = opList;
2173dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
218f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    return opList;
2193dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
2203dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
221b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillipssk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
22245580d3e3024c1536e8e1b2017b704805442b634Brian Osman    SkASSERT(fContext);
22345580d3e3024c1536e8e1b2017b704805442b634Brian Osman
2246cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // This is  a temporary fix for the partial-MDB world. In that world we're not reordering
2256cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // so ops that (in the single opList world) would've just glommed onto the end of the single
2266cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // opList but referred to a far earlier RT need to appear in their own opList.
2276cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    if (!fOpLists.empty()) {
2286cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        fOpLists.back()->makeClosed(*fContext->caps());
2296cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    }
2306cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips
23118e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips    sk_sp<GrTextureOpList> opList(new GrTextureOpList(textureProxy, fContext->getAuditTrail()));
23245580d3e3024c1536e8e1b2017b704805442b634Brian Osman
2336cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    SkASSERT(textureProxy->getLastOpList() == opList.get());
2346cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips
2356cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    fOpLists.push_back() = opList;
23645580d3e3024c1536e8e1b2017b704805442b634Brian Osman
237dc83b892a0ae431c5fe123a7e6873d63264bd64cRobert Phillips    return opList;
23845580d3e3024c1536e8e1b2017b704805442b634Brian Osman}
23945580d3e3024c1536e8e1b2017b704805442b634Brian Osman
24086e7626f080028edd58f4b326e0ba9fa83c4208ebrianosmanGrAtlasTextContext* GrDrawingManager::getAtlasTextContext() {
24186e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    if (!fAtlasTextContext) {
24286e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman        fAtlasTextContext.reset(GrAtlasTextContext::Create());
24386e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    }
24486e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman
24586e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    return fAtlasTextContext.get();
24686e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman}
24786e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman
248687378229aecefc0ab7e639181593774ec8a4290robertphillips/*
249687378229aecefc0ab7e639181593774ec8a4290robertphillips * This method finds a path renderer that can draw the specified path on
250687378229aecefc0ab7e639181593774ec8a4290robertphillips * the provided target.
251687378229aecefc0ab7e639181593774ec8a4290robertphillips * Due to its expense, the software path renderer has split out so it can
252687378229aecefc0ab7e639181593774ec8a4290robertphillips * can be individually allowed/disallowed via the "allowSW" boolean.
253687378229aecefc0ab7e639181593774ec8a4290robertphillips */
254687378229aecefc0ab7e639181593774ec8a4290robertphillipsGrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
255687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  bool allowSW,
256687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  GrPathRendererChain::DrawType drawType,
257687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  GrPathRenderer::StencilSupport* stencilSupport) {
258687378229aecefc0ab7e639181593774ec8a4290robertphillips
259687378229aecefc0ab7e639181593774ec8a4290robertphillips    if (!fPathRendererChain) {
2606b2552f10983d1b6d33303e3e9965ae71944c1d5bsalomon        fPathRendererChain = new GrPathRendererChain(fContext, fOptionsForPathRendererChain);
261687378229aecefc0ab7e639181593774ec8a4290robertphillips    }
262687378229aecefc0ab7e639181593774ec8a4290robertphillips
263687378229aecefc0ab7e639181593774ec8a4290robertphillips    GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
264687378229aecefc0ab7e639181593774ec8a4290robertphillips    if (!pr && allowSW) {
265687378229aecefc0ab7e639181593774ec8a4290robertphillips        if (!fSoftwarePathRenderer) {
26639ef7fb885d7be648b9f0ecd027bc400d1213cecbsalomon            fSoftwarePathRenderer =
26732342f032e1dfd133040324f851f0365f9d4cb51Brian Osman                    new GrSoftwarePathRenderer(fContext->resourceProvider(),
26839ef7fb885d7be648b9f0ecd027bc400d1213cecbsalomon                                               fOptionsForPathRendererChain.fAllowPathMaskCaching);
269687378229aecefc0ab7e639181593774ec8a4290robertphillips        }
2700e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon        if (fSoftwarePathRenderer->canDrawPath(args)) {
2710e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon            pr = fSoftwarePathRenderer;
2720e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon        }
273687378229aecefc0ab7e639181593774ec8a4290robertphillips    }
274687378229aecefc0ab7e639181593774ec8a4290robertphillips
275687378229aecefc0ab7e639181593774ec8a4290robertphillips    return pr;
276687378229aecefc0ab7e639181593774ec8a4290robertphillips}
277687378229aecefc0ab7e639181593774ec8a4290robertphillips
2781105224f9701e57ec5ce0354d6a380b664f5c638Brian Osmansk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
2793743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips                                                            sk_sp<GrSurfaceProxy> sProxy,
280c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                            sk_sp<SkColorSpace> colorSpace,
281c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                            const SkSurfaceProps* surfaceProps) {
2823743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips    if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
2833dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips        return nullptr;
2843dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
2853dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
2860e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // SkSurface catches bad color space usage at creation. This check handles anything that slips
2870e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // by, including internal usage. We allow a null color space here, for read/write pixels and
2880e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // other special code paths. If a color space is provided, though, enforce all other rules.
2892c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
290a9c3c6aea5f7ade5aa2d112a221c458cd759222abrianosman        SkDEBUGFAIL("Invalid config and colorspace combination");
2910e22eb8e6efc7d7ab7a601ba555947916d139906brianosman        return nullptr;
2920e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    }
29396880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
2942c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
2952c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips
29696880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    bool useDIF = false;
29796880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    if (surfaceProps) {
29896880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        useDIF = surfaceProps->isUseDeviceIndependentFonts();
29996880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    }
30096880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
30196880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() &&
3027c8460e10135c05a42d0744b84838bbc24398ac2Brian Salomon        GrFSAAType::kNone != rtp->fsaaType()) {
303c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips        // TODO: defer stencil buffer attachment for PathRenderingDrawContext
30432342f032e1dfd133040324f851f0365f9d4cb51Brian Osman        sk_sp<GrRenderTarget> rt(sk_ref_sp(rtp->instantiate(fContext->resourceProvider())));
305e60ad620fe236ce4c1e85a31bd53ed0c848da8c3Robert Phillips        if (!rt) {
306e60ad620fe236ce4c1e85a31bd53ed0c848da8c3Robert Phillips            return nullptr;
307e60ad620fe236ce4c1e85a31bd53ed0c848da8c3Robert Phillips        }
3086c7e325236b7fbb9c9b14ebb4715f88a59718695robertphillips        GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt.get());
30996880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        if (sb) {
3101105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman            return sk_sp<GrRenderTargetContext>(new GrPathRenderingRenderTargetContext(
311c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                        fContext, this, std::move(rtp),
312dfe4f2e4fe5b162d4adb4486fe751f1e3b30bea7brianosman                                                        std::move(colorSpace), surfaceProps,
3136c7e325236b7fbb9c9b14ebb4715f88a59718695robertphillips                                                        fContext->getAuditTrail(), fSingleOwner));
31496880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        }
31596880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    }
31696880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
317c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips    return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext, this, std::move(rtp),
3181105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  std::move(colorSpace),
3191105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  surfaceProps,
3201105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  fContext->getAuditTrail(),
3211105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  fSingleOwner));
3223dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
32345580d3e3024c1536e8e1b2017b704805442b634Brian Osman
3242c86249465c28a532c8be55b6ba497021e3110ecRobert Phillipssk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
3252c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                             sk_sp<SkColorSpace> colorSpace) {
32645580d3e3024c1536e8e1b2017b704805442b634Brian Osman    if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
32745580d3e3024c1536e8e1b2017b704805442b634Brian Osman        return nullptr;
32845580d3e3024c1536e8e1b2017b704805442b634Brian Osman    }
32945580d3e3024c1536e8e1b2017b704805442b634Brian Osman
3302c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // SkSurface catches bad color space usage at creation. This check handles anything that slips
3312c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // by, including internal usage. We allow a null color space here, for read/write pixels and
3322c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // other special code paths. If a color space is provided, though, enforce all other rules.
3332c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
3342c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips        SkDEBUGFAIL("Invalid config and colorspace combination");
3352c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips        return nullptr;
3362c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    }
3372c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips
33845580d3e3024c1536e8e1b2017b704805442b634Brian Osman    // GrTextureRenderTargets should always be using GrRenderTargetContext
33945580d3e3024c1536e8e1b2017b704805442b634Brian Osman    SkASSERT(!sProxy->asRenderTargetProxy());
34045580d3e3024c1536e8e1b2017b704805442b634Brian Osman
34145580d3e3024c1536e8e1b2017b704805442b634Brian Osman    sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
34245580d3e3024c1536e8e1b2017b704805442b634Brian Osman
34345580d3e3024c1536e8e1b2017b704805442b634Brian Osman    return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
3442c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        std::move(colorSpace),
3452c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        fContext->getAuditTrail(),
3462c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        fSingleOwner));
34745580d3e3024c1536e8e1b2017b704805442b634Brian Osman}
348