GrDrawingManager.cpp revision eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9
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());
96eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips    }
97eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
98a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips#ifdef SK_DEBUG
99a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips    // This block checks for any unnecessary splits in the opLists. If two sequential opLists
100a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips    // share the same backing GrSurfaceProxy it means the opList was artificially split.
101a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips    if (fOpLists.count()) {
102a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips        GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
103a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips        for (int i = 1; i < fOpLists.count(); ++i) {
104a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips            GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
105a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips
106a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips            if (prevOpList && curOpList) {
107a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips                SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
108a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips            }
109a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips
110a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips            prevOpList = curOpList;
111a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips        }
112a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips    }
113a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips#endif
114a4c93ac535d3b0ed979c7bf666f09aa7dbf1f0ebRobert Phillips
115f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips#ifdef ENABLE_MDB
1169d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary    SkDEBUGCODE(bool result =)
117f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips                        SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
1183dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    SkASSERT(result);
119f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips#endif
1203dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
121fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    GrOnFlushResourceProvider onFlushProvider(this);
122eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
123fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    if (!fOnFlushCBObjects.empty()) {
124eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // MDB TODO: pre-MDB '1' is the correct pre-allocated size. Post-MDB it will need
125eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        // to be larger.
126eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        SkAutoSTArray<1, uint32_t> opListIds(fOpLists.count());
127eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        for (int i = 0; i < fOpLists.count(); ++i) {
128eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            opListIds[i] = fOpLists[i]->uniqueID();
129eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        }
130eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
131eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        SkSTArray<1, sk_sp<GrRenderTargetContext>> renderTargetContexts;
132fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton        for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
133fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton            onFlushCBObject->preFlush(&onFlushProvider,
134fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton                                      opListIds.get(), opListIds.count(),
135fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton                                      &renderTargetContexts);
136eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            if (!renderTargetContexts.count()) {
137eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                continue;       // This is fine. No atlases of this type are required for this flush
138eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            }
139eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
140eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            for (int j = 0; j < renderTargetContexts.count(); ++j) {
141eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                GrRenderTargetOpList* opList = renderTargetContexts[j]->getOpList();
142eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                if (!opList) {
143eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                    continue;   // Odd - but not a big deal
144eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                }
145b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillips                opList->makeClosed(*fContext->caps());
146eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                opList->prepareOps(&fFlushState);
147eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                if (!opList->executeOps(&fFlushState)) {
148eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                    continue;         // This is bad
149eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips                }
150eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            }
151eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips            renderTargetContexts.reset();
152eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips        }
153eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips    }
154eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
155a13e202563979fd5076936606dcc1d660da8c632robertphillips#if 0
15609d994ecb30de2e62a31af2c16307af31fe0e0b3Brian Salomon    // Enable this to print out verbose GrOp information
157f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
158f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips        SkDEBUGCODE(fOpLists[i]->dump();)
159a13e202563979fd5076936606dcc1d660da8c632robertphillips    }
160a13e202563979fd5076936606dcc1d660da8c632robertphillips#endif
161a13e202563979fd5076936606dcc1d660da8c632robertphillips
162318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
163318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips        if (!fOpLists[i]->instantiate(fContext->resourceProvider())) {
164318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips            fOpLists[i] = nullptr;
165318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips            continue;
166318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips        }
167318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips
168318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips        fOpLists[i]->prepareOps(&fFlushState);
169318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips    }
170318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips
171a13e202563979fd5076936606dcc1d660da8c632robertphillips    // Upload all data to the GPU
172a13e202563979fd5076936606dcc1d660da8c632robertphillips    fFlushState.preIssueDraws();
173a13e202563979fd5076936606dcc1d660da8c632robertphillips
174f2361d2d93c200cd4555b5e8ecea4531801abaaaRobert Phillips    for (int i = 0; i < fOpLists.count(); ++i) {
175318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips        if (!fOpLists[i]) {
176318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips            continue;
177318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips        }
178318c419d3e373487a8bb28f56ea921ed850e61acRobert Phillips
1791e41f4a111e5b19e55d688033e7b857caef658e6Brian Salomon        if (fOpLists[i]->executeOps(&fFlushState)) {
180dc43898bbbd06202562514ae785116e0cc40f825bsalomon            flushed = true;
181dc43898bbbd06202562514ae785116e0cc40f825bsalomon        }
18218e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips        fOpLists[i]->reset();
183a13e202563979fd5076936606dcc1d660da8c632robertphillips    }
18418e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips    fOpLists.reset();
185a13e202563979fd5076936606dcc1d660da8c632robertphillips
186342bfc25de5b0452b1551bf9db4bf45eac7718b2bsalomon    SkASSERT(fFlushState.nextDrawToken() == fFlushState.nextTokenToFlush());
187a13e202563979fd5076936606dcc1d660da8c632robertphillips
18818e9484ad5b5755757dc6badb986017a088a9c6bRobert Phillips    fContext->getGpu()->finishFlush();
189a13e202563979fd5076936606dcc1d660da8c632robertphillips
190a13e202563979fd5076936606dcc1d660da8c632robertphillips    fFlushState.reset();
191ee843b2ae4d75748d8bc323287f0c310fad548a7robertphillips    // We always have to notify the cache when it requested a flush so it can reset its state.
192ee843b2ae4d75748d8bc323287f0c310fad548a7robertphillips    if (flushed || type == GrResourceCache::FlushType::kCacheRequested) {
193b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon        fContext->getResourceCache()->notifyFlushOccurred(type);
194b77a907c2fb4402b7e70e9fff70eb71482354e67bsalomon    }
195fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
196fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton        onFlushCBObject->postFlush();
197fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    }
198b8918c42b7a4a603e880f9db994f993ca0052ed2joshualitt    fFlushing = false;
1993dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
2003dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
2017ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillipsvoid GrDrawingManager::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
2026a2b1941c1da272f704439e417f212932eed2004bsalomon    if (this->wasAbandoned()) {
2036a2b1941c1da272f704439e417f212932eed2004bsalomon        return;
2046a2b1941c1da272f704439e417f212932eed2004bsalomon    }
2057ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    SkASSERT(proxy);
2066a2b1941c1da272f704439e417f212932eed2004bsalomon
2077ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    if (proxy->priv().hasPendingIO()) {
2087ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        this->flush(proxy);
2096a2b1941c1da272f704439e417f212932eed2004bsalomon    }
2106a2b1941c1da272f704439e417f212932eed2004bsalomon
211eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips    if (!proxy->instantiate(fContext->resourceProvider())) {
2127ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        return;
2137ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    }
2147ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips
215eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips    GrSurface* surface = proxy->priv().peekSurface();
216eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips
2177ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips    if (fContext->getGpu() && surface->asRenderTarget()) {
2187ee385e1dc3cd6a47dc8f7297fef6e175eaba224Robert Phillips        fContext->getGpu()->resolveRenderTarget(surface->asRenderTarget());
2196a2b1941c1da272f704439e417f212932eed2004bsalomon    }
2206a2b1941c1da272f704439e417f212932eed2004bsalomon}
2216a2b1941c1da272f704439e417f212932eed2004bsalomon
222fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Daltonvoid GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
223fe199b7d512a7cfc56deaaa134260e1d47a1e7e0Chris Dalton    fOnFlushCBObjects.push_back(onFlushCBObject);
224eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips}
225eb35f4dfaa10b9d751abbc9a1406456eb3b1649dRobert Phillips
226b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillipssk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp) {
2273dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    SkASSERT(fContext);
2283dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
2296cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // This is  a temporary fix for the partial-MDB world. In that world we're not reordering
2306cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // so ops that (in the single opList world) would've just glommed onto the end of the single
2316cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // opList but referred to a far earlier RT need to appear in their own opList.
2326cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    if (!fOpLists.empty()) {
2336cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        fOpLists.back()->makeClosed(*fContext->caps());
2343dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
2353dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
236f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(rtp,
237f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips                                                                fContext->getGpu(),
2388185f59335e8055961ea63502e74c6a186e3a546Robert Phillips                                                                fContext->getAuditTrail()));
239f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    SkASSERT(rtp->getLastOpList() == opList.get());
2403dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
241f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    fOpLists.push_back() = opList;
2423dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
243f5442bb4c152e7c8138c83d27140e55d846f7ea5Robert Phillips    return opList;
2443dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
2453dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
246b6deea8f0ed61475382fc48c7359118bfdcbff85Robert Phillipssk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
24745580d3e3024c1536e8e1b2017b704805442b634Brian Osman    SkASSERT(fContext);
24845580d3e3024c1536e8e1b2017b704805442b634Brian Osman
2496cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // This is  a temporary fix for the partial-MDB world. In that world we're not reordering
2506cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // so ops that (in the single opList world) would've just glommed onto the end of the single
2516cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    // opList but referred to a far earlier RT need to appear in their own opList.
2526cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    if (!fOpLists.empty()) {
2536cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips        fOpLists.back()->makeClosed(*fContext->caps());
2546cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    }
2556cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips
2565efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips    sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->resourceProvider(),
2575efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips                                                      textureProxy,
2585efd5ea4e146c47394d16c8b6f876264822a0beeRobert Phillips                                                      fContext->getAuditTrail()));
25945580d3e3024c1536e8e1b2017b704805442b634Brian Osman
2606cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    SkASSERT(textureProxy->getLastOpList() == opList.get());
2616cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips
2626cdc22cde8e6297d34fdaaa3ed5e69ae86c30a77Robert Phillips    fOpLists.push_back() = opList;
26345580d3e3024c1536e8e1b2017b704805442b634Brian Osman
264dc83b892a0ae431c5fe123a7e6873d63264bd64cRobert Phillips    return opList;
26545580d3e3024c1536e8e1b2017b704805442b634Brian Osman}
26645580d3e3024c1536e8e1b2017b704805442b634Brian Osman
26786e7626f080028edd58f4b326e0ba9fa83c4208ebrianosmanGrAtlasTextContext* GrDrawingManager::getAtlasTextContext() {
26886e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    if (!fAtlasTextContext) {
26986e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman        fAtlasTextContext.reset(GrAtlasTextContext::Create());
27086e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    }
27186e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman
27286e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman    return fAtlasTextContext.get();
27386e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman}
27486e7626f080028edd58f4b326e0ba9fa83c4208ebrianosman
275687378229aecefc0ab7e639181593774ec8a4290robertphillips/*
276687378229aecefc0ab7e639181593774ec8a4290robertphillips * This method finds a path renderer that can draw the specified path on
277687378229aecefc0ab7e639181593774ec8a4290robertphillips * the provided target.
278687378229aecefc0ab7e639181593774ec8a4290robertphillips * Due to its expense, the software path renderer has split out so it can
279687378229aecefc0ab7e639181593774ec8a4290robertphillips * can be individually allowed/disallowed via the "allowSW" boolean.
280687378229aecefc0ab7e639181593774ec8a4290robertphillips */
281687378229aecefc0ab7e639181593774ec8a4290robertphillipsGrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
282687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  bool allowSW,
283687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  GrPathRendererChain::DrawType drawType,
284687378229aecefc0ab7e639181593774ec8a4290robertphillips                                                  GrPathRenderer::StencilSupport* stencilSupport) {
285687378229aecefc0ab7e639181593774ec8a4290robertphillips
286687378229aecefc0ab7e639181593774ec8a4290robertphillips    if (!fPathRendererChain) {
2876b2552f10983d1b6d33303e3e9965ae71944c1d5bsalomon        fPathRendererChain = new GrPathRendererChain(fContext, fOptionsForPathRendererChain);
288687378229aecefc0ab7e639181593774ec8a4290robertphillips    }
289687378229aecefc0ab7e639181593774ec8a4290robertphillips
290687378229aecefc0ab7e639181593774ec8a4290robertphillips    GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
291687378229aecefc0ab7e639181593774ec8a4290robertphillips    if (!pr && allowSW) {
292687378229aecefc0ab7e639181593774ec8a4290robertphillips        if (!fSoftwarePathRenderer) {
29339ef7fb885d7be648b9f0ecd027bc400d1213cecbsalomon            fSoftwarePathRenderer =
29432342f032e1dfd133040324f851f0365f9d4cb51Brian Osman                    new GrSoftwarePathRenderer(fContext->resourceProvider(),
29539ef7fb885d7be648b9f0ecd027bc400d1213cecbsalomon                                               fOptionsForPathRendererChain.fAllowPathMaskCaching);
296687378229aecefc0ab7e639181593774ec8a4290robertphillips        }
2970e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon        if (fSoftwarePathRenderer->canDrawPath(args)) {
2980e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon            pr = fSoftwarePathRenderer;
2990e8fc8b9e6a138cf4a66b421fb824679df717329Brian Salomon        }
300687378229aecefc0ab7e639181593774ec8a4290robertphillips    }
301687378229aecefc0ab7e639181593774ec8a4290robertphillips
302687378229aecefc0ab7e639181593774ec8a4290robertphillips    return pr;
303687378229aecefc0ab7e639181593774ec8a4290robertphillips}
304687378229aecefc0ab7e639181593774ec8a4290robertphillips
3051105224f9701e57ec5ce0354d6a380b664f5c638Brian Osmansk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
3063743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips                                                            sk_sp<GrSurfaceProxy> sProxy,
307c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                            sk_sp<SkColorSpace> colorSpace,
308c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                            const SkSurfaceProps* surfaceProps) {
3093743013f755d23c215d852af7d829c3cd74f34a2Robert Phillips    if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
3103dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips        return nullptr;
3113dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips    }
3123dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips
3130e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // SkSurface catches bad color space usage at creation. This check handles anything that slips
3140e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // by, including internal usage. We allow a null color space here, for read/write pixels and
3150e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    // other special code paths. If a color space is provided, though, enforce all other rules.
3162c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
317a9c3c6aea5f7ade5aa2d112a221c458cd759222abrianosman        SkDEBUGFAIL("Invalid config and colorspace combination");
3180e22eb8e6efc7d7ab7a601ba555947916d139906brianosman        return nullptr;
3190e22eb8e6efc7d7ab7a601ba555947916d139906brianosman    }
32096880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
3212c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
3222c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips
32396880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    bool useDIF = false;
32496880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    if (surfaceProps) {
32596880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        useDIF = surfaceProps->isUseDeviceIndependentFonts();
32696880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    }
32796880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
32896880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() &&
3297c8460e10135c05a42d0744b84838bbc24398ac2Brian Salomon        GrFSAAType::kNone != rtp->fsaaType()) {
330c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips        // TODO: defer stencil buffer attachment for PathRenderingDrawContext
331eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips        if (!rtp->instantiate(fContext->resourceProvider())) {
332e60ad620fe236ce4c1e85a31bd53ed0c848da8c3Robert Phillips            return nullptr;
333e60ad620fe236ce4c1e85a31bd53ed0c848da8c3Robert Phillips        }
334eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips        GrRenderTarget* rt = rtp->priv().peekRenderTarget();
335eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips
336eee4d6e4e8cc5c4c79f065abcc3ce609f71238f9Robert Phillips        GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt);
33796880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        if (sb) {
3381105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman            return sk_sp<GrRenderTargetContext>(new GrPathRenderingRenderTargetContext(
339c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips                                                        fContext, this, std::move(rtp),
340dfe4f2e4fe5b162d4adb4486fe751f1e3b30bea7brianosman                                                        std::move(colorSpace), surfaceProps,
3416c7e325236b7fbb9c9b14ebb4715f88a59718695robertphillips                                                        fContext->getAuditTrail(), fSingleOwner));
34296880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt        }
34396880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt    }
34496880d9e366d58e5960aa0ee6aa67260797679ebjoshualitt
345c7635fa374b87711e5ccd8222957a8fbdc772f7bRobert Phillips    return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext, this, std::move(rtp),
3461105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  std::move(colorSpace),
3471105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  surfaceProps,
3481105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  fContext->getAuditTrail(),
3491105224f9701e57ec5ce0354d6a380b664f5c638Brian Osman                                                                  fSingleOwner));
3503dc6ae55ca01025a0d723c87d42ed72f5a4ff6ecrobertphillips}
35145580d3e3024c1536e8e1b2017b704805442b634Brian Osman
3522c86249465c28a532c8be55b6ba497021e3110ecRobert Phillipssk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
3532c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                             sk_sp<SkColorSpace> colorSpace) {
35445580d3e3024c1536e8e1b2017b704805442b634Brian Osman    if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
35545580d3e3024c1536e8e1b2017b704805442b634Brian Osman        return nullptr;
35645580d3e3024c1536e8e1b2017b704805442b634Brian Osman    }
35745580d3e3024c1536e8e1b2017b704805442b634Brian Osman
3582c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // SkSurface catches bad color space usage at creation. This check handles anything that slips
3592c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // by, including internal usage. We allow a null color space here, for read/write pixels and
3602c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    // other special code paths. If a color space is provided, though, enforce all other rules.
3612c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
3622c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips        SkDEBUGFAIL("Invalid config and colorspace combination");
3632c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips        return nullptr;
3642c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips    }
3652c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips
36645580d3e3024c1536e8e1b2017b704805442b634Brian Osman    // GrTextureRenderTargets should always be using GrRenderTargetContext
36745580d3e3024c1536e8e1b2017b704805442b634Brian Osman    SkASSERT(!sProxy->asRenderTargetProxy());
36845580d3e3024c1536e8e1b2017b704805442b634Brian Osman
36945580d3e3024c1536e8e1b2017b704805442b634Brian Osman    sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
37045580d3e3024c1536e8e1b2017b704805442b634Brian Osman
37145580d3e3024c1536e8e1b2017b704805442b634Brian Osman    return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
3722c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        std::move(colorSpace),
3732c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        fContext->getAuditTrail(),
3742c86249465c28a532c8be55b6ba497021e3110ecRobert Phillips                                                        fSingleOwner));
37545580d3e3024c1536e8e1b2017b704805442b634Brian Osman}
376