| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrBatchAtlas.h" | 8 #include "GrBatchAtlas.h" |
| 9 #include "GrBatchFlushState.h" | 9 #include "GrBatchFlushState.h" |
| 10 #include "GrBuffer.h" | |
| 11 #include "GrRectanizer.h" | 10 #include "GrRectanizer.h" |
| 12 #include "GrResourceProvider.h" | |
| 13 #include "GrTracing.h" | 11 #include "GrTracing.h" |
| 14 | 12 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////////// |
| 16 | 14 |
| 17 GrBatchAtlas::BatchPlot::BatchPlot(int index, uint64_t genID, int offX, int offY
, int width, | 15 GrBatchAtlas::BatchPlot::BatchPlot(int index, uint64_t genID, int offX, int offY
, int width, |
| 18 int height, GrPixelConfig config, GrResourceP
rovider* rp) | 16 int height, GrPixelConfig config) |
| 19 : fResourceProvider(rp) | 17 : fLastUpload(GrBatchDrawToken::AlreadyFlushedToken()) |
| 20 , fLastUpload(GrBatchDrawToken::AlreadyFlushedToken()) | |
| 21 , fLastUse(GrBatchDrawToken::AlreadyFlushedToken()) | 18 , fLastUse(GrBatchDrawToken::AlreadyFlushedToken()) |
| 22 , fIndex(index) | 19 , fIndex(index) |
| 23 , fGenID(genID) | 20 , fGenID(genID) |
| 24 , fID(CreateId(fIndex, fGenID)) | 21 , fID(CreateId(fIndex, fGenID)) |
| 25 , fDataPtr(nullptr) | 22 , fData(nullptr) |
| 26 , fTransferBuffer(nullptr) | |
| 27 , fWidth(width) | 23 , fWidth(width) |
| 28 , fHeight(height) | 24 , fHeight(height) |
| 29 , fX(offX) | 25 , fX(offX) |
| 30 , fY(offY) | 26 , fY(offY) |
| 31 , fRects(nullptr) | 27 , fRects(nullptr) |
| 32 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight)) | 28 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight)) |
| 33 , fConfig(config) | 29 , fConfig(config) |
| 34 , fBytesPerPixel(GrBytesPerPixel(config)) | 30 , fBytesPerPixel(GrBytesPerPixel(config)) |
| 35 #ifdef SK_DEBUG | 31 #ifdef SK_DEBUG |
| 36 , fDirty(false) | 32 , fDirty(false) |
| 37 #endif | 33 #endif |
| 38 { | 34 { |
| 39 fDirtyRect.setEmpty(); | 35 fDirtyRect.setEmpty(); |
| 40 } | 36 } |
| 41 | 37 |
| 42 GrBatchAtlas::BatchPlot::~BatchPlot() { | 38 GrBatchAtlas::BatchPlot::~BatchPlot() { |
| 43 if (fTransferBuffer) { | 39 sk_free(fData); |
| 44 fTransferBuffer->unref(); | |
| 45 } else { | |
| 46 sk_free(fDataPtr); | |
| 47 } | |
| 48 delete fRects; | 40 delete fRects; |
| 49 } | 41 } |
| 50 | 42 |
| 51 bool GrBatchAtlas::BatchPlot::addSubImage(int width, int height, const void* ima
ge, | 43 bool GrBatchAtlas::BatchPlot::addSubImage(int width, int height, const void* ima
ge, |
| 52 SkIPoint16* loc) { | 44 SkIPoint16* loc) { |
| 53 SkASSERT(width <= fWidth && height <= fHeight); | 45 SkASSERT(width <= fWidth && height <= fHeight); |
| 54 | 46 |
| 55 if (!fRects) { | 47 if (!fRects) { |
| 56 fRects = GrRectanizer::Factory(fWidth, fHeight); | 48 fRects = GrRectanizer::Factory(fWidth, fHeight); |
| 57 } | 49 } |
| 58 | 50 |
| 59 if (!fRects->addRect(width, height, loc)) { | 51 if (!fRects->addRect(width, height, loc)) { |
| 60 return false; | 52 return false; |
| 61 } | 53 } |
| 62 | 54 |
| 63 if (!fDataPtr) { | 55 if (!fData) { |
| 64 if (!fTransferBuffer) { | 56 fData = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPerPixel
* fWidth * |
| 65 fTransferBuffer = | 57 fHeight)); |
| 66 fResourceProvider->createBuffer(fBytesPerPixel * fWidth * fHeigh
t, | |
| 67 kXferCpuToGpu_GrBufferType, | |
| 68 kDynamic_GrAccessPattern, | |
| 69 GrResourceProvider::kNoPendingIO
_Flag); | |
| 70 } | |
| 71 if (fTransferBuffer) { | |
| 72 fDataPtr = (unsigned char*)fTransferBuffer->map(); | |
| 73 } else { | |
| 74 fDataPtr = reinterpret_cast<unsigned char*>(sk_calloc_throw(fBytesPe
rPixel * fWidth * | |
| 75 fHeight)
); | |
| 76 } | |
| 77 } | 58 } |
| 78 | |
| 79 size_t rowBytes = width * fBytesPerPixel; | 59 size_t rowBytes = width * fBytesPerPixel; |
| 80 const unsigned char* imagePtr = (const unsigned char*)image; | 60 const unsigned char* imagePtr = (const unsigned char*)image; |
| 81 // point ourselves at the right starting spot | 61 // point ourselves at the right starting spot |
| 82 unsigned char* dataPtr = fDataPtr; | 62 unsigned char* dataPtr = fData; |
| 83 dataPtr += fBytesPerPixel * fWidth * loc->fY; | 63 dataPtr += fBytesPerPixel * fWidth * loc->fY; |
| 84 dataPtr += fBytesPerPixel * loc->fX; | 64 dataPtr += fBytesPerPixel * loc->fX; |
| 85 // copy into the data buffer | 65 // copy into the data buffer |
| 86 for (int i = 0; i < height; ++i) { | 66 for (int i = 0; i < height; ++i) { |
| 87 memcpy(dataPtr, imagePtr, rowBytes); | 67 memcpy(dataPtr, imagePtr, rowBytes); |
| 88 dataPtr += fBytesPerPixel * fWidth; | 68 dataPtr += fBytesPerPixel * fWidth; |
| 89 imagePtr += rowBytes; | 69 imagePtr += rowBytes; |
| 90 } | 70 } |
| 91 | 71 |
| 92 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); | 72 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
| 93 | 73 |
| 94 loc->fX += fOffset.fX; | 74 loc->fX += fOffset.fX; |
| 95 loc->fY += fOffset.fY; | 75 loc->fY += fOffset.fY; |
| 96 SkDEBUGCODE(fDirty = true;) | 76 SkDEBUGCODE(fDirty = true;) |
| 97 | 77 |
| 98 return true; | 78 return true; |
| 99 } | 79 } |
| 100 | 80 |
| 101 void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawBatch::WritePixelsFn& writeP
ixels, | 81 void GrBatchAtlas::BatchPlot::uploadToTexture(GrDrawBatch::WritePixelsFn& writeP
ixels, |
| 102 GrDrawBatch::TransferPixelsFn& xfe
rPixels, | |
| 103 GrTexture* texture) { | 82 GrTexture* texture) { |
| 104 // We should only be issuing uploads if we are in fact dirty | 83 // We should only be issuing uploads if we are in fact dirty |
| 105 SkASSERT(fDirty && fDataPtr && texture); | 84 SkASSERT(fDirty && fData && texture); |
| 106 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTe
xture"); | 85 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), "GrBatchPlot::uploadToTe
xture"); |
| 107 size_t rowBytes = fBytesPerPixel * fWidth; | 86 size_t rowBytes = fBytesPerPixel * fWidth; |
| 108 size_t dataOffset = rowBytes * fDirtyRect.fTop + fBytesPerPixel * fDirtyRect
.fLeft; | 87 const unsigned char* dataPtr = fData; |
| 109 if (fTransferBuffer) { | 88 dataPtr += rowBytes * fDirtyRect.fTop; |
| 110 fTransferBuffer->unmap(); | 89 dataPtr += fBytesPerPixel * fDirtyRect.fLeft; |
| 111 fDataPtr = nullptr; | 90 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRect.
fTop, |
| 112 xferPixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyRe
ct.fTop, | 91 fDirtyRect.width(), fDirtyRect.height(), fConfig, dataPtr, rowBy
tes); |
| 113 fDirtyRect.width(), fDirtyRect.height(), fConfig, fTransferBu
ffer, dataOffset, | |
| 114 rowBytes); | |
| 115 } else { | |
| 116 writePixels(texture, fOffset.fX + fDirtyRect.fLeft, fOffset.fY + fDirtyR
ect.fTop, | |
| 117 fDirtyRect.width(), fDirtyRect.height(), fConfig, fDataPtr +
dataOffset, | |
| 118 rowBytes); | |
| 119 } | |
| 120 | |
| 121 fDirtyRect.setEmpty(); | 92 fDirtyRect.setEmpty(); |
| 122 SkDEBUGCODE(fDirty = false;) | 93 SkDEBUGCODE(fDirty = false;) |
| 123 } | 94 } |
| 124 | 95 |
| 125 void GrBatchAtlas::BatchPlot::resetRects() { | 96 void GrBatchAtlas::BatchPlot::resetRects() { |
| 126 if (fRects) { | 97 if (fRects) { |
| 127 fRects->reset(); | 98 fRects->reset(); |
| 128 } | 99 } |
| 129 | 100 |
| 130 fGenID++; | 101 fGenID++; |
| 131 fID = CreateId(fIndex, fGenID); | 102 fID = CreateId(fIndex, fGenID); |
| 132 | 103 |
| 133 // zero out the plot | 104 // zero out the plot |
| 134 if (fDataPtr) { | 105 if (fData) { |
| 135 sk_bzero(fDataPtr, fBytesPerPixel * fWidth * fHeight); | 106 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight); |
| 136 } | 107 } |
| 137 | 108 |
| 138 fDirtyRect.setEmpty(); | 109 fDirtyRect.setEmpty(); |
| 139 SkDEBUGCODE(fDirty = false;) | 110 SkDEBUGCODE(fDirty = false;) |
| 140 } | 111 } |
| 141 | 112 |
| 142 /////////////////////////////////////////////////////////////////////////////// | 113 /////////////////////////////////////////////////////////////////////////////// |
| 143 | 114 |
| 144 GrBatchAtlas::GrBatchAtlas(GrResourceProvider* rp, GrTexture* texture, int numPl
otsX, int numPlotsY) | 115 GrBatchAtlas::GrBatchAtlas(GrTexture* texture, int numPlotsX, int numPlotsY) |
| 145 : fTexture(texture) | 116 : fTexture(texture) |
| 146 , fAtlasGeneration(kInvalidAtlasGeneration + 1) { | 117 , fAtlasGeneration(kInvalidAtlasGeneration + 1) { |
| 147 | 118 |
| 148 int plotWidth = texture->width() / numPlotsX; | 119 int plotWidth = texture->width() / numPlotsX; |
| 149 int plotHeight = texture->height() / numPlotsY; | 120 int plotHeight = texture->height() / numPlotsY; |
| 150 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); | 121 SkASSERT(numPlotsX * numPlotsY <= BulkUseTokenUpdater::kMaxPlots); |
| 151 SkASSERT(plotWidth * numPlotsX == texture->width()); | 122 SkASSERT(plotWidth * numPlotsX == texture->width()); |
| 152 SkASSERT(plotHeight * numPlotsY == texture->height()); | 123 SkASSERT(plotHeight * numPlotsY == texture->height()); |
| 153 | 124 |
| 154 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) | 125 SkDEBUGCODE(fNumPlots = numPlotsX * numPlotsY;) |
| 155 | 126 |
| 156 // We currently do not support compressed atlases... | 127 // We currently do not support compressed atlases... |
| 157 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig)); | 128 SkASSERT(!GrPixelConfigIsCompressed(texture->desc().fConfig)); |
| 158 | 129 |
| 159 // set up allocated plots | 130 // set up allocated plots |
| 160 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY]; | 131 fPlotArray = new SkAutoTUnref<BatchPlot>[numPlotsX * numPlotsY]; |
| 161 | 132 |
| 162 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray; | 133 SkAutoTUnref<BatchPlot>* currPlot = fPlotArray; |
| 163 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { | 134 for (int y = numPlotsY - 1, r = 0; y >= 0; --y, ++r) { |
| 164 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { | 135 for (int x = numPlotsX - 1, c = 0; x >= 0; --x, ++c) { |
| 165 uint32_t index = r * numPlotsX + c; | 136 uint32_t index = r * numPlotsX + c; |
| 166 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight, | 137 currPlot->reset(new BatchPlot(index, 1, x, y, plotWidth, plotHeight, |
| 167 texture->desc().fConfig, rp)); | 138 texture->desc().fConfig)); |
| 168 | 139 |
| 169 // build LRU list | 140 // build LRU list |
| 170 fPlotList.addToHead(currPlot->get()); | 141 fPlotList.addToHead(currPlot->get()); |
| 171 ++currPlot; | 142 ++currPlot; |
| 172 } | 143 } |
| 173 } | 144 } |
| 174 } | 145 } |
| 175 | 146 |
| 176 GrBatchAtlas::~GrBatchAtlas() { | 147 GrBatchAtlas::~GrBatchAtlas() { |
| 177 SkSafeUnref(fTexture); | 148 SkSafeUnref(fTexture); |
| 178 delete[] fPlotArray; | 149 delete[] fPlotArray; |
| 179 } | 150 } |
| 180 | 151 |
| 181 void GrBatchAtlas::processEviction(AtlasID id) { | 152 void GrBatchAtlas::processEviction(AtlasID id) { |
| 182 for (int i = 0; i < fEvictionCallbacks.count(); i++) { | 153 for (int i = 0; i < fEvictionCallbacks.count(); i++) { |
| 183 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); | 154 (*fEvictionCallbacks[i].fFunc)(id, fEvictionCallbacks[i].fData); |
| 184 } | 155 } |
| 185 } | 156 } |
| 186 | 157 |
| 187 inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, B
atchPlot* plot) { | 158 inline void GrBatchAtlas::updatePlot(GrDrawBatch::Target* target, AtlasID* id, B
atchPlot* plot) { |
| 188 this->makeMRU(plot); | 159 this->makeMRU(plot); |
| 189 | 160 |
| 190 // If our most recent upload has already occurred then we have to insert a n
ew | 161 // If our most recent upload has already occurred then we have to insert a n
ew |
| 191 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocu
rred. | 162 // upload. Otherwise, we already have a scheduled upload that hasn't yet ocu
rred. |
| 192 // This new update will piggy back on that previously scheduled update. | 163 // This new update will piggy back on that previously scheduled update. |
| 193 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) { | 164 if (target->hasDrawBeenFlushed(plot->lastUploadToken())) { |
| 194 // With c+14 we could move sk_sp into lambda to only ref once. | 165 // With c+14 we could move sk_sp into lamba to only ref once. |
| 195 sk_sp<BatchPlot> plotsp(SkRef(plot)); | 166 sk_sp<BatchPlot> plotsp(SkRef(plot)); |
| 196 GrTexture* texture = fTexture; | 167 GrTexture* texture = fTexture; |
| 197 GrBatchDrawToken lastUploadToken = target->addAsapUpload( | 168 GrBatchDrawToken lastUploadToken = target->addAsapUpload( |
| 198 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels, | 169 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) { |
| 199 GrDrawBatch::TransferPixelsFn& transferPixels) { | 170 plotsp->uploadToTexture(writePixels, texture); |
| 200 plotsp->uploadToTexture(writePixels, transferPixels, texture); | |
| 201 } | 171 } |
| 202 ); | 172 ); |
| 203 plot->setLastUploadToken(lastUploadToken); | 173 plot->setLastUploadToken(lastUploadToken); |
| 204 } | 174 } |
| 205 *id = plot->id(); | 175 *id = plot->id(); |
| 206 } | 176 } |
| 207 | 177 |
| 208 bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* target, | 178 bool GrBatchAtlas::addToAtlas(AtlasID* id, GrDrawBatch::Target* target, |
| 209 int width, int height, const void* image, SkIPoint
16* loc) { | 179 int width, int height, const void* image, SkIPoint
16* loc) { |
| 210 // We should already have a texture, TODO clean this up | 180 // We should already have a texture, TODO clean this up |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp()); | 228 SkASSERT(GrBytesPerPixel(fTexture->desc().fConfig) == newPlot->bpp()); |
| 259 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); | 229 SkDEBUGCODE(bool verify = )newPlot->addSubImage(width, height, image, loc); |
| 260 SkASSERT(verify); | 230 SkASSERT(verify); |
| 261 | 231 |
| 262 // Note that this plot will be uploaded inline with the draws whereas the | 232 // Note that this plot will be uploaded inline with the draws whereas the |
| 263 // one it displaced most likely was uploaded asap. | 233 // one it displaced most likely was uploaded asap. |
| 264 // With c+14 we could move sk_sp into lamba to only ref once. | 234 // With c+14 we could move sk_sp into lamba to only ref once. |
| 265 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get())); | 235 sk_sp<BatchPlot> plotsp(SkRef(newPlot.get())); |
| 266 GrTexture* texture = fTexture; | 236 GrTexture* texture = fTexture; |
| 267 GrBatchDrawToken lastUploadToken = target->addInlineUpload( | 237 GrBatchDrawToken lastUploadToken = target->addInlineUpload( |
| 268 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels, | 238 [plotsp, texture] (GrDrawBatch::WritePixelsFn& writePixels) { |
| 269 GrDrawBatch::TransferPixelsFn& transferPixels) { | 239 plotsp->uploadToTexture(writePixels, texture); |
| 270 plotsp->uploadToTexture(writePixels, transferPixels, texture); | |
| 271 } | 240 } |
| 272 ); | 241 ); |
| 273 newPlot->setLastUploadToken(lastUploadToken); | 242 newPlot->setLastUploadToken(lastUploadToken); |
| 274 | 243 |
| 275 *id = newPlot->id(); | 244 *id = newPlot->id(); |
| 276 | 245 |
| 277 fAtlasGeneration++; | 246 fAtlasGeneration++; |
| 278 return true; | 247 return true; |
| 279 } | 248 } |
| OLD | NEW |