rsdAllocation.cpp revision 2178d4262c06210b27c51a04379d23d1368b2e8b
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 rsdAllocationAdapterOffset(const Context *rsc, const Allocation *alloc) {
476    //ALOGE("rsdAllocationAdapterOffset");
477
478    // Get a base pointer to the new LOD
479    const Allocation *base = alloc->mHal.state.baseAlloc;
480    const Type *type = alloc->mHal.state.type;
481    if (base == nullptr) {
482        return;
483    }
484
485    //ALOGE("rsdAllocationAdapterOffset  %p  %p", ptrA, ptrB);
486    //ALOGE("rsdAllocationAdapterOffset  lodCount %i", alloc->mHal.drvState.lodCount);
487
488    const int lodBias = alloc->mHal.state.originLOD;
489    uint32_t lodCount = rsMax(alloc->mHal.drvState.lodCount, (uint32_t)1);
490    for (uint32_t lod=0; lod < lodCount; lod++) {
491        alloc->mHal.drvState.lod[lod] = base->mHal.drvState.lod[lod + lodBias];
492        alloc->mHal.drvState.lod[lod].mallocPtr = GetOffsetPtr(alloc,
493                      alloc->mHal.state.originX, alloc->mHal.state.originY, alloc->mHal.state.originZ,
494                      lodBias, (RsAllocationCubemapFace)alloc->mHal.state.originFace);
495    }
496}
497
498bool rsdAllocationAdapterInit(const Context *rsc, Allocation *alloc) {
499    DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
500    if (!drv) {
501        return false;
502    }
503    alloc->mHal.drv = drv;
504
505    // We need to build an allocation that looks like a subset of the parent allocation
506    rsdAllocationAdapterOffset(rsc, alloc);
507
508    return true;
509}
510
511void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
512    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
513
514    if (alloc->mHal.state.baseAlloc == nullptr) {
515#ifndef RS_COMPATIBILITY_LIB
516        if (drv->bufferID) {
517            // Causes a SW crash....
518            //ALOGV(" mBufferID %i", mBufferID);
519            //glDeleteBuffers(1, &mBufferID);
520            //mBufferID = 0;
521        }
522        if (drv->textureID) {
523            RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
524            drv->textureID = 0;
525        }
526        if (drv->renderTargetID) {
527            RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
528            drv->renderTargetID = 0;
529        }
530#endif
531
532        if (alloc->mHal.drvState.lod[0].mallocPtr) {
533            // don't free user-allocated ptrs or IO_OUTPUT buffers
534            if (!(drv->useUserProvidedPtr) &&
535                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) &&
536                !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
537                    free(alloc->mHal.drvState.lod[0].mallocPtr);
538            }
539            alloc->mHal.drvState.lod[0].mallocPtr = nullptr;
540        }
541
542#ifndef RS_COMPATIBILITY_LIB
543        if (drv->readBackFBO != nullptr) {
544            delete drv->readBackFBO;
545            drv->readBackFBO = nullptr;
546        }
547
548        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
549            (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
550
551            DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
552            ANativeWindow *nw = drv->wndSurface;
553            if (nw) {
554                GraphicBufferMapper &mapper = GraphicBufferMapper::get();
555                mapper.unlock(drv->wndBuffer->handle);
556                int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
557
558                drv->wndSurface = nullptr;
559                native_window_api_disconnect(nw, NATIVE_WINDOW_API_CPU);
560                nw->decStrong(nullptr);
561            }
562        }
563#endif
564    }
565
566    free(drv);
567    alloc->mHal.drv = nullptr;
568}
569
570void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
571                         const Type *newType, bool zeroNew) {
572    const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
573    const uint32_t dimX = newType->getDimX();
574
575    // can't resize Allocations with user-allocated buffers
576    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
577        ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
578        return;
579    }
580    void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
581    // Calculate the object size
582    size_t s = AllocationBuildPointerTable(rsc, alloc, newType, nullptr);
583    uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
584    // Build the relative pointer tables.
585    size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
586    if(s != verifySize) {
587        rsAssert(!"Size mismatch");
588    }
589
590
591    if (dimX > oldDimX) {
592        size_t stride = alloc->mHal.state.elementSizeBytes;
593        memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
594                 0, stride * (dimX - oldDimX));
595    }
596}
597
598static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
599#ifndef RS_COMPATIBILITY_LIB
600    if (!alloc->getIsScript()) {
601        return; // nothing to sync
602    }
603
604    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
605    RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
606
607    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
608    if (!drv->textureID && !drv->renderTargetID) {
609        return; // nothing was rendered here yet, so nothing to sync
610    }
611    if (drv->readBackFBO == nullptr) {
612        drv->readBackFBO = new RsdFrameBufferObj();
613        drv->readBackFBO->setColorTarget(drv, 0);
614        drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
615                                        alloc->getType()->getDimY());
616    }
617
618    // Bind the framebuffer object so we can read back from it
619    drv->readBackFBO->setActive(rsc);
620
621    // Do the readback
622    RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
623                alloc->mHal.drvState.lod[0].dimY,
624                drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
625
626    // Revert framebuffer to its original
627    lastFbo->setActive(rsc);
628#endif
629}
630
631
632void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
633                         RsAllocationUsageType src) {
634    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
635
636    if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
637        if(!alloc->getIsRenderTarget()) {
638            rsc->setError(RS_ERROR_FATAL_DRIVER,
639                          "Attempting to sync allocation from render target, "
640                          "for non-render target allocation");
641        } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
642            rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
643                                                 "render target");
644        } else {
645            rsdAllocationSyncFromFBO(rsc, alloc);
646        }
647        return;
648    }
649
650    rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT || src == RS_ALLOCATION_USAGE_SHARED);
651
652    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
653        UploadToTexture(rsc, alloc);
654    } else {
655        if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
656            !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
657            AllocateRenderTarget(rsc, alloc);
658        }
659    }
660    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
661        UploadToBufferObject(rsc, alloc);
662    }
663
664    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
665
666        if (src == RS_ALLOCATION_USAGE_SHARED) {
667            // just a memory fence for the CPU driver
668            // vendor drivers probably want to flush any dirty cachelines for
669            // this particular Allocation
670            __sync_synchronize();
671        }
672    }
673
674    drv->uploadDeferred = false;
675}
676
677void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
678    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
679    drv->uploadDeferred = true;
680}
681
682#ifndef RS_COMPATIBILITY_LIB
683static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
684    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
685
686    int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
687    if (r) {
688        rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
689        return false;
690    }
691
692    // Must lock the whole surface
693    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
694    Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
695
696    void *dst = nullptr;
697    mapper.lock(drv->wndBuffer->handle,
698            GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
699            bounds, &dst);
700    alloc->mHal.drvState.lod[0].mallocPtr = dst;
701    alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
702    rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
703
704    return true;
705}
706#endif
707
708void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
709#ifndef RS_COMPATIBILITY_LIB
710    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
711    ANativeWindow *old = drv->wndSurface;
712
713    if (nw) {
714        nw->incStrong(nullptr);
715    }
716
717    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
718        //TODO finish support for render target + script
719        drv->wnd = nw;
720        return;
721    }
722
723    // Cleanup old surface if there is one.
724    if (drv->wndSurface) {
725        ANativeWindow *old = drv->wndSurface;
726        GraphicBufferMapper &mapper = GraphicBufferMapper::get();
727        mapper.unlock(drv->wndBuffer->handle);
728        old->cancelBuffer(old, drv->wndBuffer, -1);
729        drv->wndSurface = nullptr;
730
731        native_window_api_disconnect(old, NATIVE_WINDOW_API_CPU);
732        old->decStrong(nullptr);
733    }
734
735    if (nw != nullptr) {
736        int32_t r;
737        uint32_t flags = 0;
738
739        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
740            flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
741        }
742        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
743            flags |= GRALLOC_USAGE_HW_RENDER;
744        }
745
746        r = native_window_api_connect(nw, NATIVE_WINDOW_API_CPU);
747        if (r) {
748            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
749            goto error;
750        }
751
752        r = native_window_set_usage(nw, flags);
753        if (r) {
754            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
755            goto error;
756        }
757
758        r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
759                                                 alloc->mHal.drvState.lod[0].dimY);
760        if (r) {
761            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
762            goto error;
763        }
764
765        int format = 0;
766        const Element *e = alloc->mHal.state.type->getElement();
767        if ((e->getType() != RS_TYPE_UNSIGNED_8) ||
768            (e->getVectorSize() != 4)) {
769            // We do not check for RGBA, RGBx, to allow for interop with U8_4
770
771            rsc->setError(RS_ERROR_DRIVER, "Surface passed to setSurface is not U8_4, RGBA.");
772            goto error;
773        }
774        format = PIXEL_FORMAT_RGBA_8888;
775
776        r = native_window_set_buffers_format(nw, format);
777        if (r) {
778            rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
779            goto error;
780        }
781
782        IoGetBuffer(rsc, alloc, nw);
783        drv->wndSurface = nw;
784    }
785
786    return;
787
788 error:
789
790    if (nw) {
791        nw->decStrong(nullptr);
792    }
793
794
795#endif
796}
797
798void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
799#ifndef RS_COMPATIBILITY_LIB
800    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
801    ANativeWindow *nw = drv->wndSurface;
802    if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
803        RsdHal *dc = (RsdHal *)rsc->mHal.drv;
804        RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
805        return;
806    }
807    if (nw) {
808        if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
809            GraphicBufferMapper &mapper = GraphicBufferMapper::get();
810            mapper.unlock(drv->wndBuffer->handle);
811            int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
812            if (r) {
813                rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
814                return;
815            }
816
817            IoGetBuffer(rsc, alloc, nw);
818        }
819    } else {
820        rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
821        return;
822    }
823#endif
824}
825
826void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
827#ifndef RS_COMPATIBILITY_LIB
828    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
829    if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
830        drv->surfaceTexture->updateTexImage();
831    }
832#endif
833    if (alloc->mHal.state.yuv) {
834        DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
835    }
836}
837
838
839void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
840                         uint32_t xoff, uint32_t lod, size_t count,
841                         const void *data, size_t sizeBytes) {
842    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
843
844    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
845    uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
846    size_t size = count * eSize;
847    if (ptr != data) {
848        // Skip the copy if we are the same allocation. This can arise from
849        // our Bitmap optimization, where we share the same storage.
850        if (alloc->mHal.state.hasReferences) {
851            alloc->incRefs(data, count);
852            alloc->decRefs(ptr, count);
853        }
854        memcpy(ptr, data, size);
855    }
856    drv->uploadDeferred = true;
857}
858
859void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
860                         uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
861                         uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
862    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
863
864    size_t eSize = alloc->mHal.state.elementSizeBytes;
865    size_t lineSize = eSize * w;
866    if (!stride) {
867        stride = lineSize;
868    }
869
870    if (alloc->mHal.drvState.lod[0].mallocPtr) {
871        const uint8_t *src = static_cast<const uint8_t *>(data);
872        uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
873        if (dst == src) {
874            // Skip the copy if we are the same allocation. This can arise from
875            // our Bitmap optimization, where we share the same storage.
876            drv->uploadDeferred = true;
877            return;
878        }
879
880        for (uint32_t line=yoff; line < (yoff+h); line++) {
881            if (alloc->mHal.state.hasReferences) {
882                alloc->incRefs(src, w);
883                alloc->decRefs(dst, w);
884            }
885            memcpy(dst, src, lineSize);
886            src += stride;
887            dst += alloc->mHal.drvState.lod[lod].stride;
888        }
889        if (alloc->mHal.state.yuv) {
890            size_t clineSize = lineSize;
891            int lod = 1;
892            int maxLod = 2;
893            if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YV12) {
894                maxLod = 3;
895                clineSize >>= 1;
896            } else if (alloc->mHal.state.yuv == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
897                lod = 2;
898                maxLod = 3;
899            }
900
901            while (lod < maxLod) {
902                uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
903
904                for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
905                    memcpy(dst, src, clineSize);
906                    src += alloc->mHal.drvState.lod[lod].stride;
907                    dst += alloc->mHal.drvState.lod[lod].stride;
908                }
909                lod++;
910            }
911
912        }
913        drv->uploadDeferred = true;
914    } else {
915        Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
916    }
917}
918
919void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
920                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
921                         uint32_t lod,
922                         uint32_t w, uint32_t h, uint32_t d, const void *data,
923                         size_t sizeBytes, size_t stride) {
924    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
925
926    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
927    uint32_t lineSize = eSize * w;
928    if (!stride) {
929        stride = lineSize;
930    }
931
932    if (alloc->mHal.drvState.lod[0].mallocPtr) {
933        const uint8_t *src = static_cast<const uint8_t *>(data);
934        for (uint32_t z = zoff; z < (d + zoff); z++) {
935            uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod,
936                                        RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
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                drv->uploadDeferred = true;
941                return;
942            }
943
944            for (uint32_t line=yoff; line < (yoff+h); line++) {
945                if (alloc->mHal.state.hasReferences) {
946                    alloc->incRefs(src, w);
947                    alloc->decRefs(dst, w);
948                }
949                memcpy(dst, src, lineSize);
950                src += stride;
951                dst += alloc->mHal.drvState.lod[lod].stride;
952            }
953        }
954        drv->uploadDeferred = true;
955    }
956}
957
958void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
959                         uint32_t xoff, uint32_t lod, size_t count,
960                         void *data, size_t sizeBytes) {
961    const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
962    const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
963    if (data != ptr) {
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        memcpy(data, ptr, count * eSize);
967    }
968}
969
970void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
971                                uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
972                                uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
973    size_t eSize = alloc->mHal.state.elementSizeBytes;
974    size_t lineSize = eSize * w;
975    if (!stride) {
976        stride = lineSize;
977    }
978
979    if (alloc->mHal.drvState.lod[0].mallocPtr) {
980        uint8_t *dst = static_cast<uint8_t *>(data);
981        const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
982        if (dst == src) {
983            // Skip the copy if we are the same allocation. This can arise from
984            // our Bitmap optimization, where we share the same storage.
985            return;
986        }
987
988        for (uint32_t line=yoff; line < (yoff+h); line++) {
989            memcpy(dst, src, lineSize);
990            dst += stride;
991            src += alloc->mHal.drvState.lod[lod].stride;
992        }
993    } else {
994        ALOGE("Add code to readback from non-script memory");
995    }
996}
997
998
999void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
1000                         uint32_t xoff, uint32_t yoff, uint32_t zoff,
1001                         uint32_t lod,
1002                         uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
1003    uint32_t eSize = alloc->mHal.state.elementSizeBytes;
1004    uint32_t lineSize = eSize * w;
1005    if (!stride) {
1006        stride = lineSize;
1007    }
1008
1009    if (alloc->mHal.drvState.lod[0].mallocPtr) {
1010        uint8_t *dst = static_cast<uint8_t *>(data);
1011        for (uint32_t z = zoff; z < (d + zoff); z++) {
1012            const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod,
1013                                              RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1014            if (dst == src) {
1015                // Skip the copy if we are the same allocation. This can arise from
1016                // our Bitmap optimization, where we share the same storage.
1017                return;
1018            }
1019
1020            for (uint32_t line=yoff; line < (yoff+h); line++) {
1021                memcpy(dst, src, lineSize);
1022                dst += stride;
1023                src += alloc->mHal.drvState.lod[lod].stride;
1024            }
1025        }
1026    }
1027}
1028
1029void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
1030                          const android::renderscript::Allocation *alloc) {
1031    return alloc->mHal.drvState.lod[0].mallocPtr;
1032}
1033
1034void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
1035                          const android::renderscript::Allocation *alloc) {
1036
1037}
1038
1039void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
1040                               const android::renderscript::Allocation *dstAlloc,
1041                               uint32_t dstXoff, uint32_t dstLod, size_t count,
1042                               const android::renderscript::Allocation *srcAlloc,
1043                               uint32_t srcXoff, uint32_t srcLod) {
1044}
1045
1046
1047void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
1048                                      const android::renderscript::Allocation *dstAlloc,
1049                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1050                                      RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1051                                      const android::renderscript::Allocation *srcAlloc,
1052                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1053                                      RsAllocationCubemapFace srcFace) {
1054    size_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1055    for (uint32_t i = 0; i < h; i ++) {
1056        uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace);
1057        uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace);
1058        memcpy(dstPtr, srcPtr, w * elementSize);
1059
1060        //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1061        //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1062    }
1063}
1064
1065void rsdAllocationData3D_alloc_script(const android::renderscript::Context *rsc,
1066                                      const android::renderscript::Allocation *dstAlloc,
1067                                      uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod,
1068                                      uint32_t w, uint32_t h, uint32_t d,
1069                                      const android::renderscript::Allocation *srcAlloc,
1070                                      uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, uint32_t srcLod) {
1071    uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1072    for (uint32_t j = 0; j < d; j++) {
1073        for (uint32_t i = 0; i < h; i ++) {
1074            uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j,
1075                                           dstLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1076            uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j,
1077                                           srcLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1078            memcpy(dstPtr, srcPtr, w * elementSize);
1079
1080            //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1081            //     dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1082        }
1083    }
1084}
1085
1086void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
1087                               const android::renderscript::Allocation *dstAlloc,
1088                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1089                               RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1090                               const android::renderscript::Allocation *srcAlloc,
1091                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1092                               RsAllocationCubemapFace srcFace) {
1093    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1094        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1095                                             "yet implemented.");
1096        return;
1097    }
1098    rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
1099                                     dstLod, dstFace, w, h, srcAlloc,
1100                                     srcXoff, srcYoff, srcLod, srcFace);
1101}
1102
1103void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
1104                               const android::renderscript::Allocation *dstAlloc,
1105                               uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
1106                               uint32_t dstLod,
1107                               uint32_t w, uint32_t h, uint32_t d,
1108                               const android::renderscript::Allocation *srcAlloc,
1109                               uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
1110                               uint32_t srcLod) {
1111    if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1112        rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1113                                             "yet implemented.");
1114        return;
1115    }
1116    rsdAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff,
1117                                     dstLod, w, h, d, srcAlloc,
1118                                     srcXoff, srcYoff, srcZoff, srcLod);
1119}
1120
1121void rsdAllocationElementData(const Context *rsc, const Allocation *alloc,
1122                              uint32_t x, uint32_t y, uint32_t z,
1123                              const void *data, uint32_t cIdx, size_t sizeBytes) {
1124    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1125
1126    uint8_t * ptr = GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1127
1128    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1129    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1130
1131    if (alloc->mHal.state.hasReferences) {
1132        e->incRefs(data);
1133        e->decRefs(ptr);
1134    }
1135
1136    memcpy(ptr, data, sizeBytes);
1137    drv->uploadDeferred = true;
1138}
1139
1140void rsdAllocationElementRead(const Context *rsc, const Allocation *alloc,
1141                              uint32_t x, uint32_t y, uint32_t z,
1142                              void *data, uint32_t cIdx, size_t sizeBytes) {
1143    DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1144
1145    uint8_t * ptr = GetOffsetPtr(alloc, x, y, z, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1146
1147    const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1148    ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1149
1150    memcpy(data, ptr, sizeBytes);
1151}
1152
1153static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1154    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1155    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1156
1157    for (uint32_t y=0; y < h; y++) {
1158        uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1159        const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2, lod, face);
1160        const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2+1, lod, face);
1161
1162        for (uint32_t x=0; x < w; x++) {
1163            *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1164            oPtr ++;
1165            i1 += 2;
1166            i2 += 2;
1167        }
1168    }
1169}
1170
1171static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1172    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1173    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1174
1175    for (uint32_t y=0; y < h; y++) {
1176        uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1177        const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1178        const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1179
1180        for (uint32_t x=0; x < w; x++) {
1181            *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1182            oPtr ++;
1183            i1 += 2;
1184            i2 += 2;
1185        }
1186    }
1187}
1188
1189static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
1190    uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1191    uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
1192
1193    for (uint32_t y=0; y < h; y++) {
1194        uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1195        const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1196        const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
1197
1198        for (uint32_t x=0; x < w; x++) {
1199            *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1200            oPtr ++;
1201            i1 += 2;
1202            i2 += 2;
1203        }
1204    }
1205}
1206
1207void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
1208    if(!alloc->mHal.drvState.lod[0].mallocPtr) {
1209        return;
1210    }
1211    uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1212    for (uint32_t face = 0; face < numFaces; face ++) {
1213        for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1214            switch (alloc->getType()->getElement()->getSizeBits()) {
1215            case 32:
1216                mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1217                break;
1218            case 16:
1219                mip565(alloc, lod, (RsAllocationCubemapFace)face);
1220                break;
1221            case 8:
1222                mip8(alloc, lod, (RsAllocationCubemapFace)face);
1223                break;
1224            }
1225        }
1226    }
1227}
1228
1229uint32_t rsdAllocationGrallocBits(const android::renderscript::Context *rsc,
1230                                  android::renderscript::Allocation *alloc)
1231{
1232    return 0;
1233}
1234
1235void rsdAllocationUpdateCachedObject(const Context *rsc,
1236                                     const Allocation *alloc,
1237                                     rs_allocation *obj)
1238{
1239    obj->p = alloc;
1240#ifdef __LP64__
1241    if (alloc != nullptr) {
1242        obj->r = alloc->mHal.drvState.lod[0].mallocPtr;
1243        obj->v1 = alloc->mHal.drv;
1244        obj->v2 = (void *)alloc->mHal.drvState.lod[0].stride;
1245    } else {
1246        obj->r = nullptr;
1247        obj->v1 = nullptr;
1248        obj->v2 = nullptr;
1249    }
1250#endif
1251}
1252