OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrAtlas.h" | 9 #include "GrAtlas.h" |
10 #include "GrContext.h" | 10 #include "GrContext.h" |
11 #include "GrGpu.h" | 11 #include "GrGpu.h" |
12 #include "GrRectanizer.h" | 12 #include "GrRectanizer.h" |
13 | 13 |
14 /////////////////////////////////////////////////////////////////////////////// | 14 /////////////////////////////////////////////////////////////////////////////// |
15 | 15 |
16 // for testing | 16 // for testing |
17 #define FONT_CACHE_STATS 0 | 17 #define FONT_CACHE_STATS 0 |
18 #if FONT_CACHE_STATS | 18 #if FONT_CACHE_STATS |
19 static int g_UploadCount = 0; | 19 static int g_UploadCount = 0; |
20 #endif | 20 #endif |
21 | 21 |
22 GrPlot::GrPlot() : fDrawToken(NULL, 0) | 22 GrPlot::GrPlot() : fDrawToken(NULL, 0) |
23 , fTexture(NULL) | 23 , fTexture(NULL) |
24 , fRects(NULL) | 24 , fRects(NULL) |
25 , fAtlasMgr(NULL) | 25 , fAtlasMgr(NULL) |
26 , fBytesPerPixel(1) | 26 , fBytesPerPixel(1) |
| 27 , fDirty(false) |
| 28 , fBatchUploads(false) |
27 { | 29 { |
28 fOffset.set(0, 0); | 30 fOffset.set(0, 0); |
29 } | 31 } |
30 | 32 |
31 GrPlot::~GrPlot() { | 33 GrPlot::~GrPlot() { |
| 34 SkDELETE_ARRAY(fPlotData); |
| 35 fPlotData = NULL; |
32 delete fRects; | 36 delete fRects; |
33 } | 37 } |
34 | 38 |
35 void GrPlot::init(GrAtlasMgr* mgr, int offX, int offY, int width, int height, si
ze_t bpp) { | 39 void GrPlot::init(GrAtlasMgr* mgr, int offX, int offY, int width, int height, si
ze_t bpp, |
| 40 bool batchUploads) { |
36 fRects = GrRectanizer::Factory(width, height); | 41 fRects = GrRectanizer::Factory(width, height); |
37 fAtlasMgr = mgr; | 42 fAtlasMgr = mgr; |
38 fOffset.set(offX * width, offY * height); | 43 fOffset.set(offX * width, offY * height); |
39 fBytesPerPixel = bpp; | 44 fBytesPerPixel = bpp; |
| 45 fPlotData = NULL; |
| 46 fDirtyRect.setEmpty(); |
| 47 fDirty = false; |
| 48 fBatchUploads = batchUploads; |
40 } | 49 } |
41 | 50 |
42 static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset)
{ | 51 static inline void adjust_for_offset(GrIPoint16* loc, const GrIPoint16& offset)
{ |
43 loc->fX += offset.fX; | 52 loc->fX += offset.fX; |
44 loc->fY += offset.fY; | 53 loc->fY += offset.fY; |
45 } | 54 } |
46 | 55 |
47 bool GrPlot::addSubImage(int width, int height, const void* image, | 56 bool GrPlot::addSubImage(int width, int height, const void* image, |
48 GrIPoint16* loc) { | 57 GrIPoint16* loc) { |
| 58 float percentFull = fRects->percentFull(); |
49 if (!fRects->addRect(width, height, loc)) { | 59 if (!fRects->addRect(width, height, loc)) { |
50 return false; | 60 return false; |
51 } | 61 } |
52 | 62 |
53 SkAutoSMalloc<1024> storage; | 63 // if batching uploads, create backing memory on first use |
54 adjust_for_offset(loc, fOffset); | 64 // once the plot is nearly full we will revert to uploading each subimage in
dividually |
55 GrContext* context = fTexture->getContext(); | 65 int plotWidth = fRects->width(); |
56 // We pass the flag that does not force a flush. We assume our caller is | 66 int plotHeight = fRects->height(); |
57 // smart and hasn't referenced the part of the texture we're about to update | 67 if (fBatchUploads && NULL == fPlotData && 0.0f == percentFull) { |
58 // since the last flush. | 68 fPlotData = SkNEW_ARRAY(unsigned char, fBytesPerPixel*plotWidth*plotHeig
ht); |
59 context->writeTexturePixels(fTexture, | 69 memset(fPlotData, 0, fBytesPerPixel*plotWidth*plotHeight); |
60 loc->fX, loc->fY, width, height, | 70 } |
61 fTexture->config(), image, 0, | 71 |
62 GrContext::kDontFlush_PixelOpsFlag); | 72 // if we have backing memory, copy to the memory and set for future upload |
| 73 if (NULL != fPlotData) { |
| 74 const unsigned char* imagePtr = (const unsigned char*) image; |
| 75 // point ourselves at the right starting spot |
| 76 unsigned char* dataPtr = fPlotData; |
| 77 dataPtr += fBytesPerPixel*plotWidth*loc->fY; |
| 78 dataPtr += fBytesPerPixel*loc->fX; |
| 79 // copy into the data buffer |
| 80 for (int i = 0; i < height; ++i) { |
| 81 memcpy(dataPtr, imagePtr, fBytesPerPixel*width); |
| 82 dataPtr += fBytesPerPixel*plotWidth; |
| 83 imagePtr += fBytesPerPixel*width; |
| 84 } |
| 85 |
| 86 fDirtyRect.join(loc->fX, loc->fY, loc->fX + width, loc->fY + height); |
| 87 adjust_for_offset(loc, fOffset); |
| 88 fDirty = true; |
| 89 // otherwise, just upload the image directly |
| 90 } else { |
| 91 adjust_for_offset(loc, fOffset); |
| 92 GrContext* context = fTexture->getContext(); |
| 93 context->writeTexturePixels(fTexture, |
| 94 loc->fX, loc->fY, width, height, |
| 95 fTexture->config(), image, 0, |
| 96 GrContext::kDontFlush_PixelOpsFlag); |
| 97 } |
63 | 98 |
64 #if FONT_CACHE_STATS | 99 #if FONT_CACHE_STATS |
65 ++g_UploadCount; | 100 ++g_UploadCount; |
66 #endif | 101 #endif |
67 | 102 |
68 return true; | 103 return true; |
69 } | 104 } |
70 | 105 |
| 106 void GrPlot::uploadToTexture() { |
| 107 static const float kNearlyFullTolerance = 0.85f; |
| 108 |
| 109 // should only do this if batching is enabled |
| 110 SkASSERT(fBatchUploads); |
| 111 |
| 112 if (fDirty) { |
| 113 SkASSERT(NULL != fTexture); |
| 114 GrContext* context = fTexture->getContext(); |
| 115 // We pass the flag that does not force a flush. We assume our caller is |
| 116 // smart and hasn't referenced the part of the texture we're about to up
date |
| 117 // since the last flush. |
| 118 int rowBytes = fBytesPerPixel*fRects->width(); |
| 119 const unsigned char* dataPtr = fPlotData; |
| 120 dataPtr += rowBytes*fDirtyRect.fTop; |
| 121 dataPtr += fBytesPerPixel*fDirtyRect.fLeft; |
| 122 context->writeTexturePixels(fTexture, |
| 123 fOffset.fX + fDirtyRect.fLeft, fOffset.fY +
fDirtyRect.fTop, |
| 124 fDirtyRect.width(), fDirtyRect.height(), |
| 125 fTexture->config(), dataPtr, |
| 126 rowBytes, |
| 127 GrContext::kDontFlush_PixelOpsFlag); |
| 128 fDirtyRect.setEmpty(); |
| 129 fDirty = false; |
| 130 // If the Plot is nearly full, anything else we add will probably be sma
ll and one |
| 131 // at a time, so free up the memory and after this upload any new images
directly. |
| 132 if (fRects->percentFull() > kNearlyFullTolerance) { |
| 133 SkDELETE_ARRAY(fPlotData); |
| 134 fPlotData = NULL; |
| 135 } |
| 136 } |
| 137 } |
| 138 |
71 void GrPlot::resetRects() { | 139 void GrPlot::resetRects() { |
72 SkASSERT(NULL != fRects); | 140 SkASSERT(NULL != fRects); |
73 fRects->reset(); | 141 fRects->reset(); |
74 } | 142 } |
75 | 143 |
76 /////////////////////////////////////////////////////////////////////////////// | 144 /////////////////////////////////////////////////////////////////////////////// |
77 | 145 |
78 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config, | 146 GrAtlasMgr::GrAtlasMgr(GrGpu* gpu, GrPixelConfig config, |
79 const SkISize& backingTextureSize, | 147 const SkISize& backingTextureSize, |
80 int numPlotsX, int numPlotsY) { | 148 int numPlotsX, int numPlotsY, bool batchUploads) { |
81 fGpu = SkRef(gpu); | 149 fGpu = SkRef(gpu); |
82 fPixelConfig = config; | 150 fPixelConfig = config; |
83 fBackingTextureSize = backingTextureSize; | 151 fBackingTextureSize = backingTextureSize; |
84 fNumPlotsX = numPlotsX; | 152 fNumPlotsX = numPlotsX; |
85 fNumPlotsY = numPlotsY; | 153 fNumPlotsY = numPlotsY; |
| 154 fBatchUploads = batchUploads; |
86 fTexture = NULL; | 155 fTexture = NULL; |
87 | 156 |
88 int plotWidth = fBackingTextureSize.width() / fNumPlotsX; | 157 int textureWidth = fBackingTextureSize.width(); |
89 int plotHeight = fBackingTextureSize.height() / fNumPlotsY; | 158 int textureHeight = fBackingTextureSize.height(); |
90 | 159 |
91 SkASSERT(plotWidth * fNumPlotsX == fBackingTextureSize.width()); | 160 int plotWidth = textureWidth / fNumPlotsX; |
92 SkASSERT(plotHeight * fNumPlotsY == fBackingTextureSize.height()); | 161 int plotHeight = textureHeight / fNumPlotsY; |
| 162 |
| 163 SkASSERT(plotWidth * fNumPlotsX == textureWidth); |
| 164 SkASSERT(plotHeight * fNumPlotsY == textureHeight); |
93 | 165 |
94 // set up allocated plots | 166 // set up allocated plots |
95 size_t bpp = GrBytesPerPixel(fPixelConfig); | 167 size_t bpp = GrBytesPerPixel(fPixelConfig); |
96 fPlotArray = SkNEW_ARRAY(GrPlot, (fNumPlotsX*fNumPlotsY)); | 168 fPlotArray = SkNEW_ARRAY(GrPlot, (fNumPlotsX*fNumPlotsY)); |
97 | 169 |
98 GrPlot* currPlot = fPlotArray; | 170 GrPlot* currPlot = fPlotArray; |
99 for (int y = numPlotsY-1; y >= 0; --y) { | 171 for (int y = numPlotsY-1; y >= 0; --y) { |
100 for (int x = numPlotsX-1; x >= 0; --x) { | 172 for (int x = numPlotsX-1; x >= 0; --x) { |
101 currPlot->init(this, x, y, plotWidth, plotHeight, bpp); | 173 currPlot->init(this, x, y, plotWidth, plotHeight, bpp, batchUploads)
; |
102 | 174 |
103 // build LRU list | 175 // build LRU list |
104 fPlotList.addToHead(currPlot); | 176 fPlotList.addToHead(currPlot); |
105 ++currPlot; | 177 ++currPlot; |
106 } | 178 } |
107 } | 179 } |
108 } | 180 } |
109 | 181 |
110 GrAtlasMgr::~GrAtlasMgr() { | 182 GrAtlasMgr::~GrAtlasMgr() { |
111 SkSafeUnref(fTexture); | 183 SkSafeUnref(fTexture); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 GrPlot* plot; | 266 GrPlot* plot; |
195 while (NULL != (plot = plotIter.get())) { | 267 while (NULL != (plot = plotIter.get())) { |
196 if (plot->drawToken().isIssued()) { | 268 if (plot->drawToken().isIssued()) { |
197 return plot; | 269 return plot; |
198 } | 270 } |
199 plotIter.prev(); | 271 plotIter.prev(); |
200 } | 272 } |
201 | 273 |
202 return NULL; | 274 return NULL; |
203 } | 275 } |
| 276 |
| 277 void GrAtlasMgr::uploadPlotsToTexture() { |
| 278 if (fBatchUploads) { |
| 279 GrPlotList::Iter plotIter; |
| 280 plotIter.init(fPlotList, GrPlotList::Iter::kHead_IterStart); |
| 281 GrPlot* plot; |
| 282 while (NULL != (plot = plotIter.get())) { |
| 283 plot->uploadToTexture(); |
| 284 plotIter.next(); |
| 285 } |
| 286 } |
| 287 } |
OLD | NEW |