rsdAllocation.cpp revision f79af7e3dfd48b8fda6e4f342809a4a74cb187a5
1/*
2 * Copyright (C) 2011-2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsdCore.h"
19#include "rsdAllocation.h"
20#include "rsdFrameBufferObj.h"
21
22#include "rsAllocation.h"
23
24#include "system/window.h"
25#include "hardware/gralloc.h"
26#include "ui/Rect.h"
27#include "ui/GraphicBufferMapper.h"
28#include "gui/SurfaceTexture.h"
29
30#include <GLES/gl.h>
31#include <GLES2/gl2.h>
32#include <GLES/glext.h>
33
34using namespace android;
35using namespace android::renderscript;
36
37
38
39const static GLenum gFaceOrder[] = {
40    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
41    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
42    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
43    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
44    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
45    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
46};
47
48
49GLenum rsdTypeToGLType(RsDataType t) {
50    switch (t) {
51    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
52    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
53    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
54
55    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
56    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
57    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
58    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
59    case RS_TYPE_SIGNED_8:      return GL_BYTE;
60    case RS_TYPE_SIGNED_16:     return GL_SHORT;
61    default:    break;
62    }
63    return 0;
64}
65
66GLenum rsdKindToGLFormat(RsDataKind k) {
67    switch (k) {
68    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
69    case RS_KIND_PIXEL_A: return GL_ALPHA;
70    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
71    case RS_KIND_PIXEL_RGB: return GL_RGB;
72    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
73    case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
74    default: break;
75    }
76    return 0;
77}
78
79uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
80                      uint32_t xoff, uint32_t yoff, uint32_t lod,
81                      RsAllocationCubemapFace face) {
82    uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
83    ptr += face * alloc->mHal.drvState.faceOffset;
84    ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
85    ptr += xoff * alloc->mHal.state.elementSizeBytes;
86    return ptr;
87}
88
89
90static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
91                            uint32_t xoff, uint32_t yoff, uint32_t lod,
92                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
93    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
94
95    rsAssert(drv->textureID);
96    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
97    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
98    GLenum t = GL_TEXTURE_2D;
99    if (alloc->mHal.state.hasFaces) {
100        t = gFaceOrder[face];
101    }
102    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
103}
104
105
106static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
107    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
108
109    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
110    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
111
112    uint32_t faceCount = 1;
113    if (alloc->mHal.state.hasFaces) {
114        faceCount = 6;
115    }
116
117    rsdGLCheckError(rsc, "Upload2DTexture 1 ");
118    for (uint32_t face = 0; face < faceCount; face ++) {
119        for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
120            const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face);
121
122            GLenum t = GL_TEXTURE_2D;
123            if (alloc->mHal.state.hasFaces) {
124                t = gFaceOrder[face];
125            }
126
127            if (isFirstUpload) {
128                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
129                             alloc->mHal.state.type->getLODDimX(lod),
130                             alloc->mHal.state.type->getLODDimY(lod),
131                             0, drv->glFormat, drv->glType, p);
132            } else {
133                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
134                                alloc->mHal.state.type->getLODDimX(lod),
135                                alloc->mHal.state.type->getLODDimY(lod),
136                                drv->glFormat, drv->glType, p);
137            }
138        }
139    }
140
141    if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
142        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
143    }
144    rsdGLCheckError(rsc, "Upload2DTexture");
145}
146
147static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
148    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
149
150    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
151        if (!drv->textureID) {
152            RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
153        }
154        return;
155    }
156
157    if (!drv->glType || !drv->glFormat) {
158        return;
159    }
160
161    if (!alloc->mHal.drvState.lod[0].mallocPtr) {
162        return;
163    }
164
165    bool isFirstUpload = false;
166
167    if (!drv->textureID) {
168        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
169        isFirstUpload = true;
170    }
171
172    Upload2DTexture(rsc, alloc, isFirstUpload);
173
174    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
175        if (alloc->mHal.drvState.lod[0].mallocPtr) {
176            free(alloc->mHal.drvState.lod[0].mallocPtr);
177            alloc->mHal.drvState.lod[0].mallocPtr = NULL;
178        }
179    }
180    rsdGLCheckError(rsc, "UploadToTexture");
181}
182
183static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
184    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
185
186    if (!drv->glFormat) {
187        return;
188    }
189
190    if (!drv->renderTargetID) {
191        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
192
193        if (!drv->renderTargetID) {
194            // This should generally not happen
195            ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
196            rsc->dumpDebug();
197            return;
198        }
199        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
200        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
201                              alloc->mHal.state.dimensionX, alloc->mHal.state.dimensionY);
202    }
203    rsdGLCheckError(rsc, "AllocateRenderTarget");
204}
205
206static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
207    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
208
209    rsAssert(!alloc->mHal.state.type->getDimY());
210    rsAssert(!alloc->mHal.state.type->getDimZ());
211
212    //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
213
214    if (!drv->bufferID) {
215        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
216    }
217    if (!drv->bufferID) {
218        ALOGE("Upload to buffer object failed");
219        drv->uploadDeferred = true;
220        return;
221    }
222    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
223    RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
224                 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
225    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
226    rsdGLCheckError(rsc, "UploadToBufferObject");
227}
228
229static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
230        const Type *type, uint8_t *ptr) {
231    alloc->mHal.drvState.lod[0].dimX = type->getDimX();
232    alloc->mHal.drvState.lod[0].dimY = type->getDimY();
233    alloc->mHal.drvState.lod[0].mallocPtr = 0;
234    alloc->mHal.drvState.lod[0].stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
235    alloc->mHal.drvState.lodCount = type->getLODCount();
236    alloc->mHal.drvState.faceCount = type->getDimFaces();
237
238    size_t offsets[Allocation::MAX_LOD];
239    memset(offsets, 0, sizeof(offsets));
240
241    size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
242            rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
243    if(alloc->mHal.drvState.lodCount > 1) {
244        uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
245        uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
246        uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
247        for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
248            alloc->mHal.drvState.lod[lod].dimX = tx;
249            alloc->mHal.drvState.lod[lod].dimY = ty;
250            alloc->mHal.drvState.lod[lod].dimZ = tz;
251            alloc->mHal.drvState.lod[lod].stride = tx * type->getElementSizeBytes();
252            offsets[lod] = o;
253            o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
254            if (tx > 1) tx >>= 1;
255            if (ty > 1) ty >>= 1;
256            if (tz > 1) tz >>= 1;
257        }
258    }
259    alloc->mHal.drvState.faceOffset = o;
260
261    alloc->mHal.drvState.lod[0].mallocPtr = ptr;
262    for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
263        alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
264    }
265
266    size_t allocSize = alloc->mHal.drvState.faceOffset;
267    if(alloc->mHal.drvState.faceCount) {
268        allocSize *= 6;
269    }
270
271    return allocSize;
272}
273
274bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
275    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
276    if (!drv) {
277        return false;
278    }
279    alloc->mHal.drv = drv;
280
281    // Calculate the object size.
282    size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
283
284    uint8_t * ptr = NULL;
285    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
286    } else if (alloc->mHal.state.userProvidedPtr != NULL) {
287        // user-provided allocation
288        // limitations: no faces, no LOD, USAGE_SCRIPT only
289        if (alloc->mHal.state.usageFlags != RS_ALLOCATION_USAGE_SCRIPT) {
290            ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT");
291            return false;
292        }
293        if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
294            ALOGE("User-allocated buffers must not have multiple faces or LODs");
295            return false;
296        }
297        ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
298    } else {
299        if (forceZero) {
300            ptr = (uint8_t *)calloc(1, allocSize);
301        } else {
302            ptr = (uint8_t *)malloc(allocSize);
303        }
304        if (!ptr) {
305            alloc->mHal.drv = NULL;
306            free(drv);
307            return false;
308        }
309    }
310    // Build the pointer tables
311    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
312    if(allocSize != verifySize) {
313        rsAssert(!"Size mismatch");
314    }
315
316    drv->glTarget = GL_NONE;
317    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
318        if (alloc->mHal.state.hasFaces) {
319            drv->glTarget = GL_TEXTURE_CUBE_MAP;
320        } else {
321            drv->glTarget = GL_TEXTURE_2D;
322        }
323    } else {
324        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
325            drv->glTarget = GL_ARRAY_BUFFER;
326        }
327    }
328
329    drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
330    drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
331
332    if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
333        drv->uploadDeferred = true;
334    }
335
336
337    drv->readBackFBO = NULL;
338
339    return true;
340}
341
342void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
343    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
344
345    if (drv->bufferID) {
346        // Causes a SW crash....
347        //ALOGV(" mBufferID %i", mBufferID);
348        //glDeleteBuffers(1, &mBufferID);
349        //mBufferID = 0;
350    }
351    if (drv->textureID) {
352        RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
353        drv->textureID = 0;
354    }
355    if (drv->renderTargetID) {
356        RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
357        drv->renderTargetID = 0;
358    }
359
360    if (alloc->mHal.drvState.lod[0].mallocPtr) {
361        // don't free user-allocated ptrs
362        if (!alloc->mHal.state.userProvidedPtr) {
363            free(alloc->mHal.drvState.lod[0].mallocPtr);
364        }
365        alloc->mHal.drvState.lod[0].mallocPtr = NULL;
366    }
367    if (drv->readBackFBO != NULL) {
368        delete drv->readBackFBO;
369        drv->readBackFBO = NULL;
370    }
371    free(drv);
372    alloc->mHal.drv = NULL;
373}
374
375void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
376                         const Type *newType, bool zeroNew) {
377    void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
378    // Calculate the object size
379    size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
380    uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
381    // Build the relative pointer tables.
382    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
383    if(s != verifySize) {
384        rsAssert(!"Size mismatch");
385    }
386
387    const uint32_t oldDimX = alloc->mHal.state.dimensionX;
388    const uint32_t dimX = newType->getDimX();
389
390    if (dimX > oldDimX) {
391        uint32_t stride = alloc->mHal.state.elementSizeBytes;
392        memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
393                 0, stride * (dimX - oldDimX));
394    }
395}
396
397static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
398    if (!alloc->getIsScript()) {
399        return; // nothing to sync
400    }
401
402    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
403    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
404
405    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
406    if (!drv->textureID && !drv->renderTargetID) {
407        return; // nothing was rendered here yet, so nothing to sync
408    }
409    if (drv->readBackFBO == NULL) {
410        drv->readBackFBO = new RsdFrameBufferObj();
411        drv->readBackFBO->setColorTarget(drv, 0);
412        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
413                                        alloc->getType()->getDimY());
414    }
415
416    // Bind the framebuffer object so we can read back from it
417    drv->readBackFBO->setActive(rsc);
418
419    // Do the readback
420    RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
421                alloc->mHal.drvState.lod[0].dimY,
422                drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
423
424    // Revert framebuffer to its original
425    lastFbo->setActive(rsc);
426}
427
428
429void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
430                         RsAllocationUsageType src) {
431    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
432
433    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
434        if(!alloc->getIsRenderTarget()) {
435            rsc->setError(RS_ERROR_FATAL_DRIVER,
436                          "Attempting to sync allocation from render target, "
437                          "for non-render target allocation");
438        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
439            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
440                                                 "render target");
441        } else {
442            rsdAllocationSyncFromFBO(rsc, alloc);
443        }
444        return;
445    }
446
447    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
448
449    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
450        UploadToTexture(rsc, alloc);
451    } else {
452        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
453            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
454            AllocateRenderTarget(rsc, alloc);
455        }
456    }
457    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
458        UploadToBufferObject(rsc, alloc);
459    }
460
461    drv->uploadDeferred = false;
462}
463
464void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
465    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
466    drv->uploadDeferred = true;
467}
468
469int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) {
470    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
471    UploadToTexture(rsc, alloc);
472    return drv->textureID;
473}
474
475static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
476    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
477
478    int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
479    if (r) {
480        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
481        return false;
482    }
483
484    // Must lock the whole surface
485    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
486    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
487
488    void *dst = NULL;
489    mapper.lock(drv->wndBuffer->handle,
490            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
491            bounds, &dst);
492    alloc->mHal.drvState.lod[0].mallocPtr = dst;
493    alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
494
495    return true;
496}
497
498void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
499    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
500
501    //ALOGE("rsdAllocationSetSurfaceTexture %p  %p", alloc, nw);
502
503    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
504        //TODO finish support for render target + script
505        drv->wnd = nw;
506        return;
507    }
508
509
510    // Cleanup old surface if there is one.
511    if (alloc->mHal.state.wndSurface) {
512        ANativeWindow *old = alloc->mHal.state.wndSurface;
513        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
514        mapper.unlock(drv->wndBuffer->handle);
515        old->queueBuffer(old, drv->wndBuffer, -1);
516    }
517
518    if (nw != NULL) {
519        int32_t r;
520        uint32_t flags = 0;
521        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
522            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
523        }
524        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
525            flags |= GRALLOC_USAGE_HW_RENDER;
526        }
527
528        r = native_window_set_usage(nw, flags);
529        if (r) {
530            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
531            return;
532        }
533
534        r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX,
535                                                 alloc->mHal.state.dimensionY);
536        if (r) {
537            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
538            return;
539        }
540
541        r = native_window_set_buffer_count(nw, 3);
542        if (r) {
543            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
544            return;
545        }
546
547        IoGetBuffer(rsc, alloc, nw);
548    }
549}
550
551void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
552    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
553    ANativeWindow *nw = alloc->mHal.state.wndSurface;
554
555    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
556        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
557        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
558        return;
559    }
560
561    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
562        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
563        mapper.unlock(drv->wndBuffer->handle);
564        int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
565        if (r) {
566            rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
567            return;
568        }
569
570        IoGetBuffer(rsc, alloc, nw);
571    }
572}
573
574void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
575    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
576    alloc->mHal.state.surfaceTexture->updateTexImage();
577}
578
579
580void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
581                         uint32_t xoff, uint32_t lod, uint32_t count,
582                         const void *data, size_t sizeBytes) {
583    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
584
585    const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
586    uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
587    uint32_t size = count * eSize;
588
589    if (ptr != data) {
590        // Skip the copy if we are the same allocation. This can arise from
591        // our Bitmap optimization, where we share the same storage.
592        if (alloc->mHal.state.hasReferences) {
593            alloc->incRefs(data, count);
594            alloc->decRefs(ptr, count);
595        }
596        memcpy(ptr, data, size);
597    }
598    drv->uploadDeferred = true;
599}
600
601void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
602                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
603                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
604    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
605
606    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
607    uint32_t lineSize = eSize * w;
608    if (!stride) {
609        stride = lineSize;
610    }
611
612    if (alloc->mHal.drvState.lod[0].mallocPtr) {
613        const uint8_t *src = static_cast<const uint8_t *>(data);
614        uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
615        if (dst == src) {
616            // Skip the copy if we are the same allocation. This can arise from
617            // our Bitmap optimization, where we share the same storage.
618            drv->uploadDeferred = true;
619            return;
620        }
621
622        for (uint32_t line=yoff; line < (yoff+h); line++) {
623            if (alloc->mHal.state.hasReferences) {
624                alloc->incRefs(src, w);
625                alloc->decRefs(dst, w);
626            }
627            memcpy(dst, src, lineSize);
628            src += stride;
629            dst += alloc->mHal.drvState.lod[lod].stride;
630        }
631        drv->uploadDeferred = true;
632    } else {
633        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
634    }
635}
636
637void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
638                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
639                         uint32_t lod, RsAllocationCubemapFace face,
640                         uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
641
642}
643
644void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
645                         uint32_t xoff, uint32_t lod, uint32_t count,
646                         void *data, size_t sizeBytes) {
647    const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
648    const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
649    if (data != ptr) {
650        // Skip the copy if we are the same allocation. This can arise from
651        // our Bitmap optimization, where we share the same storage.
652        memcpy(data, ptr, count * eSize);
653    }
654}
655
656void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
657                                uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
658                                uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
659    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
660    uint32_t lineSize = eSize * w;
661    if (!stride) {
662        stride = lineSize;
663    }
664
665    if (alloc->mHal.drvState.lod[0].mallocPtr) {
666        uint8_t *dst = static_cast<uint8_t *>(data);
667        const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face);
668        if (dst == src) {
669            // Skip the copy if we are the same allocation. This can arise from
670            // our Bitmap optimization, where we share the same storage.
671            return;
672        }
673
674        for (uint32_t line=yoff; line < (yoff+h); line++) {
675            memcpy(dst, src, lineSize);
676            dst += stride;
677            src += alloc->mHal.drvState.lod[lod].stride;
678        }
679    } else {
680        ALOGE("Add code to readback from non-script memory");
681    }
682}
683
684
685void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
686                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
687                         uint32_t lod, RsAllocationCubemapFace face,
688                         uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) {
689
690}
691
692void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
693                          const android::renderscript::Allocation *alloc) {
694    return alloc->mHal.drvState.lod[0].mallocPtr;
695}
696
697void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
698                          const android::renderscript::Allocation *alloc) {
699
700}
701
702void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
703                               const android::renderscript::Allocation *dstAlloc,
704                               uint32_t dstXoff, uint32_t dstLod, uint32_t count,
705                               const android::renderscript::Allocation *srcAlloc,
706                               uint32_t srcXoff, uint32_t srcLod) {
707}
708
709
710void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
711                                      const android::renderscript::Allocation *dstAlloc,
712                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
713                                      RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
714                                      const android::renderscript::Allocation *srcAlloc,
715                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
716                                      RsAllocationCubemapFace srcFace) {
717    uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
718    for (uint32_t i = 0; i < h; i ++) {
719        uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
720        uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
721        memcpy(dstPtr, srcPtr, w * elementSize);
722
723        //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
724        //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
725    }
726}
727
728void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
729                               const android::renderscript::Allocation *dstAlloc,
730                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
731                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
732                               const android::renderscript::Allocation *srcAlloc,
733                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
734                               RsAllocationCubemapFace srcFace) {
735    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
736        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
737                                             "yet implemented.");
738        return;
739    }
740    rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
741                                     dstLod, dstFace, w, h, srcAlloc,
742                                     srcXoff, srcYoff, srcLod, srcFace);
743}
744
745void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
746                               const android::renderscript::Allocation *dstAlloc,
747                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
748                               uint32_t dstLod, RsAllocationCubemapFace dstFace,
749                               uint32_t w, uint32_t h, uint32_t d,
750                               const android::renderscript::Allocation *srcAlloc,
751                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
752                               uint32_t srcLod, RsAllocationCubemapFace srcFace) {
753}
754
755void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
756                                uint32_t x,
757                                const void *data, uint32_t cIdx, uint32_t sizeBytes) {
758    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
759
760    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
761    uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
762
763    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
764    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
765
766    if (alloc->mHal.state.hasReferences) {
767        e->incRefs(data);
768        e->decRefs(ptr);
769    }
770
771    memcpy(ptr, data, sizeBytes);
772    drv->uploadDeferred = true;
773}
774
775void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
776                                uint32_t x, uint32_t y,
777                                const void *data, uint32_t cIdx, uint32_t sizeBytes) {
778    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
779
780    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
781    uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
782
783    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
784    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
785
786    if (alloc->mHal.state.hasReferences) {
787        e->incRefs(data);
788        e->decRefs(ptr);
789    }
790
791    memcpy(ptr, data, sizeBytes);
792    drv->uploadDeferred = true;
793}
794
795static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
796    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
797    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
798
799    for (uint32_t y=0; y < h; y++) {
800        uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
801        const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
802        const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
803
804        for (uint32_t x=0; x < w; x++) {
805            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
806            oPtr ++;
807            i1 += 2;
808            i2 += 2;
809        }
810    }
811}
812
813static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
814    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
815    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
816
817    for (uint32_t y=0; y < h; y++) {
818        uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
819        const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
820        const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
821
822        for (uint32_t x=0; x < w; x++) {
823            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
824            oPtr ++;
825            i1 += 2;
826            i2 += 2;
827        }
828    }
829}
830
831static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
832    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
833    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
834
835    for (uint32_t y=0; y < h; y++) {
836        uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
837        const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
838        const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
839
840        for (uint32_t x=0; x < w; x++) {
841            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
842            oPtr ++;
843            i1 += 2;
844            i2 += 2;
845        }
846    }
847}
848
849void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
850    if(!alloc->mHal.drvState.lod[0].mallocPtr) {
851        return;
852    }
853    uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
854    for (uint32_t face = 0; face < numFaces; face ++) {
855        for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
856            switch (alloc->getType()->getElement()->getSizeBits()) {
857            case 32:
858                mip8888(alloc, lod, (RsAllocationCubemapFace)face);
859                break;
860            case 16:
861                mip565(alloc, lod, (RsAllocationCubemapFace)face);
862                break;
863            case 8:
864                mip8(alloc, lod, (RsAllocationCubemapFace)face);
865                break;
866            }
867        }
868    }
869}
870
871
872