blob: 4430b52ee6f00d7af30e7b9578170c1055165f67 [file] [log] [blame]
Robert Phillips96601082018-05-29 16:13:26 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef DDLTileHelper_DEFINED
9#define DDLTileHelper_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRect.h"
12#include "include/core/SkRefCnt.h"
13#include "include/core/SkSurfaceCharacterization.h"
Robert Phillips96601082018-05-29 16:13:26 -040014
Robert Phillips96601082018-05-29 16:13:26 -040015class DDLPromiseImageHelper;
Robert Phillips11c67672020-04-23 15:10:03 -040016class PromiseImageCallbackContext;
Robert Phillips96601082018-05-29 16:13:26 -040017class SkCanvas;
18class SkData;
19class SkDeferredDisplayList;
Robert Phillips11c67672020-04-23 15:10:03 -040020class SkDeferredDisplayListRecorder;
Robert Phillips96601082018-05-29 16:13:26 -040021class SkPicture;
22class SkSurface;
23class SkSurfaceCharacterization;
24
25class DDLTileHelper {
26public:
Robert Phillipsa865a3a2020-02-14 10:49:39 -050027 // The TileData class encapsulates the information and behavior of a single tile when
28 // rendering with DDLs.
Robert Phillips96601082018-05-29 16:13:26 -040029 class TileData {
30 public:
Robert Phillipsa865a3a2020-02-14 10:49:39 -050031 TileData() {}
32 ~TileData();
Robert Phillips96601082018-05-29 16:13:26 -040033
Robert Phillips19f466d2020-02-26 10:27:07 -050034 void init(int id,
Robert Phillips11c67672020-04-23 15:10:03 -040035 GrContext* context,
Robert Phillips19f466d2020-02-26 10:27:07 -050036 const SkSurfaceCharacterization& dstChar,
37 const SkIRect& clip);
Robert Phillipsa865a3a2020-02-14 10:49:39 -050038
39 // Convert the compressedPictureData into an SkPicture replacing each image-index
40 // with a promise image.
Robert Phillips96601082018-05-29 16:13:26 -040041 void createTileSpecificSKP(SkData* compressedPictureData,
42 const DDLPromiseImageHelper& helper);
43
Robert Phillipsa865a3a2020-02-14 10:49:39 -050044 // Create the DDL for this tile (i.e., fill in 'fDisplayList').
Robert Phillips96601082018-05-29 16:13:26 -040045 void createDDL();
46
Robert Phillips6eb5cb92020-03-05 12:52:45 -050047 // Precompile all the programs required to draw this tile's DDL
48 void precompile(GrContext*);
49
Robert Phillips24a8e9e2020-03-06 20:26:28 +000050 // Just draw the re-inflated per-tile SKP directly into this tile w/o going through a DDL
51 // first. This is used for determining the overhead of using DDLs (i.e., it replaces
52 // a 'createDDL' and 'draw' pair.
53 void drawSKPDirectly(GrContext*);
54
Robert Phillips11c67672020-04-23 15:10:03 -040055 // Replay the recorded DDL into the tile surface - filling in 'fBackendTexture'.
Robert Phillipsa865a3a2020-02-14 10:49:39 -050056 void draw(GrContext*);
Robert Phillips96601082018-05-29 16:13:26 -040057
Robert Phillips96601082018-05-29 16:13:26 -040058 void reset();
59
Robert Phillipsa865a3a2020-02-14 10:49:39 -050060 int id() const { return fID; }
Robert Phillips11c67672020-04-23 15:10:03 -040061 SkIRect clipRect() const { return fClip; }
Robert Phillipsa865a3a2020-02-14 10:49:39 -050062
Robert Phillips7b0ed552020-02-20 12:45:19 -050063 SkDeferredDisplayList* ddl() { return fDisplayList.get(); }
64
Robert Phillips11c67672020-04-23 15:10:03 -040065 sk_sp<SkImage> makePromiseImage(SkDeferredDisplayListRecorder*);
66 void dropCallbackContext() { fCallbackContext.reset(); }
67
Robert Phillips8472a3d2020-04-16 16:27:45 -040068 static void CreateBackendTexture(GrContext*, TileData*);
69 static void DeleteBackendTexture(GrContext*, TileData*);
70
Robert Phillips96601082018-05-29 16:13:26 -040071 private:
Robert Phillips8472a3d2020-04-16 16:27:45 -040072 sk_sp<SkSurface> makeWrappedTileDest(GrContext* context);
73
Robert Phillips11c67672020-04-23 15:10:03 -040074 sk_sp<PromiseImageCallbackContext> refCallbackContext() { return fCallbackContext; }
75
Robert Phillipsa865a3a2020-02-14 10:49:39 -050076 int fID = -1;
Robert Phillips11c67672020-04-23 15:10:03 -040077 SkIRect fClip; // in the device space of the final SkSurface
Robert Phillipsa865a3a2020-02-14 10:49:39 -050078 SkSurfaceCharacterization fCharacterization; // characterization for the tile's surface
Robert Phillipsa865a3a2020-02-14 10:49:39 -050079
Robert Phillips11c67672020-04-23 15:10:03 -040080 // The callback context holds (via its SkPromiseImageTexture) the backend texture
81 // that is both wrapped in 'fTileSurface' and backs this tile's promise image
82 // (i.e., the one returned by 'makePromiseImage').
83 sk_sp<PromiseImageCallbackContext> fCallbackContext;
84 // 'fTileSurface' wraps the backend texture in 'fCallbackContext' and must exist until
85 // after 'fDisplayList' has been flushed (bc it owns the proxy the DDL's destination
86 // trampoline points at).
87 // TODO: fix the ref-order so we don't need 'fTileSurface' here
Robert Phillips8472a3d2020-04-16 16:27:45 -040088 sk_sp<SkSurface> fTileSurface;
Robert Phillips11c67672020-04-23 15:10:03 -040089
Robert Phillipsa865a3a2020-02-14 10:49:39 -050090 sk_sp<SkPicture> fReconstitutedPicture;
91 SkTArray<sk_sp<SkImage>> fPromiseImages; // All the promise images in the
92 // reconstituted picture
Robert Phillips96601082018-05-29 16:13:26 -040093 std::unique_ptr<SkDeferredDisplayList> fDisplayList;
94 };
95
Robert Phillips11c67672020-04-23 15:10:03 -040096 DDLTileHelper(GrContext* context,
Robert Phillips19f466d2020-02-26 10:27:07 -050097 const SkSurfaceCharacterization& dstChar,
Robert Phillipsa865a3a2020-02-14 10:49:39 -050098 const SkIRect& viewport,
99 int numDivisions);
Robert Phillips96601082018-05-29 16:13:26 -0400100
101 void createSKPPerTile(SkData* compressedPictureData, const DDLPromiseImageHelper& helper);
102
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500103 void kickOffThreadedWork(SkTaskGroup* recordingTaskGroup,
104 SkTaskGroup* gpuTaskGroup,
105 GrContext* gpuThreadContext);
106
Robert Phillips96601082018-05-29 16:13:26 -0400107 void createDDLsInParallel();
108
Robert Phillips11c67672020-04-23 15:10:03 -0400109 // Create the DDL that will compose all the tile images into a final result.
110 void createComposeDDL();
111 SkDeferredDisplayList* composeDDL() const { return fComposeDDL.get(); }
112
Robert Phillips6eb5cb92020-03-05 12:52:45 -0500113 void precompileAndDrawAllTiles(GrContext*);
Robert Phillips96601082018-05-29 16:13:26 -0400114
Robert Phillips24a8e9e2020-03-06 20:26:28 +0000115 // For each tile, create its DDL and then draw it - all on a single thread. This is to allow
116 // comparison w/ just drawing the SKP directly (i.e., drawAllTilesDirectly). The
117 // DDL creations and draws are interleaved to prevent starvation of the GPU.
118 // Note: this is somewhat of a misuse/pessimistic-use of DDLs since they are supposed to
119 // be created on a separate thread.
120 void interleaveDDLCreationAndDraw(GrContext*);
121
122 // This draws all the per-tile SKPs directly into all of the tiles w/o converting them to
123 // DDLs first - all on a single thread.
124 void drawAllTilesDirectly(GrContext*);
125
Robert Phillips11c67672020-04-23 15:10:03 -0400126 void dropCallbackContexts();
Robert Phillips96601082018-05-29 16:13:26 -0400127 void resetAllTiles();
128
Robert Phillipsa865a3a2020-02-14 10:49:39 -0500129 int numTiles() const { return fNumDivisions * fNumDivisions; }
130
Robert Phillips8472a3d2020-04-16 16:27:45 -0400131 void createBackendTextures(SkTaskGroup*, GrContext*);
132 void deleteBackendTextures(SkTaskGroup*, GrContext*);
133
Robert Phillips96601082018-05-29 16:13:26 -0400134private:
Robert Phillips11c67672020-04-23 15:10:03 -0400135 int fNumDivisions; // number of tiles along a side
Adlai Holler8e821232020-05-11 13:33:59 -0400136 SkAutoTArray<TileData> fTiles; // 'fNumDivisions' x 'fNumDivisions'
Robert Phillips11c67672020-04-23 15:10:03 -0400137
138 std::unique_ptr<SkDeferredDisplayList> fComposeDDL;
139
140 const SkSurfaceCharacterization fDstCharacterization;
Robert Phillips96601082018-05-29 16:13:26 -0400141};
142
143#endif