/* This function is called from erlang:port_call/3. It works a lot like the control call-back, but uses the external term format for input and output. - command is an integer, obtained from the call from erlang (the second argument to erlang:port_call/3). - buf and len provide the arguments to the call (the third argument to erlang:port_call/3). They're decoded using ei. - rbuf points to a return buffer, rlen bytes long. The return data (written to *rbuf) should be a valid erlang term in the external term format. This is converted to an erlang term and returned by erlang:port_call/3 to the caller. If more space than rlen bytes is needed to return data, *rbuf can be set to memory allocated with driver_alloc. This memory will be freed automatically after call has returned. The return value (of this callback function) is the number of bytes returned in *rbuf. If ERL_DRV_ERROR_GENERAL is returned (or in fact, anything < 0), erlang:port_call/3 will throw a BAD_ARG. THIS IMPLEMENTATION of the callback handles two kinds of commands, INIT_COMMAND and ENGINE_COMMAND. An INIT_COMMAND should only be issued once during the lifecycle of the driver, *before* any data is sent to the port using port_command/port_control. The INIT_COMMAND causes the driver to load the specified shared library and call a predefined entry point (see the erlxsl_driver header file for details) to initialize an XslEngine structure. TODO: document ENGINE_COMMAND. TODO: locking during ENGINE_COMMAND calls TODO: support the transform command here as well - small binaries (which we can't/won't share/refcount) can be passed and copied... */ static int call(ErlDrvData drv_data, unsigned int command, char *buf, int len, char **rbuf, int rlen, unsigned int *flags) { int i; int type; int size; int index = 0; int rindex = 0; /*int arity; * char cmd[MAXATOMLEN];*/ char *data; DriverState state; DriverHandle *d = (DriverHandle*)drv_data; ei_decode_version(buf, &index, &i); if (command == INIT_COMMAND) { ei_get_type(buf, &index, &type, &size); INFO("ei_get_type %s of size = %i\n", ((char*)&type), size); // TODO: pull options tuple instead data = ALLOC(size + 1); ei_decode_string(buf, &index, data); INFO("Driver received data %s\n", data); state = init_provider(d, data); } else if (command == ENGINE_COMMAND) { DriverContext *ctx = ALLOC(sizeof(DriverContext)); // ErlDrvPort port = (ErlDrvPort)d->port; // XslEngine *engine = (XslEngine*)d->engine; // ErlDrvTermData callee_pid = driver_caller(port); Command *cmd = init_command(NULL, ctx, NULL, init_iov(Text, 0, NULL)); state = decode_ei_cmd(cmd, buf, &index); if (state == Success) { XslEngine *engine = (XslEngine*)d->engine; if (engine->command != NULL) { EngineState enstate = engine->command(cmd); if (enstate == Ok) { state = Success; } } } /*ei_get_type(buf, &index, &type, &size); INFO("ei_get_type %s of size = %i\n", ((char*)&type), size); data = ALLOC(size + 1); ei_decode_string(buf, &index, data);*/ } else { state = UnknownCommand; } ei_encode_version(*rbuf, &rindex); if (state == InitOk) { INFO("Provider configured with library %s\n", d->loader->name); #ifdef _DRV_SASL_LOGGING // TODO: pull the logging_port and install it.... #endif ei_encode_atom(*rbuf, &rindex, "configured"); } else if (state == Success) { ei_encode_tuple_header(*rbuf, &rindex, 2); ei_encode_atom(*rbuf, &rindex, "ok"); if (state == OutOfMemory) { ei_encode_string(*rbuf, &rindex, heap_space_exhausted); } else if (state == UnknownCommand) { ei_encode_string(*rbuf, &rindex, unknown_command); } else { const char *err = (d->loader)->error_message; ei_encode_string_len(*rbuf, &rindex, err, strlen(err)); } } else { ei_encode_tuple_header(*rbuf, &rindex, 2); ei_encode_atom(*rbuf, &rindex, "error"); if (state == OutOfMemory) { ei_encode_string(*rbuf, &rindex, heap_space_exhausted); } else if (state == UnknownCommand) { ei_encode_string(*rbuf, &rindex, unknown_command); } else { const char *err = (d->loader)->error_message; ei_encode_string_len(*rbuf, &rindex, err, strlen(err)); } } DRV_FREE(data); return(rindex); };
sk_sp<GrTextureProxy> GrYUVProvider::refAsTextureProxy(GrContext* ctx, const GrSurfaceDesc& desc, const SkColorSpace* srcColorSpace, const SkColorSpace* dstColorSpace) { SkYUVPlanesCache::Info yuvInfo; void* planes[3]; sk_sp<SkCachedData> dataStorage = init_provider(this, &yuvInfo, planes); if (!dataStorage) { return nullptr; } sk_sp<GrTextureProxy> yuvTextureProxies[3]; for (int i = 0; i < 3; i++) { int componentWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth; int componentHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight; // If the sizes of the components are not all the same we choose to create exact-match // textures for the smaller onces rather than add a texture domain to the draw. // TODO: revisit this decision to imporve texture reuse? SkBackingFit fit = (componentWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) || (componentHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight) ? SkBackingFit::kExact : SkBackingFit::kApprox; SkImageInfo imageInfo = SkImageInfo::MakeA8(componentWidth, componentHeight); SkPixmap pixmap(imageInfo, planes[i], yuvInfo.fSizeInfo.fWidthBytes[i]); SkCachedData* dataStoragePtr = dataStorage.get(); // We grab a ref to cached yuv data. When the SkImage we create below goes away it will call // the YUVGen_DataReleaseProc which will release this ref. // DDL TODO: Currently we end up creating a lazy proxy that will hold onto a ref to the // SkImage in its lambda. This means that we'll keep the ref on the YUV data around for the // life time of the proxy and not just upload. For non-DDL draws we should look into // releasing this SkImage after uploads (by deleting the lambda after instantiation). dataStoragePtr->ref(); sk_sp<SkImage> yuvImage = SkImage::MakeFromRaster(pixmap, YUVGen_DataReleaseProc, dataStoragePtr); auto proxyProvider = ctx->contextPriv().proxyProvider(); yuvTextureProxies[i] = proxyProvider->createTextureProxy(yuvImage, kNone_GrSurfaceFlags, kTopLeft_GrSurfaceOrigin, 1, SkBudgeted::kYes, fit); } // We never want to perform color-space conversion during the decode. However, if the proxy // config is sRGB then we must use a sRGB color space. sk_sp<SkColorSpace> colorSpace; if (GrPixelConfigIsSRGB(desc.fConfig)) { colorSpace = SkColorSpace::MakeSRGB(); } // TODO: investigate preallocating mip maps here sk_sp<GrRenderTargetContext> renderTargetContext(ctx->makeDeferredRenderTargetContext( SkBackingFit::kExact, desc.fWidth, desc.fHeight, desc.fConfig, std::move(colorSpace), desc.fSampleCnt, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin)); if (!renderTargetContext) { return nullptr; } GrPaint paint; auto yuvToRgbProcessor = GrYUVtoRGBEffect::Make(std::move(yuvTextureProxies[0]), std::move(yuvTextureProxies[1]), std::move(yuvTextureProxies[2]), yuvInfo.fSizeInfo.fSizes, yuvInfo.fColorSpace, false); paint.addColorFragmentProcessor(std::move(yuvToRgbProcessor)); // If we're decoding an sRGB image, the result of our linear math on the YUV planes is already // in sRGB. (The encoding is just math on bytes, with no concept of color spaces.) So, we need // to output the results of that math directly to the buffer that we will then consider sRGB. // If we have sRGB write control, we can just tell the HW not to do the Linear -> sRGB step. // Otherwise, we do our shader math to go from YUV -> sRGB, manually convert sRGB -> Linear, // then let the HW convert Linear -> sRGB. if (GrPixelConfigIsSRGB(desc.fConfig)) { if (ctx->caps()->srgbWriteControl()) { paint.setDisableOutputConversionToSRGB(true); } else { paint.addColorFragmentProcessor(GrSRGBEffect::Make(GrSRGBEffect::Mode::kSRGBToLinear, GrSRGBEffect::Alpha::kOpaque)); } } // If the caller expects the pixels in a different color space than the one from the image, // apply a color conversion to do this. std::unique_ptr<GrFragmentProcessor> colorConversionProcessor = GrNonlinearColorSpaceXformEffect::Make(srcColorSpace, dstColorSpace); if (colorConversionProcessor) { paint.addColorFragmentProcessor(std::move(colorConversionProcessor)); } paint.setPorterDuffXPFactory(SkBlendMode::kSrc); const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth, yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight); renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r); return renderTargetContext->asTextureProxyRef(); }