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