rsdAllocation.cpp revision 5537da4b965ce6d0c2043b945757b06688ee803e
1/*
2 * Copyright (C) 2013 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#include "rsdCore.h"
18#include "rsdAllocation.h"
19
20#include "rsAllocation.h"
21
22#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
23#include "system/window.h"
24#include "ui/Rect.h"
25#include "ui/GraphicBufferMapper.h"
26#endif
27
28#ifdef RS_COMPATIBILITY_LIB
29#include "rsCompatibilityLib.h"
30#else
31#include "rsdFrameBufferObj.h"
32#include "gui/GLConsumer.h"
33#include "gui/CpuConsumer.h"
34#include "gui/Surface.h"
35#include "hardware/gralloc.h"
36
37#include <GLES/gl.h>
38#include <GLES2/gl2.h>
39#include <GLES/glext.h>
40#endif
41
42#ifdef RS_SERVER
43// server requires malloc.h for memalign
44#include <malloc.h>
45#endif
46
47using namespace android;
48using namespace android::renderscript;
49
50
51#ifndef RS_COMPATIBILITY_LIB
52const static GLenum gFaceOrder[] = {
53    GL_TEXTURE_CUBE_MAP_POSITIVE_X,
54    GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
55    GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
56    GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
57    GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
58    GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
59};
60
61GLenum rsdTypeToGLType(RsDataType t) {
62    switch (t) {
63    case RS_TYPE_UNSIGNED_5_6_5:    return GL_UNSIGNED_SHORT_5_6_5;
64    case RS_TYPE_UNSIGNED_5_5_5_1:  return GL_UNSIGNED_SHORT_5_5_5_1;
65    case RS_TYPE_UNSIGNED_4_4_4_4:  return GL_UNSIGNED_SHORT_4_4_4_4;
66
67    //case RS_TYPE_FLOAT_16:      return GL_HALF_FLOAT;
68    case RS_TYPE_FLOAT_32:      return GL_FLOAT;
69    case RS_TYPE_UNSIGNED_8:    return GL_UNSIGNED_BYTE;
70    case RS_TYPE_UNSIGNED_16:   return GL_UNSIGNED_SHORT;
71    case RS_TYPE_SIGNED_8:      return GL_BYTE;
72    case RS_TYPE_SIGNED_16:     return GL_SHORT;
73    default:    break;
74    }
75    return 0;
76}
77
78GLenum rsdKindToGLFormat(RsDataKind k) {
79    switch (k) {
80    case RS_KIND_PIXEL_L: return GL_LUMINANCE;
81    case RS_KIND_PIXEL_A: return GL_ALPHA;
82    case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
83    case RS_KIND_PIXEL_RGB: return GL_RGB;
84    case RS_KIND_PIXEL_RGBA: return GL_RGBA;
85    case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
86    default: break;
87    }
88    return 0;
89}
90#endif
91
92uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
93                      uint32_t xoff, uint32_t yoff, uint32_t zoff,
94                      uint32_t lod, RsAllocationCubemapFace face) {
95    uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
96    ptr += face * alloc->mHal.drvState.faceOffset;
97    ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * alloc->mHal.drvState.lod[lod].stride;
98    ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
99    ptr += xoff * alloc->mHal.state.elementSizeBytes;
100    return ptr;
101}
102
103
104static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
105                            uint32_t xoff, uint32_t yoff, uint32_t lod,
106                            RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
107#ifndef RS_COMPATIBILITY_LIB
108    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
109
110    rsAssert(drv->textureID);
111    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
112    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
113    GLenum t = GL_TEXTURE_2D;
114    if (alloc->mHal.state.hasFaces) {
115        t = gFaceOrder[face];
116    }
117    RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
118#endif
119}
120
121
122#ifndef RS_COMPATIBILITY_LIB
123static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
124    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
125
126    RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
127    RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
128
129    uint32_t faceCount = 1;
130    if (alloc->mHal.state.hasFaces) {
131        faceCount = 6;
132    }
133
134    rsdGLCheckError(rsc, "Upload2DTexture 1 ");
135    for (uint32_t face = 0; face < faceCount; face ++) {
136        for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
137            const uint8_t *p = GetOffsetPtr(alloc, 0, 0, 0, lod, (RsAllocationCubemapFace)face);
138
139            GLenum t = GL_TEXTURE_2D;
140            if (alloc->mHal.state.hasFaces) {
141                t = gFaceOrder[face];
142            }
143
144            if (isFirstUpload) {
145                RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
146                             alloc->mHal.state.type->getLODDimX(lod),
147                             alloc->mHal.state.type->getLODDimY(lod),
148                             0, drv->glFormat, drv->glType, p);
149            } else {
150                RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
151                                alloc->mHal.state.type->getLODDimX(lod),
152                                alloc->mHal.state.type->getLODDimY(lod),
153                                drv->glFormat, drv->glType, p);
154            }
155        }
156    }
157
158    if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
159        RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
160    }
161    rsdGLCheckError(rsc, "Upload2DTexture");
162}
163#endif
164
165static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
166#ifndef RS_COMPATIBILITY_LIB
167    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
168
169    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
170        if (!drv->textureID) {
171            RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
172        }
173        return;
174    }
175
176    if (!drv->glType || !drv->glFormat) {
177        return;
178    }
179
180    if (!alloc->mHal.drvState.lod[0].mallocPtr) {
181        return;
182    }
183
184    bool isFirstUpload = false;
185
186    if (!drv->textureID) {
187        RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
188        isFirstUpload = true;
189    }
190
191    Upload2DTexture(rsc, alloc, isFirstUpload);
192
193    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
194        if (alloc->mHal.drvState.lod[0].mallocPtr) {
195            free(alloc->mHal.drvState.lod[0].mallocPtr);
196            alloc->mHal.drvState.lod[0].mallocPtr = NULL;
197        }
198    }
199    rsdGLCheckError(rsc, "UploadToTexture");
200#endif
201}
202
203static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
204#ifndef RS_COMPATIBILITY_LIB
205    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
206
207    if (!drv->glFormat) {
208        return;
209    }
210
211    if (!drv->renderTargetID) {
212        RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
213
214        if (!drv->renderTargetID) {
215            // This should generally not happen
216            ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
217            rsc->dumpDebug();
218            return;
219        }
220        RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
221        RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
222                    alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
223    }
224    rsdGLCheckError(rsc, "AllocateRenderTarget");
225#endif
226}
227
228static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
229#ifndef RS_COMPATIBILITY_LIB
230    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
231
232    rsAssert(!alloc->mHal.state.type->getDimY());
233    rsAssert(!alloc->mHal.state.type->getDimZ());
234
235    //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
236
237    if (!drv->bufferID) {
238        RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
239    }
240    if (!drv->bufferID) {
241        ALOGE("Upload to buffer object failed");
242        drv->uploadDeferred = true;
243        return;
244    }
245    RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
246    RSD_CALL_GL(glBufferData, drv->glTarget,
247                alloc->mHal.state.type->getPackedSizeBytes(),
248                alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
249    RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
250    rsdGLCheckError(rsc, "UploadToBufferObject");
251#endif
252}
253
254
255static size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
256    // YUV only supports basic 2d
257    // so we can stash the plane pointers in the mipmap levels.
258    size_t uvSize = 0;
259    state->lod[1].dimX = state->lod[0].dimX / 2;
260    state->lod[1].dimY = state->lod[0].dimY / 2;
261    state->lod[2].dimX = state->lod[0].dimX / 2;
262    state->lod[2].dimY = state->lod[0].dimY / 2;
263    state->yuv.shift = 1;
264    state->yuv.step = 1;
265    state->lodCount = 3;
266
267#ifndef RS_SERVER
268    switch(yuv) {
269    case HAL_PIXEL_FORMAT_YV12:
270        state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16);
271        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
272                (state->lod[0].stride * state->lod[0].dimY);
273        uvSize += state->lod[2].stride * state->lod[2].dimY;
274
275        state->lod[1].stride = state->lod[2].stride;
276        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) +
277                (state->lod[2].stride * state->lod[2].dimY);
278        uvSize += state->lod[1].stride * state->lod[2].dimY;
279        break;
280    case HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
281        //state->lod[1].dimX = state->lod[0].dimX;
282        state->lod[1].stride = state->lod[0].stride;
283        state->lod[2].stride = state->lod[0].stride;
284        state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
285                (state->lod[0].stride * state->lod[0].dimY);
286        state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) + 1;
287        uvSize += state->lod[1].stride * state->lod[1].dimY;
288        state->yuv.step = 2;
289        break;
290#ifndef RS_COMPATIBILITY_LIB
291    case HAL_PIXEL_FORMAT_YCbCr_420_888:
292        // This will be filled in by ioReceive()
293        break;
294#endif
295    default:
296        rsAssert(0);
297    }
298#endif
299    return uvSize;
300}
301
302
303static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
304        const Type *type, uint8_t *ptr) {
305    alloc->mHal.drvState.lod[0].dimX = type->getDimX();
306    alloc->mHal.drvState.lod[0].dimY = type->getDimY();
307    alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
308    alloc->mHal.drvState.lod[0].mallocPtr = 0;
309    // Stride needs to be 16-byte aligned too!
310    size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
311    alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
312    alloc->mHal.drvState.lodCount = type->getLODCount();
313    alloc->mHal.drvState.faceCount = type->getDimFaces();
314
315    size_t offsets[Allocation::MAX_LOD];
316    memset(offsets, 0, sizeof(offsets));
317
318    size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
319            rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
320    if(alloc->mHal.drvState.lodCount > 1) {
321        uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
322        uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
323        uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
324        for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
325            alloc->mHal.drvState.lod[lod].dimX = tx;
326            alloc->mHal.drvState.lod[lod].dimY = ty;
327            alloc->mHal.drvState.lod[lod].dimZ = tz;
328            alloc->mHal.drvState.lod[lod].stride =
329                    rsRound(tx * type->getElementSizeBytes(), 16);
330            offsets[lod] = o;
331            o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
332            if (tx > 1) tx >>= 1;
333            if (ty > 1) ty >>= 1;
334            if (tz > 1) tz >>= 1;
335        }
336    } else if (alloc->mHal.state.yuv) {
337        o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
338
339        for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
340            offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
341        }
342    }
343
344    alloc->mHal.drvState.faceOffset = o;
345
346    alloc->mHal.drvState.lod[0].mallocPtr = ptr;
347    for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
348        alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
349    }
350
351    size_t allocSize = alloc->mHal.drvState.faceOffset;
352    if(alloc->mHal.drvState.faceCount) {
353        allocSize *= 6;
354    }
355
356    return allocSize;
357}
358
359static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
360    // We align all allocations to a 16-byte boundary.
361    uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
362    if (!ptr) {
363        return NULL;
364    }
365    if (forceZero) {
366        memset(ptr, 0, allocSize);
367    }
368    return ptr;
369}
370
371bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
372    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
373    if (!drv) {
374        return false;
375    }
376    alloc->mHal.drv = drv;
377
378    // Calculate the object size.
379    size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
380
381    uint8_t * ptr = NULL;
382    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
383
384    } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
385        // Allocation is allocated when the surface is created
386        // in getSurface
387    } else if (alloc->mHal.state.userProvidedPtr != NULL) {
388        // user-provided allocation
389        // limitations: no faces, no LOD, USAGE_SCRIPT or SCRIPT+TEXTURE only
390        if (!(alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED) ||
391              alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE))) {
392            ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT | USAGE_SHARED or USAGE_SCRIPT | USAGE_SHARED | USAGE_GRAPHICS_TEXTURE");
393            return false;
394        }
395        if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
396            ALOGE("User-allocated buffers must not have multiple faces or LODs");
397            return false;
398        }
399
400        // rows must be 16-byte aligned
401        // validate that here, otherwise fall back to not use the user-backed allocation
402        if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
403            ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
404            drv->useUserProvidedPtr = false;
405
406            ptr = allocAlignedMemory(allocSize, forceZero);
407            if (!ptr) {
408                alloc->mHal.drv = NULL;
409                free(drv);
410                return false;
411            }
412
413        } else {
414            drv->useUserProvidedPtr = true;
415            ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
416        }
417    } else {
418        ptr = allocAlignedMemory(allocSize, forceZero);
419        if (!ptr) {
420            alloc->mHal.drv = NULL;
421            free(drv);
422            return false;
423        }
424    }
425    // Build the pointer tables
426    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
427    if(allocSize != verifySize) {
428        rsAssert(!"Size mismatch");
429    }
430
431#ifndef RS_SERVER
432    drv->glTarget = GL_NONE;
433    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
434        if (alloc->mHal.state.hasFaces) {
435            drv->glTarget = GL_TEXTURE_CUBE_MAP;
436        } else {
437            drv->glTarget = GL_TEXTURE_2D;
438        }
439    } else {
440        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
441            drv->glTarget = GL_ARRAY_BUFFER;
442        }
443    }
444#endif
445
446#ifndef RS_COMPATIBILITY_LIB
447    drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
448    drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
449#else
450    drv->glType = 0;
451    drv->glFormat = 0;
452#endif
453
454    if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
455        drv->uploadDeferred = true;
456    }
457
458
459    drv->readBackFBO = NULL;
460
461    // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
462    if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
463        rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
464    }
465
466    return true;
467}
468
469void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
470    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
471
472#ifndef RS_COMPATIBILITY_LIB
473    if (drv->bufferID) {
474        // Causes a SW crash....
475        //ALOGV(" mBufferID %i", mBufferID);
476        //glDeleteBuffers(1, &mBufferID);
477        //mBufferID = 0;
478    }
479    if (drv->textureID) {
480        RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
481        drv->textureID = 0;
482    }
483    if (drv->renderTargetID) {
484        RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
485        drv->renderTargetID = 0;
486    }
487#endif
488
489    if (alloc->mHal.drvState.lod[0].mallocPtr) {
490        // don't free user-allocated ptrs or IO_OUTPUT buffers
491        if (!(drv->useUserProvidedPtr) &&
492            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) &&
493            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
494                free(alloc->mHal.drvState.lod[0].mallocPtr);
495        }
496        alloc->mHal.drvState.lod[0].mallocPtr = NULL;
497    }
498
499#ifndef RS_COMPATIBILITY_LIB
500    if (drv->readBackFBO != NULL) {
501        delete drv->readBackFBO;
502        drv->readBackFBO = NULL;
503    }
504
505    if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
506        (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
507
508        DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
509        ANativeWindow *nw = drv->wndSurface;
510        if (nw) {
511            GraphicBufferMapper &mapper = GraphicBufferMapper::get();
512            mapper.unlock(drv->wndBuffer->handle);
513            int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
514
515            drv->wndSurface = NULL;
516            native_window_api_disconnect(nw, NATIVE_WINDOW_API_CPU);
517            nw->decStrong(NULL);
518        }
519    }
520#endif
521
522    free(drv);
523    alloc->mHal.drv = NULL;
524}
525
526void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
527                         const Type *newType, bool zeroNew) {
528    const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
529    const uint32_t dimX = newType->getDimX();
530
531    // can't resize Allocations with user-allocated buffers
532    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
533        ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
534        return;
535    }
536    void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
537    // Calculate the object size
538    size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
539    uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
540    // Build the relative pointer tables.
541    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
542    if(s != verifySize) {
543        rsAssert(!"Size mismatch");
544    }
545
546
547    if (dimX > oldDimX) {
548        size_t stride = alloc->mHal.state.elementSizeBytes;
549        memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
550                 0, stride * (dimX - oldDimX));
551    }
552}
553
554static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
555#ifndef RS_COMPATIBILITY_LIB
556    if (!alloc->getIsScript()) {
557        return; // nothing to sync
558    }
559
560    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
561    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
562
563    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
564    if (!drv->textureID && !drv->renderTargetID) {
565        return; // nothing was rendered here yet, so nothing to sync
566    }
567    if (drv->readBackFBO == NULL) {
568        drv->readBackFBO = new RsdFrameBufferObj();
569        drv->readBackFBO->setColorTarget(drv, 0);
570        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
571                                        alloc->getType()->getDimY());
572    }
573
574    // Bind the framebuffer object so we can read back from it
575    drv->readBackFBO->setActive(rsc);
576
577    // Do the readback
578    RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
579                alloc->mHal.drvState.lod[0].dimY,
580                drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
581
582    // Revert framebuffer to its original
583    lastFbo->setActive(rsc);
584#endif
585}
586
587
588void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
589                         RsAllocationUsageType src) {
590    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
591
592    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
593        if(!alloc->getIsRenderTarget()) {
594            rsc->setError(RS_ERROR_FATAL_DRIVER,
595                          "Attempting to sync allocation from render target, "
596                          "for non-render target allocation");
597        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
598            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
599                                                 "render target");
600        } else {
601            rsdAllocationSyncFromFBO(rsc, alloc);
602        }
603        return;
604    }
605
606    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT || src == RS_ALLOCATION_USAGE_SHARED);
607
608    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
609        UploadToTexture(rsc, alloc);
610    } else {
611        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
612            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
613            AllocateRenderTarget(rsc, alloc);
614        }
615    }
616    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
617        UploadToBufferObject(rsc, alloc);
618    }
619
620    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
621
622        if (src == RS_ALLOCATION_USAGE_SHARED) {
623            // just a memory fence for the CPU driver
624            // vendor drivers probably want to flush any dirty cachelines for
625            // this particular Allocation
626            __sync_synchronize();
627        }
628    }
629
630    drv->uploadDeferred = false;
631}
632
633void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
634    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
635    drv->uploadDeferred = true;
636}
637
638#ifndef RS_COMPATIBILITY_LIB
639static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
640    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
641
642    int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
643    if (r) {
644        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
645        return false;
646    }
647
648    // Must lock the whole surface
649    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
650    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
651
652    void *dst = NULL;
653    mapper.lock(drv->wndBuffer->handle,
654            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
655            bounds, &dst);
656    alloc->mHal.drvState.lod[0].mallocPtr = dst;
657    alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
658    rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
659
660    return true;
661}
662#endif
663
664void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
665#ifndef RS_COMPATIBILITY_LIB
666    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
667    ANativeWindow *old = drv->wndSurface;
668
669    if (nw) {
670        nw->incStrong(NULL);
671    }
672
673    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
674        //TODO finish support for render target + script
675        drv->wnd = nw;
676        return;
677    }
678
679    // Cleanup old surface if there is one.
680    if (drv->wndSurface) {
681        ANativeWindow *old = drv->wndSurface;
682        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
683        mapper.unlock(drv->wndBuffer->handle);
684        old->cancelBuffer(old, drv->wndBuffer, -1);
685        drv->wndSurface = NULL;
686
687        native_window_api_disconnect(old, NATIVE_WINDOW_API_CPU);
688        old->decStrong(NULL);
689    }
690
691    if (nw != NULL) {
692        int32_t r;
693        uint32_t flags = 0;
694
695        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
696            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
697        }
698        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
699            flags |= GRALLOC_USAGE_HW_RENDER;
700        }
701
702        r = native_window_api_connect(nw, NATIVE_WINDOW_API_CPU);
703        if (r) {
704            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
705            goto error;
706        }
707
708        r = native_window_set_usage(nw, flags);
709        if (r) {
710            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
711            goto error;
712        }
713
714        r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
715                                                 alloc->mHal.drvState.lod[0].dimY);
716        if (r) {
717            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
718            goto error;
719        }
720
721        int format = 0;
722        const Element *e = alloc->mHal.state.type->getElement();
723        rsAssert(e->getType() == RS_TYPE_UNSIGNED_8);
724        rsAssert(e->getVectorSize() == 4);
725        rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
726        format = PIXEL_FORMAT_RGBA_8888;
727
728        r = native_window_set_buffers_format(nw, format);
729        if (r) {
730            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
731            goto error;
732        }
733
734        IoGetBuffer(rsc, alloc, nw);
735        drv->wndSurface = nw;
736    }
737
738    return;
739
740 error:
741
742    if (nw) {
743        nw->decStrong(NULL);
744    }
745
746
747#endif
748}
749
750void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
751#ifndef RS_COMPATIBILITY_LIB
752    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
753    ANativeWindow *nw = drv->wndSurface;
754    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
755        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
756        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
757        return;
758    }
759    if (nw) {
760        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
761            GraphicBufferMapper &mapper = GraphicBufferMapper::get();
762            mapper.unlock(drv->wndBuffer->handle);
763            int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
764            if (r) {
765                rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
766                return;
767            }
768
769            IoGetBuffer(rsc, alloc, nw);
770        }
771    } else {
772        rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
773        return;
774    }
775#endif
776}
777
778void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
779#ifndef RS_COMPATIBILITY_LIB
780    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
781    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
782        drv->surfaceTexture->updateTexImage();
783    }
784#endif
785}
786
787
788void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
789                         uint32_t xoff, uint32_t lod, size_t count,
790                         const void *data, size_t sizeBytes) {
791    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
792
793    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
794    uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
795    size_t size = count * eSize;
796    if (ptr != data) {
797        // Skip the copy if we are the same allocation. This can arise from
798        // our Bitmap optimization, where we share the same storage.
799        if (alloc->mHal.state.hasReferences) {
800            alloc->incRefs(data, count);
801            alloc->decRefs(ptr, count);
802        }
803        memcpy(ptr, data, size);
804    }
805    drv->uploadDeferred = true;
806}
807
808void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
809                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
810                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
811    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
812
813    size_t eSize = alloc->mHal.state.elementSizeBytes;
814    size_t lineSize = eSize * w;
815    if (!stride) {
816        stride = lineSize;
817    }
818
819    if (alloc->mHal.drvState.lod[0].mallocPtr) {
820        const uint8_t *src = static_cast<const uint8_t *>(data);
821        uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
822        if (dst == src) {
823            // Skip the copy if we are the same allocation. This can arise from
824            // our Bitmap optimization, where we share the same storage.
825            drv->uploadDeferred = true;
826            return;
827        }
828
829        for (uint32_t line=yoff; line < (yoff+h); line++) {
830            if (alloc->mHal.state.hasReferences) {
831                alloc->incRefs(src, w);
832                alloc->decRefs(dst, w);
833            }
834            memcpy(dst, src, lineSize);
835            src += stride;
836            dst += alloc->mHal.drvState.lod[lod].stride;
837        }
838        if (alloc->mHal.state.yuv) {
839            size_t clineSize = lineSize;
840            int lod = 1;
841            int maxLod = 2;
842            if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YV12) {
843                maxLod = 3;
844                clineSize >>= 1;
845            } else if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
846                lod = 2;
847                maxLod = 3;
848            }
849
850            while (lod < maxLod) {
851                uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
852
853                for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
854                    memcpy(dst, src, clineSize);
855                    src += alloc->mHal.drvState.lod[lod].stride;
856                    dst += alloc->mHal.drvState.lod[lod].stride;
857                }
858                lod++;
859            }
860
861        }
862        drv->uploadDeferred = true;
863    } else {
864        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
865    }
866}
867
868void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
869                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
870                         uint32_t lod,
871                         uint32_t w, uint32_t h, uint32_t d, const void *data,
872                         size_t sizeBytes, size_t stride) {
873    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
874
875    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
876    uint32_t lineSize = eSize * w;
877    if (!stride) {
878        stride = lineSize;
879    }
880
881    if (alloc->mHal.drvState.lod[0].mallocPtr) {
882        const uint8_t *src = static_cast<const uint8_t *>(data);
883        for (uint32_t z = zoff; z < d; z++) {
884            uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod,
885                                        RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
886            if (dst == src) {
887                // Skip the copy if we are the same allocation. This can arise from
888                // our Bitmap optimization, where we share the same storage.
889                drv->uploadDeferred = true;
890                return;
891            }
892
893            for (uint32_t line=yoff; line < (yoff+h); line++) {
894                if (alloc->mHal.state.hasReferences) {
895                    alloc->incRefs(src, w);
896                    alloc->decRefs(dst, w);
897                }
898                memcpy(dst, src, lineSize);
899                src += stride;
900                dst += alloc->mHal.drvState.lod[lod].stride;
901            }
902        }
903        drv->uploadDeferred = true;
904    }
905}
906
907void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
908                         uint32_t xoff, uint32_t lod, size_t count,
909                         void *data, size_t sizeBytes) {
910    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
911    const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
912    if (data != ptr) {
913        // Skip the copy if we are the same allocation. This can arise from
914        // our Bitmap optimization, where we share the same storage.
915        memcpy(data, ptr, count * eSize);
916    }
917}
918
919void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
920                                uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
921                                uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
922    size_t eSize = alloc->mHal.state.elementSizeBytes;
923    size_t lineSize = eSize * w;
924    if (!stride) {
925        stride = lineSize;
926    }
927
928    if (alloc->mHal.drvState.lod[0].mallocPtr) {
929        uint8_t *dst = static_cast<uint8_t *>(data);
930        const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
931        if (dst == src) {
932            // Skip the copy if we are the same allocation. This can arise from
933            // our Bitmap optimization, where we share the same storage.
934            return;
935        }
936
937        for (uint32_t line=yoff; line < (yoff+h); line++) {
938            memcpy(dst, src, lineSize);
939            dst += stride;
940            src += alloc->mHal.drvState.lod[lod].stride;
941        }
942    } else {
943        ALOGE("Add code to readback from non-script memory");
944    }
945}
946
947
948void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
949                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
950                         uint32_t lod,
951                         uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
952    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
953    uint32_t lineSize = eSize * w;
954    if (!stride) {
955        stride = lineSize;
956    }
957
958    if (alloc->mHal.drvState.lod[0].mallocPtr) {
959        uint8_t *dst = static_cast<uint8_t *>(data);
960        for (uint32_t z = zoff; z < d; z++) {
961            const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod,
962                                              RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
963            if (dst == src) {
964                // Skip the copy if we are the same allocation. This can arise from
965                // our Bitmap optimization, where we share the same storage.
966                return;
967            }
968
969            for (uint32_t line=yoff; line < (yoff+h); line++) {
970                memcpy(dst, src, lineSize);
971                dst += stride;
972                src += alloc->mHal.drvState.lod[lod].stride;
973            }
974        }
975    }
976}
977
978void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
979                          const android::renderscript::Allocation *alloc) {
980    return alloc->mHal.drvState.lod[0].mallocPtr;
981}
982
983void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
984                          const android::renderscript::Allocation *alloc) {
985
986}
987
988void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
989                               const android::renderscript::Allocation *dstAlloc,
990                               uint32_t dstXoff, uint32_t dstLod, size_t count,
991                               const android::renderscript::Allocation *srcAlloc,
992                               uint32_t srcXoff, uint32_t srcLod) {
993}
994
995
996void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
997                                      const android::renderscript::Allocation *dstAlloc,
998                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
999                                      RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1000                                      const android::renderscript::Allocation *srcAlloc,
1001                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1002                                      RsAllocationCubemapFace srcFace) {
1003    size_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1004    for (uint32_t i = 0; i < h; i ++) {
1005        uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace);
1006        uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace);
1007        memcpy(dstPtr, srcPtr, w * elementSize);
1008
1009        //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1010        //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1011    }
1012}
1013
1014void rsdAllocationData3D_alloc_script(const android::renderscript::Context *rsc,
1015                                      const android::renderscript::Allocation *dstAlloc,
1016                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod,
1017                                      uint32_t w, uint32_t h, uint32_t d,
1018                                      const android::renderscript::Allocation *srcAlloc,
1019                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, uint32_t srcLod) {
1020    uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1021    for (uint32_t j = 0; j < d; j++) {
1022        for (uint32_t i = 0; i < h; i ++) {
1023            uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j,
1024                                           dstLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1025            uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j,
1026                                           srcLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1027            memcpy(dstPtr, srcPtr, w * elementSize);
1028
1029            //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1030            //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1031        }
1032    }
1033}
1034
1035void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
1036                               const android::renderscript::Allocation *dstAlloc,
1037                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1038                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1039                               const android::renderscript::Allocation *srcAlloc,
1040                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1041                               RsAllocationCubemapFace srcFace) {
1042    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1043        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1044                                             "yet implemented.");
1045        return;
1046    }
1047    rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
1048                                     dstLod, dstFace, w, h, srcAlloc,
1049                                     srcXoff, srcYoff, srcLod, srcFace);
1050}
1051
1052void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
1053                               const android::renderscript::Allocation *dstAlloc,
1054                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
1055                               uint32_t dstLod,
1056                               uint32_t w, uint32_t h, uint32_t d,
1057                               const android::renderscript::Allocation *srcAlloc,
1058                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
1059                               uint32_t srcLod) {
1060    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1061        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1062                                             "yet implemented.");
1063        return;
1064    }
1065    rsdAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff,
1066                                     dstLod, w, h, d, srcAlloc,
1067                                     srcXoff, srcYoff, srcZoff, srcLod);
1068}
1069
1070void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
1071                                uint32_t x,
1072                                const void *data, uint32_t cIdx, size_t sizeBytes) {
1073    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1074
1075    size_t eSize = alloc->mHal.state.elementSizeBytes;
1076    uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1077
1078    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1079    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1080
1081    if (alloc->mHal.state.hasReferences) {
1082        e->incRefs(data);
1083        e->decRefs(ptr);
1084    }
1085
1086    memcpy(ptr, data, sizeBytes);
1087    drv->uploadDeferred = true;
1088}
1089
1090void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
1091                                uint32_t x, uint32_t y,
1092                                const void *data, uint32_t cIdx, size_t sizeBytes) {
1093    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1094
1095    size_t eSize = alloc->mHal.state.elementSizeBytes;
1096    uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1097
1098    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1099    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1100
1101    if (alloc->mHal.state.hasReferences) {
1102        e->incRefs(data);
1103        e->decRefs(ptr);
1104    }
1105
1106    memcpy(ptr, data, sizeBytes);
1107    drv->uploadDeferred = true;
1108}
1109
1110static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1111    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1112    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1113
1114    for (uint32_t y=0; y < h; y++) {
1115        uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1116        const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2, lod, face);
1117        const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2+1, lod, face);
1118
1119        for (uint32_t x=0; x < w; x++) {
1120            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1121            oPtr ++;
1122            i1 += 2;
1123            i2 += 2;
1124        }
1125    }
1126}
1127
1128static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1129    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1130    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1131
1132    for (uint32_t y=0; y < h; y++) {
1133        uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1134        const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1135        const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1136
1137        for (uint32_t x=0; x < w; x++) {
1138            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1139            oPtr ++;
1140            i1 += 2;
1141            i2 += 2;
1142        }
1143    }
1144}
1145
1146static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1147    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1148    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1149
1150    for (uint32_t y=0; y < h; y++) {
1151        uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1152        const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1153        const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1154
1155        for (uint32_t x=0; x < w; x++) {
1156            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1157            oPtr ++;
1158            i1 += 2;
1159            i2 += 2;
1160        }
1161    }
1162}
1163
1164void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
1165    if(!alloc->mHal.drvState.lod[0].mallocPtr) {
1166        return;
1167    }
1168    uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1169    for (uint32_t face = 0; face < numFaces; face ++) {
1170        for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1171            switch (alloc->getType()->getElement()->getSizeBits()) {
1172            case 32:
1173                mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1174                break;
1175            case 16:
1176                mip565(alloc, lod, (RsAllocationCubemapFace)face);
1177                break;
1178            case 8:
1179                mip8(alloc, lod, (RsAllocationCubemapFace)face);
1180                break;
1181            }
1182        }
1183    }
1184}
1185
1186uint32_t rsdAllocationGrallocBits(const android::renderscript::Context *rsc,
1187                                  android::renderscript::Allocation *alloc)
1188{
1189    return 0;
1190}
1191
1192