Home | History | Annotate | Download | only in tools
      1 /*
      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 #include "DDLTileHelper.h"
      9 
     10 #include "DDLPromiseImageHelper.h"
     11 #include "SkCanvas.h"
     12 #include "SkDeferredDisplayListPriv.h"
     13 #include "SkDeferredDisplayListRecorder.h"
     14 #include "SkImage_Gpu.h"
     15 #include "SkPicture.h"
     16 #include "SkSurface.h"
     17 #include "SkSurfaceCharacterization.h"
     18 #include "SkTaskGroup.h"
     19 
     20 DDLTileHelper::TileData::TileData(sk_sp<SkSurface> s, const SkIRect& clip)
     21         : fSurface(std::move(s))
     22         , fClip(clip) {
     23     SkAssertResult(fSurface->characterize(&fCharacterization));
     24 }
     25 
     26 void DDLTileHelper::TileData::createTileSpecificSKP(SkData* compressedPictureData,
     27                                                     const DDLPromiseImageHelper& helper) {
     28     SkASSERT(!fReconstitutedPicture);
     29 
     30     // This is bending the DDLRecorder contract! The promise images in the SKP should be
     31     // created by the same recorder used to create the matching DDL.
     32     SkDeferredDisplayListRecorder recorder(fCharacterization);
     33 
     34     fReconstitutedPicture = helper.reinflateSKP(&recorder, compressedPictureData, &fPromiseImages);
     35 
     36     std::unique_ptr<SkDeferredDisplayList> ddl = recorder.detach();
     37     if (ddl->priv().numOpLists()) {
     38         // TODO: remove this once skbug.com/8424 is fixed. If the DDL resulting from the
     39         // reinflation of the SKPs contains opLists that means some image subset operation
     40         // created a draw.
     41         fReconstitutedPicture.reset();
     42     }
     43 }
     44 
     45 void DDLTileHelper::TileData::createDDL() {
     46     SkASSERT(!fDisplayList);
     47 
     48     SkDeferredDisplayListRecorder recorder(fCharacterization);
     49 
     50     // DDL TODO: the DDLRecorder's GrContext isn't initialized until getCanvas is called.
     51     // Maybe set it up in the ctor?
     52     SkCanvas* subCanvas = recorder.getCanvas();
     53 
     54     // Because we cheated in createTileSpecificSKP and used the wrong DDLRecorder, the GrContext's
     55     // stored in fReconstitutedPicture's promise images are incorrect. Patch them with the correct
     56     // one now.
     57     for (int i = 0; i < fPromiseImages.count(); ++i) {
     58         GrContext* newContext = subCanvas->getGrContext();
     59 
     60         if (fPromiseImages[i]->isTextureBacked()) {
     61             SkImage_GpuBase* gpuImage = (SkImage_GpuBase*) fPromiseImages[i].get();
     62             gpuImage->resetContext(sk_ref_sp(newContext));
     63         }
     64     }
     65 
     66     subCanvas->clipRect(SkRect::MakeWH(fClip.width(), fClip.height()));
     67     subCanvas->translate(-fClip.fLeft, -fClip.fTop);
     68 
     69     // Note: in this use case we only render a picture to the deferred canvas
     70     // but, more generally, clients will use arbitrary draw calls.
     71     if (fReconstitutedPicture) {
     72         subCanvas->drawPicture(fReconstitutedPicture);
     73     }
     74 
     75     fDisplayList = recorder.detach();
     76 }
     77 
     78 void DDLTileHelper::TileData::draw() {
     79     SkASSERT(fDisplayList);
     80 
     81     fSurface->draw(fDisplayList.get());
     82 }
     83 
     84 void DDLTileHelper::TileData::compose(SkCanvas* dst) {
     85     sk_sp<SkImage> img = fSurface->makeImageSnapshot();
     86     dst->save();
     87     dst->clipRect(SkRect::Make(fClip));
     88     dst->drawImage(std::move(img), fClip.fLeft, fClip.fTop);
     89     dst->restore();
     90 }
     91 
     92 void DDLTileHelper::TileData::reset() {
     93     // TODO: when DDLs are re-renderable we don't need to do this
     94     fDisplayList = nullptr;
     95 }
     96 
     97 ///////////////////////////////////////////////////////////////////////////////////////////////////
     98 
     99 DDLTileHelper::DDLTileHelper(SkCanvas* canvas, const SkIRect& viewport, int numDivisions)
    100         : fNumDivisions(numDivisions) {
    101     SkASSERT(fNumDivisions > 0);
    102     fTiles.reserve(fNumDivisions*fNumDivisions);
    103 
    104     int xTileSize = viewport.width()/fNumDivisions;
    105     int yTileSize = viewport.height()/fNumDivisions;
    106 
    107     // Create the destination tiles
    108     for (int y = 0, yOff = 0; y < fNumDivisions; ++y, yOff += yTileSize) {
    109         int ySize = (y < fNumDivisions-1) ? yTileSize : viewport.height()-yOff;
    110 
    111         for (int x = 0, xOff = 0; x < fNumDivisions; ++x, xOff += xTileSize) {
    112             int xSize = (x < fNumDivisions-1) ? xTileSize : viewport.width()-xOff;
    113 
    114             SkIRect clip = SkIRect::MakeXYWH(xOff, yOff, xSize, ySize);
    115 
    116             SkASSERT(viewport.contains(clip));
    117 
    118             SkImageInfo tileII = SkImageInfo::MakeN32Premul(xSize, ySize);
    119 
    120             sk_sp<SkSurface> tileSurface = canvas->makeSurface(tileII);
    121 
    122             // TODO: this is here to deal w/ a resource allocator bug (skbug.com/8007). If all
    123             // the DDLs are flushed at the same time (w/o the composition draws) the allocator
    124             // feels free to reuse the backing GrSurfaces!
    125             tileSurface->flush();
    126 
    127             fTiles.push_back(TileData(std::move(tileSurface), clip));
    128         }
    129     }
    130 }
    131 
    132 void DDLTileHelper::createSKPPerTile(SkData* compressedPictureData,
    133                                      const DDLPromiseImageHelper& helper) {
    134     for (int i = 0; i < fTiles.count(); ++i) {
    135         fTiles[i].createTileSpecificSKP(compressedPictureData, helper);
    136     }
    137 }
    138 
    139 void DDLTileHelper::createDDLsInParallel() {
    140 #if 1
    141     SkTaskGroup().batch(fTiles.count(), [&](int i) { fTiles[i].createDDL(); });
    142     SkTaskGroup().wait();
    143 #else
    144     // Use this code path to debug w/o threads
    145     for (int i = 0; i < fTiles.count(); ++i) {
    146         fTiles[i].createDDL();
    147     }
    148 #endif
    149 
    150 }
    151 
    152 void DDLTileHelper::drawAllTilesAndFlush(GrContext* context, bool flush) {
    153     for (int i = 0; i < fTiles.count(); ++i) {
    154         fTiles[i].draw();
    155     }
    156     if (flush) {
    157         context->flush();
    158     }
    159 }
    160 
    161 void DDLTileHelper::composeAllTiles(SkCanvas* dstCanvas) {
    162     for (int i = 0; i < fTiles.count(); ++i) {
    163         fTiles[i].compose(dstCanvas);
    164     }
    165 }
    166 
    167 void DDLTileHelper::resetAllTiles() {
    168     for (int i = 0; i < fTiles.count(); ++i) {
    169         fTiles[i].reset();
    170     }
    171 }
    172